123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2021/1/26
- * Time: 15:16
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class McnStatementsDetail extends Model
- {
- public $timestamps = false;
- protected $table = 'mcn_statements_detail';
- protected static $unguarded = true;
- public static function calculateSubtotal($orderList)
- {
- $monthData = [];
- $total = [];
- foreach($orderList as $order) {
- $order['final_amount'] = isset($order['final_amount']) ? $order['final_amount'] : 0;
- $order['return_point_amount'] = isset($order['return_point_amount']) ? $order['return_point_amount'] : 0;
- $order['percentage_of_service_charge'] = isset($order['percentage_of_service_charge']) ? $order['percentage_of_service_charge'] :
- ($order['final_amount'] > 0 ? round($order['return_point_amount'] / $order['final_amount'], 2) : 0);
- $total['final_amount'] = isset($total['final_amount']) ? $total['final_amount'] + $order['final_amount'] : $order['final_amount'];
- $total['return_point_amount'] = isset($total['return_point_amount']) ? $total['return_point_amount'] + $order['return_point_amount'] : $order['return_point_amount'];
- // 统计每月小计
- $key = $order['sort_month'];
- // $key = $order['project_name_second'].'@'.$order['month'];
- if(isset($monthData[$key])) {
- if ($monthData[$key]['sort_month_format'] == $order['sort_month']) {
- $monthData[$key]['final_amount'] += $order['final_amount'];
- $monthData[$key]['return_point_amount'] += $order['return_point_amount'];
- }
- } else {
- $monthData[$key]['final_amount'] = $order['final_amount'];
- $monthData[$key]['return_point_amount'] = $order['return_point_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];
- }
- }
|