OrderController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace addons\StarChain\api\modules\v1\controllers;
  3. use addons\StarChain\common\enums\RefundTypeEnum;
  4. use addons\StarChain\common\enums\ShippingTypeEnum;
  5. use addons\StarChain\common\enums\StarChainEnum;
  6. use addons\StarChain\common\models\GoodsAttr;
  7. use addons\StarChain\common\models\MallOrder;
  8. use addons\StarChain\common\services\ChainOrderService;
  9. use addons\StarChain\common\services\RefundService;
  10. use api\controllers\OnAuthController;
  11. use common\enums\AppEnum;
  12. use common\enums\OrderStatusEnum;
  13. use common\enums\RefundStatusEnum;
  14. use common\enums\ShippingStatusEnum;
  15. use common\globals\GlobalInvoke;
  16. use common\logic\order\OrderFormLogic;
  17. use common\logic\order\OrderInitDataLogic;
  18. use common\logic\order\OrderLimitLogic;
  19. use common\logic\order\OrderMarketingLogic;
  20. use common\models\order\Order;
  21. use common\models\order\OrderDetail;
  22. use common\models\order\OrderRefund;
  23. use common\models\user\UserAddress;
  24. use Exception;
  25. use Yii;
  26. use yii\helpers\Json;
  27. /**
  28. * 默认控制器
  29. *
  30. * Class OrderController
  31. * @package addons\StarChain\api\modules\v1\controllers
  32. */
  33. class OrderController extends OnAuthController
  34. {
  35. public $modelClass = '';
  36. /**
  37. * 不用进行登录验证的方法
  38. *
  39. * 例如: ['index', 'update', 'create', 'view', 'delete']
  40. * 默认全部需要验证
  41. *
  42. * @var array
  43. */
  44. protected $authOptional = ['index'];
  45. /**
  46. * 订单预览
  47. *
  48. * @return string
  49. */
  50. public function actionPreview()
  51. {
  52. $orderFormLogic = new OrderFormLogic();
  53. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  54. $form->attributes = Yii::$app->request->post();
  55. $form['action'] = AppEnum::ACTION_PREVIEW;
  56. !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS;
  57. // 初始化订单数据
  58. $orderInitDataLogic = new OrderInitDataLogic();
  59. $form = $orderInitDataLogic->execute($form, $form->type);
  60. if ($form === false) {
  61. return $this->error($orderInitDataLogic->getError());
  62. }
  63. // 购买限制验证
  64. $orderLimitLogic = new OrderLimitLogic();
  65. $form = $orderLimitLogic->run($form);
  66. if ($form === false) {
  67. return $this->error($orderLimitLogic->getError());
  68. }
  69. // 获取收货地址
  70. $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id);
  71. $form->address_id = $address_info['id'] ?? 0;
  72. // 营销减免活动
  73. $orderMarketingLogic = new OrderMarketingLogic();
  74. $form = $orderMarketingLogic->run($form);
  75. if ($form === false) {
  76. return $this->error($orderMarketingLogic->getError());
  77. }
  78. // 聚合供应链处理
  79. $service = new ChainOrderService(['mall_id' => $this->mall_id]);
  80. try {
  81. $form = $service->createBeforeCheck($form);
  82. } catch (Exception $e) {
  83. return $this->error($e->getMessage());
  84. }
  85. $group_order_goods = array_values($form['group_order_goods']);
  86. $marketing = $form['marketing'];
  87. $pay_type_list = Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
  88. $shipping_type_list = ShippingTypeEnum::getMap();
  89. $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list');
  90. $info['total_pay_money'] = '¥' . $form['total_pay_money'];
  91. $info['is_address'] = $form['is_address'] ?? 0;
  92. return $this->success('操作成功', $info);
  93. }
  94. /**
  95. * 创建订单
  96. *
  97. * @return string
  98. */
  99. public function actionCreate()
  100. {
  101. $orderFormLogic = new OrderFormLogic();
  102. $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
  103. $form->attributes = Yii::$app->request->post();
  104. $form['action'] = AppEnum::ACTION_CREATE;
  105. // 初始化订单数据
  106. $orderInitDataLogic = new OrderInitDataLogic();
  107. $form = $orderInitDataLogic->execute($form, $form->type);
  108. if ($form === false) {
  109. return $this->error($orderInitDataLogic->getError());
  110. }
  111. // 购买限制验证
  112. $orderLimitLogic = new OrderLimitLogic();
  113. $form = $orderLimitLogic->run($form);
  114. if ($form === false) {
  115. return $this->error($orderLimitLogic->getError());
  116. }
  117. // 营销减免活动
  118. $orderMarketingLogic = new OrderMarketingLogic();
  119. $form = $orderMarketingLogic->run($form, true);
  120. if ($form === false) {
  121. return $this->error($orderMarketingLogic->getError());
  122. }
  123. // 聚合供应链处理
  124. $service = new ChainOrderService(['mall_id' => $this->mall_id]);
  125. try {
  126. $form = $service->createBeforeCheck($form, true);
  127. } catch (Exception $e) {
  128. return $this->error($e->getMessage());
  129. }
  130. $res = Order::createOrder($form);
  131. if ($res === false) {
  132. return $this->error(Order::getStaticError());
  133. }
  134. return $this->success('操作成功', $res);
  135. }
  136. /**
  137. * 加载提交售后订单数据
  138. *
  139. * @return string
  140. */
  141. public function actionToRefundSubmit()
  142. {
  143. $params = $this->request->get();
  144. $res = Yii::$app->services->validate->validate($params, [
  145. [['order_detail_id'], 'required'],
  146. ]);
  147. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  148. $select = 'id,order_id,goods_name,sign_id,goods_id,goods_attr_name,goods_attr_id,price,num,pic_url,goods_price,order_status,marketing_type,extra_info,adjust_money';
  149. $order_detail = OrderDetail::getOne([['id' => $params['order_detail_id']], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id], 1, [], false, $select);
  150. if (empty($order_detail)) return $this->error('订单不存在');
  151. $order = MallOrder::find()->where(['id' => $order_detail['order_id']])->select('id, order_no, shipping_money, order_source')->one();
  152. if ($order->order_source != StarChainEnum::PLUGIN_SIGN) return $this->error('该订单不是聚合供应链订单');
  153. // 公共调用
  154. $order_detail = GlobalInvoke::exec(GlobalInvoke::ORDER_REFUND_SUBMIT, $this->mall_id, [
  155. 'order_detail' => $order_detail,
  156. 'mall_id' => $this->mall_id,
  157. ]) ?: $order_detail;
  158. // 价格等于商品价格加上修改价格
  159. $order_detail['goods_price'] = round($order_detail['goods_price'] + $order_detail['adjust_money'], 2);
  160. $data['goods_info'] = $order_detail;
  161. $feedback_type = RefundTypeEnum::getRefundMap();
  162. $data['feedback_type'] = [];
  163. if (in_array($order_detail['order_status'], [OrderStatusEnum::PAY])) {
  164. // 待发货
  165. $data['feedback_type'][] = $feedback_type[RefundTypeEnum::MONEY];
  166. }
  167. if (in_array($order_detail['order_status'], [OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING])) {
  168. // 已发货,已收货
  169. // $data['feedback_type'][] = $feedback_type[RefundTypeEnum::ONLY_MONEY];
  170. $data['feedback_type'][] = $feedback_type[RefundTypeEnum::MONEY_AND_PRODUCT];
  171. $data['feedback_type'][] = $feedback_type[RefundTypeEnum::EXCHANGE_GOODS];
  172. $data['feedback_type'][] = $feedback_type[RefundTypeEnum::ONLY_MONEY];
  173. }
  174. // 待发货添加邮费
  175. if ($order_detail['order_status'] == OrderStatusEnum::PAY) {
  176. $data['goods_info']['goods_price'] = bcadd($order_detail['goods_price'], $order->shipping_money, 2);
  177. }
  178. $data['order_status'] = $order_detail['order_status'];
  179. return $this->success('操作成功', $data);
  180. }
  181. /**
  182. * 提交售后数据
  183. *
  184. * @return string
  185. */
  186. public function actionDoRefundSubmit()
  187. {
  188. $params = $this->request->post();
  189. $res = Yii::$app->services->validate->validate($params, [
  190. [['order_detail_id', 'reason_id', 'type', 'remark'], 'required'],
  191. [['shipping_status', 'pic_list', 'reason', 'refund_price', 'remark', 'type'], 'safe'],
  192. ]);
  193. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  194. $pic_list = Json::decode($params['pic_list']);
  195. if (count($pic_list) > 4) return $this->error('上传图片不能超过4张');
  196. $params['user_id'] = $this->user_id;
  197. $params['mall_id'] = $this->mall_id;
  198. $where = [
  199. ['=', 'order_detail_id', $params['order_detail_id']],
  200. ['=', 'user_id', $this->user_id],
  201. ['=', 'mall_id', $this->mall_id],
  202. ['>', 'step_status', 0],
  203. ['<>', 'refund_status', RefundStatusEnum::REVOKE]
  204. ];
  205. $order_refund_model = OrderRefund::getOne($where, true);
  206. if ($order_refund_model) return $this->error('该订单商品已经申请过售后');
  207. $refund_service = new RefundService(['mall_id' => $this->mall_id]);
  208. $res = $refund_service->doRefund($params);
  209. if ($res === false) return $this->error($refund_service->error);
  210. return $this->success('操作成功', ['id' => $res->id]);
  211. }
  212. /**
  213. * 聚合供应链售后上传售后截图
  214. *
  215. * @return void
  216. */
  217. public function actionUpload()
  218. {
  219. // 走主商城
  220. return $this->error('功能开发中');
  221. }
  222. /**
  223. * 填写物流单号
  224. *
  225. * @return void
  226. */
  227. public function actionRefundSend()
  228. {
  229. // 走主商城
  230. return $this->error('功能开发中');
  231. }
  232. /**
  233. * 物流编号
  234. *
  235. * @return void
  236. */
  237. public function actionExpressList()
  238. {
  239. // 走主商城
  240. return $this->error('功能开发中');
  241. }
  242. }