StoreOrderController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. <?php
  2. namespace addons\Store\backend\controllers;
  3. use addons\Community\common\service\HeadService;
  4. use addons\Printer\common\models\Printer;
  5. use addons\Store\common\models\Store;
  6. use addons\Store\common\models\StoreGoodsAttr;
  7. use addons\Store\common\models\StoreGoodsExtra;
  8. use addons\Store\common\models\StoreOrder;
  9. use addons\Store\common\models\StoreOrderDetail;
  10. use common\enums\ExpressEnum;
  11. use common\enums\GoodsTypeEnum;
  12. use common\enums\MarketingTypeEnum;
  13. use common\enums\OrderStatusEnum;
  14. use common\enums\PayTypeEnum;
  15. use common\enums\RefundStatusEnum;
  16. use common\enums\RefundTypeEnum;
  17. use common\enums\ShippingTypeEnum;
  18. use common\enums\StatusEnum;
  19. use common\models\common\CapitalLog;
  20. use common\models\common\Express;
  21. use common\models\common\MallSetting;
  22. use common\models\mall\MallAddons;
  23. use common\models\order\Order;
  24. use common\models\order\OrderDetail;
  25. use common\models\order\OrderAction;
  26. use common\models\order\OrderExpress;
  27. use common\models\order\OrderExpressLog;
  28. use Yii;
  29. use yii\helpers\Json;
  30. use common\models\elasticsearch\order\OrderAction as OrderActionEs;
  31. use common\models\order\OrderRefund;
  32. use common\models\user\UserLevel;
  33. use common\models\user\UserWallet;
  34. use common\helpers\csv\CsvExportHelper;
  35. use common\enums\DownloadEnum;
  36. class StoreOrderController extends BaseController
  37. {
  38. /**
  39. * @name:
  40. * @msg: 订单列表
  41. * @param {*}
  42. * @return {*}
  43. */
  44. public function actionIndex()
  45. {
  46. if (!\Yii::$app->request->isAjax) {
  47. return $this->render('/store-order/index');
  48. }
  49. if (\Yii::$app->request->isGet) {
  50. //商品列表筛选
  51. $get = \Yii::$app->request->get();
  52. $isReserve = $get['is_reserve'] ?? 0;
  53. if($isReserve){
  54. $order_status_list = OrderStatusEnum::getReserveMap();
  55. }else{
  56. $order_status_list = OrderStatusEnum::getMap();
  57. }
  58. $payment_type_list = PayTypeEnum::getMap();
  59. $shipping_type_list = ShippingTypeEnum::getMap();
  60. $rules = [
  61. [['order_status'], 'in', 'range' => array_keys($order_status_list)],
  62. [['payment_type'], 'in', 'range' => array_keys($payment_type_list)],
  63. [['shipping_type'], 'in', 'range' => array_keys($shipping_type_list)],
  64. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  65. [['order_no', 'mobile', 'order_id'], 'safe'],
  66. [['type', 'type_value'], 'string'],
  67. [['is_reserve'],'in', 'range' => [0,1]],
  68. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  69. ];
  70. $res = \Yii::$app->services->validate->validate($get, $rules);
  71. if (!$res) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  72. $where = StoreOrder::searchParams($this->mall_id, $get);
  73. $with = ['base_user', 'detail','reserve'];
  74. $order = "id desc";
  75. $params = compact('where', 'with', 'order', 'alias');
  76. $page_data = StoreOrder::listPage($params);
  77. if ($page_data === false) return $this->error(StoreOrder::getStaticError());
  78. // dd(array_column($page_data['list'], 'order_status'));
  79. $compact_params = array_keys($page_data);
  80. $store_ids = array_column($page_data['list'],'store_id');
  81. $order_no_arr = array_column($page_data['list'],'order_no');
  82. $order_list = Order::lists(['where'=>[['order_no'=>$order_no_arr]],'index'=>'order_no','select'=>'id,order_no']);
  83. $store_list = Store::lists(['where'=>[['id'=>$store_ids]],'index'=>'id','select'=>'id,store_name']);
  84. extract($page_data);
  85. if(MallAddons::isInstall($this->mall_id,'Printer')){
  86. $printer_list = Printer::lists([
  87. 'where' => [
  88. ['mall_id'=> $this->mall_id],
  89. ['store_id'=>$store_ids],
  90. ['printer_type'=>2],
  91. ['status'=>StatusEnum::ENABLED]
  92. ],
  93. 'index'=>'store_id'
  94. ]);
  95. }
  96. foreach ($list as &$order) {
  97. $order['main_order_id'] = isset($order_list[$order['order_no']]['id'])?$order_list[$order['order_no']]['id']:0;
  98. $order_expresses = OrderExpress::getOrderExpresses([['order_id' => $order['id']]]);
  99. $order['store_name'] = $store_list[$order['store_id']]['store_name']??'';
  100. foreach ($order['detail'] as &$detail) {
  101. // 物流信息
  102. $detail['express'] = '';
  103. foreach ($order_expresses as $record) {
  104. $order_detail_ids = empty($record['order_detail_ids']) ? [] : json_decode($record['order_detail_ids'], true);
  105. if (is_string($order_detail_ids)) $order_detail_ids = [];
  106. if (in_array($detail['id'], $order_detail_ids)) {
  107. $detail['express'] = $record['express_name'] . ' | ' . $record['express_no'];
  108. if ($record['shipping_type'] == 0) $detail['express'] = '--';
  109. }
  110. }
  111. }
  112. $order['Printer']['is_show_printer'] = 0;
  113. if(isset($printer_list[$order['store_id']])){
  114. $order['Printer']['is_show_printer'] = 1;
  115. }
  116. }
  117. $export_list = StoreOrder::fieldsList();
  118. $marketing_type_map = MarketingTypeEnum::getMap();
  119. return $this->success('操作成功', compact($compact_params, 'order_status_list', 'payment_type_list', 'shipping_type_list', 'export_list', 'marketing_type_map'));
  120. }
  121. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  122. $post = \Yii::$app->request->post();
  123. $post['mall_id'] = $this->mall_id;
  124. $filename = 'o2o门店订单列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  125. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ORDERS, StoreOrder::class, 'exportHandle', $post);
  126. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  127. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  128. }
  129. }
  130. /**
  131. * @name:
  132. * @msg: 订单详情
  133. * @param {*}
  134. * @return {*}
  135. */
  136. public function actionDetail()
  137. {
  138. if (\Yii::$app->request->isAjax) {
  139. if (\Yii::$app->request->isGet) {
  140. //商品列表筛选
  141. $get = \Yii::$app->request->get();
  142. $order_status_list = OrderStatusEnum::getMap();
  143. $payment_type_list = PayTypeEnum::getMap();
  144. $shipping_type_list = ShippingTypeEnum::getMap();
  145. $rules = [[['id'], 'required']];
  146. $res = \Yii::$app->services->validate->validate($get, $rules);
  147. $where = [['mall_id' => $this->mall_id]];
  148. $where[] = ['=', 'id', $get['id']];
  149. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  150. $with = [
  151. 'base_user',
  152. 'detail',
  153. 'reserve'
  154. ];
  155. $params = compact('where', 'with');
  156. $order = StoreOrder::getOne($where, true, $with);
  157. if ($order === false) return $this->error(StoreOrder::getStaticError());
  158. $main_order = Order::findOne(['order_no'=>$order['order_no']]);
  159. $express = OrderExpress::lists(['where'=>[['order_id'=>$main_order['id']]]]);
  160. $express_log = OrderExpressLog::lists(['where'=>[['order_id'=>$main_order['id']]]]);
  161. $order['express'] = $express;
  162. $order['express_log'] = $express_log;
  163. $express_log = [];
  164. if ($order['express_log']) {
  165. $time = time();
  166. $hour = \Yii::$app->services->setting->mall->get($this->mall_id, 'logistics.update');// 获取更新物流时间设置
  167. if (empty($hour) && is_array($hour)) $hour = 8;// 默认8小时更新一次
  168. foreach ($order['express_log'] as $item) {
  169. $item['status'] = ExpressEnum::getStatusMap($item['status']);
  170. // 检查物流是否已完成,未完成且距离上次修改时间超过一个小时
  171. if ($item['table_express_status'] == StatusEnum::DISABLED && $item['updated_at'] + $hour * 3600 < $time) {
  172. $item['list'] = OrderExpressLog::execExpressLog($this->mall_id, $item);
  173. }
  174. $express_log[$item['order_express_id']] = $item;
  175. }
  176. }
  177. if (!empty($order['express'])) {
  178. foreach ($order['express'] as &$key) {
  179. if (isset($express_log[$key['id']])) {
  180. $express_list = Json::decode($express_log[$key['id']]['list']);
  181. $key['now_state'] = $express_log[$key['id']]['status'];
  182. $key['express_log'] = is_null($express_list) ? '' : array_reverse($express_list['Traces']);
  183. }
  184. }
  185. }
  186. if (!empty($order['detail'])) {
  187. foreach ($order['detail'] as &$detail) {
  188. if (!empty($detail['attr_groups_format'])) {
  189. $attr_groups_format = json_decode($detail['attr_groups_format'], true);
  190. $attr_list = $attr_groups_format[$detail['sign_id']];
  191. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  192. $attr_name_list = array_column($attr_list, 'attr_name');
  193. $detail['attr_list'] = [];
  194. foreach ($attr_group_name_list as $index => $attr_group_name) {
  195. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  196. }
  197. }
  198. if (empty($detail['goods_attr_name']) && !empty($detail['goods_attr_id'])) {
  199. if ($detail['order_source'] == 'Reserve') { // 服务订单
  200. $attr_info = StoreGoodsExtra::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  201. } else {
  202. $attr_info = StoreGoodsAttr::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  203. }
  204. $detail['goods_attr_name'] = $attr_info ? $attr_info['name'] : '';
  205. }
  206. }
  207. }
  208. $temp_step_arr = OrderStatusEnum::setpArrMap($order['order_status']); // 订单状态跟踪
  209. $step_arr = [];
  210. if(!empty($temp_step_arr)){
  211. foreach ($temp_step_arr as $k=>$v){
  212. if(!in_array($v['field'],['deposite_pay_time','final_payment_time'])){
  213. if($order['shipping_type'] == ShippingTypeEnum::PICKUP){
  214. if(in_array($v['field'],['consign_time'])){
  215. continue;
  216. }
  217. if($v['field'] == 'finish_time'){
  218. $step_arr[] = [
  219. 'field' => 'sign_time',
  220. 'title' => '买家自提',
  221. ];
  222. }
  223. }
  224. $step_arr[] = $v;
  225. }
  226. }
  227. }
  228. $order_action = OrderAction::getOrderAction([['order_id' => $main_order['id']??0]]);
  229. $marketing_type_map = MarketingTypeEnum::getMap();
  230. return $this->success('操作成功', compact('order', 'order_status_list', 'payment_type_list', 'shipping_type_list', 'step_arr', 'order_action', 'marketing_type_map'));
  231. }
  232. }
  233. return $this->render($this->action->id);
  234. }
  235. /**
  236. * @name:
  237. * @msg: 修改订单价格
  238. * @param {*}
  239. * @return {*}
  240. */
  241. public function actionChangePrice()
  242. {
  243. // 商品操作
  244. $form = \Yii::$app->request->post();
  245. $rules = [[['id', 'shipping_money'], 'required'], [['id'], 'integer'], [['shipping_money'], 'number'], ['detail', 'safe']];
  246. $res = \Yii::$app->services->validate->validate($form, $rules);
  247. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  248. $form['mall_id'] = $this->mall['id'];
  249. $form['mch_id'] = $this->admin->mch_id;
  250. $form['operator_id'] = $this->admin->id;
  251. $form['operator_name'] = $this->admin->account;
  252. $res = StoreOrder::ChangePrice($form);
  253. if ($res === false) return $this->error(StoreOrder::getStaticError());
  254. return $this->success();
  255. }
  256. /**
  257. * @name:
  258. * @msg: 订单支付
  259. * @param {*}
  260. * @return {*}
  261. */
  262. public function actionPay()
  263. {
  264. // 商品操作
  265. $form = \Yii::$app->request->post();
  266. $rules = [[['id'], 'required'], [['id'], 'integer']];
  267. $res = \Yii::$app->services->validate->validate($form, $rules);
  268. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  269. $form['mall_id'] = $this->mall['id'];
  270. $form['mch_id'] = $this->admin->mch_id;
  271. $form['operator_id'] = $this->admin->id;
  272. $form['operator_name'] = $this->admin->account;
  273. $res = StoreOrder::pay($form, PayTypeEnum::OFFLINE);
  274. if ($res === false) return $this->error(StoreOrder::getStaticError());
  275. return $this->success();
  276. }
  277. /**
  278. * @name:
  279. * @msg: 关闭订单
  280. * @param {*}
  281. * @return {*}
  282. */
  283. public function actionClose()
  284. {
  285. // 商品操作
  286. $form = \Yii::$app->request->post();
  287. $rules = [[['id'], 'required'], [['id'], 'integer']];
  288. $res = \Yii::$app->services->validate->validate($form, $rules);
  289. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  290. $form['mall_id'] = $this->mall['id'];
  291. $form['mch_id'] = $this->admin->mch_id;
  292. $form['operator_id'] = $this->admin->id;
  293. $form['operator_name'] = $this->admin->account;
  294. $res = StoreOrder::close($form);
  295. if ($res === false) return $this->error(StoreOrder::getStaticError());
  296. return $this->success();
  297. }
  298. public function actionDel()
  299. {
  300. // 商品操作
  301. $form = \Yii::$app->request->post();
  302. $rules = [[['id'], 'required'], [['id'], 'integer']];
  303. $res = \Yii::$app->services->validate->validate($form, $rules);
  304. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  305. $form['mall_id'] = $this->mall_id;
  306. $form['mch_id'] = $this->admin->mch_id;
  307. $form['operator_id'] = $this->admin->id;
  308. $form['operator_name'] = $this->admin->account;
  309. $res = StoreOrder::del($form);
  310. if ($res === false) return $this->error(StoreOrder::getStaticError());
  311. return $this->success();
  312. }
  313. /**
  314. * @name:
  315. * @msg: 修改订单备注
  316. * @param {*}
  317. * @return {*}
  318. */
  319. public function actionRemark()
  320. {
  321. $form = \Yii::$app->request->post();
  322. $rules = [[['id'], 'required'], [['id'], 'integer'], [['remark', 'seller_remark'], 'string']];
  323. $res = \Yii::$app->services->validate->validate($form, $rules);
  324. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  325. $form['mall_id'] = $this->mall['id'];
  326. $form['mch_id'] = $this->admin->mch_id;
  327. $form['operator_id'] = $this->admin->id;
  328. $form['operator_name'] = $this->admin->account;
  329. $res = StoreOrder::remark($form);
  330. if ($res === false) return $this->error(StoreOrder::getStaticError());
  331. return $this->success();
  332. }
  333. /**
  334. * @name:
  335. * @msg: 修改订单收货地址
  336. * @param {*}
  337. * @return {*}
  338. */
  339. public function actionChangeAddress()
  340. {
  341. $form = \Yii::$app->request->post();
  342. $rules = [
  343. [['id', 'receiver_name', 'mobile', 'province', 'city', 'area', 'address', 'region_name'], 'required'],
  344. [['id', 'province', 'city', 'area', 'zip'], 'integer'],
  345. [['address', 'region_name', 'receiver_name', 'mobile'], 'string']
  346. ];
  347. $res = \Yii::$app->services->validate->validate($form, $rules);
  348. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  349. $form['mall_id'] = $this->mall['id'];
  350. $form['mch_id'] = $this->admin->mch_id;
  351. $form['operator_id'] = $this->admin->id;
  352. $form['operator_name'] = $this->admin->account;
  353. $form['is_admin'] = 1;
  354. $res = StoreOrder::changeAddress($form);
  355. if ($res === false) return $this->error(StoreOrder::getStaticError());
  356. return $this->success();
  357. }
  358. /**
  359. * @name:
  360. * @msg: 发货
  361. * @param {*}
  362. * @return {*}
  363. */
  364. public function actionShipping()
  365. {
  366. if (\Yii::$app->request->isAjax) {
  367. if (\Yii::$app->request->isGet) {
  368. // 获取订单发货信息
  369. $jd_mch_code = MallSetting::conf($this->mall_id, 'logistics.jd_mch_code');
  370. $express_list = Express::getExpresses([], [], true, '*');
  371. return $this->success('请求成功', compact('express_list', 'jd_mch_code'));
  372. } else {
  373. // 发货
  374. $form = \Yii::$app->request->post();
  375. $rules = [
  376. [['shipping_type', 'id'], 'required'],
  377. [['express_id', 'shipping_type', 'id'], 'integer'],
  378. [['express_name', 'express_no', 'memo'], 'string'],
  379. [['order_detail_ids'], 'each', 'rule' => ['integer']],
  380. [['shipping_change', 'customer_name'], 'safe']
  381. ];
  382. $res = \Yii::$app->services->validate->validate($form, $rules);
  383. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  384. $form['mall_id'] = $this->mall_id;
  385. $form['mch_id'] = $this->admin->mch_id;
  386. $form['operator_id'] = $this->admin->id;
  387. $form['operator_name'] = $this->admin->account;
  388. if (empty($form['shipping_change'])) {
  389. $res = OrderExpress::delivery($form); // 发货
  390. } else {
  391. $res = OrderExpress::shippingChange($form); // 修改物流
  392. }
  393. if ($res === false) return $this->error(OrderExpress::getStaticError());
  394. return $this->success('发货/修改物流成功');
  395. }
  396. }
  397. }
  398. /**
  399. * 收货
  400. * @param $id
  401. * @param string $member_id
  402. * @throws UnprocessableEntityHttpException
  403. * @throws \yii\web\NotFoundHttpException
  404. */
  405. public function actionTakeDelivery()
  406. {
  407. // 商品操作
  408. $form = \Yii::$app->request->post();
  409. $rules = [[['id'], 'required'], [['id'], 'integer']];
  410. $res = \Yii::$app->services->validate->validate($form, $rules);
  411. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  412. $form['mall_id'] = $this->mall['id'];
  413. $form['mch_id'] = $this->admin->mch_id;
  414. $form['operator_id'] = $this->admin->id;
  415. $form['operator_name'] = $this->admin->account;
  416. $res = StoreOrder::takeDelivery($form);
  417. if ($res === false) return $this->error(StoreOrder::getStaticError());
  418. return $this->success();
  419. }
  420. /**
  421. * @name:
  422. * @msg: 订单操作日志
  423. * @param {*}
  424. * @return {*}
  425. */
  426. public function actionOrderAction()
  427. {
  428. try {
  429. $request = \Yii::$app->request;
  430. if ($request->isAjax) {
  431. if ($request->isGet) {
  432. $params = \Yii::$app->request->get();
  433. $params['where'] = ['mall_id' => $this->mall_id];
  434. $params['is_page'] = true;
  435. $params['order'] = 'created_at desc';
  436. $list = OrderActionEs::getList($params);
  437. return $this->success('ok', $list);
  438. }
  439. }
  440. } catch (\Exception $e) {
  441. return $this->error($e->getMessage(), []);
  442. }
  443. return $this->render($this->action->id);
  444. }
  445. /**
  446. * @name:
  447. * @msg: 订单完成
  448. * @param {*}
  449. * @return {*}
  450. */
  451. public function actionSalesOrder()
  452. {
  453. // 商品操作
  454. $form = \Yii::$app->request->post();
  455. $rules = [[['id'], 'required'], [['id'], 'integer']];
  456. $res = \Yii::$app->services->validate->validate($form, $rules);
  457. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  458. $form['mall_id'] = $this->mall_id;
  459. $form['operator_id'] = $this->admin->id;
  460. $form['operator_name'] = $this->admin->account;
  461. $res = StoreOrder::complete($form);
  462. if ($res == false) return $this->error('操作订单失败:' . StoreOrder::getStaticError());
  463. return $this->success('操作成功');
  464. }
  465. /**
  466. * @name:
  467. * @msg: 分润明细
  468. * @param {*}
  469. * @return {*}
  470. */
  471. public function actionCommissionAndScoreList()
  472. {
  473. $params = \Yii::$app->request->get();
  474. $wallet_type = 'commission';
  475. if (!empty($params['wallet_type'])) $wallet_type = $params['wallet_type'];
  476. $condition['where'] = [['order_id' => $params['order_id']]];
  477. $condition['select'] = 'id';
  478. $order_detail_list = StoreOrderDetail::lists($condition);
  479. $order_detail_ids = array_column($order_detail_list, 'id');
  480. $store_order = StoreOrder::findOne($params['order_id']);
  481. $order = Order::findOne(['order_no'=>$store_order['order_no']]);
  482. $mall_order_detail_ids = [];
  483. if($order){
  484. $condition['where'] = [['order_id' => $order['id']]];
  485. $condition['select'] = 'id';
  486. $mall_order_detail_list = OrderDetail::lists($condition);
  487. $mall_order_detail_ids = array_column($mall_order_detail_list, 'id');
  488. }
  489. $condition = [
  490. 'where' => [
  491. [
  492. 'or',
  493. [
  494. 'and',
  495. ['source_table' => 'store_order_detail'],
  496. ['source_table_id' => $order_detail_ids]
  497. ],
  498. [
  499. 'and',
  500. ['source_table' => [OrderDetail::tableName(),'order_detail']],
  501. ['source_table_id' => $mall_order_detail_ids]
  502. ],
  503. ]
  504. ],
  505. ];
  506. CapitalLog::$capitalType = "{$wallet_type}";
  507. $list = CapitalLog::getList($condition, 0);
  508. $level = UserLevel::find()->select("name,level")->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->all();
  509. $level_data = array_column($level, 'name', 'level');
  510. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  511. $condition['where'][] = ['status' => 0];
  512. $frozen_list = CapitalLog::getList($condition, 1);
  513. $list = array_merge($list, $frozen_list);
  514. return $this->success('操作成功', ['list' => $list, 'user_level' => $level_data]);
  515. }
  516. /**
  517. * @name:
  518. * @msg: 售后订单列表
  519. * @param {*}
  520. * @return {*}
  521. */
  522. public function actionRefundList()
  523. {
  524. if (!\Yii::$app->request->isAjax) {
  525. return $this->render('/store-order/refund', ['goods_type' => Json::encode(GoodsTypeEnum::getMap())]);
  526. }
  527. if (\Yii::$app->request->isGet) {
  528. //商品列表筛选
  529. $get = \Yii::$app->request->get();
  530. $refund_status_list = RefundStatusEnum::getMap();
  531. $refund_type_list = RefundTypeEnum::getMap();
  532. $rules = [
  533. [['is_feedback', 'goods_type'], 'integer'],
  534. [['keyword', 'order_no', 'type'], 'string'],
  535. [['type', 'order_no', 'keyword', 'refund_status'], 'safe'],
  536. ];
  537. $res = \Yii::$app->services->validate->validate($get, $rules);
  538. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  539. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  540. $where[] = ['!=', 'store_id', 0];
  541. if (!empty($get['goods_type'])) $where[] = ['goods_type' => $get['goods_type']];
  542. // 搜索查询
  543. !empty($get['order_no']) && $where[] = ['in', 'order_id', OrderRefund::getOidByOrderNo($this->mall_id, $get['order_no'])];
  544. if (isset($get['refund_status'])) {
  545. $get['refund_status'] = explode(',',$get['refund_status']);
  546. $where[] = ['in', 'refund_status', $get['refund_status']];
  547. //$where[] = ['=', 'refund_status', $get['refund_status']];
  548. } else {
  549. $where[] = ['in', 'refund_status', [RefundStatusEnum::APPLY, RefundStatusEnum::HANDEL, RefundStatusEnum::FINISH, RefundStatusEnum::RETURN_GOODS, RefundStatusEnum::EXCHANGE_GOODS, RefundStatusEnum::REFUSE]];
  550. }
  551. if (!empty($get['type']) && !empty($get['keyword'])) $where[] = OrderRefund::searchByKeyword($this->mall_id, $get['type'], $get['keyword']);
  552. $with = ['base_user', 'orderDetail', 'order'];
  553. $order = "id desc";
  554. $params = compact('where', 'with', 'order');
  555. $page_data = OrderRefund::listPage($params, false, true);
  556. if ($page_data === false) return $this->error(OrderRefund::getStaticError());
  557. $compact_params = array_keys($page_data);
  558. extract($page_data);
  559. $store_ids = array_column($list,'store_id');
  560. $store_list = Store::lists([
  561. 'where'=>[
  562. ['id'=>$store_ids],
  563. ['mall_id'=>$this->mall_id]
  564. ],
  565. 'index'=>'id',
  566. ]);
  567. foreach ($list as &$order) {
  568. foreach ($order['orderDetail'] as &$detail) {
  569. if (!empty($detail['attr_groups_format'])) {
  570. //规格
  571. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  572. $attr_list = $attr_groups_format[$detail['sign_id']];
  573. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  574. $attr_name_list = array_column($attr_list, 'attr_name');
  575. $detail['attr_list'] = [];
  576. foreach ($attr_group_name_list as $index => $attr_group_name) {
  577. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  578. }
  579. }
  580. }
  581. if (!empty($order['order'])) {
  582. $extraInfo = $order['order']['extra_info'];
  583. if (!empty($extraInfo['head_id']) && MallAddons::isInstall($this->mall_id, 'Community')) {
  584. $order['order']['community_addons_data'] = (new HeadService())->pickupInfo($order['mall_id'], $extraInfo['head_id']);
  585. }
  586. }
  587. $order['store_name'] = '';
  588. $order['shop_mobile'] = '';
  589. if(isset($store_list[$order['store_id']])){
  590. $order['store_name'] = $store_list[$order['store_id']]['store_name'];
  591. $order['shop_mobile'] = $store_list[$order['store_id']]['shop_mobile'];
  592. }
  593. }
  594. return $this->success('操作成功', compact($compact_params, 'refund_status_list', 'refund_type_list'));
  595. }
  596. }
  597. /**
  598. * @name:
  599. * @msg: 售后订单详情
  600. * @param {*}
  601. * @return {*}
  602. */
  603. public function actionRefundDetail()
  604. {
  605. if (\Yii::$app->request->isAjax) {
  606. if (\Yii::$app->request->isGet) {
  607. //商品列表筛选
  608. $get = \Yii::$app->request->get();
  609. $order_status_list = OrderStatusEnum::getMap();
  610. $payment_type_list = PayTypeEnum::getMap();
  611. $feedback_type_list = OrderStatusEnum::getFeedbackMap();
  612. $address_list = \Yii::$app->services->setting->mall->get($this->mall_id, 'return_address.return_address');
  613. $address_list = $address_list ? json_decode($address_list, true) : [];
  614. $rules = [[['id', 'store_id'], 'required']];
  615. $res = \Yii::$app->services->validate->validate($get, $rules);
  616. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  617. $where = [['mall_id' => $this->mall_id]];
  618. $where[] = ['id' => $get['id'], 'store_id' => $get['store_id']];
  619. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  620. $with = ['base_user', 'order' => function ($query) {
  621. $query->select('*');
  622. }, 'orderDetail', 'marketing', 'step'];
  623. $params = compact('where', 'with');
  624. $refund = OrderRefund::getOne($where, true, $with);
  625. if ($refund === false) return $this->error(OrderRefund::getStaticError());
  626. foreach ($refund['orderDetail'] as $key => $detail) {
  627. if (!empty($detail['attr_groups_format'])) {
  628. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  629. $attr_list = $attr_groups_format[$detail['sign_id']];
  630. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  631. $attr_name_list = array_column($attr_list, 'attr_name');
  632. $detail['attr_list'] = [];
  633. }
  634. if (empty($detail['goods_attr_name']) && !empty($detail['goods_attr_id'])) {
  635. if ($detail['order_source'] == 'Reserve') { // 服务订单
  636. $attr_info = StoreGoodsExtra::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  637. } else {
  638. $attr_info = StoreGoodsAttr::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  639. }
  640. $detail['goods_attr_name'] = $attr_info ? $attr_info['name'] : '';
  641. }
  642. }
  643. // 返还优惠内容
  644. if (!empty($refund['marketing'])) {
  645. $discount = [];
  646. $marketing = $refund['marketing'];
  647. if (!empty($marketing['deduct_score'])) {
  648. $score_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name');
  649. $discount[] = floatval($marketing['deduct_score']) . $score_name;
  650. }
  651. $refund['discount'] = implode('+', $discount);
  652. }
  653. $step_status = $refund['step'][0]['step_status'] ?? 0;
  654. $step_arr = RefundStatusEnum::stepNumArr(in_array($step_status, [RefundStatusEnum::STEP_NEGATIVE_1, RefundStatusEnum::STEP_NEGATIVE_2]) ? 1 : $refund['type']); // 进度条
  655. $step_text = RefundStatusEnum::getStepStatusText($step_status);
  656. return $this->success('操作成功', compact('refund', 'order_status_list', 'payment_type_list', 'feedback_type_list', 'step_arr', 'address_list', 'step_text'));
  657. }
  658. }
  659. return $this->render('/store-order/refund-detail');
  660. }
  661. /**
  662. * @name:
  663. * @msg: 售后处理
  664. * @param {*}
  665. * @return {*}
  666. */
  667. // |TODO:暂时先不做
  668. /* public function actionHandleRefund()
  669. {
  670. if (\Yii::$app->request->isAjax) {
  671. if (\Yii::$app->request->isPost) {
  672. //商品列表筛选
  673. $params = \Yii::$app->request->post();
  674. $rules = [
  675. [['id', 'step_status', 'store_id'], 'required'],
  676. [['id', 'step_status', 'mobile', 'address_type', 'store_id'], 'integer'],
  677. [['saler_express', 'saler_express_no', 'address', 'consignee'], 'string'],
  678. [['saler_express', 'saler_express_no', 'address_type', 'customer_name'], 'safe'],
  679. ];
  680. $res = \Yii::$app->services->validate->validate($params, $rules);
  681. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  682. // 退货地址
  683. $address = ['consignee' => $params['consignee'], 'mobile' => $params['mobile'], 'address' => $params['address']];
  684. // 换货物流信息
  685. $express = ['saler_express' => $params['saler_express'], 'saler_express_no' => $params['saler_express_no'], 'customer_name' => $params['customer_name']];
  686. $data = [
  687. 'id' => $params['id'],
  688. 'store_id' => $params['store_id'],
  689. 'step_status' => $params['step_status'],
  690. 'address' => $address,
  691. 'express' => $express,
  692. ];
  693. $logic = new OrderRefundLogic();
  694. $res = $logic->execute($this->mall_id, $data);
  695. if ($res == false) return $this->error($logic->getError());
  696. return $this->success('操作成功');
  697. }
  698. }
  699. } */
  700. public function actionReserve()
  701. {
  702. $is_show_username = Yii::$app->services->setting->mall->get($this->mall_id, 'order.is_show_username');
  703. return $this->render($this->action->id, ['is_show_username' => $is_show_username ?? 0]);
  704. }
  705. /**
  706. * 预约订单详情
  707. * @return string
  708. */
  709. public function actionReserveDetail()
  710. {
  711. return $this->render($this->action->id);
  712. }
  713. /**
  714. * 获取支付类型
  715. *
  716. * @return mixed
  717. */
  718. public function actionPaymentType()
  719. {
  720. return $this->success(
  721. 'ok',
  722. PayTypeEnum::getMap()
  723. );
  724. }
  725. }