InOutEvent.php 15 KB

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