PayDetailController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. $this->doInsert($data);
  305. // 金额累加到福利账户
  306. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'WELFARE')->first();
  307. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  308. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  309. $memberAccount->save();
  310. Db::commit();
  311. } catch (BusinessException $customException) {
  312. Db::rollBack();
  313. return json_fail($customException->getMessage());
  314. } catch (\Exception $e) {
  315. Db::rollBack();
  316. dump($e->getMessage());
  317. return json_fail('数据写入失败11');
  318. }
  319. return json_success('success');
  320. }
  321. /**
  322. * @Desc
  323. * @Author Gorden
  324. * @Date 2024/6/5 13:39
  325. *
  326. * @param Request $request
  327. * @return array
  328. * @throws BusinessException
  329. */
  330. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  331. {
  332. $params = $request->post();
  333. $data = $this->inputFilter($params);
  334. $goods = Goods::find($params['goods_id']);
  335. $extendJson = [];
  336. // if (!$params['is_custom']) {
  337. Db::beginTransaction();
  338. try {
  339. if (!$params['is_custom']) {
  340. $data['pay_amount'] = $goods->goods_sales_price;
  341. }
  342. $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  343. $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  344. $orderData = [
  345. 'order_id' => $orderId,
  346. 'order_groupby' => $orderGroupId,
  347. 'join_order_member_id' => $data['join_pay_member_id'],
  348. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  349. 'order_amount_total' => $data['pay_amount'],
  350. 'order_amount_pay' => $data['pay_amount'],
  351. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  352. 'order_status_payment' => 'SUCCESS',
  353. 'order_classify' => 'RECHARGE',
  354. 'order_category' => 'RECHARGE',
  355. 'order_status_system' => 'PAYING',
  356. 'order_status_storage' => 'PENDING',
  357. 'order_addtimes' => time()
  358. ];
  359. if ($type == 'WELFARE') {
  360. $orderData['order_is_complete'] = 'Y';
  361. $orderData['order_status_system'] = 'DONE';
  362. $orderData['order_status_storage'] = 'DONE';
  363. }
  364. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  365. Order::insert($orderData);
  366. $orderSheetData = [
  367. 'join_sheet_member_id' => $data['join_pay_member_id'],
  368. 'join_sheet_order_id' => $orderId,
  369. 'join_sheet_goods_id' => $params['goods_id'],
  370. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  371. 'order_sheet_status' => 'DONE',
  372. 'order_sheet_category' => 'RECHARGE',
  373. 'order_sheet_num' => 1,
  374. 'order_sheet_price' => $data['pay_amount'],
  375. 'order_sheet_amount' => $data['pay_amount'],
  376. 'order_sheet_pay' => $data['pay_amount'],
  377. 'order_sheet_addtimes' => time()
  378. ];
  379. OrderSheet::insert($orderSheetData);
  380. Db::commit();
  381. } catch (\Exception $e) {
  382. dump($e->getMessage());
  383. Db::rollBack();
  384. throw new BusinessException("数据错误");
  385. }
  386. $data['join_pay_order_id'] = $orderGroupId;
  387. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  388. $rate = 0;
  389. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  390. $attributeJson = json_decode($goods->goods_attribute_json, true);
  391. if (isset($attributeJson['added'])) {
  392. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  393. }
  394. }
  395. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  396. $extendJson['added_rate'] = $rate;
  397. // } else {
  398. // // $extendJson['notify'] = 'success';
  399. // $extendJson['is_custom'] = $params['is_custom'];
  400. // }
  401. $data['pay_extend_json'] = json_encode($extendJson);
  402. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  403. $data['pay_prepayid'] = 0;
  404. return $data;
  405. }
  406. /**
  407. * @Desc
  408. * @Author Gorden
  409. * @Date 2024/6/5 13:39
  410. *
  411. * @param Request $request
  412. * @return array
  413. * @throws BusinessException
  414. */
  415. protected function insertGiveInput(Request $request, $type = "CASH"): array
  416. {
  417. $params = $request->post();
  418. $data = $this->inputFilter($params);
  419. $goods = Goods::find($params['goods_id']);
  420. $extendJson = [];
  421. // if (!$params['is_custom']) {
  422. Db::beginTransaction();
  423. try {
  424. if (!$params['is_custom']) {
  425. $data['pay_amount'] = $goods->goods_sales_price;
  426. }
  427. // $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  428. // $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  429. // $orderData = [
  430. // 'order_id' => $orderId,
  431. // 'order_groupby' => $orderGroupId,
  432. // 'join_order_member_id' => $data['join_pay_member_id'],
  433. // 'order_name' => date('Y-m-d H:i:s') . '-订单',
  434. // 'order_amount_total' => $data['pay_amount'],
  435. // 'order_amount_pay' => $data['pay_amount'],
  436. // 'order_amount_json' => '{"tax": 0, "freight": 0}',
  437. // 'order_status_payment' => 'SUCCESS',
  438. // 'order_classify' => 'RECHARGE',
  439. // 'order_category' => 'RECHARGE',
  440. // 'order_status_system' => 'PAYING',
  441. // 'order_status_storage' => 'PENDING',
  442. // 'order_addtimes' => time()
  443. // ];
  444. // if ($type == 'WELFARE') {
  445. // $orderData['order_is_complete'] = 'Y';
  446. // $orderData['order_status_system'] = 'DONE';
  447. // $orderData['order_status_storage'] = 'DONE';
  448. // }
  449. // $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  450. //
  451. // Order::insert($orderData);
  452. // $orderSheetData = [
  453. // 'join_sheet_member_id' => $data['join_pay_member_id'],
  454. // 'join_sheet_order_id' => $orderId,
  455. // 'join_sheet_goods_id' => $params['goods_id'],
  456. // 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  457. // 'order_sheet_status' => 'DONE',
  458. // 'order_sheet_category' => 'RECHARGE',
  459. // 'order_sheet_num' => 1,
  460. // 'order_sheet_price' => $data['pay_amount'],
  461. // 'order_sheet_amount' => $data['pay_amount'],
  462. // 'order_sheet_pay' => $data['pay_amount'],
  463. // 'order_sheet_addtimes' => time()
  464. // ];
  465. //
  466. // OrderSheet::insert($orderSheetData);
  467. Db::commit();
  468. } catch (\Exception $e) {
  469. dump($e->getMessage());
  470. Db::rollBack();
  471. throw new BusinessException("数据错误");
  472. }
  473. $data['join_pay_order_id'] = "SYSTEM";
  474. $data['join_pay_object_json'] = json_encode(['order_id' => 'SYSTEM']);
  475. $rate = 0;
  476. $extendJson['added_amount'] = $data['pay_amount'];
  477. $extendJson['added_rate'] = $rate;
  478. $data['pay_extend_json'] = json_encode($extendJson);
  479. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  480. $data['pay_prepayid'] = 0;
  481. return $data;
  482. }
  483. /**
  484. * @Desc 加备注-门店、服务人员、业务人员
  485. * @Author Gorden
  486. * @Date 2024/8/29 16:14
  487. *
  488. * @param Request $request
  489. * @return Response
  490. */
  491. public function remark(Request $request)
  492. {
  493. $payId = $request->post('pay_id', '');
  494. if (!$payId) {
  495. return json_fail('参数异常');
  496. }
  497. $payDetail = PayDetail::where('pay_id', $payId)->first();
  498. if (!$payDetail) {
  499. return json_fail("数据异常");
  500. }
  501. try {
  502. $data = [
  503. 'premises_id' => $request->post('premises_id', ''),
  504. 'premises_name' => $request->post('premises_name', ''),
  505. 'business' => $request->post('business', ''),
  506. 'service' => $request->post('service', '')
  507. ];
  508. $payExtendJson = [];
  509. if (!empty($payDetail->pay_extend_json)) {
  510. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  511. }
  512. $payExtendJson['remark'] = $data;
  513. $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
  514. $payDetail->save();
  515. return json_success('success');
  516. } catch (\Exception $e) {
  517. return json_fail('备注保存失败');
  518. }
  519. }
  520. }