StoreOrder.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  379. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  380. break;
  381. case 2:
  382. //送货
  383. $data['delivery_type'] = 'send';
  384. $data['delivery_name'] = $data['sh_delivery_name'];
  385. $data['delivery_id'] = $data['sh_delivery_id'];
  386. unset($data['sh_delivery_name'], $data['sh_delivery_id']);
  387. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  388. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  389. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  390. $data['status'] = 1;
  391. StoreOrderModel::where('id',$id)->update($data);
  392. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  393. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  394. break;
  395. case 3:
  396. //虚拟发货
  397. $data['delivery_type'] = 'fictitious';
  398. $data['status'] = 1;
  399. StoreOrderModel::where('id',$id)->update($data);
  400. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  401. StoreOrderStatus::setStatus($id, 'delivery_fictitious', '已虚拟发货');
  402. break;
  403. default:
  404. return Json::fail('暂时不支持其他发货类型');
  405. break;
  406. }
  407. //短信发送
  408. event('ShortMssageSend', [StoreOrderModel::where('id', $id)->value('order_id'), 'Deliver']);
  409. return Json::successful('修改成功!');
  410. }
  411. /**
  412. * TODO 填写送货信息
  413. * @param $id
  414. * @return mixed|void
  415. * @throws \think\exception\DbException
  416. */
  417. public function delivery($id)
  418. {
  419. if (!$id) return $this->failed('数据不存在');
  420. $product = StoreOrderModel::get($id);
  421. if (!$product) return Json::fail('数据不存在!');
  422. if ($product['paid'] == 1 && $product['status'] == 0) {
  423. $f = [];
  424. $f[] = Form::input('delivery_name', '送货人姓名')->required('送货人姓名不能为空', 'required:true;');
  425. $f[] = Form::input('delivery_id', '送货人电话')->required('请输入正确电话号码', 'telephone');
  426. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDelivery', array('id' => $id)), 7);
  427. $this->assign(compact('form'));
  428. return $this->fetch('public/form-builder');
  429. } else $this->failedNotice('订单状态错误');
  430. }
  431. /**
  432. * TODO 送货信息提交
  433. * @param $id
  434. */
  435. public function updateDelivery($id)
  436. {
  437. $data = Util::postMore([
  438. 'delivery_name',
  439. 'delivery_id',
  440. ]);
  441. $data['delivery_type'] = 'send';
  442. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  443. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  444. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  445. $data['status'] = 1;
  446. StoreOrderModel::where('id',$id)->update($data);
  447. event('StoreProductOrderDeliveryAfter', [$data, $id]);
  448. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  449. return Json::successful('修改成功!');
  450. }
  451. /**
  452. * TODO 填写发货信息
  453. * @param $id
  454. * @return mixed|void
  455. * @throws \think\exception\DbException
  456. */
  457. public function deliver_goods($id)
  458. {
  459. if (!$id) return $this->failed('数据不存在');
  460. $product = StoreOrderModel::get($id);
  461. if (!$product) return Json::fail('数据不存在!');
  462. if ($product['paid'] == 1 && $product['status'] == 0) {
  463. $f = [];
  464. $f[] = Form::select('delivery_name', '快递公司')->setOptions(function () {
  465. $list = Express::where('is_show', 1)->order('sort DESC')->column('name', 'id');
  466. $menus = [];
  467. foreach ($list as $k => $v) {
  468. $menus[] = ['value' => $v, 'label' => $v];
  469. }
  470. return $menus;
  471. })->filterable(1);
  472. $f[] = Form::input('delivery_id', '快递单号');
  473. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('updateDeliveryGoods', array('id' => $id)), 7);
  474. $this->assign(compact('form'));
  475. return $this->fetch('public/form-builder');
  476. } else return $this->failedNotice('订单状态错误');
  477. }
  478. /**
  479. * TODO 发货信息提交
  480. * @param $id
  481. */
  482. public function updateDeliveryGoods($id)
  483. {
  484. $data = Util::postMore([
  485. 'delivery_name',
  486. 'delivery_id',
  487. ]);
  488. $data['delivery_type'] = 'express';
  489. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  490. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  491. $data['status'] = 1;
  492. StoreOrderModel::where('id',$id)->update($data);
  493. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  494. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  495. return Json::successful('修改成功!');
  496. }
  497. /**
  498. * 修改状态为已收货
  499. * @param $id
  500. * @return \think\response\Json|void
  501. */
  502. public function take_delivery($id)
  503. {
  504. if (!$id) return $this->failed('数据不存在');
  505. $order = StoreOrderModel::get($id);
  506. if (!$order) return Json::fail('数据不存在!');
  507. if ($order['status'] == 2) return Json::fail('不能重复收货!');
  508. if ($order['paid'] == 1 && $order['status'] == 1) $data['status'] = 2;
  509. else if ($order['pay_type'] == 'offline') $data['status'] = 2;
  510. else return Json::fail('请先发货或者送货!');
  511. StoreOrderModel::beginTrans();
  512. try {
  513. if (!StoreOrderModel::edit($data, $id)) {
  514. StoreOrderModel::rollbackTrans();
  515. return Json::fail(StoreOrderModel::getErrorInfo('收货失败,请稍候再试!'));
  516. } else {
  517. OrderRepository::storeProductOrderTakeDeliveryAdmin($order, $id);
  518. StoreOrderStatus::setStatus($id, 'take_delivery', '已收货');
  519. StoreOrderModel::commitTrans();
  520. //发送短信
  521. event('ShortMssageSend', [$order['order_id'], 'Receiving']);
  522. return Json::successful('收货成功!');
  523. }
  524. } catch (\Exception $e) {
  525. StoreOrderModel::rollbackTrans();
  526. return Json::fail($e->getMessage());
  527. }
  528. }
  529. /**
  530. * 修改退款状态
  531. * @param $id
  532. * @return \think\response\Json|void
  533. */
  534. public function refund_y($id)
  535. {
  536. if (!$id) return $this->failed('数据不存在');
  537. $product = StoreOrderModel::get($id);
  538. if (!$product) return Json::fail('数据不存在!');
  539. if ($product['paid'] == 1) {
  540. $f = [];
  541. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  542. $f[] = Form::number('refund_price', '退款金额', $product->getData('pay_price'))->precision(2)->min(0.01);
  543. $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]]);
  544. $form = Form::make_post_form('退款处理', $f, Url::buildUrl('updateRefundY', array('id' => $id)), 7);
  545. $this->assign(compact('form'));
  546. return $this->fetch('public/form-builder');
  547. } else return Json::fail('数据不存在!');
  548. }
  549. /**
  550. * 退款处理
  551. * @param $id
  552. */
  553. public function updateRefundY($id)
  554. {
  555. $data = Util::postMore([
  556. 'refund_price',
  557. ['type', 1],
  558. ]);
  559. if (!$id) return $this->failed('数据不存在');
  560. $product = StoreOrderModel::get($id);
  561. if (!$product) return Json::fail('数据不存在!');
  562. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  563. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  564. $refund_price = $data['refund_price'];
  565. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  566. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  567. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  568. if ($data['type'] == 1) {
  569. $data['refund_status'] = 2;
  570. } else if ($data['type'] == 2) {
  571. $data['refund_status'] = 0;
  572. }
  573. $type = $data['type'];
  574. unset($data['type']);
  575. $refund_data['pay_price'] = $product['pay_price'];
  576. $refund_data['refund_price'] = $refund_price;
  577. if ($product['pay_type'] == 'weixin') {
  578. if ($product['is_channel'] == 1) {//小程序
  579. try {
  580. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  581. } catch (\Exception $e) {
  582. return Json::fail($e->getMessage());
  583. }
  584. } else {//TODO 公众号
  585. try {
  586. WechatService::payOrderRefund($product['order_id'], $refund_data);
  587. } catch (\Exception $e) {
  588. return Json::fail($e->getMessage());
  589. }
  590. }
  591. } else if ($product['pay_type'] == 'yue') {
  592. BaseModel::beginTrans();
  593. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  594. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  595. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  596. try {
  597. OrderRepository::storeOrderYueRefund($product, $refund_data);
  598. } catch (\Exception $e) {
  599. BaseModel::rollbackTrans();
  600. return Json::fail($e->getMessage());
  601. }
  602. $res = $res1 && $res2;
  603. BaseModel::checkTrans($res);
  604. if (!$res) return Json::fail('余额退款失败!');
  605. } elseif ($product['pay_type'] == 'consumer') {
  606. BaseModel::beginTrans();
  607. $usermoney = User::where('uid', $product['uid'])->value('consumer');
  608. $res1 = User::bcInc($product['uid'], 'consumer', $refund_price, 'uid');
  609. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'consumer_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到消费券' . floatval($refund_price) . '元');
  610. try {
  611. OrderRepository::storeOrderYueRefund($product, $refund_data);
  612. } catch (\Exception $e) {
  613. BaseModel::rollbackTrans();
  614. return Json::fail($e->getMessage());
  615. }
  616. $res = $res1 && $res2;
  617. BaseModel::checkTrans($res);
  618. if (!$res) return Json::fail('余额退款失败!');
  619. } elseif ($product['pay_type'] == 'jialie') {
  620. try {
  621. $rs = JiaLieService::initialize()->payOrderRefund(!empty($product['re_order_id'])?$product['re_order_id']:$product['order_id'], $refund_data);//2.5.36
  622. if($rs['ret_code']=='00' && $rs['status']=='2')
  623. {
  624. }
  625. else
  626. {
  627. if (!$res) return Json::fail('嘉联退款失败!');
  628. }
  629. } catch (\Exception $e) {
  630. return Json::fail($e->getMessage());
  631. }
  632. }
  633. $resEdit = StoreOrderModel::where('id', $id)->update($data);
  634. $res = true;
  635. BaseModel::beginTrans();
  636. if ($resEdit) {
  637. $data['type'] = $type;
  638. if ($data['type'] == 1) $res = StorePink::setRefundPink($id);
  639. if (!$res) return Json::fail('修改失败');
  640. try {
  641. OrderRepository::storeProductOrderRefundY($data, $id);
  642. } catch (\Exception $e) {
  643. BaseModel::rollbackTrans();
  644. return Json::fail($e->getFile().'--'.$e->getLine().'--'.$e->getMessage());
  645. }
  646. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  647. //退佣金
  648. $brokerage_list = UserBill::where('category', 'now_money')
  649. ->where('type', 'brokerage')
  650. ->where('link_id', $id)
  651. ->where('pm', 1)
  652. ->select();
  653. if ($brokerage_list) {
  654. $brokerage_list = $brokerage_list->toArray();
  655. foreach ($brokerage_list as $item) {
  656. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  657. if ($item['number'] > $usermoney)
  658. $item['number'] = $usermoney;
  659. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  660. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $id, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  661. }
  662. }
  663. //退款扣除用户积分
  664. //购买赠送的积分
  665. $bill_integral = UserBill::where('category', 'integral')
  666. ->where('type', 'gain')
  667. ->where('link_id', $id)
  668. ->where('pm', 1)
  669. ->find();
  670. if ($bill_integral) {
  671. $bill_integral = $bill_integral->toArray();
  672. //用户积分
  673. $user_integral = User::where('uid', $bill_integral['uid'])->value('integral');
  674. if ($bill_integral['number'] > $user_integral)
  675. $bill_integral['number'] = $user_integral;
  676. User::bcDec($bill_integral['uid'], 'integral', $bill_integral['number'], 'uid');
  677. UserBill::expend('退款扣除积分', $bill_integral['uid'], 'integral', 'gain', $bill_integral['number'], $id, bcsub($user_integral, $bill_integral['number'], 2), '订单退款扣除积分' . floatval($bill_integral['number']) . '积分');
  678. }
  679. //回退库存
  680. StoreOrderModels::RegressionStock($product);
  681. BaseModel::commitTrans();
  682. return Json::successful('修改成功!');
  683. } else {
  684. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  685. return Json::fail('修改失败!');
  686. }
  687. }
  688. public function order_info($oid = '')
  689. {
  690. if (!$oid || !($orderInfo = StoreOrderModel::get($oid)))
  691. return $this->failed('订单不存在!');
  692. $userInfo = User::getUserInfos($orderInfo['uid']);
  693. if ($userInfo['spread_uid']) {
  694. $spread = User::where('uid', $userInfo['spread_uid'])->value('nickname');
  695. } else {
  696. $spread = '';
  697. }
  698. $this->assign(compact('orderInfo', 'userInfo', 'spread'));
  699. return $this->fetch();
  700. }
  701. public function express($oid = '')
  702. {
  703. if (!$oid || !($order = StoreOrderModel::get($oid)))
  704. return $this->failed('订单不存在!');
  705. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  706. $cacheName = $order['order_id'] . $order['delivery_id'];
  707. $result = CacheService::get($cacheName, null);
  708. if ($result === null) {
  709. $result = ExpressService::query($order['delivery_id']);
  710. if (is_array($result) &&
  711. isset($result['result']) &&
  712. isset($result['result']['deliverystatus']) &&
  713. $result['result']['deliverystatus'] >= 3)
  714. $cacheTime = 0;
  715. else
  716. $cacheTime = 1800;
  717. CacheService::set($cacheName, $result, $cacheTime);
  718. }
  719. $this->assign([
  720. 'order' => $order,
  721. 'express' => $result
  722. ]);
  723. return $this->fetch();
  724. }
  725. /**
  726. * 修改配送信息
  727. * @param $id
  728. * @return mixed|\think\response\Json|void
  729. */
  730. public function distribution($id)
  731. {
  732. if (!$id) return $this->failed('数据不存在');
  733. $product = StoreOrderModel::get($id);
  734. if (!$product) return Json::fail('数据不存在!');
  735. $f = [];
  736. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  737. if ($product['delivery_type'] == 'send') {
  738. $f[] = Form::input('delivery_name', '送货人姓名', $product->getData('delivery_name'));
  739. $f[] = Form::input('delivery_id', '送货人电话', $product->getData('delivery_id'));
  740. } else if ($product['delivery_type'] == 'express') {
  741. $f[] = Form::select('delivery_name', '快递公司', $product->getData('delivery_name'))->setOptions(function () {
  742. $list = Express::where('is_show', 1)->column('name', 'id');
  743. $menus = [];
  744. foreach ($list as $k => $v) {
  745. $menus[] = ['value' => $v, 'label' => $v];
  746. }
  747. return $menus;
  748. });
  749. $f[] = Form::input('delivery_id', '快递单号', $product->getData('delivery_id'));
  750. }
  751. $form = Form::make_post_form('配送信息', $f, Url::buildUrl('updateDistribution', array('id' => $id)), 7);
  752. $this->assign(compact('form'));
  753. return $this->fetch('public/form-builder');
  754. }
  755. /**
  756. * 修改配送信息
  757. * @param $id
  758. */
  759. public function updateDistribution($id)
  760. {
  761. $data = Util::postMore([
  762. 'delivery_name',
  763. 'delivery_id',
  764. ]);
  765. if (!$id) return $this->failed('数据不存在');
  766. $product = StoreOrderModel::get($id);
  767. if (!$product) return Json::fail('数据不存在!');
  768. if ($product['delivery_type'] == 'send') {
  769. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  770. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  771. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  772. } else if ($product['delivery_type'] == 'express') {
  773. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  774. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  775. }
  776. StoreOrderModel::where('id',$id)->update($data);
  777. event('StoreProductOrderDistributionAfter', [$data, $id]);
  778. StoreOrderStatus::setStatus($id, 'distribution', '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id']);
  779. return Json::successful('修改成功!');
  780. }
  781. /**
  782. * 修改退款状态
  783. * @param $id
  784. * @return mixed|\think\response\Json|void
  785. */
  786. public function refund_n($id)
  787. {
  788. if (!$id) return $this->failed('数据不存在');
  789. $product = StoreOrderModel::get($id);
  790. if (!$product) return Json::fail('数据不存在!');
  791. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  792. $f[] = Form::input('refund_reason', '退款原因')->type('textarea');
  793. $form = Form::make_post_form('退款', $f, Url::buildUrl('updateRefundN', array('id' => $id)));
  794. $this->assign(compact('form'));
  795. return $this->fetch('public/form-builder');
  796. }
  797. /**
  798. * 不退款原因
  799. * @param $id
  800. */
  801. public function updateRefundN($id)
  802. {
  803. $data = Util::postMore([
  804. 'refund_reason',
  805. ]);
  806. if (!$id) return $this->failed('数据不存在');
  807. $product = StoreOrderModel::get($id);
  808. if (!$product) return Json::fail('数据不存在!');
  809. if (!$data['refund_reason']) return Json::fail('请输入退款原因');
  810. $data['refund_status'] = 0;
  811. StoreOrderModel::where('id',$id)->update($data);
  812. event('StoreProductOrderRefundNAfter', [$data['refund_reason'], $id]);
  813. StoreOrderStatus::setStatus($id, 'refund_n', '不退款原因:' . $data['refund_reason']);
  814. return Json::successful('修改成功!');
  815. }
  816. /**
  817. * 立即支付
  818. * @param $id
  819. */
  820. public function offline($id)
  821. {
  822. $res = StoreOrderModel::updateOffline($id);
  823. if ($res) {
  824. event('StoreProductOrderOffline', [$id]);
  825. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  826. return Json::successful('修改成功!');
  827. } else {
  828. return Json::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  829. }
  830. }
  831. /**
  832. * 修改积分和金额
  833. * @param $id
  834. * @return mixed|\think\response\Json|void
  835. */
  836. public function integral_back($id)
  837. {
  838. if (!$id) return $this->failed('数据不存在');
  839. $product = StoreOrderModel::get($id);
  840. if (!$product) return Json::fail('数据不存在!');
  841. if ($product['paid'] == 1) {
  842. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  843. $f[] = Form::number('use_integral', '使用的积分', $product->getData('use_integral'))->min(0)->disabled(1);
  844. $f[] = Form::number('use_integrals', '已退积分', $product->getData('back_integral'))->min(0)->disabled(1);
  845. $f[] = Form::number('back_integral', '可退积分', bcsub($product->getData('use_integral'), $product->getData('use_integral')))->min(0);
  846. $form = Form::make_post_form('退积分', $f, Url::buildUrl('updateIntegralBack', array('id' => $id)), 7);
  847. $this->assign(compact('form'));
  848. return $this->fetch('public/form-builder');
  849. } else {
  850. return Json::fail('参数错误!');
  851. }
  852. return $this->fetch('public/form-builder');
  853. }
  854. /**
  855. * 退积分保存
  856. * @param $id
  857. */
  858. public function updateIntegralBack($id)
  859. {
  860. $data = Util::postMore([
  861. 'back_integral',
  862. ]);
  863. if (!$id) return $this->failed('数据不存在');
  864. $product = StoreOrderModel::get($id);
  865. if (!$product) return Json::fail('数据不存在!');
  866. if ($data['back_integral'] <= 0) return Json::fail('请输入积分');
  867. if ($product['use_integral'] == $product['back_integral']) return Json::fail('已退完积分!不能再积分了');
  868. $back_integral = $data['back_integral'];
  869. $data['back_integral'] = bcadd($data['back_integral'], $product['back_integral'], 2);
  870. $bj = bccomp((float)$product['use_integral'], (float)$data['back_integral'], 2);
  871. if ($bj < 0) return Json::fail('退积分大于支付积分,请修改退积分');
  872. BaseModel::beginTrans();
  873. $integral = User::where('uid', $product['uid'])->value('integral');
  874. $res1 = User::bcInc($product['uid'], 'integral', $back_integral, 'uid');
  875. $res2 = UserBill::income('商品退积分', $product['uid'], 'integral', 'pay_product_integral_back', $back_integral, $product['id'], bcadd($integral, $back_integral, 2), '订单退积分' . floatval($back_integral) . '积分到用户积分');
  876. event('StoreOrderIntegralBack', [$product, $back_integral]);
  877. try {
  878. OrderRepository::storeOrderIntegralBack($product, $back_integral);
  879. } catch (\Exception $e) {
  880. BaseModel::rollbackTrans();
  881. return Json::fail($e->getMessage());
  882. }
  883. $res = $res1 && $res2;
  884. BaseModel::checkTrans($res);
  885. if (!$res) return Json::fail('退积分失败!');
  886. if ($product['pay_price'] == 0 && $bj == 0) {
  887. $data['refund_status'] = 2;
  888. }
  889. StoreOrderModel::where('id',$id)->update($data);
  890. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  891. return Json::successful('退积分成功!');
  892. }
  893. public function remark()
  894. {
  895. $data = Util::postMore(['id', 'remark']);
  896. if (!$data['id']) return Json::fail('参数错误!');
  897. if ($data['remark'] == '') return Json::fail('请输入要备注的内容!');
  898. $id = $data['id'];
  899. unset($data['id']);
  900. StoreOrderModel::where('id',$id)->update($data);
  901. return Json::successful('备注成功!');
  902. }
  903. public function order_status($oid)
  904. {
  905. if (!$oid) return $this->failed('数据不存在');
  906. $this->assign(StoreOrderStatus::systemPage($oid));
  907. return $this->fetch();
  908. }
  909. /*
  910. * 订单列表推荐人详细
  911. */
  912. public function order_spread_user($uid)
  913. {
  914. $spread = User::where('uid', $uid)->find();
  915. $this->assign('spread', $spread);
  916. return $this->fetch();
  917. }
  918. /**
  919. * 立即核销
  920. * @param $id
  921. */
  922. public function verify($id)
  923. {
  924. StoreOrderModel::beginTrans();
  925. $orderInfo = StoreOrderModel::where('id', $id)->find();
  926. if (!$orderInfo) return Json::fail('核销订单不存在!');
  927. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  928. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  929. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  930. if ($res) return Json::fail('拼团订单暂未成功无法核销!');
  931. }
  932. $orderInfo->status = 2;
  933. if ($orderInfo->save()) {
  934. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  935. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  936. //发送短信
  937. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  938. StoreOrderModel::commitTrans();
  939. return Json::successful('核销成功!');
  940. } else {
  941. StoreOrderModel::rollbackTrans();
  942. return Json::fail('核销失败');
  943. }
  944. }
  945. public function re_pay($id)
  946. {
  947. if (!$id) return $this->failed('数据不存在');
  948. $product = StoreOrderModel::where('id',$id)->find();
  949. if (!$product) return Json::fail('数据不存在!');
  950. $f = [];
  951. $f[] = Form::input('order_id', '订单编号', $product->getData('order_id'))->disabled(1);
  952. $f[] = Form::number('total_price', '商品总价', $product->getData('total_price'))->min(0);
  953. $f[] = Form::input('auth_code', '支付码','');
  954. $form = Form::make_post_form('修改订单', $f, Url::buildUrl('paysave', array('id' => $id)));
  955. $this->assign(compact('form'));
  956. return $this->fetch('public/form-builder');
  957. }
  958. public function paysave($id)
  959. {
  960. $orderInfo = StoreOrderModel::where('id', $id)->find();
  961. $orderInfo = $orderInfo->toArray();
  962. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  963. $order_id = mt_rand(100,999).'-'.$orderInfo['order_id'];
  964. StoreOrderModel::where('id', $id)->update(['re_order_id'=>$order_id]);
  965. $rs = OrderRepository::jialiePay($order_id,input('auth_code')); //支付结果
  966. if($rs['ret_code']=="00" && $rs['status']=="2")
  967. {
  968. $this->verify($orderInfo['id']);
  969. StoreOrderModel1::paySuccess($orderInfo['order_id'],'jialie');
  970. }
  971. elseif($rs['ret_code']=="00" && $rs['status']=="1")
  972. {
  973. StoreOrderModel1::edit(['jialie_do'=>1],$orderInfo['id']);
  974. }
  975. else
  976. {
  977. return app('json')->fail($rs['ret_msg']);
  978. }
  979. return app('json')->status('success', '订单支付成功', $rs);
  980. }
  981. }