StoreOrder.php 53 KB

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