StoreOrderController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\admin\model\ump\WriteOff;
  4. use app\Request;
  5. use app\models\user\{
  6. User, UserBill
  7. };
  8. use crmeb\repositories\OrderRepository;
  9. use crmeb\repositories\ShortLetterRepositories;
  10. use crmeb\services\{
  11. MiniProgramService, UtilService, WechatService
  12. };
  13. use app\models\store\{StoreCart, StoreCoupon, StoreCouponUser, StoreOrder, StoreOrderStatus, StorePink, StoreService};
  14. use app\models\system\SystemStoreStaff;
  15. /**
  16. * 订单类
  17. * Class StoreOrderController
  18. * @package app\api\controller\admin\order
  19. */
  20. class StoreOrderController
  21. {
  22. /**
  23. * 订单数据统计
  24. * @param Request $request
  25. * @return mixed
  26. */
  27. public function statistics(Request $request)
  28. {
  29. $uid = $request->uid();
  30. if (!StoreService::orderServiceStatus($uid))
  31. return app('json')->fail('权限不足');
  32. $dataCount = StoreOrder::getOrderDataAdmin();
  33. $dataPrice = StoreOrder::getOrderTimeData();
  34. $data = array_merge($dataCount, $dataPrice);
  35. return app('json')->successful($data);
  36. }
  37. /**
  38. * 订单每月统计数据
  39. * @param Request $request
  40. * @return mixed
  41. */
  42. public function data(Request $request)
  43. {
  44. $uid = $request->uid();
  45. if (!StoreService::orderServiceStatus($uid))
  46. return app('json')->fail('权限不足');
  47. list($page, $limit, $start, $stop) = UtilService::getMore([
  48. ['page', 1],
  49. ['limit', 7],
  50. ['start', ''],
  51. ['stop', '']
  52. ], $request, true);
  53. if (!$limit) return app('json')->successful([]);
  54. $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop);
  55. if ($data) return app('json')->successful($data->toArray());
  56. return app('json')->successful([]);
  57. }
  58. /**
  59. * 订单列表
  60. * @param Request $request
  61. * @return mixed
  62. */
  63. public function lst(Request $request)
  64. {
  65. $uid = $request->uid();
  66. if (!StoreService::orderServiceStatus($uid))
  67. return app('json')->fail('权限不足');
  68. $where = UtilService::getMore([
  69. ['status', ''],
  70. ['is_del', 0],
  71. ['data', ''],
  72. ['type', ''],
  73. ['order', ''],
  74. ['page', 0],
  75. ['limit', 0]
  76. ], $request);
  77. if (!$where['limit']) return app('json')->successful([]);
  78. return app('json')->successful(StoreOrder::orderList($where));
  79. }
  80. /**
  81. * 订单详情
  82. * @param Request $request
  83. * @param $orderId
  84. * @return mixed
  85. * @throws \think\Exception
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public function detail(Request $request, $orderId)
  91. {
  92. $uid = $request->uid();
  93. if (!StoreService::orderServiceStatus($uid))
  94. return app('json')->fail('权限不足');
  95. $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id');
  96. if (!$order) return app('json')->fail('订单不存在');
  97. $order = $order->toArray();
  98. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  99. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  100. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  101. $orderInfo['nickname'] = $nickname;
  102. return app('json')->successful('ok', $orderInfo);
  103. }
  104. /**
  105. * 订单发货获取订单信息
  106. * @param Request $request
  107. * @param $orderId
  108. * @return mixed
  109. * @throws \think\Exception
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. public function delivery_gain(Request $request, $orderId)
  115. {
  116. $uid = $request->uid();
  117. if (!StoreService::orderServiceStatus($uid))
  118. return app('json')->fail('权限不足');
  119. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid');
  120. if (!$order) return app('json')->fail('订单不存在');
  121. if ($order['paid']) {
  122. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  123. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  124. return app('json')->successful('ok', $order);
  125. }
  126. return app('json')->fail('状态错误');
  127. }
  128. /**
  129. * 订单发货
  130. * @param Request $request
  131. * @return mixed
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public function delivery_keep(Request $request)
  137. {
  138. $uid = $request->uid();
  139. if (!StoreService::orderServiceStatus($uid))
  140. return app('json')->fail('权限不足');
  141. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  142. ['order_id', ''],
  143. ['delivery_type', 0],
  144. ['delivery_name', ''],
  145. ['delivery_id', ''],
  146. ], $request, true);
  147. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid');
  148. if (!$order) return app('json')->fail('订单不存在');
  149. if (!$order['status'] && $order['paid']) {
  150. $deliveryTypeArr = ['send', 'express', 'fictitious'];
  151. if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  152. if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  153. if ($delivery_type == 'express') {
  154. if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  155. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  156. }
  157. if ($delivery_type == 'send') {
  158. if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  159. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  160. }
  161. $data['status'] = 1;
  162. $data['delivery_type'] = $delivery_type;
  163. $data['delivery_name'] = $delivery_name;
  164. $data['delivery_id'] = $delivery_id;
  165. $res = StoreOrder::edit($data, $order['id'], 'id');
  166. if ($res) {
  167. if ($delivery_type == 'express') {
  168. StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  169. } else if ($delivery_type == 'send') {
  170. StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
  171. } else if ($delivery_type == 'fictitious') {
  172. StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
  173. }
  174. }
  175. event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
  176. return app('json')->successful('发货成功!');
  177. }
  178. return app('json')->fail('状态错误');
  179. }
  180. /**
  181. * 订单改价
  182. * @param Request $request
  183. * @return mixed
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @throws \think\exception\DbException
  187. */
  188. public function price(Request $request)
  189. {
  190. $uid = $request->uid();
  191. if (!StoreService::orderServiceStatus($uid))
  192. return app('json')->fail('权限不足');
  193. list($order_id, $price) = UtilService::postMore([
  194. ['order_id', ''],
  195. ['price', '']
  196. ], $request, true);
  197. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral');
  198. if (!$order) return app('json')->fail('订单不存在');
  199. $order = $order->toArray();
  200. if (!$order['paid']) {
  201. if ($price === '') return app('json')->fail('请填写实际支付金额');
  202. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  203. if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
  204. $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
  205. if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
  206. return app('json')->fail('状态错误');
  207. $order['pay_price'] = $price;
  208. event('StoreProductOrderEditAfter', [$order, $order['id']]);
  209. StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
  210. //改价短信提醒
  211. if ($price != $order['pay_price']) {
  212. $switch = sys_config('price_revision_switch') ? true : false;
  213. ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
  214. }
  215. return app('json')->successful('修改成功');
  216. }
  217. return app('json')->fail('状态错误');
  218. }
  219. /**
  220. * 订单备注
  221. * @param Request $request
  222. * @return mixed
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. * @throws \think\exception\DbException
  226. */
  227. public function remark(Request $request)
  228. {
  229. $uid = $request->uid();
  230. if (!StoreService::orderServiceStatus($uid))
  231. return app('json')->fail('权限不足');
  232. list($order_id, $remark) = UtilService::postMore([
  233. ['order_id', ''],
  234. ['remark', '']
  235. ], $request, true);
  236. $order = StoreOrder::getAdminOrderDetail($order_id, 'id');
  237. if (!$order) return app('json')->fail('订单不存在');
  238. $order = $order->toArray();
  239. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  240. if (!StoreOrder::edit(['remark' => $remark], $order['id']))
  241. return app('json')->fail('备注失败');
  242. return app('json')->successful('备注成功');
  243. }
  244. /**
  245. * 订单交易额/订单数量时间统计
  246. * @param Request $request
  247. * @return bool
  248. */
  249. public function time(Request $request)
  250. {
  251. $uid = $request->uid();
  252. if (!StoreService::orderServiceStatus($uid))
  253. return app('json')->fail('权限不足');
  254. list($start, $stop, $type) = UtilService::getMore([
  255. ['start', strtotime(date('Y-m'))],
  256. ['stop', time()],
  257. ['type', 1]
  258. ], $request, true);
  259. if ($start == $stop) {
  260. return app('json')->fail('开始时间不能等于结束时间');
  261. }
  262. if ($start > $stop) {
  263. $middle = $stop;
  264. $stop = $start;
  265. $start = $middle;
  266. }
  267. $space = bcsub($stop, $start, 0);//间隔时间段
  268. $front = bcsub($start, $space, 0);//第一个时间段
  269. if ($type == 1) {//销售额
  270. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start);
  271. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop);
  272. $chartInfo = StoreOrder::chartTimePrice($start, $stop);
  273. $data['chart'] = $chartInfo;//营业额图表数据
  274. $data['time'] = $afterPrice;//时间区间营业额
  275. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  276. $growthRate = abs($increase);
  277. if ($growthRate == 0) $data['growth_rate'] = 0;
  278. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  279. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  280. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  281. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  282. } else {//订单数
  283. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start);
  284. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop);
  285. $chartInfo = StoreOrder::chartTimeNumber($start, $stop);
  286. $data['chart'] = $chartInfo;//订单数图表数据
  287. $data['time'] = $afterNumber;//时间区间订单数
  288. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  289. $growthRate = abs($increase);
  290. if ($growthRate == 0) $data['growth_rate'] = 0;
  291. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  292. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  293. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  294. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  295. }
  296. return app('json')->successful($data);
  297. }
  298. /**
  299. * 订单支付
  300. * @param Request $request
  301. * @return mixed
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. * @throws \think\exception\DbException
  305. */
  306. public function offline(Request $request)
  307. {
  308. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  309. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id');
  310. if (!$orderInfo) return app('json')->fail('参数错误');
  311. $id = $orderInfo->id;
  312. $res = StoreOrder::updateOffline($id);
  313. if ($res) {
  314. event('StoreProductOrderOffline', [$id]);
  315. StoreOrderStatus::status($id, 'offline', '线下付款');
  316. return app('json')->successful('修改成功!');
  317. }
  318. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  319. }
  320. /**
  321. * 订单退款
  322. * @param Request $request
  323. * @return mixed
  324. * @throws \think\Exception
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. * @throws \think\exception\DbException
  328. */
  329. public function refund(Request $request)
  330. {
  331. list($orderId, $price, $type) = UtilService::postMore([
  332. ['order_id', ''],
  333. ['price', 0],
  334. ['type', 1],
  335. ], $request, true);
  336. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  337. $orderInfo = StoreOrder::getAdminOrderDetail($orderId);
  338. if (!$orderInfo) return app('json')->fail('数据不存在!');
  339. $orderInfo = $orderInfo->toArray();
  340. if ($type == 1)
  341. $data['refund_status'] = 2;
  342. else if ($type == 2)
  343. $data['refund_status'] = 0;
  344. else
  345. return app('json')->fail('退款修改状态错误');
  346. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  347. StoreOrder::update($data, ['order_id' => $orderId]);
  348. return app('json')->successful('修改退款状态成功!');
  349. }
  350. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  351. if (!$price) return app('json')->fail('请输入退款金额');
  352. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  353. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  354. if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  355. $refundData['pay_price'] = $orderInfo['pay_price'];
  356. $refundData['refund_price'] = $price;
  357. if ($orderInfo['pay_type'] == 'weixin') {
  358. if ($orderInfo['is_channel'] == 1) {// 小程序
  359. try {
  360. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  361. } catch (\Exception $e) {
  362. return app('json')->fail($e->getMessage());
  363. }
  364. } else {// 公众号
  365. try {
  366. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  367. } catch (\Exception $e) {
  368. return app('json')->fail($e->getMessage());
  369. }
  370. }
  371. } else if ($orderInfo['pay_type'] == 'yue') {//余额
  372. StoreOrder::beginTrans();
  373. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  374. if (!$userInfo) {
  375. StoreOrder::rollbackTrans();
  376. return app('json')->fail('订单用户不存在');
  377. }
  378. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
  379. $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
  380. try {
  381. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  382. } catch (\Exception $e) {
  383. StoreOrder::rollbackTrans();
  384. return app('json')->fail($e->getMessage());
  385. }
  386. $res = $res1 && $res2;
  387. StoreOrder::checkTrans($res);
  388. if (!$res) return app('json')->fail('余额退款失败!');
  389. }
  390. $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
  391. if ($resEdit) {
  392. $data['type'] = $type;
  393. if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  394. try {
  395. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  396. } catch (\Exception $e) {
  397. return app('json')->fail($e->getMessage());
  398. }
  399. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
  400. //退佣金
  401. $brokerage_list = UserBill::where('category', 'now_money')
  402. ->where('type', 'brokerage')
  403. ->where('link_id', $orderId)
  404. ->where('pm', 1)
  405. ->select()->toArray();
  406. if ($brokerage_list)
  407. foreach ($brokerage_list as $item) {
  408. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  409. if ($item['number'] > $usermoney)
  410. $item['number'] = $usermoney;
  411. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  412. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  413. }
  414. return app('json')->successful('修改成功!');
  415. } else {
  416. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
  417. return app('json')->successful('修改失败!');
  418. }
  419. }
  420. /**
  421. * 门店核销
  422. * @param Request $request
  423. */
  424. public function order_verific(Request $request)
  425. {
  426. list($verify_code, $is_confirm) = UtilService::postMore([
  427. ['verify_code', ''],
  428. ['is_confirm', 0]
  429. ], $request, true);
  430. if (!$verify_code) return app('json')->fail('缺少核销码');
  431. $orderInfo = StoreOrder::where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  432. if (!$orderInfo) return app('json')->fail('核销的订单不存在或未支付或已退款');
  433. if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
  434. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  435. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  436. if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
  437. }
  438. if (!$is_confirm) {
  439. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  440. return app('json')->success($orderInfo->toArray());
  441. }
  442. StoreOrder::beginTrans();
  443. try {
  444. $uid = $request->uid();
  445. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  446. $orderInfo->store_id = $storeStaff['store_id'];
  447. $orderInfo->clerk_id = $storeStaff['id'];
  448. }
  449. $orderInfo->status = 2;
  450. if ($orderInfo->save()) {
  451. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  452. StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
  453. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  454. StoreOrder::commitTrans();
  455. return app('json')->success('核销成功');
  456. } else {
  457. StoreOrder::rollbackTrans();
  458. return app('json')->fail('核销失败');
  459. }
  460. } catch (\PDOException $e) {
  461. StoreOrder::rollbackTrans();
  462. return app('json')->fail($e->getMessage());
  463. }
  464. }
  465. /**
  466. * 优惠券核销
  467. * @param Request $request
  468. * @return mixed
  469. * @throws \think\db\exception\DataNotFoundException
  470. * @throws \think\db\exception\DbException
  471. * @throws \think\db\exception\ModelNotFoundException
  472. */
  473. public function coupon_verific(Request $request)
  474. {
  475. list($verify_code,) = UtilService::postMore([
  476. ['verify_code', ''],
  477. ], $request, true);
  478. if (!$verify_code) return app('json')->fail('缺少核销码');
  479. $coupon = StoreCouponUser::where('code', $verify_code)->where('status', 0)->find(); //用户优惠券
  480. if (!$coupon) return app('json')->fail('核销的优惠券不存在或已使用或过期');
  481. $user = SystemStoreStaff::where('uid', $request->uid())->find();
  482. if (!$user) return app('json')->fail('你不是店员');
  483. if ($coupon['store_id'] != $user['store_id']) return app('json')->fail('你不是门店优惠券指定店员,没有权限核销');
  484. StoreCouponUser::beginTrans();
  485. try {
  486. $coupon['status'] = 1;
  487. $coupon['use_time'] = time();
  488. $res = $coupon->save();
  489. $pon = StoreCouponUser::where('order_id', $coupon['order_id'])->where('uid', $coupon['uid'])->where('code', '<>', $verify_code)->where('status', 0)->select();
  490. if (count($pon) == 0){
  491. StoreOrder::where('order_id', $coupon['order_id'])->where('uid', $coupon['uid'])->update(['use' => 1, 'status' => 3]);
  492. }
  493. WriteOff::create([
  494. 'uid' => $request->uid(),
  495. 'coupon_id' => $coupon['id'],
  496. 'add_time' => time()
  497. ]);
  498. StoreCouponUser::commitTrans();
  499. if ($res) return app('json')->success('核销成功');
  500. } catch (\PDOException $e) {
  501. StoreCouponUser::rollbackTrans();
  502. return app('json')->fail($e->getMessage());
  503. }
  504. }
  505. }