OrderController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. namespace addons\Happiness\agent\controllers;
  3. use addons\Printer\common\models\Printer;
  4. use addons\Store\common\enums\StoreEnum;
  5. use addons\Store\common\events\order\OrderRefundFinishEvent;
  6. use backend\modules\mall\services\RefundService;
  7. use common\enums\ApiStatusEnum;
  8. use common\enums\ExpressEnum;
  9. use common\enums\GoodsTypeEnum;
  10. use common\enums\MarketingTypeEnum;
  11. use common\enums\OrderSourceEnum;
  12. use common\enums\OrderStatusEnum;
  13. use common\enums\PayTypeEnum;
  14. use common\enums\RefundReasonEnum;
  15. use common\enums\RefundStatusEnum;
  16. use common\enums\RefundTypeEnum;
  17. use common\enums\ShippingTypeEnum;
  18. use common\enums\StatusEnum;
  19. use common\enums\UserWalletEnum;
  20. use common\helpers\LockHelper;
  21. use common\logic\order\OrderRefundLogic;
  22. use common\models\common\Express;
  23. use common\models\common\MallSetting;
  24. use common\models\goods\Goods;
  25. use common\models\mall\MallAddons;
  26. use common\models\order\Order;
  27. use common\models\order\OrderDetail;
  28. use common\models\order\OrderAction;
  29. use common\models\order\OrderExpress;
  30. use common\models\order\OrderRefund;
  31. use common\models\user\User;
  32. use Exception;
  33. use Yii;
  34. use yii\helpers\Json;
  35. use addons\Happiness\common\enums\HappinessEnum;
  36. use addons\Happiness\common\models\HappinessClerkLog;
  37. use addons\Happiness\common\models\HappinessOrder;
  38. use addons\Happiness\common\models\HappinessOrderRefund;
  39. use addons\Happiness\common\models\HappinessStore;
  40. use addons\Happiness\common\service\StoreService;
  41. use common\enums\AddonsEnum;
  42. use common\models\order\OrderMarketing;
  43. class OrderController extends BaseController
  44. {
  45. public $enableCsrfValidation = false;
  46. /**
  47. * 订单管理
  48. * @Author bing
  49. * @DateTime 2022-04-26 18:07:05
  50. * @return void
  51. * @copyright: Copyright (c) 2022 广东七件事集团
  52. */
  53. public function actionIndex()
  54. {
  55. if (\Yii::$app->request->isAjax) {
  56. if (\Yii::$app->request->isGet) {
  57. //商品列表筛选
  58. $get = Yii::$app->request->get();
  59. $isReserve = $get['is_reserve'] ?? 0;
  60. $order_status_list = HappinessEnum::getMap();
  61. $payment_type_list = PayTypeEnum::getMap();
  62. $shipping_type_list = ShippingTypeEnum::getMap();
  63. $rules = [
  64. [['order_status'], 'in', 'range' => array_keys($order_status_list)],
  65. [['payment_type'], 'in', 'range' => array_keys($payment_type_list)],
  66. [['shipping_type'], 'in', 'range' => array_keys($shipping_type_list)],
  67. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  68. [['order_no', 'mobile', 'order_id'], 'safe'],
  69. [['type', 'type_value'], 'string'],
  70. [['is_reserve'],'in', 'range' => [0,1]],
  71. ];
  72. $res = Yii::$app->services->validate->validate($get, $rules);
  73. if (!$res) return $this->error(Yii::$app->services->validate->getErrorMessage());
  74. $where[] = ['=', 'o.mall_id', $this->mall_id];
  75. $where[] = ['or', ['in', 'st.city_id', $this->city_ids], ['in', 'st.district_id', $this->district_ids], ['in', 'st.province_id', $this->province_ids]];
  76. $where[] = ['>=', 'o.status', StatusEnum::DISABLED];
  77. if ($isReserve) {
  78. $where[] = ['=', 'o.order_source', OrderSourceEnum::SOURCE_RESERVE];
  79. } else {
  80. $where[] = ['<>', 'o.order_source', OrderSourceEnum::SOURCE_RESERVE];
  81. }
  82. if (isset($get['order_id'])) $where[] = ['in', 'o.id', explode(',', $get['order_id'])];
  83. ($get['payment_type'] ?? false) && $where[] = ['=', 'o.payment_type', $get['payment_type']];
  84. ($get['shipping_type'] ?? false) && $where[] = ['=', 'o.shipping_type', $get['shipping_type']];
  85. if (isset($get['order_status']) && $get['order_status'] != 999) {
  86. $where[] = ['=', 'o.order_status', $get['order_status']];
  87. }
  88. ($get['order_no'] ?? false) && $where[] = ['=', 'o.order_no', $get['order_no']];
  89. ($get['start'] ?? false) && $where[] = ['>=', 'o.created_at', strtotime($get['start'])];
  90. ($get['end'] ?? false) && $where[] = ['<=', 'o.created_at', strtotime($get['end'])];
  91. if ($get['type_value'] ?? false) {
  92. switch ($get['type']) {
  93. case 'mobile':
  94. $uids = User::find()->select('id')->where(['like', 'mobile', $get['type_value'] . '%', false])->column();
  95. !empty($uids) && $where[] = ['in', 'o.user_id', $uids];
  96. break;
  97. case 'user_id':
  98. $where[] = ['in', 'o.user_id', $get['type_value']];
  99. break;
  100. default:
  101. }
  102. }
  103. $alias = 'yo';
  104. $with = ['base_user', 'detail','reserve'];
  105. $order = "id desc";
  106. $join = [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"],['LEFT JOIN',
  107. HappinessStore::tableName(). ' as st', "yo.store_id = st.id"]];
  108. $select = 'o.*,yo.store_id,st.city_id,st.district_id';
  109. $params = compact('where', 'with', 'order', 'alias', 'join', 'select');
  110. $page_data = HappinessOrder::listPage($params, false, true);
  111. if ($page_data === false) return $this->error(Goods::getStaticError());
  112. $compact_params = array_keys($page_data);
  113. $order_no_arr = array_column($page_data['list'],'order_no');
  114. $order_list = Order::lists(['where'=>[['order_no'=>$order_no_arr]],'index'=>'order_no','select'=>'id,order_no']);
  115. extract($page_data);
  116. $store_order_detail_ids = [];
  117. foreach ($page_data['list'] as $val){
  118. foreach ($val['detail'] as $value) {
  119. $store_order_detail_ids[]=$value['id'];
  120. }
  121. }
  122. $store_order_marketing_list = OrderMarketing::lists(['where'=>[['order_detail_id'=>$store_order_detail_ids]],'index'=>'order_detail_id']);
  123. $is_show_printer = 0;
  124. foreach ($list as &$order) {
  125. $order_expresses = OrderExpress::getOrderExpresses([['order_id' => $order['id']]]);
  126. $order['main_order_id'] = isset($order_list[$order['order_no']]['id']) ? $order_list[$order['order_no']]['id'] : 0;
  127. $order['Printer']['is_show_printer'] = $is_show_printer;
  128. $deduct_score = 0;
  129. $deduct_score_money = 0;
  130. foreach ($order['detail'] as &$detail) {
  131. // 物流信息
  132. $detail['express'] = '';
  133. foreach ($order_expresses as $record) {
  134. $order_detail_ids = empty($record['order_detail_ids']) ? [] : json_decode($record['order_detail_ids'], true);
  135. if (is_string($order_detail_ids)) $order_detail_ids = [];
  136. if (in_array($detail['id'], $order_detail_ids)) {
  137. $detail['express'] = $record['express_name'] . ' | ' . $record['express_no'];
  138. if ($record['shipping_type'] == 0) $detail['express'] = '--';
  139. }
  140. }
  141. if(!empty($store_order_marketing_list[$detail['id']])){
  142. $deduct_currency = json_decode($store_order_marketing_list[$detail['id']]['deduct_currency'],true);
  143. if(!empty($deduct_currency['score'])){
  144. $deduct_score+= $deduct_currency['score']['score'];
  145. $deduct_score_money+= $deduct_currency['score']['money'];
  146. }
  147. }
  148. }
  149. $order['score_deduct'] = '';
  150. if(!empty($deduct_score)&&!empty($deduct_score_money)){
  151. $deduct_score = bcadd($deduct_score,0,2);
  152. $deduct_score_money = bcadd($deduct_score_money,0,2);
  153. $order['score_deduct'] = "{$deduct_score}积分抵扣{$deduct_score_money}元";
  154. }
  155. $order['create_type_text'] = $order['create_type'] == 1 ? '线上下单' : '系统创建';
  156. }
  157. $marketing_type_map = MarketingTypeEnum::getMap();
  158. return $this->success('操作成功', compact($compact_params, 'order_status_list', 'payment_type_list', 'shipping_type_list', 'marketing_type_map'));
  159. }
  160. }
  161. return $this->render($this->action->id);
  162. }
  163. /**
  164. * 售后订单
  165. * @Author bing
  166. * @DateTime 2022-04-26 18:07:16
  167. * @return void
  168. * @copyright: Copyright (c) 2022 广东七件事集团
  169. */
  170. public function actionRefund()
  171. {
  172. if (\Yii::$app->request->isAjax) {
  173. if (\Yii::$app->request->isGet) {
  174. //商品列表筛选
  175. $get = Yii::$app->request->get();
  176. $refund_status_list = RefundStatusEnum::getMap();
  177. $refund_type_list = RefundTypeEnum::getMap();
  178. $rules = [
  179. [['is_feedback', 'goods_type'], 'integer'],
  180. [['keyword', 'order_no', 'type'], 'string'],
  181. [['type', 'order_no', 'keyword', 'refund_status'], 'safe'],
  182. ];
  183. $res = Yii::$app->services->validate->validate($get, $rules);
  184. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  185. $where = [['o.mall_id' => $this->mall_id]];
  186. $where[] = ['=', 'yo.store_id', $this->store_id];
  187. $where[] = ['=', 'o.status', StatusEnum::ENABLED];
  188. if (!empty($get['goods_type'])) $where[] = ['o.goods_type' => $get['goods_type']];
  189. // 搜索查询
  190. !empty($get['order_no']) && $where[] = ['in', 'o.order_id', OrderRefund::getOidByOrderNo($this->mall_id, $get['order_no'])];
  191. if (isset($get['refund_status'])) {
  192. $where[] = ['=', 'o.refund_status', $get['refund_status']];
  193. } else {
  194. $where[] = ['in', 'o.refund_status', [RefundStatusEnum::APPLY, RefundStatusEnum::HANDEL, RefundStatusEnum::FINISH, RefundStatusEnum::RETURN_GOODS, RefundStatusEnum::EXCHANGE_GOODS, RefundStatusEnum::REFUSE]];
  195. }
  196. if (!empty($get['type']) && !empty($get['keyword'])) $where[] = HappinessOrderRefund::searchByKeyword($this->mall_id, $get['type'], $get['keyword']);
  197. $alias = 'yo';
  198. $with = ['base_user', 'orderDetail', 'order'];
  199. $order = "id desc";
  200. $join = [['LEFT JOIN', OrderRefund::tableName(). ' as o', "o.id = yo.refund_id"]];
  201. $select = 'o.*,yo.store_id';
  202. $params = compact('where', 'with', 'order', 'alias', 'join', 'select');
  203. $page_data = HappinessOrderRefund::listPage($params, false, true);
  204. if ($page_data === false) return $this->error(OrderRefund::getStaticError());
  205. $compact_params = array_keys($page_data);
  206. extract($page_data);
  207. foreach ($list as &$order) {
  208. foreach ($order['orderDetail'] as &$detail) {
  209. if (!empty($detail['attr_groups_format'])) {
  210. //规格
  211. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  212. $attr_list = $attr_groups_format[$detail['sign_id']];
  213. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  214. $attr_name_list = array_column($attr_list, 'attr_name');
  215. $detail['attr_list'] = [];
  216. foreach ($attr_group_name_list as $index => $attr_group_name) {
  217. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  218. }
  219. }
  220. }
  221. }
  222. return $this->success('操作成功', compact($compact_params, 'refund_status_list', 'refund_type_list'));
  223. }
  224. }
  225. return $this->render($this->action->id, ['goods_type' => Json::encode(GoodsTypeEnum::getMap())]);
  226. }
  227. public function actionRefundDetail()
  228. {
  229. if (\Yii::$app->request->isAjax) {
  230. if (\Yii::$app->request->isGet) {
  231. //商品列表筛选
  232. $get = Yii::$app->request->get();
  233. $order_status_list = HappinessEnum::getMap();
  234. $payment_type_list = PayTypeEnum::getMap();
  235. $feedback_type_list = OrderStatusEnum::getFeedbackMap();
  236. // $address_list = Yii::$app->services->setting->mall->get($this->mall_id, 'return_address.return_address');
  237. // $address_list = $address_list ? json_decode($address_list, true) : [];
  238. $address_list = Yii::$app->services->setting->mall->get($this->mall_id, 'return_address.return_address');
  239. $address_list = $address_list ? json_decode($address_list, true) : [];
  240. $rules = [[['id'], 'required']];
  241. $res = Yii::$app->services->validate->validate($get, $rules);
  242. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  243. $where = [['mall_id' => $this->mall_id]];
  244. $where[] = ['=', 'id', $get['id']];
  245. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  246. $with = ['base_user', 'order', 'orderDetail', 'marketing', 'step'];
  247. $params = compact('where', 'with');
  248. $refund = OrderRefund::getOne($where, true, $with);
  249. if ($refund === false) return $this->error(Goods::getStaticError());
  250. $main_order = Order::findOne(['id'=>$refund['order_id']]);
  251. $store_order = HappinessOrder::findOne(['order_no'=>$main_order['order_no']??0]);
  252. // $store_order_credit = StoreOrderCredit::find()->where(['type'=>1,'order_id'=>$store_order['id']??0])
  253. // ->andWhere(['>','pay_time',0])->one();
  254. // $refund['orderDetail']['credit_money']= 0;
  255. // if(!empty($store_order_credit['extra'])){
  256. // $goods_id = $refund['orderDetail']['goods_id'];
  257. // $extra = json_decode($store_order_credit['extra'],true);
  258. // if(!empty($extra[$goods_id]['credit_money'])){
  259. // $refund['orderDetail']['goods_price'] = bcadd($refund['orderDetail']['goods_price'],$extra[$goods_id]['credit_money'],2);
  260. // $refund['orderDetail']['credit_money'] = $extra[$goods_id]['credit_money'];
  261. // }
  262. // }
  263. foreach ($refund['orderDetail'] as $key => $detail) {
  264. if (!empty($detail['attr_groups_format'])) {
  265. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  266. $attr_list = $attr_groups_format[$detail['sign_id']];
  267. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  268. $attr_name_list = array_column($attr_list, 'attr_name');
  269. $detail['attr_list'] = [];
  270. }
  271. }
  272. // 返还优惠内容
  273. if (!empty($refund['marketing'])) {
  274. $discount = [];
  275. $marketing = $refund['marketing'];
  276. if (!empty($marketing['deduct_score'])) {
  277. $score_name = Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name');
  278. $discount[] = floatval($marketing['deduct_score']) . $score_name;
  279. }
  280. $refund['discount'] = implode('+', $discount);
  281. }
  282. $step_status = $refund['step'][0]['step_status'] ?? 0;
  283. $step_arr = RefundStatusEnum::stepNumArr(in_array($step_status, [RefundStatusEnum::STEP_NEGATIVE_1, RefundStatusEnum::STEP_NEGATIVE_2]) ? 1 : $refund['type']);// 进度条
  284. $step_text = RefundStatusEnum::getStepStatusText($step_status);
  285. // 五洲供应链判断是否能操作
  286. // 是否显示退货超期,撤销售后
  287. $is_show_revoce = (OrderRefund::findOne(['id' => $refund['id']]))->isReturnTimeoutOfRefund();
  288. $refund['option_limit'] = $refund['order']['order_source'] = 0;
  289. return $this->success('操作成功', compact('refund', 'order_status_list', 'payment_type_list', 'feedback_type_list', 'step_arr', 'address_list', 'step_text', 'is_show_revoce'));
  290. }
  291. }
  292. return $this->render($this->action->id);
  293. }
  294. /**
  295. * 订单详情
  296. * @Author bing
  297. * @DateTime 2021-09-06 14:09:56
  298. * @return void
  299. * @copyright: Copyright (c) 2020 广东七件事集团
  300. */
  301. public function actionDetail()
  302. {
  303. if (\Yii::$app->request->isAjax) {
  304. if (\Yii::$app->request->isGet) {
  305. //商品列表筛选
  306. $get = Yii::$app->request->get();
  307. $order_status_list = HappinessEnum::getMap();
  308. $payment_type_list = PayTypeEnum::getMap();
  309. $shipping_type_list = ShippingTypeEnum::getMap();
  310. $rules = [[['id'], 'required']];
  311. $res = Yii::$app->services->validate->validate($get, $rules);
  312. $where = [['mall_id' => $this->mall_id]];
  313. $where[] = ['=', 'id', $get['id']];
  314. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  315. $with = [
  316. 'base_user',
  317. 'detail',
  318. 'reserve',
  319. 'express',
  320. 'express_log',
  321. ];
  322. $params = compact('where', 'with');
  323. $order = Order::getOne($where, true, $with);
  324. if ($order === false) return $this->error(Goods::getStaticError());
  325. // $main_order = Order::getOne([['order_no'=>$order['order_no']]],true,['express','express_log']);
  326. $express_log = [];
  327. if ($order['express_log']) {
  328. foreach ($order['express_log'] as $item) {
  329. $item['status'] = ExpressEnum::getStatusMap($item['status']);
  330. $express_log[$item['order_express_id']] = $item;
  331. }
  332. }
  333. if (!empty($order['express'])) {
  334. foreach ($order['express'] as &$key) {
  335. if (isset($express_log[$key['id']])) {
  336. $express_list = Json::decode($express_log[$key['id']]['list']);
  337. $key['now_state'] = $express_log[$key['id']]['status'];
  338. $key['express_log'] = is_null($express_list) ? '' : array_reverse($express_list['Traces']);
  339. }
  340. }
  341. }
  342. // $order['express'] = $main_order['express'] ?? [];
  343. if (!empty($order['detail'])) {
  344. foreach ($order['detail'] as &$detail) {
  345. if (!empty($detail['attr_groups_format'])) {
  346. $attr_groups_format = json_decode($detail['attr_groups_format'], true);
  347. $attr_list = $attr_groups_format[$detail['sign_id']];
  348. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  349. $attr_name_list = array_column($attr_list, 'attr_name');
  350. $detail['attr_list'] = [];
  351. foreach ($attr_group_name_list as $index => $attr_group_name) {
  352. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  353. }
  354. }
  355. }
  356. }
  357. $shipping_type_text = ShippingTypeEnum::getShippingType($order['shipping_type']);
  358. $shipping_type_fields = ShippingTypeEnum::getShippingFields($order['shipping_type']);
  359. $step_arr = HappinessEnum::setpArrMap($order['order_status'], $shipping_type_text,$shipping_type_fields);// 订单状态跟踪
  360. $order_action = OrderAction::getOrderAction([['order_id' => $order['id']]]);
  361. $marketing_type_map = MarketingTypeEnum::getMap();
  362. $stepArr[] = ['title' => '买家下单', 'created_at' => '0', 'is_step' => 0];
  363. $stepArr[] = ['title' => '买家付款', 'created_at' => '0', 'is_step' => 0,];
  364. $stepArr[] = ['title' => $shipping_type_text, 'created_at' => '0', 'is_step' => 0,];
  365. $stepArr[] = ['title' => '交易完成', 'created_at' => '0', 'is_step' => 0,];
  366. if(!empty($step_arr)){
  367. foreach ($step_arr as $k=>$v){
  368. if(!in_array($v['field'],['deposite_pay_time','final_payment_time'])){
  369. if($order['shipping_type'] == ShippingTypeEnum::PICKUP){
  370. if($v['field'] == 'sign_time'){
  371. $step_arr[$k]['title'] = '买家自提';
  372. }
  373. }
  374. }
  375. }
  376. }
  377. //获取售后原因
  378. $refund_reason = RefundReasonEnum::getMap();
  379. $order['create_type_text'] = $order['create_type'] == 1 ? '线上下单' : '系统创建';
  380. $order['option_limit'] = intval($order['supplier_type'] == 4);
  381. return $this->success('操作成功', compact('order', 'stepArr', 'order_status_list', 'payment_type_list',
  382. 'shipping_type_list', 'step_arr', 'order_action', 'marketing_type_map', 'refund_reason'), ApiStatusEnum::CODE_SUCCESS, JSON_BIGINT_AS_STRING);
  383. }
  384. }
  385. return $this->render($this->action->id);
  386. }
  387. /**
  388. * 修改备注
  389. * @Author bing
  390. * @DateTime 2021-09-14 18:40:15
  391. * @return void
  392. * @copyright: Copyright (c) 2020 广东七件事集团
  393. */
  394. public function actionRemark()
  395. {
  396. $form = Yii::$app->request->post();
  397. $rules = [[['id'], 'required'], [['id'], 'integer'], [['remark', 'seller_remark'], 'string']];
  398. $res = Yii::$app->services->validate->validate($form, $rules);
  399. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  400. $form['mall_id'] = $this->mall_id;
  401. $form['store_id'] = $this->store_id;
  402. $form['operator_id'] = $this->admin->id ?? 0;
  403. $form['operator_name'] = $this->admin->account ?? '';
  404. $res = Order::remark($form);
  405. if ($res === false) return $this->error(Goods::getStaticError());
  406. return $this->success();
  407. }
  408. /**
  409. * 发货
  410. * @Author bing
  411. * @DateTime 2021-09-15 14:29:48
  412. * @return void
  413. * @copyright: Copyright (c) 2020 广东七件事集团
  414. */
  415. public function actionShipping()
  416. {
  417. if (\Yii::$app->request->isAjax) {
  418. if (\Yii::$app->request->isGet) {
  419. // 获取订单发货信息
  420. $jd_mch_code = MallSetting::conf($this->mall_id, 'logistics.jd_mch_code');
  421. $express_list = Express::getExpresses([], [], true, '*');
  422. return $this->success('请求成功', compact('express_list', 'jd_mch_code'));
  423. } else {
  424. // 发货
  425. $form = Yii::$app->request->post();
  426. $rules = [
  427. [['shipping_type', 'id'], 'required'],
  428. [['express_id', 'shipping_type', 'id'], 'integer'],
  429. [['express_name', 'express_no', 'memo', 'memo_arr1', 'memo_arr2', 'memo_arr3'], 'string'],
  430. [['order_detail_ids'], 'each', 'rule' => ['integer']],
  431. [['shipping_change', 'customer_name'], 'safe']
  432. ];
  433. $res = Yii::$app->services->validate->validate($form, $rules);
  434. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  435. $form['mall_id'] = $this->mall_id;
  436. $form['store_id'] = $this->store_id;
  437. $form['operator_id'] = $this->admin->id ?? 0;
  438. $form['operator_name'] = $this->admin->account ?? '';
  439. $form['memo'] = json_encode([$form['memo_arr1'],$form['memo_arr2'],$form['memo_arr3']],JSON_UNESCAPED_UNICODE);
  440. if (empty($form['shipping_change'])) {
  441. $res = HappinessOrder::OrderExpressDelivery($form);
  442. }
  443. if ($res === false) return $this->error(Goods::getStaticError());
  444. return $this->success('发货/修改物流成功');
  445. }
  446. }
  447. }
  448. /**
  449. * 收货
  450. * @param $id
  451. * @param string $member_id
  452. * @throws UnprocessableEntityHttpException
  453. * @throws \yii\web\NotFoundHttpException
  454. */
  455. public function actionTakeDelivery()
  456. {
  457. // 商品操作
  458. $form = Yii::$app->request->post();
  459. $rules = [[['id'], 'required'], [['id'], 'integer']];
  460. $res = Yii::$app->services->validate->validate($form, $rules);
  461. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  462. $form['mall_id'] = $this->mall_id;
  463. $form['store_id'] = $this->store_id;
  464. $form['operator_id'] = $this->admin->id;
  465. $form['operator_name'] = $this->admin->account;
  466. $res = HappinessOrder::takeDelivery($form);
  467. if ($res === false) return $this->error(HappinessOrder::getStaticError());
  468. return $this->success();
  469. }
  470. /**
  471. * 完成订单
  472. * @param $id
  473. * @throws \yii\web\NotFoundHttpException
  474. */
  475. public function actionSalesOrder()
  476. {
  477. // 商品操作
  478. $form = Yii::$app->request->post();
  479. $rules = [[['id'], 'required'], [['id'], 'integer']];
  480. $res = Yii::$app->services->validate->validate($form, $rules);
  481. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  482. $form['mall_id'] = $this->mall_id;
  483. // $form['store_id'] = $this->store_id;
  484. $form['operator_id'] = $this->admin->id ?? 0;
  485. $form['operator_name'] = $this->admin->account ?? '';
  486. $res = Order::complete($form);
  487. if ($res == false) return $this->error('操作订单失败:' . Order::getStaticError());
  488. return $this->success('操作成功');
  489. }
  490. /**
  491. * 修改收货地址
  492. * @Author bing
  493. * @DateTime 2021-09-14 18:40:15
  494. * @return void
  495. * @copyright: Copyright (c) 2020 广东七件事集团
  496. */
  497. public function actionChangeAddress()
  498. {
  499. $form = Yii::$app->request->post();
  500. $rules = [
  501. [['id', 'receiver_name', 'mobile', 'province', 'city', 'area', 'address', 'region_name'], 'required'],
  502. [['id', 'province', 'city', 'area', 'zip'], 'integer'],
  503. [['address', 'region_name', 'receiver_name', 'mobile'], 'string']
  504. ];
  505. $res = Yii::$app->services->validate->validate($form, $rules);
  506. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  507. $form['mall_id'] = $this->mall_id;
  508. $form['store_id'] = $this->store_id;
  509. $form['operator_id'] = $this->admin->id ?? 0;
  510. $form['operator_name'] = $this->admin->account ?? '';
  511. $form['is_store'] = 1;
  512. $res = Order::changeAddress($form);
  513. if ($res === false) return $this->error(Goods::getStaticError());
  514. return $this->success();
  515. }
  516. public function actionClose()
  517. {
  518. // 商品操作
  519. $form = Yii::$app->request->post();
  520. $rules = [[['id'], 'required'], [['id'], 'integer']];
  521. $res = Yii::$app->services->validate->validate($form, $rules);
  522. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  523. $form['mall_id'] = $this->mall_id;
  524. // $form['store_id'] = $this->store_id;
  525. $form['operator_id'] = $this->admin->id ?? 0;
  526. $form['operator_name'] = $this->admin->account ?? '';
  527. $res = Order::close($form);
  528. if ($res === false) return $this->error(Goods::getStaticError());
  529. return $this->success();
  530. }
  531. public function actionDel()
  532. {
  533. // 商品操作
  534. $form = Yii::$app->request->post();
  535. $rules = [[['id'], 'required'], [['id'], 'integer']];
  536. $res = Yii::$app->services->validate->validate($form, $rules);
  537. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  538. $form['mall_id'] = $this->mall_id;
  539. $form['store_id'] = $this->store_id;
  540. $form['operator_id'] = $this->admin->id ?? 0;
  541. $form['operator_name'] = $this->admin->account ?? '';
  542. $res = Order::del($form);
  543. if ($res === false) return $this->error(Goods::getStaticError());
  544. return $this->success();
  545. }
  546. // public function actionPay()
  547. // {
  548. // // 商品操作
  549. // $form = Yii::$app->request->post();
  550. // $rules = [[['id'], 'required'], [['id'], 'integer']];
  551. // $res = Yii::$app->services->validate->validate($form, $rules);
  552. // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  553. // $form['mall_id'] = $this->mall['id'];
  554. // $form['store_id'] = $this->store_id;
  555. // $form['operator_id'] = $this->admin->id ?? 0;
  556. // $form['operator_name'] = $this->admin->account ?? '';
  557. // $res = StoreOrder::pay($form, PayTypeEnum::OFFLINE);
  558. // if ($res === false) return $this->error(Goods::getStaticError());
  559. // return $this->success();
  560. // }
  561. /**
  562. * 售后处理
  563. * @Author: lun
  564. * @DateTime: 2021/10/12 0012 11:15
  565. * @Copyright: copyright (c) 2021 广东七件事集团
  566. * @return mixed|void
  567. */
  568. public function actionHandleRefund()
  569. {
  570. if (\Yii::$app->request->isAjax) {
  571. if (\Yii::$app->request->isPost) {
  572. //商品列表筛选
  573. $params = Yii::$app->request->post();
  574. $rules = [
  575. [['id', 'step_status'], 'required'],
  576. [['id', 'step_status', 'mobile', 'address_type'], 'integer'],
  577. [['saler_express', 'saler_express_no', 'address', 'consignee', 'refund_reason'], 'string'],
  578. [['saler_express', 'saler_express_no', 'address_type', 'customer_name'], 'safe'],
  579. ];
  580. $res = Yii::$app->services->validate->validate($params, $rules);
  581. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  582. // 退货地址
  583. $address = ['consignee' => $params['consignee'], 'mobile' => $params['mobile'], 'address' => $params['address']];
  584. // 换货物流信息
  585. $express = ['saler_express' => $params['saler_express'], 'saler_express_no' => $params['saler_express_no'], 'customer_name' => $params['customer_name']];
  586. $data = [
  587. 'id' => $params['id'],
  588. 'step_status' => $params['step_status'],
  589. 'address' => $address,
  590. 'express' => $express,
  591. 'refund_reason' => $params['refund_reason'] ?? '',
  592. 'is_store' => 1,
  593. 'operator_id' => $this->admin->id
  594. ];
  595. $logic = new OrderRefundLogic();
  596. $res = $logic->execute($this->mall_id, $data);
  597. if ($res == false) return $this->error($logic->getError());
  598. if (in_array($params['step_status'], [RefundStatusEnum::STEP_4, RefundStatusEnum::STEP_6]) && empty($params['is_async'])) {
  599. //触发订单售后完成
  600. $event = new OrderRefundFinishEvent($res['mall_id']);
  601. $data = [];
  602. $data['id'] = $res['id'];
  603. $data['source_table'] = 'order_detail';
  604. $data['source_table_id'] = $res['order_detail_id'];
  605. $data['step_status'] = $params['step_status'];
  606. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  607. }
  608. return $this->success('操作成功');
  609. }
  610. }
  611. }
  612. public function actionClerk(){
  613. $params = Yii::$app->request->post();
  614. $params['mall_id'] = $this->mall_id;
  615. $params['store_id'] = $this->store_id;
  616. // $params['clerk_code'] = $params['id'];
  617. $store = HappinessStore::findOne(['id'=>$this->store_id,'status'=>StatusEnum::ENABLED]);
  618. $params['clerk_id'] = (new StoreService(['mall_id' => $this->mall_id, 'store_id' => $this->store_id]))->getClerkIdByUserId($store['user_id']??0, true);
  619. $rse = HappinessClerkLog::setData($params);
  620. if($rse==false) return $this->error(HappinessClerkLog::getStaticError());
  621. return $this->success('操作成功');
  622. }
  623. public function actionReserve()
  624. {
  625. $is_show_username = Yii::$app->services->setting->mall->get($this->mall_id, 'order.is_show_username');
  626. return $this->render($this->action->id, ['is_show_username' => $is_show_username ?? 0]);
  627. }
  628. public function actionReserveDetail()
  629. {
  630. return $this->render($this->action->id, ['is_show_username' => $is_show_username ?? 0]);
  631. }
  632. /**
  633. * 撤销售后订单
  634. *
  635. * @return mixed
  636. */
  637. public function actionRevoceRefund(int $id)
  638. {
  639. $service = new RefundService(['mall_id' => $this->mall_id]);
  640. return $service->revoceRefund($id, ['is_store' => 1, 'operator_id' => $this->admin->id])
  641. ? $this->success('撤销售成功')
  642. : $this->error($service->getError());
  643. }
  644. public function actionGetOrderDetail()
  645. {
  646. $params = \Yii::$app->request->get();
  647. $res = \Yii::$app->services->validate->validate($params, [
  648. [['order_detail_id'], 'required'],
  649. ]);
  650. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  651. $select = 'id,order_id,user_id,goods_name,sign_id,goods_attr_name,price,num,pic_url,goods_price,order_status,marketing_type,extra_info,adjust_money';
  652. $order_detail = OrderDetail::getOne([['id' => $params['order_detail_id']], 'mall_id' => $this->mall_id], 1, [], false, $select);
  653. if (empty($order_detail)) return $this->error('订单不存在');
  654. //价格等于商品价格加上修改价格
  655. $order_detail['goods_price'] = round($order_detail['goods_price'] + $order_detail['adjust_money'],2);
  656. return $this->success('操作成功',[ $order_detail]);
  657. }
  658. public function actionDoRefundSubmit()
  659. {
  660. $params = \Yii::$app->request->post();
  661. $res = \Yii::$app->services->validate->validate($params, [
  662. [['order_detail_id', 'reason', 'refund_price', 'settlement_method', 'balance_pre'], 'required'],
  663. [['shipping_status', 'pic_list', 'reason', 'refund_price', 'remark', 'type', 'user_id', 'settlement_method', 'balance_pre', 'main_order_detail_id'], 'safe'],
  664. ]);
  665. // if (empty($params['reason']))
  666. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  667. $key = sprintf('DoRefundSubmit:%s', $params['user_id']);
  668. if (LockHelper::fLock($key)) return $this->error('请勿频繁请求');
  669. // 获取主商城订单详细
  670. $main_order_detail = OrderDetail::find()
  671. ->where(['mall_id' => $this->mall_id, 'id' => $params['order_detail_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  672. ->one();
  673. if (empty($main_order_detail)) return $this->error('订单不存在');
  674. $params['mall_id'] = $this->mall_id;
  675. $where = [
  676. ['=', 'order_detail_id', $main_order_detail['id']],
  677. ['=', 'user_id', $params['user_id']],
  678. ['=', 'mall_id', $this->mall_id],
  679. // ['=', 'store_id', $this->store_id],
  680. ['>', 'step_status', 0],
  681. ['<>', 'refund_status', RefundStatusEnum::REVOKE]
  682. ];
  683. $order_refund_model = OrderRefund::getOne($where, true);
  684. if ($order_refund_model) {
  685. LockHelper::unFLock($key);
  686. return $this->error('该订单商品已经申请过售后');
  687. }
  688. // 将申请修改为主商城信息
  689. $params['order_detail_id'] = $main_order_detail['id'];
  690. $res = OrderRefund::feedback($params);
  691. if ($res === false) {
  692. LockHelper::unFLock($key);
  693. return $this->error(Order::getStaticError());
  694. }
  695. $event = new \common\events\order\OrderRefundEvent();
  696. $event->mall_id = $this->mall_id;
  697. $order_detail['order_detail_id'] = $main_order_detail['id'];
  698. $order_detail['id'] = $res->id;
  699. $order_detail['operator_id'] = $this->admin->id;
  700. $order_detail['operator_name'] = $this->admin->account;
  701. $order_detail['is_admin'] = 1;
  702. $order_detail['admin_handler_refund'] = UserWalletEnum::ADMIN_HANDLER_REFUND;
  703. Yii::$app->services->events->dispatch($event, $order_detail, $event->listeners);
  704. LockHelper::unFLock($key);
  705. return $this->success('操作成功', ['id' => $res->id]);
  706. }
  707. }