StoreOrder.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. if ($product['paid'] == 1) {
  550. $f = [];
  551. $f[] = Form::input('order_id', '退押单号', $product->getData('order_id'))->disabled(1);
  552. $f[] = Form::number('refund_price', '退押金额', bcsub($product->getData('deposit'), $product->getData('deposit_back'), 2))->precision(2)->min(0.01);
  553. // $f[] = Form::radio('type', '状态', 1)->options([['label' => '直接退押', 'value' => 1], ['label' => '退押后,返回原状态', 'value' => 2]]);
  554. $form = Form::make_post_form('退押处理', $f, Url::buildUrl('updateExchangeRefundDepositY', array('id' => $id)), 7);
  555. $this->assign(compact('form'));
  556. return $this->fetch('public/form-builder');
  557. } else return Json::fail('数据不存在!');
  558. }
  559. /**
  560. * 退款处理
  561. * @param $id
  562. */
  563. public function updateRefundY($id)
  564. {
  565. $data = Util::postMore([
  566. 'refund_price',
  567. ['type', 1],
  568. ]);
  569. if (!$id) return $this->failed('数据不存在');
  570. $product = StoreOrderModel::get($id);
  571. if (!$product) return Json::fail('数据不存在!');
  572. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  573. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  574. $refund_price = $data['refund_price'];
  575. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  576. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  577. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  578. if ($data['type'] == 1) {
  579. $data['refund_status'] = 2;
  580. } else if ($data['type'] == 2) {
  581. $data['refund_status'] = 0;
  582. }
  583. $type = $data['type'];
  584. unset($data['type']);
  585. // unset($data['refund_price']);
  586. $refund_data['pay_price'] = bcadd($product['pay_price'], $product['deposit'], 2);
  587. $refund_data['refund_price'] = $refund_price;
  588. $refund_data['refund_id'] = $product['order_id'] . time();
  589. if ($product['pay_type'] == 'weixin') {
  590. if ($product['is_channel'] == 1) {//小程序
  591. try {
  592. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  593. } catch (\Exception $e) {
  594. return Json::fail($e->getMessage());
  595. }
  596. } else {//TODO 公众号
  597. try {
  598. WechatService::payOrderRefund($product['order_id'], $refund_data);
  599. } catch (\Exception $e) {
  600. return Json::fail($e->getMessage());
  601. }
  602. }
  603. } else if ($product['pay_type'] == 'yue') {
  604. BaseModel::beginTrans();
  605. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  606. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  607. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  608. try {
  609. OrderRepository::storeOrderYueRefund($product, $refund_data);
  610. } catch (\Exception $e) {
  611. BaseModel::rollbackTrans();
  612. return Json::fail($e->getMessage());
  613. }
  614. $res = $res1 && $res2;
  615. BaseModel::checkTrans($res);
  616. if (!$res) return Json::fail('余额退款失败!');
  617. }
  618. $resEdit = StoreOrderModel::edit($data, $id);
  619. $res = true;
  620. if ($resEdit) {
  621. $data['type'] = $type;
  622. if ($data['type'] == 1) $res = StorePink::setRefundPink($id);
  623. if (!$res) return Json::fail('修改失败');
  624. try {
  625. OrderRepository::storeProductOrderRefundY($data, $id);
  626. } catch (\Exception $e) {
  627. BaseModel::rollbackTrans();
  628. return Json::fail($e->getMessage());
  629. }
  630. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  631. SystemStoreBill::where('category', 'brokerage_price')
  632. //->where('type', 'brokerage')
  633. ->where('link_id', $id)
  634. ->where('pm', 1)
  635. ->where('status', 0)
  636. ->update(['status' => -1]);
  637. UserBill::where('category', 'now_money')
  638. ->where('type', 'brokerage')
  639. ->where('link_id', $id)
  640. ->where('pm', 1)
  641. ->where('status', 0)
  642. ->update(['status' => -1]);
  643. //退佣金
  644. $brokerage_list = UserBill::where('category', 'now_money')
  645. ->where('type', 'brokerage')
  646. ->where('link_id', $id)
  647. ->where('pm', 1)
  648. ->where('status', 1)
  649. ->select();
  650. if ($brokerage_list) {
  651. $brokerage_list = $brokerage_list->toArray();
  652. foreach ($brokerage_list as $item) {
  653. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  654. if ($item['number'] > $usermoney)
  655. $item['number'] = $usermoney;
  656. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  657. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $id, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  658. }
  659. }
  660. //退款扣除用户积分
  661. //购买赠送的积分
  662. $bill_integral = UserBill::where('category', 'integral')
  663. ->where('type', 'gain')
  664. ->where('link_id', $id)
  665. ->where('pm', 1)
  666. ->find();
  667. if ($bill_integral) {
  668. $bill_integral = $bill_integral->toArray();
  669. //用户积分
  670. $user_integral = User::where('uid', $bill_integral['uid'])->value('integral');
  671. if ($bill_integral['number'] > $user_integral)
  672. $bill_integral['number'] = $user_integral;
  673. User::bcDec($bill_integral['uid'], 'integral', $bill_integral['number'], 'uid');
  674. UserBill::expend('退款扣除积分', $bill_integral['uid'], 'integral', 'gain', $bill_integral['number'], $id, bcsub($user_integral, $bill_integral['number'], 2), '订单退款扣除积分' . floatval($bill_integral['number']) . '积分');
  675. }
  676. BaseModel::commitTrans();
  677. return Json::successful('修改成功!');
  678. } else {
  679. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  680. return Json::fail('修改失败!');
  681. }
  682. }
  683. /**
  684. * 订单退押金
  685. * @param Request $request
  686. * @return mixed
  687. * @throws \think\Exception
  688. * @throws \think\db\exception\DataNotFoundException
  689. * @throws \think\db\exception\ModelNotFoundException
  690. * @throws \think\exception\DbException
  691. */
  692. public function updateRefundDepositY($id)
  693. {
  694. $data = Util::postMore([
  695. 'refund_price',
  696. ['type', 1],
  697. ]);
  698. if (!$id) return $this->failed('数据不存在');
  699. $product = StoreOrderModel::get($id);
  700. if (!$product) return Json::fail('数据不存在!');
  701. if ($product['deposit'] == $product['deposit_back']) return Json::fail('已退完押金!不能再退款了');
  702. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  703. $refund_price = $data['refund_price'];
  704. $data['deposit_back'] = bcadd($data['refund_price'], $product['deposit_back'], 2);
  705. $bj = bccomp((float)$product['deposit'], (float)$data['deposit_back'], 2);
  706. if ($bj < 0) return Json::fail('退押金额大于支付押金,请修改退押金额');
  707. if ($data['type'] == 1) {
  708. $data['deposit_status'] = 1;
  709. } else if ($data['type'] == 2) {
  710. $data['deposit_status'] = 0;
  711. }
  712. // $type = $data['type'];
  713. unset($data['type']);
  714. unset($data['refund_price']);
  715. $refund_data['pay_price'] = bcadd($product['pay_price'], $product['deposit'], 2);
  716. $refund_data['refund_price'] = $refund_price;
  717. $refund_data['refund_id'] = 'refund_deposit' . $product['order_id'] . time();
  718. if ($product['pay_type'] == 'weixin') {
  719. if ($product['is_channel'] == 1) {//小程序
  720. try {
  721. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  722. } catch (\Exception $e) {
  723. return Json::fail($e->getMessage());
  724. }
  725. } else {//TODO 公众号
  726. try {
  727. WechatService::payOrderRefund($product['order_id'], $refund_data);
  728. } catch (\Exception $e) {
  729. return Json::fail($e->getMessage());
  730. }
  731. }
  732. } else if ($product['pay_type'] == 'yue') {
  733. BaseModel::beginTrans();
  734. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  735. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  736. $res2 = UserBill::income('商品退押金', $product['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
  737. $res = $res1 && $res2;
  738. BaseModel::checkTrans($res);
  739. if (!$res) return Json::fail('余额退押金失败!');
  740. }
  741. $resEdit = StoreOrderModel::edit($data, $id);
  742. if ($resEdit) {
  743. StoreOrderStatus::setStatus($id, 'refund_deposit', '退押金给用户' . $refund_price . '元');
  744. return Json::successful('修改成功!');
  745. } else {
  746. StoreOrderStatus::setStatus($id, 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
  747. return Json::fail('修改失败!');
  748. }
  749. }
  750. /**
  751. * 订单退押金
  752. * @param Request $request
  753. * @return mixed
  754. * @throws \think\Exception
  755. * @throws \think\db\exception\DataNotFoundException
  756. * @throws \think\db\exception\ModelNotFoundException
  757. * @throws \think\exception\DbException
  758. */
  759. public function updateExchangeRefundDepositY($id)
  760. {
  761. $data = Util::postMore([
  762. 'refund_price',
  763. ['type', 1],
  764. ]);
  765. if (!$id) return $this->failed('数据不存在');
  766. $product = StoreExchangeOrder::get($id);
  767. if (!$product) return Json::fail('数据不存在!');
  768. if ($product['deposit'] == $product['deposit_back']) return Json::fail('已退完押金!不能再退款了');
  769. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  770. $refund_price = $data['refund_price'];
  771. $data['deposit_back'] = bcadd($data['refund_price'], $product['deposit_back'], 2);
  772. $bj = bccomp((float)$product['deposit'], (float)$data['deposit_back'], 2);
  773. if ($bj < 0) return Json::fail('退押金额大于支付押金,请修改退押金额');
  774. if ($data['type'] == 1) {
  775. $data['deposit_status'] = 1;
  776. } else if ($data['type'] == 2) {
  777. $data['deposit_status'] = 0;
  778. }
  779. // $type = $data['type'];
  780. unset($data['type']);
  781. unset($data['refund_price']);
  782. $refund_data['pay_price'] = bcadd($product['pay_price'], $product['deposit'], 2);
  783. $refund_data['refund_price'] = $refund_price;
  784. $refund_data['refund_id'] = 'refund_deposit' . $product['order_id'] . time();
  785. if ($product['pay_type'] == 'weixin') {
  786. if ($product['is_channel'] == 1) {//小程序
  787. try {
  788. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  789. } catch (\Exception $e) {
  790. return Json::fail($e->getMessage());
  791. }
  792. } else {//TODO 公众号
  793. try {
  794. WechatService::payOrderRefund($product['order_id'], $refund_data);
  795. } catch (\Exception $e) {
  796. return Json::fail($e->getMessage());
  797. }
  798. }
  799. } else if ($product['pay_type'] == 'yue') {
  800. BaseModel::beginTrans();
  801. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  802. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  803. $res2 = UserBill::income('商品退押金', $product['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
  804. $res = $res1 && $res2;
  805. BaseModel::checkTrans($res);
  806. if (!$res) return Json::fail('余额退押金失败!');
  807. }
  808. $resEdit = StoreExchangeOrder::edit($data, $id);
  809. if ($resEdit) {
  810. StoreOrderStatus::setStatus($id, 'refund_deposit', '退押金给用户' . $refund_price . '元');
  811. return Json::successful('修改成功!');
  812. } else {
  813. StoreOrderStatus::setStatus($id, 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
  814. return Json::fail('修改失败!');
  815. }
  816. }
  817. public function order_info($oid = '')
  818. {
  819. if (!$oid || !($orderInfo = StoreOrderModel::get($oid)))
  820. return $this->failed('订单不存在!');
  821. $userInfo = User::getUserInfos($orderInfo['uid']);
  822. if ($userInfo['spread_uid']) {
  823. $spread = User::where('uid', $userInfo['spread_uid'])->value('nickname');
  824. } else {
  825. $spread = '';
  826. }
  827. $this->assign(compact('orderInfo', 'userInfo', 'spread'));
  828. return $this->fetch();
  829. }
  830. public function express($oid = '')
  831. {
  832. if (!$oid || !($order = StoreOrderModel::get($oid)))
  833. return $this->failed('订单不存在!');
  834. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  835. $cacheName = $order['order_id'] . $order['delivery_id'];
  836. $result = CacheService::get($cacheName, null);
  837. if ($result === null) {
  838. $result = ExpressService::query($order['delivery_id']);
  839. if (is_array($result) &&
  840. isset($result['result']) &&
  841. isset($result['result']['deliverystatus']) &&
  842. $result['result']['deliverystatus'] >= 3)
  843. $cacheTime = 0;
  844. else
  845. $cacheTime = 1800;
  846. CacheService::set($cacheName, $result, $cacheTime);
  847. }
  848. $this->assign([
  849. 'order' => $order,
  850. 'express' => $result
  851. ]);
  852. return $this->fetch();
  853. }
  854. /**
  855. * 修改配送信息
  856. * @param $id
  857. * @return mixed|\think\response\Json|void
  858. */
  859. public function distribution($id)
  860. {
  861. if (!$id) return $this->failed('数据不存在');
  862. $product = StoreOrderModel::get($id);
  863. if (!$product) return Json::fail('数据不存在!');
  864. $f = [];
  865. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  866. if ($product['delivery_type'] == 'send') {
  867. $f[] = Form::input('delivery_name', '送货人姓名', $product->getData('delivery_name'));
  868. $f[] = Form::input('delivery_id', '送货人电话', $product->getData('delivery_id'));
  869. } else if ($product['delivery_type'] == 'express') {
  870. $f[] = Form::select('delivery_name', '快递公司', $product->getData('delivery_name'))->setOptions(function () {
  871. $list = Express::where('is_show', 1)->column('name', 'id');
  872. $menus = [];
  873. foreach ($list as $k => $v) {
  874. $menus[] = ['value' => $v, 'label' => $v];
  875. }
  876. return $menus;
  877. });
  878. $f[] = Form::input('delivery_id', '快递单号', $product->getData('delivery_id'));
  879. }
  880. $form = Form::make_post_form('配送信息', $f, Url::buildUrl('updateDistribution', array('id' => $id)), 7);
  881. $this->assign(compact('form'));
  882. return $this->fetch('public/form-builder');
  883. }
  884. /**
  885. * 修改配送信息
  886. * @param $id
  887. */
  888. public function updateDistribution($id)
  889. {
  890. $data = Util::postMore([
  891. 'delivery_name',
  892. 'delivery_id',
  893. ]);
  894. if (!$id) return $this->failed('数据不存在');
  895. $product = StoreOrderModel::get($id);
  896. if (!$product) return Json::fail('数据不存在!');
  897. if ($product['delivery_type'] == 'send') {
  898. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  899. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  900. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  901. } else if ($product['delivery_type'] == 'express') {
  902. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  903. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  904. }
  905. StoreOrderModel::edit($data, $id);
  906. event('StoreProductOrderDistributionAfter', [$data, $id]);
  907. StoreOrderStatus::setStatus($id, 'distribution', '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id']);
  908. return Json::successful('修改成功!');
  909. }
  910. /**
  911. * 修改退款状态
  912. * @param $id
  913. * @return mixed|\think\response\Json|void
  914. */
  915. public function refund_n($id)
  916. {
  917. if (!$id) return $this->failed('数据不存在');
  918. $product = StoreOrderModel::get($id);
  919. if (!$product) return Json::fail('数据不存在!');
  920. $f[] = Form::input('order_id', '订单号', $product->getData('order_id'))->disabled(1);
  921. $f[] = Form::input('refund_reason', '退款原因')->type('textarea');
  922. $form = Form::make_post_form('退款', $f, Url::buildUrl('updateRefundN', array('id' => $id)));
  923. $this->assign(compact('form'));
  924. return $this->fetch('public/form-builder');
  925. }
  926. /**
  927. * 不退款原因
  928. * @param $id
  929. */
  930. public function updateRefundN($id)
  931. {
  932. $data = Util::postMore([
  933. 'refund_reason',
  934. ]);
  935. if (!$id) return $this->failed('数据不存在');
  936. $product = StoreOrderModel::get($id);
  937. if (!$product) return Json::fail('数据不存在!');
  938. if (!$data['refund_reason']) return Json::fail('请输入退款原因');
  939. $data['refund_status'] = 0;
  940. StoreOrderModel::edit($data, $id);
  941. event('StoreProductOrderRefundNAfter', [$data['refund_reason'], $id]);
  942. StoreOrderStatus::setStatus($id, 'refund_n', '不退款原因:' . $data['refund_reason']);
  943. return Json::successful('修改成功!');
  944. }
  945. /**
  946. * 立即支付
  947. * @param $id
  948. */
  949. public function offline($id)
  950. {
  951. $res = StoreOrderModel::updateOffline($id);
  952. if ($res) {
  953. event('StoreProductOrderOffline', [$id]);
  954. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  955. return Json::successful('修改成功!');
  956. } else {
  957. return Json::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  958. }
  959. }
  960. /**
  961. * 修改积分和金额
  962. * @param $id
  963. * @return mixed|\think\response\Json|void
  964. */
  965. public function integral_back($id)
  966. {
  967. if (!$id) return $this->failed('数据不存在');
  968. $product = StoreOrderModel::get($id);
  969. if (!$product) return Json::fail('数据不存在!');
  970. if ($product['paid'] == 1) {
  971. $f[] = Form::input('order_id', '退款单号', $product->getData('order_id'))->disabled(1);
  972. $f[] = Form::number('use_integral', '使用的积分', $product->getData('use_integral'))->min(0)->disabled(1);
  973. $f[] = Form::number('use_integrals', '已退积分', $product->getData('back_integral'))->min(0)->disabled(1);
  974. $f[] = Form::number('back_integral', '可退积分', bcsub($product->getData('use_integral'), $product->getData('use_integral')))->min(0);
  975. $form = Form::make_post_form('退积分', $f, Url::buildUrl('updateIntegralBack', array('id' => $id)), 7);
  976. $this->assign(compact('form'));
  977. return $this->fetch('public/form-builder');
  978. } else {
  979. return Json::fail('参数错误!');
  980. }
  981. return $this->fetch('public/form-builder');
  982. }
  983. /**
  984. * 退积分保存
  985. * @param $id
  986. */
  987. public function updateIntegralBack($id)
  988. {
  989. $data = Util::postMore([
  990. 'back_integral',
  991. ]);
  992. if (!$id) return $this->failed('数据不存在');
  993. $product = StoreOrderModel::get($id);
  994. if (!$product) return Json::fail('数据不存在!');
  995. if ($data['back_integral'] <= 0) return Json::fail('请输入积分');
  996. if ($product['use_integral'] == $product['back_integral']) return Json::fail('已退完积分!不能再积分了');
  997. $back_integral = $data['back_integral'];
  998. $data['back_integral'] = bcadd($data['back_integral'], $product['back_integral'], 2);
  999. $bj = bccomp((float)$product['use_integral'], (float)$data['back_integral'], 2);
  1000. if ($bj < 0) return Json::fail('退积分大于支付积分,请修改退积分');
  1001. BaseModel::beginTrans();
  1002. $integral = User::where('uid', $product['uid'])->value('integral');
  1003. $res1 = User::bcInc($product['uid'], 'integral', $back_integral, 'uid');
  1004. $res2 = UserBill::income('商品退积分', $product['uid'], 'integral', 'pay_product_integral_back', $back_integral, $product['id'], bcadd($integral, $back_integral, 2), '订单退积分' . floatval($back_integral) . '积分到用户积分');
  1005. event('StoreOrderIntegralBack', [$product, $back_integral]);
  1006. try {
  1007. OrderRepository::storeOrderIntegralBack($product, $back_integral);
  1008. } catch (\Exception $e) {
  1009. BaseModel::rollbackTrans();
  1010. return Json::fail($e->getMessage());
  1011. }
  1012. $res = $res1 && $res2;
  1013. BaseModel::checkTrans($res);
  1014. if (!$res) return Json::fail('退积分失败!');
  1015. if ($product['pay_price'] == 0 && $bj == 0) {
  1016. $data['refund_status'] = 2;
  1017. }
  1018. StoreOrderModel::edit($data, $id);
  1019. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  1020. return Json::successful('退积分成功!');
  1021. }
  1022. public function remark()
  1023. {
  1024. $data = Util::postMore(['id', 'remark']);
  1025. if (!$data['id']) return Json::fail('参数错误!');
  1026. if ($data['remark'] == '') return Json::fail('请输入要备注的内容!');
  1027. $id = $data['id'];
  1028. unset($data['id']);
  1029. StoreOrderModel::edit($data, $id);
  1030. return Json::successful('备注成功!');
  1031. }
  1032. public function order_status($oid)
  1033. {
  1034. if (!$oid) return $this->failed('数据不存在');
  1035. $this->assign(StoreOrderStatus::systemPage($oid));
  1036. return $this->fetch();
  1037. }
  1038. /*
  1039. * 订单列表推荐人详细
  1040. */
  1041. public function order_spread_user($uid)
  1042. {
  1043. $spread = User::where('uid', $uid)->find();
  1044. $this->assign('spread', $spread);
  1045. return $this->fetch();
  1046. }
  1047. /**
  1048. * 立即核销
  1049. * @param $id
  1050. */
  1051. public function verify($id)
  1052. {
  1053. StoreOrderModel::beginTrans();
  1054. $orderInfo = StoreOrderModel::where('id', $id)->find();
  1055. if (!$orderInfo) return Json::fail('核销订单不存在!');
  1056. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  1057. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  1058. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  1059. if ($res) return Json::fail('拼团订单暂未成功无法核销!');
  1060. }
  1061. $orderInfo->status = 2;
  1062. if ($orderInfo->save()) {
  1063. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  1064. StoreOrderStatus::setStatus($orderInfo->id, 'take_delivery', '已核销');
  1065. //发送短信
  1066. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  1067. StoreOrderModel::commitTrans();
  1068. return Json::successful('核销成功!');
  1069. } else {
  1070. StoreOrderModel::rollbackTrans();
  1071. return Json::fail('核销失败');
  1072. }
  1073. }
  1074. /**
  1075. * 立即核销
  1076. * @param $id
  1077. */
  1078. public function exchange_verify($id)
  1079. {
  1080. StoreExchangeOrder::beginTrans();
  1081. $orderInfo = StoreExchangeOrder::where('id', $id)->find();
  1082. if (!$orderInfo) return Json::fail('核销订单不存在!');
  1083. if ($orderInfo->status > 0) return Json::fail('订单已核销!');
  1084. $orderInfo->status = 1;
  1085. if ($orderInfo->save()) {
  1086. StoreExchangeOrder::commitTrans();
  1087. return Json::successful('核销成功!');
  1088. } else {
  1089. StoreExchangeOrder::rollbackTrans();
  1090. return Json::fail('核销失败');
  1091. }
  1092. }
  1093. }