InOutEvent.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\event\statistics;
  3. use app\model\DataInout;
  4. use app\model\MemberAccountList;
  5. use app\model\Order;
  6. use app\model\OrderSheet;
  7. use app\model\PayDetail;
  8. use app\model\SysDept;
  9. use support\exception\BusinessException;
  10. use support\Log;
  11. class InOutEvent
  12. {
  13. /*
  14. * {"label": "标准订单收入", "value": "标准订单收入"},
  15. {"label": "餐饮订单收入", "value": "餐饮订单收入"},
  16. {"label": "购买康养城VIP套餐包收入", "value": "购买康养城VIP套餐包收入"},
  17. {"label": "购买会员合伙人收入", "value": "购买会员合伙人收入"},
  18. {"label": "购买康养推荐官收入", "value": "购买康养推荐官收入"},
  19. {"label": "购买组合包收入", "value": "购买组合包收入"},
  20. {"label": "会员充值订单收入", "value": "会员充值订单收入"},
  21. {"label": "挂账订单结算收入", "value": "挂账订单结算收入"},
  22. {"label": "购买储值卡订单收入", "value": "购买储值卡订单收入"},
  23. {"label": "佣金提现", "value": "佣金提现"},
  24. {"label": "退款", "value": "退款"}
  25. */
  26. /**
  27. * @Desc 收入
  28. * @Author Gorden
  29. * @Date 2024/10/12 15:23
  30. *
  31. * @param $params
  32. * @return void
  33. */
  34. public function dataIn($params)
  35. {
  36. try {
  37. $order = Order::where('order_id', $params['orderId'])->first();
  38. $sheets = OrderSheet::with([
  39. 'goods' => function ($query) {
  40. $query->select('goods_id', 'goods_name', 'goods_classify');
  41. }
  42. ])->where('join_sheet_order_id', $params['orderId'])
  43. ->get()
  44. ->toArray();
  45. $payDetails = PayDetail::where('join_pay_order_id', $order->order_groupby)
  46. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  47. ->where('pay_status', 'SUCCESS')
  48. ->get()
  49. ->toArray();
  50. $data = [
  51. 'dept_id' => 0,
  52. 'member_id' => $order->join_order_member_id,
  53. 'inout_classify' => 'IN',
  54. 'inout_category' => $params['inout_category'] ?? '标准订单',
  55. ];
  56. // 部门id
  57. if (!empty($order->order_config_json)) {
  58. $orderConfigJson = json_decode($order->order_config_json, true);
  59. if (!empty($orderConfigJson['dept'])) {
  60. $data['dept_id'] = $orderConfigJson['dept'];
  61. } elseif (!empty($orderConfigJson['premises'])) {
  62. $data['dept_id'] = SysDept::where('dept_name', $orderConfigJson['premises'])->value('dept_id');
  63. }
  64. }
  65. $inoutObjectJson = [];
  66. // 产品
  67. foreach ($sheets as $sheet) {
  68. $inoutObjectJson['goods'][] = [
  69. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  70. 'goods_name' => $sheet['goods']['goods_name'] ?? ''
  71. ];
  72. $data['inout_name'][] = $sheet['goods']['goods_name'] ?? '';
  73. if (!isset($inoutObjectJson['order'])) {
  74. $inoutObjectJson['order'][$params['orderId']] = [
  75. 'ordertime' => date('Y-m-d H:i:s', strtotime($order->order_addtimes)),
  76. 'pay' => $order->order_amount_pay,
  77. 'classify' => $order->order_classify,
  78. 'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
  79. ];
  80. }
  81. $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
  82. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  83. 'goods_name' => $sheet['goods']['goods_name'] ?? '',
  84. 'goods_sku_id' => $sheet['join_sheet_goods_sku_id'],
  85. 'order_sheet_num' => $sheet['order_sheet_num'],
  86. 'order_sheet_pay' => $sheet['order_sheet_pay']
  87. ];
  88. $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
  89. }
  90. $inoutObjectJson['ordergroup'] = $order->order_groupby;
  91. $data['inout_object_json'] = json_encode($inoutObjectJson);
  92. $data['inout_name'] = implode(',', $data['inout_name']);
  93. if (strlen($data['inout_name']) > 100) {
  94. $data['inout_name'] = mb_substr($data['inout_name'], 0, 100) . '等';
  95. }
  96. $payJson = [
  97. 'trade' => '',
  98. 'if_union' => count($payDetails) > 1 ? 'Y' : 'N',
  99. 'union_order_id' => count($payDetails) > 1 ? $params['orderId'] : ''
  100. ];
  101. foreach ($payDetails as $payDetail) {
  102. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  103. $data['inout_attr'] = 'MONEY';
  104. } else {
  105. $data['inout_attr'] = 'ACCOUNT';
  106. }
  107. $data['inout_amount'] = $payDetail['pay_amount'];
  108. $prepayId = explode('-', $payDetail['pay_prepayid']);
  109. if (count($prepayId) == 1) {
  110. $data['pay_type'] = $payDetail['pay_prepayid'];
  111. } elseif (count($prepayId) > 1) {
  112. $data['pay_type'] = $prepayId[1];
  113. }
  114. if (!empty($payDetail['pay_json_response'])) {
  115. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  116. if (isset($payJsonResponse['transaction_id'])) {
  117. $payJson['trade'] = $payJsonResponse['transaction_id'];
  118. } elseif (isset($payJsonResponse['trade_no'])) {
  119. $payJson['trade'] = $payJsonResponse['trade_no'];
  120. }
  121. }
  122. $data['pay_json'] = json_encode($payJson);
  123. $this->dataSave($data);
  124. }
  125. } catch (\Exception $e) {
  126. _syslog("统计", '收支明细统计失败', ['msg' => $e->getMessage()]);
  127. Log::error("收支明细统计失败", ['msg' => $e->getMessage()]);
  128. }
  129. }
  130. public function dataOut($params)
  131. {
  132. try {
  133. if ($params['type'] == 'refund') {
  134. $data = $this->orderRefund($params);
  135. } elseif ($params['type'] == 'withdraw') {
  136. $data = $this->commissionWithdraw($params);
  137. } else {
  138. throw new BusinessException("不支持的支出类型");
  139. }
  140. $this->dataSave($data);
  141. } catch (BusinessException $e) {
  142. Log::error("统计支出失败:" . $e->getMessage(), $params);
  143. } catch (\Exception $e) {
  144. Log::error("统计支出失败:" . $e->getMessage(), $params);
  145. }
  146. }
  147. public function orderRefund($params)
  148. {
  149. try {
  150. $order = Order::where('order_id', $params['orderId'])->first();
  151. $sheets = OrderSheet::with([
  152. 'goods' => function ($query) {
  153. $query->select('goods_id', 'goods_name', 'goods_classify');
  154. }
  155. ])->where('join_sheet_order_id', $params['orderId'])
  156. ->get()
  157. ->toArray();
  158. $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)
  159. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  160. ->where('pay_category', 'REFUND')
  161. ->where('pay_status', 'SUCCESS')
  162. ->first();
  163. if (!$payDetail) {
  164. throw new BusinessException("支付数据异常");
  165. }
  166. $payDetail = $payDetail->toArray();
  167. $data = [
  168. 'dept_id' => 0,
  169. 'member_id' => $order->join_order_member_id,
  170. 'inout_classify' => 'OUT',
  171. 'inout_category' => !empty(self::$orderClassify[$order->order_classify]) ? self::$orderClassify[$order->order_classify] . '退款' : '退款',
  172. ];
  173. // 部门id
  174. if (!empty($order->order_config_json)) {
  175. $orderConfigJson = json_decode($order->order_config_json, true);
  176. if (!empty($orderConfigJson['dept'])) {
  177. $data['dept_id'] = $orderConfigJson['dept'];
  178. } elseif (!empty($orderConfigJson['premises'])) {
  179. $data['dept_id'] = SysDept::where('dept_name', $orderConfigJson['premises'])->value('dept_id');
  180. }
  181. }
  182. $inoutObjectJson = [];
  183. // 产品
  184. foreach ($sheets as $sheet) {
  185. $inoutObjectJson['goods'][] = [
  186. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  187. 'goods_name' => $sheet['goods']['goods_name'] ?? ''
  188. ];
  189. $data['inout_name'][] = $sheet['goods']['goods_name'] ?? '';
  190. if (!isset($inoutObjectJson['order'])) {
  191. $inoutObjectJson['order'][$params['orderId']] = [
  192. 'pay' => $payDetail['pay_amount'],
  193. 'classify' => $order->order_classify,
  194. 'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
  195. ];
  196. }
  197. $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
  198. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  199. 'goods_name' => $sheet['goods']['goods_name'] ?? '',
  200. 'order_sheet_num' => $sheet['order_sheet_amount'],
  201. 'order_sheet_pay' => $sheet['order_sheet_pay']
  202. ];
  203. $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
  204. }
  205. $inoutObjectJson['ordergroup'] = $order->order_groupby;
  206. $data['inout_object_json'] = json_encode($inoutObjectJson);
  207. $data['inout_name'] = implode(',', $data['inout_name']);
  208. $payJson = [
  209. 'trade' => '',
  210. 'if_union' => 'N',
  211. 'union_order_id' => ''
  212. ];
  213. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  214. $data['inout_attr'] = 'MONEY';
  215. } else {
  216. $data['inout_attr'] = 'ACCOUNT';
  217. }
  218. $data['inout_amount'] = $payDetail['pay_amount'];
  219. $prepayId = explode('-', $payDetail['pay_prepayid']);
  220. if (count($prepayId) == 1) {
  221. $data['pay_type'] = $payDetail['pay_prepayid'];
  222. } elseif (count($prepayId) > 1) {
  223. $data['pay_type'] = $prepayId[1];
  224. }
  225. if (!empty($payDetail['pay_json_response'])) {
  226. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  227. if (isset($payJsonResponse['transaction_id'])) {
  228. $payJson['trade'] = $payJsonResponse['transaction_id'];
  229. } elseif (isset($payJsonResponse['trade_no'])) {
  230. $payJson['trade'] = $payJsonResponse['trade_no'];
  231. }
  232. }
  233. $data['pay_json'] = json_encode($payJson);
  234. return $data;
  235. } catch (BusinessException $e) {
  236. Log::error("记录退款收支失败", ['msg' => $e->getMessage(), 'params' => $params]);
  237. throw new BusinessException($e->getMessage());
  238. } catch (\Exception $e) {
  239. Log::error("记录退款收支失败", ['msg' => $e->getMessage(), 'params' => $params]);
  240. throw new BusinessException("退款支出处理失败");
  241. }
  242. }
  243. public function commissionWithdraw($params)
  244. {
  245. try {
  246. $memberAccountList = MemberAccountList::where('member_account_list_id', $params['account_list_id'])->first();
  247. $data = [
  248. 'dept_id' => 0,
  249. 'member_id' => $memberAccountList->join_member_account_list_member_id,
  250. 'inout_object_json' => $memberAccountList->member_account_list_json,
  251. 'inout_attr' => 'MONEY',
  252. 'inout_classify' => 'OUT',
  253. 'inout_category' => '提现',
  254. 'inout_name' => '提现',
  255. 'inout_amount' => $memberAccountList->member_account_list_amount,
  256. 'pay_type' => 'UNIONPAY',
  257. 'pay_json' => json_encode([
  258. 'trade' => '',
  259. 'if_union' => 'N',
  260. 'union_order_id' => ''
  261. ]),
  262. ];
  263. return $data;
  264. } catch (\Exception $e) {
  265. throw new BusinessException("提现支出处理失败");
  266. }
  267. }
  268. public function dataSave($data)
  269. {
  270. DataInout::insert([
  271. 'join_data_inout_dept_id' => $data['dept_id'],
  272. 'join_data_inout_member_id' => $data['member_id'],
  273. 'join_data_inout_object_json' => $data['inout_object_json'],
  274. 'data_inout_is_valid' => 'Y',
  275. 'data_inout_valid_date' => date('Y-m-d'),
  276. 'data_inout_valid_time' => date('Y-m-d H:i:s'),
  277. 'data_inout_status' => 'VALID',
  278. 'data_inout_attr' => $data['inout_attr'],
  279. 'data_inout_classify' => $data['inout_classify'],
  280. 'data_inout_category' => $data['inout_category'],
  281. 'data_inout_name' => $data['inout_name'],
  282. 'data_inout_amount' => $data['inout_amount'],
  283. 'data_inout_count' => 0,
  284. 'data_inout_json' => '[]',
  285. 'data_inout_pay_type' => $data['pay_type'] ?? '',
  286. 'data_inout_pay_json' => $data['pay_json'],
  287. 'data_inout_process_json' => '[]',
  288. 'data_inout_occurtime' => date('Y-m-d H:i:s'),
  289. 'data_inout_remark' => '',
  290. 'data_inout_extend_json' => '[]',
  291. 'data_inout_addtimes' => time()
  292. ]);
  293. }
  294. public static $orderClassify = [
  295. 'GOODS' => '标准订单',
  296. 'SERVICE' => '标准订单',
  297. 'CHNMED' => '标准订单',
  298. 'CHNNCD' => '标准订单',
  299. 'PACKAGE' => '套包订单',
  300. 'RECHARGE' => '充值订单',
  301. 'DISHES' => '餐饮订单',
  302. 'VIP' => '康养城订单',
  303. 'PARTNER' => '会员合伙人订单',
  304. 'REFERRER' => '康养推荐官订单',
  305. 'COMBINE' => '新客专享订单'
  306. ];
  307. }