combinationOrder.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\model\system\Express;
  4. use crmeb\repositories\OrderRepository;
  5. use crmeb\services\{ExpressService,
  6. MiniProgramService,
  7. WechatService,
  8. CacheService,
  9. UtilService as Util,
  10. JsonService as Json,
  11. FormBuilder as Form};
  12. use crmeb\basic\BaseModel;
  13. use think\facade\Route as Url;
  14. use app\admin\controller\AuthController;
  15. use app\admin\model\order\{StoreOrderStatus, StoreOrder as StoreOrderModel};
  16. use app\admin\model\ump\StorePink;
  17. use app\admin\model\user\{User, UserBill};
  18. /**
  19. * 订单管理控制器
  20. * Class StoreOrder
  21. * @package app\admin\controller\store
  22. */
  23. class CombinationOrder extends AuthController
  24. {
  25. /**
  26. * @return mixed
  27. */
  28. public function index()
  29. {
  30. $where = Util::getMore([
  31. ['status', ''],
  32. ['real_name', ''],
  33. ['is_del', 0],
  34. ['data', ''],
  35. ['combination_id', ''],
  36. ['export', 0],
  37. ['order', 'id desc']
  38. ], $this->request);
  39. $limitTimeList = [
  40. 'yesterday' => implode(' - ', [date('Y/m/d', strtotime('-1 day')), date('Y/m/d')]),
  41. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  42. 'week' => implode(' - ', [
  43. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  44. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  45. ]),
  46. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  47. 'quarter' => implode(' - ', [
  48. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  49. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  50. ]),
  51. 'year' => implode(' - ', [
  52. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  53. ])
  54. ];
  55. $this->assign(StoreOrderModel::systemPage($where, $this->adminId));
  56. $this->assign('price', StoreOrderModel::getOrderPrice($where));
  57. $this->assign(compact('where', 'limitTimeList'));
  58. return $this->fetch();
  59. }
  60. public function orderchart()
  61. {
  62. $where = Util::getMore([
  63. ['status', ''],
  64. ['real_name', ''],
  65. ['is_del', 0],
  66. ['data', ''],
  67. ['combination_id', ''],
  68. ['export', 0],
  69. ['order', 'id desc']
  70. ], $this->request);
  71. $limitTimeList = [
  72. 'today' => implode(' - ', [date('Y/m/d'), date('Y/m/d', strtotime('+1 day'))]),
  73. 'week' => implode(' - ', [
  74. date('Y/m/d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)),
  75. date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600))
  76. ]),
  77. 'month' => implode(' - ', [date('Y/m') . '/01', date('Y/m') . '/' . date('t')]),
  78. 'quarter' => implode(' - ', [
  79. date('Y') . '/' . (ceil((date('n')) / 3) * 3 - 3 + 1) . '/01',
  80. date('Y') . '/' . (ceil((date('n')) / 3) * 3) . '/' . date('t', mktime(0, 0, 0, (ceil((date('n')) / 3) * 3), 1, date('Y')))
  81. ]),
  82. 'year' => implode(' - ', [
  83. date('Y') . '/01/01', date('Y/m/d', strtotime(date('Y') . '/01/01 + 1year -1 day'))
  84. ])
  85. ];
  86. if ($where['data'] == '') $where['data'] = $limitTimeList['today'];
  87. $orderCount = [
  88. urlencode('未支付') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(0))->count(),
  89. urlencode('未发货') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(1))->count(),
  90. urlencode('待收货') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(2))->count(),
  91. urlencode('待评价') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(3))->count(),
  92. urlencode('交易完成') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(4))->count(),
  93. urlencode('退款中') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(-1))->count(),
  94. urlencode('已退款') => StoreOrderModel::getOrderWhere($where, StoreOrderModel::statusByWhere(-2))->count()
  95. ];
  96. $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')
  97. ->group('from_unixtime(add_time,\'%Y-%m-%d\')');
  98. $orderPrice = $model->select()->toArray();
  99. $orderDays = [];
  100. $orderCategory = [
  101. ['name' => '商品数', 'type' => 'line', 'data' => []],
  102. ['name' => '订单数', 'type' => 'line', 'data' => []],
  103. ['name' => '订单金额', 'type' => 'line', 'data' => []],
  104. ['name' => '退款金额', 'type' => 'line', 'data' => []]
  105. ];
  106. foreach ($orderPrice as $price) {
  107. $orderDays[] = $price['add_time'];
  108. $orderCategory[0]['data'][] = $price['total_num'];
  109. $orderCategory[1]['data'][] = $price['count'];
  110. $orderCategory[2]['data'][] = $price['total_price'];
  111. $orderCategory[3]['data'][] = $price['refund_price'];
  112. }
  113. $this->assign(StoreOrderModel::systemPage($where, $this->adminId));
  114. $this->assign('price', StoreOrderModel::getOrderPrice($where));
  115. $this->assign(compact('limitTimeList', 'where', 'orderCount', 'orderPrice', 'orderDays', 'orderCategory'));
  116. return $this->fetch();
  117. }
  118. /**
  119. * 修改支付金额等
  120. * @param $id
  121. * @return mixed|\think\response\Json|void
  122. */
  123. public function edit($id)
  124. {
  125. if (!$id) return $this->failed('数据不存在');
  126. $product = StoreOrderModel::get($id);
  127. if (!$product) return Json::fail('数据不存在!');
  128. $this->assign([
  129. 'title' => '修改订单', 'rules' => $this->read($id)->getContent(),
  130. 'action' => Url::buildUrl('update', array('id' => $id))
  131. ]);
  132. return $this->fetch('public/common_form');
  133. }
  134. public function read($id)
  135. {
  136. if (!$id) return $this->failed('数据不存在');
  137. $product = StoreOrderModel::get($id);
  138. if (!$product) return Json::fail('数据不存在!');
  139. Form::text('order_id', '订单编号', $product->getData('order_id'))->readonly();
  140. Form::number('total_price', '商品总价', $product->getData('total_price'))->min(0);
  141. Form::number('total_postage', '原始邮费', $product->getData('total_postage'))->min(0);
  142. Form::number('pay_price', '实际支付金额', $product->getData('pay_price'))->min(0);
  143. Form::number('pay_postage', '实际支付邮费', $product->getData('pay_postage'))->min(0);
  144. Form::number('gain_integral', '赠送积分', $product->getData('gain_integral'))->min(0);
  145. return Form::builder();
  146. }
  147. public function update($id)
  148. {
  149. $data = Util::postMore([
  150. 'order_id',
  151. 'total_price',
  152. 'total_postage',
  153. 'pay_price',
  154. 'pay_postage',
  155. 'gain_integral',
  156. ]);
  157. if ($data['total_price'] <= 0) return Json::fail('请输入商品总价');
  158. if ($data['pay_price'] <= 0) return Json::fail('请输入实际支付金额');
  159. $data['order_id'] = StoreOrderModel::changeOrderId($data['order_id']);
  160. StoreOrderModel::edit($data, $id);
  161. event('StoreProductOrderEditAfter', [$data, $id]);
  162. StoreOrderStatus::setStatus($id, 'order_edit', '修改商品总价为:' . $data['total_price'] . ' 实际支付金额' . $data['pay_price']);
  163. return Json::successful('修改成功!');
  164. }
  165. /**
  166. * 送货
  167. * @param $id
  168. * send
  169. */
  170. public function delivery($id)
  171. {
  172. if (!$id) return $this->failed('数据不存在');
  173. $product = StoreOrderModel::get($id);
  174. if (!$product) return Json::fail('数据不存在!');
  175. if ($product['paid'] == 1 && $product['status'] == 0) {
  176. $this->assign([
  177. 'title' => '送货信息', 'rules' => $this->readDelivery($id)->getContent(),
  178. 'action' => Url::buildUrl('updateDelivery', array('id' => $id))
  179. ]);
  180. return $this->fetch('public/common_form');
  181. } else return Json::fail('数据不存在!');
  182. }
  183. public function readDelivery($id)
  184. {
  185. if (!$id) return $this->failed('数据不存在');
  186. $product = StoreOrderModel::get($id);
  187. if (!$product) return Json::fail('数据不存在!');
  188. Form::text('delivery_name', '送货人姓名');
  189. Form::text('delivery_id', '送货人电话')->number();
  190. return Form::builder();
  191. }
  192. public function updateDelivery($id)
  193. {
  194. $data = Util::postMore([
  195. 'delivery_name',
  196. 'delivery_id',
  197. ]);
  198. $data['delivery_type'] = 'send';
  199. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  200. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  201. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  202. $data['status'] = 1;
  203. StoreOrderModel::edit($data, $id);
  204. event('storeProductOrderDeliveryAfter', [$data, $id]);
  205. StoreOrderStatus::setStatus($id, 'delivery', '已配送 发货人:' . $data['delivery_name'] . ' 发货人电话:' . $data['delivery_id']);
  206. return Json::successful('修改成功!');
  207. }
  208. /**
  209. * 发货
  210. * @param $id
  211. * express
  212. */
  213. public function deliver_goods($id)
  214. {
  215. if (!$id) return $this->failed('数据不存在');
  216. $product = StoreOrderModel::get($id);
  217. if (!$product) return Json::fail('数据不存在!');
  218. if ($product['paid'] == 1 && $product['status'] == 0) {
  219. $this->assign([
  220. 'title' => '发货信息', 'rules' => $this->readDeliveryGoods($id)->getContent(),
  221. 'action' => Url::buildUrl('updateDeliveryGoods', array('id' => $id))
  222. ]);
  223. return $this->fetch('public/common_form');
  224. } else return Json::fail('数据不存在!');
  225. }
  226. public function readDeliveryGoods($id)
  227. {
  228. if (!$id) return $this->failed('数据不存在');
  229. $product = StoreOrderModel::get($id);
  230. if (!$product) return Json::fail('数据不存在!');
  231. Form::select('delivery_name', '快递公司', function () {
  232. $list = Express::where('is_show', 1)->order('sort DESC')->column('name', 'id');
  233. $menus = [];
  234. foreach ($list as $k => $v) {
  235. $menus[] = ['value' => $v, 'label' => $v];
  236. }
  237. return $menus;
  238. })->filterable();
  239. Form::text('delivery_id', '快递单号');
  240. return Form::builder();
  241. }
  242. public function updateDeliveryGoods($id)
  243. {
  244. $data = Util::postMore([
  245. 'delivery_name',
  246. 'delivery_id',
  247. ]);
  248. $data['delivery_type'] = 'express';
  249. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  250. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  251. $data['status'] = 1;
  252. StoreOrderModel::edit($data, $id);
  253. event('StoreProductOrderDeliveryGoodsAfter', [$data, $id]);
  254. StoreOrderStatus::setStatus($id, 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  255. return Json::successful('修改成功!');
  256. }
  257. /**
  258. * 修改状态为已收货
  259. * @param $id
  260. * @return \think\response\Json|void
  261. */
  262. public function take_delivery($id)
  263. {
  264. if (!$id) return $this->failed('数据不存在');
  265. $product = StoreOrderModel::get($id);
  266. if (!$product) return Json::fail('数据不存在!');
  267. if ($product['status'] == 2) return Json::fail('不能重复收货!');
  268. if ($product['paid'] == 1 && $product['status'] == 1) $data['status'] = 2;
  269. else if ($product['pay_type'] == 'offline') $data['status'] = 2;
  270. else return Json::fail('请先发货或者送货!');
  271. if (!StoreOrderModel::edit($data, $id))
  272. return Json::fail(StoreOrderModel::getErrorInfo('收货失败,请稍候再试!'));
  273. else {
  274. try {
  275. OrderRepository::storeProductOrderTakeDeliveryAdmin($data, $id);
  276. } catch (\Exception $e) {
  277. return Json::fail($e->getMessage());
  278. }
  279. StoreOrderStatus::setStatus($id, 'take_delivery', '已收货');
  280. return Json::successful('收货成功!');
  281. }
  282. }
  283. /**
  284. * 修改退款状态
  285. * @param $id
  286. * @return \think\response\Json|void
  287. */
  288. public function refund_y($id)
  289. {
  290. if (!$id) return $this->failed('数据不存在');
  291. $product = StoreOrderModel::get($id);
  292. if (!$product) return Json::fail('数据不存在!');
  293. if ($product['paid'] == 1) {
  294. $this->assign([
  295. 'title' => '退款', 'rules' => $this->readRefundY($id)->getContent(),
  296. 'action' => Url::buildUrl('updateRefundY', array('id' => $id))
  297. ]);
  298. return $this->fetch('public/common_form');
  299. } else return Json::fail('数据不存在!');
  300. }
  301. public function readRefundY($id)
  302. {
  303. if (!$id) return $this->failed('数据不存在');
  304. $product = StoreOrderModel::get($id);
  305. if (!$product) return Json::fail('数据不存在!');
  306. Form::text('order_id', '退款单号', $product->getData('order_id'))->readonly();
  307. Form::number('refund_price', '退款金额', $product->getData('pay_price'))->min(0);
  308. Form::radio('type', '状态', [['label' => '直接退款', 'value' => 1], ['label' => '退款后,返回原状态', 'value' => 2]], 1);
  309. return Form::builder();
  310. }
  311. public function updateRefundY($id)
  312. {
  313. $data = Util::postMore([
  314. 'refund_price',
  315. ['type', 1],
  316. ]);
  317. if (!$id) return $this->failed('数据不存在');
  318. $product = StoreOrderModel::get($id);
  319. if (!$product) return Json::fail('数据不存在!');
  320. if ($product['pay_price'] == $product['refund_price']) return Json::fail('已退完支付金额!不能再退款了');
  321. if (!$data['refund_price']) return Json::fail('请输入退款金额');
  322. $refund_price = $data['refund_price'];
  323. $data['refund_price'] = bcadd($data['refund_price'], $product['refund_price'], 2);
  324. $bj = bccomp((float)$product['pay_price'], (float)$data['refund_price'], 2);
  325. if ($bj < 0) return Json::fail('退款金额大于支付金额,请修改退款金额');
  326. if ($data['type'] == 1) {
  327. $data['refund_status'] = 2;
  328. } else if ($data['type'] == 2) {
  329. $data['refund_status'] = 0;
  330. }
  331. $type = $data['type'];
  332. unset($data['type']);
  333. $refund_data['pay_price'] = $product['pay_price'];
  334. $refund_data['refund_price'] = $refund_price;
  335. if ($product['pay_type'] == 'weixin') {
  336. if ($product['is_channel'] == 1) {//TODO 小程序
  337. try {
  338. MiniProgramService::payOrderRefund($product['order_id'], $refund_data);//2.5.36
  339. } catch (\Exception $e) {
  340. return Json::fail($e->getMessage());
  341. }
  342. } else { //TODO 公众号
  343. try {
  344. WechatService::payOrderRefund($product['order_id'], $refund_data);
  345. } catch (\Exception $e) {
  346. return Json::fail($e->getMessage());
  347. }
  348. }
  349. } else if ($product['pay_type'] == 'yue') {
  350. BaseModel::beginTrans();
  351. $usermoney = User::where('uid', $product['uid'])->value('now_money');
  352. $res1 = User::bcInc($product['uid'], 'now_money', $refund_price, 'uid');
  353. $res2 = $res2 = UserBill::income('商品退款', $product['uid'], 'now_money', 'pay_product_refund', $refund_price, $product['id'], bcadd($usermoney, $refund_price, 2), '订单退款到余额' . floatval($refund_price) . '元');
  354. try {
  355. OrderRepository::storeOrderYueRefund($product, $refund_data);
  356. } catch (\Exception $e) {
  357. BaseModel::rollbackTrans();
  358. return Json::fail($e->getMessage());
  359. }
  360. $res = $res1 && $res2;
  361. BaseModel::checkTrans($res);
  362. if (!$res) return Json::fail('余额退款失败!');
  363. }
  364. $resEdit = StoreOrderModel::edit($data, $id);
  365. if ($resEdit) {
  366. $data['type'] = $type;
  367. if ($data['type'] == 1) StorePink::setRefundPink($id);
  368. try {
  369. OrderRepository::storeProductOrderRefundY($data, $id);
  370. } catch (\Exception $e) {
  371. return Json::fail($e->getMessage());
  372. }
  373. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元');
  374. return Json::successful('修改成功!');
  375. } else {
  376. StoreOrderStatus::setStatus($id, 'refund_price', '退款给用户' . $refund_price . '元失败');
  377. return Json::fail('修改失败!');
  378. }
  379. }
  380. public function order_info($oid = '')
  381. {
  382. if (!$oid || !($orderInfo = StoreOrderModel::get($oid)))
  383. return $this->failed('订单不存在!');
  384. $userInfo = User::getUserInfo($orderInfo['uid']);
  385. if ($userInfo['spread_uid']) {
  386. $spread = User::where('uid', $userInfo['spread_uid'])->value('nickname');
  387. } else {
  388. $spread = '';
  389. }
  390. $this->assign(compact('orderInfo', 'userInfo', 'spread'));
  391. return $this->fetch();
  392. }
  393. public function express($oid = '')
  394. {
  395. if (!$oid || !($order = StoreOrderModel::get($oid)))
  396. return $this->failed('订单不存在!');
  397. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return $this->failed('该订单不存在快递单号!');
  398. $cacheName = $order['order_id'] . $order['delivery_id'];
  399. $result = CacheService::get($cacheName, null);
  400. if ($result === null) {
  401. $result = ExpressService::query($order['delivery_id']);
  402. if (is_array($result) &&
  403. isset($result['result']) &&
  404. isset($result['result']['deliverystatus']) &&
  405. $result['result']['deliverystatus'] >= 3)
  406. $cacheTime = 0;
  407. else
  408. $cacheTime = 1800;
  409. CacheService::set($cacheName, $result, $cacheTime);
  410. }
  411. $this->assign([
  412. 'order' => $order,
  413. 'express' => $result
  414. ]);
  415. return $this->fetch();
  416. }
  417. /**
  418. * 修改配送信息
  419. * @param $id
  420. * @return mixed|\think\response\Json|void
  421. */
  422. public function distribution($id)
  423. {
  424. if (!$id) return $this->failed('数据不存在');
  425. $product = StoreOrderModel::get($id);
  426. if (!$product) return Json::fail('数据不存在!');
  427. $this->assign([
  428. 'title' => '配送信息', 'rules' => $this->readDistribution($id)->getContent(),
  429. 'action' => Url::buildUrl('updateDistribution', array('id' => $id))
  430. ]);
  431. return $this->fetch('public/common_form');
  432. }
  433. public function readDistribution($id)
  434. {
  435. if (!$id) return $this->failed('数据不存在');
  436. $product = StoreOrderModel::get($id);
  437. if (!$product) return Json::fail('数据不存在!');
  438. if ($product['delivery_type'] == 'send') {
  439. Form::text('delivery_name', '送货人姓名', $product->getData('delivery_name'));
  440. Form::text('delivery_id', '送货人电话', $product->getData('delivery_id'))->number();
  441. } else if ($product['delivery_type'] == 'express') {
  442. Form::select('delivery_name', '快递公司', function () {
  443. $list = Express::where('is_show', 1)->column('name', 'id');
  444. $menus = [];
  445. foreach ($list as $k => $v) {
  446. $menus[] = ['value' => $v, 'label' => $v];
  447. }
  448. return $menus;
  449. }, $product->getData('delivery_name'))->filterable();
  450. Form::text('delivery_id', '快递单号', $product->getData('delivery_id'));
  451. }
  452. return Form::builder();
  453. }
  454. public function updateDistribution($id)
  455. {
  456. $data = Util::postMore([
  457. 'delivery_name',
  458. 'delivery_id',
  459. ]);
  460. if (!$id) return $this->failed('数据不存在');
  461. $product = StoreOrderModel::get($id);
  462. if (!$product) return Json::fail('数据不存在!');
  463. if ($product['delivery_type'] == 'send') {
  464. if (!$data['delivery_name']) return Json::fail('请输入送货人姓名');
  465. if (!(int)$data['delivery_id']) return Json::fail('请输入送货人电话号码');
  466. else if (!preg_match("/^1[3456789]{1}\d{9}$/", $data['delivery_id'])) return Json::fail('请输入正确的送货人电话号码');
  467. } else if ($product['delivery_type'] == 'express') {
  468. if (!$data['delivery_name']) return Json::fail('请选择快递公司');
  469. if (!$data['delivery_id']) return Json::fail('请输入快递单号');
  470. }
  471. StoreOrderModel::edit($data, $id);
  472. event('StoreProductOrderDistributionAfter', [$data, $id]);
  473. StoreOrderStatus::setStatus($id, 'distribution', '修改发货信息为' . $data['delivery_name'] . '号' . $data['delivery_id']);
  474. return Json::successful('修改成功!');
  475. }
  476. /**
  477. * 修改退款状态
  478. * @param $id
  479. * @return mixed|\think\response\Json|void
  480. */
  481. public function refund_n($id)
  482. {
  483. if (!$id) return $this->failed('数据不存在');
  484. $product = StoreOrderModel::get($id);
  485. if (!$product) return Json::fail('数据不存在!');
  486. $this->assign([
  487. 'title' => '退款', 'rules' => $this->readRefundN($id)->getContent(),
  488. 'action' => Url::buildUrl('updateRefundN', array('id' => $id))
  489. ]);
  490. return $this->fetch('public/common_form');
  491. }
  492. public function readRefundN($id)
  493. {
  494. if (!$id) return $this->failed('数据不存在');
  495. $product = StoreOrderModel::get($id);
  496. if (!$product) return Json::fail('数据不存在!');
  497. Form::text('order_id', '退款单号', $product->getData('order_id'))->readonly();
  498. Form::textarea('refund_reason', '退款原因');
  499. return Form::builder();
  500. }
  501. public function updateRefundN($id)
  502. {
  503. $data = Util::postMore([
  504. 'refund_reason',
  505. ]);
  506. if (!$id) return $this->failed('数据不存在');
  507. $product = StoreOrderModel::get($id);
  508. if (!$product) return Json::fail('数据不存在!');
  509. if (!$data['refund_reason']) return Json::fail('请输入退款原因');
  510. $data['refund_status'] = 0;
  511. StoreOrderModel::edit($data, $id);
  512. event('StoreProductOrderRefundNAfter', [$data['refund_reason'], $id]);
  513. StoreOrderStatus::setStatus($id, 'refund_n', '不退款原因:' . $data['refund_reason']);
  514. return Json::successful('修改成功!');
  515. }
  516. /**
  517. * 立即支付
  518. * @param $id
  519. */
  520. public function offline($id)
  521. {
  522. $res = StoreOrderModel::updateOffline($id);
  523. if ($res) {
  524. event('StoreProductOrderOffline', [$id]);
  525. StoreOrderStatus::setStatus($id, 'offline', '线下付款');
  526. return Json::successful('修改成功!');
  527. } else {
  528. return Json::fail(StoreOrderModel::getErrorInfo('修改失败!'));
  529. }
  530. }
  531. /**
  532. * 修改积分和金额
  533. * @param $id
  534. * @return mixed|\think\response\Json|void
  535. */
  536. public function integral_back($id)
  537. {
  538. if (!$id) return $this->failed('数据不存在');
  539. $product = StoreOrderModel::get($id);
  540. if (!$product) return Json::fail('数据不存在!');
  541. if ($product['paid'] == 1) {
  542. $this->assign([
  543. 'title' => '退积分', 'rules' => $this->readIntegralBack($id)->getContent(),
  544. 'action' => Url::buildUrl('updateIntegralBack', array('id' => $id))
  545. ]);
  546. } else {
  547. return Json::fail('参数错误!');
  548. }
  549. return $this->fetch('public/common_form');
  550. }
  551. public function readIntegralBack($id)
  552. {
  553. if (!$id) return $this->failed('数据不存在');
  554. $product = StoreOrderModel::get($id);
  555. if (!$product) return Json::fail('数据不存在!');
  556. Form::text('order_id', '退积分单号', $product->getData('order_id'))->readonly();
  557. Form::number('back_integral', '退积分')->min(0);
  558. return Form::builder();
  559. }
  560. public function updateIntegralBack($id)
  561. {
  562. $data = Util::postMore([
  563. 'back_integral',
  564. ]);
  565. if (!$id) return $this->failed('数据不存在');
  566. $product = StoreOrderModel::get($id);
  567. if (!$product) return Json::fail('数据不存在!');
  568. if ($data['back_integral'] <= 0) return Json::fail('请输入积分');
  569. if ($product['use_integral'] == $product['back_integral']) return Json::fail('已退完积分!不能再积分了');
  570. $back_integral = $data['back_integral'];
  571. $data['back_integral'] = bcadd($data['back_integral'], $product['back_integral'], 2);
  572. $bj = bccomp((float)$product['use_integral'], (float)$data['back_integral'], 2);
  573. if ($bj < 0) return Json::fail('退积分大于支付积分,请修改退积分');
  574. BaseModel::beginTrans();
  575. $integral = User::where('uid', product['uid'])->value('integral');
  576. $res1 = User::bcInc($product['uid'], 'integral', $back_integral, 'uid');
  577. $res2 = UserBill::income('商品退积分', $product['uid'], 'integral', 'pay_product_integral_back', $back_integral, $product['id'], bcadd($integral, $back_integral, 2), '订单退积分' . floatval($back_integral) . '积分到用户积分');
  578. try {
  579. OrderRepository::storeOrderIntegralBack($product, $back_integral);
  580. } catch (\Exception $e) {
  581. BaseModel::rollbackTrans();
  582. return Json::fail($e->getMessage());
  583. }
  584. $res = $res1 && $res2;
  585. BaseModel::checkTrans($res);
  586. if (!$res) return Json::fail('退积分失败!');
  587. StoreOrderModel::edit($data, $id);
  588. StoreOrderStatus::setStatus($id, 'integral_back', '商品退积分:' . $data['back_integral']);
  589. return Json::successful('退积分成功!');
  590. }
  591. public function remark()
  592. {
  593. $data = Util::postMore(['id', 'remark']);
  594. if (!$data['id']) return Json::fail('参数错误!');
  595. if ($data['remark'] == '') return Json::fail('请输入要备注的内容!');
  596. $id = $data['id'];
  597. unset($data['id']);
  598. StoreOrderModel::edit($data, $id);
  599. return Json::successful('备注成功!');
  600. }
  601. public function order_status($oid)
  602. {
  603. if (!$oid) return $this->failed('数据不存在');
  604. $this->assign(StoreOrderStatus::systemPage($oid));
  605. return $this->fetch();
  606. }
  607. }