12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2021/4/15
- * Time: 13:56
- */
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class ECommerceStatementsDetail extends Model
- {
- protected $table = 'e_commerce_statements_detail';
- public $timestamps = false;
- public static function calculateSubtotal($orderList)
- {
- $monthData = [];
- $total = [];
- foreach($orderList as $order) {
- $order['url'] = isset($order['url']) ? $order['url'] : null;
- $order['actual_consumption'] = isset($order['actual_consumption']) ? $order['actual_consumption'] : 0;
- $order['discount_amount'] = isset($order['discount_amount']) ? $order['discount_amount'] : 0;
- $order['discount_rate'] = isset($order['discount_rate']) ? $order['discount_rate'] :
- ($order['actual_consumption'] > 0 ? round($order['discount_amount'] / $order['actual_consumption'], 2) : 0);
- $total['actual_consumption'] = isset($total['actual_consumption']) ? $total['actual_consumption'] + $order['actual_consumption'] : 0;
- $total['discount_amount'] = isset($total['discount_amount']) ? $total['discount_amount'] + $order['discount_amount'] : 0;
- $key = $order['proejct_name'].'@'.$order['month'];
- if (isset($monthData[$key])) {
- if ($monthData[$key]['month'] == date('m月', strtotime($order['month']))) {
- $monthData[$key]['actual_consumption'] += $order['actual_consumption'];
- $monthData[$key]['discount_amount'] += $order['discount_amount'];
- }
- } else {
- $monthData[$key]['actual_consumption'] = $order['actual_consumption'];
- $monthData[$key]['discount_amount'] = $order['discount_amount'];
- $monthData[$key]['month'] = date('m月', strtotime($order['month']));
- }
- $order['month'] = date('Y年m月', strtotime($order['month']));
- $monthData[$key]['row'][] = $order;
- }
- return ['list' => array_values($monthData), 'total' => $total];
- }
- }
|