12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2021/4/15
- * Time: 13:57
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class CPAStatementsDetail extends Model
- {
- protected $table = 'cpa_statements_detail';
- public $timestamps = false;
- public static function calculateSubtotal($orderList)
- {
- $monthData = [];
- $total = [];
- foreach($orderList as $order) {
- $order['cpa_unit_income_price'] = isset($order['cpa_unit_income_price']) ? $order['cpa_unit_income_price'] : 0;
- $order['cpa_single_quantity'] = isset($order['cpa_single_quantity']) ? $order['cpa_single_quantity'] : 0;
- $order['total_amount'] = isset($order['total_amount']) ? $order['total_amount'] : $order['final_amount'];
- $total['cpa_single_quantity'] = isset($total['cpa_single_quantity']) ? $total['cpa_single_quantity'] + $order['cpa_single_quantity'] : $order['cpa_single_quantity'];
- $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]['cpa_single_quantity'] += $order['cpa_single_quantity'];
- $monthData[$key]['total_amount'] += $order['total_amount'];
- }
- } else {
- $monthData[$key]['cpa_unit_income_price'] = $order['cpa_unit_income_price'];
- $monthData[$key]['cpa_single_quantity'] = $order['cpa_single_quantity'];
- $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];
- }
- }
|