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.

182 lines
4.9 KiB

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