OrderCreate.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\order;
  13. use addon\coupon\model\Coupon;
  14. use app\api\controller\Goodssku;
  15. use app\model\express\ExpressTemplate;
  16. use app\model\goods\Goods;
  17. use app\model\goods\GoodsStock;
  18. use app\model\member\FranchiseeExamine;
  19. use app\model\member\Member;
  20. use app\model\member\Member as MemberModel;
  21. use app\model\member\MemberAccount;
  22. use app\model\member\MemberVip;
  23. use app\model\store\Store;
  24. use app\model\system\Cron;
  25. use app\model\web\Platform as PlatformModel;
  26. use think\facade\Cache;
  27. use app\model\express\Express;
  28. use app\model\system\Pay;
  29. use app\model\goods\Cart;
  30. use app\model\member\MemberAddress;
  31. use app\model\express\Config as ExpressConfig;
  32. use app\model\BaseModel;
  33. use app\model\message\Message;
  34. /**
  35. * 订单创建(普通订单)
  36. *
  37. * @author Administrator
  38. *
  39. */
  40. class OrderCreate extends BaseModel
  41. {
  42. private $goods_money = 0;//商品金额
  43. private $delivery_money = 0;//配送费用
  44. private $coupon_money = 0;//优惠券金额
  45. private $adjust_money = 0;//调整金额
  46. private $invoice_money = 0;//发票费用
  47. private $promotion_money = 0;//优惠金额
  48. private $order_money = 0;//订单金额
  49. private $pay_money = 0;//支付总价
  50. private $is_virtual = 0; //是否是虚拟类订单
  51. private $order_name = ''; //订单详情
  52. private $goods_num = 0; //商品种数
  53. private $error = 0; //是否有错误
  54. private $error_msg = ''; //错误描述
  55. private $pay_type = 'ONLINE_PAY';
  56. private $balance_money = 0;
  57. private $is_exist_not_free = false;
  58. private $is_exist_free = false;
  59. private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
  60. public function ordercreate($data){
  61. $member_arrdess = $this->member_address($data);
  62. // $member_level = $this->member_discount($data['member_id']);
  63. // if(empty($member_level['data'])){
  64. // $member_level['data']['discount'] = 1;
  65. // $member_level['data']['name'] = '无折扣';
  66. // }
  67. $platform_model = new PlatformModel();
  68. $platform_division = $platform_model->info('platform_division');
  69. $pre =1-$platform_division['data']['value']/100;
  70. $goods_id = model('goods_sku')->getInfo(['sku_id'=>$data['sku'][0]['sku_id']],'goods_id');
  71. model("order")->startTrans();
  72. $dev=['goods_id'=>$goods_id['goods_id'],
  73. 'member_id'=>$data['member_id'],
  74. 'address_id'=>$data['address_id'],
  75. 'num'=>$data['number'],
  76. ];
  77. $delivery_money = $this->freight($dev);
  78. //循环生成多个订单
  79. $pay = new Pay();
  80. $out_trade_no = $pay->createOutTradeNo();
  81. try {
  82. $order_no = $this->createOrderNo($data['site_id']);
  83. $site_id = $data['site_id'];
  84. $site_name = $data['site_name'];
  85. $price = $data['price'];
  86. $number = $data['number'];
  87. $unit_price = $data['unit_price'];
  88. $add_data = [
  89. 'order_no' => $order_no,
  90. 'site_id' => $site_id,
  91. 'site_name' => $site_name,
  92. 'price' => $price,
  93. 'number' => $number,
  94. 'order_type' => 1,
  95. 'order_type_name' => '普通订单',
  96. 'order_status_name' => '待支付',
  97. 'member_id'=>$data['member_id'],
  98. 'order_status_action' =>'{"status":0,"name":"待支付","is_allow_refund":0,"action":[{"action":"orderClose","title":"关闭订单","color":""},{"action":"orderAddressUpdate","title":"修改地址","color":""},{"action":"orderAdjustMoney","title":"调整价格","color":""}],"member_action":[{"action":"orderClose","title":"关闭订单","color":""},{"action":"orderPay","title":"支付","color":""}],"color":""}',
  99. 'name' => $member_arrdess['name'],
  100. 'out_trade_no' => $out_trade_no,
  101. 'mobile' => $member_arrdess['mobile'],
  102. 'telephone' => $member_arrdess['telephone'],
  103. 'province_id' => $member_arrdess['province_id'],
  104. 'city_id' => $member_arrdess['city_id'],
  105. 'district_id' => $member_arrdess['district_id'],
  106. 'community_id' => $member_arrdess['community_id'] ,
  107. 'address' => $member_arrdess['address'] ,
  108. 'full_address' => $member_arrdess['full_address'] ,
  109. 'longitude' => $member_arrdess['longitude'] ,
  110. 'latitude' => $member_arrdess['latitude'] ,
  111. 'buyer_ip' => request()->ip(),
  112. 'create_time' => time(),
  113. 'delivery_money'=>$delivery_money['data'],
  114. 'pay_money' => $price,
  115. 'goods_num'=>$number,
  116. 'order_money'=>$price,
  117. 'goods_money'=>$price,
  118. ];
  119. $add_data['shop_money'] = $pre*$add_data['pay_money'];
  120. $add_data['pay_money']=$add_data['pay_money']+$delivery_money['data'];
  121. $order_id = model("order")->add($add_data);
  122. model("order")->commit();
  123. $goods_stock_model = new GoodsStock();
  124. foreach ($data['sku'] as &$v){
  125. $order_goods = $this->goodssku($v['sku_id']);
  126. $data_order_goods =[
  127. 'order_id' => $order_id,
  128. 'site_id' =>$order_goods['goodssku']['site_id'],
  129. 'site_name' =>$order_goods['goodssku']['site_name'],
  130. 'order_no' => $order_no,
  131. 'member_id' => $data['member_id'],
  132. 'sku_id' => $order_goods['goodssku']['sku_id'],
  133. 'sku_name' => $order_goods['goodssku']['sku_name'],
  134. 'sku_image' => $order_goods['goodssku']['sku_image'],
  135. 'sku_no' => $order_goods['goodssku']['sku_no'],
  136. 'is_virtual' => $order_goods['goodssku']['is_virtual'],
  137. 'goods_class' => $order_goods['goodssku']['goods_class'],
  138. 'goods_class_name' => $order_goods['goodssku']['goods_class_name'],
  139. 'price' => $unit_price,
  140. 'cost_price' => $price,
  141. 'num' => $v['num'],
  142. 'goods_money' => $unit_price*$v['num'],
  143. 'cost_money' => '',
  144. 'real_goods_money' =>$unit_price*$v['num'],
  145. 'commission_rate' => $order_goods['goodssku']['commission_rate'],
  146. 'goods_id' => $order_goods['goodssku']['goods_id'],
  147. 'delivery_status' => 0,
  148. 'delivery_status_name' => "未发货",
  149. ];
  150. $data_order_goods['real_goods_money']=$data_order_goods['real_goods_money'];
  151. model("order_goods")->add($data_order_goods);
  152. $stock_result = $goods_stock_model->decStock(["sku_id" => $data_order_goods['sku_id'], "num" => $data_order_goods['num']]);
  153. if($stock_result["code"] != 0){
  154. model("order")->rollback();
  155. return $stock_result;
  156. }
  157. }
  158. $pay->addPay(0, $out_trade_no, $this->pay_type, '商品购买', '商品购买', $add_data['pay_money'], '', 'OrderPayNotify', '');
  159. $this->addOrderCronClose($order_id);//增加关闭订单自动事件
  160. }
  161. catch(\Exception $e)
  162. {
  163. model("order")->rollback();
  164. return $this->error('', $e->getMessage().$e->getFile().$e->getLine());
  165. }
  166. return $this->success(['out_trade_no'=>$out_trade_no,'order_id'=>$order_id,'pay_money'=>$add_data['pay_money']]);
  167. }
  168. /**
  169. * @param $data
  170. * @return mixed
  171. * 用户地址
  172. */
  173. public function member_address($data){
  174. $member_address = new MemberAddress();
  175. $address = $member_address->getMemberAddressInfo([['member_id','=',$data['member_id']],['id', '=', $data['address_id']]]);
  176. $member = $address['data'];
  177. return $member;
  178. }
  179. /**
  180. * 直接下单运费
  181. */
  182. public function freight($data){
  183. $delivery_money = 0;
  184. $goods_model = new Goods();
  185. $template = new ExpressTemplate();
  186. $member_address = $this->member_address($data);
  187. $goods = $goods_model->getGoodsInfo([['goods_id', '=', $data['goods_id']]],'goods_name,site_id,is_free_shipping,shipping_template,sku_id,weight');
  188. if($goods['data']['is_free_shipping']==0){
  189. $temolate_item_info = $template->getExpressTemplateInfo($goods['data']['shipping_template'],$goods['data']['site_id']);
  190. foreach ($temolate_item_info['data']['template_item'] as $k_item => $v_item)
  191. {
  192. if(strpos($v_item['area_ids'] , '"'.$member_address['district_id'].'"') !== false)
  193. {
  194. switch($v_item['fee_type'])
  195. {
  196. case 1:
  197. $tag = $data['num']*$goods['data']['weight'];
  198. break;
  199. case 3:
  200. $tag =$data['num'];
  201. break;
  202. default:
  203. break;
  204. }
  205. //开始计算
  206. if ($tag <= $v_item['snum']) {
  207. $delivery_money += $v_item['sprice'];
  208. } else {
  209. $ext_tag = $tag - $v_item['snum'];
  210. if ($v_item['xnum'] == 0) {
  211. $v_item['xnum'] = 1;
  212. }
  213. if (($ext_tag * 100) % ($v_item['xnum'] * 100) == 0) {
  214. $ext_data = $ext_tag / $v_item['xnum'];
  215. } else {
  216. $ext_data = floor($ext_tag / $v_item['xnum']) + 1;
  217. }
  218. $delivery_money += $v_item['sprice'] + $ext_data * $v_item['xprice'];
  219. }
  220. break;
  221. }
  222. }
  223. }
  224. return $this->success($delivery_money);
  225. }
  226. /**
  227. * @param $data
  228. * @return mixed
  229. * 商品信息
  230. */
  231. public function goodsInfo($data){
  232. $goods_model = new Goods();
  233. $goods = $goods_model->getGoodsInfo([['goods_id', '=', $data]],'goods_name,is_free_shipping,shipping_template');
  234. $data['goods'] = $goods['data'];
  235. return $data;
  236. }
  237. public function goodssku($data){
  238. $goods_model =new Goods();
  239. $goods_sku =$goods_model->getGoodsSkuInfo([['sku_id','=',$data]]);
  240. $goodssku['goodssku'] = $goods_sku['data'];
  241. return $goodssku;
  242. }
  243. /**
  244. * 订单创建
  245. * @param unknown $data
  246. */
  247. public function create($data)
  248. {
  249. //查询出会员相关信息
  250. $platform_model = new PlatformModel();
  251. $platform_division = $platform_model->info('platform_division');
  252. $pre =1-$platform_division['data']['value']/100;
  253. $calculate_data = $this->calculate($data);
  254. if($this->error > 0){
  255. return $this->error("", $this->error_msg);
  256. }
  257. $pay = new Pay();
  258. if($this->is_exist_not_free){
  259. $out_trade_no = $pay->createOutTradeNo();
  260. $order_name = [];
  261. }
  262. if($this->is_exist_free){
  263. $free_out_trade_no = $pay->createOutTradeNo();
  264. $free_order_name = [];
  265. }
  266. // $member_level = $this->member_discount($data['member_id']);
  267. // if(empty($member_level['data'])){
  268. // $member_level['data']['discount'] = 1;
  269. // $member_level['data']['name'] = '无折扣';
  270. // }
  271. model("order")->startTrans();
  272. //循环生成多个订单
  273. try{
  274. $pay_money = 0;
  275. $goods_stock_model = new GoodsStock();
  276. foreach ($calculate_data['shop_goods_list'] as $k => $v)
  277. {
  278. $item_delivery = $v['delivery'] ?? [];
  279. $delivery_type = $item_delivery['delivery_type'] ?? '';
  280. $delivery_type_name = Express::express_type[$delivery_type]["title"] ?? '';
  281. //判断交易流水号
  282. if($v["pay_money"] == 0){
  283. $temp_out_trade_no = $free_out_trade_no;
  284. $free_order_name[] = $v["order_name"];
  285. }else{
  286. $temp_out_trade_no = $out_trade_no;
  287. $order_name[] = $v["order_name"];
  288. }
  289. //订单主表
  290. $order_type = $this->orderType($v, $calculate_data);
  291. $order_no = $this->createOrderNo($v['site_id']);
  292. $data_order = [
  293. 'order_no' => $order_no,
  294. 'site_id' => $v['site_id'],
  295. 'site_name' => $v['site_name'],
  296. 'order_from' => $data['order_from'],
  297. 'order_from_name' => $data['order_from_name'],
  298. 'order_type' => $order_type['order_type_id'],
  299. 'order_type_name' => $order_type['order_type_name'],
  300. 'order_status_name' => $order_type['order_status']['name'],
  301. 'order_status_action' => json_encode($order_type['order_status'], JSON_UNESCAPED_UNICODE),
  302. 'out_trade_no' => $temp_out_trade_no,
  303. 'member_id' => $data['member_id'],
  304. 'name' => $calculate_data['member_address']['name'] ?? '',
  305. 'mobile' => $calculate_data['member_address']['mobile'] ?? '',
  306. 'telephone' => $calculate_data['member_address']['telephone'] ?? '',
  307. 'province_id' => $calculate_data['member_address']['province_id'] ?? '',
  308. 'city_id' => $calculate_data['member_address']['city_id'] ?? '',
  309. 'district_id' => $calculate_data['member_address']['district_id'] ?? '',
  310. 'community_id' => $calculate_data['member_address']['community_id'] ?? '',
  311. 'address' => $calculate_data['member_address']['address'] ?? '',
  312. 'full_address' => $calculate_data['member_address']['full_address'] ?? '',
  313. 'longitude' => $calculate_data['member_address']['longitude'] ?? '',
  314. 'latitude' => $calculate_data['member_address']['latitude'] ?? '',
  315. 'buyer_ip' => request()->ip(),
  316. 'goods_money' => $v['goods_money'],
  317. 'delivery_money' => $v['delivery_money'],
  318. 'coupon_id' => isset($v['coupon_id']) ? $v['coupon_id'] : 0,
  319. 'coupon_money' => $v['coupon_money'],
  320. 'adjust_money' => $v['adjust_money'],
  321. 'invoice_money' => $v['invoice_money'],
  322. 'promotion_money' => $v['promotion_money'],
  323. 'order_money' => $v['order_money'],
  324. 'balance_money' => $v['balance_money'],
  325. 'pay_money' => $v['pay_money'],
  326. 'create_time' => time(),
  327. 'is_enable_refund' => 0,
  328. 'order_name' => $v["order_name"],
  329. 'goods_num' => $v['goods_num'],
  330. 'delivery_type' => $delivery_type,
  331. 'delivery_type_name' => $delivery_type_name,
  332. 'delivery_store_id' => $v["delivery_store_id"] ?? 0,
  333. "delivery_store_name" => $v["delivery_store_name"] ?? '',
  334. "delivery_store_info" => $v["delivery_store_info"] ?? '',
  335. "buyer_message" => $data["buyer_message"],
  336. "website_id" => $v["website_id"],
  337. ];
  338. $data_order['shop_money'] = $pre*$data_order['pay_money'];
  339. // print_r($data_order);die;
  340. $order_id = model("order")->add($data_order);
  341. $this->addOrderCronClose($order_id);//增加关闭订单自动事件
  342. $order_id_arr[] = $order_id;
  343. $pay_money += $v['pay_money'];
  344. //订单项目表
  345. foreach ($v['goods_list'] as $k_order_goods => $order_goods)
  346. {
  347. $data_order_goods = array(
  348. 'order_id' => $order_id,
  349. 'site_id' => $v['site_id'],
  350. 'site_name' => $v['site_name'],
  351. 'order_no' => $order_no,
  352. 'member_id' => $data['member_id'],
  353. 'sku_id' => $order_goods['sku_id'],
  354. 'sku_name' => $order_goods['sku_name'],
  355. 'sku_image' => $order_goods['sku_image'],
  356. 'sku_no' => $order_goods['sku_no'],
  357. 'is_virtual' => $order_goods['is_virtual'],
  358. 'goods_class' => $order_goods['goods_class'],
  359. 'goods_class_name' => $order_goods['goods_class_name'],
  360. 'price' => $order_goods['discount_price'],
  361. 'cost_price' => $order_goods['cost_price'],
  362. 'num' => $order_goods['num'],
  363. 'goods_money' => $order_goods['discount_price']*$order_goods['num'],
  364. 'cost_money' => $order_goods['cost_price']*$order_goods['num'],
  365. 'commission_rate' => $order_goods['commission_rate'],
  366. 'goods_id' => $order_goods['goods_id'],
  367. 'delivery_status' => 0,
  368. 'delivery_status_name' => "未发货",
  369. );
  370. $data_order_goods['real_goods_money'] = $data_order_goods['goods_money'];
  371. model("order_goods")->add($data_order_goods);
  372. //库存变化
  373. $stock_result = $goods_stock_model->decStock(["sku_id" => $order_goods['sku_id'], "num" => $order_goods['num']]);
  374. if($stock_result["code"] != 0){
  375. model("order")->rollback();
  376. return $stock_result;
  377. }
  378. // model("goods_sku")->setDec([['sku_id', '=', $order_goods['sku_id']]], 'stock', $order_goods['num']);
  379. // $sku_stock = model("goods_sku")->getInfo([['sku_id', '=', $order_goods['sku_id']]], 'stock');
  380. // if($sku_stock['stock'] < 0)
  381. // {
  382. // model("order")->rollback();
  383. // return $this->error('', "库存不足");
  384. // }
  385. }
  386. //订单满减优惠
  387. $manjian_rule = isset($v['promotion']['manjian']) ? $v['promotion']['manjian'] : '';
  388. if(!empty($manjian_rule))
  389. {
  390. $order_promotion_detail = [
  391. 'order_id' => $order_id,
  392. 'site_id' => $v['site_id'],
  393. 'promotion_text' => '满'.$manjian_rule['money'].'元减'.$manjian_rule['discount_money'],
  394. 'money' => $manjian_rule['discount_money'],
  395. 'create_time' => time()
  396. ];
  397. model('order_promotion_detail')->add($order_promotion_detail);
  398. }
  399. //优惠券
  400. if($data_order['coupon_id'] > 0 && $data_order['coupon_money'] > 0)
  401. {
  402. //优惠券处理方案
  403. $member_coupon_model = new Coupon();
  404. $coupon_use_result = $member_coupon_model->useCoupon($data_order['coupon_id'], $data['member_id'], $order_id);//使用优惠券
  405. if($coupon_use_result['code'] < 0)
  406. {
  407. model("order")->rollback();
  408. return $this->error('', "COUPON_ERROR");
  409. }
  410. }
  411. }
  412. //扣除余额(统一扣除)
  413. if($calculate_data["balance_money"] > 0){
  414. $balance_result = $this->useBalance($calculate_data);
  415. if($balance_result["code"] < 0){
  416. model("order")->rollback();
  417. return $balance_result;
  418. }
  419. }
  420. //生成整体付费支付单据
  421. if($this->is_exist_not_free) {
  422. $order_name_title = implode(",", $order_name);
  423. $pay->addPay(0, $out_trade_no, $this->pay_type, $order_name_title, $order_name_title, $this->pay_money, '', 'OrderPayNotify', '');
  424. }
  425. //免费订单支付单据
  426. if($this->is_exist_free){
  427. $free_order_name_title = implode(",", $free_order_name);
  428. $pay->addPay(0, $free_out_trade_no, $this->pay_type, $free_order_name_title, $free_order_name_title, 0, '', 'OrderPayNotify', '');
  429. }
  430. $cart_ids = isset($data['cart_ids']) ? $data['cart_ids'] : '';
  431. if(!empty($cart_ids))
  432. {
  433. $cart = new Cart();
  434. $data_cart = [
  435. 'cart_id' => $cart_ids,
  436. 'member_id' => $data['member_id']
  437. ];
  438. $cart->deleteCart($data_cart);
  439. }
  440. Cache::tag("order_create_member_".$data['member_id'])->clear();
  441. //循环执行订单完成事件
  442. foreach($order_id_arr as $k => $v){
  443. $result_list = event("OrderCreate", ['order_id' => $v]);
  444. if(!empty($result_list)){
  445. foreach($result_list as $k => $v){
  446. if(!empty($v) && $v["code"] < 0){
  447. model("order")->rollback();
  448. return $v;
  449. }
  450. }
  451. }
  452. }
  453. // $this->checkFree($data_order);//如果订单金额为0, 直接调用支付成功
  454. model("order")->commit();
  455. //订单生成的消息
  456. $message_model = new Message();
  457. $message_model->sendMessage(['keywords' => "ORDER_CREATE", 'order_id'=>$order_id]);
  458. return $this->success(['out_trade_no'=>$out_trade_no ?? $free_out_trade_no,'order_id'=>$order_id_arr,'pay_money'=>$this->pay_money]);
  459. }catch(\Exception $e)
  460. {
  461. model("order")->rollback();
  462. return $this->error('', $e->getMessage().$e->getFile().$e->getLine());
  463. }
  464. }
  465. public function member_discount($member_id){
  466. $member = new MemberModel();
  467. $vip_level = $member->getMemberInfo([['member_id','=',$member_id]],'member_level,member_type');
  468. if($vip_level['data']['member_type']==1) {
  469. $vip_discount = new MemberVip();
  470. $member_discount = $vip_discount->getMemberVipInfo([['id', '=', $vip_level['data']['member_level']]], 'discount,name');
  471. }
  472. if($vip_level['data']['member_type']==2){
  473. $franchisee_discount = new FranchiseeExamine();
  474. $member_discount = $franchisee_discount->getFranchiseeExamineInfo([['member_id', '=', $member_id]], 'discount');
  475. if($member_discount['data']) {
  476. $member_discount['data']['name'] = '加盟商';
  477. }
  478. }
  479. return $member_discount;
  480. }
  481. /**
  482. * 生成订单编号
  483. *
  484. * @param array $site_id
  485. */
  486. public function createOrderNo($site_id)
  487. {
  488. $time_str = date('YmdHi');
  489. $num = 0;
  490. $max_no = Cache::get($site_id . "_" . $time_str);
  491. if (! isset($max_no) || empty($max_no)) {
  492. $max_no = 1;
  493. } else {
  494. $max_no = $max_no + 1;
  495. }
  496. $order_no = $time_str . sprintf("%04d", $max_no);
  497. Cache::set($site_id . "_" . $time_str, $max_no);
  498. return $order_no;
  499. }
  500. /**
  501. * 订单类型判断
  502. * @param unknown $shop_goods
  503. */
  504. public function orderType($shop_goods, $data)
  505. {
  506. if($data["is_virtual"] == 1)
  507. {
  508. $order = new VirtualOrder();
  509. return [
  510. 'order_type_id' => 4,
  511. 'order_type_name' => '虚拟订单',
  512. 'order_status' => $order->order_status[0]
  513. ];
  514. }else{
  515. if($shop_goods['delivery']['delivery_type'] == 'express')
  516. {
  517. $order = new Order();
  518. return [
  519. 'order_type_id' => 1,
  520. 'order_type_name' => '普通订单',
  521. 'order_status' => $order->order_status[0]
  522. ];
  523. }elseif($shop_goods['delivery']['delivery_type'] == 'store'){
  524. $order = new StoreOrder();
  525. return [
  526. 'order_type_id' => 2,
  527. 'order_type_name' => '自提订单',
  528. 'order_status' => $order->order_status[0]
  529. ];
  530. }elseif($shop_goods['delivery']['delivery_type'] == 'local'){
  531. $order = new LocalOrder();
  532. return [
  533. 'order_type_id' => 3,
  534. 'order_type_name' => '外卖订单',
  535. 'order_status' => $order->order_status[0]
  536. ];
  537. }
  538. }
  539. }
  540. /**
  541. * 订单计算
  542. * @param unknown $data
  543. */
  544. public function calculate($data)
  545. {
  546. $data = $this->initMemberAddress($data);//初始化地址
  547. $data = $this->initMemberAccount($data);//初始化会员账户
  548. //余额付款
  549. if($data['is_balance'] > 0)
  550. {
  551. $this->member_balance_money = $data["member_account"]["balance_total"] ?? 0;
  552. }
  553. //传输购物车id组合','隔开要进行拆单
  554. if(!empty($data['cart_ids']))
  555. {
  556. //商品列表信息
  557. $shop_goods_list = $this->getOrderGoodsCalculate($data);
  558. foreach ($shop_goods_list as $k => $v)
  559. {
  560. $data['shop_goods_list'][$k] = $this->shopOrderCalculate($v, $data);
  561. }
  562. }
  563. else{
  564. //商品列表信息
  565. $shop_goods_list = $this->getOrderGoodsCalculate($data);
  566. //判断是否是虚拟订单
  567. if($shop_goods_list[0]['goods_list'][0]['is_virtual'])
  568. {
  569. $this->is_virtual = 1;
  570. }else{
  571. $this->is_virtual = 0;
  572. }
  573. $data['shop_goods_list'][$shop_goods_list[0]['goods_list'][0]['site_id']] = $this->shopOrderCalculate($shop_goods_list[0], $data);
  574. }
  575. //总结计算
  576. $data['delivery_money'] = $this->delivery_money;
  577. $data['coupon_money'] = $this->coupon_money;
  578. $data['adjust_money'] = $this->adjust_money;
  579. $data['invoice_money'] = $this->invoice_money;
  580. $data['promotion_money'] = $this->promotion_money;
  581. $data['order_money'] = $this->order_money;
  582. $data['balance_money'] = $this->balance_money;
  583. $data['pay_money'] = $this->pay_money;
  584. $data['goods_money'] = $this->goods_money;
  585. $data['goods_num'] = $this->goods_num;
  586. $data['is_virtual'] = $this->is_virtual;
  587. return $data;
  588. }
  589. /**
  590. * 待付款订单
  591. * @param unknown $data
  592. */
  593. public function orderPayment($data)
  594. {
  595. $calculate_data = $this->calculate($data);
  596. foreach ($calculate_data['shop_goods_list'] as $k => $v)
  597. {
  598. //1、查询会员优惠券
  599. $member_coupon_model = new Coupon();
  600. $member_coupon_list_result = $member_coupon_model->getMemberCouponList($data["member_id"], 1, $v["site_id"], $v["goods_money"], "money desc");
  601. $coupon_list = $member_coupon_list_result["data"];
  602. $calculate_data['shop_goods_list'][$k]["coupon_list"] = $coupon_list;
  603. $express_type = [];
  604. if($this->is_virtual == 0){
  605. if(!empty($calculate_data['member_address'])){
  606. //2. 查询店铺配送方式(1. 物流 2. 自提 3. 外卖)
  607. if($v["express_config"]["is_use"] == 1){
  608. $express_type[] = ["title" => Express::express_type["express"]["title"], "name" => "express"];
  609. }
  610. //查询店铺是否开启门店自提
  611. if($v["store_config"]["is_use"] == 1){
  612. $store_model = new Store();
  613. $member_address = $calculate_data["member_address"] ?? [];
  614. $province_id = $member_address["province_id"] ?? 0;
  615. $city_id = $member_address["city_id"] ?? 0;
  616. $store_condition = array(
  617. ["site_id", "=", $v["site_id"]],
  618. ["province_id", "=", $province_id],
  619. ["city_id", "=", $city_id],
  620. ["status", "=", 1],
  621. ["is_pickup", "=", 1],
  622. );
  623. $store_list_result = $store_model->getStoreList($store_condition);
  624. $store_list = $store_list_result["data"];
  625. $express_type[] = ["title" => Express::express_type["store"]["title"], "name" => "store", "store_list" => $store_list];
  626. }
  627. //查询店铺是否开启外卖配送
  628. if($v["local_config"]["is_use"] == 1){
  629. //查询本店的通讯地址
  630. $express_type[] = ["title" => "外卖配送", "name" => "local"];
  631. }
  632. }
  633. }
  634. $calculate_data['shop_goods_list'][$k]["express_type"] = $express_type;
  635. }
  636. return $calculate_data;
  637. }
  638. /**
  639. * 初始化收货地址
  640. * @param unknown $data
  641. */
  642. public function initMemberAddress($data)
  643. {
  644. //收货人地址管理
  645. if(empty($data['member_address']))
  646. {
  647. $member_address = new MemberAddress();
  648. $address = $member_address->getMemberAddressInfo([['member_id', '=', $data['member_id']], ['is_default', '=', 1]]);
  649. $data['member_address'] = $address['data'];
  650. }
  651. return $data;
  652. }
  653. /**
  654. * 获取商品的计算信息
  655. * @param unknown $data
  656. */
  657. public function getOrderGoodsCalculate($data)
  658. {
  659. $shop_goods_list = [];
  660. //传输购物车id组合','隔开要进行拆单
  661. if(!empty($data['cart_ids']))
  662. {
  663. // $cache = Cache::get("order_create_cart_".$data['cart_ids'].'_'.$data['member_id']);
  664. // if(!empty($cache))
  665. // {
  666. // return $cache;
  667. // }
  668. $goods_list = $this->getCartGoodsList($data['cart_ids'], $data['member_id']);
  669. foreach ($goods_list as $k => $shop_goods)
  670. {
  671. $shop_goods['promotion_money'] = 0;
  672. //查询店铺是否存在满减活动
  673. $manjian_info = model("promotion_manjian")->getInfo(['site_id' => $shop_goods['site_id'], "status" => 1], 'rule_json');
  674. $manjian_rule = [];
  675. if(!empty($manjian_info))
  676. {
  677. $manjian_rules = json_decode($manjian_info['rule_json'], true);
  678. $manjian_rules = array_reverse($manjian_rules);
  679. foreach ($manjian_rules as $k => $v)
  680. {
  681. if($shop_goods['goods_money'] >= $v['money'])
  682. {
  683. $shop_goods['promotion_money'] += $v['discount_money'];//优惠金额
  684. $promotion_item = $v;
  685. $promotion_item["content"] = "满".$v['money']."减".$v['discount_money'];
  686. $shop_goods['promotion']['manjian'] = $promotion_item;//前台显示满减规则
  687. break;
  688. }
  689. }
  690. }
  691. $shop_goods_list[$shop_goods['site_id']] = $shop_goods;
  692. }
  693. // Cache::tag("order_create_member_".$data['member_id'])->set("order_create_cart_".$data['cart_ids'].'_'.$data['member_id'], $shop_goods_list, 600);
  694. }else{
  695. // $cache = Cache::get("order_create_".$data['sku_id'].'_'.$data['num'].'_'.$data['member_id']);
  696. // if(!empty($cache))
  697. // {
  698. // return $cache;
  699. // }
  700. $shop_goods = $this->getShopGoodsList($data);
  701. //查询店铺是否存在满减活动
  702. $manjian_info = model("promotion_manjian")->getInfo(['site_id' => $shop_goods['site_id'], "status" => 1], 'rule_json');
  703. $manjian_rule = [];
  704. $shop_goods['promotion_money'] = 0;
  705. if(!empty($manjian_info))
  706. {
  707. $manjian_rules = json_decode($manjian_info['rule_json'], true);
  708. $manjian_rules = array_reverse($manjian_rules);
  709. foreach ($manjian_rules as $k => $v)
  710. {
  711. if($shop_goods['goods_money'] >= $v['money'])
  712. {
  713. $shop_goods['promotion_money'] = $v['discount_money'];//优惠金额
  714. $promotion_item = $v;
  715. $promotion_item["content"] = "满".$v['money']."减".$v['discount_money'];
  716. $shop_goods['promotion']['manjian'] = $promotion_item;//前台显示满减规则
  717. break;
  718. }
  719. }
  720. }
  721. $shop_goods_list[0] = $shop_goods;
  722. // Cache::tag("order_create_member_".$data['member_id'])->set("order_create_".$data['sku_id'].'_'.$data['num'].'_'.$data['member_id'], $shop_goods_list, 600);
  723. }
  724. return $shop_goods_list;
  725. }
  726. /**
  727. * 获取购物车商品列表信息
  728. * @param unknown $cart_ids
  729. */
  730. public function getCartGoodsList($cart_ids, $member_id)
  731. {
  732. //组装商品列表
  733. $field = ' ngc.member_id, ngc.sku_id, ngc.num, ngs.sku_name, ngs.sku_no,
  734. ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,
  735. ngs.site_id, ngs.site_name, ngs.website_id, ngs.is_own, ngs.goods_state, ngs.is_virtual,
  736. ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.commission_rate,ngs.goods_id';
  737. $alias = 'ngc';
  738. $join = [
  739. [
  740. 'goods_sku ngs',
  741. 'ngc.sku_id = ngs.sku_id',
  742. 'inner'
  743. ],
  744. ];
  745. $goods_list = model("goods_cart")->getList([['cart_id', 'in', $cart_ids], ['member_id', '=', $member_id]], $field, '', $alias, $join);
  746. $shop_goods_list = [];
  747. if(!empty($goods_list))
  748. {
  749. foreach ($goods_list as $k => $v)
  750. {
  751. $goods_model = new \app\model\goods\Goods();
  752. $gods_price =$goods_model->getGoodsInfo([['goods_id','=',$v['goods_id']]],'pay_num1,pay_num2,pay_num3,price1,price2,price3,vip_price');
  753. $user_model = new Member();
  754. $user_info = $user_model->getMemberInfo([['member_id','=',$member_id]],'member_type,member_level');
  755. if($user_info['data']['member_type']==2||$user_info['data']['member_level']==1) {
  756. $money=$gods_price['data']['vip_price'];
  757. }
  758. else {
  759. if ($v['num'] >= $gods_price['data']['pay_num1']) {
  760. $money = $gods_price['data']['price1'];
  761. }
  762. if ($v['num'] >= $gods_price['data']['pay_num2']) {
  763. $money = $gods_price['data']['price2'];
  764. }
  765. if ($v['num'] >= $gods_price['data']['pay_num3']) {
  766. $money = $gods_price['data']['price3'];
  767. }
  768. }
  769. $v['discount_price'] =$money;
  770. $site_id = $v['site_id'];
  771. if(isset($shop_goods_list[$site_id]))
  772. {
  773. $shop_goods_list[$site_id]['goods_list'][] = $v;
  774. $shop_goods_list[$site_id]['order_name'] = string_split($shop_goods_list[$site_id]['order_name'], ",", $v['sku_name']);
  775. $shop_goods_list[$site_id]['goods_num'] += $v['num'];
  776. $shop_goods_list[$site_id]['goods_money'] += $v['discount_price'] * $v['num'];
  777. $shop_goods_list[$site_id]['goods_list_str'] = $shop_goods_list[$site_id]['goods_list_str'].';'.$v['sku_id'].':'.$v['num'];
  778. }else{
  779. $shop_goods_list[$site_id]['site_id'] = $site_id;
  780. $shop_goods_list[$site_id]['site_name'] = $v['site_name'];
  781. $shop_goods_list[$site_id]['website_id'] = $v['website_id'];
  782. $shop_goods_list[$site_id]['goods_money'] = $v['discount_price'] * $v['num'];
  783. $shop_goods_list[$site_id]['goods_list_str'] = $v['sku_id'].':'.$v['num'];
  784. $shop_goods_list[$site_id]['order_name'] = string_split("", ",", $v['sku_name']);
  785. $shop_goods_list[$site_id]['goods_num'] = $v['num'];
  786. $shop_goods_list[$site_id]['goods_list'][] = $v;
  787. }
  788. }
  789. }
  790. return $shop_goods_list;
  791. }
  792. /**
  793. * 获取立即购买商品信息
  794. * @param unknown $data
  795. * @return multitype:string number unknown mixed
  796. */
  797. public function getShopGoodsList($data)
  798. {
  799. $sku_info = model("goods_sku")->getInfo([['sku_id', '=', $data['sku_id']]], 'sku_id, sku_name, sku_no, price, discount_price,
  800. cost_price, stock, volume, weight, sku_image, site_id, site_name,
  801. website_id, is_own, goods_state, is_virtual, is_free_shipping, shipping_template,goods_class, goods_class_name, commission_rate, goods_id');
  802. $sku_info['num'] = $data['num'];
  803. $goods_money = $sku_info['discount_price'] * $data['num'];
  804. $goods_list[] = $sku_info;
  805. $shop_goods = [
  806. 'goods_money' => $goods_money,
  807. 'site_id' => $sku_info['site_id'],
  808. 'site_name' => $sku_info['site_name'],
  809. 'website_id' => $sku_info['website_id'],
  810. 'goods_list_str' => $sku_info['sku_id'].':'.$sku_info['num'],
  811. 'goods_list' => $goods_list,
  812. 'order_name' => $sku_info["sku_name"],
  813. 'goods_num' => $sku_info['num'],
  814. ];
  815. return $shop_goods;
  816. }
  817. /**
  818. * 获取店铺订单计算
  819. * @param unknown $site_id 店铺id
  820. * @param unknown $site_name 店铺名称
  821. * @param unknown $goods_money 商品总价
  822. * @param unknown $goods_list 店铺商品列表
  823. * @param unknown $data 传输生成订单数据
  824. */
  825. public function shopOrderCalculate($shop_goods, $data)
  826. {
  827. $site_id = $shop_goods['site_id'];
  828. //定义计算金额
  829. $goods_money = $shop_goods['goods_money']; //商品金额
  830. $delivery_money = 0; //配送费用
  831. $promotion_money = $shop_goods['promotion_money']; //优惠费用(满减)
  832. $coupon_money = 0; //优惠券费用
  833. $adjust_money = 0; //调整金额
  834. $invoice_money = 0; //发票金额
  835. $order_money = 0; //订单金额
  836. $balance_money = 0; //会员余额
  837. $pay_money = 0; //应付金额
  838. //计算邮费
  839. if($this->is_virtual == 1)
  840. {
  841. //虚拟订单 运费为0
  842. $delivery_money = 0;
  843. $shop_goods['delivery']['delivery_type'] = '';
  844. }else{
  845. if(empty($data['member_address']))
  846. {
  847. $delivery_money = 0;
  848. $shop_goods['delivery']['delivery_type'] = 'express';
  849. $this->error = 1;
  850. $this->error_msg = "未配置默认收货地址!";
  851. }else{
  852. //查询店铺是否开启快递配送
  853. $express_config_model = new ExpressConfig();
  854. $express_config_result = $express_config_model->getExpressConfig($site_id);
  855. $express_config = $express_config_result["data"];
  856. $shop_goods["express_config"] = $express_config;
  857. //查询店铺是否开启门店自提
  858. $store_config_result = $express_config_model->getStoreConfig($site_id);
  859. $store_config = $store_config_result["data"];
  860. $shop_goods["store_config"] = $store_config;
  861. //查询店铺是否开启外卖配送
  862. $local_config_result = $express_config_model->getLocalDeliveryConfig($site_id);
  863. $local_config = $local_config_result["data"];
  864. $shop_goods["local_config"] = $local_config;
  865. if(!isset($data['delivery'][$site_id]["delivery_type"]) || $data['delivery'][$site_id]["delivery_type"] == "express"){
  866. if($shop_goods["express_config"]["is_use"] == 1){
  867. //物流配送
  868. $express = new Express();
  869. $express_fee_result = $express->calculate($shop_goods, $data);
  870. if($express_fee_result["code"] < 0){
  871. $this->error = 1;
  872. $this->error_msg = $express_fee_result["message"];
  873. $delivery_fee = 0;
  874. }else{
  875. $delivery_fee = $express_fee_result['data']['delivery_fee'];
  876. }
  877. }else{
  878. $this->error = 1;
  879. $this->error_msg = "物流配送方式未开启!";
  880. $delivery_fee = 0;
  881. }
  882. $delivery_money = $delivery_fee;
  883. $shop_goods['delivery']['delivery_type'] = 'express';
  884. }else if($data['delivery'][$site_id]["delivery_type"] == "store"){
  885. //门店自提
  886. $delivery_money = 0;
  887. $shop_goods['delivery']['delivery_type'] = 'store';
  888. if($shop_goods["store_config"]["is_use"] == 0){
  889. $this->error = 1;
  890. $this->error_msg = "门店自提方式未开启!";
  891. }
  892. if(empty($data['delivery'][$site_id]["store_id"])){
  893. $this->error = 1;
  894. $this->error_msg = "门店未选择!";
  895. }
  896. $shop_goods['delivery']['store_id'] = $data['delivery'][$site_id]["store_id"];
  897. $shop_goods = array_merge($shop_goods,$this->storeOrderData($shop_goods, $data));
  898. }else if($data['delivery'][$site_id]["delivery_type"] == "local"){
  899. //外卖配送
  900. $delivery_money = 0;
  901. $shop_goods['delivery']['delivery_type'] = 'local';
  902. if($shop_goods["local_config"]["is_use"] == 0){
  903. $this->error = 1;
  904. $this->error_msg = "外卖配送方式未开启!";
  905. }
  906. }
  907. }
  908. }
  909. $order_money = $goods_money + $delivery_money - $promotion_money + $invoice_money;
  910. //优惠券活动(采用站点id:coupon_id)
  911. if(!empty($data['coupon'][$site_id]) && $data['coupon'][$site_id]["coupon_id"] > 0)
  912. {
  913. $coupon_info = $data['coupon'][$site_id];
  914. $shop_goods['coupon_id'] = $coupon_info["coupon_id"];
  915. //查询优惠券信息,计算优惠券费用
  916. $coupon = new Coupon();
  917. $coupon_info_result = $coupon->getCouponInfo([['coupon_id', '=', $shop_goods['coupon_id']]], 'member_id,at_least,money,state');
  918. $coupon_info = $coupon_info_result["data"];
  919. if(($coupon_info['at_least'] <= $goods_money-$promotion_money) && ($coupon_info['state'] == 1) && $coupon_info['member_id'] == $data['member_id'])
  920. {
  921. $coupon_money = $coupon_info['money'];
  922. }else{
  923. $this->error = 1;
  924. $this->error_msg = "优惠券不可用!";
  925. }
  926. }
  927. if($coupon_money > $order_money){
  928. $coupon_money = $order_money;
  929. }
  930. $order_money -= $coupon_money;
  931. //买家留言
  932. if(isset($data['buyer_message']) && isset($data['buyer_message'][$site_id])){
  933. $item_buyer_message = $data['buyer_message'][$site_id];
  934. $shop_goods["buyer_message"] = $item_buyer_message;
  935. }else{
  936. $shop_goods["buyer_message"] = '';
  937. }
  938. if($order_money < 0)
  939. {
  940. $order_money = 0;
  941. }
  942. //余额抵扣(判断是否使用余额)
  943. if($this->member_balance_money > 0)
  944. {
  945. if($order_money <= $this->member_balance_money)
  946. {
  947. $balance_money = $order_money;
  948. }else{
  949. $balance_money = $this->member_balance_money;
  950. }
  951. }else{
  952. $balance_money = 0;
  953. }
  954. $pay_money = $order_money - $balance_money;//计算出实际支付金额
  955. //判断是否存在支付金额为0的订单
  956. if($pay_money > 0){
  957. $this->is_exist_not_free = true;
  958. }else{
  959. $this->is_exist_free = true;
  960. }
  961. $this->member_balance_money -= $balance_money;//预减少账户余额
  962. $this->balance_money += $balance_money;//累计余额
  963. //总结计算
  964. $shop_goods['goods_money'] = $goods_money;
  965. $shop_goods['delivery_money'] = $delivery_money;
  966. $shop_goods['coupon_money'] = $coupon_money;
  967. $shop_goods['adjust_money'] = $adjust_money;
  968. $shop_goods['invoice_money'] = $invoice_money;
  969. $shop_goods['promotion_money'] = $promotion_money;
  970. $shop_goods['order_money'] = $order_money;
  971. $shop_goods['balance_money'] = $balance_money;
  972. $shop_goods['pay_money'] = $pay_money;
  973. $this->goods_money += $goods_money;
  974. $this->delivery_money += $delivery_money;
  975. $this->coupon_money += $coupon_money;
  976. $this->adjust_money += $adjust_money;
  977. $this->invoice_money += $invoice_money;
  978. $this->promotion_money += $promotion_money;
  979. $this->order_money += $order_money;
  980. $this->pay_money += $pay_money;
  981. $this->goods_num += $shop_goods["goods_num"];
  982. $this->order_name = string_split($this->order_name, ",", $shop_goods["order_name"]);
  983. return $shop_goods;
  984. }
  985. /**
  986. * 增加订单自动关闭事件
  987. * @param $order_id
  988. */
  989. public function addOrderCronClose($order_id){
  990. //计算订单自动关闭时间
  991. $config_model = new Config();
  992. $order_config_result = $config_model->getOrderEventTimeConfig();
  993. $order_config = $order_config_result["data"];
  994. $now_time = time();
  995. if(!empty($order_config)){
  996. $execute_time = $now_time + $order_config["value"]["auto_close"]*60;//自动关闭时间
  997. }else{
  998. $execute_time = $now_time + 3600;//尚未配置 默认一天
  999. }
  1000. $cron_model = new Cron();
  1001. $cron_model->addCron(1, 0, "订单自动关闭", "CronOrderClose", $execute_time, $order_id);
  1002. }
  1003. /**
  1004. * 验证订单支付金额知否为0 如果为0 立即支付完成
  1005. * @param $order_data
  1006. */
  1007. public function checkFree($order_data){
  1008. if($order_data["pay_money"] == 0){
  1009. // $pay_model = new Pay();
  1010. // $pay_model->onlinePay($order_data["out_trade_no"], "ONLINE_PAY", '', '');
  1011. }
  1012. }
  1013. /**
  1014. * 补齐门店数据
  1015. * @param $data
  1016. */
  1017. public function storeOrderData($shop_goods, $data){
  1018. $temp_data = [];
  1019. $delivery_store_id = $shop_goods['delivery']['store_id'] ?? 0;//门店id
  1020. if($delivery_store_id > 0){
  1021. $member_address = $data["member_address"] ?? [];
  1022. $province_id = $member_address["province_id"] ?? 0;
  1023. $city_id = $member_address["city_id"] ?? 0;
  1024. $store_model = new Store();
  1025. $condition = array(
  1026. ["store_id", "=", $delivery_store_id],
  1027. ["site_id", "=", $shop_goods['site_id']],
  1028. ["province_id", "=", $province_id],
  1029. ["city_id", "=", $city_id],
  1030. ["status", "=", 1],
  1031. ["is_pickup", "=", 1],
  1032. );
  1033. $store_info_result = $store_model->getStoreInfo($condition);
  1034. $store_info = $store_info_result["data"] ?? [];
  1035. if(empty($store_info)) {
  1036. $this->error = 1;
  1037. $this->error_msg = "当前门店不存在或未开启!";
  1038. }else {
  1039. $temp_data["delivery_store_id"] = $delivery_store_id;
  1040. $delivery_store_name = $store_info_result["data"]["store_name"];
  1041. $temp_data["delivery_store_name"] = $delivery_store_name;
  1042. $delivery_store_info = array(
  1043. "open_date" => $store_info["open_date"],
  1044. "full_address" => $store_info["full_address"],
  1045. "longitude" => $store_info["longitude"],
  1046. "latitude" => $store_info["latitude"],
  1047. "telphone" => $store_info["telphone"],
  1048. );
  1049. $temp_data["delivery_store_info"] = json_encode($delivery_store_info, JSON_UNESCAPED_UNICODE);
  1050. }
  1051. }else{
  1052. $this->error = 1;
  1053. $this->error_msg = "配送门店不可为空!";
  1054. }
  1055. return $temp_data;
  1056. }
  1057. /**
  1058. * 使用余额
  1059. * @param $order_data
  1060. * @return array
  1061. */
  1062. public function useBalance($data){
  1063. $this->pay_type = "BALANCE";
  1064. $member_model = new Member();
  1065. $result = $member_model->checkPayPassword($data["member_id"], $data["pay_password"]);
  1066. if($result["code"] >= 0){
  1067. $balance_money = $data["member_account"]["balance_money"];//不可提现余额
  1068. $balance = $data["member_account"]["balance"];//可提现余额
  1069. $member_account_model = new MemberAccount();
  1070. $surplus_banance = $data["balance_money"];
  1071. //优先扣除不可提现余额
  1072. if($balance > 0){
  1073. if($balance >= $surplus_banance){
  1074. $real_balance = $surplus_banance;
  1075. }else{
  1076. $real_balance = $balance;
  1077. }
  1078. $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除不可提现余额:".$real_balance);
  1079. $surplus_banance -= $real_balance;
  1080. }
  1081. // if($balance_money > 0){
  1082. // if($balance_money > $surplus_banance){
  1083. // $real_balance_money = $surplus_banance;
  1084. // }else{
  1085. // $real_balance_money = $balance_money;
  1086. // }
  1087. // $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除不可提现余额:".$real_balance);
  1088. // }
  1089. if($surplus_banance > 0){
  1090. $result = $member_account_model->addMemberAccount($data["member_id"], "balance_money", -$surplus_banance, "order", "余额抵扣","订单余额抵扣,扣除可提现余额:".$surplus_banance);
  1091. }
  1092. return $result;
  1093. }else{
  1094. return $result;
  1095. }
  1096. }
  1097. /**
  1098. * 初始化会员账户
  1099. * @param $data
  1100. * @return mixed
  1101. */
  1102. public function initMemberAccount($data){
  1103. $member_model = new Member();
  1104. $member_info_result = $member_model->getMemberDetail($data["member_id"]);
  1105. $member_info = $member_info_result["data"];
  1106. if(!empty($member_info)){
  1107. if(!empty($member_info["pay_password"])){
  1108. $is_pay_password = 1;
  1109. }else{
  1110. $is_pay_password = 0;
  1111. }
  1112. unset($member_info["pay_password"]);
  1113. $member_info["is_pay_password"] = $is_pay_password;
  1114. $data['member_account'] = $member_info;
  1115. }
  1116. return $data;
  1117. }
  1118. }