StoreOrderController.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. <?php
  2. namespace app\api\controller\admin;
  3. use app\admin\model\order\StoreOrder as StoreOrderModel;
  4. use app\admin\model\system\SystemStoreBill;
  5. use app\admin\model\system\SystemStoreExtract;
  6. use app\admin\model\system\SystemStorePoint;
  7. use app\admin\model\system\SystemStoreProductStock;
  8. use app\admin\model\system\SystemStoreSender;
  9. use app\admin\model\user\UserRecharge;
  10. use app\models\system\SystemStore;
  11. use app\Request;
  12. use crmeb\basic\BaseModel;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use app\models\user\{
  17. User, UserBill
  18. };
  19. use crmeb\repositories\OrderRepository;
  20. use crmeb\repositories\ShortLetterRepositories;
  21. use crmeb\services\{
  22. MiniProgramService, UtilService, WechatService
  23. };
  24. use app\models\store\{Card, StoreCart, StoreExchangeOrder, StoreOrder, StoreOrderStatus, StorePink, StoreService};
  25. use app\models\system\SystemStoreStaff;
  26. /**
  27. * 订单类
  28. * Class StoreOrderController
  29. * @package app\api\controller\admin\order
  30. */
  31. class StoreOrderController
  32. {
  33. public function pointList(Request $request)
  34. {
  35. $uid = $request->uid();
  36. $where = UtilService::getMore([
  37. ['page', 1],
  38. ['limit', 20],
  39. ['type', 1]
  40. ]);
  41. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  42. if (!$info) {
  43. return app('json')->fail('权限不足');
  44. }
  45. // var_dump($info->toArray());
  46. $where['store_id'] = $info['store_id'];
  47. $where['excel'] = 0;
  48. // $where['type'] = $info['store_id'];
  49. return app('json')->successful('ok', SystemStorePoint::getStoreList($where));
  50. }
  51. public function senderList(Request $request)
  52. {
  53. $uid = $request->uid();
  54. $where = UtilService::getMore([
  55. ['page', 1],
  56. ['limit', 20],
  57. ]);
  58. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  59. if (!$info) {
  60. return app('json')->fail('权限不足');
  61. }
  62. // var_dump($info->toArray());
  63. $where['store_id'] = $info['store_id'];
  64. $where['excel'] = 0;
  65. // $where['type'] = $info['store_id'];
  66. return app('json')->successful('ok', SystemStoreSender::getStoreList($where));
  67. }
  68. public function pointDetail($id, Request $request)
  69. {
  70. $uid = $request->uid();
  71. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  72. if (!$info) {
  73. return app('json')->fail('权限不足');
  74. }
  75. return app('json')->successful('ok', ['data' => SystemStorePoint::getStoreDispose($id)]);
  76. }
  77. public function sendCard(Request $request)
  78. {
  79. $uid = $request->uid();
  80. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  81. if (!$info) {
  82. return app('json')->fail('权限不足');
  83. }
  84. $code = UtilService::postMore(['code']);
  85. $res = Card::sendCard($info['store_id'], $code['code']);
  86. if ($res)
  87. return app('json')->successful('赠送成功');
  88. else return app('json')->fail(Card::getErrorInfo('赠送失败'));
  89. }
  90. public function sendCardList(Request $request)
  91. {
  92. $uid = $request->uid();
  93. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  94. if (!$info) {
  95. return app('json')->fail('权限不足');
  96. }
  97. $where = UtilService::getMore([['page', 1], ['limit', 10], ['status', '']]);
  98. $where['store_id'] = $info['store_id'];
  99. $list = Card::systemPage($where);
  100. unset($where['page']);
  101. unset($where['limit']);
  102. $count = Card::where($where)->count();
  103. return app('json')->success('ok', compact('list', 'count'));
  104. }
  105. public function checkCard(Request $request)
  106. {
  107. $uid = $request->uid();
  108. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  109. if (!$info) {
  110. return app('json')->fail('权限不足');
  111. }
  112. $code = UtilService::postMore(['code', 'password']);
  113. $res = Card::checkCard($info['store_id'], $code['code'], $code['password']);
  114. if ($res)
  115. return app('json')->successful('核销成功');
  116. else return app('json')->fail(Card::getErrorInfo('核销失败'));
  117. }
  118. public function addDetail(Request $request, $id = 0)
  119. {
  120. $uid = $request->uid();
  121. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  122. if (!$info) {
  123. return app('json')->fail('权限不足');
  124. }
  125. $data = UtilService::postMore([
  126. ['name', ''],
  127. ['phone', ''],
  128. ['address', ''],
  129. ['detailed_address', ''],
  130. ['latlng', ''],
  131. ['valid_time', []],
  132. ['day_time', []],
  133. ['is_show', 0],
  134. ]);
  135. $data['store_id'] = $info['store_id'];
  136. $data['address'] = implode(',', $data['address']);
  137. $data['latlng'] = is_string($data['latlng']) ? explode(',', $data['latlng']) : $data['latlng'];
  138. if (!isset($data['latlng'][0]) || !isset($data['latlng'][1])) return app('json')->fail('请选择门店位置');
  139. $data['latitude'] = $data['latlng'][0];
  140. $data['longitude'] = $data['latlng'][1];
  141. $data['valid_time'] = implode(' - ', $data['valid_time']);
  142. $data['day_time'] = implode(' - ', $data['day_time']);
  143. unset($data['latlng']);
  144. if ($id) {
  145. $store_info = SystemStorePoint::get($id);
  146. if ($store_info['store_id'] != $info['store_id']) {
  147. return app('json')->fail('权限不足');
  148. }
  149. if (SystemStorePoint::where('id', $id)->update($data)) {
  150. SystemStorePoint::commitTrans();
  151. return app('json')->successful('修改成功');
  152. } else {
  153. SystemStorePoint::rollbackTrans();
  154. return app('json')->fail('修改失败或者您没有修改什么!');
  155. }
  156. } else {
  157. $data['add_time'] = time();
  158. $data['is_show'] = 1;
  159. if ($res = SystemStorePoint::create($data)) {
  160. SystemStorePoint::commitTrans();
  161. return app('json')->successful('保存成功', ['id' => $res->id]);
  162. } else {
  163. SystemStorePoint::rollbackTrans();
  164. return app('json')->fail('保存失败!');
  165. }
  166. }
  167. }
  168. /**
  169. * 删除恢复门店
  170. * @param $id
  171. */
  172. public function point_delete($id, Request $request)
  173. {
  174. if (!$id) return app('json')->fail('数据不存在');
  175. if (!SystemStorePoint::be(['id' => $id])) return app('json')->fail('数据不存在');
  176. $uid = $request->uid();
  177. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  178. if (!$info) {
  179. return app('json')->fail('权限不足');
  180. }
  181. $store_info = SystemStorePoint::get($id);
  182. if ($store_info['store_id'] != $info['store_id']) {
  183. return app('json')->fail('权限不足');
  184. }
  185. if (SystemStorePoint::be(['id' => $id, 'is_del' => 1])) {
  186. $data['is_del'] = 0;
  187. if (!SystemStorePoint::edit($data, $id))
  188. return app('json')->fail(SystemStorePoint::getErrorInfo('恢复失败,请稍候再试!'));
  189. else
  190. return app('json')->success('恢复门店成功!');
  191. } else {
  192. $data['is_del'] = 1;
  193. if (!SystemStorePoint::edit($data, $id))
  194. return app('json')->fail(SystemStorePoint::getErrorInfo('删除失败,请稍候再试!'));
  195. else
  196. return app('json')->success('删除门店成功!');
  197. }
  198. }
  199. public function stockList(Request $request)
  200. {
  201. $uid = $request->uid();
  202. $where = UtilService::getMore([
  203. ['type', ''],
  204. ['page', 1],
  205. ['limit', 20],
  206. ['start_time', ''],
  207. ['end_time', ''],
  208. ]);
  209. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  210. if (!$info) {
  211. return app('json')->fail('权限不足');
  212. }
  213. return app('json')->successful('ok', ['warning' => SystemStoreProductStock::getWarning($info['store_id']), 'data' => SystemStoreProductStock::getStockList($where, $info['store_id'])]);
  214. }
  215. public function billList(Request $request)
  216. {
  217. $uid = $request->uid();
  218. $where = UtilService::getMore([
  219. ['page', 1],
  220. ['limit', 20],
  221. ['type', ''],
  222. ['time', ''],
  223. ]);
  224. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  225. if (!$info) {
  226. return app('json')->fail('权限不足');
  227. }
  228. return app('json')->successful('ok', ['switch' => sys_config('withdraw_switch', 0), 'all' => SystemStore::get($info['store_id'])['brokerage_price'], 'data' => SystemStoreBill::getList($where, $info['store_id'])]);
  229. }
  230. public function cash(Request $request)
  231. {
  232. $uid = $request->uid();
  233. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  234. if (!$info) {
  235. return app('json')->fail('权限不足');
  236. }
  237. if (!in_array(sys_config('default_store_admin', 7), explode(',', $request->admin_info()['roles']))) {
  238. return app('json')->fail('权限不足');
  239. }
  240. $store = SystemStore::get($info['store_id']);
  241. $extractInfo = UtilService::postMore([
  242. ['money', 0],
  243. ['bankname', ''],
  244. ['name', ''],
  245. ['cardnum', ''],
  246. ], $request);
  247. $extractInfo['extract_type'] = 'bank';
  248. $extractInfo['name'] = $extractInfo['name'] ?? $store['leader'];
  249. if (!preg_match('/^(([1-9]\d*)|0)(\.\d{1-2})?$/', $extractInfo['money'])) app('json')->fail('提现金额输入有误');
  250. $store = SystemStore::get($info['store_id']);
  251. if ($extractInfo['money'] > $store['brokerage_price']) return app('json')->fail('可提现佣金不足');
  252. if (!$extractInfo['cardnum'] == '')
  253. if (!preg_match('/^([1-9]{1})(\d{14}|\d{18})$/', $extractInfo['cardnum']))
  254. return app('json')->fail('银行卡号有误');
  255. if (SystemStoreExtract::userExtract($store, $extractInfo))
  256. return app('json')->successful('申请提现成功!');
  257. else
  258. return app('json')->fail(SystemStoreExtract::getErrorInfo('提现失败'));
  259. }
  260. public function childrenList(Request $request)
  261. {
  262. $uid = $request->uid();
  263. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  264. if (!$info) {
  265. return app('json')->fail('权限不足');
  266. }
  267. $where = UtilService::getMore([
  268. ['page', 1],
  269. ['limit', 20],
  270. ['name', ''],
  271. ['level', ''],
  272. ['excel', 0],
  273. ['type', ''],
  274. ['parent_id', 0],
  275. ]);
  276. $where['self_store'] = $info['store_id'];
  277. return app('json')->successful('ok', \app\admin\model\system\SystemStore::getStoreList($where));
  278. }
  279. /**
  280. * 订单数据统计
  281. * @param Request $request
  282. * @return mixed
  283. * @throws DataNotFoundException
  284. * @throws DbException
  285. * @throws ModelNotFoundException
  286. */
  287. public function statistics(Request $request)
  288. {
  289. $uid = $request->uid();
  290. // if (!StoreService::orderServiceStatus($uid))
  291. // return app('json')->fail('权限不足');
  292. $where = [];
  293. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  294. if ($info) {
  295. $where['store_id'] = $info['store_id'];
  296. }
  297. $dataCount = StoreOrder::getOrderDataAdmin($where);
  298. $dataPrice = StoreOrder::getOrderTimeData($where);
  299. $data = array_merge($dataCount, $dataPrice);
  300. return app('json')->successful($data);
  301. }
  302. /**
  303. * 订单每月统计数据
  304. * @param Request $request
  305. * @return mixed
  306. * @throws DataNotFoundException
  307. * @throws DbException
  308. * @throws ModelNotFoundException
  309. */
  310. public function data(Request $request)
  311. {
  312. $uid = $request->uid();
  313. // if (!StoreService::orderServiceStatus($uid))
  314. // return app('json')->fail('权限不足');
  315. list($page, $limit, $start, $stop) = UtilService::getMore([
  316. ['page', 1],
  317. ['limit', 7],
  318. ['start', ''],
  319. ['stop', '']
  320. ], $request, true);
  321. $where = [];
  322. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  323. if ($info) {
  324. $where['store_id'] = $info['store_id'];
  325. }
  326. if (!$limit) return app('json')->successful([]);
  327. $data = StoreOrder::getOrderDataPriceCount($page, $limit, $start, $stop, $where);
  328. if ($data) return app('json')->successful($data->toArray());
  329. return app('json')->successful([]);
  330. }
  331. /**
  332. * 订单列表
  333. * @param Request $request
  334. * @return mixed
  335. * @throws DataNotFoundException
  336. * @throws DbException
  337. * @throws ModelNotFoundException
  338. */
  339. public function lst(Request $request)
  340. {
  341. $uid = $request->uid();
  342. // if (!StoreService::orderServiceStatus($uid))
  343. // return app('json')->fail('权限不足');
  344. $where = UtilService::getMore([
  345. ['status', ''],
  346. ['user_phone', ''],
  347. ['is_del', 0],
  348. ['data', ''],
  349. ['type', ''],
  350. ['shipping_type', ''],
  351. ['order', ''],
  352. ['page', 0],
  353. ['limit', 0]
  354. ], $request);
  355. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  356. if ($info) {
  357. $where['store_id'] = $info['store_id'];
  358. }
  359. if (!$where['limit']) return app('json')->successful([]);
  360. return app('json')->successful(StoreOrder::orderList($where));
  361. }
  362. /**
  363. * 订单列表
  364. * @param Request $request
  365. * @return mixed
  366. * @throws DataNotFoundException
  367. * @throws DbException
  368. * @throws ModelNotFoundException
  369. */
  370. public function exchange_order_lst(Request $request)
  371. {
  372. $uid = $request->uid();
  373. $where = UtilService::getMore([
  374. ['status', ''],
  375. ['user_phone', ''],
  376. ['is_del', 0],
  377. ['data', ''],
  378. ['order', ''],
  379. ['page', 0],
  380. ['limit', 0]
  381. ], $request);
  382. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  383. if ($info) {
  384. $where['store_id'] = $info['store_id'];
  385. }
  386. if (!$where['limit']) return app('json')->successful([]);
  387. return app('json')->successful(StoreExchangeOrder::orderList($where));
  388. }
  389. /**
  390. * 订单详情
  391. * @param Request $request
  392. * @param $orderId
  393. * @return mixed
  394. * @throws DataNotFoundException
  395. * @throws ModelNotFoundException
  396. * @throws DbException
  397. */
  398. public function detail(Request $request, $orderId)
  399. {
  400. $uid = $request->uid();
  401. // if (!StoreService::orderServiceStatus($uid))
  402. // return app('json')->fail('权限不足');
  403. $where = [];
  404. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  405. if ($info) {
  406. $where['store_id'] = $info['store_id'];
  407. }
  408. $order = StoreOrder::getAdminOrderDetail($orderId, 'id,uid,order_id,add_time,status,total_num,total_price,total_postage,pay_price,pay_postage,paid,refund_status,remark,pink_id,combination_id,mark,seckill_id,bargain_id,delivery_type,pay_type,real_name,user_phone,user_address,coupon_price,freight_price,delivery_name,delivery_type,delivery_id,store_id,point_id', $where);
  409. if (!$order) return app('json')->fail('订单不存在');
  410. $order = $order->toArray();
  411. $nickname = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  412. $orderInfo = StoreOrder::tidyAdminOrder([$order], true)[0];
  413. unset($orderInfo['uid'], $orderInfo['seckill_id'], $orderInfo['pink_id'], $orderInfo['combination_id'], $orderInfo['bargain_id'], $orderInfo['status'], $orderInfo['total_postage']);
  414. $orderInfo['nickname'] = $nickname;
  415. return app('json')->successful('ok', $orderInfo);
  416. }
  417. /**
  418. * 订单发货获取订单信息
  419. * @param Request $request
  420. * @param $orderId
  421. * @return mixed
  422. * @throws DataNotFoundException
  423. * @throws ModelNotFoundException
  424. * @throws DbException
  425. */
  426. public function delivery_gain(Request $request, $orderId)
  427. {
  428. $uid = $request->uid();
  429. // if (!StoreService::orderServiceStatus($uid))
  430. // return app('json')->fail('权限不足');
  431. $where = [];
  432. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  433. if ($info) {
  434. $where['store_id'] = $info['store_id'];
  435. }
  436. $order = StoreOrder::getAdminOrderDetail($orderId, 'real_name,user_phone,user_address,order_id,uid,status,paid', $where);
  437. if (!$order) return app('json')->fail('订单不存在');
  438. if ($order['paid']) {
  439. $order['nickname'] = User::getUserInfo($order['uid'], 'nickname')['nickname'];
  440. $order = $order->hidden(['uid', 'status', 'paid'])->toArray();
  441. return app('json')->successful('ok', $order);
  442. }
  443. return app('json')->fail('状态错误');
  444. }
  445. /**
  446. * 订单发货
  447. * @param Request $request
  448. * @return mixed
  449. * @throws DataNotFoundException
  450. * @throws ModelNotFoundException
  451. * @throws \think\exception\DbException
  452. * @throws DbException
  453. */
  454. public function delivery_keep(Request $request)
  455. {
  456. $uid = $request->uid();
  457. // if (!StoreService::orderServiceStatus($uid))
  458. // return app('json')->fail('权限不足');
  459. list($order_id, $delivery_type, $delivery_name, $delivery_id) = UtilService::postMore([
  460. ['order_id', ''],
  461. ['delivery_type', 0],
  462. ['delivery_name', ''],
  463. ['delivery_id', ''],
  464. ], $request, true);
  465. $where = [];
  466. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  467. if ($info) {
  468. $where['store_id'] = $info['store_id'];
  469. }
  470. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,status,paid', $where);
  471. if (!$order) return app('json')->fail('订单不存在');
  472. if (!$order['status'] && $order['paid']) {
  473. $deliveryTypeArr = isset($where['store_id']) ? ['send'] : ['send', 'express', 'fictitious'];
  474. if (!strlen(trim($delivery_type))) return app('json')->fail('请填写发货方式');
  475. if (!in_array($delivery_type, $deliveryTypeArr)) return app('json')->fail('发货方式错误');
  476. if ($delivery_type == 'express') {
  477. if (!strlen(trim($delivery_name))) return app('json')->fail('请选择快递公司');
  478. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写快递单号');
  479. }
  480. if ($delivery_type == 'send') {
  481. if (!strlen(trim($delivery_name))) return app('json')->fail('请填写发货人');
  482. if (!strlen(trim($delivery_id))) return app('json')->fail('请填写发货手机号');
  483. }
  484. $data['status'] = 1;
  485. $data['delivery_type'] = $delivery_type;
  486. $data['delivery_name'] = $delivery_name;
  487. $data['delivery_id'] = $delivery_id;
  488. $res = StoreOrder::edit($data, $order['id'], 'id');
  489. if ($res) {
  490. if ($delivery_type == 'express') {
  491. StoreOrderStatus::status($order['id'], 'delivery_goods', '已发货 快递公司:' . $data['delivery_name'] . ' 快递单号:' . $data['delivery_id']);
  492. } else if ($delivery_type == 'send') {
  493. StoreOrderStatus::status($order['id'], 'delivery', '已配送 发货人:' . $delivery_name . ' 发货人电话:' . $delivery_id);
  494. } else if ($delivery_type == 'fictitious') {
  495. StoreOrderStatus::status($order['id'], 'delivery_fictitious', '虚拟产品已发货');
  496. }
  497. }
  498. event('StoreProductOrderDeliveryGoodsAfter', [$data, $order['id']]);
  499. return app('json')->successful('发货成功!');
  500. }
  501. return app('json')->fail('状态错误');
  502. }
  503. /**
  504. * 订单改价
  505. * @param Request $request
  506. * @return mixed
  507. * @throws DataNotFoundException
  508. * @throws ModelNotFoundException
  509. * @throws DbException
  510. */
  511. public function price(Request $request)
  512. {
  513. $uid = $request->uid();
  514. // if (!StoreService::orderServiceStatus($uid))
  515. // return app('json')->fail('权限不足');
  516. list($order_id, $price) = UtilService::postMore([
  517. ['order_id', ''],
  518. ['price', '']
  519. ], $request, true);
  520. $where = [];
  521. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  522. if ($info) {
  523. $where['store_id'] = $info['store_id'];
  524. }
  525. $order = StoreOrder::getAdminOrderDetail($order_id, 'id,paid,user_phone,pay_price,order_id,total_price,total_postage,pay_postage,gain_integral', $where);
  526. if (!$order) return app('json')->fail('订单不存在');
  527. $order = $order->toArray();
  528. if (!$order['paid']) {
  529. if ($price === '') return app('json')->fail('请填写实际支付金额');
  530. if ($price < 0) return app('json')->fail('实际支付金额不能小于0元');
  531. if ($order['pay_price'] == $price) return app('json')->successful('修改成功');
  532. $order['order_id'] = StoreOrder::changeOrderId($order['order_id']);
  533. if (!StoreOrder::edit(['pay_price' => $price, 'order_id' => $order['order_id']], $order['id'], 'id'))
  534. return app('json')->fail('状态错误');
  535. $order['pay_price'] = $price;
  536. event('StoreProductOrderEditAfter', [$order, $order['id']]);
  537. StoreOrderStatus::status($order['id'], 'order_edit', '修改实际支付金额' . $price);
  538. //改价短信提醒
  539. if ($price != $order['pay_price']) {
  540. $switch = sys_config('price_revision_switch') ? true : false;
  541. ShortLetterRepositories::send($switch, $order['user_phone'], ['order_id' => $order_id, 'pay_price' => $order['pay_price']], 'PRICE_REVISION_CODE');
  542. }
  543. return app('json')->successful('修改成功');
  544. }
  545. return app('json')->fail('状态错误');
  546. }
  547. /**
  548. * 订单备注
  549. * @param Request $request
  550. * @return mixed
  551. * @throws DataNotFoundException
  552. * @throws ModelNotFoundException
  553. * @throws DbException
  554. */
  555. public function remark(Request $request)
  556. {
  557. $uid = $request->uid();
  558. // if (!StoreService::orderServiceStatus($uid))
  559. // return app('json')->fail('权限不足');
  560. list($order_id, $remark) = UtilService::postMore([
  561. ['order_id', ''],
  562. ['remark', '']
  563. ], $request, true);
  564. $where = [];
  565. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  566. if ($info) {
  567. $where['store_id'] = $info['store_id'];
  568. }
  569. $order = StoreOrder::getAdminOrderDetail($order_id, 'id', $where);
  570. if (!$order) return app('json')->fail('订单不存在');
  571. $order = $order->toArray();
  572. if (!strlen(trim($remark))) return app('json')->fail('请填写备注内容');
  573. if (!StoreOrder::edit(['remark' => $remark], $order['id']))
  574. return app('json')->fail('备注失败');
  575. return app('json')->successful('备注成功');
  576. }
  577. /**
  578. * 订单交易额/订单数量时间统计
  579. * @param Request $request
  580. * @return bool
  581. */
  582. public function time(Request $request)
  583. {
  584. $uid = $request->uid();
  585. // if (!StoreService::orderServiceStatus($uid))
  586. // return app('json')->fail('权限不足');
  587. list($start, $stop, $type) = UtilService::getMore([
  588. ['start', strtotime(date('Y-m'))],
  589. ['stop', time()],
  590. ['type', 1]
  591. ], $request, true);
  592. $where = [];
  593. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  594. if ($info) {
  595. $where['store_id'] = $info['store_id'];
  596. }
  597. if ($start == $stop) {
  598. return app('json')->fail('开始时间不能等于结束时间');
  599. }
  600. if ($start > $stop) {
  601. $middle = $stop;
  602. $stop = $start;
  603. $start = $middle;
  604. }
  605. $space = bcsub($stop, $start, 0);//间隔时间段
  606. $front = bcsub($start, $space, 0);//第一个时间段
  607. if ($type == 1) {//销售额
  608. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($front, $start, $where);
  609. $afterPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $stop, $where);
  610. $chartInfo = StoreOrder::chartTimePrice($start, $stop, $where);
  611. $data['chart'] = $chartInfo;//营业额图表数据
  612. $data['time'] = $afterPrice;//时间区间营业额
  613. $increase = (float)bcsub($afterPrice, $frontPrice, 2); //同比上个时间区间增长营业额
  614. $growthRate = abs($increase);
  615. if ($growthRate == 0) $data['growth_rate'] = 0;
  616. else if ($frontPrice == 0) $data['growth_rate'] = $growthRate;
  617. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontPrice, 2), 100, 0);//时间区间增长率
  618. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  619. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  620. } else {//订单数
  621. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($front, $start, $where);
  622. $afterNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $stop, $where);
  623. $chartInfo = StoreOrder::chartTimeNumber($start, $stop, $where);
  624. $data['chart'] = $chartInfo;//订单数图表数据
  625. $data['time'] = $afterNumber;//时间区间订单数
  626. $increase = (int)bcsub($afterNumber, $frontNumber, 0); //同比上个时间区间增长订单数
  627. $growthRate = abs($increase);
  628. if ($growthRate == 0) $data['growth_rate'] = 0;
  629. else if ($frontNumber == 0) $data['growth_rate'] = $growthRate;
  630. else $data['growth_rate'] = (int)bcmul(bcdiv($growthRate, $frontNumber, 2), 100, 0);//时间区间增长率
  631. $data['increase_time'] = abs($increase); //同比上个时间区间增长营业额
  632. $data['increase_time_status'] = $increase >= 0 ? 1 : 2; //同比上个时间区间增长营业额增长 1 减少 2
  633. }
  634. return app('json')->successful($data);
  635. }
  636. public function month(Request $request)
  637. {
  638. $uid = $request->uid();
  639. $where = [];
  640. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  641. if ($info) {
  642. $where['store_id'] = $info['store_id'];
  643. }
  644. $start = StoreOrder::min('add_time');
  645. $data = [];
  646. while ($start < time()) {
  647. $start_month = date('Y-m', $start);
  648. $end = strtotime('+1month', strtotime($start_month)) - 1;
  649. $frontPrice = StoreOrder:: getOrderTimeBusinessVolumePrice($start, $end, $where);
  650. $frontNumber = StoreOrder:: getOrderTimeBusinessVolumeNumber($start, $end, $where);
  651. $start = $end + 1;
  652. $data[$start_month] = compact('frontPrice', 'frontNumber');
  653. }
  654. return app('json')->successful($data);
  655. }
  656. /**
  657. * 订单支付
  658. * @param Request $request
  659. * @return mixed
  660. * @throws DataNotFoundException
  661. * @throws ModelNotFoundException
  662. * @throws DbException
  663. */
  664. public function offline(Request $request)
  665. {
  666. $uid = $request->uid();
  667. list($orderId) = UtilService::postMore([['order_id', '']], $request, true);
  668. $where = [];
  669. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  670. if ($info) {
  671. $where['store_id'] = $info['store_id'];
  672. }
  673. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, 'id', $where);
  674. if (!$orderInfo) return app('json')->fail('参数错误');
  675. $id = $orderInfo->id;
  676. $res = StoreOrder::updateOffline($id);
  677. if ($res) {
  678. event('StoreProductOrderOffline', [$id]);
  679. StoreOrderStatus::status($id, 'offline', '线下付款');
  680. return app('json')->successful('修改成功!');
  681. }
  682. return app('json')->fail(StoreOrder::getErrorInfo('修改失败!'));
  683. }
  684. /**
  685. * 订单退款
  686. * @param Request $request
  687. * @return mixed
  688. * @throws \think\Exception
  689. * @throws DataNotFoundException
  690. * @throws ModelNotFoundException
  691. * @throws DbException
  692. */
  693. public function refund(Request $request)
  694. {
  695. $uid = $request->uid();
  696. list($orderId, $price, $type) = UtilService::postMore([
  697. ['order_id', ''],
  698. ['price', 0],
  699. ['type', 1],
  700. ], $request, true);
  701. $where = [];
  702. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  703. if ($info) {
  704. $where['store_id'] = $info['store_id'];
  705. }
  706. if (!strlen(trim($orderId))) return app('json')->fail('参数错误');
  707. $orderInfo = StoreOrder::getAdminOrderDetail($orderId, '*', $where);
  708. if (!$orderInfo) return app('json')->fail('数据不存在!');
  709. $orderInfo = $orderInfo->toArray();
  710. if ($type == 1)
  711. $data['refund_status'] = 2;
  712. else if ($type == 2)
  713. $data['refund_status'] = 0;
  714. else
  715. return app('json')->fail('退款修改状态错误');
  716. if ($orderInfo['pay_price'] == 0 || $type == 2) {
  717. StoreOrder::update($data, ['order_id' => $orderId]);
  718. return app('json')->successful('修改退款状态成功!');
  719. }
  720. if ($orderInfo['pay_price'] == $orderInfo['refund_price']) return app('json')->fail('已退完支付金额!不能再退款了');
  721. if (!$price) return app('json')->fail('请输入退款金额');
  722. $data['refund_price'] = bcadd($price, $orderInfo['refund_price'], 2);
  723. $bj = bccomp((float)$orderInfo['pay_price'], (float)$data['refund_price'], 2);
  724. if ($bj < 0) return app('json')->fail('退款金额大于支付金额,请修改退款金额');
  725. $refundData['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
  726. $refundData['refund_price'] = $price;
  727. $refundData['refund_id'] = $orderInfo['order_id'] . time();
  728. if ($orderInfo['pay_type'] == 'weixin') {
  729. if ($orderInfo['is_channel'] == 1) {// 小程序
  730. try {
  731. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refundData);
  732. } catch (\Exception $e) {
  733. return app('json')->fail($e->getMessage());
  734. }
  735. } else {// 公众号
  736. try {
  737. WechatService::payOrderRefund($orderInfo['order_id'], $refundData);
  738. } catch (\Exception $e) {
  739. return app('json')->fail($e->getMessage());
  740. }
  741. }
  742. } else if ($orderInfo['pay_type'] == 'yue') {//余额
  743. StoreOrder::beginTrans();
  744. $userInfo = User::getUserInfo($orderInfo['uid'], 'now_money');
  745. if (!$userInfo) {
  746. StoreOrder::rollbackTrans();
  747. return app('json')->fail('订单用户不存在');
  748. }
  749. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $price, 'uid');
  750. $res2 = $res2 = UserBill::income('商品退款', $orderInfo['uid'], 'now_money', 'pay_product_refund', $price, $orderInfo['id'], bcadd($userInfo['now_money'], $price, 2), '订单退款到余额' . floatval($price) . '元');
  751. try {
  752. OrderRepository::storeOrderYueRefund($orderInfo, $refundData);
  753. } catch (\Exception $e) {
  754. StoreOrder::rollbackTrans();
  755. return app('json')->fail($e->getMessage());
  756. }
  757. $res = $res1 && $res2;
  758. StoreOrder::checkTrans($res);
  759. if (!$res) return app('json')->fail('余额退款失败!');
  760. }
  761. $resEdit = StoreOrder::edit($data, $orderInfo['id'], 'id');
  762. if ($resEdit) {
  763. $data['type'] = $type;
  764. if ($data['type'] == 1) StorePink::setRefundPink($orderInfo['id']);
  765. try {
  766. OrderRepository::storeProductOrderRefundY($data, $orderInfo['id']);
  767. } catch (\Exception $e) {
  768. return app('json')->fail($e->getMessage());
  769. }
  770. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元');
  771. SystemStoreBill::where('category', 'brokerage_price')
  772. //->where('type', 'brokerage')
  773. ->where('link_id', $orderId)
  774. ->where('pm', 1)
  775. ->where('status', 0)
  776. ->update(['status' => -1]);
  777. \app\admin\model\user\UserBill::where('category', 'now_money')
  778. ->where('type', 'brokerage')
  779. ->where('link_id', $orderId)
  780. ->where('pm', 1)
  781. ->where('status', 0)
  782. ->update(['status' => -1]);
  783. //退佣金
  784. $brokerage_list = UserBill::where('category', 'now_money')
  785. ->where('type', 'brokerage')
  786. ->where('link_id', $orderId)
  787. ->where('pm', 1)
  788. ->where('status', 1)
  789. ->select()->toArray();
  790. if ($brokerage_list)
  791. foreach ($brokerage_list as $item) {
  792. $usermoney = User::where('uid', $item['uid'])->value('brokerage_price');
  793. if ($item['number'] > $usermoney)
  794. $item['number'] = $usermoney;
  795. User::bcDec($item['uid'], 'brokerage_price', $item['number'], 'uid');
  796. UserBill::expend('退款退佣金', $item['uid'], 'now_money', 'brokerage', $item['number'], $orderId, bcsub($usermoney, $item['number'], 2), '订单退款扣除佣金' . floatval($item['number']) . '元');
  797. }
  798. return app('json')->successful('修改成功!');
  799. } else {
  800. StoreOrderStatus::status($orderInfo['id'], 'refund_price', '退款给用户' . $price . '元失败');
  801. return app('json')->successful('修改失败!');
  802. }
  803. }
  804. /**
  805. * 门店核销
  806. * @param Request $request
  807. * @return
  808. * @throws DataNotFoundException
  809. * @throws DbException
  810. * @throws ModelNotFoundException
  811. */
  812. public function order_verific(Request $request)
  813. {
  814. $uid = $request->uid();
  815. list($verify_code, $is_confirm) = UtilService::postMore([
  816. ['verify_code', ''],
  817. ['is_confirm', 0]
  818. ], $request, true);
  819. if (!$verify_code) return app('json')->fail('缺少核销码');
  820. $orderInfo = StoreOrder::where('verify_code', $verify_code)->where('store_id', 'in', [SystemStoreStaff::where('uid', $uid)->value('store_id'), 0])->where('paid', 1)->where('refund_status', 0)->find();
  821. if (!$orderInfo) return app('json')->fail('核销的订单不存在于该门店或未支付或已退款');
  822. if ($orderInfo->status > 0) return app('json')->fail('订单已经核销');
  823. if ($orderInfo->combination_id && $orderInfo->pink_id) {
  824. $res = StorePink::where('id', $orderInfo->pink_id)->where('status', '<>', 2)->count();
  825. if ($res) return app('json')->fail('拼团订单暂未成功无法核销!');
  826. }
  827. if (!$is_confirm) {
  828. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  829. return app('json')->success($orderInfo->toArray());
  830. }
  831. StoreOrder::beginTrans();
  832. try {
  833. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  834. // $orderInfo->store_id = $storeStaff['store_id'];
  835. $orderInfo->clerk_id = $storeStaff['id'];
  836. }
  837. $orderInfo->status = 2;
  838. $orderInfo->verify_time = time();
  839. if ($orderInfo->store_id == 0) {
  840. $orderInfo->store_id = SystemStoreStaff::where('uid', $uid)->value('store_id');
  841. $store = SystemStore::get($orderInfo->store_id);
  842. $orderInfo->province = explode(',', $store['address'])[0] ?? '';
  843. $orderInfo->city = explode(',', $store['address'])[1] ?? '';
  844. $orderInfo->district = explode(',', $store['address'])[2] ?? '';
  845. SystemStore::decStock($orderInfo);
  846. }
  847. if ($orderInfo->save()) {
  848. OrderRepository::storeProductOrderTakeDeliveryAdmin($orderInfo);
  849. StoreOrderStatus::status($orderInfo->id, 'take_delivery', '已核销');
  850. event('ShortMssageSend', [$orderInfo['order_id'], 'Receiving']);
  851. StoreOrder::commitTrans();
  852. return app('json')->success('核销成功');
  853. } else {
  854. StoreOrder::rollbackTrans();
  855. return app('json')->fail('核销失败');
  856. }
  857. } catch (\PDOException $e) {
  858. StoreOrder::rollbackTrans();
  859. return app('json')->fail($e->getMessage());
  860. } catch (\Exception $e) {
  861. StoreOrder::rollbackTrans();
  862. return app('json')->fail($e->getMessage());
  863. }
  864. }
  865. /**
  866. * 门店核销
  867. * @param Request $request
  868. * @return
  869. * @throws DataNotFoundException
  870. * @throws DbException
  871. * @throws ModelNotFoundException
  872. */
  873. public function exchange_order_verific(Request $request)
  874. {
  875. $uid = $request->uid();
  876. list($verify_code, $is_confirm) = UtilService::postMore([
  877. ['verify_code', ''],
  878. ['is_confirm', 0]
  879. ], $request, true);
  880. if (!$verify_code) return app('json')->fail('缺少核销码');
  881. $orderInfo = StoreExchangeOrder::where('verify_code', $verify_code)->where('store_id', 'in', [SystemStoreStaff::where('uid', $uid)->value('store_id'), 0])->where('refund_status', 0)->find();
  882. if (!$orderInfo) return app('json')->fail('核销的兑换券不存在于该门店或未支付或已退款');
  883. if ($orderInfo->status == 2) return app('json')->fail('兑换券已经失效');
  884. if ($orderInfo->status > 0) return app('json')->fail('兑换券已经核销');
  885. if (!$is_confirm) {
  886. $orderInfo['image'] = StoreCart::getProductImage($orderInfo->cart_id);
  887. return app('json')->success($orderInfo->toArray());
  888. }
  889. StoreExchangeOrder::beginTrans();
  890. try {
  891. if (SystemStoreStaff::verifyStatus($uid) && ($storeStaff = SystemStoreStaff::where('uid', $uid)->field(['store_id', 'id'])->find())) {
  892. $orderInfo->clerk_id = $storeStaff['id'];
  893. }
  894. $orderInfo->status = 1;
  895. $orderInfo->verify_time = time();
  896. if ($orderInfo->store_id == 0) {
  897. $orderInfo->store_id = SystemStoreStaff::where('uid', $uid)->value('store_id');
  898. SystemStore::decStock($orderInfo);
  899. }
  900. if ($orderInfo->save()) {
  901. StoreExchangeOrder::commitTrans();
  902. return app('json')->success('核销成功');
  903. } else {
  904. StoreExchangeOrder::rollbackTrans();
  905. return app('json')->fail('核销失败');
  906. }
  907. } catch (\PDOException $e) {
  908. StoreExchangeOrder::rollbackTrans();
  909. return app('json')->fail($e->getMessage());
  910. } catch (\Exception $e) {
  911. StoreExchangeOrder::rollbackTrans();
  912. return app('json')->fail($e->getMessage());
  913. }
  914. }
  915. public function rechargeList(Request $request)
  916. {
  917. $uid = $request->uid();
  918. $where = UtilService::getMore([
  919. ['page', 1],
  920. ['limit', 20],
  921. ]);
  922. $info = SystemStoreStaff::where('uid', $uid)->where('status', 1)->find();
  923. if (!$info) {
  924. return app('json')->fail('权限不足');
  925. }
  926. $where['store_id'] = $info['store_id'];
  927. return app('json')->successful('ok', UserRecharge::getStoreRechargeList($where));
  928. }
  929. public function back_deposit(Request $request)
  930. {
  931. $uid = $request->uid();
  932. $data = UtilService::postMore([
  933. ['order_id', ''],
  934. ['refund_price', 0],
  935. ['type', 1],
  936. ]);
  937. if (!$data['order_id']) return app('json')->fail('数据不存在');
  938. $orderInfo = StoreOrder::where('order_id', $data['order_id'])->where('store_id', SystemStoreStaff::where('uid', $uid)->value('store_id'))->find();
  939. if (!$orderInfo) return app('json')->fail('订单不存在于该门店');
  940. if ($orderInfo['deposit'] == $orderInfo['deposit_back']) return app('json')->fail('已退完押金!不能再退款了');
  941. if (!$data['refund_price']) return app('json')->fail('请输入退款金额');
  942. $refund_price = $data['refund_price'];
  943. $data['deposit_back'] = bcadd($data['refund_price'], $orderInfo['deposit_back'], 2);
  944. $bj = bccomp((float)$orderInfo['deposit'], (float)$data['deposit_back'], 2);
  945. if ($bj < 0) return app('json')->fail('退押金额大于支付押金,请修改退押金额');
  946. if ($data['type'] == 1) {
  947. $data['deposit_status'] = 1;
  948. } else if ($data['type'] == 2) {
  949. $data['deposit_status'] = 0;
  950. }
  951. // $type = $data['type'];
  952. unset($data['type']);
  953. unset($data['refund_price']);
  954. $refund_data['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
  955. $refund_data['refund_price'] = $refund_price;
  956. $refund_data['refund_id'] = 'refund_deposit' . $orderInfo['order_id'] . time();
  957. if ($orderInfo['pay_type'] == 'weixin') {
  958. if ($orderInfo['is_channel'] == 1) {//小程序
  959. try {
  960. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refund_data);//2.5.36
  961. } catch (\Exception $e) {
  962. return app('json')->fail($e->getMessage());
  963. }
  964. } else {//TODO 公众号
  965. try {
  966. WechatService::payOrderRefund($orderInfo['order_id'], $refund_data);
  967. } catch (\Exception $e) {
  968. return app('json')->fail($e->getMessage());
  969. }
  970. }
  971. } else if ($orderInfo['pay_type'] == 'yue') {
  972. BaseModel::beginTrans();
  973. $usermoney = User::where('uid', $orderInfo['uid'])->value('now_money');
  974. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $refund_price, 'uid');
  975. $res2 = UserBill::income('商品退押金', $orderInfo['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $orderInfo['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
  976. $res = $res1 && $res2;
  977. BaseModel::checkTrans($res);
  978. if (!$res) return app('json')->fail('余额退押金失败!');
  979. }
  980. $resEdit = StoreOrder::edit($data, $orderInfo['id']);
  981. if ($resEdit) {
  982. \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['id'], 'refund_deposit', '退押金给用户' . $refund_price . '元');
  983. return app('json')->success('成功!');
  984. } else {
  985. \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['id'], 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
  986. return app('json')->fail('失败!');
  987. }
  988. }
  989. public function exchange_order_back_deposit(Request $request)
  990. {
  991. $uid = $request->uid();
  992. $data = UtilService::postMore([
  993. ['id', ''],
  994. ['refund_price', 0],
  995. ['type', 1],
  996. ], $request);
  997. // var_dump($data);
  998. if (!$data['id']) return app('json')->fail('数据不存在');
  999. $orderInfo = StoreExchangeOrder::where('id', $data['id'])->where('store_id', SystemStoreStaff::where('uid', $uid)->value('store_id'))->where('refund_status', 0)->find();
  1000. if (!$orderInfo) return app('json')->fail('订单不存在于该门店或未支付或已退款');
  1001. if ($orderInfo['deposit'] == $orderInfo['deposit_back']) return app('json')->fail('已退完押金!不能再退款了');
  1002. if (!$data['refund_price']) return app('json')->fail('请输入退款金额');
  1003. $refund_price = $data['refund_price'];
  1004. $data['deposit_back'] = bcadd($data['refund_price'], $orderInfo['deposit_back'], 2);
  1005. $bj = bccomp((float)$orderInfo['deposit'], (float)$data['deposit_back'], 2);
  1006. if ($bj < 0) return app('json')->fail('退押金额大于支付押金,请修改退押金额');
  1007. if ($data['type'] == 1) {
  1008. $data['deposit_status'] = 1;
  1009. } else if ($data['type'] == 2) {
  1010. $data['deposit_status'] = 0;
  1011. }
  1012. // $type = $data['type'];
  1013. unset($data['type']);
  1014. unset($data['refund_price']);
  1015. $refund_data['pay_price'] = bcadd($orderInfo['pay_price'], $orderInfo['deposit'], 2);
  1016. $refund_data['refund_price'] = $refund_price;
  1017. $refund_data['refund_id'] = 'refund_deposit' . $orderInfo['order_id'] . time();
  1018. $order = StoreOrderModel::where('order_id', $orderInfo['order_id'])->find();
  1019. if ($order['pay_type'] == 'weixin') {
  1020. if ($order['is_channel'] == 1) {//小程序
  1021. try {
  1022. MiniProgramService::payOrderRefund($orderInfo['order_id'], $refund_data);//2.5.36
  1023. } catch (\Exception $e) {
  1024. return app('json')->fail($e->getMessage());
  1025. }
  1026. } else {//TODO 公众号
  1027. try {
  1028. WechatService::payOrderRefund($orderInfo['order_id'], $refund_data);
  1029. } catch (\Exception $e) {
  1030. return app('json')->fail($e->getMessage());
  1031. }
  1032. }
  1033. } else if ($orderInfo['pay_type'] == 'yue') {
  1034. BaseModel::beginTrans();
  1035. $usermoney = User::where('uid', $orderInfo['uid'])->value('now_money');
  1036. $res1 = User::bcInc($orderInfo['uid'], 'now_money', $refund_price, 'uid');
  1037. $res2 = UserBill::income('商品退押金', $orderInfo['uid'], 'now_money', 'pay_deposit_refund', $refund_price, $orderInfo['id'], bcadd($usermoney, $refund_price, 2), '订单退押金到余额' . floatval($refund_price) . '元');
  1038. $res = $res1 && $res2;
  1039. BaseModel::checkTrans($res);
  1040. if (!$res) return app('json')->fail('余额退押金失败!');
  1041. }
  1042. $resEdit = StoreExchangeOrder::edit($data, $orderInfo['id']);
  1043. if ($resEdit) {
  1044. \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['oid'], 'refund_deposit', '退押金给用户' . $refund_price . '元');
  1045. return app('json')->success('成功!');
  1046. } else {
  1047. \app\admin\model\order\StoreOrderStatus::setStatus($orderInfo['oid'], 'refund_deposit', '退押金给用户' . $refund_price . '元失败');
  1048. return app('json')->fail('失败!');
  1049. }
  1050. }
  1051. }