StoreOrder.php 39 KB

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