StoreOrder.php 44 KB

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