OrderModel.class.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. *
  6. * @author fish
  7. *
  8. */
  9. namespace Home\Model;
  10. use Think\Model;
  11. class OrderModel extends Model{
  12. /**
  13. * 抽奖商品订单开奖
  14. * @param unknown $goods_id
  15. * @param string $oids
  16. * @param string $is_auto_open_lottery
  17. */
  18. function open_goods_lottery_order($goods_id,$oids='',$is_auto_open_lottery = false)
  19. {
  20. $sql_over = "select p.pin_id,p.state from ".C('DB_PREFIX')."pin as p,".C('DB_PREFIX')."pin_order as po,
  21. ".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."order_goods as og
  22. where p.state = 0 and p.pin_id = po.pin_id and po.order_id = o.order_id
  23. and o.order_id = og.order_id and og.goods_id and og.goods_id = {$goods_id}
  24. order by p.pin_id asc ";
  25. $list_over=M()->query($sql_over);
  26. foreach($list_over as $pin_over)
  27. {
  28. M('pin')->where( array('pin_id' => $pin_over['pin_id']) )->save( array('end_time' => time()) );
  29. }
  30. //begin_time
  31. $pin_goods = M('pin_goods')->where( array('goods_id' => $goods_id) )->find();
  32. $begin_time = $pin_goods['begin_time'];
  33. M('pin_goods')->where( array('goods_id' => $goods_id) )->save( array('end_time' => time()) );
  34. $sql = "select p.pin_id from ".C('DB_PREFIX')."pin as p,".C('DB_PREFIX')."pin_order as po,
  35. ".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."order_goods as og
  36. where p.state = 1 and p.pin_id = po.pin_id and po.order_id = o.order_id
  37. and o.order_id = og.order_id and og.goods_id and og.goods_id = {$goods_id} and p.begin_time >= {$begin_time}
  38. order by p.pin_id asc ";
  39. $list=M()->query($sql);
  40. //begin_time
  41. foreach($list as $pin)
  42. {
  43. M('pin')->where( array('pin_id' => $pin['pin_id']) )->save( array('lottery_state' => 1) );
  44. }
  45. $lottery_goods = M('lottery_goods')->where( array('goods_id' => $goods_id) )->find();
  46. if($is_auto_open_lottery) {
  47. //自动开奖
  48. $real_win_quantity = $lottery_goods['real_win_quantity'];
  49. $win_quantity = $lottery_goods['win_quantity'];
  50. $auto_jia_order = $win_quantity - $real_win_quantity;
  51. if($real_win_quantity > 0)
  52. {
  53. $sql = "select o.order_id from ".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."order_goods as og
  54. where o.order_id = og.order_id and og.goods_id and o.order_status_id =1 and o.date_added >{$begin_time} and og.goods_id = {$goods_id}
  55. group by o.member_id order by o.order_id desc limit {$win_quantity}";
  56. $list=M()->query($sql);
  57. if(!empty($list)) {
  58. //有订单
  59. $oids_arr = array();
  60. foreach($list as $val)
  61. {
  62. $oids_arr[] = $val['order_id'];
  63. }
  64. $oids_count = count($oids_arr);
  65. if($oids_count < $real_win_quantity)
  66. {
  67. $real_win_quantity = $oids_count;//实际取出的数量=订单数量
  68. $auto_jia_order = $win_quantity - $real_win_quantity;//如果数据库中订单数量小于真实中奖人数
  69. }
  70. shuffle($oids_arr);
  71. $need_order_ids_arr = array_rand($oids_arr, $real_win_quantity);
  72. $need_order_arr = array();//随机出需要的订单id
  73. if($real_win_quantity == 1) {
  74. $need_order_arr[] = $oids_arr[$need_order_ids_arr];
  75. } else {
  76. foreach($need_order_ids_arr as $vv)
  77. {
  78. $need_order_arr[] = $oids_arr[$vv];
  79. }
  80. }
  81. M('order')->where( array('order_id' => array('in', $need_order_arr )) )->save( array('lottery_win' => 1) );
  82. if($auto_jia_order > 0)
  83. {
  84. //作假人数大于0,那么就开假奖
  85. $this->open_jia_lottery($goods_id,$auto_jia_order);
  86. }
  87. }else {
  88. //没有订单,全假开奖
  89. $this->open_jia_lottery($goods_id,$win_quantity);
  90. }
  91. } else {
  92. //全假开奖
  93. $this->open_jia_lottery($goods_id,$win_quantity);
  94. }
  95. } else {
  96. //手动开奖
  97. $win_quantity = $lottery_goods['win_quantity'];
  98. $ids_arr = explode(',',$oids);
  99. if(!empty($ids_arr) && !empty($oids)) {
  100. M('order')->where( array('order_id' => array('in',$oids)) )->save( array('lottery_win' => 1) );
  101. $auto_jia_order = $win_quantity - count($ids_arr);
  102. }else {
  103. $auto_jia_order = $win_quantity;
  104. }
  105. if($auto_jia_order > 0) //作假人数大于0,那么就开假奖
  106. $this->open_jia_lottery($goods_id,$auto_jia_order);
  107. }
  108. M('lottery_goods')->where( array('goods_id' => $goods_id) )->save( array('is_open_lottery' => 1) );
  109. }
  110. /**
  111. * 开假奖励
  112. * @param unknown $goods_id
  113. */
  114. function open_jia_lottery($goods_id,$need_count)
  115. {
  116. $sql = "select * from ".C('DB_PREFIX')."jiauser order by Rand() desc limit {$need_count}";
  117. $list=M()->query($sql);
  118. $order_no = build_order_no($goods_id);
  119. $mobile_arr = array('31','58','51','52','80','55','32','82','35');
  120. foreach($list as $key => $val)
  121. {
  122. //build_order_no
  123. $order_no = substr($order_no, 0, strlen($order_no) -4).mt_rand(0,9).mt_rand(1,8).mt_rand(0,9).mt_rand(0,9);
  124. $val['order_no'] = $order_no;
  125. if(empty($val['mobile'])) {
  126. $val['mobile'] = '1'.$mobile_arr[array_rand($mobile_arr)].'*****'.mt_rand(0, 9).mt_rand(0, 9).mt_rand(0, 9);
  127. } else {
  128. $val['mobile'] = substr($val['mobile'],0,3).'*****'.substr($val['mobile'],-3,3);
  129. }
  130. $data = array();
  131. $data['goods_id'] = $goods_id;
  132. $data['avatar'] = $val['avatar'];
  133. $data['uname'] = $val['username'];
  134. $data['order_sn'] = $val['order_no'];
  135. $data['mobile'] = $val['mobile'];
  136. $data['addtime'] = time();
  137. M('jiaorder')->add($data);
  138. }
  139. }
  140. function get_all_address($uid){
  141. $list=M('address')->where(array('member_id'=>$uid))->order('address_id desc ')->select();
  142. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  143. if(!empty($list)){
  144. foreach ($list as $k => $v) {
  145. $list[$k]['id']=$v['address_id'];
  146. $list[$k]['address_id']=$hashids->encode($v['address_id']);
  147. }
  148. }
  149. return $list;
  150. }
  151. function order_info($order_id){
  152. //订单信息
  153. $order_sql='select t.title,o.* from '.C('DB_PREFIX').'order o,'.C('DB_PREFIX').'transport t where o.shipping_method=t.id and o.order_id='.$order_id;
  154. $order=M()->query($order_sql);
  155. //收货地址
  156. $address=M('Address')->find($order[0]['address_id']);
  157. //商品详情
  158. $goods=M('OrderGoods')->where(array('order_id'=>$order_id))->select();
  159. //商品选项
  160. //$option=M('OrderOption')->where(array('order_id'=>$order_id))->select();
  161. //总计
  162. $total=M('OrderTotal')->where(array('order_id'=>$order_id))->select();
  163. //订单历史
  164. $history=M('OrderHistory')->where(array('order_id'=>$order_id))->select();
  165. return array(
  166. 'order'=>$order,
  167. 'address'=>$address,
  168. //'option'=>$option,
  169. 'goods'=>$goods,
  170. 'total'=>$total,
  171. 'history'=>$history
  172. );
  173. }
  174. function show_order_page($member_id){
  175. $count=M('order')->where(array('member_id'=>$member_id))->count();
  176. $Page = new \Think\Page($count,C('FRONT_PAGE_NUM'));
  177. $show = $Page->show();// 分页显示输出
  178. $show=str_replace("/user/order/p/","/order/", $show);
  179. $sql='SELECT o.order_num_alias as alias,o.order_id,o.name,o.date_added,o.total,os.name as status FROM '.C('DB_PREFIX').'order o,'.C('DB_PREFIX')
  180. ."order_status os where o.order_status_id=os.order_status_id and o.member_id=".$member_id.' order by o.order_id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  181. $list=M()->query($sql);
  182. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  183. if(!empty($list)){
  184. foreach ($list as $k => $v) {
  185. $list[$k]['order_id']=$hashids->encode($v['order_id']);
  186. }
  187. }
  188. return array(
  189. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  190. 'list'=>$list,
  191. 'page'=>$show
  192. );
  193. }
  194. function addOrder($data) {
  195. $integral_model = D('Seller/Integral');
  196. $order = array();
  197. $order['member_id']=$data['member_id'];
  198. $order['order_num_alias']=$data['order_num_alias'];
  199. $order['name']=$data['name'];
  200. if( isset($data['from_type']) )
  201. {
  202. $order['from_type']=$data['from_type'];
  203. }
  204. if($data['delivery'] == 'pickup')
  205. {
  206. $order['telephone']=$data['ziti_mobile'];
  207. $order['shipping_name']=$data['ziti_name'];
  208. }else{
  209. $order['telephone']=$data['telephone'];
  210. $order['shipping_name']=$data['shipping_name'];
  211. }
  212. $order['type']=$data['type'];
  213. $order['shipping_address']=$data['shipping_address'];
  214. $order['shipping_city_id']=$data['shipping_city_id'];
  215. $order['shipping_country_id']=$data['shipping_country_id'];
  216. $order['shipping_province_id']=$data['shipping_province_id'];
  217. $order['shipping_tel']=$data['shipping_tel'];
  218. $order['order_status_id']=C('default_order_status_id');
  219. $order['voucher_id']=$data['voucher_id'];
  220. $order['voucher_credit']=$data['voucher_credit'];
  221. $order['ip']=get_client_ip();
  222. $order['shipping_fare'] = $data['shipping_fare'];
  223. $order['ip_region'] = '';
  224. if($data['total'] <0)
  225. {
  226. $data['total'] = 0;
  227. }
  228. $order['date_added'] =time();
  229. $order['total'] =$data['total'];
  230. $order['user_agent']=$data['user_agent'];
  231. $order['shipping_method']=0;//快递id
  232. $order['delivery']=$data['delivery'];
  233. //$data['delivery']
  234. $order['payment_code']=$data['payment_method'];
  235. $order['address_id']=$data['address_id'];
  236. $order['comment']=$data['comment'];
  237. $order['store_id'] = $data['store_id'];
  238. $order_id=M('Order')->add($order);
  239. //Model $data['member_id']
  240. $goods_model = D('Home/Goods');
  241. $member_info = M('member')->where( array('member_id' => $data['member_id']) )->find();
  242. $is_pin = 0;
  243. $pin_id = 0;
  244. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  245. $share_model = D('Seller/Fissionsharing');
  246. $kucun_method = C('kucun_method');
  247. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  248. $free_tuan = 0;
  249. if(isset($data['goodss'])){
  250. foreach ($data['goodss'] as $goods) {
  251. //$goods_id=$hashids->decode($goods['goods_id']);
  252. $goods_id = $goods['goods_id'];
  253. $pin_id = $goods['pin_id'];
  254. $commiss_one_money = 0;
  255. $goods_info = M('goods')->field('points,commiss_fen_one_disc,commiss_fen_two_disc,commiss_fen_three_disc,commiss_three_dan_disc,commiss_two_dan_disc,commiss_one_dan_disc,store_id,type,model,image')->where( array('goods_id' => $goods_id) )->find();
  256. //points buy_send_score type
  257. if( !empty($goods_info['points']) && $goods_info['points'] > 0 && $goods_info['type'] != 'integral')
  258. {
  259. //$goods['quantity']
  260. //integral_model
  261. $score = $goods_info['points'] * $goods['quantity'];
  262. $integral_model->charge_member_score( $data['member_id'] , $score,'in', 'goodsbuy', $order_id);
  263. }else if( C('buy_send_score') > 0 && $goods_info['type'] != 'integral')
  264. {
  265. $score = C('buy_send_score') * $goods['quantity'];
  266. $integral_model->charge_member_score( $data['member_id'] , $score,'in', 'goodsbuy', $order_id);
  267. }
  268. //'is_pin' => $is_pin,
  269. $is_pin = $goods['is_pin'];
  270. //判断是否拼团开始
  271. $commiss_one_money = 0;
  272. $commiss_two_money = 0;
  273. $commiss_three_money = 0;
  274. $commiss_fen_one_money = 0;
  275. $commiss_fen_two_money = 0;
  276. $commiss_fen_three_money = 0;
  277. //<?php if( C('opencommiss') == 1){
  278. //C('commiss_level_num') >= 1
  279. if($is_pin == 1)
  280. {
  281. $pin_goods = M('pin_goods')->field('commiss_one_pin_disc,commiss_two_pin_disc,commiss_three_pin_disc')->where( array('goods_id' => $goods_id) )->find();
  282. //$goods['total']
  283. if(C('opencommiss') == 1)
  284. {
  285. if(C('commiss_level_num') >= 1)
  286. {
  287. $commiss_one_money = round( ($pin_goods['commiss_one_pin_disc'] * $goods['total'])/100 , 2);
  288. }
  289. if(C('commiss_level_num') >= 2)
  290. {
  291. $commiss_two_money = round( ($pin_goods['commiss_two_pin_disc'] * $goods['total'])/100 , 2);
  292. }
  293. if(C('commiss_level_num') >= 3)
  294. {
  295. $commiss_three_money = round( ($pin_goods['commiss_three_pin_disc'] * $goods['total'])/100 , 2);
  296. }
  297. }
  298. if(C('is_open_fissionsharing') == 1)
  299. {
  300. if(C('fissionsharing_level') >= 1)
  301. {
  302. $commiss_fen_one_money = round( ($goods_info['commiss_fen_one_disc'] * $goods['total'])/100 , 2);
  303. }
  304. if(C('fissionsharing_level') >= 2)
  305. {
  306. $commiss_fen_two_money = round( ($goods_info['commiss_fen_two_disc'] * $goods['total'])/100 , 2);
  307. }
  308. if(C('fissionsharing_level') >= 3)
  309. {
  310. $commiss_fen_three_money = round( ($goods_info['commiss_fen_three_disc'] * $goods['total'])/100 , 2);
  311. }
  312. }
  313. $goods_info['type'] = 'pintuan';
  314. $pin_model = D('Home/Pin');
  315. $pin_id = $pin_model->checkPinState($goods['pin_id']);
  316. //addOrder
  317. $is_pin_over = $pin_model->getNowPinState($goods['pin_id']);
  318. if($is_pin_over == 1 || $is_pin_over == 2)
  319. {
  320. $pin_id = 0;
  321. }
  322. if($pin_id ==0) {
  323. //新开团
  324. $pin_id = $pin_model->openNewTuan($order_id,$goods_id,$data['member_id']);
  325. $is_new_tuan = true;
  326. }
  327. //插入拼团订单
  328. $pin_model->insertTuanOrder($pin_id,$order_id);
  329. }else{
  330. if(C('opencommiss') == 1)
  331. {
  332. if(C('commiss_level_num') >= 1)
  333. {
  334. $commiss_one_money = round( ($goods_info['commiss_one_dan_disc'] * $goods['total'])/100 , 2);
  335. }
  336. if(C('commiss_level_num') >= 2)
  337. {
  338. $commiss_two_money = round( ($goods_info['commiss_two_dan_disc'] * $goods['total'])/100 , 2);
  339. }
  340. if(C('commiss_level_num') >= 3)
  341. {
  342. $commiss_three_money = round( ($goods_info['commiss_three_dan_disc'] * $goods['total'])/100 , 2);
  343. }
  344. }
  345. if(C('is_open_fissionsharing') == 1)
  346. {
  347. if(C('fissionsharing_level') >= 1)
  348. {
  349. $commiss_fen_one_money = round( ($goods_info['commiss_fen_one_disc'] * $goods['total'])/100 , 2);
  350. }
  351. if(C('fissionsharing_level') >= 2)
  352. {
  353. $commiss_fen_two_money = round( ($goods_info['commiss_fen_two_disc'] * $goods['total'])/100 , 2);
  354. }
  355. if(C('fissionsharing_level') >= 3)
  356. {
  357. $commiss_fen_three_money = round( ($goods_info['commiss_fen_three_disc'] * $goods['total'])/100 , 2);
  358. }
  359. }
  360. }
  361. //var_dump($goods_info,$goods['total']);die();
  362. $goods['member_disc'] = isset($goods['member_disc']) ? $goods['member_disc'] : 100;
  363. //判断是否拼团结束
  364. $type = ($is_pin == 1) ? 'pintuan': 'normal';
  365. //header_disc
  366. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_goods SET order_id = '" .$order_id
  367. ."',goods_id='".$goods_id."'"
  368. .",store_id='".$goods_info['store_id']."'"
  369. .",name='".addslashes($goods['name'])."'"
  370. .",model='".$goods['model']."'"
  371. .",commiss_one_money='".$commiss_one_money."'"
  372. .",commiss_two_money='".$commiss_two_money."'"
  373. .",commiss_three_money='".$commiss_three_money."'"
  374. .",commiss_fen_one_money='".$commiss_fen_one_money."'"
  375. .",commiss_fen_two_money='".$commiss_fen_two_money."'"
  376. .",commiss_fen_three_money='".$commiss_fen_three_money."'"
  377. .",head_disc='".$goods['header_disc']."'"
  378. .",member_disc='".$goods['member_disc']."'"
  379. .",level_name='".$goods['level_name']."'"
  380. .",is_pin='".$is_pin."'"
  381. .",goods_images='".$goods_info['image']."'"
  382. .",goods_type='".$type."'"
  383. .",shipping_fare='".$goods['shipping_fare']."'"
  384. .",quantity='".(int)$goods['quantity']."'"
  385. .",price='".(float)$goods['price']."'"
  386. .",rela_goodsoption_valueid='".$goods['option']."'"
  387. .",comment='".$goods['comment']."'"
  388. .",total='".(float)$goods['total']."'"
  389. );
  390. $order_goods_id=$this->getLastInsID();
  391. //检测是否需要将订单放入分佣里面
  392. if(C('is_open_fissionsharing') == 1)
  393. {
  394. $share_model->add_sharing_order($order_id,$goods_id,$order_goods_id,$data['member_id'],$goods_info['store_id'] );
  395. }
  396. if(!empty($goods['option']))
  397. {
  398. $options_arr = array();
  399. $option_value_id_arr = explode('_',$goods['option']);
  400. foreach($option_value_id_arr as $id_val)
  401. {
  402. $goods_option_value = M('goods_option_value')->where( array('option_value_id' => $id_val,'goods_id' =>$goods_id) )->find();
  403. $options_arr[$goods_option_value['goods_option_id']] = $goods_option_value['goods_option_value_id'];
  404. $goods_option = M('goods_option')->where( array('goods_option_id' =>$goods_option_value['goods_option_id']) )->find();
  405. $option_value = M('option_value')->where( array('option_value_id' =>$goods_option_value['option_value_id']) )->find();
  406. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_option SET order_id = '" .$order_id
  407. ."',order_goods_id='".$order_goods_id."'"
  408. .",goods_option_id='".(int)$goods_option_value['goods_option_id']."'"
  409. .",goods_option_value_id='".(int)$goods_option_value['goods_option_value_id']."'"
  410. .",name='".$goods_option['option_name']."'"
  411. .",value='".$option_value['value_name']."'"
  412. );
  413. }
  414. }
  415. if($kucun_method == 0)
  416. {
  417. $goods_model->del_goods_mult_option_quantity($order_id,$goods['option'],$goods_id,$goods['quantity'],1);
  418. }
  419. }
  420. }
  421. //type normal pintuan is_pin
  422. $order_type = $is_pin == 1 ? 'pintuan': 'normal';
  423. M('order')->where( array('order_id' => $order_id) )->save( array('is_pin' => $is_pin, 'order_type' =>$order_type) );
  424. if(isset($data['totals'])){
  425. foreach ($data['totals'] as $total) {
  426. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_total SET order_id = '" .$order_id
  427. ."',code='".$total['code']."'"
  428. .",title='".$total['title']."'"
  429. .",text='".$total['text']."'"
  430. .",value='".(float)$total['value']."'");
  431. }
  432. }
  433. $oh = array();
  434. $oh['order_id']=$order_id;
  435. $oh['order_status_id']=C('default_order_status_id');
  436. $oh['comment']='创建订单';
  437. $oh['date_added']=time();
  438. $oh_id=M('OrderHistory')->add($oh);
  439. //storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'下了订单 '.$data['order_num_alias'].' 未支付');
  440. return $order_id;
  441. }
  442. function addOrder2($data) {
  443. $order['member_id']=$data['member_id'];
  444. $order['order_num_alias']=$data['order_num_alias'];
  445. $order['name']=$data['name'];
  446. $order['email']=$data['email'];
  447. $order['telephone']=$data['telephone'];
  448. $order['shipping_name']=$data['shipping_name'];
  449. $order['shipping_address']=$data['shipping_address'];
  450. $order['shipping_city_id']=$data['shipping_city_id'];
  451. $order['shipping_country_id']=$data['shipping_country_id'];
  452. $order['shipping_province_id']=$data['shipping_province_id'];
  453. $order['shipping_tel']=$data['shipping_tel'];
  454. $order['comment']=$data['comment'];
  455. $order['order_status_id']=C('default_order_status_id');
  456. $order['ip']=get_client_ip();
  457. $order['voucher_id']=$data['voucher_id'];
  458. $order['voucher_credit']=$data['voucher_credit'];
  459. $order['shipping_fare'] = $data['shipping_fare'];
  460. $order['ip_region'] = '';
  461. if($data['total'] <0)
  462. {
  463. $data['total'] = 0;
  464. }
  465. $order['date_added'] =time();
  466. $order['total'] =$data['total'];
  467. $order['user_agent']=$data['user_agent'];
  468. $order['shipping_method']=$data['shipping_method'];
  469. $order['delivery']=$data['delivery'];
  470. $order['payment_code']=$data['payment_method'];
  471. $order['address_id']=$data['address_id'];
  472. $order_id=M('Order')->add($order);
  473. //Model $data['member_id']
  474. $goods_model = D('Home/Goods');
  475. $member_info = M('member')->where( array('member_id' => $data['member_id']) )->find();
  476. $is_pin = 0;
  477. $pin_id = 0;
  478. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  479. $kucun_method = C('kucun_method');
  480. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  481. $free_tuan = 0;
  482. if(isset($data['goodss'])){
  483. foreach ($data['goodss'] as $goods) {
  484. $goods_id=$hashids->decode($goods['goods_id']);
  485. $pin_id = $goods['pin_id'];
  486. $commiss_one_money = 0;
  487. $goods_info = M('goods')->field('type,is_free_in,commiss_one_money,commiss_one_pin_disc,commiss_one_dan_disc,pinprice,danprice')->where( array('goods_id' => $goods_id[0]) )->find();
  488. $is_new_tuan = false;
  489. if($goods['pin_type'] == 'pin'){
  490. $is_pin = 1;
  491. $pin_model = D('Home/Pin');
  492. $pin_id = $pin_model->checkPinState($goods['pin_id']);
  493. if($pin_id ==0) {
  494. //新开团
  495. $pin_id = $pin_model->openNewTuan($order_id,$goods_id,$data['member_id']);
  496. $is_new_tuan = true;
  497. }
  498. //插入拼团订单
  499. $pin_model->insertTuanOrder($pin_id,$order_id);
  500. //is_header_disc 记录团长折扣 header_disc commiss_one_money
  501. $commiss_one_money = round( ($goods_info['pinprice'] * $goods_info['commiss_one_pin_disc'])/100,2);
  502. } else {
  503. $commiss_one_money = round( ($goods_info['danprice'] * $goods_info['commiss_one_dan_disc'])/100,2);
  504. }
  505. if($is_new_tuan && $goods_info['is_free_in'] == 1)
  506. {
  507. //团长可以免单开团,需要判断是否拥有免单券
  508. //$data['member_id']
  509. $voucher_free_info = M('voucher_free')->where( array('user_id' =>$data['member_id'],'state'=>0) )->find();
  510. if(!empty($voucher_free_info))
  511. {
  512. //可以免单开团
  513. M('voucher_free')->where( array('id' =>$voucher_free_info['id']) )->save( array('state' => 1) );
  514. $free_tuan = 1;
  515. }
  516. }
  517. if($free_tuan == 1)
  518. {
  519. $goods['total'] = 0;
  520. }
  521. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_goods SET order_id = '" .$order_id
  522. ."',goods_id='".$goods_id[0]."'"
  523. .",store_id='".$goods['store_id']."'"
  524. .",name='".addslashes($goods['name'])."'"
  525. .",model='".$goods['model']."'"
  526. .",is_pin='".$is_pin."'"
  527. .",pin_id='".$pin_id."'"
  528. .",free_tuan='".$free_tuan."'"
  529. .",head_disc='".$goods['header_disc']."'"
  530. .",commiss_one_money='".$commiss_one_money."'"
  531. .",quantity='".(int)$goods['quantity']."'"
  532. .",price='".(float)$goods['price']."'"
  533. .",total='".(float)$goods['total']."'"
  534. );
  535. if($goods_info['type'] =='haitao')
  536. {
  537. $og_haitao_data = array();
  538. $og_haitao_data['order_id'] = $order_id;
  539. $og_haitao_data['real_name'] = $member_info['id_cardreal_name'];
  540. $og_haitao_data['id_card'] = $member_info['id_card'];
  541. $og_haitao_data['add_time'] = time();
  542. M('order_goods_haitao')->add($og_haitao_data);
  543. }
  544. $order_goods_id=$this->getLastInsID();
  545. foreach ($goods['option'] as $option) {
  546. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_option SET order_id = '" .$order_id
  547. ."',order_goods_id='".$order_goods_id."'"
  548. .",goods_option_id='".(int)$option['goods_option_id']."'"
  549. .",goods_option_value_id='".(int)$option['goods_option_value_id']."'"
  550. .",name='".$option['name']."'"
  551. .",value='".$option['value']."'"
  552. .",type='".$option['type']."'"
  553. );
  554. }
  555. if($kucun_method == 0)
  556. {
  557. $goods_model->del_goods_mult_option_quantity($order_id,$goods['quantity'],1);
  558. }
  559. }
  560. }
  561. //pintuan
  562. M('order')->where( array('order_id' => $order_id) )->save( array('is_pin' => $is_pin, 'pin_id' =>$pin_id) );
  563. //免单开团
  564. if($free_tuan == 1)
  565. {
  566. M('order')->where( array('order_id' => $order_id) )->save( array('total' => 0) );
  567. }
  568. if(isset($data['totals'])){
  569. foreach ($data['totals'] as $total) {
  570. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_total SET order_id = '" .$order_id
  571. ."',code='".$total['code']."'"
  572. .",title='".$total['title']."'"
  573. .",text='".$total['text']."'"
  574. .",value='".(float)$total['value']."'");
  575. }
  576. }
  577. $oh['order_id']=$order_id;
  578. $oh['order_status_id']=C('default_order_status_id');
  579. $oh['comment']=$data['comment'];
  580. $oh['date_added']=time();
  581. $oh_id=M('OrderHistory')->add($oh);
  582. //storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'下了订单 '.$data['order_num_alias'].' 未支付');
  583. return $order_id;
  584. }
  585. /**
  586. * 通用设置订单状态
  587. * 一般拼团成功时使用
  588. */
  589. function change_order_status($order_id,$order_status_id)
  590. {
  591. //设置订单状态
  592. $this->execute("UPDATE " . C('DB_PREFIX') . "order SET order_status_id = {$order_status_id} where order_id=".$order_id);
  593. }
  594. /**
  595. * 确认收货
  596. * @param unknown $order_id
  597. */
  598. function receive_order($order_id)
  599. {
  600. //设置订单状态
  601. $this->execute("UPDATE " . C('DB_PREFIX') . "order SET order_status_id = 6 where order_id=".$order_id);
  602. $order_history = array();
  603. $order_history['order_id'] = $order_id;
  604. $order_history['order_status_id'] = 6;
  605. $order_history['notify'] = 0;
  606. $order_history['comment'] = '用户确认收货';
  607. $order_history['date_added']=time();
  608. M('order_history')->add($order_history);
  609. $fenxiao_model = D('Home/Fenxiao');
  610. $fenxiao_model->send_order_commiss_money($order_id);
  611. $share_model = D('Seller/Fissionsharing');
  612. $share_model->send_order_commiss_money_do( $order_id);
  613. $integral_model = D('Seller/Integral');
  614. $integral_model->send_order_score_dr($order_id);
  615. }
  616. function cancel_order($order_id){
  617. //设置订单状态
  618. $this->execute("UPDATE " . C('DB_PREFIX') . "order SET order_status_id = 5 where order_id=".$order_id);
  619. //写人订单历史
  620. $this->execute("INSERT INTO " . C('DB_PREFIX') . 'order_history SET order_status_id = 5,order_id='.$order_id.",comment='用户取消了订单',date_added=".time());
  621. //订单商品
  622. $goods=M('order_goods')->where(array('order_id'=>$order_id))->select();
  623. $goods_model = D('Home/Goods');
  624. $kucun_method = C('kucun_method');
  625. $kucun_method = empty($kucun_method) ? 0 : intval($kucun_method);
  626. if(isset($goods) && $kucun_method == 0 ){
  627. foreach ($goods as $key => $value) {
  628. //$this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity + " . (int)$value['quantity'] . ") WHERE goods_id = '" . $value['goods_id'] . "' ");
  629. //del_goods_mult_option_quantity($order_id,$option,$goods_id,$quantity,$type='1')
  630. $goods_model->del_goods_mult_option_quantity($order_id,$goods['rela_goodsoption_valueid'],$value['goods_id'],$value['quantity'],2);
  631. //$goods_model->del_goods_mult_option_quantity($order_id,$value['quantity'],2);
  632. //销量回退
  633. //$this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count - " . (int)$value['quantity'] . ") WHERE goods_id = '" . $value['goods_id'] . "' ");
  634. }
  635. }
  636. }
  637. /**
  638. * 订单自动配送操作 同城配送订单预查询运费
  639. * @param unknown $order
  640. */
  641. public function order_auto_delivery($order){
  642. $presale_result = D('Home/PresaleGoods')->getOrderPresaleInfo( $order['order_id'] );
  643. if( $presale_result['code'] == 0 )
  644. {
  645. //预售订单不自动发货
  646. return false;
  647. }
  648. if($order['delivery'] == 'localtown_delivery'){//同城配送订单
  649. $is_localtown_auto_delivery = D('Home/Front')->get_config_by_name('is_localtown_auto_delivery');
  650. if($is_localtown_auto_delivery == 1){//订单自动确认配送
  651. //同城配送
  652. D('Seller/Order')->do_send_localtown($order['order_id'],'后台自动确认发货,开始配送货物');
  653. }
  654. //同城配送订单预查询运费
  655. $this->third_delivery_query($order);
  656. }else if($order['delivery'] == 'tuanz_send' && $order['type'] != 'pintuan' ){//团长配送订单
  657. $is_communityhead_auto_delivery = D('Home/Front')->get_config_by_name('is_communityhead_auto_delivery');
  658. if($is_communityhead_auto_delivery == 1){//团长订单自动确认配送
  659. D('Seller/Order')->do_send_tuanz($order['order_id'],'后台自动确认发货,开始配送货物');
  660. $is_communityhead_auto_service = D('Home/Front')->get_config_by_name('is_communityhead_auto_service');
  661. if($is_communityhead_auto_service == 1){//团长订单自动确认送达团长
  662. D('Seller/Order')->do_tuanz_over($order['order_id'],'后台自动确认送达团长');
  663. D('Home/Frontorder')->send_order_operate($order['order_id']);
  664. }
  665. }
  666. }else if($order['delivery'] == 'pickup' && $order['type'] != 'pintuan'){//到点自提订单
  667. $is_ziti_auto_delivery = D('Home/Front')->get_config_by_name('is_ziti_auto_delivery');
  668. if($is_ziti_auto_delivery == 1){//到店自提订单自动确认配送
  669. D('Seller/Order')->do_send_tuanz($order['order_id'],'后台自动确认发货,开始配送货物');
  670. $is_ziti_auto_service = D('Home/Front')->get_config_by_name('is_ziti_auto_service');
  671. if($is_ziti_auto_service == 1){//到店自提订单自动确认送达团长
  672. D('Seller/Order')->do_tuanz_over($order['order_id'],'后台自动确认送达团长');
  673. D('Home/Frontorder')->send_order_operate($order['order_id']);
  674. }
  675. }
  676. }
  677. //订单自动推送给第三方配送公司
  678. $this->order_autosend_third_delivery($order);
  679. }
  680. /**
  681. * @author cy 2020-12-29
  682. * @desc 同城配送订单预查询运费
  683. * @param $order_id
  684. */
  685. public function third_delivery_query($order){
  686. //订单号
  687. $order_id = $order['order_id'];
  688. $is_localtown_imdada_status = D('Home/Front')->get_config_by_name('is_localtown_imdada_status');
  689. $is_imdada_prequery_status = D('Home/Front')->get_config_by_name('is_imdada_prequery_status');
  690. $is_localtown_sf_status = D('Home/Front')->get_config_by_name('is_localtown_sf_status');
  691. $is_sf_prequery_status = D('Home/Front')->get_config_by_name('is_sf_prequery_status');
  692. //是否开启码科配送,
  693. //是否开启预测码科配送 todo
  694. $is_localtown_mk_status = D('Home/Front')->get_config_by_name('is_localtown_mk_status');
  695. $is_make_prequery_status = D('Home/Front')->get_config_by_name('is_make_prequery_status');
  696. $order_info = $order;
  697. //店铺地址
  698. if($order_info['store_id'] > 0) {
  699. $store_data = $this->getOrderStoreAddress($order_info);
  700. $order_info['store_data'] = $store_data;
  701. }
  702. //商品信息
  703. $sql = "select og.goods_id,og.name as goods_name,og.quantity,og.rela_goodsoption_valueid,g.weight as goods_weight from "
  704. . C('DB_PREFIX')."lionfish_comshop_order_goods as og left join ".C('DB_PREFIX')."lionfish_comshop_goods as g on og.goods_id=g.id "
  705. ." where og.order_id = ".$order_id;
  706. $goods_list = M()->query($sql);
  707. $goods_count = 0;
  708. $goods_weight = 0;
  709. $goods_type_count = 0;
  710. foreach($goods_list as $k=>$v){
  711. $goods_count = $goods_count + $v['quantity'];
  712. if(!empty($v['rela_goodsoption_valueid'])){
  713. $goods_option_mult_value = M('lionfish_comshop_goods_option_item_value')->where( array('option_item_ids' => $v['rela_goodsoption_valueid'],'goods_id' => $v['goods_id']) )->find();
  714. if(!empty($goods_option_mult_value)){
  715. $v['goods_weight'] = $goods_option_mult_value['weight'];
  716. }
  717. }
  718. $goods_weight = $goods_weight + $v['quantity'] * $v['goods_weight'];
  719. $goods_type_count = $goods_type_count + 1;
  720. }
  721. if(empty($goods_weight)){
  722. $goods_weight = 100;//默认100克
  723. }
  724. $order_info['goods_list'] = $goods_list;
  725. //商品种类
  726. $order_info['goods_type_count'] = $goods_type_count;
  727. //商品数量
  728. $order_info['goods_count'] = $goods_count;
  729. //商品重量
  730. $order_info['goods_weight'] = $goods_weight;
  731. //订单总金额
  732. $order_info['order_total'] = $order_info['total']+$order_info['packing_fare']+$order_info['shipping_fare']-$order_info['voucher_credit']-$order_info['fullreduction_money']-$order_info['score_for_money']+$order_info['localtown_add_shipping_fare']-$order_info['fare_shipping_free'];
  733. //收货人地址
  734. $province_info = D('Home/Front')->get_area_info($order_info['shipping_province_id']);
  735. $city_info = D('Home/Front')->get_area_info($order_info['shipping_city_id']);
  736. $area_info = D('Home/Front')->get_area_info($order_info['shipping_country_id']);
  737. $order_info['shipping_address'] = $province_info['name'].$city_info['name'].$area_info['name'].$order_info['shipping_address'];
  738. //收货人经纬度
  739. $order_distribution_info = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  740. //收货人地址纬度
  741. $order_info['shipping_lat'] = $order_distribution_info['member_lat'];
  742. //收货人地址经度
  743. $order_info['shipping_lng'] = $order_distribution_info['member_lon'];
  744. //商城名称
  745. $shoname = D('Home/Front')->get_config_by_name('shoname');
  746. $order_info['shoname'] = $shoname;
  747. //达达配送
  748. if($is_localtown_imdada_status == 1 && $is_imdada_prequery_status == 1){
  749. $imdada = new \Lib\Localtown\Imdada();
  750. $result = $imdada->queryDeliverFee($order_info);
  751. $thirth_data = [];
  752. $thirth_data['order_id'] = $order_info['order_id'];
  753. $thirth_data['third_distribution_type'] = "imdada";
  754. if($result['status'] == 1){//成功
  755. $thirth_data['status'] = 1;
  756. //平台订单号
  757. $thirth_data['delivery_no'] = $result['result']['deliveryNo'];
  758. //实际运费(单位:元)
  759. $thirth_data['shipping_fee'] = $result['result']['fee'];
  760. //运费(单位:元)
  761. $thirth_data['shipping_delivery_fee'] = $result['result']['deliverFee'];
  762. //配送距离(单位:米)
  763. $thirth_data['distance'] = $result['result']['distance'];
  764. }else{
  765. $thirth_data['status'] = 0;
  766. //平台订单号
  767. $thirth_data['delivery_no'] = "";
  768. //实际运费(单位:元)
  769. $thirth_data['shipping_fee'] = 0;
  770. //运费(单位:元)
  771. $thirth_data['shipping_delivery_fee'] = 0;
  772. //配送距离(单位:米)
  773. $thirth_data['distance'] = 0;
  774. $thirth_data['message'] = $result['message'];
  775. }
  776. $this->addThirthQuery($thirth_data);
  777. }
  778. //顺丰同城配送
  779. if($is_localtown_sf_status == 1 && $is_sf_prequery_status == 1){
  780. $sfexpress = new \Lib\Localtown\Sfexpress();
  781. $result = $sfexpress->queryDeliverFee($order_info);
  782. $thirth_data = [];
  783. $thirth_data['order_id'] = $order_info['order_id'];
  784. $thirth_data['third_distribution_type'] = "sf";
  785. if($result['status'] == 1){//成功
  786. $thirth_data['status'] = 1;
  787. //平台订单号
  788. $thirth_data['delivery_no'] = "";
  789. //实际运费(单位:元)
  790. $thirth_data['shipping_fee'] = round($result['result']['total_price']/100,2);
  791. //运费(单位:元)
  792. $thirth_data['shipping_delivery_fee'] = round($result['result']['charge_price_list']['shop_pay_price']/100,2);
  793. //配送距离(单位:米)
  794. $thirth_data['distance'] = $result['result']['delivery_distance_meter'];
  795. }else{
  796. $thirth_data['status'] = 0;
  797. //平台订单号
  798. $thirth_data['delivery_no'] = "";
  799. //实际运费(单位:元)
  800. $thirth_data['shipping_fee'] = 0;
  801. //运费(单位:元)
  802. $thirth_data['shipping_delivery_fee'] = 0;
  803. //配送距离(单位:米)
  804. $thirth_data['distance'] = 0;
  805. $thirth_data['message'] = $result['message'];
  806. }
  807. $this->addThirthQuery($thirth_data);
  808. }
  809. //如果是码科配送
  810. if( isset($is_localtown_mk_status) && $is_localtown_mk_status == 1 && isset($is_make_prequery_status) && $is_make_prequery_status == 1 )
  811. {
  812. $make_model = D('Home/Make');
  813. $result = $make_model->queryDeliverFee($order_info);
  814. $thirth_data = [];
  815. $thirth_data['order_id'] = $order_info['order_id'];
  816. $thirth_data['third_distribution_type'] = "mk";
  817. if( !empty($result) && $result['code'] == 0 )
  818. {
  819. $thirth_data['status'] = 1;
  820. //平台订单号
  821. $thirth_data['delivery_no'] = "";
  822. //实际运费(单位:元)
  823. $thirth_data['shipping_fee'] = round($result['total_price'],2);
  824. //运费(单位:元)
  825. $thirth_data['shipping_delivery_fee'] = round($result['total_price'],2);
  826. //配送距离(单位:米)
  827. $thirth_data['distance'] = $result['distance'] * 1000;
  828. }else {
  829. $thirth_data['status'] = 0;
  830. //平台订单号
  831. $thirth_data['delivery_no'] = "";
  832. //实际运费(单位:元)
  833. $thirth_data['shipping_fee'] = 0;
  834. //运费(单位:元)
  835. $thirth_data['shipping_delivery_fee'] = 0;
  836. //配送距离(单位:米)
  837. $thirth_data['distance'] = 0;
  838. $thirth_data['message'] = $result['message'];
  839. }
  840. $this->addThirthQuery($thirth_data);
  841. }
  842. }
  843. /**
  844. * @author cy 2020-12-29
  845. * @desc 保存第三方配送预查询费用信息
  846. * @param $thirth_data
  847. */
  848. public function addThirthQuery($thirth_data){
  849. $order_id = $thirth_data['order_id'];
  850. $third_distribution_type = $thirth_data['third_distribution_type'];
  851. $thirth_query = M('lionfish_comshop_orderdistribution_thirth_query')->where(array('order_id'=>$order_id,'third_distribution_type'=>$third_distribution_type))->find();
  852. $thirth_data['addtime'] = time();
  853. if(!empty($thirth_query)){
  854. M('lionfish_comshop_orderdistribution_thirth_query')->where(array('id'=>$thirth_query['id']))->save($thirth_data);
  855. }else{
  856. M('lionfish_comshop_orderdistribution_thirth_query')->add($thirth_data);
  857. }
  858. }
  859. /**
  860. * @author cy 2020-12-30
  861. * @desc 获取供应商地址信息
  862. * @param $order_info
  863. * @return array
  864. */
  865. public function getOrderStoreAddress($order_info){
  866. $store_id = $order_info['store_id'];
  867. $province_id = D('Home/Front')->get_supply_config_by_name('localtown_shop_province_id',$store_id);
  868. $city_id = D('Home/Front')->get_supply_config_by_name('localtown_shop_city_id',$store_id);
  869. $area_id = D('Home/Front')->get_supply_config_by_name('localtown_shop_area_id',$store_id);
  870. $country_id = D('Home/Front')->get_supply_config_by_name('localtown_shop_country_id',$store_id);
  871. $shop_address = D('Home/Front')->get_supply_config_by_name('localtown_shop_address',$store_id);
  872. $shop_lon = D('Home/Front')->get_supply_config_by_name('localtown_shop_lon',$store_id);
  873. $shop_lat = D('Home/Front')->get_supply_config_by_name('localtown_shop_lat',$store_id);
  874. $shop_telephone = D('Home/Front')->get_supply_config_by_name('localtown_shop_telephone',$store_id);
  875. //storename
  876. $lionfish_comshop_supply = M('lionfish_comshop_supply')->where( array('id' => $store_id) )->find();
  877. $store_data = [];
  878. $store_data['address'] = $province_id.$city_id.$area_id.$country_id.$shop_address;
  879. $store_data['city'] = $city_id;
  880. $store_data['shop_lon'] = $shop_lon;
  881. $store_data['shop_lat'] = $shop_lat;
  882. $store_data['shop_telephone'] = $shop_telephone;
  883. $store_data['begin_address'] = $shop_address;
  884. $store_data['begin_detail'] = $province_id.$city_id.$area_id.$country_id;
  885. $store_data['begin_username'] = $lionfish_comshop_supply['storename'];
  886. return $store_data;
  887. }
  888. /**
  889. * 订单自动推送给第三方配送公司
  890. * @param array $order
  891. */
  892. public function order_autosend_third_delivery($order){
  893. if($order['delivery'] == 'localtown_delivery'){//同城配送订单
  894. //供应商“第三方配送服务” 1、开启,0、关闭
  895. $supply_third_delivery_service = D('Home/Front')->get_config_by_name('supply_third_delivery_service');
  896. $is_order_push_status = D('Home/Front')->get_config_by_name('is_order_push_status');
  897. $order_push_third_distribution_company = D('Home/Front')->get_config_by_name('order_push_third_distribution_company');
  898. if($is_order_push_status == 1){//订单自动确认配送
  899. if($order['store_id'] == 0 || ($order['store_id'] > 0 && $supply_third_delivery_service == 1)){
  900. $this->order_send_third_delivery_company($order,$order_push_third_distribution_company);
  901. }
  902. }
  903. }
  904. }
  905. /**
  906. * @author cy 2021-02-25
  907. * @desc 订单自动发送给第三方配送公司
  908. * @param $order_info
  909. * @param $data_type
  910. */
  911. public function order_send_third_delivery_company($order_info,$data_type){
  912. $order_id = $order_info['order_id'];
  913. //店铺地址
  914. if($order_info['store_id'] > 0) {
  915. $store_data = D('Home/Order')->getOrderStoreAddress($order_info);
  916. $order_info['store_data'] = $store_data;
  917. }
  918. //商品信息
  919. $sql = "select og.goods_id,og.name as goods_name,og.quantity,og.price,og.total,og.rela_goodsoption_valueid,g.weight as goods_weight from "
  920. . C('DB_PREFIX')."lionfish_comshop_order_goods as og left join ".C('DB_PREFIX')."lionfish_comshop_goods as g on og.goods_id=g.id "
  921. ." where og.order_id = ".$order_id;
  922. $goods_list = M()->query($sql);
  923. $goods_count = 0;
  924. $goods_weight = 0;
  925. $goods_type_count = 0;
  926. foreach($goods_list as $k=>$v){
  927. $goods_count = $goods_count + $v['quantity'];
  928. if(!empty($v['rela_goodsoption_valueid'])){
  929. $goods_option_mult_value = M('lionfish_comshop_goods_option_item_value')->where( array('option_item_ids' => $v['rela_goodsoption_valueid'],'goods_id' => $v['goods_id']) )->find();
  930. if(!empty($goods_option_mult_value)){
  931. $v['goods_weight'] = $goods_option_mult_value['weight'];
  932. }
  933. }
  934. $goods_weight = $goods_weight + $v['quantity'] * $v['goods_weight'];
  935. $goods_type_count = $goods_type_count + 1;
  936. }
  937. if(empty($goods_weight)){
  938. $goods_weight = 100;//默认100克
  939. }
  940. $order_info['goods_list'] = $goods_list;
  941. //商品种类
  942. $order_info['goods_type_count'] = $goods_type_count;
  943. //商品数量
  944. $order_info['goods_count'] = $goods_count;
  945. //商品重量
  946. $order_info['goods_weight'] = $goods_weight;
  947. //订单总金额
  948. $order_info['order_total'] = $order_info['total']+$order_info['packing_fare']+$order_info['shipping_fare']-$order_info['voucher_credit']-$order_info['fullreduction_money']-$order_info['score_for_money']+$order_info['localtown_add_shipping_fare']-$order_info['fare_shipping_free'];
  949. //收货人地址
  950. $province_info = D('Home/Front')->get_area_info($order_info['shipping_province_id']);
  951. $city_info = D('Home/Front')->get_area_info($order_info['shipping_city_id']);
  952. $area_info = D('Home/Front')->get_area_info($order_info['shipping_country_id']);
  953. $order_info['shipping_address'] = $province_info['name'].$city_info['name'].$area_info['name'].$order_info['shipping_address'];
  954. //收货人经纬度
  955. $order_distribution_info = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  956. //收货人地址纬度
  957. $order_info['shipping_lat'] = $order_distribution_info['member_lat'];
  958. //收货人地址经度
  959. $order_info['shipping_lng'] = $order_distribution_info['member_lon'];
  960. //商城名称
  961. $shoname = D('Home/Front')->get_config_by_name('shoname');
  962. $order_info['shoname'] = $shoname;
  963. if($data_type == 'imdada'){
  964. $imdada = new \Lib\Localtown\Imdada();
  965. $result = $imdada->addOrder($order_info);
  966. if($result['status'] == 1){//成功
  967. //配送费用
  968. $delivery_fee = $result['result']['fee'];
  969. $express_info = array();
  970. $express_info['delivery_fee'] = $delivery_fee;
  971. D('Seller/Order')->do_send_localtown_thirth_delivery($order_id,$data_type,$express_info);
  972. $shipping_money = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  973. D('Seller/Supply')->update_supply_commission($order_id,$shipping_money["shipping_money"]);
  974. }
  975. }else if($data_type == 'sf'){
  976. $sfexpress = new \Lib\Localtown\Sfexpress();
  977. $result = $sfexpress->addOrder($order_info);
  978. if($result['status'] == 1){//成功
  979. //配送费用
  980. $delivery_fee = round($result['result']['total_price']/100,2);
  981. //顺丰订单号
  982. $delivery_order_id = $result['result']['sf_order_id'];
  983. //顺丰运单号
  984. $delivery_bill_id = $result['result']['sf_bill_id'];
  985. $express_info = array();
  986. $express_info['delivery_fee'] = $delivery_fee;
  987. $express_info['delivery_order_id'] = $delivery_order_id;
  988. $express_info['delivery_bill_id'] = $delivery_bill_id;
  989. D('Seller/Order')->do_send_localtown_thirth_delivery($order_id,$data_type,$express_info);
  990. $shipping_money = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  991. D('Seller/Supply')->update_supply_commission($order_id,$shipping_money["shipping_money"]);
  992. }
  993. }else if( $data_type == 'make' )
  994. {
  995. //$store_data = D('Home/Make')->getOrderStoreAddress($order_info);
  996. $result = D('Home/Make')->addOrder($order_info);
  997. if( $result['code'] == 0 )
  998. {
  999. //码科订单号
  1000. $delivery_order_id = $result['order_number'];
  1001. $express_info = array();
  1002. $express_info['delivery_fee'] = $order_distribution_info['shipping_money'];
  1003. $express_info['delivery_order_id'] = $delivery_order_id;
  1004. $express_info['delivery_bill_id'] = '';
  1005. D('Seller/Order')->do_send_localtown_thirth_delivery($order_id,$data_type,$express_info);
  1006. $shipping_money = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  1007. D('Seller/Supply')->update_supply_commission($order_id,$shipping_money["shipping_money"]);
  1008. }
  1009. }else if($data_type == 'ele'){//蜂鸟即配
  1010. $eleDistribution = new \Lib\Localtown\EleDistribution();
  1011. $order_code = build_order_no(session('user_auth.uid'));
  1012. //保存蜂鸟即配商户订单号
  1013. M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->save(['order_code'=>$order_code]);
  1014. $order_info['order_num_alias'] = $order_code;
  1015. $result = $eleDistribution->addOrder($order_info);
  1016. if($result['status'] == 1){//成功
  1017. //配送费用
  1018. $delivery_fee = round($result['result']['total_price']/100,2);
  1019. //蜂鸟即配订单号
  1020. $delivery_order_id = $result['result']['sf_order_id'];
  1021. //蜂鸟即配运单号
  1022. $delivery_bill_id = $result['result']['sf_bill_id'];
  1023. $express_info = array();
  1024. $express_info['delivery_fee'] = $delivery_fee;
  1025. $express_info['delivery_order_id'] = $delivery_order_id;
  1026. $express_info['delivery_bill_id'] = $delivery_bill_id;
  1027. D('Seller/Order')->do_send_localtown_thirth_delivery($order_id,$data_type,$express_info);
  1028. $shipping_money = M('lionfish_comshop_orderdistribution_order')->where( array('order_id' => $order_id ) )->find();
  1029. D('Seller/Supply')->update_supply_commission($order_id,$shipping_money["shipping_money"]);
  1030. }
  1031. }
  1032. }
  1033. }