StoreOrder.php 49 KB

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