PayDetailController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\admin\controller\notify\RechargeController;
  4. use app\admin\service\goods\GoodsService;
  5. use app\admin\service\goods\GoodsSkuService;
  6. use app\admin\service\member\MemberService;
  7. use app\controller\Curd;
  8. use app\model\Goods;
  9. use app\model\GoodsSku;
  10. use app\model\Member;
  11. use app\model\MemberAccount;
  12. use app\model\Order;
  13. use app\model\OrderSheet;
  14. use app\model\PayDetail;
  15. use app\model\SysDept;
  16. use support\Db;
  17. use support\exception\BusinessException;
  18. use support\Log;
  19. use support\Request;
  20. use support\Response;
  21. use Tinywan\Jwt\JwtToken;
  22. use Webman\Event\Event;
  23. use function PHPSTORM_META\type;
  24. class PayDetailController extends Curd
  25. {
  26. public function __construct()
  27. {
  28. $this->model = new PayDetail();
  29. }
  30. public function select(Request $request): Response
  31. {
  32. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  33. $order = $request->get('order', 'desc');
  34. $field = $field ?? 'pay_addtimes';
  35. // $where['pay_status'] = 'SUCCESS';
  36. if ($where['pay_category'] == 'OTHER') {
  37. $where['pay_status'] = 'SUCCESS';
  38. $where['pay_category'] = ['not in', ['RECHARGE', 'REFUND', 'ROLE_REFUND', 'CROWN_CONTINUE', 'CLEAR', 'GIVE']];
  39. } elseif ($where['pay_category'] == 'RECHARGE') {
  40. $where['pay_category'] = ['in', ['RECHARGE', 'VIP']];
  41. }
  42. if (!empty($where['pay_addtimes'])) {
  43. $where['pay_addtimes'][0] = !empty($where['pay_addtimes'][0]) ? strtotime($where['pay_addtimes'][0]) : '';
  44. $where['pay_addtimes'][1] = !empty($where['pay_addtimes'][1]) ? strtotime($where['pay_addtimes'][1]) : '';
  45. }
  46. if (!empty($request->get('premises_id'))) {
  47. $orderIds = Order::whereJsonContains('order_extend_json->dept_id', intval($request->get('premises_id')))
  48. ->whereIn('order_classify', ['RECHARGE', 'COMBINE', 'PARTNER', 'RECHARGE'])
  49. ->where('order_status_payment', 'SUCCESS')
  50. ->orWhereJsonContains('order_extend_json->referee_dept_id', intval($request->get('premises_id')))
  51. ->pluck('order_id')
  52. ->toArray();
  53. $orderIdsStr = "'" . implode("','", $orderIds) . "'";
  54. $payIds = PayDetail::whereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.order_id')) in ({$orderIdsStr})")
  55. ->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.recharge_order_id')) in ({$orderIdsStr})")
  56. ->pluck('pay_id')
  57. ->toArray();
  58. $where['pay_id'] = '';
  59. if (!empty($payIds)) {
  60. $where['pay_id'] = ['in', $payIds];
  61. }
  62. }
  63. $query = $this->doSelect($where, $field, $order, $request->get());
  64. return $this->doFormat($query, $format, $limit);
  65. }
  66. protected function doSelect(array $where, string $field = null, string $order = 'desc', $params = [])
  67. {
  68. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  69. foreach ($where as $column => $value) {
  70. if (is_array($value)) {
  71. if ($value[0] === 'like' || $value[0] === 'not like') {
  72. $model = $model->where($column, $value[0], "%$value[1]%");
  73. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  74. $model = $model->where($column, $value[0], $value[1]);
  75. } elseif ($value[0] == 'in' && !empty($value[1])) {
  76. $valArr = $value[1];
  77. if (is_string($value[1])) {
  78. $valArr = explode(",", trim($value[1]));
  79. }
  80. $model = $model->whereIn($column, $valArr);
  81. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  82. $valArr = $value[1];
  83. if (is_string($value[1])) {
  84. $valArr = explode(",", trim($value[1]));
  85. }
  86. $model = $model->whereNotIn($column, $valArr);
  87. } elseif ($value[0] == 'null') {
  88. $model = $model->whereNull($column);
  89. } elseif ($value[0] == 'not null') {
  90. $model = $model->whereNotNull($column);
  91. } elseif ($value[0] !== '' || $value[1] !== '') {
  92. $model = $model->whereBetween($column, $value);
  93. }
  94. } else {
  95. $model = $model->where($column, $value);
  96. }
  97. }
  98. if (empty($params['type'])) {
  99. $model = $model->where(function ($query) {
  100. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  101. ->orWhere('pay_prepayid', 'like', '%CASH%')
  102. ->orWhere('pay_prepayid', 'like', '%WELFARE%');
  103. });
  104. } else {
  105. if ($params['type'] == 'NORMAL') {
  106. $model = $model->where(function ($query) {
  107. $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  108. ->orWhere('pay_prepayid', 'like', '%CASH%');
  109. });
  110. } else if ($params['type'] == 'VIP') {
  111. $model = $model->where(function ($query) {
  112. $query->orWhere('pay_category', 'VIP');
  113. });
  114. } else {
  115. $model = $model->where(function ($query) {
  116. $query->orWhere('pay_prepayid', 'like', '%WELFARE%');
  117. });
  118. }
  119. }
  120. if (!empty($params['order_type'])) {
  121. if ($params['order_type'] == 'COMBINE') {
  122. $model = $model->where('join_pay_order_id', 'COMBINE');
  123. } elseif ($params['order_type'] == 'PARTNER') {
  124. $model = $model->where('join_pay_order_id', 'PARTNER');
  125. } elseif ($params['order_type'] == 'VIP') {
  126. $model = $model->where('pay_category', 'VIP');
  127. } elseif ($params['order_type'] == 'RECHARGE') {
  128. $model = $model->where('join_pay_order_id', '<>', 'PARTNER')
  129. ->where('join_pay_order_id', '<>', 'COMBINE')
  130. ->where('pay_category', '<>', 'VIP');
  131. }
  132. }
  133. if ($field) {
  134. $model = $model->orderBy($field, $order);
  135. }
  136. return $model;
  137. }
  138. public function selectWelfare(Request $request): Response
  139. {
  140. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  141. $order = $request->get('order', 'desc');
  142. $field = $field ?? 'pay_addtimes';
  143. // $where['pay_status'] = 'SUCCESS';
  144. if ($where['pay_category'] == 'OTHER') {
  145. $where['pay_status'] = 'SUCCESS';
  146. $where['pay_category'] = ['<>', 'RECHARGE'];
  147. }
  148. // else if ($where['pay_category'] == 'RECHARGE') {
  149. // $where['pay_status'] = 'SUCCESS';
  150. // $where['pay_category'] = 'RECHARGE';
  151. // $where['pay_prepayid'] = ['in','WXPAY,ALIPAY'];
  152. // }
  153. $query = $this->doSelectWelfare($where, $field, $order);
  154. return $this->doFormat($query, $format, $limit);
  155. }
  156. protected function doSelectWelfare(array $where, string $field = null, string $order = 'desc')
  157. {
  158. $model = $this->model->with(['member', 'cert', 'memberAccount']);
  159. foreach ($where as $column => $value) {
  160. if (is_array($value)) {
  161. if ($value[0] === 'like' || $value[0] === 'not like') {
  162. $model = $model->where($column, $value[0], "%$value[1]%");
  163. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  164. $model = $model->where($column, $value[0], $value[1]);
  165. } elseif ($value[0] == 'in' && !empty($value[1])) {
  166. $valArr = $value[1];
  167. if (is_string($value[1])) {
  168. $valArr = explode(",", trim($value[1]));
  169. }
  170. $model = $model->whereIn($column, $valArr);
  171. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  172. $valArr = $value[1];
  173. if (is_string($value[1])) {
  174. $valArr = explode(",", trim($value[1]));
  175. }
  176. $model = $model->whereNotIn($column, $valArr);
  177. } elseif ($value[0] == 'null') {
  178. $model = $model->whereNull($column);
  179. } elseif ($value[0] == 'not null') {
  180. $model = $model->whereNotNull($column);
  181. } elseif ($value[0] !== '' || $value[1] !== '') {
  182. $model = $model->whereBetween($column, $value);
  183. }
  184. } else {
  185. $model = $model->where($column, $value);
  186. }
  187. }
  188. $model = $model->where(function ($query) {
  189. $query->Where('pay_prepayid', 'like', '%WELFARE%');
  190. });
  191. if ($field) {
  192. $model = $model->orderBy($field, $order);
  193. }
  194. return $model;
  195. }
  196. public function afterQuery($items)
  197. {
  198. foreach ($items as &$item) {
  199. $prepayId = explode('-', $item->pay_prepayid);
  200. $item->order_classify = $item->pay_category;
  201. if (count($prepayId) > 1) {
  202. $item->pay_prepayid = $prepayId[1];
  203. }
  204. $item->type = 'NORMAL';
  205. if (isset($prepayId[1]) && $prepayId[1] == 'WELFARE') {
  206. $item->type = 'WELFARE';
  207. } else if ($item->pay_category == 'VIP') {
  208. $item->type = 'VIP';
  209. }
  210. $memberAccount = [];
  211. if (!empty($item->memberAccount)) {
  212. foreach ($item->memberAccount as $account) {
  213. $memberAccount[strtolower($account->member_account_classify)] = $account;
  214. }
  215. $item->memberAccount = $memberAccount;
  216. }
  217. $addedAmount = 0;
  218. if (!empty($item->pay_extend_json)) {
  219. $payExtendJson = json_decode($item->pay_extend_json, true);
  220. if (isset($payExtendJson['remark'])) {
  221. $item->service = $payExtendJson['remark']['service'] ?? '';
  222. $item->business = $payExtendJson['remark']['business'] ?? '';
  223. $item->premises_id = $payExtendJson['remark']['premises_id'] ?? '';
  224. $item->premises_name = $payExtendJson['remark']['premises_name'] ?? '';
  225. }
  226. if (!empty($payExtendJson['added_amount'])) {
  227. $addedAmount = sprintf('%.2f', $payExtendJson['added_amount']);
  228. }
  229. }
  230. $payObjectJson = [];
  231. if (!empty($item->join_pay_object_json)) {
  232. $payObjectJson = json_decode($item->join_pay_object_json, true);
  233. }
  234. $originPayAmount = $item->pay_amount;
  235. if (in_array($item['join_pay_order_id'], ['COMBINE', 'PARTNER', 'FRANCHISEE']) && !empty($item->join_pay_object_json)) {
  236. $item->order_classify = $item['join_pay_order_id'];
  237. if (isset($payObjectJson['recharge_order_id'])) {
  238. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $payObjectJson['recharge_order_id'])
  239. ->where('pay_status', 'SUCCESS')
  240. ->get()
  241. ->toArray();
  242. if (count($payDetails) > 1) {
  243. $item->pay_prepayid = 'BY_STAGES';
  244. } else if (count($payDetails) == 1) {
  245. $item->pay_prepayid = $payDetails[0]['pay_prepayid'];
  246. }
  247. $item->pay_amount = PayDetail::whereJsonContains('join_pay_object_json->order_id', $payObjectJson['recharge_order_id'])
  248. ->where('pay_status', 'SUCCESS')
  249. ->sum('pay_amount');
  250. $orderStatus = OrderSheet::where('join_sheet_order_id', $payObjectJson['recharge_order_id'])->value('order_sheet_status');
  251. if ($orderStatus == 'BEING') {
  252. $item->pay_status = 'BEING';
  253. }
  254. }
  255. }
  256. if (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  257. if (in_array($item->join_pay_order_id, ['PARTNER', 'FRANCHISEE'])) {
  258. $addedAmount = sprintf('%.2f', $payObjectJson['recharge_config']['nbr']);
  259. } else {
  260. $addedAmount = sprintf('%.2f', round($item->pay_amount * $payObjectJson['recharge_config']['nbr'], 2));
  261. }
  262. }
  263. $item->added_amount = $addedAmount;
  264. $item->to_account_amount = sprintf('%.2f', $originPayAmount + $addedAmount);
  265. if ($item->pay_category == 'VIP') {
  266. $payObjectJson = json_decode($item->join_pay_object_json, true);
  267. if (isset($payObjectJson['order_id'])) {
  268. $orderStatus = OrderSheet::where('join_sheet_order_id', $payObjectJson['order_id'])->value('order_sheet_status');
  269. if ($orderStatus == 'BEING') {
  270. $item->pay_status = 'BEING';
  271. }
  272. }
  273. }
  274. }
  275. return $items;
  276. }
  277. public function statistics(Request $request)
  278. {
  279. $type = $request->get('type');
  280. $orderType = $request->get('order_type');
  281. $premisesId = $request->get('premises_id');
  282. $memberId = $request->get('join_pay_member_id');
  283. $payAddtimes = $request->get('pay_addtimes');
  284. if (!empty($payAddtimes)) {
  285. $payAddtimes[0] = strtotime($payAddtimes[0]);
  286. $payAddtimes[1] = strtotime($payAddtimes[1]);
  287. }
  288. $payDetailModel = PayDetail::where('pay_status', 'SUCCESS')
  289. ->where('pay_category', 'RECHARGE')
  290. ->select(Db::raw("SUM(CASE WHEN join_pay_order_id='COMBINE' THEN 1000 ELSE pay_amount END) as amount, COUNT(pay_id) as total"));
  291. // ->when(!empty($type), function ($query) use ($type) {
  292. // if ($type == 'NORMAL') {
  293. // $query->whereIn('pay_prepayid', ['ALIPAY', 'WXPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  294. // ->orWhere('pay_prepayid', 'like', '%CASH%');
  295. // } else if ($type == 'VIP') {
  296. // $query->where('pay_category', 'VIP');
  297. // } else {
  298. // $query->where('pay_prepayid', 'like', '%WELFARE%');
  299. // }
  300. // })->when(!empty($orderType), function ($query) use ($orderType) {
  301. // if (in_array($orderType, ['COMBINE', 'PARTNER'])) {
  302. // $query->where('join_pay_order_id', $orderType);
  303. // } elseif ($orderType == 'VIP') {
  304. // $query->where('pay_category', 'VIP');
  305. // } elseif ($orderType == 'RECHARGE') {
  306. // $query->where('join_pay_order_id', '<>', 'PARTNER')
  307. // ->where('join_pay_order_id', '<>', 'COMBINE')
  308. // ->where('pay_category', '<>', 'VIP');
  309. // }
  310. // })->when(!empty($premisesId), function ($query) use ($premisesId) {
  311. // $orderIds = Order::whereJsonContains('order_extend_json->dept_id', intval($premisesId))
  312. // ->whereIn('order_classify', ['RECHARGE', 'COMBINE', 'PARTNER', 'RECHARGE'])
  313. // ->where('order_status_payment', 'SUCCESS')
  314. // ->orWhereJsonContains('order_extend_json->referee_dept_id', intval($premisesId))
  315. // ->pluck('order_id')
  316. // ->toArray();
  317. // $orderIdsStr = "'" . implode("','", $orderIds) . "'";
  318. // $payIds = PayDetail::whereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.order_id')) in ({$orderIdsStr})")
  319. // ->orWhereRaw("JSON_UNQUOTE(JSON_EXTRACT(join_pay_object_json,'$.recharge_order_id')) in ({$orderIdsStr})")
  320. // ->pluck('pay_id')
  321. // ->toArray();
  322. // $query->whereIn('pay_id', $payIds);
  323. // })->when(!empty($memberId), function ($query) use ($memberId) {
  324. // $query->where('join_pay_member_id', $memberId);
  325. // })->when(!empty($payAddtimes),function ($query) use ($payAddtimes){
  326. // $query->whereBetween('pay_addtimes',$payAddtimes);
  327. // });
  328. $totalModel = clone $payDetailModel;
  329. $total = $totalModel->first()->toArray();
  330. $todayUnixStart = strtotime(date('Y-m-d'));
  331. $todayUnixEnd = strtotime(date('Y-m-d 23:59:59'));
  332. $todayTotalModel = clone $payDetailModel;
  333. $todayTotal = $todayTotalModel->whereBetween('pay_addtimes', [$todayUnixStart, $todayUnixEnd])
  334. ->first()
  335. ->toArray();
  336. $yesterdayStart = strtotime(date('Y-m-d', strtotime("-1 days")));
  337. $yesterdayEnd = strtotime(date('Y-m-d 23:59:59', strtotime("-1 days")));
  338. $yesterdayTotalModel = clone $payDetailModel;
  339. $yesterdayTotal = $yesterdayTotalModel->whereBetween('pay_addtimes', [$yesterdayStart, $yesterdayEnd])
  340. ->first()
  341. ->toArray();
  342. $monthStart = strtotime(date('Y-m-01'));
  343. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  344. $monthTotalModel = clone $payDetailModel;
  345. $monthTotal = $monthTotalModel->whereBetween('pay_addtimes', [$monthStart, $monthEnd])
  346. ->first()
  347. ->toArray();
  348. return json_success('success', [
  349. 'total' => $total,
  350. 'todayTotal' => $todayTotal,
  351. 'yesterdayTotal' => $yesterdayTotal,
  352. 'monthTotal' => $monthTotal
  353. ]);
  354. }
  355. /**
  356. * @Desc 产品订单
  357. * @Author Gorden
  358. * @Date 2024/6/5 10:10
  359. *
  360. * @param Request $request
  361. * @return Response
  362. */
  363. public function goodsOrder(Request $request)
  364. {
  365. $orderId = $request->get('order_id', '');
  366. $memberId = $request->get('member_id', '');
  367. if (!$orderId || !$memberId) {
  368. return json_fail('参数异常');
  369. }
  370. $orderSheet = OrderSheet::with([
  371. 'order' => function ($query) {
  372. $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_extend_json', 'order_addtimes');
  373. },
  374. 'goods' => function ($query) {
  375. $query->select('goods_id', 'goods_name', 'goods_status', 'goods_cover', 'goods_market_price', 'goods_sales_price', 'goods_addtimes');
  376. },
  377. 'sku' => function ($query) {
  378. $query->select('goods_sku_id', 'goods_sku_specs_json', 'goods_sku_market_price', 'goods_sku_sales_price');
  379. }
  380. ])->where('join_sheet_order_id', $orderId)
  381. ->get()
  382. ->toArray();
  383. $member = Member::with([
  384. 'cert' => function ($query) {
  385. $query->select('join_cert_member_id', 'member_cert_name');
  386. },
  387. 'info' => function ($query) {
  388. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  389. }
  390. ])->where('member_id', $memberId)
  391. ->select('member_id', 'member_mobile', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'member_is_franchisee')
  392. ->first();
  393. if (!$member) {
  394. return json_fail('数据异常');
  395. }
  396. $member = $member->toArray();
  397. $certName = $member['cert']['member_cert_name'] ?? '';
  398. $nickname = $member['info']['member_info_nickname'] ?? '';
  399. $mobile = $member['member_mobile'] ?? '';
  400. $member['member_name'] = MemberService::getMemberCertName($mobile, $certName, $nickname);
  401. $member['info']['member_info_headimg'] = MemberService::getAvatarUrl($member['info']['member_info_headimg'] ?? '');
  402. $member['level'] = MemberService::getRoleName($member['join_member_role_id']);
  403. $order = [];
  404. $payDetails = PayDetail::whereJsonContains('join_pay_object_json->order_id', $orderId)
  405. ->where('pay_status', 'SUCCESS')
  406. ->select('pay_status', 'pay_amount', 'pay_paytimes', 'pay_prepayid', 'join_pay_object_json', 'pay_extend_json')
  407. ->orderBy('pay_paytimes', 'DESC')
  408. ->get()
  409. ->toArray();
  410. foreach ($orderSheet as &$item) {
  411. if (!empty($item['goods']) && !empty($item['goods']['goods_cover'])) {
  412. $item['goods']['goods_cover'] = getenv("STORAGE_DOMAIN") . $item['goods']['goods_cover'];
  413. }
  414. if (!empty($item['sku']) && !empty($item['sku']['goods_sku_specs_json'])) {
  415. $item['sku']['goods_sku_title'] = GoodsSkuService::getSkuTitle($item['sku']['goods_sku_specs_json']);
  416. }
  417. if (!empty($item['order'])) {
  418. if (!empty($item['order']['order_extend_json'])) {
  419. $orderExtendJson = json_decode($item['order']['order_extend_json'], true);
  420. if (!empty($orderExtendJson['referee'])) {
  421. $item['order']['referee'] = $orderExtendJson['referee'];
  422. }
  423. if (!empty($orderExtendJson['referee_premises'])) {
  424. $item['order']['referee_premises'] = $orderExtendJson['referee_premises'];
  425. }
  426. }
  427. }
  428. $order = $item['order'];
  429. }
  430. $order['added_amount'] = 0;
  431. foreach ($payDetails as $detail) {
  432. if (!empty($detail['join_pay_object_json'])) {
  433. $payObjectJson = json_decode($detail['join_pay_object_json'], true);
  434. if (!empty($payObjectJson['recharge_config']) && $payObjectJson['recharge_config']['nbr'] > 0) {
  435. if ($detail['join_pay_order_id'] == 'PARTNER') {
  436. $order['added_amount'] = sprintf('%.2f', $payObjectJson['recharge_config']['nbr']);
  437. } else {
  438. $order['added_amount'] = sprintf('%.2f', round($detail['pay_amount'] * $payObjectJson['recharge_config']['nbr'], 2));
  439. }
  440. // $order['added_amount'] = sprintf('%.2f',round($detail['pay_amount'] * $payObjectJson['recharge_config']['nbr'], 2));
  441. }
  442. }
  443. if ($order['added_amount'] == 0 && !empty($detail['pay_extend_json'])) {
  444. $payExtendJson = json_decode($detail['pay_extend_json'], true);
  445. if (!empty($payExtendJson['added_amount'])) {
  446. $order['added_amount'] = sprintf('%.2f', $payExtendJson['added_amount']);
  447. }
  448. }
  449. }
  450. $data = [
  451. 'payDetails' => $payDetails,
  452. 'goodsOrder' => $orderSheet,
  453. 'member' => $member,
  454. 'order' => $order
  455. ];
  456. return json_success('', $data);
  457. }
  458. /**
  459. * 余额账户充值
  460. */
  461. public function insertRecharge(Request $request): Response
  462. {
  463. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  464. return json_fail($this->validateClass->getError());
  465. }
  466. $password = $request->post('recharge_password', '');
  467. if (!$password || $password != '123456') {
  468. return json_fail('充值密码错误');
  469. }
  470. Db::beginTransaction();
  471. try {
  472. $data = $this->insertRechargeInput($request);
  473. $data['pay_prepayid'] = $request->post('pay_prepayid', '');
  474. unset($data['orderId']);
  475. $payDetailId = $this->doInsert($data);
  476. (new RechargeController)->disposePaySuccess($payDetailId);
  477. Db::commit();
  478. } catch (BusinessException $customException) {
  479. Db::rollBack();
  480. _syslog('充值', '后台充值失败', false, $request->all());
  481. Log::error("后台充值失败", ['msg' => $customException->getMessage()]);
  482. return json_fail($customException->getMessage());
  483. } catch (\Exception $e) {
  484. Db::rollBack();
  485. _syslog('充值', '后台充值失败', false, $request->all());
  486. Log::error("后台充值失败", ['msg' => $e->getMessage()]);
  487. return json_fail('数据写入失败');
  488. }
  489. _syslog('充值', '后台充值成功', false, $request->all());
  490. return json_success('success');
  491. }
  492. /**
  493. * 余额账户赠送
  494. */
  495. public function insertGive(Request $request): Response
  496. {
  497. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  498. return json_fail($this->validateClass->getError());
  499. }
  500. $password = $request->post('recharge_password', '');
  501. if (!$password || $password != '123456') {
  502. return json_fail('赠送密码错误');
  503. }
  504. Db::beginTransaction();
  505. try {
  506. $data = $this->insertGiveInput($request);
  507. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-CASH';
  508. // 金额累加到福利账户
  509. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])->where('member_account_classify', 'CASH')->first();
  510. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  511. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  512. $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
  513. $memberAccount->member_account_updatetimes = time();
  514. $memberAccount->save();
  515. $data['pay_amount'] = 0;
  516. $this->doInsert($data);
  517. Db::commit();
  518. } catch (BusinessException $customException) {
  519. Db::rollBack();
  520. return json_fail($customException->getMessage());
  521. } catch (\Exception $e) {
  522. Db::rollBack();
  523. dump($e->getMessage());
  524. return json_fail('数据写入失败11');
  525. }
  526. return json_success('success');
  527. }
  528. /**
  529. * 福利账户充值
  530. */
  531. public function insertRechargeWelfare(Request $request): Response
  532. {
  533. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  534. return json_fail($this->validateClass->getError());
  535. }
  536. $password = $request->post('recharge_password', '');
  537. if (!$password || $password != '123456') {
  538. return json_fail('充值密码错误');
  539. }
  540. Db::beginTransaction();
  541. try {
  542. $data = $this->insertRechargeInput($request, 'WELFARE');
  543. $orderId = $data['orderId'];
  544. unset($data['orderId']);
  545. $data['pay_prepayid'] = $data['join_pay_member_id'] . '-WELFARE';
  546. $memberAccount = MemberAccount::where('join_account_member_id', $data['join_pay_member_id'])
  547. ->where('member_account_classify', 'WELFARE')
  548. ->first();
  549. if ($memberAccount->member_account_status != 'ACTIVED') {
  550. throw new BusinessException('福利账户未开启');
  551. }
  552. $this->doInsert($data);
  553. // 金额累加到福利账户
  554. $memberAccount->member_account_income = $memberAccount->member_account_income + $data['pay_amount'];
  555. $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $data['pay_amount'];
  556. $memberAccount->member_account_update_user_id = JwtToken::getCurrentId();
  557. $memberAccount->member_account_updatetimes = time();
  558. $memberAccount->save();
  559. Db::commit();
  560. // 入收支明细表
  561. $params['orderId'] = $orderId;
  562. $params['inout_category'] = '会员充值';
  563. Event::dispatch('statistics.inout.in', $params);
  564. } catch (BusinessException $customException) {
  565. Db::rollBack();
  566. return json_fail($customException->getMessage());
  567. } catch (\Exception $e) {
  568. Db::rollBack();
  569. dump($e->getMessage());
  570. return json_fail('数据写入失败11');
  571. }
  572. return json_success('success');
  573. }
  574. /**
  575. * @Desc
  576. * @Author Gorden
  577. * @Date 2024/6/5 13:39
  578. *
  579. * @param Request $request
  580. * @return array
  581. * @throws BusinessException
  582. */
  583. protected function insertRechargeInput(Request $request, $type = "CASH"): array
  584. {
  585. $params = $request->post();
  586. $data = $this->inputFilter($params);
  587. $goods = Goods::find($params['goods_id']);
  588. $extendJson = [];
  589. $orderExtendJson = [];
  590. // if (!$params['is_custom']) {
  591. // Db::beginTransaction();
  592. try {
  593. if (!$params['is_custom']) {
  594. $data['pay_amount'] = $goods->goods_sales_price;
  595. }
  596. $orderExtendJson['referee_dept_id'] = $params['dept_id'] ?? '';
  597. $orderExtendJson['referee'] = $params['referee'] ?? '';
  598. if (!empty($params['dept_id'])) {
  599. $orderExtendJson['referee_premises'] = SysDept::where('dept_id', $params['dept_id'])->value('dept_name');
  600. }
  601. $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  602. $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  603. $orderData = [
  604. 'order_id' => $orderId,
  605. 'order_groupby' => $orderGroupId,
  606. 'join_order_member_id' => $data['join_pay_member_id'],
  607. 'order_name' => date('Y-m-d H:i:s') . '-订单',
  608. 'order_amount_total' => $data['pay_amount'],
  609. 'order_amount_pay' => $data['pay_amount'],
  610. 'order_amount_json' => '{"tax": 0, "freight": 0}',
  611. 'order_status_payment' => 'SUCCESS',
  612. 'order_classify' => 'RECHARGE',
  613. 'order_category' => 'RECHARGE',
  614. 'order_status_system' => 'PAYING',
  615. 'order_status_storage' => 'PENDING',
  616. 'order_config_json' => json_encode(['order_category' => 'RECHARGE']),
  617. 'order_extend_json' => json_encode($orderExtendJson),
  618. 'order_platform' => 'SYSTEM',
  619. 'order_addtimes' => time()
  620. ];
  621. if ($type == 'WELFARE') {
  622. $orderData['order_is_complete'] = 'Y';
  623. $orderData['order_status_system'] = 'DONE';
  624. $orderData['order_status_storage'] = 'DONE';
  625. unset($orderData['order_config_json']);
  626. }
  627. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  628. Order::insert($orderData);
  629. $orderSheetData = [
  630. 'join_sheet_member_id' => $data['join_pay_member_id'],
  631. 'join_sheet_order_id' => $orderId,
  632. 'join_sheet_goods_id' => $params['goods_id'],
  633. 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  634. 'order_sheet_status' => 'DONE',
  635. 'order_sheet_category' => 'RECHARGE',
  636. 'order_sheet_num' => 1,
  637. 'order_sheet_price' => $data['pay_amount'],
  638. 'order_sheet_amount' => $data['pay_amount'],
  639. 'order_sheet_pay' => $data['pay_amount'],
  640. 'order_sheet_addtimes' => time()
  641. ];
  642. OrderSheet::insert($orderSheetData);
  643. // Db::commit();
  644. } catch (\Exception $e) {
  645. // Db::rollBack();
  646. throw new BusinessException("数据错误");
  647. }
  648. $data['orderId'] = $orderId;
  649. $data['join_pay_order_id'] = $orderGroupId;
  650. $data['join_pay_object_json'] = json_encode(['order_id' => $orderId]);
  651. $rate = 0;
  652. if (!empty($goods->goods_attribute_json) && $type == "CASH") {
  653. $attributeJson = json_decode($goods->goods_attribute_json, true);
  654. if (isset($attributeJson['added'])) {
  655. $rate = $attributeJson['added'] ? $attributeJson['added']['nbr'] : 0;
  656. }
  657. }
  658. $extendJson['added_amount'] = number_format($data['pay_amount'] * $rate, 2, '.', '');
  659. $extendJson['added_rate'] = $rate;
  660. // } else {
  661. // // $extendJson['notify'] = 'success';
  662. // $extendJson['is_custom'] = $params['is_custom'];
  663. // }
  664. $data['pay_extend_json'] = json_encode($extendJson);
  665. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  666. $data['pay_prepayid'] = 0;
  667. return $data;
  668. }
  669. /**
  670. * @Desc
  671. * @Author Gorden
  672. * @Date 2024/6/5 13:39
  673. *
  674. * @param Request $request
  675. * @return array
  676. * @throws BusinessException
  677. */
  678. protected function insertGiveInput(Request $request, $type = "CASH"): array
  679. {
  680. $params = $request->post();
  681. $data = $this->inputFilter($params);
  682. $goods = Goods::find($params['goods_id']);
  683. $extendJson = [];
  684. // if (!$params['is_custom']) {
  685. Db::beginTransaction();
  686. try {
  687. if (!$params['is_custom']) {
  688. $data['pay_amount'] = $goods->goods_sales_price;
  689. }
  690. // $orderId = "OD" . date('ymdHi') . random_string(4, 'up');
  691. // $orderGroupId = "OD" . date('ymdHi') . random_string(4, 'up');
  692. // $orderData = [
  693. // 'order_id' => $orderId,
  694. // 'order_groupby' => $orderGroupId,
  695. // 'join_order_member_id' => $data['join_pay_member_id'],
  696. // 'order_name' => date('Y-m-d H:i:s') . '-订单',
  697. // 'order_amount_total' => $data['pay_amount'],
  698. // 'order_amount_pay' => $data['pay_amount'],
  699. // 'order_amount_json' => '{"tax": 0, "freight": 0}',
  700. // 'order_status_payment' => 'SUCCESS',
  701. // 'order_classify' => 'RECHARGE',
  702. // 'order_category' => 'RECHARGE',
  703. // 'order_status_system' => 'PAYING',
  704. // 'order_status_storage' => 'PENDING',
  705. // 'order_addtimes' => time()
  706. // ];
  707. // if ($type == 'WELFARE') {
  708. // $orderData['order_is_complete'] = 'Y';
  709. // $orderData['order_status_system'] = 'DONE';
  710. // $orderData['order_status_storage'] = 'DONE';
  711. // }
  712. // $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  713. //
  714. // Order::insert($orderData);
  715. // $orderSheetData = [
  716. // 'join_sheet_member_id' => $data['join_pay_member_id'],
  717. // 'join_sheet_order_id' => $orderId,
  718. // 'join_sheet_goods_id' => $params['goods_id'],
  719. // 'join_sheet_goods_sku_id' => $sku ? $sku->goods_sku_id : 0,
  720. // 'order_sheet_status' => 'DONE',
  721. // 'order_sheet_category' => 'RECHARGE',
  722. // 'order_sheet_num' => 1,
  723. // 'order_sheet_price' => $data['pay_amount'],
  724. // 'order_sheet_amount' => $data['pay_amount'],
  725. // 'order_sheet_pay' => $data['pay_amount'],
  726. // 'order_sheet_addtimes' => time()
  727. // ];
  728. //
  729. // OrderSheet::insert($orderSheetData);
  730. Db::commit();
  731. } catch (\Exception $e) {
  732. dump($e->getMessage());
  733. Db::rollBack();
  734. throw new BusinessException("数据错误");
  735. }
  736. $data['join_pay_order_id'] = "SYSTEM";
  737. $data['join_pay_object_json'] = json_encode(['order_id' => 'SYSTEM']);
  738. $rate = 0;
  739. $extendJson['added_amount'] = $data['pay_amount'];
  740. $extendJson['added_rate'] = $rate;
  741. $data['pay_extend_json'] = json_encode($extendJson);
  742. $data['pay_paytimes'] = date('Y-m-d H:i:s', strtotime($data['pay_paytimes']));
  743. $data['pay_prepayid'] = 0;
  744. return $data;
  745. }
  746. /**
  747. * @Desc 加备注-门店、服务人员、业务人员
  748. * @Author Gorden
  749. * @Date 2024/8/29 16:14
  750. *
  751. * @param Request $request
  752. * @return Response
  753. */
  754. public function remark(Request $request)
  755. {
  756. $payId = $request->post('pay_id', '');
  757. if (!$payId) {
  758. return json_fail('参数异常');
  759. }
  760. $payDetail = PayDetail::where('pay_id', $payId)->first();
  761. if (!$payDetail) {
  762. return json_fail("数据异常");
  763. }
  764. try {
  765. $data = [
  766. 'premises_id' => $request->post('premises_id', ''),
  767. 'premises_name' => $request->post('premises_name', ''),
  768. 'business' => $request->post('business', ''),
  769. 'service' => $request->post('service', '')
  770. ];
  771. $payExtendJson = [];
  772. if (!empty($payDetail->pay_extend_json)) {
  773. $payExtendJson = json_decode($payDetail->pay_extend_json, true);
  774. }
  775. $payExtendJson['remark'] = $data;
  776. $payDetail->pay_extend_json = json_encode($payExtendJson, JSON_UNESCAPED_UNICODE);
  777. $payDetail->save();
  778. return json_success('success');
  779. } catch (\Exception $e) {
  780. return json_fail('备注保存失败');
  781. }
  782. }
  783. }