You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
5.8 KiB

  1. <?php
  2. namespace backend\modules\shop\controllers;
  3. use api\logic\WxPaymentLogic;
  4. use backend\models\shop\logic\payment\WxPaymentManager;
  5. use backend\modules\shop\logic\delivery\DeliveryManager;
  6. use backend\modules\shop\models\ars\Delivery;
  7. use backend\modules\shop\models\ars\RefundLog;
  8. use Yii;
  9. use backend\modules\shop\models\ars\Order;
  10. use backend\modules\shop\models\searchs\OrderSearch;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. /**
  15. * OrderController implements the CRUD actions for Order model.
  16. */
  17. class OrderController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['POST'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all Order models.
  35. * @return mixed
  36. */
  37. public function actionIndex()
  38. {
  39. $searchModel = new OrderSearch();
  40. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  41. return $this->render('index', [
  42. 'searchModel' => $searchModel,
  43. 'dataProvider' => $dataProvider,
  44. 'columns' => $searchModel->columns()
  45. ]);
  46. }
  47. /**
  48. * Displays a single Order model.
  49. * @param integer $id
  50. * @return mixed
  51. * @throws NotFoundHttpException if the model cannot be found
  52. */
  53. public function actionView($id)
  54. {
  55. return $this->render('view', [
  56. 'model' => $this->findModel($id),
  57. ]);
  58. }
  59. /**
  60. * Creates a new Order model.
  61. * If creation is successful, the browser will be redirected to the 'view' page.
  62. * @return mixed
  63. */
  64. public function actionCreate()
  65. {
  66. $model = new Order();
  67. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  68. return $this->redirect('index');
  69. }
  70. return $this->render('create', [
  71. 'model' => $model,
  72. ]);
  73. }
  74. /**
  75. * Updates an existing Order model.
  76. * If update is successful, the browser will be redirected to the 'view' page.
  77. * @param integer $id
  78. * @return mixed
  79. * @throws NotFoundHttpException if the model cannot be found
  80. */
  81. public function actionUpdate($id)
  82. {
  83. $model = $this->findModel($id);
  84. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  85. return $this->redirect('index');
  86. }
  87. return $this->render('update', [
  88. 'model' => $model,
  89. ]);
  90. }
  91. /**
  92. * @param $id
  93. * @return \yii\web\Response
  94. * @throws NotFoundHttpException
  95. * @throws \Throwable
  96. * @throws \yii\db\StaleObjectException
  97. */
  98. public function actionDelete($id)
  99. {
  100. $this->findModel($id)->delete();
  101. return $this->redirect(['index']);
  102. }
  103. /**
  104. * Finds the Order model based on its primary key value.
  105. * If the model is not found, a 404 HTTP exception will be thrown.
  106. * @param integer $id
  107. * @return Order the loaded model
  108. * @throws NotFoundHttpException if the model cannot be found
  109. */
  110. protected function findModel($id)
  111. {
  112. if (($model = Order::findOne($id)) !== null) {
  113. return $model;
  114. }
  115. throw new NotFoundHttpException('The requested page does not exist.');
  116. }
  117. /**
  118. * @author iron
  119. * 文件导出
  120. */
  121. public function actionExport()
  122. {
  123. $searchModel = new OrderSearch();
  124. $params = Yii::$app->request->queryParams;
  125. if ($params['page-type'] == 'all') {
  126. $dataProvider = $searchModel->allData($params);
  127. } else {
  128. $dataProvider = $searchModel->search($params);
  129. }
  130. \iron\widget\Excel::export([
  131. 'models' => $dataProvider->getModels(),
  132. 'format' => 'Xlsx',
  133. 'asAttachment' => true,
  134. 'fileName' =>'Orders'. "-" .date('Y-m-d H/i/s', time()),
  135. 'columns' => $searchModel->columns()
  136. ]);
  137. }
  138. /**
  139. * @param $id
  140. * @return string|\yii\web\Response
  141. * @throws NotFoundHttpException
  142. * 订单发货
  143. */
  144. public function actionDelivery($id)
  145. {
  146. $order = $this->findModel($id);
  147. $delivery = new Delivery();
  148. if (Yii::$app->request->isPost) {
  149. $res = DeliveryManager::orderDelivery($order, $delivery);
  150. if ($res['status']) {
  151. return $this->redirect(['index']);
  152. } else {
  153. Yii::$app->session->setFlash('error', $res['info']);
  154. }
  155. }
  156. /*获取发货商品信息(包括已发货和未发货)*/
  157. $deliveryGoods = DeliveryManager::deliveryGoodsInfo($id);
  158. return $this->render('delivery', [
  159. 'order' => $order,
  160. 'delivery' => $delivery,
  161. 'deliveryGoods' => $deliveryGoods
  162. ]);
  163. }
  164. /**
  165. * @param $id
  166. * @return string|\yii\web\Response
  167. * @throws \yii\base\InvalidConfigException
  168. * @var $wxPayment
  169. */
  170. public function actionRefund($id)
  171. {
  172. $model = RefundLog::findOne([
  173. 'order_id' => Order::findOne($id)->order_sn,
  174. 'status' => WxPaymentLogic::STATUS_REFUND_WAIT
  175. ]);
  176. if (Yii::$app->request->post()) {
  177. $wxPayment = Yii::createObject([
  178. 'class' => 'backend\modules\payment\logic\WxPaymentManager'
  179. ]);
  180. $res = $wxPayment->refund($model->order_id, $model->refund_amount, Yii::$app->user->id);
  181. if ($res['status']) {
  182. return $this->redirect(['index']);
  183. } else {
  184. Yii::$app->session->setFlash('error', $res['info']);
  185. }
  186. }
  187. return $this->render('refund', [
  188. 'model' => $model,
  189. ]);
  190. }
  191. }