OrderV2Model.class.php 19 KB

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