StoreOrderController.php 44 KB

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