StoreOrder.php 45 KB

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