StoreOrder.php 49 KB

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