StoreOrder.php 40 KB

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