PayDetailController.php 16 KB

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