新版订单消耗系统

GameStatementsDetail.php 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/15
  6. * Time: 13:57
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class GameStatementsDetail extends Model
  11. {
  12. protected $table = 'game_statements_detail';
  13. public $timestamps = false;
  14. public static function calculateSubtotal($orderList)
  15. {
  16. $monthData = [];
  17. $total = [];
  18. foreach($orderList as $order) {
  19. $order['actual_consumption'] = isset($order['actual_consumption']) ? $order['actual_consumption'] : 0;
  20. $order['discount_amount'] = isset($order['discount_amount']) ? $order['discount_amount'] : 0;
  21. $order['link_fee'] = isset($order['link_fee']) ? $order['link_fee'] : 0;
  22. $order['top_fee'] = isset($order['top_fee']) ? $order['top_fee'] : 0;
  23. $order['flow_boost_back_point'] = isset($order['flow_boost_back_point']) ? $order['flow_boost_back_point'] : 0;
  24. $order['total_amount'] = isset($order['total_amount']) ? $order['total_amount'] : 0;
  25. $order['discount_rate'] = isset($order['discount_rate']) ? $order['discount_rate'] :
  26. ($order['actual_consumption'] > 0 ? round($order['discount_amount'] / $order['actual_consumption'], 4) : 0);
  27. $total['actual_consumption'] = isset($total['actual_consumption']) ? $total['actual_consumption'] + $order['actual_consumption'] : $order['actual_consumption'];
  28. $total['discount_amount'] = isset($total['discount_amount']) ? $total['discount_amount'] + $order['discount_amount'] : $order['discount_amount'];
  29. $total['link_fee'] = isset($total['link_fee']) ? $total['link_fee'] + $order['link_fee'] : $order['link_fee'];
  30. $total['top_fee'] = isset($total['top_fee']) ? $total['top_fee'] + $order['top_fee'] : $order['top_fee'];
  31. $total['flow_boost_back_point'] = isset($total['flow_boost_back_point']) ? $total['flow_boost_back_point'] + $order['flow_boost_back_point'] : $order['flow_boost_back_point'];
  32. $total['total_amount'] = isset($total['total_amount']) ? $total['total_amount'] + $order['total_amount'] : $order['total_amount'];
  33. $key = $order['sort_month'].'@'.$order['proejct_name'];
  34. if (isset($monthData[$key])) {
  35. if($monthData[$key]['sort_month_format'] == $order['sort_month']) {
  36. $monthData[$key]['actual_consumption'] += $order['actual_consumption'];
  37. $monthData[$key]['discount_amount'] += $order['discount_amount'];
  38. $monthData[$key]['link_fee'] += $order['link_fee'];
  39. $monthData[$key]['top_fee'] += $order['top_fee'];
  40. $monthData[$key]['flow_boost_back_point'] += $order['flow_boost_back_point'];
  41. $monthData[$key]['total_amount'] += $order['total_amount'];
  42. }
  43. } else {
  44. $monthData[$key]['actual_consumption'] = $order['actual_consumption'];
  45. $monthData[$key]['discount_amount'] = $order['discount_amount'];
  46. $monthData[$key]['link_fee'] = $order['link_fee'];
  47. $monthData[$key]['top_fee'] = $order['top_fee'];
  48. $monthData[$key]['flow_boost_back_point'] = $order['flow_boost_back_point'];
  49. $monthData[$key]['total_amount'] = $order['total_amount'];
  50. $monthData[$key]['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  51. $monthData[$key]['sort_month_format'] = $order['sort_month'];
  52. }
  53. // 将当月数据写入
  54. $order['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  55. $order['month'] = date('Y年m月', strtotime($order['month']));
  56. $order['order_original_month'] = !empty($order['order_original_month']) ? date('Y年m月', strtotime($order['order_original_month'])) : '';
  57. $monthData[$key]['row'][] = $order;
  58. }
  59. ksort($monthData);
  60. return ['list' => array_values($monthData), 'total' => $total];
  61. }
  62. }