TopicOrderCreate.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\topic\model;
  13. use app\model\order\OrderCreate;
  14. use app\model\goods\GoodsStock;
  15. use app\model\store\Store;
  16. use think\facade\Cache;
  17. use app\model\express\Express;
  18. use app\model\system\Pay;
  19. use app\model\express\Config as ExpressConfig;
  20. /**
  21. * 订单创建(专题)
  22. *
  23. * @author Administrator
  24. *
  25. */
  26. class TopicOrderCreate extends OrderCreate
  27. {
  28. private $goods_money = 0;//商品金额
  29. private $balance_money = 0;//余额
  30. private $delivery_money = 0;//配送费用
  31. private $coupon_money = 0;//优惠券金额
  32. private $adjust_money = 0;//调整金额
  33. private $invoice_money = 0;//发票费用
  34. private $promotion_money = 0;//优惠金额
  35. private $order_money = 0;//订单金额
  36. private $pay_money = 0;//支付总价
  37. private $is_virtual = 0; //是否是虚拟类订单
  38. private $order_name = ''; //订单详情
  39. private $goods_num = 0; //商品种数
  40. private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
  41. private $pay_type = 'ONLINE_PAY';//支付方式
  42. private $error = 0; //是否有错误
  43. private $error_msg = ''; //错误描述
  44. /**
  45. * 订单创建
  46. * @param unknown $data
  47. */
  48. public function create($data)
  49. {
  50. //查询出会员相关信息
  51. $calculate_data = $this->calculate($data);
  52. if (isset($calculate_data['code']) && $calculate_data['code'] < 0)
  53. return $calculate_data;
  54. if ($this->error > 0) {
  55. return $this->error("", $this->error_msg);
  56. }
  57. $pay = new Pay();
  58. $out_trade_no = $pay->createOutTradeNo();
  59. model("order")->startTrans();
  60. //循环生成多个订单
  61. try {
  62. $pay_money = 0;
  63. $goods_stock_model = new GoodsStock();
  64. $shop_goods_list = $calculate_data['shop_goods_list'];
  65. $item_delivery = $shop_goods_list['delivery'] ?? [];
  66. $delivery_type = $item_delivery['delivery_type'] ?? '';
  67. $delivery_type_name = Express::express_type[ $delivery_type ]["title"] ?? '';
  68. //订单主表
  69. $order_type = $this->orderType($shop_goods_list, $calculate_data);
  70. $order_no = $this->createOrderNo($shop_goods_list['site_id']);
  71. $data_order = [
  72. 'order_no' => $order_no,
  73. 'site_id' => $shop_goods_list['site_id'],
  74. 'site_name' => $shop_goods_list['site_name'],
  75. 'order_from' => $data['order_from'],
  76. 'order_from_name' => $data['order_from_name'],
  77. 'order_type' => $order_type['order_type_id'],
  78. 'order_type_name' => $order_type['order_type_name'],
  79. 'order_status_name' => $order_type['order_status']['name'],
  80. 'order_status_action' => json_encode($order_type['order_status'], JSON_UNESCAPED_UNICODE),
  81. 'out_trade_no' => $out_trade_no,
  82. 'member_id' => $data['member_id'],
  83. 'name' => $calculate_data['member_address']['name'] ?? '',
  84. 'mobile' => $calculate_data['member_address']['mobile'] ?? '',
  85. 'telephone' => $calculate_data['member_address']['telephone'] ?? '',
  86. 'province_id' => $calculate_data['member_address']['province_id'] ?? '',
  87. 'city_id' => $calculate_data['member_address']['city_id'] ?? '',
  88. 'district_id' => $calculate_data['member_address']['district_id'] ?? '',
  89. 'community_id' => $calculate_data['member_address']['community_id'] ?? '',
  90. 'address' => $calculate_data['member_address']['address'] ?? '',
  91. 'full_address' => $calculate_data['member_address']['full_address'] ?? '',
  92. 'longitude' => $calculate_data['member_address']['longitude'] ?? '',
  93. 'latitude' => $calculate_data['member_address']['latitude'] ?? '',
  94. 'buyer_ip' => request()->ip(),
  95. 'goods_money' => $shop_goods_list['goods_money'],
  96. 'delivery_money' => $shop_goods_list['delivery_money'],
  97. 'coupon_id' => isset($shop_goods_list['coupon_id']) ? $shop_goods_list['coupon_id'] : 0,
  98. 'coupon_money' => $shop_goods_list['coupon_money'],
  99. 'adjust_money' => $shop_goods_list['adjust_money'],
  100. 'invoice_money' => $shop_goods_list['invoice_money'],
  101. 'promotion_money' => $shop_goods_list['promotion_money'],
  102. 'order_money' => $shop_goods_list['order_money'],
  103. 'balance_money' => $shop_goods_list['balance_money'],
  104. 'pay_money' => $shop_goods_list['pay_money'],
  105. 'create_time' => time(),
  106. 'is_enable_refund' => 0,
  107. 'order_name' => $shop_goods_list["order_name"],
  108. 'goods_num' => $shop_goods_list['goods_num'],
  109. 'delivery_type' => $delivery_type,
  110. 'delivery_type_name' => $delivery_type_name,
  111. 'delivery_store_id' => $shop_goods_list["delivery_store_id"] ?? 0,
  112. "delivery_store_name" => $shop_goods_list["delivery_store_name"] ?? '',
  113. "delivery_store_info" => $shop_goods_list["delivery_store_info"] ?? '',
  114. "buyer_message" => $data["buyer_message"],
  115. "promotion_type" => "topic",
  116. "promotion_type_name" => "专题",
  117. "promotion_status_name" => "",
  118. "website_id" => $shop_goods_list["website_id"]
  119. ];
  120. $order_id = model("order")->add($data_order);
  121. $pay_money += $shop_goods_list['pay_money'];
  122. //订单项目表
  123. foreach ($shop_goods_list['goods_list'] as $k_order_goods => $order_goods) {
  124. $data_order_goods = array(
  125. 'order_id' => $order_id,
  126. 'site_id' => $shop_goods_list['site_id'],
  127. 'site_name' => $shop_goods_list['site_name'],
  128. 'order_no' => $order_no,
  129. 'member_id' => $data['member_id'],
  130. 'sku_id' => $order_goods['sku_id'],
  131. 'sku_name' => $order_goods['sku_name'],
  132. 'sku_image' => $order_goods['sku_image'],
  133. 'sku_no' => $order_goods['sku_no'],
  134. 'is_virtual' => $order_goods['is_virtual'],
  135. 'goods_class' => $order_goods['goods_class'],
  136. 'goods_class_name' => $order_goods['goods_class_name'],
  137. 'price' => $order_goods['discount_price'],
  138. 'cost_price' => $order_goods['cost_price'],
  139. 'num' => $order_goods['num'],
  140. 'goods_money' => $order_goods['discount_price'] * $order_goods['num'],
  141. 'cost_money' => $order_goods['cost_price'] * $order_goods['num'],
  142. 'commission_rate' => $order_goods['commission_rate'],
  143. 'goods_id' => $order_goods['goods_id'],
  144. 'delivery_status' => 0,
  145. 'delivery_status_name' => "未发货",
  146. );
  147. model("order_goods")->add($data_order_goods);
  148. //库存变化
  149. $stock_result = $goods_stock_model->decStock([ "sku_id" => $order_goods['sku_id'], "num" => $order_goods['num'] ]);
  150. if ($stock_result["code"] != 0) {
  151. model("order")->rollback();
  152. return $stock_result;
  153. }
  154. // model("goods_sku")->setDec([['sku_id', '=', $order_goods['sku_id']]], 'stock', $order_goods['num']);
  155. // $sku_stock = model("goods_sku")->getInfo([['sku_id', '=', $order_goods['sku_id']]], 'stock');
  156. // if($sku_stock['stock'] < 0)
  157. // {
  158. // model("order")->rollback();
  159. // return $this->error('', "库存不足");
  160. // }
  161. }
  162. //扣除余额(统一扣除)
  163. if($calculate_data["balance_money"] > 0){
  164. $balance_result = $this->useBalance($calculate_data);
  165. if($balance_result["code"] < 0){
  166. model("order")->rollback();
  167. return $balance_result;
  168. }
  169. }
  170. //生成整体支付单据
  171. $pay->addPay(0, $out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'OrderPayNotify', '');
  172. $this->addOrderCronClose($order_id);//增加关闭订单自动事件
  173. Cache::tag("order_create_topic_" . $data['member_id'])->clear();
  174. $result_list = event("OrderCreate", [ 'order_id' => $order_id ]);
  175. if(!empty($result_list)){
  176. foreach($result_list as $k => $v){
  177. if(!empty($v) && $v["code"] < 0){
  178. model("order")->rollback();
  179. return $v;
  180. }
  181. }
  182. }
  183. model("order")->commit();
  184. return $this->success($out_trade_no);
  185. } catch (\Exception $e) {
  186. model("order")->rollback();
  187. return $this->error('', $e->getMessage());
  188. }
  189. }
  190. /**
  191. * 订单计算
  192. * @param unknown $data
  193. */
  194. public function calculate($data)
  195. {
  196. $data = $this->initMemberAddress($data);
  197. $data = $this->initMemberAccount($data);//初始化会员账户
  198. //余额付款
  199. if($data['is_balance'] > 0)
  200. {
  201. $this->member_balance_money = $data["member_account"]["balance"] ?? 0;
  202. }
  203. //商品列表信息
  204. $shop_goods_list = $this->getOrderGoodsCalculate($data);
  205. //查询专题信息
  206. $topic_model = new Topic();
  207. $topic_id = $shop_goods_list["goods_list"][0]["topic_id"];
  208. $topic_info_result = $topic_model->getTopicInfo([ [ "topic_id", "=", $topic_id ] ]);
  209. $topic_info = $topic_info_result["data"];
  210. if (empty($topic_info)) {
  211. return $this->error([], "找不到可用的专题活动!");
  212. }
  213. $data["topic_info"] = $topic_info;
  214. //判断时间段是否符合
  215. $time = time();//当日时间戳
  216. if (empty($topic_info) || $topic_info["status"] != 1 || ($time < $topic_info["start_time"] || $time > $topic_info["end_time"])) {
  217. $this->error = 1;
  218. $this->error_msg = "当前商品专题活动未开启!";
  219. }
  220. $data['shop_goods_list'] = $this->shopOrderCalculate($shop_goods_list, $data);
  221. //总结计算
  222. $data['delivery_money'] = $this->delivery_money;
  223. $data['coupon_money'] = $this->coupon_money;
  224. $data['adjust_money'] = $this->adjust_money;
  225. $data['invoice_money'] = $this->invoice_money;
  226. $data['promotion_money'] = $this->promotion_money;
  227. $data['order_money'] = $this->order_money;
  228. $data['balance_money'] = $this->balance_money;
  229. $data['pay_money'] = $this->pay_money;
  230. $data['goods_money'] = $this->goods_money;
  231. $data['goods_num'] = $this->goods_num;
  232. $data['is_virtual'] = $this->is_virtual;
  233. return $data;
  234. }
  235. /**
  236. * 待付款订单
  237. * @param unknown $data
  238. */
  239. public function orderPayment($data)
  240. {
  241. $calculate_data = $this->calculate($data);
  242. if (isset($calculate_data['code']) && $calculate_data['code'] < 0)
  243. return $calculate_data;
  244. $shop_goods_list = $calculate_data['shop_goods_list'];
  245. $calculate_data['shop_goods_list']["coupon_list"] = [];
  246. $express_type = [];
  247. if ($this->is_virtual == 0) {
  248. if (!empty($calculate_data['member_address'])) {
  249. //2. 查询店铺配送方式(1. 物流 2. 自提 3. 外卖)
  250. if ($shop_goods_list["express_config"]["is_use"] == 1) {
  251. $express_type[] = [ "title" => Express::express_type["express"]["title"], "name" => "express" ];
  252. }
  253. //查询店铺是否开启门店自提
  254. if ($shop_goods_list["store_config"]["is_use"] == 1) {
  255. $store_model = new Store();
  256. $member_address = $calculate_data["member_address"] ?? [];
  257. $province_id = $member_address["province_id"] ?? 0;
  258. $city_id = $member_address["city_id"] ?? 0;
  259. // $district_id = $member_address["district_id"] ?? 0;
  260. $store_condition = array(
  261. [ "site_id", "=", $shop_goods_list["site_id"] ],
  262. [ "province_id", "=", $province_id ],
  263. [ "city_id", "=", $city_id ],
  264. [ "status", "=", 1 ],
  265. [ "is_pickup", "=", 1 ],
  266. // ["district_id", "=", $district_id],
  267. );
  268. $store_list_result = $store_model->getStoreList($store_condition);
  269. $store_list = $store_list_result["data"];
  270. $express_type[] = [ "title" => Express::express_type["store"]["title"], "name" => "store", "store_list" => $store_list ];
  271. }
  272. //查询店铺是否开启外卖配送
  273. if ($shop_goods_list["local_config"]["is_use"] == 1) {
  274. //查询本店的通讯地址
  275. $express_type[] = [ "title" => "外卖配送", "name" => "local" ];
  276. }
  277. }
  278. }
  279. $calculate_data['shop_goods_list']["express_type"] = $express_type;
  280. return $this->success($calculate_data);
  281. }
  282. /**
  283. * 获取商品的计算信息
  284. * @param unknown $data
  285. */
  286. public function getOrderGoodsCalculate($data)
  287. {
  288. $shop_goods_list = [];
  289. //通过专题id查询 商品数据
  290. // $cache = Cache::get("order_create_topic_".$data['id'].'_'.$data['num'].'_'.$data['member_id']);
  291. // if(!empty($cache))
  292. // {
  293. // return $cache;
  294. // }
  295. $goods_list = $this->getTopicGoodsInfo($data["id"], $data['num']);
  296. $goods_list['promotion_money'] = 0;
  297. $shop_goods_list = $goods_list;
  298. // Cache::tag("order_create_topic_".$data['member_id'])->set("order_create_topic_".$data['id'].'_'.$data['num'].'_'.$data['member_id'], $shop_goods_list, 600);
  299. return $shop_goods_list;
  300. }
  301. /**
  302. * 获取专题商品列表信息
  303. * @param unknown $bl_id
  304. */
  305. public function getTopicGoodsInfo($id, $num)
  306. {
  307. //组装商品列表
  308. $field = 'ptg.sku_id,ptg.id,ptg.topic_id,ptg.topic_price,ngs.sku_name, ngs.sku_no,
  309. ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,
  310. ngs.site_id, ngs.site_name, ngs.website_id, ngs.is_own, ngs.goods_state, ngs.is_virtual,
  311. ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.commission_rate,ngs.goods_id';
  312. $alias = 'ptg';
  313. $join = [
  314. [
  315. 'goods_sku ngs',
  316. 'ptg.sku_id = ngs.sku_id',
  317. 'inner'
  318. ],
  319. ];
  320. $info = model("promotion_topic_goods")->getInfo([ [ 'ptg.id', '=', $id ] ], $field, $alias, $join);
  321. $shop_goods_list = [];
  322. if (!empty($info)) {
  323. //判断是否是虚拟订单
  324. if ($info['is_virtual']) {
  325. $this->is_virtual = 1;
  326. } else {
  327. $this->is_virtual = 0;
  328. }
  329. $info["num"] = $num;
  330. $site_id = $info['site_id'];
  331. $info["discount_price"] = $info["topic_price"];
  332. $shop_goods_list['site_id'] = $site_id;
  333. $shop_goods_list['site_name'] = $info['site_name'];
  334. $shop_goods_list['goods_money'] = $info['discount_price'] * $info['num'];
  335. $shop_goods_list['goods_list_str'] = $info['sku_id'] . ':' . $info['num'];
  336. $shop_goods_list['order_name'] = string_split("", ",", $info['sku_name']);
  337. $shop_goods_list['goods_num'] = $info['num'];
  338. $shop_goods_list['goods_list'][] = $info;
  339. $shop_goods_list['website_id'] = $info['website_id'];
  340. }
  341. return $shop_goods_list;
  342. }
  343. /**
  344. * 获取店铺订单计算
  345. * @param unknown $site_id 店铺id
  346. * @param unknown $site_name 店铺名称
  347. * @param unknown $goods_money 商品总价
  348. * @param unknown $goods_list 店铺商品列表
  349. * @param unknown $data 传输生成订单数据
  350. */
  351. public function shopOrderCalculate($shop_goods, $data)
  352. {
  353. $site_id = $shop_goods['site_id'];
  354. //定义计算金额
  355. $goods_money = $shop_goods['goods_money']; //商品金额
  356. $delivery_money = 0; //配送费用
  357. $promotion_money = $shop_goods['promotion_money']; //优惠费用(满减)
  358. $coupon_money = 0; //优惠券费用
  359. $adjust_money = 0; //调整金额
  360. $invoice_money = 0; //发票金额
  361. $order_money = 0; //订单金额
  362. $balance_money = 0; //会员余额
  363. $pay_money = 0; //应付金额
  364. //计算邮费
  365. if ($this->is_virtual == 1) {
  366. //虚拟订单 运费为0
  367. $delivery_money = 0;
  368. $shop_goods['delivery']['delivery_type'] = '';
  369. } else {
  370. //计算邮费
  371. if (empty($data['member_address'])) {
  372. $delivery_money = 0;
  373. $shop_goods['delivery']['delivery_type'] = 'express';
  374. $this->error = 1;
  375. $this->error_msg = "未配置默认收货地址!";
  376. } else {
  377. //查询店铺是否开启快递配送
  378. $express_config_model = new ExpressConfig();
  379. $express_config_result = $express_config_model->getExpressConfig($site_id);
  380. $express_config = $express_config_result["data"];
  381. $shop_goods["express_config"] = $express_config;
  382. //查询店铺是否开启门店自提
  383. $store_config_result = $express_config_model->getStoreConfig($site_id);
  384. $store_config = $store_config_result["data"];
  385. $shop_goods["store_config"] = $store_config;
  386. //查询店铺是否开启外卖配送
  387. $local_config_result = $express_config_model->getLocalDeliveryConfig($site_id);
  388. $local_config = $local_config_result["data"];
  389. $shop_goods["local_config"] = $local_config;
  390. if (!isset($data['delivery']["delivery_type"]) || $data['delivery']["delivery_type"] == "express") {
  391. if ($shop_goods["express_config"]["is_use"] == 1) {
  392. //物流配送
  393. $express = new Express();
  394. $express_fee_result = $express->calculate($shop_goods, $data);
  395. if ($express_fee_result["code"] < 0) {
  396. $this->error = 1;
  397. $this->error_msg = $express_fee_result["message"];
  398. $delivery_fee = 0;
  399. } else {
  400. $delivery_fee = $express_fee_result['data']['delivery_fee'];
  401. }
  402. } else {
  403. $this->error = 1;
  404. $this->error_msg = "物流配送方式未开启!";
  405. $delivery_fee = 0;
  406. }
  407. $delivery_money = $delivery_fee;
  408. $shop_goods['delivery']['delivery_type'] = 'express';
  409. } else if ($data['delivery']["delivery_type"] == "store") {
  410. //门店自提
  411. $delivery_money = 0;
  412. $shop_goods['delivery']['delivery_type'] = 'store';
  413. if ($shop_goods["store_config"]["is_use"] == 0) {
  414. $this->error = 1;
  415. $this->error_msg = "门店自提方式未开启!";
  416. }
  417. if (empty($data['delivery']["store_id"])) {
  418. $this->error = 1;
  419. $this->error_msg = "门店未选择!";
  420. }
  421. $shop_goods['delivery']['store_id'] = $data['delivery']["store_id"];
  422. $shop_goods = array_merge($shop_goods, $this->storeOrderData($shop_goods, $data));
  423. } else if ($data['delivery']["delivery_type"] == "local") {
  424. //外卖配送
  425. $delivery_money = 0;
  426. $shop_goods['delivery']['delivery_type'] = 'local';
  427. if ($shop_goods["local_config"]["is_use"] == 0) {
  428. $this->error = 1;
  429. $this->error_msg = "外卖配送方式未开启!";
  430. }
  431. }
  432. }
  433. }
  434. $order_money = $goods_money + $delivery_money - $promotion_money - $coupon_money - $invoice_money;
  435. if ($order_money < 0) {
  436. $order_money = 0;
  437. }
  438. //余额抵扣(判断是否使用余额)
  439. if($this->member_balance_money > 0)
  440. {
  441. if($order_money <= $this->member_balance_money)
  442. {
  443. $balance_money = $order_money;
  444. }else{
  445. $balance_money = $this->member_balance_money;
  446. }
  447. }else{
  448. $balance_money = 0;
  449. }
  450. $pay_money = $order_money - $balance_money;//计算出实际支付金额
  451. $this->member_balance_money -= $balance_money;//预减少账户余额
  452. $this->balance_money += $balance_money;//累计余额
  453. //总结计算
  454. $shop_goods['goods_money'] = $goods_money;
  455. $shop_goods['delivery_money'] = $delivery_money;
  456. $shop_goods['coupon_money'] = $coupon_money;
  457. $shop_goods['adjust_money'] = $adjust_money;
  458. $shop_goods['invoice_money'] = $invoice_money;
  459. $shop_goods['promotion_money'] = $promotion_money;
  460. $shop_goods['order_money'] = $order_money;
  461. $shop_goods['balance_money'] = $balance_money;
  462. $shop_goods['pay_money'] = $pay_money;
  463. $this->goods_money += $goods_money;
  464. $this->delivery_money += $delivery_money;
  465. $this->coupon_money += $coupon_money;
  466. $this->adjust_money += $adjust_money;
  467. $this->invoice_money += $invoice_money;
  468. $this->promotion_money += $promotion_money;
  469. $this->order_money += $order_money;
  470. $this->pay_money += $pay_money;
  471. $this->goods_num += $shop_goods["goods_num"];
  472. $this->order_name = string_split($this->order_name, ",", $shop_goods["order_name"]);
  473. return $shop_goods;
  474. }
  475. }