OrderController.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\Community\common\service\HeadService;
  4. use addons\Printer\common\models\Printer;
  5. use addons\Store\common\enums\StoreEnum;
  6. use addons\Store\common\events\order\OrderRefundFinishEvent;
  7. use addons\Store\common\models\Store;
  8. use addons\Store\common\models\StoreClerkLog;
  9. use addons\Store\common\models\StoreGoodsAttr;
  10. use addons\Store\common\models\StoreGoodsExtra;
  11. use addons\Store\common\models\StoreOrder;
  12. use addons\Store\common\models\StoreOrderCredit;
  13. use addons\Store\common\models\StoreOrderDetail;
  14. use addons\Store\common\models\StoreOrderMarketing;
  15. use addons\Store\common\models\StoreRefundAddress;
  16. use backend\modules\mall\services\RefundService;
  17. use addons\Store\common\services\StoreService;
  18. use common\enums\ApiStatusEnum;
  19. use common\enums\DownloadEnum;
  20. use common\enums\ExpressEnum;
  21. use common\enums\GoodsTypeEnum;
  22. use common\enums\MarketingTypeEnum;
  23. use common\enums\OrderSourceEnum;
  24. use common\enums\OrderStatusEnum;
  25. use common\enums\PayTypeEnum;
  26. use common\enums\RefundReasonEnum;
  27. use common\enums\RefundStatusEnum;
  28. use common\enums\RefundTypeEnum;
  29. use common\enums\ShippingTypeEnum;
  30. use common\enums\StatusEnum;
  31. use common\enums\UserWalletEnum;
  32. use common\helpers\csv\CsvExportHelper;
  33. use common\helpers\LockHelper;
  34. use common\logic\order\OrderRefundLogic;
  35. use common\models\common\CapitalLog;
  36. use common\models\common\Express;
  37. use common\models\common\MallSetting;
  38. use common\models\goods\Goods;
  39. use common\models\mall\MallAddons;
  40. use common\models\order\Order;
  41. use common\models\order\OrderDetail;
  42. use common\models\order\OrderAction;
  43. use common\models\order\OrderExpress;
  44. use common\models\order\OrderExpressLog;
  45. use common\models\order\OrderRefund;
  46. use common\models\user\User;
  47. use common\models\user\UserLevel;
  48. use Exception;
  49. use Yii;
  50. use yii\helpers\Json;
  51. use addons\Printer\common\logic\PrinterBase;
  52. use addons\Printer\common\logic\PrintOrder;
  53. use common\enums\AddonsEnum;
  54. class OrderController extends BaseController
  55. {
  56. public $enableCsrfValidation = false;
  57. /**
  58. * 订单管理
  59. * @Author bing
  60. * @DateTime 2022-04-26 18:07:05
  61. * @return void
  62. * @copyright: Copyright (c) 2022 广东七件事集团
  63. */
  64. public function actionIndex()
  65. {
  66. if (\Yii::$app->request->isAjax) {
  67. if (\Yii::$app->request->isGet) {
  68. //商品列表筛选
  69. $get = Yii::$app->request->get();
  70. $isReserve = $get['is_reserve'] ?? 0;
  71. if($isReserve){
  72. $order_status_list = OrderStatusEnum::getReserveMap();
  73. $order_status_list[3] = '服务中';
  74. }else{
  75. $order_status_list = OrderStatusEnum::getMap();
  76. }
  77. $payment_type_list = PayTypeEnum::getMap();
  78. $shipping_type_list = ShippingTypeEnum::getMap();
  79. $rules = [
  80. [['order_status'], 'in', 'range' => array_keys($order_status_list)],
  81. [['payment_type'], 'in', 'range' => array_keys($payment_type_list)],
  82. [['shipping_type'], 'in', 'range' => array_keys($shipping_type_list)],
  83. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  84. [['order_no', 'mobile', 'order_id'], 'safe'],
  85. [['type', 'type_value'], 'string'],
  86. [['is_reserve'],'in', 'range' => [0,1]],
  87. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  88. ];
  89. $res = Yii::$app->services->validate->validate($get, $rules);
  90. if (!$res) return $this->error(Yii::$app->services->validate->getErrorMessage());
  91. $where[] = ['=', 'mall_id', $this->mall_id];
  92. $where[] = ['=', 'store_id', $this->store_id];
  93. $where[] = ['=', 'supplier_type', 0];
  94. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  95. if ($isReserve) {
  96. $where[] = ['=', 'order_source', OrderSourceEnum::SOURCE_RESERVE];
  97. } else {
  98. $where[] = ['<>', 'order_source', OrderSourceEnum::SOURCE_RESERVE];
  99. }
  100. if (isset($get['order_id'])) $where[] = ['in', 'id', explode(',', $get['order_id'])];
  101. ($get['payment_type'] ?? false) && $where[] = ['=', 'payment_type', $get['payment_type']];
  102. ($get['shipping_type'] ?? false) && $where[] = ['=', 'shipping_type', $get['shipping_type']];
  103. if (isset($get['order_status']) && $get['order_status'] != 999) {
  104. if ($isReserve && $get['order_status'] == OrderStatusEnum::SING) { // 以前端栏目配置服务中即待验收其状态包括已提货
  105. $where[] = ['in', 'order_status', [OrderStatusEnum::SING, OrderStatusEnum::TAKE_FINISH]];
  106. } else {
  107. $where[] = ['=', 'order_status', $get['order_status']];
  108. }
  109. }
  110. ($get['order_no'] ?? false) && $where[] = ['=', 'order_no', $get['order_no']];
  111. ($get['start'] ?? false) && $where[] = ['>=', 'created_at', strtotime($get['start'])];
  112. ($get['end'] ?? false) && $where[] = ['<=', 'created_at', strtotime($get['end'])];
  113. if ($get['type_value'] ?? false) {
  114. switch ($get['type']) {
  115. case 'mobile':
  116. $uids = User::find()->select('id')->where(['like', 'mobile', $get['type_value'] . '%', false])->column();
  117. !empty($uids) && $where[] = ['in', 'user_id', $uids];
  118. break;
  119. case 'user_id':
  120. $where[] = ['in', 'user_id', $get['type_value']];
  121. break;
  122. case 'consignee':
  123. case 'consignee_mobile':
  124. if ($get['type'] == 'consignee') {
  125. $where[] = ['like', 'receiver_name', $get['type_value'] . '%', false];
  126. } else {
  127. $where[] = ['like', 'mobile', $get['type_value'] . '%', false];
  128. }
  129. break;
  130. default:
  131. }
  132. }
  133. $with = ['base_user', 'detail','reserve'];
  134. $order = "id desc";
  135. $params = compact('where', 'with', 'order');
  136. $page_data = StoreOrder::listPage($params, false, true);
  137. if ($page_data === false) return $this->error(Goods::getStaticError());
  138. $compact_params = array_keys($page_data);
  139. $order_no_arr = array_column($page_data['list'],'order_no');
  140. $order_list = Order::lists(['where'=>[['order_no'=>$order_no_arr]],'index'=>'order_no','select'=>'id,order_no']);
  141. extract($page_data);
  142. $store_order_detail_ids = [];
  143. foreach ($page_data['list'] as $val){
  144. foreach ($val['detail'] as $value) {
  145. $store_order_detail_ids[]=$value['id'];
  146. }
  147. }
  148. $store_order_marketing_list = StoreOrderMarketing::lists(['where'=>[['order_detail_id'=>$store_order_detail_ids]],'index'=>'order_detail_id']);
  149. $is_show_printer = 0;
  150. if(MallAddons::isInstall($this->mall_id,'Printer')){
  151. $res = Printer::lists([
  152. 'where' => [
  153. ['mall_id'=> $this->mall_id],
  154. ['store_id'=>$this->store_id],
  155. ['printer_type'=>2],
  156. ['status'=>StatusEnum::ENABLED]
  157. ]
  158. ]);
  159. if(!empty($res)) $is_show_printer = 1;
  160. }
  161. foreach ($list as &$order) {
  162. $order_expresses = OrderExpress::getOrderExpresses([['order_id' => $order['id']]]);
  163. $order['main_order_id'] = isset($order_list[$order['order_no']]['id']) ? $order_list[$order['order_no']]['id'] : 0;
  164. $order['Printer']['is_show_printer'] = $is_show_printer;
  165. $deduct_score = 0;
  166. $deduct_score_money = 0;
  167. foreach ($order['detail'] as &$detail) {
  168. // 物流信息
  169. $detail['express'] = '';
  170. foreach ($order_expresses as $record) {
  171. $order_detail_ids = empty($record['order_detail_ids']) ? [] : json_decode($record['order_detail_ids'], true);
  172. if (is_string($order_detail_ids)) $order_detail_ids = [];
  173. if (in_array($detail['id'], $order_detail_ids)) {
  174. $detail['express'] = $record['express_name'] . ' | ' . $record['express_no'];
  175. if ($record['shipping_type'] == 0) $detail['express'] = '--';
  176. }
  177. }
  178. if(!empty($store_order_marketing_list[$detail['id']])){
  179. $deduct_currency = json_decode($store_order_marketing_list[$detail['id']]['deduct_currency'],true);
  180. if(!empty($deduct_currency['score'])){
  181. $deduct_score+= $deduct_currency['score']['score'];
  182. $deduct_score_money+= $deduct_currency['score']['money'];
  183. }
  184. }
  185. }
  186. $order['score_deduct'] = '';
  187. if(!empty($deduct_score)&&!empty($deduct_score_money)){
  188. $deduct_score = bcadd($deduct_score,0,2);
  189. $deduct_score_money = bcadd($deduct_score_money,0,2);
  190. $order['score_deduct'] = "{$deduct_score}积分抵扣{$deduct_score_money}元";
  191. }
  192. $order['create_type_text'] = $order['create_type'] == 1 ? '线上下单' : '系统创建';
  193. }
  194. $export_list = StoreOrder::fieldsList();
  195. if($isReserve){
  196. $export_list['create_type'] = '创建方式';
  197. }
  198. $marketing_type_map = MarketingTypeEnum::getMap();
  199. return $this->success('操作成功', compact($compact_params, 'order_status_list', 'payment_type_list', 'shipping_type_list', 'export_list', 'marketing_type_map'));
  200. }
  201. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  202. $post = \Yii::$app->request->post();
  203. $post['mall_id'] = $this->mall_id;
  204. $post['store_id'] = $this->store_id;
  205. $filename = 'o2o门店订单列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  206. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ORDERS, StoreOrder::class, 'exportHandle', $post,0,$this->store_id);
  207. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  208. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  209. }
  210. }
  211. return $this->render($this->action->id);
  212. }
  213. /**
  214. * 售后订单
  215. * @Author bing
  216. * @DateTime 2022-04-26 18:07:16
  217. * @return void
  218. * @copyright: Copyright (c) 2022 广东七件事集团
  219. */
  220. public function actionRefund()
  221. {
  222. if (\Yii::$app->request->isAjax) {
  223. if (\Yii::$app->request->isGet) {
  224. //商品列表筛选
  225. $get = Yii::$app->request->get();
  226. $refund_status_list = RefundStatusEnum::getMap();
  227. $refund_type_list = RefundTypeEnum::getMap();
  228. $rules = [
  229. [['is_feedback', 'goods_type'], 'integer'],
  230. [['keyword', 'order_no', 'type'], 'string'],
  231. [['type', 'order_no', 'keyword', 'refund_status'], 'safe'],
  232. ];
  233. $res = Yii::$app->services->validate->validate($get, $rules);
  234. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  235. $where = [['mall_id' => $this->mall_id]];
  236. $where[] = ['=', 'store_id', $this->store_id];
  237. $where[] = ['=', 'status', StatusEnum::ENABLED];
  238. if (!empty($get['goods_type'])) $where[] = ['goods_type' => $get['goods_type']];
  239. // 搜索查询
  240. !empty($get['order_no']) && $where[] = ['in', 'order_id', OrderRefund::getOidByOrderNo($this->mall_id, $get['order_no'])];
  241. if (isset($get['refund_status'])) {
  242. $where[] = ['in', 'refund_status', explode(",",$get['refund_status'])];
  243. } else {
  244. $where[] = ['in', 'refund_status', [RefundStatusEnum::APPLY, RefundStatusEnum::HANDEL, RefundStatusEnum::FINISH, RefundStatusEnum::RETURN_GOODS, RefundStatusEnum::EXCHANGE_GOODS, RefundStatusEnum::REFUSE]];
  245. }
  246. if (!empty($get['type']) && !empty($get['keyword'])) $where[] = OrderRefund::searchByKeyword($this->mall_id, $get['type'], $get['keyword']);
  247. $with = ['base_user', 'orderDetail', 'order'];
  248. $order = "id desc";
  249. $params = compact('where', 'with', 'order');
  250. $page_data = OrderRefund::listPage($params, false, true);
  251. if ($page_data === false) return $this->error(OrderRefund::getStaticError());
  252. $compact_params = array_keys($page_data);
  253. extract($page_data);
  254. foreach ($list as &$order) {
  255. foreach ($order['orderDetail'] as &$detail) {
  256. if (!empty($detail['attr_groups_format'])) {
  257. //规格
  258. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  259. $attr_list = $attr_groups_format[$detail['sign_id']];
  260. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  261. $attr_name_list = array_column($attr_list, 'attr_name');
  262. $detail['attr_list'] = [];
  263. foreach ($attr_group_name_list as $index => $attr_group_name) {
  264. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  265. }
  266. }
  267. }
  268. if (!empty($order['order'])) {
  269. $extraInfo = $order['order']['extra_info'];
  270. if (!empty($extraInfo['head_id']) && MallAddons::isInstall($this->mall_id, 'Community')) {
  271. $order['order']['community_addons_data'] = (new HeadService())->pickupInfo($order['mall_id'], $extraInfo['head_id']);
  272. }
  273. }
  274. }
  275. return $this->success('操作成功', compact($compact_params, 'refund_status_list', 'refund_type_list'));
  276. }
  277. }
  278. return $this->render($this->action->id, ['goods_type' => Json::encode(GoodsTypeEnum::getMap())]);
  279. }
  280. public function actionRefundDetail()
  281. {
  282. if (\Yii::$app->request->isAjax) {
  283. if (\Yii::$app->request->isGet) {
  284. //商品列表筛选
  285. $get = Yii::$app->request->get();
  286. $order_status_list = OrderStatusEnum::getMap();
  287. $payment_type_list = PayTypeEnum::getMap();
  288. $feedback_type_list = OrderStatusEnum::getFeedbackMap();
  289. // $address_list = Yii::$app->services->setting->mall->get($this->mall_id, 'return_address.return_address');
  290. // $address_list = $address_list ? json_decode($address_list, true) : [];
  291. $address_list = StoreRefundAddress::lists([
  292. 'where' => [
  293. ['store_id' => $this->store_id],
  294. ['status' => StatusEnum::ENABLED]
  295. ],
  296. 'select'=>'address,mobile,name as consignee, refund_name',
  297. ]);
  298. $rules = [[['id'], 'required']];
  299. $res = Yii::$app->services->validate->validate($get, $rules);
  300. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  301. $where = [['mall_id' => $this->mall_id], ['store_id' => $this->store_id]];
  302. $where[] = ['=', 'id', $get['id']];
  303. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  304. $with = ['base_user', 'order' => function ($query) {
  305. $query->select('*');
  306. }, 'orderDetail', 'marketing', 'step'];
  307. $params = compact('where', 'with');
  308. $refund = OrderRefund::getOne($where, true, $with);
  309. if ($refund === false) return $this->error(Goods::getStaticError());
  310. $main_order = Order::findOne(['id'=>$refund['order_id']]);
  311. $store_order = StoreOrder::findOne(['order_no'=>$main_order['order_no']??0]);
  312. $store_order_credit = StoreOrderCredit::find()->where(['type'=>1,'order_id'=>$store_order['id']??0])
  313. ->andWhere(['>','pay_time',0])->one();
  314. $refund['orderDetail']['credit_money']= 0;
  315. if(!empty($store_order_credit['extra'])){
  316. $goods_id = $refund['orderDetail']['goods_id'];
  317. $extra = json_decode($store_order_credit['extra'],true);
  318. if(!empty($extra[$goods_id]['credit_money'])){
  319. $refund['orderDetail']['goods_price'] = bcadd($refund['orderDetail']['goods_price'],$extra[$goods_id]['credit_money'],2);
  320. $refund['orderDetail']['credit_money'] = $extra[$goods_id]['credit_money'];
  321. }
  322. }
  323. foreach ($refund['orderDetail'] as $key => $detail) {
  324. if (!empty($detail['attr_groups_format'])) {
  325. $attr_groups_format = Json::decode($detail['attr_groups_format'], true);
  326. $attr_list = $attr_groups_format[$detail['sign_id']];
  327. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  328. $attr_name_list = array_column($attr_list, 'attr_name');
  329. $detail['attr_list'] = [];
  330. }
  331. if (empty($detail['goods_attr_name']) && !empty($detail['goods_attr_id'])) {
  332. if ($detail['order_source'] == 'Reserve') { // 服务订单
  333. $attr_info = StoreGoodsExtra::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  334. } else {
  335. $attr_info = StoreGoodsAttr::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  336. }
  337. $detail['goods_attr_name'] = $attr_info ? $attr_info['name'] : '';
  338. }
  339. }
  340. // 返还优惠内容
  341. if (!empty($refund['marketing'])) {
  342. $discount = [];
  343. $marketing = $refund['marketing'];
  344. if (!empty($marketing['deduct_score'])) {
  345. $score_name = Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name');
  346. $discount[] = floatval($marketing['deduct_score']) . $score_name;
  347. }
  348. $refund['discount'] = implode('+', $discount);
  349. }
  350. $step_status = $refund['step'][0]['step_status'] ?? 0;
  351. $step_arr = RefundStatusEnum::stepNumArr(in_array($step_status, [RefundStatusEnum::STEP_NEGATIVE_1, RefundStatusEnum::STEP_NEGATIVE_2]) ? 1 : $refund['type']);// 进度条
  352. $step_text = RefundStatusEnum::getStepStatusText($step_status);
  353. // 五洲供应链判断是否能操作
  354. // 是否显示退货超期,撤销售后
  355. $is_show_revoce = (OrderRefund::findOne(['id' => $refund['id']]))->isReturnTimeoutOfRefund();
  356. $refund['option_limit'] = $refund['order']['order_source'] == AddonsEnum::ADDONS_NAME_WUZHOU_CHAIN ? 1 : 0;
  357. return $this->success('操作成功', compact('refund', 'order_status_list', 'payment_type_list', 'feedback_type_list', 'step_arr', 'address_list', 'step_text', 'is_show_revoce'));
  358. }
  359. }
  360. return $this->render($this->action->id);
  361. }
  362. /**
  363. * 平台订单
  364. * @Author bing
  365. * @DateTime 2022-04-26 18:07:29
  366. * @return void
  367. * @copyright: Copyright (c) 2022 广东七件事集团
  368. */
  369. public function actionPlatformOrder()
  370. {
  371. if (\Yii::$app->request->isAjax) {
  372. if (\Yii::$app->request->isGet) {
  373. //商品列表筛选
  374. $get = Yii::$app->request->get();
  375. $order_status_list = OrderStatusEnum::getMap();
  376. $payment_type_list = PayTypeEnum::getMap();
  377. $shipping_type_list = ShippingTypeEnum::getMap();
  378. $rules = [
  379. [['order_status'], 'in', 'range' => array_keys($order_status_list)],
  380. [['payment_type'], 'in', 'range' => array_keys($payment_type_list)],
  381. [['shipping_type'], 'in', 'range' => array_keys($shipping_type_list)],
  382. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  383. [['order_no', 'mobile', 'order_id'], 'safe'],
  384. [['type', 'type_value'], 'string'],
  385. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  386. ];
  387. $res = Yii::$app->services->validate->validate($get, $rules);
  388. if (!$res) return $this->error(Yii::$app->services->validate->getErrorMessage());
  389. $where[] = ['=', 'mall_id', $this->mall_id];
  390. $where[] = ['=', 'store_id', $this->store_id];
  391. $where[] = ['>', 'supplier_type', 0];
  392. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  393. if (isset($get['order_id'])) $where[] = ['in', 'id', explode(',', $get['order_id'])];
  394. ($get['payment_type'] ?? false) && $where[] = ['=', 'payment_type', $get['payment_type']];
  395. ($get['shipping_type'] ?? false) && $where[] = ['=', 'shipping_type', $get['shipping_type']];
  396. if (isset($get['order_status']) && $get['order_status'] != 999) {
  397. $where[] = ['=', 'order_status', $get['order_status']];
  398. }
  399. ($get['order_no'] ?? false) && $where[] = ['=', 'order_no', $get['order_no']];
  400. ($get['start'] ?? false) && $where[] = ['>=', 'created_at', strtotime($get['start'])];
  401. ($get['end'] ?? false) && $where[] = ['<=', 'created_at', strtotime($get['end'])];
  402. if ($get['type_value'] ?? false) {
  403. switch ($get['type']) {
  404. case 'mobile':
  405. $uids = User::find()->select('id')->where(['like', 'mobile', $get['type_value'] . '%', false])->column();
  406. !empty($uids) && $where[] = ['in', 'user_id', $uids];
  407. break;
  408. case 'user_id':
  409. $where[] = ['in', 'user_id', $get['type_value']];
  410. break;
  411. default:
  412. }
  413. }
  414. $with = ['base_user', 'detail'];
  415. $order = "id desc";
  416. $params = compact('where', 'with', 'order', 'alias');
  417. $page_data = StoreOrder::listPage($params, false, true);
  418. if ($page_data === false) return $this->error(Goods::getStaticError());
  419. $compact_params = array_keys($page_data);
  420. extract($page_data);
  421. foreach ($list as &$order) {
  422. $order_expresses = OrderExpress::getOrderExpresses([['order_id' => $order['id']]]);
  423. foreach ($order['detail'] as &$detail) {
  424. // 物流信息
  425. $detail['express'] = '';
  426. foreach ($order_expresses as $record) {
  427. $order_detail_ids = empty($record['order_detail_ids']) ? [] : json_decode($record['order_detail_ids'], true);
  428. if (is_string($order_detail_ids)) $order_detail_ids = [];
  429. if (in_array($detail['id'], $order_detail_ids)) {
  430. $detail['express'] = $record['express_name'] . ' | ' . $record['express_no'];
  431. if ($record['shipping_type'] == 0) $detail['express'] = '--';
  432. }
  433. }
  434. }
  435. // 五洲供应链订单限制操作
  436. $item['option_limit'] = 0;
  437. if ($item['supplier_type'] == StoreEnum::SELF_MALL_WUZHOU) $item['option_limit'] = 1;
  438. }
  439. $export_list = StoreOrder::fieldsList();
  440. $marketing_type_map = MarketingTypeEnum::getMap();
  441. return $this->success('操作成功', compact($compact_params, 'order_status_list', 'payment_type_list', 'shipping_type_list', 'export_list', 'marketing_type_map'));
  442. }
  443. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  444. $post = \Yii::$app->request->post();
  445. $post['mall_id'] = $this->mall_id;
  446. $filename = '订单列表-' . date('YmdHis') . '_' . rand(10000, 99999);
  447. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_ORDERS, Order::class, 'exportHandle', $post);
  448. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  449. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  450. }
  451. }
  452. return $this->render($this->action->id);
  453. }
  454. /**
  455. * 订单详情
  456. * @Author bing
  457. * @DateTime 2021-09-06 14:09:56
  458. * @return void
  459. * @copyright: Copyright (c) 2020 广东七件事集团
  460. */
  461. public function actionDetail()
  462. {
  463. if (\Yii::$app->request->isAjax) {
  464. if (\Yii::$app->request->isGet) {
  465. //商品列表筛选
  466. $get = Yii::$app->request->get();
  467. $order_status_list = OrderStatusEnum::getMap();
  468. $payment_type_list = PayTypeEnum::getMap();
  469. $shipping_type_list = ShippingTypeEnum::getMap();
  470. $rules = [[['id'], 'required']];
  471. $res = Yii::$app->services->validate->validate($get, $rules);
  472. $where = [['mall_id' => $this->mall_id]];
  473. $where[] = ['=', 'store_id', $this->store_id];
  474. $where[] = ['=', 'id', $get['id']];
  475. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  476. $with = [
  477. 'base_user',
  478. 'detail',
  479. 'reserve'
  480. ];
  481. $params = compact('where', 'with');
  482. $order = StoreOrder::getOne($where, true, $with);
  483. if ($order === false) return $this->error(Goods::getStaticError());
  484. $main_order = Order::getOne([['order_no'=>$order['order_no']]],true,['express','express_log']);
  485. $express_log = [];
  486. if ($main_order['express_log']) {
  487. foreach ($main_order['express_log'] as $item) {
  488. $item['status'] = ExpressEnum::getStatusMap($item['status']);
  489. $express_log[$item['order_express_id']] = $item;
  490. }
  491. }
  492. if (!empty($main_order['express'])) {
  493. foreach ($main_order['express'] as &$key) {
  494. if (isset($express_log[$key['id']])) {
  495. $express_list = Json::decode($express_log[$key['id']]['list']);
  496. $key['now_state'] = $express_log[$key['id']]['status'];
  497. $key['express_log'] = is_null($express_list) ? '' : array_reverse($express_list['Traces']);
  498. }
  499. }
  500. }
  501. $order['express'] = $main_order['express'] ?? [];
  502. if (!empty($order['detail'])) {
  503. foreach ($order['detail'] as &$detail) {
  504. if (!empty($detail['attr_groups_format'])) {
  505. $attr_groups_format = json_decode($detail['attr_groups_format'], true);
  506. $attr_list = $attr_groups_format[$detail['sign_id']];
  507. $attr_group_name_list = array_column($attr_list, 'attr_group_name');
  508. $attr_name_list = array_column($attr_list, 'attr_name');
  509. $detail['attr_list'] = [];
  510. foreach ($attr_group_name_list as $index => $attr_group_name) {
  511. $detail['attr_list'][] = $attr_group_name . ':' . $attr_name_list[$index];
  512. }
  513. }
  514. if (empty($detail['goods_attr_name']) && !empty($detail['goods_attr_id'])) {
  515. if ($detail['order_source'] == 'Reserve') { // 服务订单
  516. $attr_info = StoreGoodsExtra::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  517. } else {
  518. $attr_info = StoreGoodsAttr::getOne([['=', 'id', $detail['goods_attr_id']]],true, ['goods']);
  519. }
  520. $detail['goods_attr_name'] = $attr_info ? $attr_info['name'] : '';
  521. }
  522. }
  523. }
  524. $shipping_type_text = ShippingTypeEnum::getShippingType($order['shipping_type']);
  525. $shipping_type_fields = ShippingTypeEnum::getShippingFields($order['shipping_type']);
  526. $step_arr = StoreEnum::setpArrMap($order['order_status'], $shipping_type_text,$shipping_type_fields);// 订单状态跟踪
  527. $order_action = OrderAction::getOrderAction([['order_id' => $main_order['id']]]);
  528. $marketing_type_map = MarketingTypeEnum::getMap();
  529. $stepArr[] = ['title' => '买家下单', 'created_at' => '0', 'is_step' => 0];
  530. $stepArr[] = ['title' => '买家付款', 'created_at' => '0', 'is_step' => 0,];
  531. $stepArr[] = ['title' => $shipping_type_text, 'created_at' => '0', 'is_step' => 0,];
  532. $stepArr[] = ['title' => '交易完成', 'created_at' => '0', 'is_step' => 0,];
  533. $store = Store::getOne([['id' => $order['store_id']]], true);
  534. //获取售后原因
  535. $refund_reason = RefundReasonEnum::getMap();
  536. $order['create_type_text'] = $order['create_type'] == 1 ? '线上下单' : '系统创建';
  537. $order['option_limit'] = intval($order['supplier_type'] == StoreEnum::SELF_MALL_WUZHOU);
  538. return $this->success('操作成功', compact('order', 'stepArr', 'order_status_list', 'payment_type_list',
  539. 'shipping_type_list', 'step_arr', 'order_action', 'marketing_type_map', 'store', 'refund_reason'), ApiStatusEnum::CODE_SUCCESS, JSON_BIGINT_AS_STRING);
  540. }
  541. }
  542. return $this->render($this->action->id);
  543. }
  544. /**
  545. * 修改备注
  546. * @Author bing
  547. * @DateTime 2021-09-14 18:40:15
  548. * @return void
  549. * @copyright: Copyright (c) 2020 广东七件事集团
  550. */
  551. public function actionRemark()
  552. {
  553. $form = Yii::$app->request->post();
  554. $rules = [[['id'], 'required'], [['id'], 'integer'], [['remark', 'seller_remark'], 'string']];
  555. $res = Yii::$app->services->validate->validate($form, $rules);
  556. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  557. $form['mall_id'] = $this->mall_id;
  558. $form['store_id'] = $this->store_id;
  559. $form['operator_id'] = $this->admin->id ?? 0;
  560. $form['operator_name'] = $this->admin->account ?? '';
  561. $res = StoreOrder::remark($form);
  562. if ($res === false) return $this->error(Goods::getStaticError());
  563. return $this->success();
  564. }
  565. /**
  566. * 发货
  567. * @Author bing
  568. * @DateTime 2021-09-15 14:29:48
  569. * @return void
  570. * @copyright: Copyright (c) 2020 广东七件事集团
  571. */
  572. public function actionShipping()
  573. {
  574. if (\Yii::$app->request->isAjax) {
  575. if (\Yii::$app->request->isGet) {
  576. // 获取订单发货信息
  577. $jd_mch_code = MallSetting::conf($this->mall_id, 'logistics.jd_mch_code');
  578. $express_list = Express::getExpresses([], [], true, '*');
  579. return $this->success('请求成功', compact('express_list', 'jd_mch_code'));
  580. } else {
  581. // 发货
  582. $form = Yii::$app->request->post();
  583. $rules = [
  584. [['shipping_type', 'id'], 'required'],
  585. [['express_id', 'shipping_type', 'id'], 'integer'],
  586. [['express_name', 'express_no', 'memo', 'memo_arr1', 'memo_arr2', 'memo_arr3'], 'string'],
  587. [['order_detail_ids'], 'each', 'rule' => ['integer']],
  588. [['shipping_change', 'customer_name'], 'safe']
  589. ];
  590. $res = Yii::$app->services->validate->validate($form, $rules);
  591. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  592. $form['mall_id'] = $this->mall_id;
  593. $form['store_id'] = $this->store_id;
  594. $form['operator_id'] = $this->admin->id ?? 0;
  595. $form['operator_name'] = $this->admin->account ?? '';
  596. $form['memo'] = json_encode([$form['memo_arr1'],$form['memo_arr2'],$form['memo_arr3']],JSON_UNESCAPED_UNICODE);
  597. if (empty($form['shipping_change'])) {
  598. $res = StoreOrder::OrderExpressDelivery($form);
  599. } else {
  600. $res = StoreOrder::OrderExpressShippingChange($form);// 修改物流
  601. }
  602. if ($res === false) return $this->error(Goods::getStaticError());
  603. return $this->success('发货/修改物流成功');
  604. }
  605. }
  606. }
  607. /**
  608. * 收货
  609. * @param $id
  610. * @param string $member_id
  611. * @throws UnprocessableEntityHttpException
  612. * @throws \yii\web\NotFoundHttpException
  613. */
  614. public function actionTakeDelivery()
  615. {
  616. // 商品操作
  617. $form = Yii::$app->request->post();
  618. $rules = [[['id'], 'required'], [['id'], 'integer']];
  619. $res = Yii::$app->services->validate->validate($form, $rules);
  620. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  621. $form['mall_id'] = $this->mall_id;
  622. $form['store_id'] = $this->store_id;
  623. $form['operator_id'] = $this->admin->id;
  624. $form['operator_name'] = $this->admin->account;
  625. $res = StoreOrder::takeDelivery($form);
  626. if ($res === false) return $this->error(Goods::getStaticError());
  627. return $this->success();
  628. }
  629. /**
  630. * 完成订单
  631. * @param $id
  632. * @throws \yii\web\NotFoundHttpException
  633. */
  634. public function actionSalesOrder()
  635. {
  636. // 商品操作
  637. $form = Yii::$app->request->post();
  638. $rules = [[['id'], 'required'], [['id'], 'integer']];
  639. $res = Yii::$app->services->validate->validate($form, $rules);
  640. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  641. $form['mall_id'] = $this->mall_id;
  642. $form['store_id'] = $this->store_id;
  643. $form['operator_id'] = $this->admin->id ?? 0;
  644. $form['operator_name'] = $this->admin->account ?? '';
  645. $res = StoreOrder::complete($form);
  646. if ($res == false) return $this->error('操作订单失败:' . Order::getStaticError());
  647. return $this->success('操作成功');
  648. }
  649. /**
  650. * 修改收货地址
  651. * @Author bing
  652. * @DateTime 2021-09-14 18:40:15
  653. * @return void
  654. * @copyright: Copyright (c) 2020 广东七件事集团
  655. */
  656. public function actionChangeAddress()
  657. {
  658. $form = Yii::$app->request->post();
  659. $rules = [
  660. [['id', 'receiver_name', 'mobile', 'province', 'city', 'area', 'address', 'region_name'], 'required'],
  661. [['id', 'province', 'city', 'area', 'zip'], 'integer'],
  662. [['address', 'region_name', 'receiver_name', 'mobile'], 'string']
  663. ];
  664. $res = Yii::$app->services->validate->validate($form, $rules);
  665. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  666. $form['mall_id'] = $this->mall_id;
  667. $form['store_id'] = $this->store_id;
  668. $form['operator_id'] = $this->admin->id ?? 0;
  669. $form['operator_name'] = $this->admin->account ?? '';
  670. $form['is_store'] = 1;
  671. $res = StoreOrder::changeAddress($form);
  672. if ($res === false) return $this->error(Goods::getStaticError());
  673. return $this->success();
  674. }
  675. public function actionClose()
  676. {
  677. // 商品操作
  678. $form = Yii::$app->request->post();
  679. $rules = [[['id'], 'required'], [['id'], 'integer']];
  680. $res = Yii::$app->services->validate->validate($form, $rules);
  681. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  682. $form['mall_id'] = $this->mall_id;
  683. $form['store_id'] = $this->store_id;
  684. $form['operator_id'] = $this->admin->id ?? 0;
  685. $form['operator_name'] = $this->admin->account ?? '';
  686. $res = StoreOrder::close($form);
  687. if ($res === false) return $this->error(Goods::getStaticError());
  688. return $this->success();
  689. }
  690. public function actionDel()
  691. {
  692. // 商品操作
  693. $form = Yii::$app->request->post();
  694. $rules = [[['id'], 'required'], [['id'], 'integer']];
  695. $res = Yii::$app->services->validate->validate($form, $rules);
  696. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  697. $form['mall_id'] = $this->mall_id;
  698. $form['store_id'] = $this->store_id;
  699. $form['operator_id'] = $this->admin->id ?? 0;
  700. $form['operator_name'] = $this->admin->account ?? '';
  701. $res = StoreOrder::del($form);
  702. if ($res === false) return $this->error(Goods::getStaticError());
  703. return $this->success();
  704. }
  705. public function actionPay()
  706. {
  707. // 商品操作
  708. $form = Yii::$app->request->post();
  709. $rules = [[['id'], 'required'], [['id'], 'integer']];
  710. $res = Yii::$app->services->validate->validate($form, $rules);
  711. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  712. $form['mall_id'] = $this->mall['id'];
  713. $form['store_id'] = $this->store_id;
  714. $form['operator_id'] = $this->admin->id ?? 0;
  715. $form['operator_name'] = $this->admin->account ?? '';
  716. $res = StoreOrder::pay($form, PayTypeEnum::OFFLINE);
  717. if ($res === false) return $this->error(Goods::getStaticError());
  718. return $this->success();
  719. }
  720. /**
  721. * 售后处理
  722. * @Author: lun
  723. * @DateTime: 2021/10/12 0012 11:15
  724. * @Copyright: copyright (c) 2021 广东七件事集团
  725. * @return mixed|void
  726. */
  727. public function actionHandleRefund()
  728. {
  729. if (\Yii::$app->request->isAjax) {
  730. if (\Yii::$app->request->isPost) {
  731. //商品列表筛选
  732. $params = Yii::$app->request->post();
  733. $rules = [
  734. [['id', 'step_status'], 'required'],
  735. [['id', 'step_status', 'mobile', 'address_type'], 'integer'],
  736. [['saler_express', 'saler_express_no', 'address', 'consignee', 'refund_reason'], 'string'],
  737. [['saler_express', 'saler_express_no', 'address_type', 'customer_name'], 'safe'],
  738. ];
  739. $res = Yii::$app->services->validate->validate($params, $rules);
  740. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  741. // 退货地址
  742. $address = ['consignee' => $params['consignee'], 'mobile' => $params['mobile'], 'address' => $params['address']];
  743. // 换货物流信息
  744. $express = ['saler_express' => $params['saler_express'], 'saler_express_no' => $params['saler_express_no'], 'customer_name' => $params['customer_name']];
  745. $data = [
  746. 'id' => $params['id'],
  747. 'step_status' => $params['step_status'],
  748. 'address' => $address,
  749. 'express' => $express,
  750. 'refund_reason' => $params['refund_reason'] ?? '',
  751. 'is_store' => 1,
  752. 'operator_id' => $this->admin->id
  753. ];
  754. $logic = new OrderRefundLogic();
  755. $res = $logic->execute($this->mall_id, $data);
  756. if ($res == false) return $this->error($logic->getError());
  757. if (in_array($params['step_status'], [RefundStatusEnum::STEP_4, RefundStatusEnum::STEP_6]) && empty($params['is_async'])) {
  758. //触发订单售后完成
  759. $event = new OrderRefundFinishEvent($res['mall_id']);
  760. $data = [];
  761. $data['id'] = $res['id'];
  762. $data['source_table'] = 'order_detail';
  763. $data['source_table_id'] = $res['order_detail_id'];
  764. $data['step_status'] = $params['step_status'];
  765. Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  766. }
  767. return $this->success('操作成功');
  768. }
  769. }
  770. }
  771. public function actionCommissionAndScoreList(){
  772. $params = Yii::$app->request->get();
  773. $wallet_type = 'commission';
  774. if(!empty($params['wallet_type'])) $wallet_type = $params['wallet_type'];
  775. $condition['where'] = [['order_id'=>$params['order_id']]];
  776. $condition['select'] = 'id';
  777. $order_detail_list = StoreOrderDetail::lists($condition);
  778. $order_detail_ids = array_column($order_detail_list,'id');
  779. $store_order = StoreOrder::findOne($params['order_id']);
  780. $order = Order::findOne(['order_no'=>$store_order['order_no']]);
  781. $mall_order_detail_ids = [];
  782. if($order){
  783. $condition['where'] = [['order_id' => $order['id']]];
  784. $condition['select'] = 'id';
  785. $mall_order_detail_list = OrderDetail::lists($condition);
  786. $mall_order_detail_ids = array_column($mall_order_detail_list, 'id');
  787. }
  788. $condition = [
  789. 'where' => [
  790. [
  791. 'or',
  792. [
  793. 'and',
  794. ['source_table'=>['store_order_detail',StoreOrderDetail::tableName()]],
  795. ['source_table_id'=>$order_detail_ids]
  796. ],
  797. [
  798. 'and',
  799. ['source_table' => [OrderDetail::tableName(),'order_detail']],
  800. ['source_table_id' => $mall_order_detail_ids]
  801. ]
  802. ]
  803. ],
  804. ];
  805. CapitalLog::$capitalType = "{$wallet_type}";
  806. $list = CapitalLog::getList($condition,0);
  807. $level = UserLevel::find()->select("name,level")->where(['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED])->asArray()->all();
  808. $level_data = array_column($level,'name','level');
  809. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  810. $condition['where'][] = ['status'=>0];
  811. $frozen_list = CapitalLog::getList($condition,1);
  812. $list = array_merge($list,$frozen_list);
  813. return $this->success('操作成功',['list'=>$list,'user_level'=>$level_data]);
  814. }
  815. public function actionClerk(){
  816. $params = Yii::$app->request->post();
  817. $params['mall_id'] = $this->mall_id;
  818. $params['store_id'] = $this->store_id;
  819. // $params['clerk_code'] = $params['id'];
  820. $store = Store::findOne(['id'=>$this->store_id,'status'=>StatusEnum::ENABLED]);
  821. $storeService = new StoreService();
  822. $storeService->mall_id = $this->mall_id;
  823. $storeService->store_id = $this->store_id;
  824. $params['clerk_id'] = $storeService->getClerkIdByUserId($store['user_id']??0, true);
  825. $rse = StoreClerkLog::setData($params);
  826. if($rse==false) return $this->error(StoreClerkLog::getStaticError());
  827. return $this->success('操作成功');
  828. }
  829. public function actionReserve()
  830. {
  831. $is_show_username = Yii::$app->services->setting->mall->get($this->mall_id, 'order.is_show_username');
  832. return $this->render($this->action->id, ['is_show_username' => $is_show_username ?? 0]);
  833. }
  834. public function actionReserveDetail()
  835. {
  836. return $this->render($this->action->id, ['is_show_username' => $is_show_username ?? 0]);
  837. }
  838. public function actionManualRefund()
  839. {
  840. $params = \Yii::$app->request->post();
  841. $res = \Yii::$app->services->validate->validate($params, [
  842. [['id', 'refund_amount', 'reason'], 'required'],
  843. ]);
  844. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  845. try {
  846. $order = StoreOrder::findOne(['id' => $params['id']]);
  847. if (empty($order)) throw new Exception('订单不存在');
  848. if ($order->order_source != OrderSourceEnum::SOURCE_RESERVE) throw new Exception('当前订单类型不支持手动退款');
  849. if (empty($order->pay_status)) throw new Exception('当前订单未支付');
  850. if ($order->order_status < OrderStatusEnum::PAY) throw new Exception('当前订单状态不支持退款');
  851. $order_detail_id = StoreOrderDetail::find()->where(['order_id' => $params['id']])->select('main_order_detail_id')->scalar();
  852. // 执行退款
  853. $params = [
  854. 'order_detail_id' => $order_detail_id,
  855. 'reason' => $params['reason'],
  856. 'remark' => '',
  857. 'type' => RefundTypeEnum::MONEY,
  858. 'refund_price' => bcmul($params['refund_amount'], 1, 2),
  859. 'user_id' => $order->user_id,
  860. 'mall_id' => $order->mall_id,
  861. ];
  862. $res = OrderRefund::feedback($params);
  863. if ($res === false) throw new \Exception(Order::getStaticError());
  864. $event = new \common\events\order\OrderRefundEvent();
  865. $event->mall_id = $order->mall_id;
  866. $order_detail['order_detail_id'] = $order_detail_id;
  867. $order_detail['id'] = $res->id;
  868. $order_detail['operator_id'] = $this->admin->id;
  869. $order_detail['operator_name'] = $this->admin->account;
  870. $order_detail['is_store'] = 1;
  871. $order_detail['is_manual'] = 1;
  872. Yii::$app->services->events->dispatch($event, $order_detail, $event->listeners);
  873. // 执行退款操作
  874. // 退货地址
  875. $address = ['consignee' => '','mobile' => '','address' => ''];
  876. // 换货物流信息
  877. $express = ['saler_express' => '','saler_express_no' => '','customer_name' => ''];
  878. $data = [
  879. 'id' => $res->id,
  880. 'step_status' => RefundStatusEnum::STEP_6, // 直接打款操作
  881. 'address' => $address,
  882. 'express' => $express,
  883. 'refund_reason' => '',
  884. 'not_dec_fee' => StatusEnum::ENABLED, // 不需要手续费
  885. 'is_store' => 1,
  886. 'is_manual' => 1,
  887. 'operator_id' => $this->admin->id
  888. ];
  889. $logic = new OrderRefundLogic();
  890. $res = $logic->execute($this->mall_id, $data);
  891. if (!$res) throw new \Exception($logic->getError());
  892. } catch (Exception $e) {
  893. return $this->error($e->getMessage());
  894. }
  895. return $this->success();
  896. }
  897. /**
  898. * 撤销售后订单
  899. *
  900. * @return mixed
  901. */
  902. public function actionRevoceRefund(int $id)
  903. {
  904. $service = new RefundService(['mall_id' => $this->mall_id]);
  905. return $service->revoceRefund($id, ['is_store' => 1, 'operator_id' => $this->admin->id])
  906. ? $this->success('撤销售成功')
  907. : $this->error($service->getError());
  908. }
  909. /**
  910. *
  911. * @return mixed|string|void
  912. */
  913. public function actionPrinter()
  914. {
  915. if (\Yii::$app->request->isAjax){
  916. if (\Yii::$app->request->isPost){
  917. $params = Yii::$app->request->post();
  918. $rules = [
  919. [['order_id'], 'required'],
  920. [['order_id'], 'integer'],
  921. [['order_type'], 'safe'],
  922. [['print_type'], 'required'],
  923. [['print_type'], 'integer'],
  924. ];
  925. $res = Yii::$app->services->validate->validate($params, $rules);
  926. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  927. $order_type = null;
  928. if(isset($params['order_type']))$order_type = $params['order_type'];
  929. $storeOrderInfo = StoreOrder::findOne($params['order_id']);
  930. $orderInfo = Order::findOne(['order_no' => $storeOrderInfo->order_no]);
  931. PrinterBase::async_handle(['order_id' => $orderInfo->id,'type' => PrintOrder::PRINT_TYPE_SYSTEM,'order_type'=>$order_type, 'print_type' => $params['print_type'], 'store_order_id' => $storeOrderInfo->id]);
  932. return $this->success('操作成功');
  933. }
  934. }
  935. }
  936. public function actionGetOrderDetail()
  937. {
  938. $params = \Yii::$app->request->get();
  939. $res = \Yii::$app->services->validate->validate($params, [
  940. [['order_detail_id'], 'required'],
  941. ]);
  942. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  943. $select = 'id, main_order_detail_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';
  944. $order_detail = StoreOrderDetail::getOne([['id' => $params['order_detail_id']], 'mall_id' => $this->mall_id], 1, [], false, $select);
  945. if (empty($order_detail)) return $this->error('订单不存在');
  946. //价格等于商品价格加上修改价格
  947. $order_detail['goods_price'] = round($order_detail['goods_price'] + $order_detail['adjust_money'],2);
  948. return $this->success('操作成功',[ $order_detail]);
  949. }
  950. public function actionGetGoodsCommissionList(){
  951. $params = Yii::$app->request->get();
  952. $condition = [
  953. 'where'=>[
  954. ['source_table'=>[StoreOrder::tableName()]],
  955. ['source_table_id'=>$params['order_id']]
  956. ],
  957. ];
  958. $list = [];
  959. $check_order_detail_refund = StoreOrderDetail::findOne(['order_id' => $params['order_id'], 'is_feedback' => 2]);
  960. if (empty($check_order_detail_refund)) {
  961. CapitalLog::$capitalType = "{$params['wallet_type']}";
  962. $extra_list = CapitalLog::getList($condition,0);
  963. if(!empty($extra_list)) $list = $extra_list;
  964. }
  965. $condition = [
  966. 'where' => [
  967. ['source_table' => ['order_detail', StoreOrderDetail::tableName()]],
  968. ['source_table_id' => $params['goods_id']],
  969. ['mall_id' => $this->mall_id],
  970. ['from_type' => ['Agent', 'Area', 'CloudStock', 'Achievement', 'Distribution']],
  971. ],
  972. ];
  973. // $wallet_type = $params['wallet_type'];
  974. CapitalLog::$capitalType = "{$params['wallet_type']}";
  975. $temp_list = CapitalLog::getList($condition,0);
  976. if(!empty($temp_list)) $list = array_merge($list,$temp_list);
  977. return $this->success('操作成功',['list'=>$list]);
  978. }
  979. public function actionDoRefundSubmit()
  980. {
  981. $params = \Yii::$app->request->post();
  982. $res = \Yii::$app->services->validate->validate($params, [
  983. [['order_detail_id', 'reason', 'refund_price', 'settlement_method', 'balance_pre'], 'required'],
  984. [['shipping_status', 'pic_list', 'reason', 'refund_price', 'remark', 'type', 'user_id', 'settlement_method', 'balance_pre', 'main_order_detail_id'], 'safe'],
  985. ]);
  986. // if (empty($params['reason']))
  987. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  988. $key = sprintf('DoRefundSubmit:%s', $params['user_id']);
  989. if (LockHelper::fLock($key)) return $this->error('请勿频繁请求');
  990. // 获取主商城订单详细
  991. $main_order_detail = OrderDetail::find()
  992. ->where(['mall_id' => $this->mall_id, 'id' => $params['main_order_detail_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  993. ->one();
  994. if (empty($main_order_detail)) return $this->error('订单不存在');
  995. $params['mall_id'] = $this->mall_id;
  996. $where = [
  997. ['=', 'order_detail_id', $main_order_detail['id']],
  998. ['=', 'user_id', $params['user_id']],
  999. ['=', 'mall_id', $this->mall_id],
  1000. ['=', 'store_id', $this->store_id],
  1001. ['>', 'step_status', 0],
  1002. ['<>', 'refund_status', RefundStatusEnum::REVOKE]
  1003. ];
  1004. $order_refund_model = OrderRefund::getOne($where, true);
  1005. if ($order_refund_model) {
  1006. LockHelper::unFLock($key);
  1007. return $this->error('该订单商品已经申请过售后');
  1008. }
  1009. // 将申请修改为主商城信息
  1010. $params['order_detail_id'] = $main_order_detail['id'];
  1011. $res = OrderRefund::feedback($params);
  1012. if ($res === false) {
  1013. LockHelper::unFLock($key);
  1014. return $this->error(Order::getStaticError());
  1015. }
  1016. $event = new \common\events\order\OrderRefundEvent();
  1017. $event->mall_id = $this->mall_id;
  1018. $order_detail['order_detail_id'] = $main_order_detail['id'];
  1019. $order_detail['id'] = $res->id;
  1020. $order_detail['operator_id'] = $this->admin->id;
  1021. $order_detail['operator_name'] = $this->admin->account;
  1022. $order_detail['is_admin'] = 1;
  1023. $order_detail['admin_handler_refund'] = UserWalletEnum::ADMIN_HANDLER_REFUND;
  1024. Yii::$app->services->events->dispatch($event, $order_detail, $event->listeners);
  1025. LockHelper::unFLock($key);
  1026. return $this->success('操作成功', ['id' => $res->id]);
  1027. }
  1028. }