OrderController.php 38 KB

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