StoreOrder.php 43 KB

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