OrderSubmitForm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\purchase\order;
  8. use app\constants\OptionSetting;
  9. use app\models\Address;
  10. use app\models\Admin;
  11. use app\models\Cart;
  12. use app\models\common\CommonOrder;
  13. use app\models\DeliveryInfo;
  14. use app\models\District;
  15. use app\models\FoodCart;
  16. use app\models\FoodFlag;
  17. use app\models\Goods;
  18. use app\models\GoodsBook;
  19. use app\models\Md;
  20. use app\models\MdProfit;
  21. use app\models\Option;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\Register;
  25. use app\models\Store;
  26. use app\models\Supplier;
  27. use app\models\User;
  28. use app\models\UserCoupon;
  29. use app\models\VerifyCard;
  30. use app\models\VerifyCardSale;
  31. use app\models\VerifyCardLog;
  32. use app\plugins\adopt\models\AdoptGoods;
  33. use app\plugins\adopt\models\AdoptOrderInfo;
  34. use app\utils\CloudPrint;
  35. use app\utils\Delivery\Delivery;
  36. use app\utils\Notice\NoticeSend;
  37. use app\utils\OrderNo;
  38. use app\utils\PrintOrder;
  39. use app\utils\Tools;
  40. use yii\helpers\Json;
  41. use app\modules\alliance\models\purchase\order\OrderSubmitPreviewForm;
  42. use app\models\PurchaseOrder;
  43. use app\models\PurchaseOrderDetail;
  44. use app\models\PurchaseCart;
  45. class OrderSubmitForm extends OrderForm
  46. {
  47. public $user;
  48. public $payment;
  49. public $cart_id_list;
  50. public $attr_list;
  51. public $goods_id;
  52. public $remark;
  53. public function rules()
  54. {
  55. $rules = [
  56. [['payment'], 'integer'],
  57. ['address_id', 'integer'],
  58. [['cart_id_list', 'attr_list', 'goods_id', 'remark'], 'safe'],
  59. ];
  60. return $rules;
  61. }
  62. public function afterValidate() {
  63. $this->address = $this->getAddressData();
  64. }
  65. public function save()
  66. {
  67. if (!$this->validate())
  68. return [
  69. 'code' => 1,
  70. 'msg' => $this->getErrorSummary(false)[0],
  71. ];
  72. if(empty($this->address)){
  73. return [
  74. 'code' => 1,
  75. 'msg' => '请指定收货地址',
  76. ];
  77. }
  78. try {
  79. $form = new OrderSubmitPreviewForm();
  80. $form->attributes = $this->attributes;
  81. $form->store_id = $this->store_id;
  82. $form->user_id = $this->user_id;
  83. $form->cart_id_list = $this->cart_id_list;
  84. $form->attr_list = $this->attr_list;
  85. $form->goods_id = $this->goods_id;
  86. $form->is_pay = 0;
  87. $prev = $form->search();
  88. if($prev['code'] != 0){
  89. return $prev;
  90. }
  91. $mchListData = $prev['data'];
  92. } catch (\Exception $e) {
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage()
  96. ];
  97. }
  98. $order_id_list = [];
  99. /**
  100. * @var $address Address
  101. */
  102. $address = (object)($this->address);
  103. $t = \Yii::$app->db->beginTransaction();
  104. foreach ($mchListData['goods_list'] as $mch) {
  105. $mch_goods = $mch['goods_list'];
  106. $check_res = $this->checkAdmin($mch_goods);
  107. if ($check_res['code'] === 1) {
  108. return $check_res;
  109. }
  110. // if($mch['in_rule'] == 0){
  111. // $t->rollBack();
  112. // return [
  113. // 'code' => 1,
  114. // 'msg' => '收货地址不支持快递',
  115. // ];
  116. // }
  117. if ($mch_goods) {
  118. $data = [
  119. 'province_id' => $address->province_id,
  120. 'city_id' => $address->city_id,
  121. 'district_id' => $address->district_id,
  122. ];
  123. //代理检测
  124. $address_res = $this->checkAddress($data, $mch_goods);
  125. if ($address_res['code'] !== 0) {
  126. return $address_res;
  127. }
  128. //胜天半子检测
  129. $stbz_res = $this->checkStbz($mch_goods, $address);
  130. if ($stbz_res['code'] !== 0) {
  131. return $stbz_res;
  132. }
  133. $order = new PurchaseOrder();
  134. $order->store_id = $this->store_id;
  135. $order->user_id = $this->user_id;
  136. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_ALLIANCE_PURCHASE);
  137. $order->pay_price = $mchListData['express_price_total'] + $mchListData['price_total'];
  138. $order->supplier_id = $mch['supplier']['id'];
  139. $order->cloud_order_id = $mchListData['cloud_order_id'];
  140. $order->created_at = time();
  141. $order->remark = $this->remark ? : '';
  142. if ($this->payment == 2) {
  143. $order->pay_type = 2;
  144. $order->is_pay = 0;
  145. }
  146. if ($this->payment == 3) {
  147. $order->pay_type = 3;
  148. $order->is_pay = 0;
  149. }
  150. $order->address = $address->province . $address->city . $address->district . $address->detail;
  151. $order->mobile = $address->mobile;
  152. $order->name = $address->name;
  153. $order->province_id = $address->province_id;
  154. $order->city_id = $address->city_id;
  155. $order->district_id = $address->district_id;
  156. $order->address_data = json_encode([
  157. 'province' => trim($address->province),
  158. 'city' => trim($address->city),
  159. 'district' => trim($address->district),
  160. 'detail' => trim($address->detail),
  161. 'latitude' => $address->latitude,
  162. 'longitude' => $address->longitude,
  163. ], JSON_UNESCAPED_UNICODE);
  164. $order->express_price = $mchListData['express_price_total'];
  165. $order->total_price = $mchListData['price_total'];
  166. $order->saas_id = get_saas_user_id();
  167. if ($order->save()) {
  168. // 处理订单生成之后其他相关数据
  169. $orderRes = $this->insertData($mch, $order);
  170. if ($orderRes['code'] == 1) {
  171. $t->rollBack();
  172. return $orderRes;
  173. }
  174. // $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0);
  175. // $printer_order->print_order();
  176. // 订单提交完成发送消息
  177. // NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name']);
  178. $order_id_list[] = $order->id;
  179. } else {
  180. $t->rollBack();
  181. return $this->getErrorResponse($order);
  182. }
  183. }
  184. }
  185. if (count($order_id_list) > 0) {
  186. $t->commit();
  187. //多个订单合并
  188. return [
  189. 'code' => 0,
  190. 'msg' => '订单提交成功',
  191. 'data' => (object)[
  192. 'order_id_list' => $order_id_list,
  193. 'add_time' => $order->created_at
  194. ],
  195. ];
  196. } else {
  197. $t->rollBack();
  198. return [
  199. 'code' => 1,
  200. 'msg' => '订单提交失败',
  201. 'order_list' => $order_id_list
  202. ];
  203. }
  204. }
  205. public function checkAdmin($goods_info) {
  206. //判断是否有集采商品且存在区域代理
  207. foreach ($goods_info as $item) {
  208. if ((int)$item['goods']['send_type'] === 1) {
  209. // $option = Option::get('agency_price_config', 0, 'saas');
  210. // if ($option['value']) {
  211. // $option = json_decode($option['value'], true);
  212. // }
  213. // $model_type = $option['model_type'] ?: 0;
  214. $province_id = $this->address['province_id'];
  215. $city_id = $this->address['city_id'];
  216. $district_id = $this->address['district_id'];
  217. $admin = Admin::find()
  218. ->where(['is_delete' => 0])
  219. ->andWhere([
  220. 'area_level' => 1,
  221. 'province_id' => $province_id,
  222. 'city_id' => $city_id,
  223. 'district_id' => $district_id,
  224. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  225. ])->one();
  226. if (empty($admin)) {
  227. $admin = Admin::find()
  228. ->where(['is_delete' => 0])
  229. ->andWhere([
  230. 'area_level' => 2,
  231. 'province_id' => $province_id,
  232. 'city_id' => $city_id,
  233. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  234. ])->one();
  235. if (empty($admin)) {
  236. $admin = Admin::find()
  237. ->where(['is_delete' => 0])
  238. ->andWhere([
  239. 'area_level' => 3,
  240. 'province_id' => $province_id,
  241. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  242. ])->one();
  243. }
  244. }
  245. if (!$admin) {
  246. $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column();
  247. $district = implode(' ', $district);
  248. return [
  249. 'code' => 1,
  250. 'msg' => "未找到对应".$district."区域下的代理"
  251. ];
  252. }
  253. }
  254. }
  255. }
  256. public function getOrderNo()
  257. {
  258. $order_no = null;
  259. while (true) {
  260. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  261. $exist_order_no = Order::find()->where(['order_no' => $order_no])->exists();
  262. if (!$exist_order_no) {
  263. break;
  264. }
  265. }
  266. return $order_no;
  267. }
  268. /**
  269. * @param $mch
  270. * @param $order Order
  271. * @return array
  272. */
  273. private function insertData($mch, $order)
  274. {
  275. // 计算商品相关
  276. $goods_list = $mch['goods_list'];
  277. foreach ($goods_list as $goods) {
  278. foreach ($goods['cart'] as $cart) {
  279. // 删除购物车
  280. if (isset($cart['cart_id']) && $cart['cart_id'] > 0) {
  281. PurchaseCart::updateAll(['is_delete' => 1], ['id' => $cart['cart_id']]);
  282. }
  283. $order_detail = new PurchaseOrderDetail();
  284. $order_detail->order_id = $order->id;
  285. $order_detail->goods_id = $goods['goods']['id'];
  286. $order_detail->num = $cart['num'];
  287. $order_detail->total_price = $cart['cart_price'];
  288. $order_detail->attr = json_encode($cart['attr_list'], JSON_UNESCAPED_UNICODE);
  289. $order_detail->pic = $goods['goods']['pic_url'];
  290. $order_detail->goods_name = $goods['goods']['name'];
  291. $order_detail->send_type = (int)$goods['goods']['send_type'];
  292. if (!$order_detail->save()) {
  293. return [
  294. 'code' => 1,
  295. 'msg' => '订单提交失败,请稍后再重试',
  296. 'error' => $order_detail->errors
  297. ];
  298. }
  299. }
  300. }
  301. $order->afterOtherDataSave();
  302. return [
  303. 'code' => 0,
  304. 'msg' => ''
  305. ];
  306. }
  307. public function checkAddress($data, $mch_goods = null) {
  308. try {
  309. $option = Option::get('agency_price_config', 0, 'saas');
  310. if ($option['value']) {
  311. $option = json_decode($option['value'], true);
  312. }
  313. // $model_type = $option['model_type'] ?: 0;
  314. if (empty($data['province_id']) || empty($data['city_id']) || empty($data['district_id'])) {
  315. throw new \Exception('地址信息为空');
  316. }
  317. $province_id = $data['province_id'];
  318. $city_id = $data['city_id'];
  319. $district_id = $data['district_id'];
  320. $message['code'] = 0;
  321. $message['msg'] = "success";
  322. if (empty($mch_goods)) {
  323. throw new \Exception('商品为空');
  324. }
  325. foreach ($mch_goods as $good) {
  326. if ((int)$good['goods']['send_type'] === 1) {
  327. $admin = Admin::find()
  328. ->where(['is_delete' => 0])
  329. ->andWhere([
  330. 'area_level' => 1,
  331. 'province_id' => $province_id,
  332. 'city_id' => $city_id,
  333. 'district_id' => $district_id,
  334. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  335. ])->one();
  336. if (empty($admin)) {
  337. $admin = Admin::find()
  338. ->where(['is_delete' => 0])
  339. ->andWhere([
  340. 'area_level' => 2,
  341. 'province_id' => $province_id,
  342. 'city_id' => $city_id,
  343. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  344. ])->one();
  345. if (empty($admin)) {
  346. $admin = Admin::find()
  347. ->where(['is_delete' => 0])
  348. ->andWhere([
  349. 'area_level' => 3,
  350. 'province_id' => $province_id,
  351. 'type' => Admin::ADMIN_TYPE_FRONT_AGENT
  352. ])->one();
  353. }
  354. }
  355. // if (!$admin) {
  356. // $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column();
  357. // $district = implode(' ', $district);
  358. // if ((int)$model_type === 0) {
  359. // $admin = Admin::find()
  360. // ->where(['is_delete' => 0])
  361. // ->andWhere([
  362. // 'area_level' => 2,
  363. // 'province_id' => $province_id,
  364. // 'city_id' => $city_id
  365. // ])->one();
  366. // } else {
  367. // $admin = Admin::find()
  368. // ->where(['is_delete' => 0])
  369. // ->andWhere([
  370. // 'area_level' => 1,
  371. // 'province_id' => $province_id,
  372. // 'city_id' => $city_id,
  373. // 'district_id' => $district_id
  374. // ])->one();
  375. // }
  376. if (empty($admin)) {
  377. $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column();
  378. $district = implode(' ', $district);
  379. return [
  380. 'code' => 1,
  381. 'msg' => "未找到".$district."区域下的前置仓"
  382. ];
  383. }
  384. }
  385. }
  386. debug_log(json_encode($message));
  387. return $message;
  388. } catch (\Exception $e) {
  389. return [
  390. 'code' => 1,
  391. 'msg' => $e->getMessage()
  392. ];
  393. }
  394. }
  395. public function checkStbz($data, $address = null) {
  396. try {
  397. if (!empty($data)) {
  398. foreach ($data as $value) {
  399. $goods_url = "/goods/getAttrParams";
  400. $param = [
  401. 'id' => $value['goods']['id']
  402. ];
  403. //请求接口
  404. $domain = (new OptionSetting)->getCloudDomainName();
  405. $goodsInfo = cloud_post($domain . $goods_url, $param);
  406. $goodsInfo = json_decode($goodsInfo, true);
  407. if ((int)$goodsInfo['code'] !== 0) {
  408. throw new \Exception($goodsInfo['msg']);
  409. }
  410. if ($goodsInfo['data']['stbz_goods_id'] > 0) {
  411. if (empty($value['goods']['attrs'])) {
  412. throw new \Exception("规格信息错误");
  413. }
  414. $attrs = json_decode($value['goods']['attrs'], true);
  415. $spu = [];
  416. foreach ($value['cart'] as $cart) {
  417. $num = $cart['num'];
  418. if (empty($cart['attr_list'])) {
  419. throw new \Exception("规格信息错误");
  420. }
  421. $attr_id = array_column($cart['attr_list'], 'attr_id');
  422. foreach ($attrs as $attr) {
  423. $attrs_id = array_column($attr['attr_list'], 'attr_id');
  424. if (!array_diff($attrs_id, $attr_id)) {
  425. $no = $attr['no'];
  426. $spu[] = [
  427. 'number' => $num,
  428. 'sku' => $no,
  429. ];
  430. }
  431. }
  432. }
  433. if (empty($address)) {
  434. throw new \Exception("地址信息错误");
  435. }
  436. $params = [
  437. 'spu' => $spu,
  438. 'address' => [
  439. 'consignee' => $address->name,
  440. 'phone' => $address->mobile,
  441. 'province' => $address->province,
  442. 'city' => $address->city,
  443. 'area' => $address->district,
  444. 'street' => '',
  445. 'description' => $address->detail,
  446. ]
  447. ];
  448. debug_log('胜天半子商品信息' . json_encode($params));
  449. $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params);
  450. $checkData = \json_decode($checkData, true);
  451. debug_log('胜天半子下单检测' . json_encode($checkData));
  452. if ($checkData['code'] != 1) {
  453. throw new \Exception($checkData['msg']);
  454. }
  455. }
  456. }
  457. }
  458. return [
  459. 'code' => 0,
  460. 'msg' => '下单成功'
  461. ];
  462. } catch (\Exception $e) {
  463. return [
  464. 'code' => 1,
  465. 'msg' => $e->getMessage()
  466. ];
  467. }
  468. }
  469. }