MchOrderSubmitForm.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\order;
  8. use app\models\AccountLog;
  9. use app\models\Address;
  10. use app\models\Cart;
  11. use app\models\CashierActionLog;
  12. use app\models\common\CommonOrder;
  13. use app\models\FoodCart;
  14. use app\models\FoodFlag;
  15. use app\models\Goods;
  16. use app\models\HangingOrder;
  17. use app\models\HangingOrderDetail;
  18. use app\models\Md;
  19. use app\models\MdProfit;
  20. use app\models\Option;
  21. use app\models\Order;
  22. use app\models\OrderDetail;
  23. use app\models\Store;
  24. use app\models\User;
  25. use app\models\UserCoupon;
  26. use app\models\VerifyCardSale;
  27. use app\models\VideoGoodsList;
  28. use app\models\VideoGoodsSetting;
  29. use app\models\VideoGoodsShare;
  30. use app\modules\client\models\OrderComplete;
  31. use app\modules\client\models\v1\order\OrderForm;
  32. use app\modules\common\models\NotifyForm;
  33. use app\plugins\adopt\models\AdoptGoods;
  34. use app\plugins\adopt\models\AdoptOrderInfo;
  35. use app\utils\CloudPrint;
  36. use app\utils\Delivery\Delivery;
  37. use app\utils\Notice\NoticeSend;
  38. use app\utils\OrderNo;
  39. use app\utils\PrintOrder;
  40. use app\utils\Tools;
  41. use yii\helpers\Json;
  42. class MchOrderSubmitForm extends MchOrderForm
  43. {
  44. public $user;
  45. public $payment;
  46. public $use_integral;
  47. public $actual_price;// 管理员实收金额
  48. public $sale_time;// 销售日期
  49. public $ready_money = ['money'=>0,'give_change'=>0];//现金收入相关
  50. public $remark = '';
  51. public $hanging_order_id = 0;
  52. public $address_id;
  53. public $is_combine = 0;//是否使用余额
  54. public $md_id;
  55. public $is_offline = 0;
  56. public function rules()
  57. {
  58. $rules = [
  59. [['payment', 'use_integral','hanging_order_id','is_combine', 'address_id', 'md_id', 'is_offline'], 'integer'],
  60. [['actual_price'],'number'],
  61. [['sale_time','ready_money','remark'],'string']
  62. ];
  63. return array_merge(parent::rules(), $rules);
  64. }
  65. public function save()
  66. {
  67. if (!$this->validate())
  68. return [
  69. 'code' => 1,
  70. 'msg' => $this->getErrorSummary(false)[0],
  71. ];
  72. try {
  73. $mchListData = $this->getMchListData(true);
  74. } catch (\Exception $e) {
  75. return [
  76. 'code' => 1,
  77. 'msg' => '下单失败'
  78. ];
  79. }
  80. $order_id_list = [];
  81. $level = $this->level;
  82. $md_select = 0;
  83. /**
  84. * @var $address Address
  85. */
  86. //$address = (object)($this->address);
  87. $t = \Yii::$app->db->beginTransaction();
  88. foreach ($mchListData as &$mch) {
  89. if (isset($mch['use_integral']) && !$mch['use_integral']) {
  90. $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0];
  91. $mch['give'] = 0;
  92. }
  93. $mch_goods = $mch['goods_list'];
  94. if ($mch_goods) {
  95. $checkMchData = $this->checkMchData($mch);
  96. if ($checkMchData['code'] == 1) {
  97. $t->rollBack();
  98. return $checkMchData;
  99. }
  100. $level_price = 0;
  101. $total_price = 0;
  102. $is_have_express = false;
  103. $is_have_shop = $mch['is_shop'] == 1 ? 1 : 0;
  104. $order_type = 0;
  105. $is_have_delivery = 0;
  106. $total_integral = 0;
  107. foreach ($mch_goods as $v) {
  108. $order_type = 0;
  109. $level_price += $v['level_price'];
  110. $total_price += isset($v['total_price']) ? $v['total_price'] : $v['price'];
  111. }
  112. $mch['level_price'] = $level_price;
  113. $mch['total_price'] = $total_price;
  114. $mch['total_integral'] = $total_integral; //积分兑换
  115. // 开启使用积分抵扣
  116. if (isset($mch['use_integral']) && $mch['use_integral'] == 1) {
  117. if ($this->user->integral < $mch['integral']['forehead']) {
  118. return [
  119. 'code' => 1,
  120. 'msg' => '积分可抵扣支付数量不足'
  121. ];
  122. }
  123. }
  124. $payPrice = $this->getPayPrice($mch);
  125. $order = new Order();
  126. // 订单来源
  127. $order->order_origin = Order::ORDER_SOURCE_CASHIER;// 收银台订单
  128. $order->store_id = $this->store_id;
  129. $order->user_id = $this->user_id ?? 0;
  130. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  131. // 实际支付金额 按前台传过来的值
  132. $order->pay_price = $this->actual_price;
  133. $order->hanging_order_id = $this->hanging_order_id;
  134. if (isset($mch['picker_coupon']) && !empty($mch['picker_coupon'])) {
  135. $order->user_coupon_id = $mch['picker_coupon']['user_coupon_id'];
  136. $order->coupon_sub_price = $mch['picker_coupon']['sub_price'];
  137. // 查找优惠券优惠的商品
  138. $this->pickerGoods($mch);
  139. }
  140. // 订单类型
  141. $order->order_type = $order_type;
  142. // todo 积分兑换商品
  143. /*if (isset($mch['total_integral']) && $mch['total_integral'] > 0 ) {
  144. if (get_user()->integral < $mch['total_integral']) {
  145. //获取商城积分抵扣规则 x积分 = 1元
  146. $integral_rule = Option::find()->where(['store_id'=>get_store_id(),'name'=>'integral'])->select('value')->asArray()->one();
  147. $difference_price = sprintf("%.2f", ($mch['total_integral'] - get_user()->integral ) / $integral_rule['value'] );
  148. $order->pay_price += $difference_price;
  149. $order->total_price += $difference_price;
  150. $order->integral_price = get_user()->integral;
  151. $order->integral_difference_price = $difference_price;
  152. $mch['integral_type'] = 2; //积分不足
  153. $mch['integral_difference_price'] = $difference_price; //补的差额
  154. }else{
  155. $order->integral_price = $mch['total_integral'];
  156. $order->integral_difference_price = 0;
  157. $mch['integral_type'] = 1; //全部使用积分
  158. $mch['integral_difference_price'] = 0; //补的差额
  159. }
  160. //当用户有积分时
  161. if(get_user()->integral > 0){
  162. $upIntegral = get_user()->integral - $order->integral_price;
  163. //减用户积分
  164. if(! User::updateAll(['integral'=>$upIntegral],['id'=> get_user_id()]) ){
  165. $t->rollBack();
  166. return [
  167. 'code' => 1,
  168. 'msg' => '积分抵扣失败,稍后重试!'
  169. ];
  170. }
  171. }
  172. }*/
  173. // 是否是平台小程序订单
  174. $order->is_platform = 0;
  175. $order->md_id = $this->md_id;
  176. $order->created_at = time();
  177. $order->first_price = 0;
  178. $order->second_price = 0;
  179. $order->third_price = 0;
  180. $order->content = isset($mch['content']) ? $mch['content'] : '';
  181. $order->is_offline = $this->is_offline == 1 ? 1 : 0;
  182. $order->is_delivery = $is_have_delivery ? 1 : 0;
  183. if (isset($mch['use_integral']) && $mch['use_integral'] == 1) {
  184. $order->integral = json_encode($mch['integral'], JSON_UNESCAPED_UNICODE);
  185. } else {
  186. $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0], JSON_UNESCAPED_UNICODE);
  187. }
  188. $order->version = cyy_version();
  189. $order->mch_id = $mch['mch_id'];
  190. $order->discount = isset($level['discount']) ? $level['discount'] : 10;
  191. $order->remark = $this->remark ?? '这是一个收银台过来的订单';
  192. // 微信支付
  193. if ($this->payment == 1) {
  194. $order->pay_type = 1;
  195. $order->is_pay = 0;
  196. }
  197. // 支付宝支付
  198. if ($this->payment == 4) {
  199. $order->pay_type = 4;
  200. $order->is_pay = 0;
  201. }
  202. // 线下支付
  203. if ($this->payment == 6) {
  204. $order->pay_type = 6;
  205. $order->is_pay = 0;
  206. }
  207. //货到付款
  208. if ($this->payment == 2) {
  209. $order->pay_type = 2;
  210. $order->is_pay = 0;
  211. if (!$order->user_id) {
  212. return [
  213. 'code' => 1,
  214. 'msg' => '下单失败,货到付款需选择用户'
  215. ];
  216. }
  217. }
  218. if ($this->address_id) {
  219. $address = Address::findOne([
  220. 'id' => $this->address_id
  221. ]);
  222. } else {
  223. $address = Address::findOne([
  224. 'is_default' => 1,
  225. 'user_id' => $this->saas_user->id,
  226. 'is_delete' => 0,
  227. ]);
  228. }
  229. if (!empty($address)) {
  230. $order->address = $address->province . $address->city . $address->district . $address->detail;
  231. $order->mobile = $address->mobile;
  232. $order->name = $address->name;
  233. $order->province_id = $address->province_id;
  234. $order->city_id = $address->city_id;
  235. $order->district_id = $address->district_id;
  236. $address_arr = json_encode([
  237. 'province' => trim($address->province),
  238. 'city' => trim($address->city),
  239. 'district' => trim($address->district),
  240. 'detail' => trim($address->detail),
  241. 'latitude' => $address->latitude,
  242. 'longitude' => $address->longitude,
  243. ], JSON_UNESCAPED_UNICODE);
  244. $order->address_data = $address_arr;
  245. }
  246. // 余额抵扣支付
  247. if ($this->is_combine) {
  248. if ($this->user->money >= $order->pay_price) {
  249. $order->pay_type = 3;
  250. $order->is_pay = 0;
  251. }
  252. }
  253. $order->total_price = $mch['total_price'];
  254. //获取代理配置信息
  255. $store = Store::findOne($this->store_id);
  256. $admin_profit = Option::get('admin_profit', 0, 'saas', 0)['value'];
  257. if (!empty($admin_profit) && !empty($store->transfer_profit)) {
  258. $order->ag_rebate = ($payPrice * $store->transfer_profit / 100) * ($admin_profit / 100);
  259. }
  260. // 配送订单预存数据
  261. $order->saas_id = 0;
  262. $order->integral_difference_price = 0;
  263. $order->sale_time = strtotime($this->sale_time);
  264. if(is_array($this->ready_money)){
  265. $order->ready_money = json_encode($this->ready_money);
  266. }else{
  267. $order->ready_money = $this->ready_money;
  268. }
  269. if (!$order->save()) {
  270. $t->rollBack();
  271. return [
  272. 'code' => 1,
  273. 'msg' => implode(';', array_values($order->firstErrors))
  274. ];
  275. } else {
  276. // 绑定上下级
  277. $user = User::findOne($this->user_id);
  278. if ($user && $user->parent_user_id) {
  279. CommonOrder::saveParentId($user->parent_user_id);
  280. }
  281. // 处理订单生成之后其他相关数据
  282. $orderRes = $this->insertData($mch, $order);
  283. $this->reduceIntegral($order);
  284. if ($orderRes['code'] == 1) {
  285. $t->rollBack();
  286. return $orderRes;
  287. }
  288. if($order->hanging_order_id > 0){
  289. HangingOrder::updateAll(['is_delete'=>1],['id'=>$order->hanging_order_id]);
  290. HangingOrderDetail::updateAll(['is_delete'=>1],['order_id'=>$order->hanging_order_id]);
  291. }
  292. $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0, 0, $order['mch_id']);
  293. $printer_order->print_order();
  294. /*// 订单提交完成发送消息
  295. NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name']);*/
  296. $order_id_list[] = $order->id;
  297. }
  298. }
  299. }
  300. if (count($order_id_list) > 0) {
  301. $t->commit();
  302. // 生成操作记录
  303. CashierActionLog::setLog($this->store_id,get_user_id(),CashierActionLog::CREATE_ORDER,'提交订单', $this->md_id);
  304. if (count($order_id_list) > 1) {//多个订单合并
  305. return [
  306. 'code' => 0,
  307. 'msg' => '订单提交成功',
  308. 'data' => (object)[
  309. 'order_id_list' => $order_id_list,
  310. 'add_time' => $order->created_at
  311. ],
  312. ];
  313. } else {//单个订单
  314. $order_id = $order_id_list[0];
  315. CloudPrint::doPrint($order_id, 0, $this->store_id);
  316. return [
  317. 'code' => 0,
  318. 'msg' => '订单提交成功',
  319. 'data' => (object)[
  320. 'order_id' => $order_id_list[0],
  321. 'add_time' => $order->created_at,
  322. 'order_price'=> $order->pay_price,
  323. 'pay_type' => $order->pay_type
  324. ],
  325. ];
  326. }
  327. } else {
  328. $t->rollBack();
  329. return [
  330. 'order_list' => $order_id_list,
  331. 'order_price'=> $order->pay_price
  332. ];
  333. }
  334. }
  335. // 使用积分抵扣,订单生成后扣除用户积分
  336. private function reduceIntegral($order) {
  337. $integral = Json::decode($order->integral);
  338. // 减去当前用户账户积分
  339. if ($integral['forehead_integral'] > 0) {
  340. $user = User::findOne($order->user_id);
  341. $before = $user->integral;
  342. $user->integral -= $integral['forehead_integral'];
  343. if ($user->save()) {
  344. $log = new AccountLog();
  345. $log->store_id = $order->store_id;
  346. $log->user_id = $user->id;
  347. $log->type = AccountLog::TYPE_INTEGRAL;
  348. $log->log_type = AccountLog::LOG_TYPE_EXPEND;
  349. $log->amount = $integral['forehead_integral'];
  350. $log->desc = "订单积分抵扣支付,订单号为:{$order->order_no}。";
  351. $log->before = $before;
  352. $log->after = $user->integral;
  353. $log->operator = '';
  354. $log->operator_id = 0;
  355. $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  356. $log->created_at = time();
  357. $log->order_id = $order->id;
  358. $log->order_type = AccountLog::TYPE_PLATFORM_ORDER;
  359. $log->save();
  360. }
  361. }
  362. }
  363. // 检查数据
  364. private function checkMchData($mch)
  365. {
  366. if (empty($mch['goods_list'])) {
  367. return [
  368. 'code' => 1,
  369. 'msg' => '商品不存在或已删除'
  370. ];
  371. }
  372. $checkCoupon = $this->checkCoupon($mch);
  373. if ($checkCoupon['code'] == 1) {
  374. return $checkCoupon;
  375. }
  376. $checkGoods = $this->checkGoods($mch);
  377. if ($checkGoods['code'] == 1) {
  378. return $checkGoods;
  379. }
  380. return ['code' => 0, 'msg' => 'success'];
  381. }
  382. // 优惠券可优惠的商品总额计算
  383. private function pickerGoods(&$mch)
  384. {
  385. $totalPrice = 0;
  386. $pickerCoupon = $mch['picker_coupon'];
  387. if (empty($pickerCoupon)) {
  388. return;
  389. }
  390. foreach ($mch['goods_list'] as $item) {
  391. if ($pickerCoupon['appoint_type'] == 1) {
  392. if ($pickerCoupon['cat_id_list'] !== null) {
  393. $catIdList = $pickerCoupon['cat_id_list'];
  394. if (array_intersect($item['cat_id'], $catIdList)) {
  395. $totalPrice += $item['price'];
  396. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  397. }
  398. } else {
  399. $totalPrice += $item['price'];
  400. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  401. }
  402. } else if ($pickerCoupon['appoint_type'] == 2) {
  403. if ($pickerCoupon['goods_id_list'] !== null) {
  404. $goodsIdList = $pickerCoupon['goods_id_list'];
  405. if (in_array($item['goods_id'], $goodsIdList)) {
  406. $totalPrice += $item['price'];
  407. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  408. }
  409. } else {
  410. $totalPrice += $item['price'];
  411. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  412. }
  413. } else {
  414. $totalPrice += $item['price'];
  415. $mch['picker_coupon']['goods_id'][] = $item['goods_id'];
  416. }
  417. }
  418. $mch['picker_coupon']['total_price'] = $totalPrice;
  419. }
  420. // 检测优惠券是否可使用
  421. private function checkCoupon($mch)
  422. {
  423. if (empty($mch['picker_coupon'])) {
  424. return [
  425. 'code' => 0,
  426. 'msg' => ''
  427. ];
  428. }
  429. $ok = false;
  430. foreach ($mch['coupon_list'] as $item) {
  431. if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) {
  432. $ok = true;
  433. }
  434. }
  435. if (!$ok) {
  436. return [
  437. 'code' => 1,
  438. 'msg' => '该优惠券已过期'
  439. ];
  440. } else {
  441. return [
  442. 'code' => 0,
  443. 'msg' => ''
  444. ];
  445. }
  446. }
  447. // 检测商品相关
  448. private function checkGoods($mch)
  449. {
  450. \Yii::warning($mch);
  451. foreach ($mch['goods_list'] as $goods) {
  452. $attr_id_list = [];
  453. foreach ($goods['attr_list'] as $item) {
  454. array_push($attr_id_list, $item['attr_id']);
  455. }
  456. $_goods = Goods::find()->where('id = ' . $goods['goods_id'] . ' for update')->one();
  457. //2砍价商品6当面付
  458. if ($_goods->type != 2 && $_goods->type != 6 && $_goods->product_type == Goods::GOODS_TYPE_NORMAL || $_goods->product_type == Goods::GOODS_TYPE_ADOPT) {
  459. if (!$_goods->numSub($attr_id_list, $goods['num'], $this->md_id)) {
  460. return [
  461. 'code' => 1,
  462. 'msg' => '订单提交失败,商品“' . $_goods->name . '”库存不足',
  463. 'attr_id_list' => $attr_id_list,
  464. 'attr_list' => $goods['attr_list'],
  465. ];
  466. }
  467. }
  468. if($_goods->product_type == Goods::GOODS_TYPE_DATE){
  469. $num = Goods::getGoodsNum($_goods)['data'];
  470. $attr = $goods['book'];
  471. foreach ($attr as $iattr) {
  472. $idate = $iattr['date'];
  473. foreach ($num as $inum) {
  474. $indate = $inum['date'];
  475. if(($idate == $indate) && ($inum['num'] < $goods['num'])){
  476. return ['code' => 1, 'msg' => $idate.'库存不足'];
  477. }
  478. }
  479. }
  480. }
  481. }
  482. return ['code' => 0, 'msg' => ''];
  483. }
  484. // 获得实际支付金额
  485. private function getPayPrice($mch)
  486. {
  487. $goods_list = $mch['goods_list'];
  488. $payPrice = $mch['level_price'];
  489. if (isset($mch['use_integral']) && $mch['use_integral']) {
  490. $payPrice -= $mch['integral']['forehead'];
  491. }
  492. if (isset($mch['picker_coupon'])) {
  493. $pickerCoupon = $mch['picker_coupon'];
  494. if ($pickerCoupon['sub_price'] > 0) {
  495. $coupon_price = 0;
  496. if ($pickerCoupon['appoint_type'] == 1 && $pickerCoupon['cat_id_list'] != null) {
  497. foreach ($goods_list as $goods) {
  498. foreach ($goods['cat_id'] as $v1) {
  499. if (in_array($v1, $pickerCoupon['cat_id_list'])) {
  500. $coupon_price += floatval($goods['level_price']);
  501. break;
  502. };
  503. };
  504. }
  505. } else if ($pickerCoupon['appoint_type'] == 2 && $pickerCoupon['goods_id_list'] != null) {
  506. foreach ($goods_list as $goods) {
  507. if (in_array($goods['goods_id'], $pickerCoupon['goods_id_list'])) {
  508. $coupon_price += floatval($goods['level_price']);
  509. };
  510. }
  511. };
  512. if ($pickerCoupon['sub_price'] > $coupon_price && $coupon_price > 0) {
  513. $payPrice -= $coupon_price;
  514. } else {
  515. $payPrice -= $pickerCoupon['sub_price'];
  516. }
  517. }
  518. }
  519. // Todo 此处有待确认算法是否正确 by Syan
  520. // $payPrice = $payPrice > 0 ? $payPrice : 0.01;
  521. /*if ($mch['express_price'] > 0) {
  522. if (isset($mch['send']) && $mch['send'] == 'express') {
  523. $payPrice += $mch['express_price'];
  524. } else {
  525. foreach ($goods_list as $goods) {
  526. if (isset($goods['send']) && $goods['send'] == 'express') {
  527. $payPrice += $mch['express_price'];
  528. break;
  529. }
  530. }
  531. }
  532. }*/
  533. return $payPrice > 0 ? $payPrice : 0;
  534. }
  535. /**
  536. * @param $mch
  537. * @param $order Order
  538. * @return array
  539. */
  540. private function insertData($mch, $order)
  541. {
  542. // 计算商品相关
  543. $goods_list = $mch['goods_list'];
  544. $goodsPrice = 0;
  545. // 计算商品分润
  546. $goods_profit = 0;
  547. $chain_goods_profit = 0;
  548. foreach ($goods_list as $goods) {
  549. if ($goods['product_type'] == Goods::GOODS_TYPE_ADOPT) {
  550. $adopt_order_info = new AdoptOrderInfo();
  551. $adopt_order_info->name = $this->adopt_name;
  552. $adopt_order_info->mobile = $this->adopt_mobile;
  553. $adopt_order_info->order_id = $order->id;
  554. $adopt_order_info->store_id = $this->store_id;
  555. $adopt_goods = AdoptGoods::find()->where(['goods_id' => $goods['id']])->one();
  556. $adopt_order_info->mature_time = time() + $adopt_goods->growth_cycle * 86400;
  557. if (!$adopt_order_info->save()) {
  558. return [
  559. 'code' => 1,
  560. 'msg' => '订单提交失败,请稍后再重试',
  561. 'error' => $adopt_order_info->errors
  562. ];
  563. }
  564. }
  565. // 存入下单表单
  566. if ($goods['form'] && $goods['form']['is_form'] == 1) {
  567. foreach ($goods['form']['list'] as $index => $value) {
  568. $order_form = new \app\models\OrderForm();
  569. $order_form->store_id = $this->store_id;
  570. $order_form->order_id = $order->id;
  571. $order_form->goods_id = $goods['id'];
  572. $order_form->key = $value['name'];
  573. $order_form->value = $value['default'];
  574. $order_form->type = $value['type'];
  575. $order_form->is_delete = 0;
  576. $order_form->save();
  577. }
  578. }
  579. // 删除购物车
  580. if (isset($goods['cart_id'])) {
  581. Cart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]);
  582. }
  583. $order_detail = new OrderDetail();
  584. $order_detail->order_id = $order->id;
  585. $order_detail->goods_id = $goods['goods_id'];
  586. $order_detail->num = $goods['num'];
  587. // if ($goods['is_level'] && $goods['is_level'] == 1) {
  588. // $order_detail->is_level = (int)$goods['is_level'];
  589. // } else {
  590. $order_detail->is_level = 1;
  591. // }
  592. $payPrice = $goods['level_price'];
  593. if (isset($mch['picker_coupon']) && $mch['picker_coupon'] && !empty($mch['picker_coupon'])) {
  594. if (!empty($mch['picker_coupon']['goods_id_list']) && in_array($goods['goods_id'], \json_decode($mch['picker_coupon']['goods_id_list'], true)) && $mch['picker_coupon']['total_price'] > 0) {
  595. $pickerCouponSubPrice = doubleval($mch['picker_coupon']['sub_price'] * doubleval($goods['price']) / $mch['picker_coupon']['total_price']);
  596. $payPrice -= $pickerCouponSubPrice;
  597. }
  598. // 删除优惠券
  599. UserCoupon::updateAll(['is_use' => 1], ['id' => $mch['picker_coupon']['user_coupon_id']]);
  600. }
  601. if (isset($mch['use_integral']) && $mch['use_integral']) {
  602. if ($goods['resIntegral'] && $goods['resIntegral']['forehead'] > 0) {
  603. $payPrice -= $goods['resIntegral']['forehead'];
  604. }
  605. }
  606. $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0;
  607. $goodsPrice += $payPrice;
  608. $orderPrice = floatval($order->pay_price) - floatval($order->express_price);
  609. if ($goodsPrice > $orderPrice) {
  610. $payPrice = $payPrice - ($goodsPrice - $orderPrice);
  611. $goodsPrice = $orderPrice;
  612. }
  613. //$payPrice = $this->getBookPrice($payPrice, $goods_list);
  614. // $order_detail->supplier_price = $goods['supplier_price'];
  615. $order_detail->total_price = $payPrice;
  616. // 计算单个商品可分红金额
  617. $profit = $goods['rate_type'] == 0 ? floatval($payPrice * $goods['rate'] / 100) : $goods['rate'];
  618. $goods_profit += $profit;
  619. $order_detail->profit = $profit;
  620. // 链动
  621. $chain_profit = $goods['chain_rate_type'] == 0 ? floatval($payPrice * $goods['chain_rate'] / 100) : $goods['chain_rate'];
  622. $chain_goods_profit += $chain_profit;
  623. $order_detail->chain_profit = $chain_profit;
  624. $order_detail->is_delete = 0;
  625. if ($goods['product_type'] == 1) {
  626. $order_detail->attr = json_encode($goods['book'], JSON_UNESCAPED_UNICODE);
  627. } elseif ($goods['product_type'] == 2) {
  628. //$order_detail->attr = json_encode($goods['service'], JSON_UNESCAPED_UNICODE);
  629. $service = $goods['service'];
  630. $time = explode('-', $service['time']);
  631. $order_detail->attr = json_encode([
  632. 'start_date' => $service['date'] . ' ' . $time[0],
  633. 'end_date' => $service['date'] . ' ' . $time[1],
  634. 'time' => $service['time'],
  635. 'date' => $service['date'],
  636. 'price' => $service['price'],
  637. ], JSON_UNESCAPED_UNICODE);
  638. } else {
  639. $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE);
  640. }
  641. $goods_info = Goods::findOne($goods['goods_id']);
  642. $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic;
  643. $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name'];
  644. $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]);
  645. $order_detail->delivery_type = $mch['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : ($mch['send'] == 'delivery' ? OrderDetail::GOODS_DELIVERY_IM : OrderDetail::GOODS_DELIVERY_SHOP);
  646. if ($mch['send'] == 'shop' && empty(input_params('flag_id'))) {
  647. if(isset($goods['shop_id']) && $goods['shop_id'] > 0){
  648. $order_detail->shop_id = $goods['shop_id'];
  649. }else{
  650. $order_detail->shop_id = 0;
  651. }
  652. }
  653. if ($goods['give'] > 0) {
  654. $order_detail->integral = $goods['give'];
  655. } else {
  656. $order_detail->integral = 0;
  657. }
  658. $attr_id_list = [];
  659. foreach ($goods['attr_list'] as $item) {
  660. array_push($attr_id_list, $item['attr_id']);
  661. }
  662. $order_detail->batch_price_tips = $goods['current_batch_price_tips'];
  663. // 存入点餐的user_id
  664. if (!empty($goods['user_id'])) {
  665. $order_detail->user_id = $goods['user_id'];
  666. }
  667. // 积分兑换商品 1次只能兑换1个
  668. if ($order->order_type == 7){
  669. $order_detail->total_price = $order->total_price;
  670. $order_detail->integral_price = $order->integral_price;
  671. $order_detail->integral_difference_price = $order->integral_difference_price;
  672. }
  673. if (!$order_detail->save()) {
  674. return [
  675. 'code' => 1,
  676. 'msg' => '订单提交失败,请稍后再重试',
  677. 'error' => $order_detail->errors[0]
  678. ];
  679. }
  680. if (!empty($goods['videoId'])) {
  681. //获取抖品信息
  682. $video_goods = VideoGoodsList::findOne($goods['videoId']);
  683. //获取抖品配置信息
  684. $setting = VideoGoodsSetting::findOne(['store_id' => get_store_id()]);
  685. //获取商品配置信息
  686. $goods_info = Goods::findOne($goods['goods_id']);
  687. $profit = ($setting->profit / 100);
  688. if ($profit > 0 && $video_goods && $goods_info) {
  689. $video_share = new VideoGoodsShare();
  690. $video_share->order_id = $order->id;
  691. $video_share->goods_id = $goods['goods_id'];
  692. $video_share->num = $goods['num'];
  693. $video_share->order_detail = $order_detail->id;
  694. $video_share->video_goods_id = $goods['videoId'];
  695. $video_share->store_id = get_store_id();
  696. $video_share->author_user_id = $video_goods->user_id;
  697. $video_share->created_at = time();
  698. $video_share->profit = $setting->profit;
  699. $video_share->proportion = ($goods['level_price'] * $profit);
  700. if (!$video_share->save()) {
  701. return [
  702. 'code' => 1,
  703. 'msg' => implode(';', array_values($video_share->firstErrors))
  704. ];
  705. }
  706. }
  707. }
  708. }
  709. // goods_profit 计算之后存入订单表中
  710. $order->profit = $goods_profit;
  711. $order->chain_profit = $chain_goods_profit;
  712. $order->save();
  713. // 计算门店收益
  714. if (get_md_id() > 0 && !in_array($order->order_type, [1, 2])) {
  715. $result = OrderForm::findPrice($order->id);
  716. MdProfit::handleProfit($order->id, $result['original_price'], $result['md_price']);
  717. if (!empty(input_params('flag_id'))) {
  718. FoodCart::updateAll(['is_delete' => 1], ['flag_id' => input_params('flag_id')]);
  719. FoodFlag::updateAll(['status' => 1], ['id' => input_params('flag_id')]);
  720. }
  721. }else{
  722. if ($order->is_offline || in_array($order->order_type, [1, 2])) {
  723. MdProfit::handleProfit($order->id, $order->total_price, $order->total_price, $order->md_id);
  724. }
  725. }
  726. return [
  727. 'code' => 0,
  728. 'msg' => ''
  729. ];
  730. }
  731. }