InOutEvent.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. $inoutObjectJson['ordergroup'] = $order->order_groupby;
  79. $data['inout_object_json'] = json_encode($inoutObjectJson);
  80. $data['inout_name'] = implode(',', $data['inout_name']);
  81. $payJson = [
  82. 'trade'=>'',
  83. 'if_union' => count($payDetails) > 1 ? 'Y' : 'N',
  84. 'union_order_id' => count($payDetails) > 1 ? $params['orderId'] : ''
  85. ];
  86. foreach ($payDetails as $payDetail) {
  87. if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
  88. $data['inout_attr'] = 'MONEY';
  89. } else {
  90. $data['inout_attr'] = 'ACCOUNT';
  91. }
  92. $data['inout_amount'] = $payDetail['pay_amount'];
  93. $prepayId = explode('-', $payDetail['pay_prepayid']);
  94. if (count($prepayId) == 1) {
  95. $data['pay_type'] = $payDetail['pay_prepayid'];
  96. } elseif (count($prepayId) > 1) {
  97. $data['pay_type'] = $prepayId[1];
  98. }
  99. if (!empty($payDetail['pay_json_response'])) {
  100. $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
  101. if (isset($payJsonResponse['transaction_id'])){
  102. $payJson['trade'] = $payJsonResponse['transaction_id'];
  103. }elseif (isset($payJsonResponse['trade_no'])){
  104. $payJson['trade'] = $payJsonResponse['trade_no'];
  105. }
  106. }
  107. $data['pay_json'] = json_encode($payJson);
  108. $this->dataSave($data);
  109. }
  110. } catch (\Exception $e) {
  111. _syslog("统计", '收支明细统计失败', ['msg' => $e->getMessage()]);
  112. Log::error("收支明细统计失败", ['msg' => $e->getMessage()]);
  113. }
  114. }
  115. public function dataOut()
  116. {
  117. }
  118. public function dataSave($data)
  119. {
  120. DataInout::insert([
  121. 'join_data_inout_dept_id' => $data['dept_id'],
  122. 'join_data_inout_member_id' => $data['member_id'],
  123. 'join_data_inout_object_json' => $data['inout_object_json'],
  124. 'data_inout_is_valid' => 'Y',
  125. 'data_inout_valid_date' => date('Y-m-d'),
  126. 'data_inout_valid_time' => date('Y-m-d H:i:s'),
  127. 'data_inout_status' => 'VALID',
  128. 'data_inout_attr' => $data['inout_attr'],
  129. 'data_inout_classify' => $data['inout_classify'],
  130. 'data_inout_category' => $data['inout_category'],
  131. 'data_inout_name' => $data['inout_name'],
  132. 'data_inout_amount' => $data['inout_amount'],
  133. 'data_inout_count' => 0,
  134. 'data_inout_json' => '[]',
  135. 'data_inout_pay_type' => $data['pay_type'] ?? '',
  136. 'data_inout_pay_json' => $data['pay_json'],
  137. 'data_inout_process_json' => '[]',
  138. 'data_inout_occurtime' => date('Y-m-d H:i:s'),
  139. 'data_inout_remark' => '',
  140. 'data_inout_extend_json' => '[]',
  141. 'data_inout_addtimes' => time()
  142. ]);
  143. }
  144. }