OrderModel.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 Admin\Model;
  15. class OrderModel{
  16. /**
  17. *显示订单状态单位分页
  18. */
  19. public function show_order_page($search, $is_daochu = false){
  20. $sql="SELECT o.order_id,o.type,o.telephone,o.order_num_alias,o.total,o.lottery_win,o.delivery,o.date_added,o.is_zhuli,o.is_balance,og.member_disc,og.level_name,og.head_disc,og.is_pin,og.quantity,o.shipping_method,o.shipping_no,o.shipping_name,o.shipping_tel,o.shipping_province_id,o.shipping_city_id,o.shipping_country_id,o.shipping_address,og.pin_id,o.ip_region,o.payment_code,o.shipping_method,o.date_added,o.comment,o.date_modified,m.uname,os.order_status_id,os.name,og.store_id FROM "
  21. .C('DB_PREFIX').'order o,'.C('DB_PREFIX').'order_goods as og,'.C('DB_PREFIX').'member m,'.C('DB_PREFIX').'order_status os WHERE o.member_id=m.member_id AND '
  22. .'o.order_status_id=os.order_status_id and og.order_id =o.order_id ';
  23. if(isset($search['order_num'])){
  24. $sql.=" and o.order_num_alias='".$search['order_num']."'";
  25. }
  26. //member_id
  27. if(isset($search['member_id']) && !empty($search['member_id'])){
  28. $sql.=" and m.member_id='".$search['member_id']."'";
  29. }
  30. //shipping_tel
  31. if(isset($search['shipping_tel']) && !empty($search['shipping_tel'])){
  32. $sql.=" and o.shipping_tel like '%".$search['shipping_tel']."%'";
  33. }
  34. //telephone
  35. if(isset($search['telephone']) && !empty($search['telephone'])){
  36. $sql.=" and o.telephone like '%".$search['telephone']."%'";
  37. }
  38. //shipping_no
  39. if(isset($search['shipping_no']) && !empty($search['shipping_no'])){
  40. $sql.=" and o.shipping_no like '%".$search['shipping_no']."%'";
  41. }
  42. if(isset($search['transaction_id']) && !empty($search['transaction_id'])){
  43. $sql.=" and o.transaction_id='".$search['transaction_id']."'";
  44. }
  45. //delivery
  46. if(isset($search['delivery']) && !empty($search['delivery'])){
  47. $sql.=" and o.delivery='".$search['delivery']."'";
  48. }
  49. //shipping_no
  50. if(isset($search['goods_id']) && !empty($search['goods_id'])){
  51. $sql.=" and og.goods_id='".$search['goods_id']."'";
  52. }
  53. //is_pin
  54. if(isset($search['is_pin']) && $search['is_pin'] >= 0){
  55. $sql.=" and o.is_pin='".$search['is_pin']."'";
  56. }
  57. //date_added_begin
  58. if(isset($search['date_added_begin']) && $search['date_added_begin'] >= 0){
  59. $sql.=" and o.date_added >=".$search['date_added_begin'];
  60. }
  61. if(isset($search['date_added_end']) && $search['date_added_end'] >= 0){
  62. $sql.=" and o.date_added <=".$search['date_added_end'];
  63. }
  64. if(isset($search['shipping_name']) && !empty($search['shipping_name']) ){
  65. //
  66. $sql.=" and o.shipping_name like '%".$search['shipping_name']."%'";
  67. }
  68. if(isset($search['user_name']) && !empty($search['user_name'])){
  69. //
  70. $sql.=" and o.shipping_name like '%".$search['user_name']."%'";
  71. }
  72. if(isset($search['status'])){
  73. $sql.=" and os.order_status_id=".$search['status'];
  74. }
  75. if(isset($search['store_id']))
  76. {
  77. $sql.=" and og.store_id=".$search['store_id'];
  78. }
  79. if(isset($search['order_status_id']))
  80. {
  81. if($search['order_status_id'] == 999)
  82. {
  83. $sql.=" and o.is_balance = 0 and o.order_status_id in (1,4,6,11) ";
  84. } else
  85. {
  86. $sql.=" and o.order_status_id=".$search['order_status_id'];
  87. if( $search['order_status_id'] == 1)
  88. {
  89. $sql.=" and (o.type='normal' or o.type='pintuan' or ( o.type='lottery' and o.lottery_win =1 ) ) ";
  90. }
  91. }
  92. }
  93. $count=count(M()->query($sql));
  94. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  95. $show = $Page->show();// 分页显示输出
  96. if($is_daochu) {
  97. //导出excel
  98. $sql.=' group by o.order_id ORDER BY o.order_id DESC ';
  99. } else {
  100. $sql.=' group by o.order_id ORDER BY o.order_id DESC LIMIT '.$Page->firstRow.','.$Page->listRows;
  101. }
  102. $list=M()->query($sql);
  103. $pin_model = D('Home/Pin');
  104. foreach($list as $key => $val)
  105. {
  106. $val['pin_state'] = 0;
  107. $order_goods_info_list = M('order_goods')->field('order_goods_id,name')->where( array('order_id' => $val['order_id']) )->select();
  108. $name_arr = array();
  109. foreach($order_goods_info_list as $order_goods_info)
  110. {
  111. $order_goods_id = $order_goods_info['order_goods_id'];
  112. $option_list = M('order_option')->where( array('order_goods_id' =>$order_goods_id,'order_id'=> $val['order_id']) )->select();
  113. if(!empty($option_list))
  114. {
  115. $str = '规格:';
  116. foreach ($option_list as $option) {
  117. $str .= $option['name'].': '.$option['value'].' ';
  118. }
  119. $name_arr[] = $order_goods_info['name'] .= $str;
  120. } else {
  121. $name_arr[] = $order_goods_info['name'];
  122. }
  123. }
  124. $val['goods_name'] = implode('<br/>',$name_arr);
  125. //store_id
  126. $seller_info = M('seller')->field('s_true_name')->where( array('s_id'=>$val['store_id']) )->find();
  127. $val['s_true_name'] = $seller_info['s_true_name'];
  128. //ordertype
  129. if($val['is_pin'] == 1)
  130. {
  131. //pin_order $val['order_id']
  132. $pin_order_info = M('pin_order')->where( array('order_id' => $val['order_id'] ) )->find();
  133. $pin_id = $pin_order_info['pin_id'];
  134. $state = $pin_model->getNowPinState($pin_id);
  135. $pin_info = M('pin')->where( array('pin_id' => $pin_id) )->find();
  136. $str = '';
  137. if($state == 1)
  138. {
  139. $str .='<a class="btn btn-xs btn-info" href='.U("Pin/show_order",array("pin_id"=>$pin_id)).'>';
  140. $str .= $pin_info['need_count'].'人团 拼团id:'.$pin_id.'</a><br/><span class="label label-success ">已成团</span>';
  141. } else if($state == 2)
  142. {
  143. $str .='<a class="btn btn-xs btn-info" href='.U("Pin/show_order",array("pin_id"=>$pin_id)).'>';
  144. $str .= '拼团';
  145. $str .= '</a>';
  146. } else {
  147. $str = '<span class="label label-success">进行中</span>';
  148. }
  149. //Pin/show_order/pin_id/484
  150. $val['pin_state'] = $state;
  151. $val['ordertype'] = $str;
  152. //已完成 拼团中,已失败
  153. //<span class="label label-info arrowed-in-right arrowed">{$v.ordertype}</span>
  154. // </a>
  155. } else {
  156. $val['ordertype'] = '<span class="badge badge-info">单独购买</span>';
  157. }
  158. if($val['type'] == 'integral')
  159. {
  160. $integral_order = M('integral_order')->field('score')->where( array('order_id' => $val['order_id']) )->find();
  161. $val['score'] = intval($integral_order['score']);
  162. }
  163. $list[$key] = $val;
  164. }
  165. return array(
  166. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  167. 'list'=>$list,
  168. 'page'=>$show
  169. );
  170. }
  171. //订单信息
  172. public function order_info($id){
  173. //订单详情
  174. $order_sql="SELECT o.*,m.uname,m.email,o.shipping_tel,os.name,o.shipping_address as address FROM "
  175. .C('DB_PREFIX').'order o,'.C('DB_PREFIX').'member m, '
  176. .C('DB_PREFIX').'order_status os WHERE o.member_id=m.member_id and '
  177. .'o.order_status_id=os.order_status_id AND o.order_id='.$id;
  178. $order=M()->query($order_sql);
  179. //商品清单
  180. $order_product=M('order_goods')->where('order_id='.$id)->select();
  181. foreach($order_product as $key => $val)
  182. {
  183. $commiss_list = M('member_commiss_order')->where( array('order_goods_id' => $val['order_goods_id'], 'order_id' =>$val['order_id']) )->order()->select();
  184. if(!empty($commiss_list))
  185. {
  186. foreach($commiss_list as $kk => $vv)
  187. {
  188. //member_id
  189. $mb_info = M('member')->field('uname')->where( array('member_id' => $vv['member_id']) )->find();
  190. $vv['uname'] = $mb_info['uname'];
  191. $commiss_list[$kk] = $vv;
  192. }
  193. }
  194. $val['commiss_list'] = $commiss_list;
  195. $share_list = M('member_sharing_order')->where( array('order_goods_id' => $val['order_goods_id'], 'order_id' =>$val['order_id']) )->order()->select();
  196. if(!empty($share_list))
  197. {
  198. foreach($share_list as $kk => $vv)
  199. {
  200. //member_id
  201. $mb_info = M('member')->field('uname')->where( array('member_id' => $vv['member_id']) )->find();
  202. $vv['uname'] = $mb_info['uname'];
  203. $share_list[$kk] = $vv;
  204. }
  205. }
  206. $val['share_list'] = $share_list;
  207. $order_product[$key] = $val;
  208. }
  209. //价格、运费
  210. $order_total = M()->query("SELECT * FROM " .C('DB_PREFIX').
  211. "order_total WHERE order_id =" .$id." ORDER BY sort_order");
  212. //订单状态
  213. $order_statuses=M('OrderStatus')->select();
  214. //订单历史
  215. $order_history=M('order_history')->where(array('order_id'=>$id))->select();
  216. return array(
  217. 'order'=>$order[0],
  218. 'order_product'=>$order_product,
  219. 'order_total'=>$order_total,
  220. 'order_statuses'=>$order_statuses,
  221. 'order_history'=>$order_history
  222. );
  223. }
  224. function addOrderHistory($order_id, $data) {
  225. $order['order_id']=$order_id;
  226. $order['date_modified']=time();
  227. if($data['order_status_id'] == 4)
  228. {
  229. $order['shipping_method']=$data['shipping_method'];
  230. $order['shipping_no']=$data['shipping_no'];
  231. }
  232. $order['order_status_id']=$data['order_status_id'];
  233. M('Order')->save($order);
  234. if($data['order_status_id'] == 4)
  235. {
  236. $order_info = M('order')->where( array('order_id' => $order_id) )->find();
  237. $notify_model = D('Home/Weixinnotify');
  238. //$integral_model = D('Seller/Integral');
  239. //$integral_model->send_order_score_dr($order_id);
  240. if($order_info['delivery'] == 'pickup')
  241. {
  242. $notify_model->sendPickupMsg($order_id);
  243. } else {
  244. $notify_model->sendExpressMsg($order_id);
  245. $ebuss_info = M('config')->where( array('name' => 'EXPRESS_EBUSS_ID') )->find();
  246. if(!empty($ebuss_info['value']))
  247. {
  248. $w=new \Lib\Kuaidiniao();
  249. $rs = $w->subscribe($order_id);
  250. }
  251. }
  252. }
  253. if($data['order_status_id'] == 7)
  254. {
  255. $weixin_model = D('Home/Weixin');
  256. $weixin_model->refundOrder($order_id);
  257. }
  258. $oh['order_id']=$order_id;
  259. $oh['order_status_id']=$data['order_status_id'];
  260. $oh['notify']=(isset($data['notify']) ? (int)$data['notify'] : 0) ;
  261. $oh['comment']=strip_tags($data['comment']);
  262. $oh['date_added']=time();
  263. $oh_id=M('OrderHistory')->add($oh);
  264. return $oh_id;
  265. }
  266. public function getOrderHistories($order_id) {
  267. $query = M()->query("SELECT oh.date_added, os.name AS status, oh.comment, oh.notify FROM "
  268. . C('DB_PREFIX') . "order_history oh LEFT JOIN "
  269. . C('DB_PREFIX') . "order_status os ON oh.order_status_id = os.order_status_id WHERE oh.order_id = '" . (int)$order_id
  270. . "' ORDER BY oh.date_added ASC");
  271. return $query;
  272. }
  273. function del_order($id){
  274. M('order')->where(array('order_id'=>$id))->delete();
  275. M('order_goods')->where(array('order_id'=>$id))->delete();
  276. M('order_history')->where(array('order_id'=>$id))->delete();
  277. M('order_total')->where(array('order_id'=>$id))->delete();
  278. return array(
  279. 'status'=>'success',
  280. 'message'=>'删除成功',
  281. 'jump'=>U('Order/index')
  282. );
  283. }
  284. }
  285. ?>