StoreOrder.php 43 KB

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