InOutEvent.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\event\statistics;
  3. use app\model\DataInout;
  4. use app\model\Order;
  5. use app\model\OrderSheet;
  6. use app\model\PayDetail;
  7. use app\model\SysDept;
  8. use support\Log;
  9. class InOutEvent
  10. {
  11. /*
  12. * {"label": "标准订单收入", "value": "标准订单收入"},
  13. {"label": "餐饮订单收入", "value": "餐饮订单收入"},
  14. {"label": "购买康养城VIP套餐包收入", "value": "购买康养城VIP套餐包收入"},
  15. {"label": "购买会员合伙人收入", "value": "购买会员合伙人收入"},
  16. {"label": "购买康养推荐官收入", "value": "购买康养推荐官收入"},
  17. {"label": "购买组合包收入", "value": "购买组合包收入"},
  18. {"label": "会员充值订单收入", "value": "会员充值订单收入"},
  19. {"label": "挂账订单结算收入", "value": "挂账订单结算收入"},
  20. {"label": "购买储值卡订单收入", "value": "购买储值卡订单收入"},
  21. {"label": "佣金提现", "value": "佣金提现"},
  22. {"label": "退款", "value": "退款"}
  23. */
  24. public function dataIn($params)
  25. {
  26. try {
  27. $order = Order::where('order_id', $params['orderId'])->first();
  28. $sheets = OrderSheet::with([
  29. 'goods' => function ($query) {
  30. $query->select('goods_id', 'goods_name', 'goods_classify');
  31. }
  32. ])->where('join_sheet_order_id', $params['orderId'])
  33. ->get()
  34. ->toArray();
  35. $payDetails = PayDetail::where('join_pay_order_id', $order->order_groupby)
  36. ->whereJsonContains('join_pay_object_json->order_id',$params['orderId'])
  37. ->where('pay_status', 'SUCCESS')
  38. ->get()
  39. ->toArray();
  40. $data = [
  41. 'dept_id' => 0,
  42. 'member_id' => $order->join_order_member_id,
  43. 'inout_classify' => 'IN',
  44. 'inout_category' => $params['inout_category'] ?? '标准订单收入',
  45. ];
  46. // 部门id
  47. if (!empty($order->order_config_json)) {
  48. $orderConfigJson = json_decode($order->order_config_json, true);
  49. if (!empty($orderConfigJson['dept'])) {
  50. $data['dept_id'] = $orderConfigJson['dept'];
  51. } elseif (!empty($orderConfigJson['premises'])) {
  52. $data['dept_id'] = SysDept::where('dept_name', $orderConfigJson['premises'])->value('dept_id');
  53. }
  54. }
  55. $inoutObjectJson = [];
  56. // 产品
  57. foreach ($sheets as $sheet) {
  58. $inoutObjectJson['goods'][] = [
  59. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  60. 'goods_name' => $sheet['goods']['goods_name'] ?? ''
  61. ];
  62. $data['inout_name'][] = $sheet['goods']['goods_name'] ?? '';
  63. if (!isset($inoutObjectJson['order'])) {
  64. $inoutObjectJson['order'][$params['orderId']] = [
  65. 'pay' => $order->order_amount_pay,
  66. 'classify' => $order->order_classify,
  67. 'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
  68. ];
  69. }
  70. $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
  71. 'goods_id' => $sheet['goods']['goods_id'] ?? '',
  72. 'goods_name' => $sheet['goods']['goods_name'] ?? '',
  73. 'order_sheet_num' => $sheet['order_sheet_amount'],
  74. 'order_sheet_pay' => $sheet['order_sheet_pay']
  75. ];
  76. $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
  77. }
  78. $data['inout_object_json'] = json_encode($inoutObjectJson);
  79. $data['inout_name'] = implode(',', $data['inout_name']);
  80. $payJson = [
  81. 'trade'=>'',
  82. 'if_union' => count($payDetails) > 1 ? 'Y' : 'N',
  83. 'union_order_id' => count($payDetails) > 1 ? $params['orderId'] : ''
  84. ];
  85. foreach ($payDetails as $payDetail) {
  86. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  87. $data['inout_attr'] = 'MONEY';
  88. } else {
  89. $data['inout_attr'] = 'ACCOUNT';
  90. }
  91. $data['inout_amount'] = $payDetail['pay_amount'];
  92. $prepayId = explode('-', $payDetail['pay_prepayid']);
  93. if (count($prepayId) == 1) {
  94. $data['pay_type'] = $payDetail['pay_prepayid'];
  95. } elseif (count($prepayId) > 1) {
  96. $data['pay_type'] = $prepayId[1];
  97. }
  98. if (!empty($payDetail['pay_json_response'])) {
  99. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  100. if (isset($payJsonResponse['transaction_id'])){
  101. $payJson['trade'] = $payJsonResponse['transaction_id'];
  102. }elseif (isset($payJsonResponse['trade_no'])){
  103. $payJson['trade'] = $payJsonResponse['trade_no'];
  104. }
  105. }
  106. $data['pay_json'] = json_encode($payJson);
  107. $this->dataSave($data);
  108. }
  109. } catch (\Exception $e) {
  110. _syslog("统计", '收支明细统计失败', ['msg' => $e->getMessage()]);
  111. Log::error("收支明细统计失败", ['msg' => $e->getMessage()]);
  112. }
  113. }
  114. public function dataOut()
  115. {
  116. }
  117. public function dataSave($data)
  118. {
  119. DataInout::insert([
  120. 'join_data_inout_dept_id' => $data['dept_id'],
  121. 'join_data_inout_member_id' => $data['member_id'],
  122. 'join_data_inout_object_json' => $data['inout_object_json'],
  123. 'data_inout_is_valid' => 'Y',
  124. 'data_inout_valid_date' => date('Y-m-d'),
  125. 'data_inout_valid_time' => date('Y-m-d H:i:s'),
  126. 'data_inout_status' => 'VALID',
  127. 'data_inout_attr' => $data['inout_attr'],
  128. 'data_inout_classify' => $data['inout_classify'],
  129. 'data_inout_category' => $data['inout_category'],
  130. 'data_inout_name' => $data['inout_name'],
  131. 'data_inout_amount' => $data['inout_amount'],
  132. 'data_inout_count' => 0,
  133. 'data_inout_json' => '[]',
  134. 'data_inout_pay_type' => $data['pay_type'] ?? '',
  135. 'data_inout_pay_json' => $data['pay_json'],
  136. 'data_inout_process_json' => '[]',
  137. 'data_inout_occurtime' => date('Y-m-d H:i:s'),
  138. 'data_inout_remark' => '',
  139. 'data_inout_extend_json' => '[]',
  140. 'data_inout_addtimes' => time()
  141. ]);
  142. }
  143. }