StoreOrder.php 44 KB

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