StoreOrder.php 38 KB

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