StoreOrder.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 crmeb\repositories\OrderRepository;
  12. use crmeb\repositories\ShortLetterRepositories;
  13. use crmeb\services\{
  14. ExpressService,
  15. JsonService,
  16. MiniProgramService,
  17. WechatService,
  18. FormBuilder as Form,
  19. CacheService,
  20. UtilService as Util,
  21. JsonService as Json
  22. };
  23. use app\admin\model\order\StoreOrderStatus;
  24. use app\admin\model\ump\StorePink;
  25. use app\admin\model\user\{
  26. User, UserBill
  27. };
  28. use crmeb\basic\BaseModel;
  29. use think\facade\Route as Url;
  30. use app\admin\model\order\StoreOrder as StoreOrderModel;
  31. use crmeb\services\YLYService;
  32. use think\facade\Log;
  33. /**
  34. * 订单管理控制器 同一个订单表放在一个控制器
  35. * Class StoreOrder
  36. * @package app\admin\controller\store
  37. */
  38. class StoreOrder extends AuthController
  39. {
  40. /**
  41. * @return mixed
  42. */
  43. public function index()
  44. {
  45. $this->assign([
  46. 'year' => get_month(),
  47. 'real_name' => $this->request->get('real_name', ''),
  48. 'status' => $this->request->param('status', ''),
  49. 'orderCount' => StoreOrderModel::orderCount(),
  50. 'payTypeCount' => StoreOrderModel::payTypeCount(),
  51. ]);
  52. return $this->fetch();
  53. }
  54. /**
  55. * 获取头部订单金额等信息
  56. * return json
  57. */
  58. public function getBadge()
  59. {
  60. $where = Util::postMore([
  61. ['status', ''],
  62. ['real_name', ''],
  63. ['is_del', 0],
  64. ['data', ''],
  65. ['type', ''],
  66. ['pay_type', ''],
  67. ['order', '']
  68. ]);
  69. return Json::successful(StoreOrderModel::getBadge($where));
  70. }
  71. /**
  72. * 获取订单列表
  73. * return json
  74. */
  75. public function order_list()
  76. {
  77. $where = Util::getMore([
  78. ['status', ''],
  79. ['real_name', $this->request->param('real_name', '')],
  80. ['is_del', 0],
  81. ['data', ''],
  82. ['type', ''],
  83. ['pay_type', ''],
  84. ['order', ''],
  85. ['page', 1],
  86. ['limit', 20],
  87. ['excel', 0]
  88. ]);
  89. return Json::successlayui(StoreOrderModel::OrderList($where));
  90. }
  91. public function order_print($id = '')
  92. {
  93. if (!$id) {
  94. return JsonService::fail('缺少参数');
  95. }
  96. $order = StoreOrderModel::get($id);
  97. if (!$order) {
  98. return JsonService::fail('订单没有查到,无法打印!');
  99. }
  100. try {
  101. $order['cart_id'] = is_string($order['cart_id']) ? json_decode($order['cart_id'], true) : $order['cart_id'];
  102. $cartInfo = StoreOrderCartInfo::whereIn('cart_id', $order['cart_id'])->field('cart_info')->select();
  103. $cartInfo = count($cartInfo) ? $cartInfo->toArray() : [];
  104. $product = [];
  105. foreach ($cartInfo as $item) {
  106. $value = is_string($item['cart_info']) ? json_decode($item['cart_info'], true) : $item['cart_info'];
  107. $value['productInfo']['store_name'] = $value['productInfo']['store_name'] ?? "";
  108. $value['productInfo']['store_name'] = StoreOrderCartInfo::getSubstrUTf8($value['productInfo']['store_name'], 10, 'UTF-8', '');
  109. $product[] = $value;
  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. Log::error('小票打印出现错误,错误原因:' . $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. /**
  240. * 修改订单提交更新
  241. * @param $id
  242. */
  243. public function update($id)
  244. {
  245. $data = Util::postMore([
  246. 'order_id',
  247. 'total_price',
  248. 'total_postage',
  249. 'pay_price',
  250. 'pay_postage',
  251. 'gain_integral',
  252. ]);
  253. if ($data['total_price'] <= 0) return Json::fail('请输入商品总价');
  254. if ($data['pay_price'] <= 0) return Json::fail('请输入实际支付金额');
  255. $source_info = StoreOrderModel::where('order_id', $data['order_id'])->find();
  256. $data['order_id'] = StoreOrderModel::changeOrderId($data['order_id']);
  257. StoreOrderModel::edit($data, $id);
  258. //改价短信提醒
  259. if ($data['pay_price'] != $source_info['pay_price']) {
  260. $switch = sys_config('price_revision_switch') ? true : false;
  261. ShortLetterRepositories::send($switch, $source_info['user_phone'], ['order_id' => $source_info, 'pay_price' => $source_info['pay_price']], 'PRICE_REVISION_CODE');
  262. }
  263. event('StoreProductOrderEditAfter', [$data, $id]);
  264. StoreOrderStatus::setStatus($id, 'order_edit', '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']);
  265. return Json::successful('修改成功!');
  266. }
  267. /*
  268. * 发送货
  269. * @param int $id
  270. * @return html
  271. * */
  272. public function order_goods($id = 0)
  273. {
  274. $list = Express::where('is_show', 1)->order('sort desc')->column('name', 'id');
  275. $this->assign([
  276. 'list' => $list,
  277. 'id' => $id
  278. ]);
  279. return $this->fetch();
  280. }
  281. /*
  282. * 删除订单
  283. * */
  284. public function del_order()
  285. {
  286. $ids = Util::postMore(['ids'])['ids'];
  287. if (!count($ids)) return Json::fail('请选择需要删除的订单');
  288. if (StoreOrderModel::where('is_del', 0)->where('id', 'in', $ids)->count())
  289. return Json::fail('您选择的的订单存在用户未删除的订单,无法删除用户未删除的订单');
  290. $res = StoreOrderModel::where('id', 'in', $ids)->update(['is_system_del' => 1]);
  291. if ($res)
  292. return Json::successful('删除成功');
  293. else
  294. return Json::fail('删除失败');
  295. }
  296. /**
  297. * TODO 送货信息提交
  298. * @param Request $request
  299. * @param $id
  300. */
  301. public function update_delivery($id = 0)
  302. {
  303. $data = Util::postMore([
  304. ['type', 1],
  305. ['delivery_name', ''],
  306. ['delivery_id', ''],
  307. ['sh_delivery_name', ''],
  308. ['sh_delivery_id', ''],
  309. ], $this->request);
  310. switch ((int)$data['type']) {
  311. case 1:
  312. //发货
  313. $data['delivery_type'] = 'express';
  314. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  315. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  316. $data['status'] = 1;
  317. StoreOrderModel::edit($data, $id);
  318. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  319. @file_put_contents('quanju.txt', "-发货\r\n", 8);
  320. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  321. break;
  322. case 2:
  323. @file_put_contents('quanju.txt', "-送货\r\n", 8);
  324. //送货
  325. $data['delivery_type'] = 'send';
  326. $data['delivery_name'] = $data['sh_delivery_name'];
  327. $data['delivery_id'] = $data['sh_delivery_id'];
  328. unset($data['sh_delivery_name'], $data['sh_delivery_id']);
  329. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  330. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  331. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  332. $data['status'] = 1;
  333. StoreOrderModel::edit($data, $id);
  334. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  335. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  336. break;
  337. case 3:
  338. //虚拟发货
  339. $data['delivery_type'] = 'fictitious';
  340. $data['status'] = 1;
  341. StoreOrderModel::edit($data, $id);
  342. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  343. StoreOrderStatus::setStatus($id, 'delivery_fictitious', '已虚拟发货');
  344. break;
  345. default:
  346. return Json::fail('暂时不支持其他发货类型');
  347. break;
  348. }
  349. //短信发送
  350. event('ShortMssageSend', [StoreOrderModel::where('id', $id)->value('order_id'), 'Deliver']);
  351. return Json::successful('修改成功!');
  352. }
  353. /**
  354. * TODO 填写送货信息
  355. * @param $id
  356. * @return mixed|void
  357. * @throws \think\exception\DbException
  358. */
  359. public function delivery($id)
  360. {
  361. if (!$id) return $this->failed('数据不存在');
  362. $product = StoreOrderModel::get($id);
  363. if (!$product) return Json::fail('数据不存在!');
  364. if ($product['paid'] == 1 && $product['status'] == 0) {
  365. $f = [];
  366. $f[] = Form::input('delivery_name', '送货人姓名')->required('送货人姓名不能为空', 'required:true;');
  367. $f[] = Form::input('delivery_id', '送货人电话')->required('请输入正确电话号码', 'telephone');
  368. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDelivery', array('id' => $id)), 7);
  369. $this->assign(compact('form'));
  370. return $this->fetch('public/form-builder');
  371. } else $this->failedNotice('订单状态错误');
  372. }
  373. /**
  374. * TODO 送货信息提交
  375. * @param $id
  376. */
  377. public function updateDelivery($id)
  378. {
  379. $data = Util::postMore([
  380. 'delivery_name',
  381. 'delivery_id',
  382. ]);
  383. $data['delivery_type'] = 'send';
  384. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  385. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  386. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  387. $data['status'] = 1;
  388. StoreOrderModel::edit($data, $id);
  389. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  390. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  391. return Json::successful('修改成功!');
  392. }
  393. /**
  394. * TODO 填写发货信息
  395. * @param $id
  396. * @return mixed|void
  397. * @throws \think\exception\DbException
  398. */
  399. public function deliver_goods($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::select('delivery_name', '快递公司')->setOptions(function () {
  407. $list = Express::where('is_show', 1)->order('sort DESC')->column('name', 'id');
  408. $menus = [];
  409. foreach ($list as $k => $v) {
  410. $menus[] = ['value' => $v, 'label' => $v];
  411. }
  412. return $menus;
  413. })->filterable(1);
  414. $f[] = Form::input('delivery_id', '快递单号');
  415. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDeliveryGoods', array('id' => $id)), 7);
  416. $this->assign(compact('form'));
  417. return $this->fetch('public/form-builder');
  418. } else return $this->failedNotice('订单状态错误');
  419. }
  420. /**
  421. * TODO 发货信息提交
  422. * @param $id
  423. */
  424. public function updateDeliveryGoods($id)
  425. {
  426. $data = Util::postMore([
  427. 'delivery_name',
  428. 'delivery_id',
  429. ]);
  430. $data['delivery_type'] = 'express';
  431. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  432. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  433. $data['status'] = 1;
  434. StoreOrderModel::edit($data, $id);
  435. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  436. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  437. return Json::successful('修改成功!');
  438. }
  439. /**
  440. * 修改状态为已收货
  441. * @param $id
  442. * @return \think\response\Json|void
  443. */
  444. public function take_delivery($id)
  445. {
  446. if (!$id) return $this->failed('数据不存在');
  447. $order = StoreOrderModel::get($id);
  448. if (!$order) return Json::fail('数据不存在!');
  449. if ($order['status'] == 2) return Json::fail('不能重复收货!');
  450. if ($order['paid'] == 1 && $order['status'] == 1) $data['status'] = 2;
  451. else if ($order['pay_type'] == 'offline') $data['status'] = 2;
  452. else return Json::fail('请先发货或者送货!');
  453. StoreOrderModel::beginTrans();
  454. try {
  455. if (!StoreOrderModel::edit($data, $id)) {
  456. StoreOrderModel::rollbackTrans();
  457. return Json::fail(StoreOrderModel::getErrorInfo('收货失败,请稍候再试!'));
  458. } else {
  459. OrderRepository::storeProductOrderTakeDeliveryAdmin($order, $id);
  460. StoreOrderStatus::setStatus($id, 'take_delivery', '已收货');
  461. StoreOrderModel::commitTrans();
  462. //发送短信
  463. event('ShortMssageSend', [$order['order_id'], 'Receiving']);
  464. return Json::successful('收货成功!');
  465. }
  466. } catch (\Exception $e) {
  467. StoreOrderModel::rollbackTrans();
  468. return Json::fail($e->getMessage());
  469. }
  470. }
  471. /**
  472. * 修改退款状态
  473. * @param $id
  474. * @return \think\response\Json|void
  475. */
  476. public function refund_y($id)
  477. {
  478. if (!$id) return $this->failed('数据不存在');
  479. $product = StoreOrderModel::get($id);
  480. if (!$product) return Json::fail('数据不存在!');
  481. if ($product['paid'] == 1) {
  482. $f = [];
  483. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  484. $f[] = Form::number('refund_price', '退款金额', $product->getData('pay_price'))->precision(2)->min(0.01);
  485. $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]]);
  486. $form = Form::make_post_form('退款处理', $f, Url::buildUrl('updateRefundY', array('id' => $id)), 7);
  487. $this->assign(compact('form'));
  488. return $this->fetch('public/form-builder');
  489. } else return Json::fail('数据不存在!');
  490. }
  491. /**
  492. * 退款处理
  493. * @param $id
  494. */
  495. public function updateRefundY($id)
  496. {
  497. $data = Util::postMore([
  498. 'refund_price',
  499. ['type', 1],
  500. ]);
  501. if (!$id) return $this->failed('数据不存在');
  502. $product = StoreOrderModel::get($id);
  503. if (!$product) return Json::fail('数据不存在!');
  504. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  505. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  506. $refund_price = $data['refund_price'];
  507. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  508. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  509. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  510. if ($data['type'] == 1) {
  511. $data['refund_status'] = 2;
  512. } else if ($data['type'] == 2) {
  513. $data['refund_status'] = 0;
  514. }
  515. $type = $data['type'];
  516. unset($data['type']);
  517. $refund_data['pay_price'] = $product['pay_price'];
  518. $refund_data['refund_price'] = $refund_price;
  519. if ($product['pay_type'] == 'weixin') {
  520. if ($product['is_channel'] == 1) {//小程序
  521. try {
  522. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  523. } catch (\Exception $e) {
  524. return Json::fail($e->getMessage());
  525. }
  526. } else {//TODO 公众号
  527. try {
  528. WechatService::payOrderRefund($product['order_id'], $refund_data);
  529. } catch (\Exception $e) {
  530. return Json::fail($e->getMessage());
  531. }
  532. }
  533. } else if ($product['pay_type'] == 'yue') {
  534. BaseModel::beginTrans();
  535. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  536. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  537. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  538. try {
  539. OrderRepository::storeOrderYueRefund($product, $refund_data);
  540. } catch (\Exception $e) {
  541. BaseModel::rollbackTrans();
  542. return Json::fail($e->getMessage());
  543. }
  544. $res = $res1 && $res2;
  545. BaseModel::checkTrans($res);
  546. if (!$res) return Json::fail('余额退款失败!');
  547. }
  548. $resEdit = StoreOrderModel::edit($data, $id);
  549. $res = true;
  550. if ($resEdit) {
  551. $data['type'] = $type;
  552. if ($data['type'] == 1) $res = StorePink::setRefundPink($id);
  553. if (!$res) return Json::fail('修改失败');
  554. try {
  555. OrderRepository::storeProductOrderRefundY($data, $id);
  556. } catch (\Exception $e) {
  557. BaseModel::rollbackTrans();
  558. return Json::fail($e->getMessage());
  559. }
  560. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  561. //退佣金
  562. $brokerage_list = UserBill::where('category', 'brokerage_price')
  563. ->where('link_id', $id)
  564. ->where('pm', 1)
  565. ->where('status', 1)
  566. ->select();
  567. if ($brokerage_list) {
  568. $brokerage_list = $brokerage_list->toArray();
  569. foreach ($brokerage_list as $item) {
  570. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  571. if ($item['number'] > $usermoney)
  572. $item['number'] = $usermoney;
  573. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  574. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $id, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  575. }
  576. }
  577. //退健康币
  578. $brokerage_list = UserBill::where('category', 'brokerage_price')
  579. ->where('link_id', $id)
  580. ->where('pm', 0)
  581. ->where('status', 1)
  582. ->select();
  583. if ($brokerage_list) {
  584. $brokerage_list = $brokerage_list->toArray();
  585. foreach ($brokerage_list as $item) {
  586. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  587. // if ($item['number'] > $usermoney)
  588. // $item['number'] = $usermoney;
  589. User::bcInc($item['uid'], 'brokerage_price', $item['number'], 'uid');
  590. UserBill::expend('退款退健康币', $item['uid'], 'now_money', 'brokerage', $item['number'], $id, bcadd($usermoney, $item['number'], 2), '订单退款退回健康币' . floatval($item['number']) . '元');
  591. }
  592. }
  593. //退款扣除用户积分
  594. //购买赠送的积分
  595. $bill_integral = UserBill::where('category', 'integral')
  596. ->where('type', 'gain')
  597. ->where('link_id', $id)
  598. ->where('pm', 1)
  599. ->find();
  600. if ($bill_integral) {
  601. $bill_integral = $bill_integral->toArray();
  602. //用户积分
  603. $user_integral = User::where('uid', $bill_integral['uid'])->value('integral');
  604. if ($bill_integral['number'] > $user_integral)
  605. $bill_integral['number'] = $user_integral;
  606. User::bcDec($bill_integral['uid'], 'integral', $bill_integral['number'], 'uid');
  607. UserBill::expend('退款扣除积分', $bill_integral['uid'], 'integral', 'gain', $bill_integral['number'], $id, bcsub($user_integral, $bill_integral['number'], 2), '订单退款扣除积分' . floatval($bill_integral['number']) . '积分');
  608. }
  609. BaseModel::commitTrans();
  610. return Json::successful('修改成功!');
  611. } else {
  612. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  613. return Json::fail('修改失败!');
  614. }
  615. }
  616. public function order_info($oid = '')
  617. {
  618. if (!$oid || !($orderInfo = StoreOrderModel::get($oid)))
  619. return $this->failed('订单不存在!');
  620. $userInfo = User::getUserInfos($orderInfo['uid']);
  621. if ($userInfo['spread_uid']) {
  622. $spread = User::where('uid', $userInfo['spread_uid'])->value('nickname');
  623. } else {
  624. $spread = '';
  625. }
  626. $this->assign(compact('orderInfo', 'userInfo', 'spread'));
  627. return $this->fetch();
  628. }
  629. public function express($oid = '')
  630. {
  631. if (!$oid || !($order = StoreOrderModel::get($oid)))
  632. return $this->failed('订单不存在!');
  633. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  634. $cacheName = $order['order_id'] . $order['delivery_id'];
  635. $result = CacheService::get($cacheName, null);
  636. if ($result === null) {
  637. $result = ExpressService::query($order['delivery_id']);
  638. if (is_array($result) &&
  639. isset($result['result']) &&
  640. isset($result['result']['deliverystatus']) &&
  641. $result['result']['deliverystatus'] >= 3)
  642. $cacheTime = 0;
  643. else
  644. $cacheTime = 1800;
  645. CacheService::set($cacheName, $result, $cacheTime);
  646. }
  647. $this->assign([
  648. 'order' => $order,
  649. 'express' => $result
  650. ]);
  651. return $this->fetch();
  652. }
  653. /**
  654. * 修改配送信息
  655. * @param $id
  656. * @return mixed|\think\response\Json|void
  657. */
  658. public function distribution($id)
  659. {
  660. if (!$id) return $this->failed('数据不存在');
  661. $product = StoreOrderModel::get($id);
  662. if (!$product) return Json::fail('数据不存在!');
  663. $f = [];
  664. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  665. if ($product['delivery_type'] == 'send') {
  666. $f[] = Form::input('delivery_name', '送货人姓名', $product->getData('delivery_name'));
  667. $f[] = Form::input('delivery_id', '送货人电话', $product->getData('delivery_id'));
  668. } else if ($product['delivery_type'] == 'express') {
  669. $f[] = Form::select('delivery_name', '快递公司', $product->getData('delivery_name'))->setOptions(function () {
  670. $list = Express::where('is_show', 1)->column('name', 'id');
  671. $menus = [];
  672. foreach ($list as $k => $v) {
  673. $menus[] = ['value' => $v, 'label' => $v];
  674. }
  675. return $menus;
  676. });
  677. $f[] = Form::input('delivery_id', '快递单号', $product->getData('delivery_id'));
  678. }
  679. $form = Form::make_post_form('配送信息', $f, Url::buildUrl('updateDistribution', array('id' => $id)), 7);
  680. $this->assign(compact('form'));
  681. return $this->fetch('public/form-builder');
  682. }
  683. /**
  684. * 修改配送信息
  685. * @param $id
  686. */
  687. public function updateDistribution($id)
  688. {
  689. $data = Util::postMore([
  690. 'delivery_name',
  691. 'delivery_id',
  692. ]);
  693. if (!$id) return $this->failed('数据不存在');
  694. $product = StoreOrderModel::get($id);
  695. if (!$product) return Json::fail('数据不存在!');
  696. if ($product['delivery_type'] == 'send') {
  697. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  698. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  699. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  700. } else if ($product['delivery_type'] == 'express') {
  701. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  702. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  703. }
  704. StoreOrderModel::edit($data, $id);
  705. event('StoreProductOrderDistributionAfter', [$data, $id]);
  706. StoreOrderStatus::setStatus($id, 'distribution', '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id']);
  707. return Json::successful('修改成功!');
  708. }
  709. /**
  710. * 修改退款状态
  711. * @param $id
  712. * @return mixed|\think\response\Json|void
  713. */
  714. public function refund_n($id)
  715. {
  716. if (!$id) return $this->failed('数据不存在');
  717. $product = StoreOrderModel::get($id);
  718. if (!$product) return Json::fail('数据不存在!');
  719. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  720. $f[] = Form::input('refund_reason', '退款原因')->type('textarea');
  721. $form = Form::make_post_form('退款', $f, Url::buildUrl('updateRefundN', array('id' => $id)));
  722. $this->assign(compact('form'));
  723. return $this->fetch('public/form-builder');
  724. }
  725. /**
  726. * 不退款原因
  727. * @param $id
  728. */
  729. public function updateRefundN($id)
  730. {
  731. $data = Util::postMore([
  732. 'refund_reason',
  733. ]);
  734. if (!$id) return $this->failed('数据不存在');
  735. $product = StoreOrderModel::get($id);
  736. if (!$product) return Json::fail('数据不存在!');
  737. if (!$data['refund_reason']) return Json::fail('请输入退款原因');
  738. $data['refund_status'] = 0;
  739. StoreOrderModel::edit($data, $id);
  740. event('StoreProductOrderRefundNAfter', [$data['refund_reason'], $id]);
  741. StoreOrderStatus::setStatus($id, 'refund_n', '不退款原因:' . $data['refund_reason']);
  742. return Json::successful('修改成功!');
  743. }
  744. /**
  745. * 立即支付
  746. * @param $id
  747. */
  748. public function offline($id)
  749. {
  750. $res = StoreOrderModel::updateOffline($id);
  751. if ($res) {
  752. event('StoreProductOrderOffline', [$id]);
  753. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  754. return Json::successful('修改成功!');
  755. } else {
  756. return Json::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  757. }
  758. }
  759. /**
  760. * 修改积分和金额
  761. * @param $id
  762. * @return mixed|\think\response\Json|void
  763. */
  764. public function integral_back($id)
  765. {
  766. if (!$id) return $this->failed('数据不存在');
  767. $product = StoreOrderModel::get($id);
  768. if (!$product) return Json::fail('数据不存在!');
  769. if ($product['paid'] == 1) {
  770. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  771. $f[] = Form::number('use_integral', '使用的积分', $product->getData('use_integral'))->min(0)->disabled(1);
  772. $f[] = Form::number('use_integrals', '已退积分', $product->getData('back_integral'))->min(0)->disabled(1);
  773. $f[] = Form::number('back_integral', '可退积分', bcsub($product->getData('use_integral'), $product->getData('use_integral')))->min(0);
  774. $form = Form::make_post_form('退积分', $f, Url::buildUrl('updateIntegralBack', array('id' => $id)), 7);
  775. $this->assign(compact('form'));
  776. return $this->fetch('public/form-builder');
  777. } else {
  778. return Json::fail('参数错误!');
  779. }
  780. return $this->fetch('public/form-builder');
  781. }
  782. /**
  783. * 退积分保存
  784. * @param $id
  785. */
  786. public function updateIntegralBack($id)
  787. {
  788. $data = Util::postMore([
  789. 'back_integral',
  790. ]);
  791. if (!$id) return $this->failed('数据不存在');
  792. $product = StoreOrderModel::get($id);
  793. if (!$product) return Json::fail('数据不存在!');
  794. if ($data['back_integral'] <= 0) return Json::fail('请输入积分');
  795. if ($product['use_integral'] == $product['back_integral']) return Json::fail('已退完积分!不能再积分了');
  796. $back_integral = $data['back_integral'];
  797. $data['back_integral'] = bcadd($data['back_integral'], $product['back_integral'], 2);
  798. $bj = bccomp((float)$product['use_integral'], (float)$data['back_integral'], 2);
  799. if ($bj < 0) return Json::fail('退积分大于支付积分,请修改退积分');
  800. BaseModel::beginTrans();
  801. $integral = User::where('uid', $product['uid'])->value('integral');
  802. $res1 = User::bcInc($product['uid'], 'integral', $back_integral, 'uid');
  803. $res2 = UserBill::income('商品退积分', $product['uid'], 'integral', 'pay_product_integral_back', $back_integral, $product['id'], bcadd($integral, $back_integral, 2), '订单退积分' . floatval($back_integral) . '积分到用户积分');
  804. event('StoreOrderIntegralBack', [$product, $back_integral]);
  805. try {
  806. OrderRepository::storeOrderIntegralBack($product, $back_integral);
  807. } catch (\Exception $e) {
  808. BaseModel::rollbackTrans();
  809. return Json::fail($e->getMessage());
  810. }
  811. $res = $res1 && $res2;
  812. BaseModel::checkTrans($res);
  813. if (!$res) return Json::fail('退积分失败!');
  814. if ($product['pay_price'] == 0 && $bj == 0) {
  815. $data['refund_status'] = 2;
  816. }
  817. StoreOrderModel::edit($data, $id);
  818. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  819. return Json::successful('退积分成功!');
  820. }
  821. public function remark()
  822. {
  823. $data = Util::postMore(['id', 'remark']);
  824. if (!$data['id']) return Json::fail('参数错误!');
  825. if ($data['remark'] == '') return Json::fail('请输入要备注的内容!');
  826. $id = $data['id'];
  827. unset($data['id']);
  828. StoreOrderModel::edit($data, $id);
  829. return Json::successful('备注成功!');
  830. }
  831. public function order_status($oid)
  832. {
  833. if (!$oid) return $this->failed('数据不存在');
  834. $this->assign(StoreOrderStatus::systemPage($oid));
  835. return $this->fetch();
  836. }
  837. /*
  838. * 订单列表推荐人详细
  839. */
  840. public function order_spread_user($uid)
  841. {
  842. $spread = User::where('uid', $uid)->find();
  843. $this->assign('spread', $spread);
  844. return $this->fetch();
  845. }
  846. /**
  847. * 立即核销
  848. * @param $id
  849. */
  850. public function verify($id)
  851. {
  852. StoreOrderModel::beginTrans();
  853. $orderInfo = StoreOrderModel::where('id', $id)->find();
  854. if (!$orderInfo) return Json::fail('核销订单不存在!');
  855. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  856. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  857. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  858. if ($res) return Json::fail('拼团订单暂未成功无法核销!');
  859. }
  860. $orderInfo->status = 2;
  861. if ($orderInfo->save()) {
  862. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  863. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  864. //发送短信
  865. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  866. StoreOrderModel::commitTrans();
  867. return Json::successful('核销成功!');
  868. } else {
  869. StoreOrderModel::rollbackTrans();
  870. return Json::fail('核销失败');
  871. }
  872. }
  873. }