StoreOrder.php 49 KB

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