OrderSubmitForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scanCodePay\models\form;
  8. use app\models\Cart;
  9. use app\models\Goods;
  10. use app\models\UserCoupon;
  11. use app\plugins\scanCodePay\models\Order;
  12. use app\plugins\scanCodePay\models\OrderDetail;
  13. // use app\utils\CloudPrint;
  14. use app\utils\Notice\NoticeSend;
  15. use app\utils\OrderNo;
  16. use yii\helpers\Json;
  17. class OrderSubmitForm extends OrderForm
  18. {
  19. public $user;
  20. public $payment;
  21. public $use_integral;
  22. public function rules()
  23. {
  24. $rules = [
  25. [['payment', 'use_integral'], 'integer']
  26. ];
  27. return array_merge(parent::rules(), $rules);
  28. }
  29. public function save()
  30. {
  31. if (!$this->validate())
  32. return [
  33. 'code' => 1,
  34. 'msg' => $this->getErrorSummary(false)[0],
  35. ];
  36. try {
  37. $mchListData = $this->getMchListData(true);
  38. } catch (\Exception $e) {
  39. return [
  40. 'code' => 1,
  41. 'msg' => $e->getMessage(),
  42. 'file' => $e->getFile(),
  43. 'line' => $e->getLine()
  44. ];
  45. }
  46. $order_id_list = [];
  47. $t = \Yii::$app->db->beginTransaction();
  48. foreach ($mchListData as &$mch) {
  49. if (isset($mch['use_integral']) && !$mch['use_integral']) {
  50. $mch['integral'] = ['forehead' => 0, 'forehead_integral' => 0];
  51. $mch['give'] = 0;
  52. }
  53. $mch_goods = $mch['goods_list'];
  54. if ($mch_goods) {
  55. $checkMchData = $this->checkMchData($mch);
  56. if ($checkMchData['code'] == 1) {
  57. return $checkMchData;
  58. }
  59. $payPrice = $this->getPayPrice($mch);
  60. $order = new Order();
  61. $order->store_id = $this->store_id;
  62. $order->user_id = $this->user_id;
  63. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY);
  64. $order->pay_price = $payPrice;
  65. if (isset($mch['picker_coupon']) && !empty($mch['picker_coupon'])) {
  66. $order->user_coupon_id = $mch['picker_coupon']['user_coupon_id'];
  67. $order->coupon_sub_price = $mch['picker_coupon']['sub_price'];
  68. // 查找优惠券优惠的商品
  69. $this->pickerGoods($mch);
  70. }
  71. // 是否是平台小程序订单
  72. if (is_platform()) {
  73. $order->is_platform = 1;
  74. }
  75. $order->created_at = time();
  76. $order->first_price = 0;
  77. $order->second_price = 0;
  78. $order->third_price = 0;
  79. $order->content = $mch['content'];
  80. $order->is_offline = 1;
  81. if (isset($mch['integral'])) {
  82. $order->integral = json_encode($mch['integral'], JSON_UNESCAPED_UNICODE);
  83. }
  84. $order->version = cyy_version();
  85. $order->mch_id = $mch['mch_id'];
  86. $order->discount = 10; // 当面付订单不享受会员折扣
  87. $order->remark = isset($mch['remark']) ?: '' ;
  88. if ($this->payment == 2) {
  89. $order->pay_type = 2;
  90. $order->is_pay = 0;
  91. }
  92. if ($this->payment == 3) {
  93. $order->pay_type = 3;
  94. $order->is_pay = 0;
  95. }
  96. $order->total_price = $mch['total_price'];
  97. $order->express_price = 0;
  98. if ($order->save()) {
  99. // 处理订单生成之后其他相关数据
  100. $orderRes = $this->insertData($mch, $order);
  101. if ($orderRes['code'] == 1) {
  102. $t->rollBack();
  103. return $orderRes;
  104. }
  105. // 订单提交完成发送消息
  106. NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name'], 0, $order->store_id);
  107. $order_id_list[] = $order->id;
  108. } else {
  109. $t->rollBack();
  110. return $this->getErrorResponse($order);
  111. }
  112. }
  113. }
  114. if (count($order_id_list) > 0) {
  115. $t->commit();
  116. if (count($order_id_list) > 1) {//多个订单合并
  117. return [
  118. 'code' => 0,
  119. 'msg' => '订单提交成功',
  120. 'data' => (object)[
  121. 'order_id_list' => $order_id_list,
  122. 'add_time' => $order->created_at
  123. ],
  124. ];
  125. } else {//单个订单
  126. $order_id = $order_id_list[0];
  127. // CloudPrint::doPrint($order_id, 0, $this->store_id);
  128. return [
  129. 'code' => 0,
  130. 'msg' => '订单提交成功',
  131. 'data' => (object)[
  132. 'order_id' => $order_id_list[0],
  133. 'add_time' => $order->created_at
  134. ],
  135. ];
  136. }
  137. } else {
  138. $t->rollBack();
  139. return [
  140. 'order_list' => $order_id_list
  141. ];
  142. }
  143. }
  144. // 获得实际支付金额
  145. private function getPayPrice($mch)
  146. {
  147. $payPrice = $mch['total_price'];
  148. if (isset($mch['use_integral']) && $mch['use_integral']) {
  149. $payPrice -= $mch['integral']['forehead'];
  150. }
  151. if (isset($mch['picker_coupon'])) {
  152. $payPrice -= $mch['picker_coupon']['sub_price'];
  153. }
  154. return $payPrice > 0 ? $payPrice : 0.01;
  155. }
  156. // 检查数据
  157. private function checkMchData($mch)
  158. {
  159. if (empty($mch['goods_list'])) {
  160. return [
  161. 'code' => 1,
  162. 'msg' => '商品不存在或已删除'
  163. ];
  164. }
  165. $checkCoupon = $this->checkCoupon($mch);
  166. if ($checkCoupon['code'] == 1) {
  167. return $checkCoupon;
  168. }
  169. return ['code' => 0, 'msg' => 'success'];
  170. }
  171. // 检测优惠券是否可使用
  172. private function checkCoupon($mch)
  173. {
  174. if (empty($mch['picker_coupon'])) {
  175. return [
  176. 'code' => 0,
  177. 'msg' => ''
  178. ];
  179. }
  180. $ok = false;
  181. foreach ($mch['coupon_list'] as $item) {
  182. if ($item['user_coupon_id'] == $mch['picker_coupon']['user_coupon_id']) {
  183. $ok = true;
  184. }
  185. }
  186. if (!$ok) {
  187. return [
  188. 'code' => 1,
  189. 'msg' => '该优惠券已过期'
  190. ];
  191. } else {
  192. return [
  193. 'code' => 0,
  194. 'msg' => ''
  195. ];
  196. }
  197. }
  198. // 优惠券可优惠的商品总额计算
  199. private function pickerGoods(&$mch)
  200. {
  201. $pickerCoupon = $mch['picker_coupon'];
  202. if (empty($pickerCoupon)) {
  203. return;
  204. }
  205. $mch['picker_coupon']['total_price'] = $mch['total_price'];
  206. }
  207. private function insertData($mch, $order)
  208. {
  209. // 存入下单表单
  210. if (isset($mch['form']) && $mch['form'] && $mch['form']['is_form'] == 1) {
  211. foreach ($mch['form']['list'] as $index => $value) {
  212. $order_form = new \app\models\OrderForm();
  213. $order_form->store_id = $this->store_id;
  214. $order_form->order_id = $order->id;
  215. $order_form->key = $value['name'];
  216. $order_form->value = $value['default'];
  217. $order_form->type = $value['type'];
  218. $order_form->is_delete = 0;
  219. $order_form->save();
  220. }
  221. }
  222. // 减去当前用户账户积分
  223. if (isset($mch['integral']) && $mch['integral'] && $mch['integral']['forehead_integral'] > 0) {
  224. $this->user->integral -= $mch['integral']['forehead_integral'];
  225. if ($this->user->integral < 0) {
  226. return [
  227. 'code' => 1,
  228. 'msg' => '积分不足'
  229. ];
  230. }
  231. $this->user->save();
  232. }
  233. // 计算商品相关
  234. $payPrice = $mch['total_price'];
  235. $goods_list = $mch['goods_list'];
  236. foreach ($goods_list as $goods) {
  237. // 删除购物车
  238. if (isset($goods['cart_id'])) {
  239. Cart::updateAll(['is_delete' => 1], ['id' => $goods['cart_id']]);
  240. }
  241. $order_detail = new OrderDetail();
  242. $order_detail->order_id = $order->id;
  243. $order_detail->goods_id = $goods['goods_id'];
  244. $order_detail->num = $goods['num'];
  245. // if ($goods['is_level'] && $goods['is_level'] == 1) {
  246. // $order_detail->is_level = (int)$goods['is_level'];
  247. // } else {
  248. $order_detail->is_level = 1;
  249. // }
  250. if (isset($mch['picker_coupon']) && $mch['picker_coupon'] && !empty($mch['picker_coupon'])) {
  251. if ($mch['picker_coupon']['total_price'] > 0) {
  252. $pickerCouponSubPrice = doubleval($mch['picker_coupon']['sub_price'] * doubleval($goods['price']) / $mch['picker_coupon']['total_price']);
  253. $payPrice -= $pickerCouponSubPrice;
  254. }
  255. // 删除优惠券
  256. UserCoupon::updateAll(['is_use' => 1], ['id' => $mch['picker_coupon']['user_coupon_id']]);
  257. }
  258. if (isset($mch['use_integral']) && $mch['use_integral']) {
  259. if ($goods['resIntegral'] && $goods['resIntegral']['forehead'] > 0) {
  260. $payPrice -= $goods['resIntegral']['forehead'];
  261. }
  262. }
  263. $payPrice = $payPrice >= 0 ? sprintf('%.2f', $payPrice) : 0;
  264. // $order_detail->supplier_price = $goods['supplier_price'];
  265. $order_detail->total_price = $payPrice;
  266. $order_detail->is_delete = 0;
  267. $order_detail->attr = json_encode($goods['attr_list'], JSON_UNESCAPED_UNICODE);
  268. $goods_info = Goods::findOne($goods['goods_id']);
  269. $order_detail->pic = $goods['goods_pic'] ? $goods['goods_pic'] : $goods_info->cover_pic;
  270. $order_detail->goods_name = $goods_info ? $goods_info->name : $goods['goods_name'];
  271. $order_detail->goods_info = $goods_info ? Json::encode($goods_info->toArray()) : json::encode([]);
  272. // $order_detail->delivery_type = $goods['send'] == 'express' ? OrderDetail::GOODS_DELIVERY_EXPRESS : OrderDetail::GOODS_DELIVERY_SHOP;
  273. // if ($goods['send'] == 'shop') {
  274. // $order_detail->shop_id = $goods['shop_id'];
  275. // }
  276. if (isset($goods['give']) && $goods['give'] > 0) {
  277. $order_detail->integral = $goods['give'];
  278. } else {
  279. $order_detail->integral = 0;
  280. }
  281. $attr_id_list = [];
  282. foreach ($goods['attr_list'] as $item) {
  283. array_push($attr_id_list, $item['attr_id']);
  284. }
  285. // $order_detail->batch_price_tips = $goods['current_batch_price_tips'];
  286. if (!$order_detail->save()) {
  287. return [
  288. 'code' => 1,
  289. 'msg' => '订单提交失败,请稍后再重试',
  290. 'error' => $order_detail->errors[0]
  291. ];
  292. }
  293. }
  294. return [
  295. 'code' => 0,
  296. 'msg' => ''
  297. ];
  298. }
  299. }