StoreOrder.php 41 KB

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