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.

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