InOutEvent.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. 'pay' => $order->order_amount_pay,
  76. 'classify' => $order->order_classify,
  77. 'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
  78. ];
  79. }
  80. $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
  81. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  82. 'goods_name' => $sheet['goods']['goods_name'] ?? '',
  83. 'order_sheet_num' => $sheet['order_sheet_amount'],
  84. 'order_sheet_pay' => $sheet['order_sheet_pay']
  85. ];
  86. $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
  87. }
  88. $inoutObjectJson['ordergroup'] = $order->order_groupby;
  89. $data['inout_object_json'] = json_encode($inoutObjectJson);
  90. $data['inout_name'] = implode(',', $data['inout_name']);
  91. $payJson = [
  92. 'trade' => '',
  93. 'if_union' => count($payDetails) > 1 ? 'Y' : 'N',
  94. 'union_order_id' => count($payDetails) > 1 ? $params['orderId'] : ''
  95. ];
  96. foreach ($payDetails as $payDetail) {
  97. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  98. $data['inout_attr'] = 'MONEY';
  99. } else {
  100. $data['inout_attr'] = 'ACCOUNT';
  101. }
  102. $data['inout_amount'] = $payDetail['pay_amount'];
  103. $prepayId = explode('-', $payDetail['pay_prepayid']);
  104. if (count($prepayId) == 1) {
  105. $data['pay_type'] = $payDetail['pay_prepayid'];
  106. } elseif (count($prepayId) > 1) {
  107. $data['pay_type'] = $prepayId[1];
  108. }
  109. if (!empty($payDetail['pay_json_response'])) {
  110. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  111. if (isset($payJsonResponse['transaction_id'])) {
  112. $payJson['trade'] = $payJsonResponse['transaction_id'];
  113. } elseif (isset($payJsonResponse['trade_no'])) {
  114. $payJson['trade'] = $payJsonResponse['trade_no'];
  115. }
  116. }
  117. $data['pay_json'] = json_encode($payJson);
  118. $this->dataSave($data);
  119. }
  120. } catch (\Exception $e) {
  121. _syslog("统计", '收支明细统计失败', ['msg' => $e->getMessage()]);
  122. Log::error("收支明细统计失败", ['msg' => $e->getMessage()]);
  123. }
  124. }
  125. public function dataOut($params)
  126. {
  127. try {
  128. if ($params['type'] == 'refund') {
  129. $data = $this->orderRefund($params);
  130. } elseif ($params['type'] == 'withdraw') {
  131. $data = $this->commissionWithdraw($params);
  132. } else {
  133. throw new BusinessException("不支持的支出类型");
  134. }
  135. $this->dataSave($data);
  136. } catch (BusinessException $e) {
  137. Log::error("统计支出失败:" . $e->getMessage(), $params);
  138. } catch (\Exception $e) {
  139. Log::error("统计支出失败:" . $e->getMessage(), $params);
  140. }
  141. }
  142. public function orderRefund($params)
  143. {
  144. try {
  145. $order = Order::where('order_id', $params['orderId'])->first();
  146. $sheets = OrderSheet::with([
  147. 'goods' => function ($query) {
  148. $query->select('goods_id', 'goods_name', 'goods_classify');
  149. }
  150. ])->where('join_sheet_order_id', $params['orderId'])
  151. ->get()
  152. ->toArray();
  153. $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)
  154. ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  155. ->where('pay_category', 'REFUND')
  156. ->where('pay_status', 'SUCCESS')
  157. ->first();
  158. if (!$payDetail) {
  159. throw new BusinessException("支付数据异常");
  160. }
  161. $payDetail = $payDetail->toArray();
  162. $data = [
  163. 'dept_id' => 0,
  164. 'member_id' => $order->join_order_member_id,
  165. 'inout_classify' => 'OUT',
  166. 'inout_category' => '退款',
  167. ];
  168. // 部门id
  169. if (!empty($order->order_config_json)) {
  170. $orderConfigJson = json_decode($order->order_config_json, true);
  171. if (!empty($orderConfigJson['dept'])) {
  172. $data['dept_id'] = $orderConfigJson['dept'];
  173. } elseif (!empty($orderConfigJson['premises'])) {
  174. $data['dept_id'] = SysDept::where('dept_name', $orderConfigJson['premises'])->value('dept_id');
  175. }
  176. }
  177. $inoutObjectJson = [];
  178. // 产品
  179. foreach ($sheets as $sheet) {
  180. $inoutObjectJson['goods'][] = [
  181. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  182. 'goods_name' => $sheet['goods']['goods_name'] ?? ''
  183. ];
  184. $data['inout_name'][] = $sheet['goods']['goods_name'] ?? '';
  185. if (!isset($inoutObjectJson['order'])) {
  186. $inoutObjectJson['order'][$params['orderId']] = [
  187. 'pay' => $payDetail['pay_amount'],
  188. 'classify' => $order->order_classify,
  189. 'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
  190. ];
  191. }
  192. $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
  193. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  194. 'goods_name' => $sheet['goods']['goods_name'] ?? '',
  195. 'order_sheet_num' => $sheet['order_sheet_amount'],
  196. 'order_sheet_pay' => $sheet['order_sheet_pay']
  197. ];
  198. $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
  199. }
  200. $inoutObjectJson['ordergroup'] = $order->order_groupby;
  201. $data['inout_object_json'] = json_encode($inoutObjectJson);
  202. $data['inout_name'] = implode(',', $data['inout_name']);
  203. $payJson = [
  204. 'trade' => '',
  205. 'if_union' => 'N',
  206. 'union_order_id' => ''
  207. ];
  208. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  209. $data['inout_attr'] = 'MONEY';
  210. } else {
  211. $data['inout_attr'] = 'ACCOUNT';
  212. }
  213. $data['inout_amount'] = $payDetail['pay_amount'];
  214. $prepayId = explode('-', $payDetail['pay_prepayid']);
  215. if (count($prepayId) == 1) {
  216. $data['pay_type'] = $payDetail['pay_prepayid'];
  217. } elseif (count($prepayId) > 1) {
  218. $data['pay_type'] = $prepayId[1];
  219. }
  220. if (!empty($payDetail['pay_json_response'])) {
  221. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  222. if (isset($payJsonResponse['transaction_id'])) {
  223. $payJson['trade'] = $payJsonResponse['transaction_id'];
  224. } elseif (isset($payJsonResponse['trade_no'])) {
  225. $payJson['trade'] = $payJsonResponse['trade_no'];
  226. }
  227. }
  228. $data['pay_json'] = json_encode($payJson);
  229. return $data;
  230. } catch (BusinessException $e) {
  231. throw new BusinessException($e->getMessage());
  232. } catch (\Exception $e) {
  233. throw new BusinessException("退款支出处理失败");
  234. }
  235. }
  236. public function commissionWithdraw($params)
  237. {
  238. try {
  239. $memberAccountList = MemberAccountList::where('member_account_list_id', $params['account_list_id'])->first();
  240. $data = [
  241. 'dept_id' => 0,
  242. 'member_id' => $memberAccountList->join_member_account_list_member_id,
  243. 'inout_object_json' => $memberAccountList->member_account_list_json,
  244. 'inout_attr' => 'MONEY',
  245. 'inout_classify' => 'OUT',
  246. 'inout_category' => '提现',
  247. 'inout_name' => '提现',
  248. 'inout_amount' => $memberAccountList->member_account_list_amount,
  249. 'pay_type' => 'UNIONPAY',
  250. 'pay_json' => json_encode([
  251. 'trade' => '',
  252. 'if_union' => 'N',
  253. 'union_order_id' => ''
  254. ]),
  255. ];
  256. return $data;
  257. } catch (\Exception $e) {
  258. throw new BusinessException("提现支出处理失败");
  259. }
  260. }
  261. public function dataSave($data)
  262. {
  263. DataInout::insert([
  264. 'join_data_inout_dept_id' => $data['dept_id'],
  265. 'join_data_inout_member_id' => $data['member_id'],
  266. 'join_data_inout_object_json' => $data['inout_object_json'],
  267. 'data_inout_is_valid' => 'Y',
  268. 'data_inout_valid_date' => date('Y-m-d'),
  269. 'data_inout_valid_time' => date('Y-m-d H:i:s'),
  270. 'data_inout_status' => 'VALID',
  271. 'data_inout_attr' => $data['inout_attr'],
  272. 'data_inout_classify' => $data['inout_classify'],
  273. 'data_inout_category' => $data['inout_category'],
  274. 'data_inout_name' => $data['inout_name'],
  275. 'data_inout_amount' => $data['inout_amount'],
  276. 'data_inout_count' => 0,
  277. 'data_inout_json' => '[]',
  278. 'data_inout_pay_type' => $data['pay_type'] ?? '',
  279. 'data_inout_pay_json' => $data['pay_json'],
  280. 'data_inout_process_json' => '[]',
  281. 'data_inout_occurtime' => date('Y-m-d H:i:s'),
  282. 'data_inout_remark' => '',
  283. 'data_inout_extend_json' => '[]',
  284. 'data_inout_addtimes' => time()
  285. ]);
  286. }
  287. }