StoreOrderCreate.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use addon\coupon\model\Coupon;
  12. use app\model\express\Config as ExpressConfig;
  13. use app\model\express\Express;
  14. use app\model\goods\Goods;
  15. use app\model\goods\GoodsStock;
  16. use app\model\member\MemberAccount;
  17. use app\model\order\CashOrder;
  18. use app\model\order\Config;
  19. use app\model\order\OrderCreate;
  20. use app\model\store\Store;
  21. use app\model\system\Pay;
  22. use think\facade\Cache;
  23. /**
  24. * 订单创建(秒杀)
  25. *
  26. * @author Administrator
  27. *
  28. */
  29. class StoreOrderCreate extends OrderCreate
  30. {
  31. private $goods_money = 0;//商品金额
  32. private $balance_money = 0;//余额
  33. private $delivery_money = 0;//配送费用
  34. private $coupon_money = 0;//优惠券金额
  35. private $adjust_money = 0;//调整金额
  36. private $invoice_money = 0;//发票费用
  37. private $promotion_money = 0;//优惠金额
  38. private $order_money = 0;//订单金额
  39. private $pay_money = 0;//支付总价
  40. private $is_virtual = 0; //是否是虚拟类订单
  41. private $order_name = ''; //订单详情
  42. private $goods_num = 0; //商品种数
  43. private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
  44. private $pay_type = 'ONLINE_PAY';//支付方式
  45. private $invoice_delivery_money = 0;
  46. private $error = 0; //是否有错误
  47. private $error_msg = ''; //错误描述
  48. /**
  49. * 订单创建
  50. * @param unknown $data
  51. */
  52. public function create($data)
  53. {
  54. //查询出会员相关信息
  55. $calculate_data = $this->calculate($data);
  56. if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
  57. return $calculate_data;
  58. if ($this->error > 0) {
  59. return $this->error([ 'error_code' => $this->error ], $this->error_msg);
  60. }
  61. if (!empty($calculate_data[ 'invoice_type' ])) {
  62. if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == '') {
  63. //物流,同城
  64. if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == 'express' || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == 'local') {
  65. $calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
  66. $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
  67. }
  68. //门店
  69. if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == 'store') {
  70. $delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
  71. $calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
  72. $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
  73. }
  74. }
  75. }
  76. $pay = new Pay();
  77. $out_trade_no = $pay->createOutTradeNo($data[ 'member_id' ]);
  78. model('order')->startTrans();
  79. //循环生成多个订单
  80. try {
  81. $pay_money = 0;
  82. $goods_stock_model = new GoodsStock();
  83. $shop_goods_list = $calculate_data[ 'shop_goods_list' ];
  84. $item_delivery = $shop_goods_list[ 'delivery' ] ?? [];
  85. $delivery_type = $item_delivery[ 'delivery_type' ] ?? '';
  86. $delivery_type_name = Express::express_type[ $delivery_type ][ 'title' ] ?? '';
  87. //订单主表
  88. $order_type = $this->orderType($shop_goods_list, $calculate_data);
  89. $order_no = $this->createOrderNo($shop_goods_list[ 'site_id' ], $data[ 'member_id' ]);
  90. $cash_order_model = new CashOrder();
  91. $data_order = [
  92. 'order_no' => $order_no,
  93. 'site_id' => $shop_goods_list[ 'site_id' ],
  94. 'order_from' => $data[ 'order_from' ],
  95. 'order_from_name' => $data[ 'order_from_name' ],
  96. 'order_type' => 5,
  97. 'order_type_name' => '收银订单',
  98. 'order_status_name' => '待付款',
  99. 'order_status_action' => json_encode($cash_order_model->order_status[ 0 ], JSON_UNESCAPED_UNICODE),
  100. 'out_trade_no' => $out_trade_no,
  101. 'member_id' => $data[ 'member_id' ],
  102. 'name' => $calculate_data[ 'member_address' ][ 'name' ] ?? '',
  103. 'mobile' => $calculate_data[ 'member_address' ][ 'mobile' ] ?? '',
  104. 'telephone' => $calculate_data[ 'member_address' ][ 'telephone' ] ?? '',
  105. 'province_id' => $calculate_data[ 'member_address' ][ 'province_id' ] ?? '',
  106. 'city_id' => $calculate_data[ 'member_address' ][ 'city_id' ] ?? '',
  107. 'district_id' => $calculate_data[ 'member_address' ][ 'district_id' ] ?? '',
  108. 'community_id' => $calculate_data[ 'member_address' ][ 'community_id' ] ?? '',
  109. 'address' => $calculate_data[ 'member_address' ][ 'address' ] ?? '',
  110. 'full_address' => $calculate_data[ 'member_address' ][ 'full_address' ] ?? '',
  111. 'longitude' => $calculate_data[ 'member_address' ][ 'longitude' ] ?? '',
  112. 'latitude' => $calculate_data[ 'member_address' ][ 'latitude' ] ?? '',
  113. 'buyer_ip' => request()->ip(),
  114. 'goods_money' => $shop_goods_list[ 'goods_money' ],
  115. 'delivery_money' => $shop_goods_list[ 'delivery_money' ],
  116. 'coupon_id' => isset($shop_goods_list[ 'coupon_id' ]) ? $shop_goods_list[ 'coupon_id' ] : 0,
  117. 'coupon_money' => $shop_goods_list[ 'coupon_money' ] ?? 0,
  118. 'adjust_money' => $shop_goods_list[ 'adjust_money' ],
  119. 'invoice_money' => $shop_goods_list[ 'invoice_money' ],
  120. 'promotion_money' => $shop_goods_list[ 'promotion_money' ],
  121. 'order_money' => $shop_goods_list[ 'order_money' ],
  122. 'balance_money' => $shop_goods_list[ 'balance_money' ],
  123. 'pay_money' => $shop_goods_list[ 'pay_money' ],
  124. 'create_time' => time(),
  125. 'is_enable_refund' => 0,
  126. 'order_name' => $shop_goods_list[ 'order_name' ],
  127. 'goods_num' => $shop_goods_list[ 'goods_num' ],
  128. 'delivery_type' => $delivery_type,
  129. 'delivery_type_name' => $delivery_type_name,
  130. 'delivery_store_id' => $calculate_data[ 'delivery_store_id' ] ?? 0,
  131. 'delivery_store_name' => $calculate_data[ 'delivery_store_name' ] ?? '',
  132. 'delivery_store_info' => $calculate_data[ 'delivery_store_info' ] ?? '',
  133. 'buyer_message' => $data[ 'buyer_message' ],
  134. 'promotion_type' => 'cash',
  135. 'promotion_type_name' => '门店收银',
  136. 'promotion_status_name' => '',
  137. 'invoice_delivery_money' => $shop_goods_list[ 'invoice_delivery_money' ] ?? 0,
  138. 'taxpayer_number' => $shop_goods_list[ 'taxpayer_number' ] ?? '',
  139. 'invoice_rate' => $shop_goods_list[ 'invoice_rate' ] ?? 0,
  140. 'invoice_content' => $shop_goods_list[ 'invoice_content' ] ?? '',
  141. 'invoice_full_address' => $shop_goods_list[ 'invoice_full_address' ] ?? '',
  142. 'is_invoice' => $shop_goods_list[ 'is_invoice' ] ?? 0,
  143. 'invoice_type' => $shop_goods_list[ 'invoice_type' ] ?? 0,
  144. 'invoice_title' => $shop_goods_list[ 'invoice_title' ] ?? '',
  145. 'is_tax_invoice' => $shop_goods_list[ 'is_tax_invoice' ] ?? '',
  146. 'invoice_email' => $shop_goods_list[ 'invoice_email' ] ?? '',
  147. 'invoice_title_type' => $shop_goods_list[ 'invoice_email' ] ?? 0,
  148. ];
  149. $order_id = model('order')->add($data_order);
  150. $pay_money += $shop_goods_list[ 'pay_money' ];
  151. //订单项目表
  152. foreach ($shop_goods_list[ 'goods_list' ] as $k_order_goods => $order_goods) {
  153. $data_order_goods = array (
  154. 'order_id' => $order_id,
  155. 'site_id' => $shop_goods_list[ 'site_id' ],
  156. 'order_no' => $order_no,
  157. 'member_id' => $data[ 'member_id' ],
  158. 'sku_id' => $order_goods[ 'sku_id' ],
  159. 'sku_name' => $order_goods[ 'sku_name' ],
  160. 'sku_image' => $order_goods[ 'sku_image' ],
  161. 'sku_no' => $order_goods[ 'sku_no' ],
  162. 'is_virtual' => $order_goods[ 'is_virtual' ],
  163. 'goods_class' => $order_goods[ 'goods_class' ],
  164. 'goods_class_name' => $order_goods[ 'goods_class_name' ],
  165. 'price' => $order_goods[ 'discount_price' ],
  166. 'cost_price' => $order_goods[ 'cost_price' ],
  167. 'num' => $order_goods[ 'num' ],
  168. 'goods_money' => $order_goods[ 'discount_price' ] * $order_goods[ 'num' ],
  169. 'cost_money' => $order_goods[ 'cost_price' ] * $order_goods[ 'num' ],
  170. 'goods_id' => $order_goods[ 'goods_id' ],
  171. 'delivery_status' => 0,
  172. 'delivery_status_name' => '未发货',
  173. 'real_goods_money' => $order_goods[ 'real_goods_money' ],
  174. 'coupon_money' => $order_goods[ 'coupon_money' ] ?? 0,
  175. 'promotion_money' => $order_goods[ 'promotion_money' ],
  176. 'goods_name' => $order_goods[ 'goods_name' ],
  177. 'sku_spec_format' => $order_goods[ 'sku_spec_format' ],
  178. );
  179. model('order_goods')->add($data_order_goods);
  180. //库存变化
  181. $stock_result = $goods_stock_model->decStock([ 'sku_id' => $order_goods[ 'sku_id' ], 'num' => $order_goods[ 'num' ] ]);
  182. if ($stock_result[ 'code' ] != 0) {
  183. model('order')->rollback();
  184. return $stock_result;
  185. }
  186. }
  187. $result_list = event('OrderCreate', [ 'order_id' => $order_id, 'site_id' => $shop_goods_list[ 'site_id' ] ]);
  188. if (!empty($result_list)) {
  189. foreach ($result_list as $k => $v) {
  190. if (!empty($v) && $v[ 'code' ] < 0) {
  191. model('order')->rollback();
  192. return $v;
  193. }
  194. }
  195. }
  196. $config_model = new Config();
  197. $balance_config = $config_model->getBalanceConfig($shop_goods_list[ 'site_id' ]);
  198. //扣除余额(统一扣除)
  199. if ($calculate_data[ 'balance_money' ] > 0 && $balance_config[ 'data' ][ 'value' ][ 'balance_show' ] == 1) {
  200. $calculate_data[ 'order_id' ] = $order_id;
  201. $balance_result = $this->storeUseBalance($calculate_data);
  202. if ($balance_result[ 'code' ] < 0) {
  203. model('order')->rollback();
  204. return $balance_result;
  205. }
  206. }
  207. //优惠券
  208. if ($data_order[ 'coupon_id' ] > 0 && $data_order[ 'coupon_money' ] > 0) {
  209. //优惠券处理方案
  210. $member_coupon_model = new Coupon();
  211. $coupon_use_result = $member_coupon_model->useCoupon($data_order[ 'coupon_id' ], $data[ 'member_id' ], $order_id);//使用优惠券
  212. if ($coupon_use_result[ 'code' ] < 0) {
  213. model('order')->rollback();
  214. return $this->error('', 'COUPON_ERROR');
  215. }
  216. }
  217. //生成整体支付单据
  218. $pay->addPay($data[ 'site_id' ], $out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'OrderPayNotify', '');
  219. $this->addOrderCronClose($order_id, $data[ 'site_id' ]);//增加关闭订单自动事件
  220. Cache::tag('order_create_cash_' . $data[ 'member_id' ])->clear();
  221. model('order')->commit();
  222. return $this->success($out_trade_no);
  223. } catch (\Exception $e) {
  224. model('order')->rollback();
  225. return $this->error('', $e->getMessage());
  226. }
  227. }
  228. /**
  229. * 订单计算
  230. * @param unknown $data
  231. */
  232. public function calculate($data)
  233. {
  234. $data = $this->storeInfo($data);
  235. $data = $this->initMemberAccount($data);//初始化会员账户
  236. //余额付款
  237. if ($data[ 'is_balance' ] > 0) {
  238. $this->member_balance_money = $data[ 'member_account' ][ 'balance_total' ] ?? 0;
  239. }
  240. //商品列表信息
  241. $shop_goods_list = $this->getOrderGoodsCalculate($data);
  242. $data[ 'shop_goods_list' ] = $this->shopOrderCalculate($shop_goods_list, $data);
  243. //总结计算
  244. $data[ 'delivery_money' ] = sprintf('%.2f', $this->delivery_money);
  245. $data[ 'coupon_money' ] = sprintf('%.2f', $this->coupon_money);
  246. $data[ 'adjust_money' ] = $this->adjust_money;
  247. $data[ 'invoice_money' ] = $this->invoice_money;
  248. $data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
  249. $data[ 'promotion_money' ] = sprintf('%.2f', $this->promotion_money);
  250. $data[ 'order_money' ] = sprintf('%.2f', $this->order_money);
  251. $data[ 'balance_money' ] = sprintf('%.2f', $this->balance_money);
  252. $data[ 'pay_money' ] = sprintf('%.2f', $this->pay_money);
  253. $data[ 'goods_money' ] = sprintf('%.2f', $this->goods_money);
  254. $data[ 'goods_num' ] = $this->goods_num;
  255. $data[ 'is_virtual' ] = $this->is_virtual;
  256. return $data;
  257. }
  258. /**
  259. * 待付款订单
  260. * @param unknown $data
  261. */
  262. public function orderPayment($data)
  263. {
  264. $calculate_data = $this->calculate($data);
  265. if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
  266. return $calculate_data;
  267. //1、查询会员优惠券
  268. $coupon_list = $this->getOrderCouponList($calculate_data);
  269. $calculate_data[ 'shop_goods_list' ][ 'coupon_list' ] = $coupon_list;
  270. $express_type = [];
  271. $calculate_data[ 'shop_goods_list' ][ 'express_type' ] = $express_type;
  272. return $this->success($calculate_data);
  273. }
  274. /**
  275. * 获取商品的计算信息
  276. * @param unknown $data
  277. */
  278. public function getOrderGoodsCalculate($data)
  279. {
  280. $goods_list = $this->getStoreGoodsList($data[ 'sku_ids' ], $data[ 'nums' ], $data[ 'member_id' ], $data[ 'site_id' ], $data[ 'store_id' ]);
  281. $goods_list[ 'promotion_money' ] = 0;
  282. $shop_goods_list = $goods_list;
  283. //满减优惠
  284. $shop_goods_list = $this->manjianPromotion($shop_goods_list);
  285. // Cache::tag('order_create_store_'.$data['member_id'])->set('order_create_seckill_'.$data['id'].'_'.$data['num'].'_'.$data['member_id'], $shop_goods_list, 600);
  286. return $shop_goods_list;
  287. }
  288. /**
  289. * 获取秒杀商品列表信息
  290. * @param unknown $bl_id
  291. */
  292. public function getStoreGoodsList($sku_ids, $nums, $member_id, $site_id, $store_id)
  293. {
  294. $goods_model = new Goods();
  295. //组装商品列表
  296. $field = 'sgs.stock,sgs.sale_num,ngs.sku_name, ngs.sku_id,ngs.sku_no,
  297. ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,
  298. ngs.site_id, ngs.goods_state, ngs.is_virtual,
  299. ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.goods_id,ngs.sku_spec_format,ngs.goods_name';
  300. $alias = 'sgs';
  301. $join = [
  302. [
  303. 'goods_sku ngs',
  304. 'sgs.sku_id = ngs.sku_id',
  305. 'inner'
  306. ],
  307. ];
  308. $goods_list = model('store_goods_sku')->getList([ [ 'sgs.sku_id', 'in', $sku_ids ], [ 'ngs.site_id', '=', $site_id ], [ 'sgs.store_id', '=', $store_id ] ], $field, '', $alias, $join);
  309. if (!empty($goods_list)) {
  310. foreach ($goods_list as $k => $v) {
  311. $num = $nums[ $v[ 'sku_id' ] ] ?? 0;
  312. $v[ 'num' ] = $num;
  313. $site_id = $v[ 'site_id' ];
  314. $price_result = $goods_model->getGoodsPrice($v[ 'sku_id' ], $member_id);
  315. $price_info = $price_result[ 'data' ];
  316. $price = $price_info[ 'price' ];
  317. $v[ 'price' ] = $price;
  318. $v[ 'goods_money' ] = $price * $v[ 'num' ];
  319. $v[ 'real_goods_money' ] = $v[ 'goods_money' ];
  320. $v[ 'coupon_money' ] = 0;//优惠券金额
  321. $v[ 'promotion_money' ] = 0;//优惠金额
  322. $v[ 'stock' ] = numberFormat($v[ 'stock' ]);
  323. if (isset($shop_goods_list)) {
  324. $shop_goods_list[ 'goods_list' ][] = $v;
  325. $shop_goods_list[ 'order_name' ] = string_split($shop_goods_list[ 'order_name' ], ',', $v[ 'sku_name' ]);
  326. $shop_goods_list[ 'goods_num' ] += $num;
  327. $shop_goods_list[ 'goods_money' ] += $v[ 'goods_money' ];
  328. $shop_goods_list[ 'goods_list_str' ] = $shop_goods_list[ 'goods_list_str' ] . ';' . $v[ 'sku_id' ] . ':' . $num;
  329. } else {
  330. $shop_goods_list[ 'site_id' ] = $site_id;
  331. $shop_goods_list[ 'goods_money' ] = $v[ 'goods_money' ];
  332. $shop_goods_list[ 'goods_list_str' ] = $v[ 'sku_id' ] . ':' . $num;
  333. $shop_goods_list[ 'order_name' ] = string_split('', ',', $v[ 'sku_name' ]);
  334. $shop_goods_list[ 'goods_num' ] = $num;
  335. $shop_goods_list[ 'goods_list' ][] = $v;
  336. }
  337. }
  338. }
  339. return $shop_goods_list;
  340. }
  341. /**
  342. * 获取店铺订单计算
  343. * @param unknown $site_id 店铺id
  344. * @param unknown $goods_money 商品总价
  345. * @param unknown $goods_list 店铺商品列表
  346. * @param unknown $data 传输生成订单数据
  347. */
  348. public function shopOrderCalculate($shop_goods, $data)
  349. {
  350. $site_id = $shop_goods[ 'site_id' ];
  351. //交易配置
  352. $config_model = new Config();
  353. $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
  354. $order_config = $order_config_result[ 'data' ];
  355. $shop_goods[ 'order_config' ] = $order_config[ 'value' ] ?? [];
  356. //定义计算金额
  357. $goods_money = $shop_goods[ 'goods_money' ]; //商品金额
  358. $delivery_money = 0; //配送费用
  359. $promotion_money = $shop_goods[ 'promotion_money' ]; //优惠费用(满减)
  360. $coupon_money = 0; //优惠券费用
  361. $adjust_money = 0; //调整金额
  362. $invoice_money = 0; //发票金额
  363. $order_money = 0; //订单金额
  364. $balance_money = 0; //会员余额
  365. $pay_money = 0; //应付金额
  366. //计算邮费
  367. $express_config_model = new ExpressConfig();
  368. //查询店铺是否开启门店自提
  369. $store_config_result = $express_config_model->getStoreConfig($site_id);
  370. $store_config = $store_config_result[ 'data' ];
  371. $shop_goods[ 'store_config' ] = $store_config;
  372. $shop_goods[ 'delivery' ][ 'delivery_type' ] = '';
  373. //门店自提
  374. $delivery_money = 0;
  375. if (empty($data[ 'store_id' ])) {
  376. $this->error = 1;
  377. $this->error_msg = '门店未选择!';
  378. }
  379. $shop_goods[ 'delivery' ][ 'store_id' ] = $data[ 'store_id' ];
  380. //发票相关
  381. $shop_goods = $this->invoice($shop_goods, $data);
  382. $order_money = $goods_money + $delivery_money - $promotion_money - $shop_goods[ 'invoice_money' ] + $shop_goods[ 'invoice_delivery_money' ];
  383. $shop_goods[ 'order_money' ] = $order_money;//订单总金额
  384. //优惠券活动(采用站点id:coupon_id)
  385. $shop_goods = $this->couponPromotion($shop_goods, $data);
  386. $coupon_money = $shop_goods[ 'coupon_money' ] ?? 0;
  387. $order_money = $shop_goods[ 'order_money' ];
  388. //理论上是多余的操作
  389. if ($order_money < 0) {
  390. $order_money = 0;
  391. }
  392. //余额抵扣(判断是否使用余额)
  393. if ($this->member_balance_money > 0) {
  394. if ($order_money <= $this->member_balance_money) {
  395. $balance_money = $order_money;
  396. } else {
  397. $balance_money = $this->member_balance_money;
  398. }
  399. } else {
  400. $balance_money = 0;
  401. }
  402. $pay_money = $order_money - $balance_money;//计算出实际支付金额
  403. $this->member_balance_money -= $balance_money;//预减少账户余额
  404. $this->balance_money += $balance_money;//累计余额
  405. //总结计算
  406. $shop_goods[ 'goods_money' ] = $goods_money;
  407. $shop_goods[ 'delivery_money' ] = $delivery_money;
  408. $shop_goods[ 'adjust_money' ] = $adjust_money;
  409. $shop_goods[ 'promotion_money' ] = $promotion_money;
  410. $shop_goods[ 'order_money' ] = $order_money;
  411. $shop_goods[ 'balance_money' ] = $balance_money;
  412. $shop_goods[ 'pay_money' ] = $pay_money;
  413. $this->goods_money += $goods_money;
  414. $this->delivery_money += $delivery_money;
  415. $this->coupon_money += $coupon_money;
  416. $this->adjust_money += $adjust_money;
  417. $this->invoice_money += $shop_goods[ 'invoice_money' ];
  418. $this->invoice_delivery_money += $shop_goods[ 'invoice_delivery_money' ];
  419. $this->promotion_money += $promotion_money;
  420. $this->order_money += $order_money;
  421. $this->pay_money += $pay_money;
  422. $this->goods_num += $shop_goods[ 'goods_num' ];
  423. $this->order_name = string_split($this->order_name, ',', $shop_goods[ 'order_name' ]);
  424. return $shop_goods;
  425. }
  426. /**
  427. * 补齐门店数据
  428. * @param $data
  429. */
  430. public function storeInfo($data)
  431. {
  432. $delivery_store_id = $data[ 'store_id' ] ?? 0;//门店id
  433. if ($delivery_store_id > 0) {
  434. $store_model = new Store();
  435. $condition = array (
  436. [ 'store_id', '=', $delivery_store_id ],
  437. );
  438. $store_info_result = $store_model->getStoreInfo($condition);
  439. $store_info = $store_info_result[ 'data' ] ?? [];
  440. if (empty($store_info)) {
  441. $this->error = 1;
  442. $this->error_msg = '当前门店不存在或未开启!';
  443. } else {
  444. $data[ 'site_id' ] = $store_info[ 'site_id' ];
  445. $data[ 'delivery_store_id' ] = $delivery_store_id;
  446. $delivery_store_name = $store_info_result[ 'data' ][ 'store_name' ];
  447. $data[ 'delivery_store_name' ] = $delivery_store_name;
  448. $delivery_store_info = array (
  449. 'open_date' => $store_info[ 'open_date' ],
  450. 'full_address' => $store_info[ 'full_address' ] . $store_info[ 'address' ],
  451. 'longitude' => $store_info[ 'longitude' ],
  452. 'latitude' => $store_info[ 'latitude' ],
  453. 'telphone' => $store_info[ 'telphone' ],
  454. 'store_image' => $store_info[ 'store_image' ],
  455. );
  456. $data[ 'delivery_store_info' ] = json_encode($delivery_store_info, JSON_UNESCAPED_UNICODE);
  457. $data[ 'member_address' ] = array (
  458. 'name' => '门店收银',
  459. 'mobile' => '',
  460. 'telephone' => '',
  461. 'province_id' => $store_info[ 'province_id' ],
  462. 'city_id' => $store_info[ 'city_id' ],
  463. 'district_id' => $store_info[ 'district_id' ],
  464. 'community_id' => $store_info[ 'community_id' ],
  465. 'address' => $store_info[ 'address' ],
  466. 'full_address' => str_replace(',', '-', $store_info[ 'full_address' ]),
  467. 'longitude' => $store_info[ 'longitude' ],
  468. 'latitude' => $store_info[ 'latitude' ],
  469. );
  470. }
  471. } else {
  472. $this->error = 1;
  473. $this->error_msg = '配送门店不可为空!';
  474. }
  475. return $data;
  476. }
  477. /**
  478. * 使用余额
  479. * @param $order_data
  480. * @return array
  481. */
  482. public function storeUseBalance($data)
  483. {
  484. $this->pay_type = 'BALANCE';
  485. $balance_money = $data[ 'member_account' ][ 'balance_money' ];//储值余额
  486. $balance = $data[ 'member_account' ][ 'balance' ];//现金余额
  487. $member_account_model = new MemberAccount();
  488. $surplus_banance = $data[ 'balance_money' ];
  489. //优先扣除储值余额
  490. if ($balance > 0) {
  491. if ($balance >= $surplus_banance) {
  492. $real_balance = $surplus_banance;
  493. } else {
  494. $real_balance = $balance;
  495. }
  496. $result = $member_account_model->addMemberAccount($data[ 'site_id' ], $data[ 'member_id' ], 'balance', -$real_balance, 'order', $data[ 'order_id' ], '订单消费扣除');
  497. $surplus_banance -= $real_balance;
  498. }
  499. if ($surplus_banance > 0) {
  500. $result = $member_account_model->addMemberAccount($data[ 'site_id' ], $data[ 'member_id' ], 'balance_money', -$surplus_banance, 'order', $data[ 'order_id' ], '订单消费扣除');
  501. }
  502. return $result;
  503. }
  504. }