StoreOrder.php 49 KB

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