StoreOrderController.php 45 KB

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