StoreOrder.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\order;
  8. use app\admin\controller\AuthController;
  9. use app\admin\model\order\StoreOrderCartInfo;
  10. use app\admin\model\order\StoreOrderRefund;
  11. use app\admin\model\system\Express;
  12. use crmeb\repositories\NoticeRepositories;
  13. use crmeb\repositories\OrderRepository;
  14. use crmeb\repositories\ShortLetterRepositories;
  15. use crmeb\services\{ExpressService,
  16. JiaLieService,
  17. JsonService,
  18. JsonService as Json,
  19. MiniProgramService,
  20. UtilService,
  21. WechatService,
  22. FormBuilder as Form,
  23. CacheService,
  24. UtilService as Util
  25. };
  26. use app\admin\model\order\StoreOrderStatus;
  27. use app\admin\model\ump\StorePink;
  28. use think\Exception;
  29. use app\admin\model\user\{
  30. User, UserBill
  31. };
  32. use crmeb\basic\BaseModel;
  33. use think\facade\Route as Url;
  34. use app\admin\model\order\StoreOrder as StoreOrderModel;
  35. use app\models\store\StoreOrder as StoreOrderModel1;
  36. use crmeb\services\YLYService;
  37. use think\facade\Log;
  38. use app\models\store\StoreOrder as StoreOrderModels;
  39. /**
  40. * 订单管理控制器 同一个订单表放在一个控制器
  41. * Class StoreOrder
  42. * @package app\admin\controller\store
  43. */
  44. class StoreOrder extends AuthController
  45. {
  46. /**
  47. * @return mixed
  48. */
  49. public function index()
  50. {
  51. $this->assign([
  52. 'year' => get_month(),
  53. 'real_name' => $this->request->get('real_name', ''),
  54. 'status' => $this->request->param('status', ''),
  55. 'orderCount' => StoreOrderModel::orderCount($this->adminInfo['store_id']),
  56. 'payTypeCount' => StoreOrderModel::payTypeCount($this->adminInfo['store_id']),
  57. ]);
  58. return $this->fetch();
  59. }
  60. /**
  61. * 获取头部订单金额等信息
  62. * return json
  63. */
  64. public function getBadge()
  65. {
  66. $where = Util::postMore([
  67. ['status', ''],
  68. ['real_name', ''],
  69. ['is_del', 0],
  70. ['data', ''],
  71. ['type', ''],
  72. ['pay_type', ''],
  73. ['order', '']
  74. ]);
  75. $where['store_id'] = $this->adminInfo['store_id'];
  76. if ($this->request->post('store_id') > 0) $where['store_id'] = $this->request->post('store_id');
  77. return Json::successful(StoreOrderModel::getBadge($where));
  78. }
  79. /**
  80. * 获取订单列表
  81. * return json
  82. */
  83. public function order_list()
  84. {
  85. $where = Util::getMore([
  86. ['status', ''],
  87. ['real_name', $this->request->param('real_name', '')],
  88. ['is_del', 0],
  89. ['data', ''],
  90. ['type', ''],
  91. ['pay_type', ''],
  92. ['order', ''],
  93. ['page', 1],
  94. ['limit', 20],
  95. ['excel', 0],
  96. ['store_id', $this->adminInfo['store_id']],
  97. ]);
  98. return Json::successlayui(StoreOrderModel::OrderList($where));
  99. }
  100. public function order_print($id = '')
  101. {
  102. if (!$id) {
  103. return JsonService::fail('缺少参数');
  104. }
  105. $order = StoreOrderModel::get($id);
  106. if (!$order) {
  107. return JsonService::fail('订单没有查到,无法打印!');
  108. }
  109. try {
  110. $order['cart_id'] = is_array($order['cart_id']) ? $order['cart_id'] : json_decode($order['cart_id'], true);
  111. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', join(",", $order['cart_id']))->field('cart_info')->select();
  112. $cartInfo = count($cartInfo) ? $cartInfo->toArray() : [];
  113. $product = [];
  114. foreach ($cartInfo as $item) {
  115. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  116. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  117. $value['productInfo']['store_name'] = StoreOrderCartInfo::getSubstrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  118. $product[] = $value;
  119. }
  120. if (!$product) {
  121. return JsonService::fail('订单商品获取失败,无法打印!');
  122. }
  123. $res = YLYService::instance()->selectStore($order['store_id'])->setContent(sys_config('site_name'), is_object($order) ? $order->toArray() : $order, $product)->orderPrinting();
  124. if ($res) {
  125. return JsonService::successful('打印成功');
  126. } else {
  127. return JsonService::fail('打印失败');
  128. }
  129. } catch (\Exception $e) {
  130. Log::error('小票打印出现错误,错误原因:' . $e->getMessage());
  131. return JsonService::fail($e->getFile() . '--' . $e->getLine() . '--' . $e->getMessage());
  132. }
  133. }
  134. public function info($id)
  135. {
  136. if (!$id) {
  137. return JsonService::fail('缺少参数');
  138. }
  139. $order = StoreOrderModel::get($id);
  140. if (!$order) {
  141. return JsonService::fail('订单没有查到,无法打印!');
  142. }
  143. $info = $order->toArray();
  144. $order['cart_id'] = is_array($order['cart_id']) ? $order['cart_id'] : json_decode($order['cart_id'], true);
  145. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', join(",", $order['cart_id']))->field('cart_info')->select()->toArray();
  146. return Json::successful(compact('info', 'cartInfo'));
  147. }
  148. public function out_goods($id)
  149. {
  150. $this->assign('id', $id);
  151. return $this->fetch();
  152. }
  153. public function cart_refund()
  154. {
  155. $order = UtilService::getMore([
  156. ['order_id', 0],
  157. ['cart_id', 0],
  158. ['product_id', 0],
  159. ['refund_num', 0],
  160. ['cart_num', 0],
  161. ['refund_price', 0],
  162. ]);
  163. if ($order['order_id'] == 0) return JsonService::fail('订单不能为空');
  164. if ($order['cart_id'] == 0) return JsonService::fail('订单购物车不能为空');
  165. if ($order['product_id'] == 0) return JsonService::fail('商品编号不能为空');
  166. if ($order['refund_num'] == 0) return JsonService::fail('退货数量');
  167. if ($order['refund_price'] == 0) return JsonService::fail('商品价格');
  168. $order['admin_id'] = $this->adminId;
  169. $order['refund_time'] = time();
  170. StoreOrderRefund::refund($order);
  171. }
  172. /**
  173. * 核销码核销
  174. * @param string $verify_code
  175. * @return html
  176. */
  177. public function write_order($verify_code = '', $is_confirm = 0)
  178. {
  179. if ($this->request->isAjax()) {
  180. if (!$verify_code) return Json::fail('缺少核销码!');
  181. StoreOrderModel::beginTrans();
  182. $orderInfo = StoreOrderModel::where('verify_code', $verify_code)->where('paid', 1)->where('refund_status', 0)->find();
  183. if (!$orderInfo) return Json::fail('核销订单不存在!');
  184. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  185. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  186. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  187. if ($res) return Json::fail('拼团订单暂未成功无法核销!');
  188. }
  189. if ($is_confirm == 0) {
  190. $orderInfo['nickname'] = User::where(['uid' => $orderInfo['uid']])->value('nickname');
  191. return Json::successful($orderInfo);
  192. }
  193. $orderInfo->status = 2;
  194. if ($orderInfo->save()) {
  195. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  196. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  197. //发送短信
  198. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  199. StoreOrderModel::commitTrans();
  200. return Json::successful('核销成功!');
  201. } else {
  202. StoreOrderModel::rollbackTrans();
  203. return Json::fail('核销失败');
  204. }
  205. } else
  206. $this->assign('is_layui', 1);
  207. return $this->fetch();
  208. }
  209. public function orderchart()
  210. {
  211. $where = Util::getMore([
  212. ['status', ''],
  213. ['real_name', ''],
  214. ['is_del', 0],
  215. ['data', ''],
  216. ['combination_id', ''],
  217. ['export', 0],
  218. ['order', 'id desc']
  219. ], $this->request);
  220. $limitTimeList = [
  221. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  222. 'week' => implode(' - ', [
  223. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  224. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  225. ]),
  226. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  227. 'quarter' => implode(' - ', [
  228. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  229. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  230. ]),
  231. 'year' => implode(' - ', [
  232. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  233. ])
  234. ];
  235. if ($where['data'] == '') $where['data'] = $limitTimeList['today'];
  236. $orderCount = [
  237. urlencode('未支付') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(0))->count(),
  238. urlencode('未发货') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(1))->count(),
  239. urlencode('待收货') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(2))->count(),
  240. urlencode('待评价') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(3))->count(),
  241. urlencode('交易完成') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(4))->count(),
  242. urlencode('退款中') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(-1))->count(),
  243. urlencode('已退款') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(-2))->count()
  244. ];
  245. $model = StoreOrderModel::getOrderWhere($where, new StoreOrderModel())->field('sum(total_num) total_num,count(*) count,sum(total_price) total_price,sum(refund_price) refund_price,from_unixtime(add_time,\'%Y-%m-%d\') add_time')
  246. ->group('from_unixtime(add_time,\'%Y-%m-%d\')');
  247. $orderPrice = $model->select()->toArray();
  248. $orderDays = [];
  249. $orderCategory = [
  250. ['name' => '商品数', 'type' => 'line', 'data' => []],
  251. ['name' => '订单数', 'type' => 'line', 'data' => []],
  252. ['name' => '订单金额', 'type' => 'line', 'data' => []],
  253. ['name' => '退款金额', 'type' => 'line', 'data' => []]
  254. ];
  255. foreach ($orderPrice as $price) {
  256. $orderDays[] = $price['add_time'];
  257. $orderCategory[0]['data'][] = $price['total_num'];
  258. $orderCategory[1]['data'][] = $price['count'];
  259. $orderCategory[2]['data'][] = $price['total_price'];
  260. $orderCategory[3]['data'][] = $price['refund_price'];
  261. }
  262. $this->assign(StoreOrderModel::systemPage($where, $this->adminId));
  263. $this->assign('price', StoreOrderModel::getOrderPrice($where));
  264. $this->assign(compact('limitTimeList', 'where', 'orderCount', 'orderPrice', 'orderDays', 'orderCategory'));
  265. return $this->fetch();
  266. }
  267. /**
  268. * 修改支付金额等
  269. * @param $id
  270. * @return mixed|\think\response\Json|void
  271. */
  272. public function edit($id)
  273. {
  274. if (!$id) return $this->failed('数据不存在');
  275. $product = StoreOrderModel::get($id);
  276. if (!$product) return Json::fail('数据不存在!');
  277. $f = [];
  278. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(1);
  279. $f[] = Form::number('total_price', '商品总价', $product->getData('total_price'))->min(0);
  280. $f[] = Form::number('total_postage', '原始邮费', $product->getData('total_postage'))->min(0);
  281. $f[] = Form::number('pay_price', '实际支付金额', $product->getData('pay_price'))->min(0);
  282. $f[] = Form::number('pay_postage', '实际支付邮费', $product->getData('pay_postage'));
  283. $f[] = Form::number('gain_integral', '赠送积分', $product->getData('gain_integral'));
  284. // $f[] = Form::radio('status','状态',$product->getData('status'))->options([['label'=>'开启','value'=>1],['label'=>'关闭','value'=>0]]);
  285. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('update', array('id' => $id)));
  286. $this->assign(compact('form'));
  287. return $this->fetch('public/form-builder');
  288. }
  289. /**
  290. * 修改订单提交更新
  291. * @param $id
  292. */
  293. public function update($id)
  294. {
  295. $data = Util::postMore([
  296. 'order_id',
  297. 'total_price',
  298. 'total_postage',
  299. 'pay_price',
  300. 'pay_postage',
  301. 'gain_integral',
  302. ]);
  303. if ($data['total_price'] <= 0) return Json::fail('请输入商品总价');
  304. if ($data['pay_price'] <= 0) return Json::fail('请输入实际支付金额');
  305. $orderInfo = StoreOrderModel::get($id);
  306. if (!$orderInfo) {
  307. return Json::fail('订单不存在');
  308. }
  309. $orderInfo->order_id = StoreOrderModel::changeOrderId($data['order_id']);
  310. $pay_price = $orderInfo->pay_price;
  311. $orderInfo->pay_price = $data['pay_price'];
  312. $orderInfo->total_price = $data['total_price'];
  313. $orderInfo->total_postage = $data['total_postage'];
  314. $orderInfo->pay_postage = $data['pay_postage'];
  315. $orderInfo->gain_integral = $data['gain_integral'];
  316. if ($orderInfo->save()) {
  317. //改价短信提醒
  318. if ($data['pay_price'] != $pay_price) {
  319. $switch = sys_config('price_revision_switch') ? true : false;
  320. ShortLetterRepositories::send($switch, $orderInfo->user_phone, ['order_id' => $orderInfo->order_id, 'pay_price' => $orderInfo->pay_price], 'PRICE_REVISION_CODE');
  321. }
  322. event('StoreProductOrderEditAfter', [$data, $id]);
  323. StoreOrderStatus::setStatus($id, 'order_edit', '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']);
  324. return Json::successful('修改成功!');
  325. } else {
  326. return Json::fail('订单修改失败');
  327. }
  328. }
  329. /*
  330. * 发送货
  331. * @param int $id
  332. * @return html
  333. * */
  334. public function order_goods($id = 0)
  335. {
  336. $list = Express::where('is_show', 1)->order('sort desc')->column('name', 'id');
  337. $this->assign([
  338. 'list' => $list,
  339. 'id' => $id
  340. ]);
  341. return $this->fetch();
  342. }
  343. /*
  344. * 删除订单
  345. * */
  346. public function del_order()
  347. {
  348. $ids = Util::postMore(['ids'])['ids'];
  349. if (!count($ids)) return Json::fail('请选择需要删除的订单');
  350. if (StoreOrderModel::where('is_del', 0)->where('id', 'in', $ids)->count())
  351. return Json::fail('您选择的的订单存在用户未删除的订单,无法删除用户未删除的订单');
  352. $res = StoreOrderModel::where('id', 'in', $ids)->update(['is_system_del' => 1]);
  353. if ($res)
  354. return Json::successful('删除成功');
  355. else
  356. return Json::fail('删除失败');
  357. }
  358. /**
  359. * TODO 送货信息提交
  360. * @param Request $request
  361. * @param $id
  362. */
  363. public function update_delivery($id = 0)
  364. {
  365. $data = Util::postMore([
  366. ['type', 1],
  367. ['delivery_name', ''],
  368. ['delivery_id', ''],
  369. ['sh_delivery_name', ''],
  370. ['sh_delivery_id', ''],
  371. ], $this->request);
  372. switch ((int)$data['type']) {
  373. case 1:
  374. //发货
  375. $data['delivery_type'] = 'express';
  376. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  377. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  378. $data['status'] = 1;
  379. // StoreOrderModel::where('id',$id)->update($data);
  380. StoreOrderModel::edit($data, $id);
  381. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  382. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  383. break;
  384. case 2:
  385. //送货
  386. $data['delivery_type'] = 'send';
  387. $data['delivery_name'] = $data['sh_delivery_name'];
  388. $data['delivery_id'] = $data['sh_delivery_id'];
  389. unset($data['sh_delivery_name'], $data['sh_delivery_id']);
  390. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  391. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  392. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  393. $data['status'] = 1;
  394. // StoreOrderModel::where('id',$id)->update($data);
  395. StoreOrderModel::edit($data, $id);
  396. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  397. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  398. break;
  399. case 3:
  400. //虚拟发货
  401. $data['delivery_type'] = 'fictitious';
  402. $data['status'] = 1;
  403. // StoreOrderModel::where('id',$id)->update($data);
  404. StoreOrderModel::edit($data, $id);
  405. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  406. StoreOrderStatus::setStatus($id, 'delivery_fictitious', '已虚拟发货');
  407. break;
  408. default:
  409. return Json::fail('暂时不支持其他发货类型');
  410. break;
  411. }
  412. //短信发送
  413. event('ShortMssageSend', [StoreOrderModel::where('id', $id)->value('order_id'), 'Deliver']);
  414. return Json::successful('修改成功!');
  415. }
  416. /**
  417. * TODO 填写送货信息
  418. * @param $id
  419. * @return mixed|void
  420. * @throws \think\exception\DbException
  421. */
  422. public function delivery($id)
  423. {
  424. if (!$id) return $this->failed('数据不存在');
  425. $product = StoreOrderModel::get($id);
  426. if (!$product) return Json::fail('数据不存在!');
  427. if ($product['paid'] == 1 && $product['status'] == 0) {
  428. $f = [];
  429. $f[] = Form::input('delivery_name', '送货人姓名')->required('送货人姓名不能为空', 'required:true;');
  430. $f[] = Form::input('delivery_id', '送货人电话')->required('请输入正确电话号码', 'telephone');
  431. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDelivery', array('id' => $id)), 7);
  432. $this->assign(compact('form'));
  433. return $this->fetch('public/form-builder');
  434. } else $this->failedNotice('订单状态错误');
  435. }
  436. /**
  437. * TODO 送货信息提交
  438. * @param $id
  439. */
  440. public function updateDelivery($id)
  441. {
  442. $data = Util::postMore([
  443. 'delivery_name',
  444. 'delivery_id',
  445. ]);
  446. $data['delivery_type'] = 'send';
  447. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  448. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  449. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  450. $data['status'] = 1;
  451. StoreOrderModel::where('id', $id)->update($data);
  452. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  453. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  454. return Json::successful('修改成功!');
  455. }
  456. /**
  457. * TODO 填写发货信息
  458. * @param $id
  459. * @return mixed|void
  460. * @throws \think\exception\DbException
  461. */
  462. public function deliver_goods($id)
  463. {
  464. if (!$id) return $this->failed('数据不存在');
  465. $product = StoreOrderModel::get($id);
  466. if (!$product) return Json::fail('数据不存在!');
  467. if ($product['paid'] == 1 && $product['status'] == 0) {
  468. $f = [];
  469. $f[] = Form::select('delivery_name', '快递公司')->setOptions(function () {
  470. $list = Express::where('is_show', 1)->order('sort DESC')->column('name', 'id');
  471. $menus = [];
  472. foreach ($list as $k => $v) {
  473. $menus[] = ['value' => $v, 'label' => $v];
  474. }
  475. return $menus;
  476. })->filterable(1);
  477. $f[] = Form::input('delivery_id', '快递单号');
  478. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDeliveryGoods', array('id' => $id)), 7);
  479. $this->assign(compact('form'));
  480. return $this->fetch('public/form-builder');
  481. } else return $this->failedNotice('订单状态错误');
  482. }
  483. /**
  484. * TODO 发货信息提交
  485. * @param $id
  486. */
  487. public function updateDeliveryGoods($id)
  488. {
  489. $data = Util::postMore([
  490. 'delivery_name',
  491. 'delivery_id',
  492. ]);
  493. $data['delivery_type'] = 'express';
  494. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  495. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  496. $data['status'] = 1;
  497. StoreOrderModel::where('id', $id)->update($data);
  498. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  499. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  500. return Json::successful('修改成功!');
  501. }
  502. /**
  503. * 修改状态为已收货
  504. * @param $id
  505. * @return \think\response\Json|void
  506. */
  507. public function take_delivery($id)
  508. {
  509. if (!$id) return $this->failed('数据不存在');
  510. $order = StoreOrderModel::get($id);
  511. if (!$order) return Json::fail('数据不存在!');
  512. if ($order['status'] == 2) return Json::fail('不能重复收货!');
  513. if ($order['paid'] == 1 && $order['status'] == 1) $data['status'] = 2;
  514. else if ($order['pay_type'] == 'offline') $data['status'] = 2;
  515. else return Json::fail('请先发货或者送货!');
  516. StoreOrderModel::beginTrans();
  517. try {
  518. if (!StoreOrderModel::edit($data, $id)) {
  519. StoreOrderModel::rollbackTrans();
  520. return Json::fail(StoreOrderModel::getErrorInfo('收货失败,请稍候再试!'));
  521. } else {
  522. OrderRepository::storeProductOrderTakeDeliveryAdmin($order, $id);
  523. StoreOrderStatus::setStatus($id, 'take_delivery', '已收货');
  524. StoreOrderModel::commitTrans();
  525. //发送短信
  526. event('ShortMssageSend', [$order['order_id'], 'Receiving']);
  527. return Json::successful('收货成功!');
  528. }
  529. } catch (\Exception $e) {
  530. StoreOrderModel::rollbackTrans();
  531. return Json::fail($e->getMessage());
  532. }
  533. }
  534. /**
  535. * 修改退款状态
  536. * @param $id
  537. * @return \think\response\Json|void
  538. */
  539. public function refund_y($id)
  540. {
  541. if (!$id) return $this->failed('数据不存在');
  542. $product = StoreOrderModel::get($id);
  543. if (!$product) return Json::fail('数据不存在!');
  544. if ($product['paid'] == 1) {
  545. $f = [];
  546. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  547. $f[] = Form::number('refund_price', '退款金额', $product->getData('pay_price'))->precision(2)->min(0.01);
  548. $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]]);
  549. $form = Form::make_post_form('退款处理', $f, Url::buildUrl('updateRefundY', array('id' => $id)), 7);
  550. $this->assign(compact('form'));
  551. return $this->fetch('public/form-builder');
  552. } else return Json::fail('数据不存在!');
  553. }
  554. /**
  555. * 退款处理
  556. * @param $id
  557. */
  558. public function updateRefundY($id)
  559. {
  560. $data = Util::postMore([
  561. 'refund_price',
  562. ['type', 1],
  563. ]);
  564. if (!$id) return $this->failed('数据不存在');
  565. $product = StoreOrderModel::get($id);
  566. if (!$product) return Json::fail('数据不存在!');
  567. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  568. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  569. $refund_price = $data['refund_price'];
  570. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  571. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  572. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  573. if ($data['type'] == 1) {
  574. $data['refund_status'] = 2;
  575. } else if ($data['type'] == 2) {
  576. $data['refund_status'] = 0;
  577. }
  578. $type = $data['type'];
  579. unset($data['type']);
  580. $refund_data['pay_price'] = $product['pay_price'];
  581. $refund_data['refund_price'] = $refund_price;
  582. if ($product['pay_type'] == 'weixin') {
  583. if ($product['is_channel'] == 1) {//小程序
  584. try {
  585. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  586. } catch (\Exception $e) {
  587. return Json::fail($e->getMessage());
  588. }
  589. } else {//TODO 公众号
  590. try {
  591. WechatService::payOrderRefund($product['order_id'], $refund_data);
  592. } catch (\Exception $e) {
  593. return Json::fail($e->getMessage());
  594. }
  595. }
  596. } else if ($product['pay_type'] == 'yue') {
  597. BaseModel::beginTrans();
  598. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  599. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  600. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  601. try {
  602. OrderRepository::storeOrderYueRefund($product, $refund_data);
  603. } catch (\Exception $e) {
  604. BaseModel::rollbackTrans();
  605. return Json::fail($e->getMessage());
  606. }
  607. $res = $res1 && $res2;
  608. BaseModel::checkTrans($res);
  609. if (!$res) return Json::fail('余额退款失败!');
  610. } elseif ($product['pay_type'] == 'consumer') {
  611. BaseModel::beginTrans();
  612. $usermoney = User::where('uid', $product['uid'])->value('consumer');
  613. $res1 = User::bcInc($product['uid'], 'consumer', $refund_price, 'uid');
  614. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'consumer', 'consumer_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到消费券' . floatval($refund_price) . '元');
  615. try {
  616. OrderRepository::storeOrderYueRefund($product, $refund_data);
  617. } catch (\Exception $e) {
  618. BaseModel::rollbackTrans();
  619. return Json::fail($e->getMessage());
  620. }
  621. $res = $res1 && $res2;
  622. BaseModel::checkTrans($res);
  623. if (!$res) return Json::fail('余额退款失败!');
  624. } elseif ($product['pay_type'] == 'jialie') {
  625. try {
  626. $rs = JiaLieService::initialize()->payOrderRefund(!empty($product['re_order_id']) ? $product['re_order_id'] : $product['order_id'], $refund_data);//2.5.36
  627. if ($rs['ret_code'] == '00' && $rs['status'] == '2') {
  628. } else {
  629. if (!$rs) return Json::fail('嘉联退款失败!');
  630. }
  631. } catch (\Exception $e) {
  632. return Json::fail($e->getMessage());
  633. }
  634. }
  635. $resEdit = StoreOrderModel::where('id', $id)->update($data);
  636. $res = true;
  637. BaseModel::beginTrans();
  638. if ($resEdit) {
  639. $data['type'] = $type;
  640. if ($data['type'] == 1) $res = StorePink::setRefundPink($id);
  641. if (!$res) return Json::fail('修改失败');
  642. try {
  643. OrderRepository::storeProductOrderRefundY($data, $id);
  644. } catch (\Exception $e) {
  645. BaseModel::rollbackTrans();
  646. return Json::fail($e->getFile() . '--' . $e->getLine() . '--' . $e->getMessage());
  647. }
  648. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  649. //退佣金
  650. $brokerage_list = UserBill::where('category', 'now_money')
  651. ->where('type', 'brokerage')
  652. ->where('link_id', $id)
  653. ->where('pm', 1)
  654. ->select();
  655. if ($brokerage_list) {
  656. $brokerage_list = $brokerage_list->toArray();
  657. foreach ($brokerage_list as $item) {
  658. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  659. if ($item['number'] > $usermoney)
  660. $item['number'] = $usermoney;
  661. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  662. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $id, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  663. }
  664. }
  665. //退款扣除用户积分
  666. //购买赠送的积分
  667. $bill_integral = UserBill::where('category', 'integral')
  668. ->where('type', 'gain')
  669. ->where('link_id', $id)
  670. ->where('pm', 1)
  671. ->find();
  672. if ($bill_integral) {
  673. $bill_integral = $bill_integral->toArray();
  674. //用户积分
  675. $user_integral = User::where('uid', $bill_integral['uid'])->value('integral');
  676. if ($bill_integral['number'] > $user_integral)
  677. $bill_integral['number'] = $user_integral;
  678. User::bcDec($bill_integral['uid'], 'integral', $bill_integral['number'], 'uid');
  679. UserBill::expend('退款扣除积分', $bill_integral['uid'], 'integral', 'gain', $bill_integral['number'], $id, bcsub($user_integral, $bill_integral['number'], 2), '订单退款扣除积分' . floatval($bill_integral['number']) . '积分');
  680. }
  681. //回退库存
  682. StoreOrderModels::RegressionStock($product);
  683. BaseModel::commitTrans();
  684. return Json::successful('修改成功!');
  685. } else {
  686. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  687. return Json::fail('修改失败!');
  688. }
  689. }
  690. public function order_info($oid = '')
  691. {
  692. if (!$oid || !($orderInfo = StoreOrderModel::get($oid)))
  693. return $this->failed('订单不存在!');
  694. $userInfo = User::getUserInfos($orderInfo['uid']);
  695. if ($userInfo['spread_uid']) {
  696. $spread = User::where('uid', $userInfo['spread_uid'])->value('nickname');
  697. } else {
  698. $spread = '';
  699. }
  700. $this->assign(compact('orderInfo', 'userInfo', 'spread'));
  701. return $this->fetch();
  702. }
  703. public function express($oid = '')
  704. {
  705. if (!$oid || !($order = StoreOrderModel::get($oid)))
  706. return $this->failed('订单不存在!');
  707. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  708. $cacheName = $order['order_id'] . $order['delivery_id'];
  709. $result = CacheService::get($cacheName, null);
  710. if ($result === null) {
  711. $result = ExpressService::query($order['delivery_id']);
  712. if (is_array($result) &&
  713. isset($result['result']) &&
  714. isset($result['result']['deliverystatus']) &&
  715. $result['result']['deliverystatus'] >= 3)
  716. $cacheTime = 0;
  717. else
  718. $cacheTime = 1800;
  719. CacheService::set($cacheName, $result, $cacheTime);
  720. }
  721. $this->assign([
  722. 'order' => $order,
  723. 'express' => $result
  724. ]);
  725. return $this->fetch();
  726. }
  727. /**
  728. * 修改配送信息
  729. * @param $id
  730. * @return mixed|\think\response\Json|void
  731. */
  732. public function distribution($id)
  733. {
  734. if (!$id) return $this->failed('数据不存在');
  735. $product = StoreOrderModel::get($id);
  736. if (!$product) return Json::fail('数据不存在!');
  737. $f = [];
  738. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  739. if ($product['delivery_type'] == 'send') {
  740. $f[] = Form::input('delivery_name', '送货人姓名', $product->getData('delivery_name'));
  741. $f[] = Form::input('delivery_id', '送货人电话', $product->getData('delivery_id'));
  742. } else if ($product['delivery_type'] == 'express') {
  743. $f[] = Form::select('delivery_name', '快递公司', $product->getData('delivery_name'))->setOptions(function () {
  744. $list = Express::where('is_show', 1)->column('name', 'id');
  745. $menus = [];
  746. foreach ($list as $k => $v) {
  747. $menus[] = ['value' => $v, 'label' => $v];
  748. }
  749. return $menus;
  750. });
  751. $f[] = Form::input('delivery_id', '快递单号', $product->getData('delivery_id'));
  752. }
  753. $form = Form::make_post_form('配送信息', $f, Url::buildUrl('updateDistribution', array('id' => $id)), 7);
  754. $this->assign(compact('form'));
  755. return $this->fetch('public/form-builder');
  756. }
  757. /**
  758. * 修改配送信息
  759. * @param $id
  760. */
  761. public function updateDistribution($id)
  762. {
  763. $data = Util::postMore([
  764. 'delivery_name',
  765. 'delivery_id',
  766. ]);
  767. if (!$id) return $this->failed('数据不存在');
  768. $product = StoreOrderModel::get($id);
  769. if (!$product) return Json::fail('数据不存在!');
  770. if ($product['delivery_type'] == 'send') {
  771. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  772. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  773. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  774. } else if ($product['delivery_type'] == 'express') {
  775. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  776. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  777. }
  778. StoreOrderModel::where('id', $id)->update($data);
  779. event('StoreProductOrderDistributionAfter', [$data, $id]);
  780. StoreOrderStatus::setStatus($id, 'distribution', '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id']);
  781. return Json::successful('修改成功!');
  782. }
  783. /**
  784. * 修改退款状态
  785. * @param $id
  786. * @return mixed|\think\response\Json|void
  787. */
  788. public function refund_n($id)
  789. {
  790. if (!$id) return $this->failed('数据不存在');
  791. $product = StoreOrderModel::get($id);
  792. if (!$product) return Json::fail('数据不存在!');
  793. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  794. $f[] = Form::input('refund_reason', '退款原因')->type('textarea');
  795. $form = Form::make_post_form('退款', $f, Url::buildUrl('updateRefundN', array('id' => $id)));
  796. $this->assign(compact('form'));
  797. return $this->fetch('public/form-builder');
  798. }
  799. /**
  800. * 不退款原因
  801. * @param $id
  802. */
  803. public function updateRefundN($id)
  804. {
  805. $data = Util::postMore([
  806. 'refund_reason',
  807. ]);
  808. if (!$id) return $this->failed('数据不存在');
  809. $product = StoreOrderModel::get($id);
  810. if (!$product) return Json::fail('数据不存在!');
  811. if (!$data['refund_reason']) return Json::fail('请输入退款原因');
  812. $data['refund_status'] = 0;
  813. StoreOrderModel::where('id', $id)->update($data);
  814. event('StoreProductOrderRefundNAfter', [$data['refund_reason'], $id]);
  815. StoreOrderStatus::setStatus($id, 'refund_n', '不退款原因:' . $data['refund_reason']);
  816. return Json::successful('修改成功!');
  817. }
  818. /**
  819. * 立即支付
  820. * @param $id
  821. */
  822. public function offline($id)
  823. {
  824. $res = StoreOrderModel::updateOffline($id);
  825. if ($res) {
  826. event('StoreProductOrderOffline', [$id]);
  827. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  828. return Json::successful('修改成功!');
  829. } else {
  830. return Json::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  831. }
  832. }
  833. /**
  834. * 修改积分和金额
  835. * @param $id
  836. * @return mixed|\think\response\Json|void
  837. */
  838. public function integral_back($id)
  839. {
  840. if (!$id) return $this->failed('数据不存在');
  841. $product = StoreOrderModel::get($id);
  842. if (!$product) return Json::fail('数据不存在!');
  843. if ($product['paid'] == 1) {
  844. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  845. $f[] = Form::number('use_integral', '使用的积分', $product->getData('use_integral'))->min(0)->disabled(1);
  846. $f[] = Form::number('use_integrals', '已退积分', $product->getData('back_integral'))->min(0)->disabled(1);
  847. $f[] = Form::number('back_integral', '可退积分', bcsub($product->getData('use_integral'), $product->getData('use_integral')))->min(0);
  848. $form = Form::make_post_form('退积分', $f, Url::buildUrl('updateIntegralBack', array('id' => $id)), 7);
  849. $this->assign(compact('form'));
  850. return $this->fetch('public/form-builder');
  851. } else {
  852. return Json::fail('参数错误!');
  853. }
  854. return $this->fetch('public/form-builder');
  855. }
  856. /**
  857. * 退积分保存
  858. * @param $id
  859. */
  860. public function updateIntegralBack($id)
  861. {
  862. $data = Util::postMore([
  863. 'back_integral',
  864. ]);
  865. if (!$id) return $this->failed('数据不存在');
  866. $product = StoreOrderModel::get($id);
  867. if (!$product) return Json::fail('数据不存在!');
  868. if ($data['back_integral'] <= 0) return Json::fail('请输入积分');
  869. if ($product['use_integral'] == $product['back_integral']) return Json::fail('已退完积分!不能再积分了');
  870. $back_integral = $data['back_integral'];
  871. $data['back_integral'] = bcadd($data['back_integral'], $product['back_integral'], 2);
  872. $bj = bccomp((float)$product['use_integral'], (float)$data['back_integral'], 2);
  873. if ($bj < 0) return Json::fail('退积分大于支付积分,请修改退积分');
  874. BaseModel::beginTrans();
  875. $integral = User::where('uid', $product['uid'])->value('integral');
  876. $res1 = User::bcInc($product['uid'], 'integral', $back_integral, 'uid');
  877. $res2 = UserBill::income('商品退积分', $product['uid'], 'integral', 'pay_product_integral_back', $back_integral, $product['id'], bcadd($integral, $back_integral, 2), '订单退积分' . floatval($back_integral) . '积分到用户积分');
  878. event('StoreOrderIntegralBack', [$product, $back_integral]);
  879. try {
  880. OrderRepository::storeOrderIntegralBack($product, $back_integral);
  881. } catch (\Exception $e) {
  882. BaseModel::rollbackTrans();
  883. return Json::fail($e->getMessage());
  884. }
  885. $res = $res1 && $res2;
  886. BaseModel::checkTrans($res);
  887. if (!$res) return Json::fail('退积分失败!');
  888. if ($product['pay_price'] == 0 && $bj == 0) {
  889. $data['refund_status'] = 2;
  890. }
  891. StoreOrderModel::where('id', $id)->update($data);
  892. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  893. return Json::successful('退积分成功!');
  894. }
  895. public function remark()
  896. {
  897. $data = Util::postMore(['id', 'remark']);
  898. if (!$data['id']) return Json::fail('参数错误!');
  899. if ($data['remark'] == '') return Json::fail('请输入要备注的内容!');
  900. $id = $data['id'];
  901. unset($data['id']);
  902. StoreOrderModel::where('id', $id)->update($data);
  903. return Json::successful('备注成功!');
  904. }
  905. public function order_status($oid)
  906. {
  907. if (!$oid) return $this->failed('数据不存在');
  908. $this->assign(StoreOrderStatus::systemPage($oid));
  909. return $this->fetch();
  910. }
  911. /*
  912. * 订单列表推荐人详细
  913. */
  914. public function order_spread_user($uid)
  915. {
  916. $spread = User::where('uid', $uid)->find();
  917. $this->assign('spread', $spread);
  918. return $this->fetch();
  919. }
  920. /**
  921. * 立即核销
  922. * @param $id
  923. */
  924. public function verify($id)
  925. {
  926. StoreOrderModel::beginTrans();
  927. $orderInfo = StoreOrderModel::where('id', $id)->find();
  928. if (!$orderInfo) return Json::fail('核销订单不存在!');
  929. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  930. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  931. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  932. if ($res) return Json::fail('拼团订单暂未成功无法核销!');
  933. }
  934. $orderInfo->status = 2;
  935. if ($orderInfo->save()) {
  936. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  937. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  938. //发送短信
  939. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  940. StoreOrderModel::commitTrans();
  941. return Json::successful('核销成功!');
  942. } else {
  943. StoreOrderModel::rollbackTrans();
  944. return Json::fail('核销失败');
  945. }
  946. }
  947. public function re_pay($id)
  948. {
  949. if (!$id) return $this->failed('数据不存在');
  950. $product = StoreOrderModel::where('id', $id)->find();
  951. if (!$product) return Json::fail('数据不存在!');
  952. $f = [];
  953. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(1);
  954. $f[] = Form::number('total_price', '商品总价', $product->getData('total_price'))->min(0);
  955. $f[] = Form::input('auth_code', '支付码', '');
  956. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('paysave', array('id' => $id)));
  957. $this->assign(compact('form'));
  958. return $this->fetch('public/form-builder');
  959. }
  960. public function paysave($id)
  961. {
  962. $orderInfo = StoreOrderModel::where('id', $id)->find();
  963. $orderInfo = $orderInfo->toArray();
  964. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  965. $order_id = mt_rand(100, 999) . '-' . $orderInfo['order_id'];
  966. StoreOrderModel::where('id', $id)->update(['re_order_id' => $order_id]);
  967. $rs = OrderRepository::jialiePay($order_id, input('auth_code')); //支付结果
  968. if ($rs['ret_code'] == "00" && $rs['status'] == "2") {
  969. $this->verify($orderInfo['id']);
  970. StoreOrderModel1::paySuccess($orderInfo['order_id'], 'jialie');
  971. } elseif ($rs['ret_code'] == "00" && $rs['status'] == "1") {
  972. StoreOrderModel1::edit(['jialie_do' => 1], $orderInfo['id']);
  973. } else {
  974. return app('json')->fail($rs['ret_msg']);
  975. }
  976. return app('json')->status('success', '订单支付成功', $rs);
  977. }
  978. }