CaiWuXiTong/app/Support/CourierBills/WeightDifferenceSubsidy.php

30 lines
1.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\CourierBills;
use Illuminate\Database\Query\Builder;
/** 武进韵达重量多报超过1kg的运费差额,汇总后补给快递50%。 */
final class WeightDifferenceSubsidy
{
public const LABEL = '补武进韵达重量差运费(50%)';
public function query(int $statementId): Builder
{
return CourierBillMatcher::db()->table('courier_bill_lines')
->where('statement_id', $statementId)->where('express_name', '武进韵达')
->whereIn('category', [...Category::PAYABLE, Category::PRIOR])->whereNull('resolution')
->whereRaw('bill_weight - charge_weight > 1')
->whereColumn('bill_amount', '>', 'calc_freight');
}
/** 按差额合计计算一半,再四舍五入到分,避免逐行四舍五入累积误差。 */
public function summary(int $statementId): array
{
$sum = $this->query($statementId)->selectRaw('COUNT(*) AS n, SUM(bill_amount - calc_freight) AS difference')->first();
return ['count' => (int) $sum->n, 'difference' => round((float) $sum->difference, 4),
'amount' => round((float) $sum->difference / 2, 2)];
}
}