PayDetailController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. if (!empty($payIds)) {
  41. $where['pay_id'] = ['in', $payIds];
  42. }
  43. }
  44. // else if ($where['pay_category'] == 'RECHARGE') {
  45. // $where['pay_status'] = 'SUCCESS';
  46. // $where['pay_category'] = 'RECHARGE';
  47. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  48. // }
  49. $query = $this->doSelect($where, $field, $order);
  50. return $this->doFormat($query, $format, $limit);
  51. }
  52. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  53. {
  54. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  55. foreach ($where as $column => $value) {
  56. if (is_array($value)) {
  57. if ($value[0] === 'like' || $value[0] === 'not like') {
  58. $model = $model->where($column, $value[0], "%$value[1]%");
  59. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  60. $model = $model->where($column, $value[0], $value[1]);
  61. } elseif ($value[0] == 'in' && !empty($value[1])) {
  62. $valArr = $value[1];
  63. if (is_string($value[1])) {
  64. $valArr = explode(",", trim($value[1]));
  65. }
  66. $model = $model->whereIn($column, $valArr);
  67. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  68. $valArr = $value[1];
  69. if (is_string($value[1])) {
  70. $valArr = explode(",", trim($value[1]));
  71. }
  72. $model = $model->whereNotIn($column, $valArr);
  73. } elseif ($value[0] == 'null') {
  74. $model = $model->whereNull($column);
  75. } elseif ($value[0] == 'not null') {
  76. $model = $model->whereNotNull($column);
  77. } elseif ($value[0] !== '' || $value[1] !== '') {
  78. $model = $model->whereBetween($column, $value);
  79. }
  80. } else {
  81. $model = $model->where($column, $value);
  82. }
  83. }
  84. $model = $model->where(function ($query) {
  85. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY'])
  86. ->orWhere('pay_prepayid', 'like', '%CASH%');
  87. });
  88. if ($field) {
  89. $model = $model->orderBy($field, $order);
  90. }
  91. return $model;
  92. }
  93. public function selectWelfare(Request $request): Response
  94. {
  95. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  96. $order = $request->get('order', 'desc');
  97. $field = $field ?? 'pay_addtimes';
  98. // $where['pay_status'] = 'SUCCESS';
  99. if ($where['pay_category'] == 'OTHER') {
  100. $where['pay_status'] = 'SUCCESS';
  101. $where['pay_category'] = ['<>', 'RECHARGE'];
  102. }
  103. // else if ($where['pay_category'] == 'RECHARGE') {
  104. // $where['pay_status'] = 'SUCCESS';
  105. // $where['pay_category'] = 'RECHARGE';
  106. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  107. // }
  108. $query = $this->doSelectWelfare($where, $field, $order);
  109. return $this->doFormat($query, $format, $limit);
  110. }
  111. protected function doSelectWelfare(array $where, string $field = null, string $order = 'desc')
  112. {
  113. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  114. foreach ($where as $column => $value) {
  115. if (is_array($value)) {
  116. if ($value[0] === 'like' || $value[0] === 'not like') {
  117. $model = $model->where($column, $value[0], "%$value[1]%");
  118. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  119. $model = $model->where($column, $value[0], $value[1]);
  120. } elseif ($value[0] == 'in' && !empty($value[1])) {
  121. $valArr = $value[1];
  122. if (is_string($value[1])) {
  123. $valArr = explode(",", trim($value[1]));
  124. }
  125. $model = $model->whereIn($column, $valArr);
  126. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  127. $valArr = $value[1];
  128. if (is_string($value[1])) {
  129. $valArr = explode(",", trim($value[1]));
  130. }
  131. $model = $model->whereNotIn($column, $valArr);
  132. } elseif ($value[0] == 'null') {
  133. $model = $model->whereNull($column);
  134. } elseif ($value[0] == 'not null') {
  135. $model = $model->whereNotNull($column);
  136. } elseif ($value[0] !== '' || $value[1] !== '') {
  137. $model = $model->whereBetween($column, $value);
  138. }
  139. } else {
  140. $model = $model->where($column, $value);
  141. }
  142. }
  143. $model = $model->where(function ($query) {
  144. $query->Where('pay_prepayid', 'like', '%WELFARE%');
  145. });
  146. if ($field) {
  147. $model = $model->orderBy($field, $order);
  148. }
  149. return $model;
  150. }
  151. public function afterQuery($items)
  152. {
  153. foreach ($items as &$item) {
  154. $prepayId = explode('-', $item->pay_prepayid);
  155. if (count($prepayId) > 1) {
  156. $item->pay_prepayid = $prepayId[1];
  157. }
  158. $memberAccount = [];
  159. if (!empty($item->memberAccount)) {
  160. foreach ($item->memberAccount as $account) {
  161. $memberAccount[strtolower($account->member_account_classify)] = $account;
  162. }
  163. $item->memberAccount = $memberAccount;
  164. }
  165. if (!empty($item->pay_extend_json)) {
  166. $payExtendJson = json_decode($item->pay_extend_json, true);
  167. if (isset($payExtendJson['remark'])) {
  168. $item->service = $payExtendJson['remark']['service'] ?? '';
  169. $item->business = $payExtendJson['remark']['business'] ?? '';
  170. $item->premises_id = $payExtendJson['remark']['premises_id'] ?? '';
  171. $item->premises_name = $payExtendJson['remark']['premises_name'] ?? '';
  172. }
  173. }
  174. }
  175. return $items;
  176. }
  177. /**
  178. * @Desc 产品订单
  179. * @Author Gorden
  180. * @Date 2024/6/5 10:10
  181. *
  182. * @param Request $request
  183. * @return Response
  184. */
  185. public function goodsOrder(Request $request)
  186. {
  187. $orderId = $request->get('order_id', '');
  188. if (!$orderId) {
  189. return json_fail('参数异常');
  190. }
  191. $orderSheet = OrderSheet::with([
  192. 'order' => function ($query) {
  193. $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');
  194. },
  195. 'goods' => function ($query) {
  196. $query->select('goods_id', 'goods_name', 'goods_status', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
  197. },
  198. 'sku' => function ($query) {
  199. $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
  200. }
  201. ])->where('join_sheet_order_id', $orderId)
  202. ->get()
  203. ->toArray();
  204. return json_success('', $orderSheet);
  205. }
  206. /**
  207. * 余额账户充值
  208. */
  209. public function insertRecharge(Request $request): Response
  210. {
  211. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  212. return json_fail($this->validateClass->getError());
  213. }
  214. $password = $request->post('recharge_password', '');
  215. if (!$password || $password != '123456') {
  216. return json_fail('充值密码错误');
  217. }
  218. Db::beginTransaction();
  219. try {
  220. $data = $this->insertRechargeInput($request);
  221. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
  222. $payDetailId = $this->doInsert($data);
  223. (new RechargeController)->disposePaySuccess($payDetailId);
  224. Db::commit();
  225. } catch (BusinessException $customException) {
  226. Db::rollBack();
  227. return json_fail($customException->getMessage());
  228. } catch (\Exception $e) {
  229. Db::rollBack();
  230. dump($e->getMessage());
  231. return json_fail('数据写入失败11');
  232. }
  233. return json_success('success');
  234. }
  235. /**
  236. * 福利账户充值
  237. */
  238. public function insertRechargeWelfare(Request $request): Response
  239. {
  240. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  241. return json_fail($this->validateClass->getError());
  242. }
  243. $password = $request->post('recharge_password', '');
  244. if (!$password || $password != '123456') {
  245. return json_fail('充值密码错误');
  246. }
  247. Db::beginTransaction();
  248. try {
  249. $data = $this->insertRechargeInput($request, 'WELFARE');
  250. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
  251. $this->doInsert($data);
  252. // 金额累加到福利账户
  253. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'WELFARE')->first();
  254. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  255. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  256. $memberAccount->save();
  257. Db::commit();
  258. } catch (BusinessException $customException) {
  259. Db::rollBack();
  260. return json_fail($customException->getMessage());
  261. } catch (\Exception $e) {
  262. Db::rollBack();
  263. dump($e->getMessage());
  264. return json_fail('数据写入失败11');
  265. }
  266. return json_success('success');
  267. }
  268. /**
  269. * @Desc
  270. * @Author Gorden
  271. * @Date 2024/6/5 13:39
  272. *
  273. * @param Request $request
  274. * @return array
  275. * @throws BusinessException
  276. */
  277. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  278. {
  279. $params = $request->post();
  280. $data = $this->inputFilter($params);
  281. $goods = Goods::find($params['goods_id']);
  282. $extendJson = [];
  283. // if (!$params['is_custom']) {
  284. Db::beginTransaction();
  285. try {
  286. if (!$params['is_custom']) {
  287. $data['pay_amount'] = $goods->goods_sales_price;
  288. }
  289. $orderId = "OD" . date("YmdHis") . random_string(6, 'up');
  290. $orderGroupId = "OD" . date("YmdHis") . random_string(6, 'up');
  291. $orderData = [
  292. 'order_id' => $orderId,
  293. 'order_groupby' => $orderGroupId,
  294. 'join_order_member_id' => $data['join_pay_member_id'],
  295. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  296. 'order_amount_total' => $data['pay_amount'],
  297. 'order_amount_pay' => $data['pay_amount'],
  298. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  299. 'order_status_payment' => 'SUCCESS',
  300. 'order_category' => 'RECHARGE',
  301. 'order_status_system' => 'PAYING',
  302. 'order_status_storage' => 'PENDING',
  303. 'order_addtimes' => time()
  304. ];
  305. if ($type == 'WELFARE') {
  306. $orderData['order_is_complete'] = 'Y';
  307. $orderData['order_status_system'] = 'DONE';
  308. $orderData['order_status_storage'] = 'DONE';
  309. }
  310. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  311. Order::insert($orderData);
  312. $orderSheetData = [
  313. 'join_sheet_member_id' => $data['join_pay_member_id'],
  314. 'join_sheet_order_id' => $orderId,
  315. 'join_sheet_goods_id' => $params['goods_id'],
  316. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  317. 'order_sheet_status' => 'DONE',
  318. 'order_sheet_category' => 'RECHARGE',
  319. 'order_sheet_num' => 1,
  320. 'order_sheet_price' => $data['pay_amount'],
  321. 'order_sheet_amount' => $data['pay_amount'],
  322. 'order_sheet_pay' => $data['pay_amount'],
  323. 'order_sheet_addtimes' => time()
  324. ];
  325. OrderSheet::insert($orderSheetData);
  326. Db::commit();
  327. } catch (\Exception $e) {
  328. dump($e->getMessage());
  329. Db::rollBack();
  330. throw new BusinessException("数据错误");
  331. }
  332. $data['join_pay_order_id'] = $orderGroupId;
  333. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  334. $rate = 0;
  335. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  336. $attributeJson = json_decode($goods->goods_attribute_json, true);
  337. if (isset($attributeJson['added'])) {
  338. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  339. }
  340. }
  341. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  342. $extendJson['added_rate'] = $rate;
  343. // } else {
  344. // // $extendJson['notify'] = 'success';
  345. // $extendJson['is_custom'] = $params['is_custom'];
  346. // }
  347. $data['pay_extend_json'] = json_encode($extendJson);
  348. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  349. $data['pay_prepayid'] = 0;
  350. return $data;
  351. }
  352. /**
  353. * @Desc 加备注-门店、服务人员、业务人员
  354. * @Author Gorden
  355. * @Date 2024/8/29 16:14
  356. *
  357. * @param Request $request
  358. * @return Response
  359. */
  360. public function remark(Request $request)
  361. {
  362. $payId = $request->post('pay_id', '');
  363. if (!$payId) {
  364. return json_fail('参数异常');
  365. }
  366. $payDetail = PayDetail::where('pay_id', $payId)->first();
  367. if (!$payDetail) {
  368. return json_fail("数据异常");
  369. }
  370. try {
  371. $data = [
  372. 'premises_id' => $request->post('premises_id', ''),
  373. 'premises_name' => $request->post('premises_name', ''),
  374. 'business' => $request->post('business', ''),
  375. 'service' => $request->post('service', '')
  376. ];
  377. $payExtendJson = [];
  378. if (!empty($payDetail->pay_extend_json)) {
  379. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  380. }
  381. $payExtendJson['remark'] = $data;
  382. $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
  383. $payDetail->save();
  384. return json_success('success');
  385. } catch (\Exception $e) {
  386. return json_fail('备注保存失败');
  387. }
  388. }
  389. }