OrderV2Model.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Model;
  15. use Think\Model;
  16. /**
  17. * @author yj
  18. * @desc 重构前端订单模型
  19. * Class OrderV2Model
  20. * @package Home\Model
  21. */
  22. class OrderV2Model {
  23. /**
  24. * @author yj
  25. * @desc 购物车结算 提交, 余额支付分拆出来方法
  26. * @param $order_all_id
  27. * @param $order
  28. * @param $pay_total
  29. * @param $ck_yupay
  30. * @param $buy_type
  31. * @param $is_integral
  32. * @param $is_spike
  33. * @param $is_just_1
  34. */
  35. public function carOrderYuerPay( $order_all_id, $order ,$pay_total , $ck_yupay ,$buy_type, $is_integral, $is_spike , $is_just_1 )
  36. {
  37. $member_id = $order['member_id'];
  38. $oid = $order['order_id'];
  39. $member_info = M('lionfish_comshop_member')->field('we_openid,account_money')->where( array('member_id' => $member_id ) )->find();
  40. $is_open_yinpay = D('Home/Front')->get_config_by_name('is_open_yinpay');
  41. if($ck_yupay == 1 && $pay_total >0 && $order['type'] != 'ignore')
  42. {
  43. //开始余额支付
  44. $member_charge_flow_data = array();
  45. $member_charge_flow_data['formid'] = '';
  46. $member_charge_flow_data['member_id'] = $member_id;
  47. $member_charge_flow_data['trans_id'] = $oid;
  48. $member_charge_flow_data['money'] = $pay_total;
  49. $member_charge_flow_data['state'] = 3;
  50. $member_charge_flow_data['charge_time'] = time();
  51. $member_charge_flow_data['remark'] = '会员前台余额支付';
  52. $member_charge_flow_data['add_time'] = time();
  53. M('lionfish_comshop_member_charge_flow')->add($member_charge_flow_data);
  54. $charge_flow_id = M('lionfish_comshop_member_charge_flow')->getLastInsID();
  55. //开始处理扣钱
  56. M('lionfish_comshop_member')->where( array('member_id' => $member_id) )->setInc('account_money',-$pay_total);
  57. $mb_info = M('lionfish_comshop_member')->field('account_money')->where( array('member_id' =>$member_id ) )->find();
  58. M('lionfish_comshop_member_charge_flow')->where( array('id' => $charge_flow_id ) )->save( array('operate_end_yuer' => $mb_info['account_money']) );
  59. }
  60. $order_status_id = $order['is_pin'] == 1 ? 2:1;
  61. if( $buy_type == 'presale' )
  62. {
  63. //预售
  64. $order_status_id = 15;
  65. }else if( $buy_type == 'virtualcard' )
  66. {
  67. $order_status_id = 4;
  68. }
  69. //lionfish_comshop_order_all can_yupay
  70. //开始处理订单状态
  71. //$order_all = M('order_all')->where( array('id' => $order_all_id) )->find();
  72. $order_all = M('lionfish_comshop_order_all')->where( array('id' => $order_all_id) )->find();
  73. $out_trade_no = $order_all_id.'-'.time();
  74. if( !empty($order) )
  75. {
  76. D('Home/Pin')->insertNotifyOrder($order['order_id']);
  77. //支付完成
  78. $o = array();
  79. $o['order_status_id'] = $order_status_id;
  80. $o['paytime']=time();
  81. $o['transaction_id'] = 0;
  82. M('lionfish_comshop_order_all')->where( array('id' => $out_trade_no) )->save($o);
  83. $order_relate_list = M('lionfish_comshop_order_relate')->where( array('order_all_id' => $order_all['id']) )->select();
  84. foreach($order_relate_list as $order_relate)
  85. {
  86. $order = M('lionfish_comshop_order')->where( array('order_id' => $order_relate['order_id'] ) )->find();
  87. if( $order && $order['order_status_id'] == 3)
  88. {
  89. $o = array();
  90. $o['payment_code'] = 'yuer';
  91. $o['order_id']=$order['order_id'];
  92. $o['order_status_id'] = $order_status_id;
  93. $o['date_modified']=time();
  94. $o['pay_time']=time();
  95. $o['transaction_id'] = $is_integral ==1? '积分兑换':'余额支付';
  96. if($order['delivery'] == 'hexiao' && $buy_type != 'presale' ){//核销订单 支付完成状态改成 已发货待收货
  97. $o['order_status_id'] = 4;
  98. }
  99. M('lionfish_comshop_order')->where( array('order_id' => $order['order_id'] ) )->save($o);
  100. //暂时屏蔽
  101. $kucun_method = D('Home/Front')->get_config_by_name('kucun_method');
  102. if( empty($kucun_method) )
  103. {
  104. $kucun_method = 0;
  105. }
  106. if($kucun_method == 1)
  107. {//支付完减库存,增加销量
  108. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->select();
  109. foreach($order_goods_list as $order_goods)
  110. {
  111. D('Home/Pingoods')->del_goods_mult_option_quantity($order['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],1);
  112. }
  113. }
  114. $oh = array();
  115. $oh['order_id']=$order['order_id'];
  116. $oh['order_status_id'] = $order_status_id;
  117. $oh['comment']='买家已付款';
  118. $oh['date_added']=time();
  119. $oh['notify']=1;
  120. //邀新有礼下单完成领取礼包
  121. D('Home/Invitegift')->collectInvitegiftAfterOrder($order, 'orderpay');
  122. if(($order['delivery'] == 'hexiao' || $buy_type == 'virtualcard') && $buy_type != 'presale'){//核销订单 支付完成状态改成 已发货待收货 排除预售
  123. $oh['order_status_id'] = 4;
  124. }
  125. M('lionfish_comshop_order_history')->add($oh);
  126. //订单自动配送
  127. if($buy_type != 'presale')
  128. D('Home/Order')->order_auto_delivery($order);
  129. //发送购买通知
  130. //TODO 先屏蔽,等待调试这个消息
  131. D('Home/Weixinnotify')->orderBuy($order['order_id'], true);
  132. if($order['is_pin'] == 1)
  133. {
  134. $pin_order = M('lionfish_comshop_pin_order')->where( array('order_id' =>$order['order_id'] ) )->find();
  135. //D('Home/Pin')->insertNotifyOrder($order['order_id']);
  136. $pin_info = M('lionfish_comshop_pin')->where(array('pin_id' => $pin_order['pin_id']) )->find();//加锁查询
  137. $pin_buy_count = D('Home/Pin')->get_tuan_buy_count($pin_order['pin_id']);
  138. $res = $is_can_add = D('Seller/Redisorder')->add_pintuan_user( $pin_order['pin_id'] );
  139. if( $pin_info['state'] == 1 && !$res )
  140. {
  141. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->find();
  142. M('lionfish_comshop_pin_order')->where( array('pin_id' =>$pin_order['pin_id'],'order_id' => $order['order_id'] ) )->delete();
  143. $newpin_id = D('Home/Pin')->openNewTuan($order['order_id'],$order_goods_info['goods_id'],$order['member_id']);
  144. //插入拼团订单
  145. D('Home/Pin')->insertTuanOrder($newpin_id,$order['order_id']);
  146. unset($pin_info);
  147. $is_pin_success = D('Home/Pin')->checkPinSuccess($newpin_id);
  148. if($is_pin_success) {
  149. D('Home/Pin')->updatePintuanSuccess($newpin_id);
  150. }
  151. }else{
  152. $is_pin_success = D('Home/Pin')->checkPinSuccess($pin_order['pin_id']);
  153. if($is_pin_success) {
  154. D('Home/Pin')->updatePintuanSuccess($pin_order['pin_id']);
  155. }
  156. }
  157. }
  158. //检测预售
  159. if( $buy_type == 'presale' )
  160. {
  161. D('Home/PresaleGoods')->payBackOrder( $order['order_id'] , 0 );
  162. }
  163. //检测礼品卡
  164. if( $buy_type == 'virtualcard' )
  165. {
  166. D('Seller/VirtualCard')->payBackOrder( $order['order_id'] );
  167. }
  168. }
  169. }
  170. //返回支付成功给app
  171. $data = array();
  172. $data['code'] = 0;
  173. $data['has_yupay'] = 1;
  174. $data['is_integral'] = $is_integral;
  175. $data['is_spike'] = $is_spike;
  176. $data['is_go_orderlist'] = $is_just_1;
  177. $data['order_id'] = $oid;
  178. $data['order_all_id'] = $order_all_id;
  179. echo json_encode($data);
  180. die();
  181. }
  182. }
  183. /**
  184. * @author yj
  185. * @desc 取消预售过期订单, 但是不退款 不退库存
  186. * @param $order_ids_arr
  187. */
  188. public function canclePresaleOverOrder($order_ids_arr)
  189. {
  190. // 启动事务
  191. M()->startTrans();
  192. $kucun_method = D('Home/Front')->get_config_by_name('kucun_method');
  193. if( empty($kucun_method) )
  194. {
  195. $kucun_method = 0;
  196. }
  197. foreach( $order_ids_arr as $order_id )
  198. {
  199. //设置订单状态
  200. $up_order_data = array();
  201. $up_order_data['order_status_id'] = 5;
  202. $result = M('lionfish_comshop_order')->where( array('order_id' => $order_id, 'order_status_id' => 15 ) )->lock(true)->save( $up_order_data );
  203. if( $result )
  204. {
  205. //写人订单历史
  206. $order_history = array();
  207. $order_history['order_id'] = $order_id;
  208. $order_history['order_status_id'] = 5;
  209. $order_history['notify'] = 0;
  210. $order_history['comment'] = '未在规定时间内支付尾款,订单取消';
  211. $order_history['date_added']=time();
  212. M('lionfish_comshop_order_history')->add($order_history);
  213. //订单商品
  214. $goods = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_id) )->select();
  215. if(isset($goods) && $kucun_method == 0 ){
  216. foreach ($goods as $key => $value) {
  217. D('Home/Pingoods')->del_goods_mult_option_quantity($order_id,$value['rela_goodsoption_valueid'],$value['goods_id'],$value['quantity'],2);
  218. }
  219. }
  220. }
  221. }
  222. // 提交事务
  223. M()->commit();
  224. }
  225. /**
  226. * @author yj
  227. * @desc 预售核销订单--发货后变更一次核销时间---应用于购买后几日发货的情况
  228. * @param $order_goods_id
  229. * @param $goods_id
  230. */
  231. public function modifyOrderGoodsHexiaoTime( $order_id ,$goods_id , $begin_time )
  232. {
  233. $goods_salesroombase = M('lionfish_comshop_goods_salesroombase')->where(array('goods_id'=>$goods_id))->find();
  234. $hx_data = [];
  235. $hx_data['effect_begin_time'] = $begin_time; //核销有效时间开始
  236. if($goods_salesroombase['hx_expire_type'] == 0){
  237. $hx_data['effect_end_time'] = $begin_time+$goods_salesroombase['hx_expire_day']*24*60*60;
  238. }
  239. M('lionfish_comshop_order_goods_saleshexiao')->where(['order_id' => $order_id , 'goods_id' => $goods_id ])->save($hx_data);
  240. }
  241. /**
  242. * @author cy 2021-04-27
  243. * @desc 购物车结算 提交, 货到付款订单结算方法
  244. * @param $order_all_id
  245. * @param $order
  246. * @param $pay_total
  247. * @param $cashon_delivery
  248. * @param $buy_type
  249. * @param $is_spike
  250. * @param $is_just_1
  251. */
  252. public function carOrderCashonPay( $order_all_id, $order ,$pay_total , $cashon_delivery ,$buy_type, $is_spike , $is_just_1 )
  253. {
  254. $member_id = $order['member_id'];
  255. $oid = $order['order_id'];
  256. $member_info = M('lionfish_comshop_member')->field('we_openid,account_money')->where( array('member_id' => $member_id ) )->find();
  257. $order_status_id = 1;
  258. //开始处理订单状态
  259. $order_all = M('lionfish_comshop_order_all')->where( array('id' => $order_all_id) )->find();
  260. $out_trade_no = $order_all_id.'-'.time();
  261. if( !empty($order) )
  262. {
  263. //支付完成
  264. $o = array();
  265. $o['order_status_id'] = $order_status_id;
  266. $o['paytime']=time();
  267. $o['transaction_id'] = 0;
  268. M('lionfish_comshop_order_all')->where( array('id' => $out_trade_no) )->save($o);
  269. $order_relate_list = M('lionfish_comshop_order_relate')->where( array('order_all_id' => $order_all['id']) )->select();
  270. foreach($order_relate_list as $order_relate)
  271. {
  272. $order = M('lionfish_comshop_order')->where( array('order_id' => $order_relate['order_id'] ) )->find();
  273. if( $order && $order['order_status_id'] == 3)
  274. {
  275. $o = array();
  276. $o['payment_code'] = 'cashon_delivery';
  277. $o['order_id'] = $order['order_id'];
  278. $o['order_status_id'] = $order_status_id;
  279. $o['date_modified'] = time();
  280. $o['pay_time'] = time();
  281. $o['transaction_id'] = '货到付款';
  282. if($order['delivery'] == 'hexiao' && $buy_type != 'presale' ){//核销订单 支付完成状态改成 已发货待收货
  283. $o['order_status_id'] = 4;
  284. }
  285. M('lionfish_comshop_order')->where( array('order_id' => $order['order_id'] ) )->save($o);
  286. //暂时屏蔽
  287. $kucun_method = D('Home/Front')->get_config_by_name('kucun_method');
  288. if( empty($kucun_method) )
  289. {
  290. $kucun_method = 0;
  291. }
  292. if($kucun_method == 1)
  293. {//支付完减库存,增加销量
  294. $order_goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->select();
  295. foreach($order_goods_list as $order_goods)
  296. {
  297. D('Home/Pingoods')->del_goods_mult_option_quantity($order['order_id'],$order_goods['rela_goodsoption_valueid'],$order_goods['goods_id'],$order_goods['quantity'],1);
  298. }
  299. }
  300. $oh = array();
  301. $oh['order_id'] = $order['order_id'];
  302. $oh['order_status_id'] = $order_status_id;
  303. $oh['comment'] = '买家已付款';
  304. $oh['date_added'] = time();
  305. $oh['notify'] = 1;
  306. //邀新有礼下单完成领取礼包
  307. D('Home/Invitegift')->collectInvitegiftAfterOrder($order, 'orderpay');
  308. if(($order['delivery'] == 'hexiao' || $buy_type == 'virtualcard') && $buy_type != 'presale'){//核销订单 支付完成状态改成 已发货待收货 排除预售
  309. $oh['order_status_id'] = 4;
  310. }
  311. M('lionfish_comshop_order_history')->add($oh);
  312. //订单自动配送
  313. if($buy_type != 'presale')
  314. D('Home/Order')->order_auto_delivery($order);
  315. //发送购买通知
  316. //TODO 先屏蔽,等待调试这个消息
  317. D('Home/Weixinnotify')->orderBuy($order['order_id'], true);
  318. /**if($order['is_pin'] == 1)
  319. {
  320. $pin_order = M('lionfish_comshop_pin_order')->where( array('order_id' =>$order['order_id'] ) )->find();
  321. D('Home/Pin')->insertNotifyOrder($order['order_id']);
  322. $pin_info = M('lionfish_comshop_pin')->where(array('pin_id' => $pin_order['pin_id']) )->find();//加锁查询
  323. $pin_buy_count = D('Home/Pin')->get_tuan_buy_count($pin_order['pin_id']);
  324. $res = $is_can_add = D('Seller/Redisorder')->add_pintuan_user( $pin_order['pin_id'] );
  325. if( $pin_info['state'] == 1 && !$res )
  326. {
  327. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_id' => $order['order_id']) )->find();
  328. M('lionfish_comshop_pin_order')->where( array('pin_id' =>$pin_order['pin_id'],'order_id' => $order['order_id'] ) )->delete();
  329. $newpin_id = D('Home/Pin')->openNewTuan($order['order_id'],$order_goods_info['goods_id'],$order['member_id']);
  330. //插入拼团订单
  331. D('Home/Pin')->insertTuanOrder($newpin_id,$order['order_id']);
  332. unset($pin_info);
  333. $is_pin_success = D('Home/Pin')->checkPinSuccess($newpin_id);
  334. if($is_pin_success) {
  335. D('Home/Pin')->updatePintuanSuccess($newpin_id);
  336. }
  337. }else{
  338. $is_pin_success = D('Home/Pin')->checkPinSuccess($pin_order['pin_id']);
  339. if($is_pin_success) {
  340. D('Home/Pin')->updatePintuanSuccess($pin_order['pin_id']);
  341. }
  342. }
  343. }
  344. //检测预售
  345. if( $buy_type == 'presale' )
  346. {
  347. D('Home/PresaleGoods')->payBackOrder( $order['order_id'] , 0 );
  348. }
  349. //检测礼品卡
  350. if( $buy_type == 'virtualcard' )
  351. {
  352. D('Seller/VirtualCard')->payBackOrder( $order['order_id'] );
  353. }**/
  354. }
  355. }
  356. //返回支付成功给app
  357. $data = array();
  358. $data['code'] = 0;
  359. $data['has_yupay'] = 1;
  360. $data['is_integral'] = 0;
  361. $data['is_spike'] = $is_spike;
  362. $data['is_go_orderlist'] = $is_just_1;
  363. $data['order_id'] = $oid;
  364. $data['order_all_id'] = $order_all_id;
  365. echo json_encode($data);
  366. die();
  367. }
  368. }
  369. public function getOrderTotalMoney( $item )
  370. {
  371. $free_tongji = $item['total'] + $item['packing_fare'] +$item['shipping_fare']-$item['voucher_credit']-$item['fullreduction_money'] - $item['score_for_money'] + $item['localtown_add_shipping_fare']- $item['fare_shipping_free'];
  372. if($free_tongji < 0){
  373. $free_tongji = 0;
  374. }
  375. return round( $free_tongji ,2 );
  376. }
  377. }