123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2021/4/22
- * Time: 10:57
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class PddStatementsDetail extends Model
- {
- protected $table = 'pdd_statements_detail';
- public $timestamps = false;
- public static function calculateSubtotal($orderList)
- {
- $monthData = [];
- $total = [];
- foreach($orderList as $order) {
- $order['actual_consumption'] = isset($order['actual_consumption']) ? $order['actual_consumption'] : 0;
- $order['first_invoiced_amount'] = isset($order['first_invoiced_amount']) ? $order['first_invoiced_amount'] : 0;
- $order['final_invoiced_amount'] = isset($order['final_invoiced_amount']) ? $order['final_invoiced_amount'] : 0;
- $order['total_amount'] = isset($order['total_amount']) ? $order['total_amount'] : 0;
- $total['actual_consumption'] = isset($total['actual_consumption']) ? $total['actual_consumption'] + $order['actual_consumption'] : $order['actual_consumption'];
- $total['first_invoiced_amount'] = isset($total['first_invoiced_amount']) ? $total['first_invoiced_amount'] + $order['first_invoiced_amount'] : $order['first_invoiced_amount'];
- $total['final_invoiced_amount'] = isset($total['final_invoiced_amount']) ? $total['final_invoiced_amount'] + $order['final_invoiced_amount'] : $order['final_invoiced_amount'];
- $total['total_amount'] = isset($total['total_amount']) ? $total['total_amount'] + $order['total_amount'] : $order['total_amount'];
- $key = $order['sort_month'].'@'.$order['proejct_name'];
- if (isset($monthData[$key])) {
- if($monthData[$key]['sort_month_format'] == $order['sort_month']) {
- $monthData[$key]['actual_consumption'] += $order['actual_consumption'];
- $monthData[$key]['first_invoiced_amount'] += $order['first_invoiced_amount'];
- $monthData[$key]['final_invoiced_amount'] += $order['final_invoiced_amount'];
- $monthData[$key]['total_amount'] += $order['total_amount'];
- }
- } else {
- $monthData[$key]['actual_consumption'] = $order['actual_consumption'];
- $monthData[$key]['first_invoiced_amount'] = $order['first_invoiced_amount'];
- $monthData[$key]['final_invoiced_amount'] = $order['final_invoiced_amount'];
- $monthData[$key]['total_amount'] = $order['total_amount'];
- $monthData[$key]['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
- $monthData[$key]['sort_month_format'] = $order['sort_month'];
- }
- // 将当月数据写入
- $order['sort_month'] = date('Y年m月', strtotime($order['sort_month']));
- $order['month'] = date('Y年m月', strtotime($order['month']));
- $order['order_original_month'] = !empty($order['order_original_month']) ? date('Y年m月', strtotime($order['order_original_month'])) : '';
- $monthData[$key]['row'][] = $order;
- }
- ksort($monthData);
- return ['list' => array_values($monthData), 'total' => $total];
- }
- }
|