新版订单消耗系统

PddStatementsDetail.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/22
  6. * Time: 10:57
  7. */
  8. namespace App\Models;
  9. use Illuminate\Database\Eloquent\Model;
  10. class PddStatementsDetail extends Model
  11. {
  12. protected $table = 'pdd_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['first_invoiced_amount'] = isset($order['first_invoiced_amount']) ? $order['first_invoiced_amount'] : 0;
  21. $order['final_invoiced_amount'] = isset($order['final_invoiced_amount']) ? $order['final_invoiced_amount'] : 0;
  22. $order['total_amount'] = isset($order['total_amount']) ? $order['total_amount'] : 0;
  23. $total['actual_consumption'] = isset($total['actual_consumption']) ? $total['actual_consumption'] + $order['actual_consumption'] : $order['actual_consumption'];
  24. $total['first_invoiced_amount'] = isset($total['first_invoiced_amount']) ? $total['first_invoiced_amount'] + $order['first_invoiced_amount'] : $order['first_invoiced_amount'];
  25. $total['final_invoiced_amount'] = isset($total['final_invoiced_amount']) ? $total['final_invoiced_amount'] + $order['final_invoiced_amount'] : $order['final_invoiced_amount'];
  26. $total['total_amount'] = isset($total['total_amount']) ? $total['total_amount'] + $order['total_amount'] : $order['total_amount'];
  27. $key = $order['sort_month'].'@'.$order['proejct_name'];
  28. if (isset($monthData[$key])) {
  29. if($monthData[$key]['sort_month_format'] == $order['sort_month']) {
  30. $monthData[$key]['actual_consumption'] += $order['actual_consumption'];
  31. $monthData[$key]['first_invoiced_amount'] += $order['first_invoiced_amount'];
  32. $monthData[$key]['final_invoiced_amount'] += $order['final_invoiced_amount'];
  33. $monthData[$key]['total_amount'] += $order['total_amount'];
  34. }
  35. } else {
  36. $monthData[$key]['actual_consumption'] = $order['actual_consumption'];
  37. $monthData[$key]['first_invoiced_amount'] = $order['first_invoiced_amount'];
  38. $monthData[$key]['final_invoiced_amount'] = $order['final_invoiced_amount'];
  39. $monthData[$key]['total_amount'] = $order['total_amount'];
  40. $monthData[$key]['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  41. $monthData[$key]['sort_month_format'] = $order['sort_month'];
  42. }
  43. // 将当月数据写入
  44. $order['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
  45. $order['month'] = date('Y年m月', strtotime($order['month']));
  46. $order['order_original_month'] = !empty($order['order_original_month']) ? date('Y年m月', strtotime($order['order_original_month'])) : '';
  47. $monthData[$key]['row'][] = $order;
  48. }
  49. ksort($monthData);
  50. return ['list' => array_values($monthData), 'total' => $total];
  51. }
  52. }