Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\index\controller\shop;
  3. use addons\shop\library\KdApiExpOrder;
  4. use addons\shop\model\Order as OrderModel;
  5. use addons\shop\model\OrderAction;
  6. use addons\shop\model\OrderAftersales;
  7. use addons\shop\model\OrderGoods;
  8. use app\common\controller\Frontend;
  9. use addons\shop\model\UserCoupon;
  10. use think\Db;
  11. class Order extends Frontend
  12. {
  13. protected $layout = 'default';
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 我的订单
  18. */
  19. public function index()
  20. {
  21. $param = $this->request->param();
  22. $param['user_id'] = $this->auth->id;
  23. $param['f'] = isset($param['f']) ? $param['f'] : 0;
  24. $orderList = OrderModel::tableList($param);
  25. $this->view->assign('param', $param);
  26. $this->view->assign('orderList', $orderList);
  27. $this->view->assign('title', '我的订单');
  28. return $this->view->fetch();
  29. }
  30. /**
  31. * 订单详情
  32. */
  33. public function detail()
  34. {
  35. $order_sn = $this->request->param('orderid');
  36. if (empty($order_sn)) {
  37. $this->error('参数缺失');
  38. }
  39. $order = OrderModel::getDetail($order_sn, $this->auth->id);
  40. if (empty($order)) {
  41. $this->error('未找到订单');
  42. }
  43. $this->view->assign('title', '订单详情');
  44. $this->view->assign('orderInfo', $order);
  45. return $this->view->fetch();
  46. }
  47. /**
  48. * 查看物流
  49. */
  50. public function logistics()
  51. {
  52. $order_sn = $this->request->param('orderid');
  53. if (empty($order_sn)) {
  54. $this->error('参数缺失');
  55. }
  56. $order = OrderModel::getDetail($order_sn, $this->auth->id);
  57. if (empty($order)) {
  58. $this->error('未找到订单');
  59. }
  60. if ($this->request->isPost()) {
  61. if (!$order->shippingstate) {
  62. $this->error('订单未发货');
  63. }
  64. $electronics = Db::name('shop_order_electronics')->where('order_sn', $order_sn)->where('status', 0)->find();
  65. if (!$electronics) {
  66. $this->error('订单未发货');
  67. }
  68. $result = KdApiExpOrder::getLogisticsQuery([
  69. 'order_sn' => $order_sn,
  70. 'logistic_code' => $electronics['logistic_code'],
  71. 'shipper_code' => $electronics['shipper_code']
  72. ]);
  73. if (isset($result['Success']) && $result['Success']) {
  74. $this->success('查询成功', null, ['traces' => $result['Traces']]);
  75. } else {
  76. $this->success('查询失败');
  77. }
  78. }
  79. $this->view->assign('title', '物流详情');
  80. $this->view->assign('orderInfo', $order);
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 申请售后
  85. */
  86. public function apply()
  87. {
  88. $id = $this->request->param('id/d');
  89. if (empty($id)) {
  90. $this->error('参数缺失');
  91. }
  92. $row = \addons\shop\model\OrderGoods::get($id, ['Order']);
  93. if (!$row) {
  94. $this->error('未找到记录');
  95. }
  96. if (!in_array($row->salestate, [0, 6])) {
  97. $this->error('未审核,不能重复申请');
  98. }
  99. $order = $row->order;
  100. if (empty($order)) {
  101. $this->error('订单不存在');
  102. }
  103. if ($order->user_id != $this->auth->id) {
  104. $this->error('不可越权操作');
  105. }
  106. //未退商品
  107. $goodsNums = OrderGoods::where('order_sn', $order->order_sn)->where('salestate', 'IN', [0, 6])->count();
  108. $realprice = $row['realprice'];
  109. $config = get_addon_config('shop');
  110. if (!isset($config['shippingfeerefund']) || $config['shippingfeerefund'] == 'lastrefund') {
  111. //运费叠加至最后一个退款商品,商品退到最后一件 需要退邮费
  112. $shippingfee = $goodsNums > 1 ? 0 : $order->shippingfee;
  113. } else {
  114. //平分运费模式,数量取该订单全部商品
  115. $nums = OrderGoods::where('order_sn', $order->order_sn)->count();
  116. $shippingfee = bcmul($order->shippingfee, $nums, 2);
  117. }
  118. $refundprice = bcadd($realprice, $shippingfee, 2);
  119. if ($this->request->isPost()) {
  120. $reason = $this->request->post('reason');
  121. $images = $this->request->post('images');
  122. $expressno = $this->request->post('expressno');
  123. $type = $this->request->post('type'); //1=仅退款,2=退款退货
  124. if ($type != 1 && empty($reason)) {
  125. $this->error('请输入售后原因');
  126. }
  127. if (!in_array($type, [1, 2])) {
  128. $this->error('不存在的售后类型');
  129. }
  130. //订单正常,已发货,已支付
  131. if (in_array($order->orderstate, [0, 3, 4]) && $order->paystate && $goodsNums > 0) {
  132. if ($type != 1 && !$order->shippingstate) {
  133. $this->error('申请售后类型错误');
  134. }
  135. $reason = $type == 1 && empty($reason) ? '仅退款' : $reason;
  136. // 启动事务
  137. Db::startTrans();
  138. try {
  139. $row->salestate = $type == 1 ? 2 : 1;
  140. $row->save();
  141. $order->orderstate = 4;
  142. $order->save();
  143. //添加售后记录
  144. OrderAftersales::create([
  145. 'user_id' => $this->auth->id,
  146. 'order_id' => $order->id,
  147. 'order_goods_id' => $id,
  148. 'type' => $type,
  149. 'nums' => $row['nums'],
  150. 'reason' => $reason,
  151. 'realprice' => $realprice,
  152. 'shippingfee' => $shippingfee,
  153. 'refund' => $refundprice,
  154. 'images' => $images,
  155. ]);
  156. // 提交事务
  157. Db::commit();
  158. } catch (\Exception $e) {
  159. // 回滚事务
  160. Db::rollback();
  161. $this->error('申请失败');
  162. }
  163. //记录操作
  164. OrderAction::push($order->order_sn, '用户', $type == 1 ? '申请退款' : '用户申请退款退货');
  165. $this->success('申请售后成功,等待审核', $order->url);
  166. }
  167. $this->error('不允许的退款订单');
  168. }
  169. $this->view->assign('title', '申请售后');
  170. $this->view->assign('orderInfo', $order);
  171. $this->view->assign('goodsInfo', $row);
  172. $this->view->assign('refundprice', $refundprice);
  173. return $this->view->fetch();
  174. }
  175. /**
  176. * 查看售后状态
  177. */
  178. public function aftersale()
  179. {
  180. $id = $this->request->param('id');
  181. if (empty($id)) {
  182. $this->error('参数缺失');
  183. }
  184. $afterSaleInfo = OrderAftersales::with(['OrderGoods'])->where('order_goods_id', $id)->order('id desc')->where('user_id', $this->auth->id)->find();
  185. if (empty($afterSaleInfo)) {
  186. $this->error('未找到记录');
  187. }
  188. $this->view->assign('title', '售后状态');
  189. $this->view->assign('aftersaleInfo', $afterSaleInfo);
  190. $this->view->assign('goodsInfo', $afterSaleInfo->order_goods);
  191. return $this->view->fetch();
  192. }
  193. /**
  194. * 取消订单
  195. */
  196. public function cancel()
  197. {
  198. $order_sn = $this->request->post('orderid');
  199. if (!$order_sn) {
  200. $this->error('参数错误');
  201. }
  202. $order = OrderModel::getByOrderSn($order_sn);
  203. if (empty($order)) {
  204. $this->error('订单不存在');
  205. }
  206. if ($order->user_id != $this->auth->id) {
  207. $this->error('不能越权操作');
  208. }
  209. if ($order->status == 'hidden') {
  210. $this->error('订单已失效!');
  211. }
  212. //可以取消
  213. if (!$order->paystate && !$order->orderstate) {
  214. // 启动事务
  215. Db::startTrans();
  216. try {
  217. $order->orderstate = 1;
  218. $order->canceltime = time();
  219. $order->save();
  220. foreach ($order->order_goods as $item) {
  221. $sku = $item->sku;
  222. $goods = $item->goods;
  223. //商品库存恢复
  224. if ($sku) {
  225. $sku->setInc('stocks', $item->nums);
  226. }
  227. if ($goods) {
  228. $goods->setInc('stocks', $item->nums);
  229. }
  230. }
  231. //恢复优惠券
  232. UserCoupon::resetUserCoupon($order->user_coupon_id, $order->order_sn);
  233. // 提交事务
  234. Db::commit();
  235. } catch (\Exception $e) {
  236. // 回滚事务
  237. Db::rollback();
  238. $this->error('订单取消失败');
  239. }
  240. //记录操作
  241. OrderAction::push($order->order_sn, '系统', '订单取消成功');
  242. $this->success('订单取消成功!', $order['status']);
  243. } else {
  244. $this->error('订单不允许取消');
  245. }
  246. }
  247. //保存快递信息
  248. public function saveexpress()
  249. {
  250. $id = $this->request->post('id');
  251. $expressname = $this->request->post('expressname');
  252. $expressno = $this->request->post('expressno');
  253. if (empty($id)) {
  254. $this->error('参数缺失');
  255. }
  256. if (empty($expressname)) {
  257. $this->error('快递名称不能为空');
  258. }
  259. if (empty($expressno)) {
  260. $this->error('快递单号不能为空');
  261. }
  262. $row = OrderAftersales::where('order_goods_id', $id)->order('id desc')->where('user_id', $this->auth->id)->find();
  263. if (empty($row)) {
  264. $this->error('未找到记录');
  265. }
  266. $row->expressname = $expressname;
  267. $row->expressno = $expressno;
  268. $row->save();
  269. $this->success('保存成功');
  270. }
  271. /**
  272. * 确认收货
  273. */
  274. public function takedelivery()
  275. {
  276. $order_sn = $this->request->post('orderid');
  277. if (!$order_sn) {
  278. $this->error('参数错误');
  279. }
  280. $order = OrderModel::getByOrderSn($order_sn);
  281. if (empty($order)) {
  282. $this->error('订单不存在');
  283. }
  284. if ($order->user_id != $this->auth->id) {
  285. $this->error('不能越权操作');
  286. }
  287. if ($order->status == 'hidden') {
  288. $this->error('订单已失效!');
  289. }
  290. if ($order->paystate == 1 && !$order->orderstate && $order->shippingstate == 1) {
  291. $order->shippingstate = 2;
  292. $order->save();
  293. //记录操作
  294. OrderAction::push($order->order_sn, '系统', '订单确认收货成功');
  295. $this->success('确认收货成功');
  296. }
  297. $this->error('未可确认收货');
  298. }
  299. }