PayDetailController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\controller\notify\RechargeController;
  4. use app\controller\Curd;
  5. use app\model\Goods;
  6. use app\model\GoodsSku;
  7. use app\model\MemberAccount;
  8. use app\model\Order;
  9. use app\model\OrderSheet;
  10. use app\model\PayDetail;
  11. use support\Db;
  12. use support\exception\BusinessException;
  13. use support\Request;
  14. use support\Response;
  15. use function PHPSTORM_META\type;
  16. class PayDetailController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new PayDetail();
  21. }
  22. public function select(Request $request): Response
  23. {
  24. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  25. $order = $request->get('order', 'desc');
  26. $field = $field ?? 'pay_addtimes';
  27. // $where['pay_status'] = 'SUCCESS';
  28. if ($where['pay_category'] == 'OTHER') {
  29. $where['pay_status'] = 'SUCCESS';
  30. $where['pay_category'] = ['<>', 'RECHARGE'];
  31. }
  32. if (!empty($where['pay_addtimes'])) {
  33. $where['pay_addtimes'][0] = !empty($where['pay_addtimes'][0]) ? strtotime($where['pay_addtimes'][0]) : '';
  34. $where['pay_addtimes'][1] = !empty($where['pay_addtimes'][1]) ? strtotime($where['pay_addtimes'][1]) : '';
  35. }
  36. if (!empty($request->get('premises_id'))) {
  37. $payIds = PayDetail::whereJsonContains('pay_extend_json->remark->premises_id', intval($request->get('premises_id')))
  38. ->pluck('pay_id')
  39. ->toArray();
  40. $where['pay_id'] = '';
  41. if (!empty($payIds)) {
  42. $where['pay_id'] = ['in', $payIds];
  43. }
  44. }
  45. // else if ($where['pay_category'] == 'RECHARGE') {
  46. // $where['pay_status'] = 'SUCCESS';
  47. // $where['pay_category'] = 'RECHARGE';
  48. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  49. // }
  50. $query = $this->doSelect($where, $field, $order,$request->get());
  51. return $this->doFormat($query, $format, $limit);
  52. }
  53. protected function doSelect(array $where, string $field = null, string $order = 'desc',$params=[])
  54. {
  55. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  56. foreach ($where as $column => $value) {
  57. if (is_array($value)) {
  58. if ($value[0] === 'like' || $value[0] === 'not like') {
  59. $model = $model->where($column, $value[0], "%$value[1]%");
  60. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  61. $model = $model->where($column, $value[0], $value[1]);
  62. } elseif ($value[0] == 'in' && !empty($value[1])) {
  63. $valArr = $value[1];
  64. if (is_string($value[1])) {
  65. $valArr = explode(",", trim($value[1]));
  66. }
  67. $model = $model->whereIn($column, $valArr);
  68. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  69. $valArr = $value[1];
  70. if (is_string($value[1])) {
  71. $valArr = explode(",", trim($value[1]));
  72. }
  73. $model = $model->whereNotIn($column, $valArr);
  74. } elseif ($value[0] == 'null') {
  75. $model = $model->whereNull($column);
  76. } elseif ($value[0] == 'not null') {
  77. $model = $model->whereNotNull($column);
  78. } elseif ($value[0] !== '' || $value[1] !== '') {
  79. $model = $model->whereBetween($column, $value);
  80. }
  81. } else {
  82. $model = $model->where($column, $value);
  83. }
  84. }
  85. if (empty($params['type'])){
  86. $model = $model->where(function ($query) {
  87. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  88. ->orWhere('pay_prepayid', 'like', '%CASH%')
  89. ->orWhere('pay_prepayid', 'like', '%WELFARE%');
  90. });
  91. }else{
  92. if ($params['type'] == 'NORMAL'){
  93. $model = $model->where(function ($query) {
  94. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY','OFFLINE_WXPAY','OFFLINE_ALIPAY','MONEY'])
  95. ->orWhere('pay_prepayid', 'like', '%CASH%');
  96. });
  97. }else{
  98. $model = $model->where(function ($query) {
  99. $query->orWhere('pay_prepayid', 'like', '%WELFARE%');
  100. });
  101. }
  102. }
  103. if ($field) {
  104. $model = $model->orderBy($field, $order);
  105. }
  106. return $model;
  107. }
  108. public function selectWelfare(Request $request): Response
  109. {
  110. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  111. $order = $request->get('order', 'desc');
  112. $field = $field ?? 'pay_addtimes';
  113. // $where['pay_status'] = 'SUCCESS';
  114. if ($where['pay_category'] == 'OTHER') {
  115. $where['pay_status'] = 'SUCCESS';
  116. $where['pay_category'] = ['<>', 'RECHARGE'];
  117. }
  118. // else if ($where['pay_category'] == 'RECHARGE') {
  119. // $where['pay_status'] = 'SUCCESS';
  120. // $where['pay_category'] = 'RECHARGE';
  121. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  122. // }
  123. $query = $this->doSelectWelfare($where, $field, $order);
  124. return $this->doFormat($query, $format, $limit);
  125. }
  126. protected function doSelectWelfare(array $where, string $field = null, string $order = 'desc')
  127. {
  128. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  129. foreach ($where as $column => $value) {
  130. if (is_array($value)) {
  131. if ($value[0] === 'like' || $value[0] === 'not like') {
  132. $model = $model->where($column, $value[0], "%$value[1]%");
  133. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  134. $model = $model->where($column, $value[0], $value[1]);
  135. } elseif ($value[0] == 'in' && !empty($value[1])) {
  136. $valArr = $value[1];
  137. if (is_string($value[1])) {
  138. $valArr = explode(",", trim($value[1]));
  139. }
  140. $model = $model->whereIn($column, $valArr);
  141. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  142. $valArr = $value[1];
  143. if (is_string($value[1])) {
  144. $valArr = explode(",", trim($value[1]));
  145. }
  146. $model = $model->whereNotIn($column, $valArr);
  147. } elseif ($value[0] == 'null') {
  148. $model = $model->whereNull($column);
  149. } elseif ($value[0] == 'not null') {
  150. $model = $model->whereNotNull($column);
  151. } elseif ($value[0] !== '' || $value[1] !== '') {
  152. $model = $model->whereBetween($column, $value);
  153. }
  154. } else {
  155. $model = $model->where($column, $value);
  156. }
  157. }
  158. $model = $model->where(function ($query) {
  159. $query->Where('pay_prepayid', 'like', '%WELFARE%');
  160. });
  161. if ($field) {
  162. $model = $model->orderBy($field, $order);
  163. }
  164. return $model;
  165. }
  166. public function afterQuery($items)
  167. {
  168. foreach ($items as &$item) {
  169. $prepayId = explode('-', $item->pay_prepayid);
  170. if (count($prepayId) > 1) {
  171. $item->pay_prepayid = $prepayId[1];
  172. }
  173. $item->type = 'NORMAL';
  174. if (isset($prepayId[1]) && $prepayId[1] == 'WELFARE'){
  175. $item->type = 'WELFARE';
  176. }
  177. $memberAccount = [];
  178. if (!empty($item->memberAccount)) {
  179. foreach ($item->memberAccount as $account) {
  180. $memberAccount[strtolower($account->member_account_classify)] = $account;
  181. }
  182. $item->memberAccount = $memberAccount;
  183. }
  184. if (!empty($item->pay_extend_json)) {
  185. $payExtendJson = json_decode($item->pay_extend_json, true);
  186. if (isset($payExtendJson['remark'])) {
  187. $item->service = $payExtendJson['remark']['service'] ?? '';
  188. $item->business = $payExtendJson['remark']['business'] ?? '';
  189. $item->premises_id = $payExtendJson['remark']['premises_id'] ?? '';
  190. $item->premises_name = $payExtendJson['remark']['premises_name'] ?? '';
  191. }
  192. }
  193. }
  194. return $items;
  195. }
  196. /**
  197. * @Desc 产品订单
  198. * @Author Gorden
  199. * @Date 2024/6/5 10:10
  200. *
  201. * @param Request $request
  202. * @return Response
  203. */
  204. public function goodsOrder(Request $request)
  205. {
  206. $orderId = $request->get('order_id', '');
  207. if (!$orderId) {
  208. return json_fail('参数异常');
  209. }
  210. $orderSheet = OrderSheet::with([
  211. 'order' => function ($query) {
  212. $query->select('order_id', 'order_amount_total', 'order_amount_pay', 'order_is_complete', 'order_category', 'order_status_system', 'order_status_payment', 'order_status_storage', 'order_addtimes');
  213. },
  214. 'goods' => function ($query) {
  215. $query->select('goods_id', 'goods_name', 'goods_status', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
  216. },
  217. 'sku' => function ($query) {
  218. $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
  219. }
  220. ])->where('join_sheet_order_id', $orderId)
  221. ->get()
  222. ->toArray();
  223. return json_success('', $orderSheet);
  224. }
  225. /**
  226. * 余额账户充值
  227. */
  228. public function insertRecharge(Request $request): Response
  229. {
  230. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  231. return json_fail($this->validateClass->getError());
  232. }
  233. $password = $request->post('recharge_password', '');
  234. if (!$password || $password != '123456') {
  235. return json_fail('充值密码错误');
  236. }
  237. Db::beginTransaction();
  238. try {
  239. $data = $this->insertRechargeInput($request);
  240. $data['pay_prepayid'] = $request->post('pay_prepayid', '');
  241. $payDetailId = $this->doInsert($data);
  242. (new RechargeController)->disposePaySuccess($payDetailId);
  243. Db::commit();
  244. } catch (BusinessException $customException) {
  245. Db::rollBack();
  246. return json_fail($customException->getMessage());
  247. } catch (\Exception $e) {
  248. Db::rollBack();
  249. dump($e->getMessage());
  250. return json_fail('数据写入失败11');
  251. }
  252. return json_success('success');
  253. }
  254. /**
  255. * 余额账户赠送
  256. */
  257. public function insertGive(Request $request): Response
  258. {
  259. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  260. return json_fail($this->validateClass->getError());
  261. }
  262. $password = $request->post('recharge_password', '');
  263. if (!$password || $password != '123456') {
  264. return json_fail('赠送密码错误');
  265. }
  266. Db::beginTransaction();
  267. try {
  268. $data = $this->insertGiveInput($request);
  269. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
  270. // 金额累加到福利账户
  271. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'CASH')->first();
  272. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  273. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  274. $memberAccount->save();
  275. $data['pay_amount'] = 0;
  276. $this->doInsert($data);
  277. Db::commit();
  278. } catch (BusinessException $customException) {
  279. Db::rollBack();
  280. return json_fail($customException->getMessage());
  281. } catch (\Exception $e) {
  282. Db::rollBack();
  283. dump($e->getMessage());
  284. return json_fail('数据写入失败11');
  285. }
  286. return json_success('success');
  287. }
  288. /**
  289. * 福利账户充值
  290. */
  291. public function insertRechargeWelfare(Request $request): Response
  292. {
  293. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  294. return json_fail($this->validateClass->getError());
  295. }
  296. $password = $request->post('recharge_password', '');
  297. if (!$password || $password != '123456') {
  298. return json_fail('充值密码错误');
  299. }
  300. Db::beginTransaction();
  301. try {
  302. $data = $this->insertRechargeInput($request, 'WELFARE');
  303. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
  304. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])
  305. ->where('member_account_classify', 'WELFARE')
  306. ->first();
  307. if ($memberAccount->member_account_status != 'ACTIVED'){
  308. throw new BusinessException('福利账户未开启');
  309. }
  310. $this->doInsert($data);
  311. // 金额累加到福利账户
  312. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  313. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  314. $memberAccount->save();
  315. Db::commit();
  316. } catch (BusinessException $customException) {
  317. Db::rollBack();
  318. return json_fail($customException->getMessage());
  319. } catch (\Exception $e) {
  320. Db::rollBack();
  321. dump($e->getMessage());
  322. return json_fail('数据写入失败11');
  323. }
  324. return json_success('success');
  325. }
  326. /**
  327. * @Desc
  328. * @Author Gorden
  329. * @Date 2024/6/5 13:39
  330. *
  331. * @param Request $request
  332. * @return array
  333. * @throws BusinessException
  334. */
  335. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  336. {
  337. $params = $request->post();
  338. $data = $this->inputFilter($params);
  339. $goods = Goods::find($params['goods_id']);
  340. $extendJson = [];
  341. // if (!$params['is_custom']) {
  342. Db::beginTransaction();
  343. try {
  344. if (!$params['is_custom']) {
  345. $data['pay_amount'] = $goods->goods_sales_price;
  346. }
  347. $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  348. $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  349. $orderData = [
  350. 'order_id' => $orderId,
  351. 'order_groupby' => $orderGroupId,
  352. 'join_order_member_id' => $data['join_pay_member_id'],
  353. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  354. 'order_amount_total' => $data['pay_amount'],
  355. 'order_amount_pay' => $data['pay_amount'],
  356. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  357. 'order_status_payment' => 'SUCCESS',
  358. 'order_classify' => 'RECHARGE',
  359. 'order_category' => 'RECHARGE',
  360. 'order_status_system' => 'PAYING',
  361. 'order_status_storage' => 'PENDING',
  362. 'order_addtimes' => time()
  363. ];
  364. if ($type == 'WELFARE') {
  365. $orderData['order_is_complete'] = 'Y';
  366. $orderData['order_status_system'] = 'DONE';
  367. $orderData['order_status_storage'] = 'DONE';
  368. }
  369. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  370. Order::insert($orderData);
  371. $orderSheetData = [
  372. 'join_sheet_member_id' => $data['join_pay_member_id'],
  373. 'join_sheet_order_id' => $orderId,
  374. 'join_sheet_goods_id' => $params['goods_id'],
  375. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  376. 'order_sheet_status' => 'DONE',
  377. 'order_sheet_category' => 'RECHARGE',
  378. 'order_sheet_num' => 1,
  379. 'order_sheet_price' => $data['pay_amount'],
  380. 'order_sheet_amount' => $data['pay_amount'],
  381. 'order_sheet_pay' => $data['pay_amount'],
  382. 'order_sheet_addtimes' => time()
  383. ];
  384. OrderSheet::insert($orderSheetData);
  385. Db::commit();
  386. } catch (\Exception $e) {
  387. dump($e->getMessage());
  388. Db::rollBack();
  389. throw new BusinessException("数据错误");
  390. }
  391. $data['join_pay_order_id'] = $orderGroupId;
  392. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  393. $rate = 0;
  394. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  395. $attributeJson = json_decode($goods->goods_attribute_json, true);
  396. if (isset($attributeJson['added'])) {
  397. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  398. }
  399. }
  400. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  401. $extendJson['added_rate'] = $rate;
  402. // } else {
  403. // // $extendJson['notify'] = 'success';
  404. // $extendJson['is_custom'] = $params['is_custom'];
  405. // }
  406. $data['pay_extend_json'] = json_encode($extendJson);
  407. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  408. $data['pay_prepayid'] = 0;
  409. return $data;
  410. }
  411. /**
  412. * @Desc
  413. * @Author Gorden
  414. * @Date 2024/6/5 13:39
  415. *
  416. * @param Request $request
  417. * @return array
  418. * @throws BusinessException
  419. */
  420. protected function insertGiveInput(Request $request, $type = "CASH"): array
  421. {
  422. $params = $request->post();
  423. $data = $this->inputFilter($params);
  424. $goods = Goods::find($params['goods_id']);
  425. $extendJson = [];
  426. // if (!$params['is_custom']) {
  427. Db::beginTransaction();
  428. try {
  429. if (!$params['is_custom']) {
  430. $data['pay_amount'] = $goods->goods_sales_price;
  431. }
  432. // $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  433. // $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  434. // $orderData = [
  435. // 'order_id' => $orderId,
  436. // 'order_groupby' => $orderGroupId,
  437. // 'join_order_member_id' => $data['join_pay_member_id'],
  438. // 'order_name' => date('Y-m-d H:i:s') . '-订单',
  439. // 'order_amount_total' => $data['pay_amount'],
  440. // 'order_amount_pay' => $data['pay_amount'],
  441. // 'order_amount_json' => '{"tax": 0, "freight": 0}',
  442. // 'order_status_payment' => 'SUCCESS',
  443. // 'order_classify' => 'RECHARGE',
  444. // 'order_category' => 'RECHARGE',
  445. // 'order_status_system' => 'PAYING',
  446. // 'order_status_storage' => 'PENDING',
  447. // 'order_addtimes' => time()
  448. // ];
  449. // if ($type == 'WELFARE') {
  450. // $orderData['order_is_complete'] = 'Y';
  451. // $orderData['order_status_system'] = 'DONE';
  452. // $orderData['order_status_storage'] = 'DONE';
  453. // }
  454. // $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  455. //
  456. // Order::insert($orderData);
  457. // $orderSheetData = [
  458. // 'join_sheet_member_id' => $data['join_pay_member_id'],
  459. // 'join_sheet_order_id' => $orderId,
  460. // 'join_sheet_goods_id' => $params['goods_id'],
  461. // 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  462. // 'order_sheet_status' => 'DONE',
  463. // 'order_sheet_category' => 'RECHARGE',
  464. // 'order_sheet_num' => 1,
  465. // 'order_sheet_price' => $data['pay_amount'],
  466. // 'order_sheet_amount' => $data['pay_amount'],
  467. // 'order_sheet_pay' => $data['pay_amount'],
  468. // 'order_sheet_addtimes' => time()
  469. // ];
  470. //
  471. // OrderSheet::insert($orderSheetData);
  472. Db::commit();
  473. } catch (\Exception $e) {
  474. dump($e->getMessage());
  475. Db::rollBack();
  476. throw new BusinessException("数据错误");
  477. }
  478. $data['join_pay_order_id'] = "SYSTEM";
  479. $data['join_pay_object_json'] = json_encode(['order_id' => 'SYSTEM']);
  480. $rate = 0;
  481. $extendJson['added_amount'] = $data['pay_amount'];
  482. $extendJson['added_rate'] = $rate;
  483. $data['pay_extend_json'] = json_encode($extendJson);
  484. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  485. $data['pay_prepayid'] = 0;
  486. return $data;
  487. }
  488. /**
  489. * @Desc 加备注-门店、服务人员、业务人员
  490. * @Author Gorden
  491. * @Date 2024/8/29 16:14
  492. *
  493. * @param Request $request
  494. * @return Response
  495. */
  496. public function remark(Request $request)
  497. {
  498. $payId = $request->post('pay_id', '');
  499. if (!$payId) {
  500. return json_fail('参数异常');
  501. }
  502. $payDetail = PayDetail::where('pay_id', $payId)->first();
  503. if (!$payDetail) {
  504. return json_fail("数据异常");
  505. }
  506. try {
  507. $data = [
  508. 'premises_id' => $request->post('premises_id', ''),
  509. 'premises_name' => $request->post('premises_name', ''),
  510. 'business' => $request->post('business', ''),
  511. 'service' => $request->post('service', '')
  512. ];
  513. $payExtendJson = [];
  514. if (!empty($payDetail->pay_extend_json)) {
  515. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  516. }
  517. $payExtendJson['remark'] = $data;
  518. $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
  519. $payDetail->save();
  520. return json_success('success');
  521. } catch (\Exception $e) {
  522. return json_fail('备注保存失败');
  523. }
  524. }
  525. }