StoreOrder.php 39 KB

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