PayDetailController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 insertRechargeWelfare(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->insertRechargeInput($request, 'WELFARE');
  269. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
  270. $this->doInsert($data);
  271. // 金额累加到福利账户
  272. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'WELFARE')->first();
  273. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  274. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  275. $memberAccount->save();
  276. Db::commit();
  277. } catch (BusinessException $customException) {
  278. Db::rollBack();
  279. return json_fail($customException->getMessage());
  280. } catch (\Exception $e) {
  281. Db::rollBack();
  282. dump($e->getMessage());
  283. return json_fail('数据写入失败11');
  284. }
  285. return json_success('success');
  286. }
  287. /**
  288. * @Desc
  289. * @Author Gorden
  290. * @Date 2024/6/5 13:39
  291. *
  292. * @param Request $request
  293. * @return array
  294. * @throws BusinessException
  295. */
  296. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  297. {
  298. $params = $request->post();
  299. $data = $this->inputFilter($params);
  300. $goods = Goods::find($params['goods_id']);
  301. $extendJson = [];
  302. // if (!$params['is_custom']) {
  303. Db::beginTransaction();
  304. try {
  305. if (!$params['is_custom']) {
  306. $data['pay_amount'] = $goods->goods_sales_price;
  307. }
  308. $orderId = "OD" . date('ymdH') . random_string(6, 'up');
  309. $orderGroupId = "OD" . date('ymdH') . random_string(6, 'up');
  310. $orderData = [
  311. 'order_id' => $orderId,
  312. 'order_groupby' => $orderGroupId,
  313. 'join_order_member_id' => $data['join_pay_member_id'],
  314. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  315. 'order_amount_total' => $data['pay_amount'],
  316. 'order_amount_pay' => $data['pay_amount'],
  317. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  318. 'order_status_payment' => 'SUCCESS',
  319. 'order_category' => 'RECHARGE',
  320. 'order_status_system' => 'PAYING',
  321. 'order_status_storage' => 'PENDING',
  322. 'order_addtimes' => time()
  323. ];
  324. if ($type == 'WELFARE') {
  325. $orderData['order_is_complete'] = 'Y';
  326. $orderData['order_status_system'] = 'DONE';
  327. $orderData['order_status_storage'] = 'DONE';
  328. }
  329. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  330. Order::insert($orderData);
  331. $orderSheetData = [
  332. 'join_sheet_member_id' => $data['join_pay_member_id'],
  333. 'join_sheet_order_id' => $orderId,
  334. 'join_sheet_goods_id' => $params['goods_id'],
  335. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  336. 'order_sheet_status' => 'DONE',
  337. 'order_sheet_category' => 'RECHARGE',
  338. 'order_sheet_num' => 1,
  339. 'order_sheet_price' => $data['pay_amount'],
  340. 'order_sheet_amount' => $data['pay_amount'],
  341. 'order_sheet_pay' => $data['pay_amount'],
  342. 'order_sheet_addtimes' => time()
  343. ];
  344. OrderSheet::insert($orderSheetData);
  345. Db::commit();
  346. } catch (\Exception $e) {
  347. dump($e->getMessage());
  348. Db::rollBack();
  349. throw new BusinessException("数据错误");
  350. }
  351. $data['join_pay_order_id'] = $orderGroupId;
  352. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  353. $rate = 0;
  354. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  355. $attributeJson = json_decode($goods->goods_attribute_json, true);
  356. if (isset($attributeJson['added'])) {
  357. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  358. }
  359. }
  360. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  361. $extendJson['added_rate'] = $rate;
  362. // } else {
  363. // // $extendJson['notify'] = 'success';
  364. // $extendJson['is_custom'] = $params['is_custom'];
  365. // }
  366. $data['pay_extend_json'] = json_encode($extendJson);
  367. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  368. $data['pay_prepayid'] = 0;
  369. return $data;
  370. }
  371. /**
  372. * @Desc 加备注-门店、服务人员、业务人员
  373. * @Author Gorden
  374. * @Date 2024/8/29 16:14
  375. *
  376. * @param Request $request
  377. * @return Response
  378. */
  379. public function remark(Request $request)
  380. {
  381. $payId = $request->post('pay_id', '');
  382. if (!$payId) {
  383. return json_fail('参数异常');
  384. }
  385. $payDetail = PayDetail::where('pay_id', $payId)->first();
  386. if (!$payDetail) {
  387. return json_fail("数据异常");
  388. }
  389. try {
  390. $data = [
  391. 'premises_id' => $request->post('premises_id', ''),
  392. 'premises_name' => $request->post('premises_name', ''),
  393. 'business' => $request->post('business', ''),
  394. 'service' => $request->post('service', '')
  395. ];
  396. $payExtendJson = [];
  397. if (!empty($payDetail->pay_extend_json)) {
  398. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  399. }
  400. $payExtendJson['remark'] = $data;
  401. $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
  402. $payDetail->save();
  403. return json_success('success');
  404. } catch (\Exception $e) {
  405. return json_fail('备注保存失败');
  406. }
  407. }
  408. }