OrderModel.class.php 48 KB

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