StoreOrderController.php 24 KB

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