CloudCallbackController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers;
  8. use app\jobs\CreatedCloudGoodsUpdateLog;
  9. use app\models\Admin;
  10. use app\models\AgentFrontBind;
  11. use app\models\AgentFrontCentralizeGoods;
  12. use app\models\AgentFrontCentralizeGoodsCancelLog;
  13. use app\models\AgentFrontCentralizeGoodsExt;
  14. use app\models\AgentFrontCentralizeGoodsLog;
  15. use app\models\CloudGoodsUpdateLog;
  16. use app\models\District;
  17. use app\models\DriverLineOrder;
  18. use app\models\Goods;
  19. use app\models\Option;
  20. use app\models\Order;
  21. use app\models\OrderDetail;
  22. use app\models\PurchaseOrder;
  23. use app\models\SaaSLeaguePriceLog;
  24. use app\models\SaasUser;
  25. use app\models\Store;
  26. use app\models\StoreCloud;
  27. use app\models\Supplier;
  28. use app\models\User;
  29. use app\models\VerifyCardSale;
  30. use app\modules\admin\models\GoodsForm;
  31. use app\modules\admin\models\MerchantForm;
  32. use app\modules\admin\models\OrderRevokeForm;
  33. use app\utils\Notice\NoticeSend;
  34. use app\utils\PrintOrder;
  35. use yii\web\Controller;
  36. use app\models\OrderTransit;
  37. /**
  38. * 微信回调类
  39. * Class WechatNotifyController
  40. * @package app\modules\common\controllers
  41. */
  42. class CloudCallbackController extends Controller
  43. {
  44. public function actionIndex(){
  45. $post = post_params();
  46. $content = Option::get('cloud', 0, 'saas');
  47. $contentParams = json_decode($content['value'],true);
  48. $data = [];
  49. $data['action'] = $post['action'];
  50. $data['time'] = $post['time'];
  51. $data['name'] = $contentParams['name'];
  52. $data['pwd'] = md5(md5($contentParams['pwd']));
  53. $data['order_id'] = $post['order_id'];
  54. if ($data['action'] === 'send_order_purchase' || $data['action'] === 'send_order') {
  55. $data['express_name'] = $post['express_name'];
  56. $data['express_no'] = $post['express_no'];
  57. $data['send_type'] = $post['send_type'];
  58. }
  59. if ($data['action'] === 'order_centralized') {
  60. $data['mch_id'] = $post['mch_id'];
  61. $data['supplier_id'] = $post['supplier_id'];
  62. if ($post['type']) {
  63. $data['type'] = $post['type'];
  64. $data['province_id'] = $post['province_id'];
  65. $data['city_id'] = $post['city_id'];
  66. $data['district_id'] = $post['district_id'];
  67. }
  68. }
  69. if ($data['action'] === 'centralized_handle') {
  70. $data['status'] = $post['status'];
  71. }
  72. if ($data['action'] === 'update_cloud_goods') {
  73. $data['goods_id'] = $post['goods_id'];
  74. $data['price_open'] = $post['price_open'];
  75. $data['supplier_id'] = $post['supplier_id'];
  76. }
  77. if ($data['action'] === 'goods_bind_supplier') {
  78. $data['mch_id'] = $post['mch_id'];
  79. }
  80. if ($data['action'] === 'order_cancel_result') {
  81. $data['supplier_id'] = $post['supplier_id'];
  82. $data['type'] = $post['type'];
  83. $data['status'] = $post['status'];
  84. }
  85. if ($data['action'] === 'order_goods_cancel') {
  86. $data['supplier_id'] = $post['supplier_id'];
  87. $data['goods_id'] = $post['goods_id'];
  88. $data['md_id'] = $post['md_id'];
  89. $data['num'] = $post['num'];
  90. }
  91. if($this->setSign($data) != $post['sign']){
  92. $message = [];
  93. $message['code'] = 1;
  94. $message['msg'] = "验证失败";
  95. debug_log("验证失败");
  96. return $this->asJson($message);
  97. exit;
  98. }
  99. if ($data['action'] === 'send_order_purchase' || $data['action'] === 'send_order') {
  100. if ($data['action'] === 'send_order_purchase') {
  101. $orderTransitInfo = OrderTransit::findOne(['cloud_order_id'=>$data['order_id'],'is_delete'=>0]);
  102. } elseif ($data['action'] === 'send_order') {
  103. $orderTransitInfo = PurchaseOrder::findOne(['cloud_order_id'=>$data['order_id'],'is_delete'=>0]);
  104. }
  105. if(!$orderTransitInfo){
  106. $message = [];
  107. $message['code'] = 1;
  108. $message['msg'] = "订单信息不存在";
  109. return $this->asJson($message);
  110. exit;
  111. }
  112. if($data['action'] === 'send_order_purchase' && $orderTransitInfo->status != 0 && !intval($post['update_send_info'])){
  113. $message = [];
  114. $message['code'] = 1;
  115. $message['msg'] = "订单信息有误";
  116. return $this->asJson($message);
  117. exit;
  118. }
  119. if($data['action'] !== 'send_order_purchase' && $orderTransitInfo->trade_status != 0 && !intval($post['update_send_info'])){
  120. $message = [];
  121. $message['code'] = 1;
  122. $message['msg'] = "订单信息有误";
  123. return $this->asJson($message);
  124. exit;
  125. }
  126. $orderTransitInfo->express_no = $post['express_no'];
  127. if ($data['action'] === 'send_order_purchase') {
  128. if (!intval($post['update_send_info'])) {
  129. $orderTransitInfo->status = 1; //已发货
  130. }
  131. $orderTransitInfo->send_type = $post['send_type'];
  132. $orderTransitInfo->express_name = $post['express_name'];
  133. } elseif ($data['action'] === 'send_order') {
  134. if (!intval($post['update_send_info'])) {
  135. $orderTransitInfo->trade_status = PurchaseOrder::ORDER_FLOW_SEND; //已发货
  136. $orderTransitInfo->send_time = time();
  137. }
  138. $orderTransitInfo->express = $post['express_name'];
  139. }
  140. if(!$orderTransitInfo->save()){
  141. $message = [];
  142. $message['code'] = 1;
  143. $message['msg'] = "修改失败";
  144. }else{
  145. if ($data['action'] === 'send_order_purchase') {
  146. $order = Order::findOne($orderTransitInfo->order_id);
  147. $order_id = $orderTransitInfo->order_id;
  148. $order_transit = OrderTransit::find()->where(['order_id' => $order_id])
  149. ->andWhere(['>=', 'status', 1])
  150. ->select('order_detail_ids')->asArray()->all();//;
  151. // $order_transit_count = $order_transit;
  152. $order_transit_count = 0;
  153. foreach ($order_transit as $transit) {
  154. $order_detail_ids = count(json_decode($transit['order_detail_ids']));
  155. $order_transit_count += $order_detail_ids;
  156. }
  157. $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->count();
  158. if ($order_transit_count >= $order_detail) {
  159. $order->express = $post['express_name'] ?? '';
  160. $order->express_no = $post['express_no'] ?? '';
  161. if (!intval($post['update_send_info'])) {
  162. $order->trade_status = 2;
  163. $order->send_time = time();
  164. }
  165. }
  166. if (!$order->save()) {
  167. $message['code'] = 1;
  168. $message['msg'] = "修改失败";
  169. return $this->asJson($message);
  170. }
  171. }
  172. $message = [];
  173. $message['code'] = 0;
  174. $message['msg'] = "success";
  175. }
  176. return $this->asJson($message);
  177. exit;
  178. }
  179. if ($data['action'] === 'order_centralized') {
  180. $cloud_mch_id = $data['mch_id'];
  181. $supplier_id = $data['supplier_id'];
  182. $supplier = Supplier::findOne(['cloud_supplier_id' => $supplier_id]);
  183. $store_cloud = StoreCloud::findOne(['cloud_store_id' => $cloud_mch_id])->store_id;
  184. $store = Store::findOne($store_cloud);
  185. debug_log($cloud_mch_id);
  186. // $option = Option::get('agency_price_config', 0, 'saas');
  187. // if ($option['value']) {
  188. // $option = json_decode($option['value'], true);
  189. // }
  190. // $model_type = $option['model_type'] ?: 0;
  191. $md_id = 0;
  192. //批发
  193. //这里应该要注释了 说是集采订单只能转单了
  194. if ($data['type']) {
  195. $supplier_bind_admin_id = AgentFrontBind::find()->where([
  196. 'type' => AgentFrontBind::TYPE_SUPPLIER,
  197. 'type_id' => $supplier->id,
  198. 'status' => 1,
  199. 'is_delete' => 0,
  200. ])->select('front_agent_admin_id')->column();
  201. if (!$supplier_bind_admin_id) {
  202. $message['code'] = 1;
  203. $message['msg'] = "供货商未找到对应的前置仓代理";
  204. return $this->asJson($message);
  205. }
  206. $province_id = $data['province_id'];
  207. $city_id = $data['city_id'];
  208. $district_id = $data['district_id'];
  209. $admin = Admin::find()
  210. ->where(['is_delete' => 0])
  211. ->andWhere([
  212. 'area_level' => 1,
  213. 'province_id' => $province_id,
  214. 'city_id' => $city_id,
  215. 'district_id' => $district_id,
  216. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT,
  217. 'id' => $supplier_bind_admin_id
  218. ])->one();
  219. if (empty($admin)) {
  220. $admin = Admin::find()
  221. ->where(['is_delete' => 0])
  222. ->andWhere([
  223. 'area_level' => 2,
  224. 'province_id' => $province_id,
  225. 'city_id' => $city_id,
  226. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT,
  227. 'id' => $supplier_bind_admin_id
  228. ])->one();
  229. if (empty($admin)) {
  230. $admin = Admin::find()
  231. ->where(['is_delete' => 0])
  232. ->andWhere([
  233. 'area_level' => 3,
  234. 'province_id' => $province_id,
  235. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT,
  236. 'id' => $supplier_bind_admin_id
  237. ])->one();
  238. }
  239. }
  240. } else {
  241. //转单
  242. $province_id = $store->province_id;
  243. $city_id = $store->city_id;
  244. $district_id = $store->district_id;
  245. debug_log(['data' => $data, 'post' => $post]);
  246. //判断门店是否绑定的有前置仓信息
  247. // $orderTransit = OrderTransit::findOne(['cloud_order_id' => $data['order_id']]);
  248. // $store_order = Order::findOne($orderTransit->order_id);
  249. $md_id = $post['md_id'];
  250. $supplier_bind_admin_id = AgentFrontBind::find()->where([
  251. 'type' => AgentFrontBind::TYPE_SUPPLIER,
  252. 'type_id' => $supplier->id,
  253. 'status' => 1,
  254. 'is_delete' => 0,
  255. ])->select('front_agent_admin_id')->column();
  256. if (!$supplier_bind_admin_id) {
  257. $message['code'] = 1;
  258. $message['msg'] = "供货商未找到对应的前置仓代理";
  259. return $this->asJson($message);
  260. }
  261. if ($md_id > 0) {
  262. $bind_agent_admin = AgentFrontBind::findOne([
  263. 'type' => AgentFrontBind::TYPE_STORE,
  264. 'status' => 1,
  265. 'is_delete' => 0,
  266. 'md_id' => $md_id,
  267. 'front_agent_admin_id' => $supplier_bind_admin_id
  268. ]);
  269. }
  270. if (!isset($bind_agent_admin)) {
  271. $bind_agent_admin = AgentFrontBind::findOne([
  272. 'type' => AgentFrontBind::TYPE_STORE,
  273. 'type_id' => $store->id,
  274. 'status' => 1,
  275. 'is_delete' => 0,
  276. 'md_id' => 0,
  277. 'front_agent_admin_id' => $supplier_bind_admin_id
  278. ]);
  279. }
  280. $admin = Admin::findOne(['id' => $bind_agent_admin->front_agent_admin_id]);
  281. }
  282. // if ((int)$model_type === 0) {
  283. // $admin = Admin::find()
  284. // ->where(['is_delete' => 0])
  285. // ->andWhere([
  286. // 'area_level' => 2,
  287. // 'province_id' => $province_id,
  288. // 'city_id' => $city_id
  289. // ])->one();
  290. // } else {
  291. // $admin = Admin::find()
  292. // ->where(['is_delete' => 0])
  293. // ->andWhere([
  294. // 'area_level' => 1,
  295. // 'province_id' => $province_id,
  296. // 'city_id' => $city_id,
  297. // 'district_id' => $district_id
  298. // ])->one();
  299. // }
  300. $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column();
  301. $district = implode(' ', $district);
  302. $message['code'] = 1;
  303. $message['msg'] = "未找到" . $district . "区域下的前置仓代理";
  304. if ($admin) {
  305. $message = [];
  306. $message['code'] = 0;
  307. $message['msg'] = "success";
  308. $message['data'] = [
  309. 'admin_id' => $admin->id,
  310. 'md_id' => $md_id
  311. ];
  312. }
  313. debug_log(json_encode($message));
  314. return $this->asJson($message);
  315. exit;
  316. }
  317. if ($data['action'] === 'centralized_handle') {
  318. $order_id = json_decode($data['order_id'], true);
  319. if (!empty($order_id) && is_array($order_id)) {
  320. $purchase = PurchaseOrder::find()->where(['cloud_order_id' => $order_id, 'is_delete' => 0])->all();
  321. foreach ($purchase as $item) {
  322. $item->centralized_status = ((int)$data['status'] + 1);
  323. if ((int)$data['status'] === 3) {
  324. $item->trade_status = 2;
  325. }
  326. $item->save();
  327. }
  328. }
  329. $message = [];
  330. $message['code'] = 0;
  331. $message['msg'] = "success";
  332. return $this->asJson($message);
  333. exit;
  334. }
  335. if ($data['action'] === 'send_order_purchase_group') {
  336. $order_id = $data['order_id'];
  337. $order = OrderTransit::findOne(['cloud_order_id' => $order_id]);
  338. $order->status = 1;
  339. // $order->confirm_time = time();
  340. $order->save();
  341. $order_transit = OrderTransit::find()->where(['order_id' => $order->order_id])
  342. ->andWhere(['>=', 'status', 1])
  343. ->select('order_detail_ids')->asArray()->all();//;
  344. $order_transit_count = 0;
  345. foreach ($order_transit as $transit) {
  346. $order_detail_ids = count(json_decode($transit['order_detail_ids']));
  347. $order_transit_count += $order_detail_ids;
  348. }
  349. $order_detail = OrderDetail::find()->where(['order_id' => $order->order_id])->count();
  350. debug_log($order_transit_count . '-----' . $order_detail, 'cloud.log');
  351. if ($order_transit_count >= $order_detail) {
  352. $order = Order::findOne($order->order_id);
  353. $order->trade_status = 2;
  354. $order->send_time = time();
  355. if (!$order->save()) {
  356. return $this->asJson([
  357. 'code' => 1,
  358. 'msg' => json_encode($order->errors)
  359. ]);
  360. }
  361. }
  362. $message = [];
  363. $message['code'] = 0;
  364. $message['msg'] = "success";
  365. return $this->asJson($message);
  366. }
  367. if ($data['action'] === 'update_cloud_goods') {
  368. $cloud_goods_id = $data['goods_id'];
  369. $cloud_supplier_id = $data['supplier_id'];
  370. $price_open = $data['price_open'];
  371. $num_open = intval($post['num_open']);
  372. $store_id = (int)$post['store_id'];
  373. $andWhere = [];
  374. if($store_id){
  375. $andWhere['store_id'] = $store_id;
  376. }
  377. $goods = Goods::find()->where(['cloud_goods_id' => $data['goods_id'], 'is_delete' => 0])
  378. ->andWhere($andWhere)
  379. ->andWhere(['OR', ['is', 'is_wholesale', null], ['<=', 'is_wholesale', 0]])->asArray()->all();
  380. foreach ($goods as $item) {
  381. //获取商城是否设置云仓产品自动更新
  382. // $cloud_is_update = Option::get('cloud_is_update', $item['store_id'], 'store')['value'];
  383. $log = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $cloud_goods_id, 'store_id' => $item['store_id']]) ?? new CloudGoodsUpdateLog();
  384. $log->cloud_supplier_id = $cloud_supplier_id;
  385. $log->cloud_goods_id = $cloud_goods_id;
  386. $log->store_id = $item['store_id'];
  387. $log->goods_id = $item['id'];
  388. $log->is_update = 0;
  389. $log->is_audit = $price_open;
  390. $log->status = (int)$item['status'];
  391. $log->created_at = time();
  392. $log->updated_at = '0';
  393. $log->save();
  394. debug_log(json_encode($log->errors));
  395. }
  396. // $cloud_goods_id = $data['goods_id'];
  397. $form = new GoodsForm();
  398. //找出商品未审核或者未更新的产品
  399. $CloudGoodsUpdateLog = CloudGoodsUpdateLog::find()->where(['cloud_goods_id' => $cloud_goods_id, 'updated_at' => 0])->andWhere($andWhere)->asArray()->all();
  400. foreach ($CloudGoodsUpdateLog as $value) {
  401. $goods_info = Goods::findOne(['cloud_goods_id' => $cloud_goods_id, 'store_id' => $value['store_id'], 'is_wholesale' => 0]);
  402. $goods_info->status = 0;
  403. $goods_info->save();
  404. //判断是否开启自动更新
  405. $cloud_is_update = Option::get('cloud_is_update', $value['store_id'], 'store')['value'];
  406. if ($data['price_open'] || intval($cloud_is_update)) {
  407. //直接通过审核(目前只有点击审核通过时才会调用此事件)
  408. $item = CloudGoodsUpdateLog::findOne($value['id']);
  409. $item->is_audit = 1;
  410. $item->updated_at = time();
  411. //更新产品逻辑
  412. if ($value['goods_id']) {
  413. $StoreCloud = StoreCloud::findOne(['store_id' => $value['store_id'], 'is_delete' => 0, 'is_enable' => 1]);
  414. $form->id = $value['cloud_goods_id'];
  415. $form->store_id =$value['store_id'];
  416. $goodsInfo = $form->saveCloudGoods($num_open);
  417. $merchantForm = new MerchantForm();
  418. $merchantForm->token_stroe_cloud_id = $StoreCloud->id;
  419. debug_log('store_id' . $value['store_id']);
  420. $result = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'], $goodsInfo['goods_id'], $value['store_id']);
  421. if ((int)$result['code'] !== 0) {
  422. return $result;
  423. }
  424. $item->is_update = 1;
  425. }
  426. $item->save();
  427. }
  428. }
  429. $message['code'] = 0;
  430. $message['msg'] = "success";
  431. return $this->asJson($message);
  432. }
  433. //赤店返回绑定该前置仓的供货商
  434. if ($data['action'] === 'goods_bind_supplier') {
  435. $store_cloud = StoreCloud::findOne(['cloud_store_id' => $data['mch_id'], 'is_delete' => 0]);
  436. if ($store_cloud) {
  437. $agent_front_bind = AgentFrontBind::findOne(['type' => 1, 'status' => 1, 'type_id' => $store_cloud->store_id, 'is_delete' => 0]);
  438. if ($agent_front_bind) {
  439. $front_agent_admin_id = $agent_front_bind->front_agent_admin_id;
  440. $supplier_id = AgentFrontBind::find()
  441. ->where(['type' => 0, 'status' => 1, 'is_delete' => 0, 'front_agent_admin_id' => $front_agent_admin_id])
  442. ->select('type_id')->column();
  443. $supplier_id = Supplier::find()->where(['id' => $supplier_id, 'is_delete' => 0])->select('cloud_supplier_id')->column();
  444. return $this->asJson([
  445. 'code' => 0,
  446. 'msg' => 'success',
  447. 'data' => $supplier_id
  448. ]);
  449. } else {
  450. return $this->asJson([
  451. 'code' => 0,
  452. 'msg' => 'success',
  453. 'data' => [0]
  454. ]);
  455. }
  456. }
  457. return $this->asJson([
  458. 'code' => 1,
  459. 'msg' => '未查找到用户信息'
  460. ]);
  461. }
  462. //供货商同意取消订单(可能是三方操作)
  463. if ($data['action'] === 'order_cancel_result') {
  464. if (intval($data['status']) === 2) {
  465. return $this->asJson([
  466. 'code' => 0,
  467. 'msg' => '完成。已拒绝'
  468. ]);
  469. }
  470. //如果已经转单 就调用取消接口
  471. $orderTransit = OrderTransit::findOne(['cloud_order_id' => $data['order_id'], 'is_delete' => 0]);
  472. if (!$orderTransit) {
  473. return $this->asJson([
  474. 'code' => 1,
  475. 'msg' => '未查找到云仓订单'
  476. ]);
  477. }
  478. if (intval($orderTransit->status) == -1) {
  479. return $this->asJson([
  480. 'code' => 1,
  481. 'msg' => '订单已取消'
  482. ]);
  483. }
  484. $order = Order::findOne($orderTransit->order_id);
  485. $form = new OrderRevokeForm();
  486. $form->order_id = $order->id;
  487. $form->delete_pass = true;
  488. $form->user_id = $order->user_id;
  489. $form->store_id = $order->store_id;
  490. $res = $form->save();
  491. if ($res['code'] == 0) {
  492. VerifyCardSale::cancelCard($order);
  493. $order->save();
  494. $goods = Goods::findOne(OrderDetail::findOne(['order_id' => $order->id])->goods_id);
  495. if ($order->order_type == Order::ORDER_TYPE_Adopt) {
  496. NoticeSend::OrderCancel($order->user_id, $order->mobile, $order->order_no, $order->pay_price, $goods->name, 1);
  497. }else{
  498. NoticeSend::OrderCancel($order->user_id, $order->mobile, $order->order_no, $order->pay_price, $goods->name, 0);
  499. }
  500. // 订单打印
  501. if ((int)$order->md_id === -1 || (int)$order->md_id === 0 || !isset($order->md_id)) {
  502. $order->md_id = 0;
  503. }
  504. $printer_order = new PrintOrder($order->store_id, $order->id, 'confirm', 0, $order->md_id, 0, $order['mch_id']);
  505. $printer_order->is_refund = true;
  506. $printer_order->print_order();
  507. if($order->take_price > 0){
  508. $user = User::findOne($order->user_id);
  509. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  510. if($saas_user){
  511. $take_price = $order->take_price;
  512. $before = $saas_user->league_price;
  513. $saas_user->updateCounters(['league_price' => floatval($take_price)]);
  514. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  515. $order->store_id,
  516. $saas_user->id,
  517. $take_price,
  518. $before,
  519. \app\models\SaaSLeaguePriceLog::TYPE_CANCEL,
  520. \app\models\SaaSLeaguePriceLog::SEND_TYPE,
  521. \app\models\SaaSLeaguePriceLog::ROLE_USER,
  522. $order->id
  523. );
  524. }
  525. }
  526. $orderTransit->status = -1;
  527. $orderTransit->save();
  528. return $this->asJson([
  529. 'code' => 0,
  530. 'msg' => '退款完成。'
  531. ]);
  532. }
  533. return $this->asJson([
  534. 'code' => 0,
  535. 'msg' => '退款失败。'
  536. ]);
  537. }
  538. //订单商品取消
  539. if ($data['action'] === 'order_goods_cancel') {
  540. // $cloud_order_goods_cancel = cache()->get('cloud_order_goods_cancel');
  541. // if ($cloud_order_goods_cancel) {
  542. // return $this->asJson([
  543. // 'code' => 0,
  544. // 'msg' => '请稍后重试。'
  545. // ]);
  546. // } else {
  547. // cache()->set('cloud_order_goods_cancel', $data['order_id'], 3);
  548. // }
  549. $goods_log = AgentFrontCentralizeGoodsLog::find()->where(['cloud_order_id' => $data['order_id']])
  550. ->select('centralize_goods_id')->column();
  551. if (!$goods_log) {
  552. return $this->asJson([
  553. 'code' => 1,
  554. 'msg' => '获取商品失败'
  555. ]);
  556. }
  557. debug_log(['goods_log' => $goods_log], '20250721.log');
  558. debug_log(['post' => $post], '20250721.log');
  559. $attr = json_decode($post['attr'], true);
  560. $attr_id_arr = array_column($attr, 'attr_id');
  561. sort($attr_id_arr);
  562. //找到对应的商品 进行扣减分拣数量 这里正常来讲只会找到一个
  563. $agentFrontCentralizeGoodsArray = AgentFrontCentralizeGoods::find()->where([
  564. 'id' => $goods_log,
  565. 'cloud_goods_id' => $data['goods_id']
  566. ])->asArray()->all();
  567. if (!$agentFrontCentralizeGoodsArray) {
  568. return $this->asJson([
  569. 'code' => 1,
  570. 'msg' => '获取商品失败AgentFrontCentralizeGoods'
  571. ]);
  572. }
  573. debug_log(['agentFrontCentralizeGoodsArray' => $agentFrontCentralizeGoodsArray], '20250721.log');
  574. $centralize_goods_id = 0;
  575. foreach ($agentFrontCentralizeGoodsArray as $centralizeGoodsItem) {
  576. $centralizeGoodsItemAttr = json_decode($centralizeGoodsItem['attr'], true);
  577. sort($centralizeGoodsItemAttr);
  578. if (!array_diff($attr_id_arr, $centralizeGoodsItemAttr)) {
  579. $centralize_goods_id = $centralizeGoodsItem['id'];
  580. }
  581. }
  582. debug_log(['centralize_goods_id' => $centralize_goods_id], '20250721.log');
  583. $agentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne($centralize_goods_id);
  584. if (!$agentFrontCentralizeGoods) {
  585. return $this->asJson([
  586. 'code' => 1,
  587. 'msg' => '获取商品失败AgentFrontCentralizeGoods'
  588. ]);
  589. }
  590. ///
  591. $agentFrontCentralizeGoodsExt = AgentFrontCentralizeGoodsExt::findOne([
  592. 'centralize_goods_id' => $centralize_goods_id,
  593. 'md_id' => $data['md_id']
  594. ]);
  595. if (!$agentFrontCentralizeGoodsExt) {
  596. return $this->asJson([
  597. 'code' => 1,
  598. 'msg' => '获取商品失败AgentFrontCentralizeGoodsExt'
  599. ]);
  600. }
  601. //如果是待分拣状态那么就减去对应的数量
  602. if (intval($agentFrontCentralizeGoodsExt->status) === AgentFrontCentralizeGoodsExt::STATUS_WAIT_SORTING) {
  603. if ($agentFrontCentralizeGoodsExt->goods_num - $data['num'] >= $agentFrontCentralizeGoodsExt->sorting_num) {
  604. if (intval($agentFrontCentralizeGoods->status) === AgentFrontCentralizeGoods::STATUS_SORTING) {
  605. if ($agentFrontCentralizeGoods->goods_num - $data['num'] >= $agentFrontCentralizeGoods->sorting_num) {
  606. $agentFrontCentralizeGoods->updateCounters(['goods_num' => -$data['num']]);
  607. $agentFrontCentralizeGoodsExt->updateCounters(['goods_num' => -$data['num']]);
  608. }
  609. }
  610. }
  611. }
  612. $cancel_log = new AgentFrontCentralizeGoodsCancelLog();
  613. $cancel_log->front_admin_id = $agentFrontCentralizeGoods->front_admin_id;
  614. $cancel_log->supplier_id = $data['supplier_id'];
  615. $cancel_log->centralize_goods_id = $agentFrontCentralizeGoods->id;
  616. $cancel_log->cloud_order_id = $data['order_id'];
  617. $cancel_log->md_id = $data['md_id'];
  618. $cancel_log->goods_num = $data['num'];
  619. if (!$cancel_log->save()) {
  620. debug_log(['centralize_goods_id' => $cancel_log->errors], '20250721.log');
  621. };
  622. ///这里需要增加一下DriverLineOrder中的拦截的商品数量
  623. $driverLineOrder = DriverLineOrder::findOne(['centralize_goods_ext_id' => $agentFrontCentralizeGoodsExt->id, 'md_id' => $data['md_id']]);
  624. if (!$driverLineOrder) {
  625. return $this->asJson([
  626. 'code' => 1,
  627. 'msg' => '获取商品失败DriverLineOrder'
  628. ]);
  629. }
  630. $driverLineOrder->updateCounters(['cancel_goods_num' => $data['num']]);
  631. $cancel_log->driver_line_order_id = $driverLineOrder->id;
  632. $cancel_log->save();
  633. return $this->asJson([
  634. 'code' => 0,
  635. 'msg' => '完成。操作完成'
  636. ]);
  637. }
  638. }
  639. public static function setSign($data)
  640. {
  641. ksort($data);
  642. $str = '';
  643. foreach($data as $key => $val) {
  644. $str .= $str ? '&'.$key.'='.$val : $key.'='.$val;
  645. }
  646. return md5($str);
  647. }
  648. }