StoreOrder.php 41 KB

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