30 lines
1.2 KiB
PHP
30 lines
1.2 KiB
PHP
<?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)];
|
||
}
|
||
}
|