You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\ReturnData;
  4. use DB;
  5. class Order extends BaseModel
  6. {
  7. //购物车模型
  8. /**
  9. * 关联到模型的数据表
  10. *
  11. * @var string
  12. */
  13. protected $table = 'order';
  14. public $timestamps = false;
  15. //获取订单列表
  16. public static function getList(array $param)
  17. {
  18. extract($param);
  19. $limit = isset($limit) ? $limit : 10;
  20. $offset = isset($offset) ? $offset : 0;
  21. $model = new self();
  22. if(isset($user_id)){$where['user_id'] = $user_id;}
  23. $where['is_delete'] = 0;
  24. //0或者不传表示全部,1待付款,2待发货,3待收货,4待评价(确认收货,交易成功),5退款/售后
  25. if($status == 1)
  26. {
  27. $where['order_status'] = 0;
  28. $where['pay_status'] = 0;
  29. }
  30. elseif($status == 2)
  31. {
  32. $where['order_status'] = 0;
  33. $where['shipping_status'] = 0;
  34. $where['pay_status'] = 1;
  35. }
  36. elseif($status == 3)
  37. {
  38. $where['order_status'] = 0;
  39. $where['refund_status'] = 0;
  40. $where['shipping_status'] = 1;
  41. $where['pay_status'] = 1;
  42. }
  43. elseif($status == 4)
  44. {
  45. $where['order_status'] = 3;
  46. $where['refund_status'] = 0;
  47. $where['shipping_status'] = 2;
  48. $where['is_comment'] = 0;
  49. }
  50. elseif($status == 5)
  51. {
  52. $where['order_status'] = 3;
  53. $model = $model->where('refund_status','<>',0);
  54. }
  55. $model = $model->where($where);
  56. $res['count'] = $model->count();
  57. $res['list'] = array();
  58. if($res['count']>0)
  59. {
  60. $order_list = $model->orderBy('id', 'desc')->skip($offset)->take($limit)->get();
  61. if($order_list)
  62. {
  63. foreach($order_list as $k=>$v)
  64. {
  65. $order_status_arr = self::getOrderStatusText($v);
  66. $order_list[$k]['order_status_text'] = $order_status_arr?$order_status_arr['text']:'';
  67. $order_list[$k]['order_status_num'] = $order_status_arr?$order_status_arr['num']:'';
  68. $order_list[$k]['province_name'] = Region::getRegionName($v['province']);
  69. $order_list[$k]['city_name'] = Region::getRegionName($v['city']);
  70. $order_list[$k]['district_name'] = Region::getRegionName($v['district']);
  71. $order_goods = OrderGoods::where(array('order_id'=>$v['id']))->get();
  72. $order_list[$k]['goods_list'] = $order_goods;
  73. }
  74. }
  75. $res['list'] = $order_list;
  76. }
  77. return ReturnData::create(ReturnData::SUCCESS,$res);
  78. }
  79. public static function getOne(array $param)
  80. {
  81. extract($param);
  82. $where['id'] = $order_id;
  83. $where['user_id'] = $user_id;
  84. if(isset($order_status)){$where['order_status'] = $order_status;}
  85. if(isset($pay_status)){$where['pay_status'] = $pay_status;}
  86. $res = self::where($where)->first();
  87. if(!$res)
  88. {
  89. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  90. }
  91. $order_status_arr = self::getOrderStatusText($res);
  92. $res['order_status_text'] = $order_status_arr['text'];
  93. $res['order_status_num'] = $order_status_arr['num'];
  94. $res['province_name'] = Region::getRegionName($res['province']);
  95. $res['city_name'] = Region::getRegionName($res['city']);
  96. $res['district_name'] = Region::getRegionName($res['district']);
  97. $order_goods = OrderGoods::where(array('order_id'=>$res['id']))->get();
  98. $res['goods_list'] = $order_goods;
  99. return ReturnData::create(ReturnData::SUCCESS,$res);
  100. }
  101. //生成订单
  102. public static function add(array $param)
  103. {
  104. extract($param);
  105. //获取订单商品列表
  106. $cartCheckoutGoods = Cart::cartCheckoutGoodsList(array('ids'=>$cartids,'user_id'=>$user_id));
  107. $order_goods = $cartCheckoutGoods['data'];
  108. if(empty($order_goods['list'])){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品不存在');}
  109. //获取收货地址
  110. $user_address = UserAddress::getOne($user_id,$default_address_id);
  111. if(!$user_address){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'收货地址不存在');}
  112. //获取优惠券信息
  113. $user_bonus = UserBonus::getUserBonusByid(array('user_bonus_id'=>$user_bonus_id,'user_id'=>$user_id));
  114. $discount = !empty($user_bonus)?$user_bonus['money']:0.00; //优惠金额=优惠券
  115. $order_amount = $order_goods['total_price'] - $discount;
  116. $pay_status = 0; //未付款
  117. //如果各种优惠金额大于订单实际金额跟运费之和,则默认订单状态为已付款
  118. if($order_amount < 0)
  119. {
  120. $order_amount = 0;
  121. $pay_status = 1; //已付款
  122. }
  123. //构造订单字段
  124. $order_info = array(
  125. 'order_sn' => date('YmdHis').rand(1000,9999),
  126. 'add_time' => time(),
  127. 'pay_status' => $pay_status,
  128. 'user_id' => $user_id,
  129. 'goods_amount' => $order_goods['total_price'], //商品的总金额
  130. 'order_amount' => $order_amount, //应付金额=商品总价+运费-优惠(积分、红包)
  131. 'discount' => $discount, //优惠金额
  132. 'name' => $user_address['name'],
  133. //'country' => $user_address['country'],
  134. 'province' => $user_address['province'],
  135. 'city' => $user_address['city'],
  136. 'district' => $user_address['district'],
  137. 'address' => $user_address['address'],
  138. 'zipcode' => $user_address['zipcode'],
  139. 'mobile' => $user_address['mobile'],
  140. 'place_type' => $place_type, //订单来源
  141. 'bonus_id' => !empty($user_bonus)?$user_bonus['id']:0,
  142. 'bonus_money' => !empty($user_bonus)?$user_bonus['money']:0.00,
  143. 'message' => !empty($message)?$message:'',
  144. );
  145. //插入订单
  146. $order_id = self::insertGetId($order_info);
  147. if ($order_id)
  148. {
  149. //订单生成成功之后,扣除用户的积分和改变优惠券的使用状态
  150. //改变优惠券使用状态
  151. UserBonus::where(array('user_id'=>$user_id,'id'=>$user_bonus_id))->update(array('status'=>1,'used_time'=>time()));
  152. //扣除用户积分,预留
  153. //$updateMember['validscore'] = $addressInfo['validscore']-$PointPay;
  154. //M("Member")->where(array('id'=>$CustomerSysNo))->save($updateMember);
  155. //增加一条积分支出记录,一条购物获取积分记录
  156. //插入订单商品
  157. $order_goods_list = array();
  158. foreach($order_goods['list'] as $k=>$v)
  159. {
  160. $temp_order_goods = array(
  161. 'order_id' => $order_id,
  162. 'goods_id' => $v['goods_id'],
  163. 'goods_name' => $v['title'],
  164. 'goods_number' => $v['goods_number'],
  165. 'market_price' => $v['market_price'],
  166. 'goods_price' => $v['final_price'],
  167. //'goods_attr' => '', //商品属性,预留
  168. 'goods_img' => $v['litpic']
  169. );
  170. array_push($order_goods_list,$temp_order_goods);
  171. //订单商品直接减库存操作
  172. Goods::changeGoodsStock(array('goods_id'=>$v['goods_id'],'goods_number'=>$v['goods_number']));
  173. }
  174. $result = DB::table('order_goods')->insert($order_goods_list);
  175. if($result)
  176. {
  177. //删除购物对应的记录
  178. Cart::where(array('user_id'=>$user_id))->whereIn('id', explode("_",$cartids))->delete();
  179. return ReturnData::create(ReturnData::SUCCESS,$order_id);
  180. }
  181. else
  182. {
  183. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'订单商品添加失败');
  184. }
  185. }
  186. else
  187. {
  188. return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'生成订单失败');
  189. }
  190. }
  191. public static function modify($where, array $data)
  192. {
  193. if (self::where($where)->update($data) === false)
  194. {
  195. return false;
  196. }
  197. return true;
  198. }
  199. //删除一条记录
  200. public static function remove($id,$user_id)
  201. {
  202. if(!is_array($id)){$id = explode(',', $id);}
  203. if (self::whereIn('id', $id)->where('user_id',$user_id)->delete() === false)
  204. {
  205. return false;
  206. }
  207. return true;
  208. }
  209. //获取订单状态文字:1待付款,2待发货,3待收货,4待评价(确认收货,交易成功),5退款/售后,6已取消,7无效
  210. public static function getOrderStatusText($where)
  211. {
  212. $res = '';
  213. if($where['order_status'] == 0 && $where['pay_status'] ==0)
  214. {
  215. $res = array('text'=>'待付款','num'=>1);
  216. }
  217. elseif($where['order_status'] == 0 && $where['shipping_status'] == 0 && $where['pay_status'] == 1)
  218. {
  219. $res = array('text'=>'待发货','num'=>2);
  220. }
  221. elseif($where['order_status'] == 0 && $where['refund_status'] == 0 && $where['shipping_status'] == 1 && $where['pay_status'] == 1)
  222. {
  223. $res = array('text'=>'待收货','num'=>3);
  224. }
  225. elseif($where['order_status'] == 3 && $where['refund_status'] == 0)
  226. {
  227. $res = array('text'=>'交易成功','num'=>4);
  228. }
  229. elseif($where['order_status'] == 3 && $where['refund_status'] == 1)
  230. {
  231. $res = array('text'=>'售后中','num'=>5);
  232. }
  233. elseif($where['order_status'] == 1)
  234. {
  235. $res = array('text'=>'已取消','num'=>6);
  236. }
  237. elseif($where['order_status'] == 2)
  238. {
  239. $res = array('text'=>'无效','num'=>7);
  240. }
  241. elseif($where['order_status'] == 3 && $where['refund_status'] == 2)
  242. {
  243. $res = array('text'=>'退款成功','num'=>8);
  244. }
  245. return $res;
  246. }
  247. //获取发票类型文字:0不索要发票,1个人,2企业
  248. public static function getInvoiceText($where)
  249. {
  250. $res = '';
  251. if($where['invoice'] == 0)
  252. {
  253. $res = '不索要发票';
  254. }
  255. elseif($where['invoice'] == 1)
  256. {
  257. $res = '个人';
  258. }
  259. elseif($where['invoice'] == 2)
  260. {
  261. $res = '企业';
  262. }
  263. return $res;
  264. }
  265. //获取订单来源文字:1pc,2weixin,3app,4wap
  266. public static function getPlaceTypeText($where)
  267. {
  268. $res = '';
  269. if($where['place_type'] === 1)
  270. {
  271. $res = 'pc';
  272. }
  273. elseif($where['place_type'] === 2)
  274. {
  275. $res = 'weixin';
  276. }
  277. elseif($where['place_type'] === 3)
  278. {
  279. $res = 'app';
  280. }
  281. elseif($where['place_type'] === 4)
  282. {
  283. $res = 'wap';
  284. }
  285. return $res;
  286. }
  287. //根据订单id返库存
  288. public static function returnStock($order_id)
  289. {
  290. $order_goods = OrderGoods::where(array('order_id'=>$order_id))->get();
  291. if(!$order_goods){return false;}
  292. foreach($order_goods as $k=>$v)
  293. {
  294. //订单商品直接返库存
  295. Goods::changeGoodsStock(array('goods_id'=>$v['goods_id'],'goods_number'=>$v['goods_number'],'type'=>1));
  296. }
  297. return true;
  298. }
  299. //订单超时,设为无效
  300. public static function orderSetInvalid($order_id)
  301. {
  302. $order = self::where(array('id'=>$order_id,'order_status'=>0,'pay_status'=>0))->update(['order_status'=>2]);
  303. if(!$order){return false;}
  304. //返库存
  305. self::returnStock($order_id);
  306. return true;
  307. }
  308. }