StoreOrder.php 41 KB

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