CaiWuXiTong/app/Support/CourierBills/SettlementComparison.php

40 lines
1.9 KiB
PHP

<?php
namespace App\Support\CourierBills;
/** 对账展示和导出统一使用最终结算口径,保留原核算字段。 */
final class SettlementComparison
{
public static function difference(mixed $billed, mixed $settled, int $precision = 2): ?float
{
return $billed === null || $settled === null ? null : round((float) $billed - (float) $settled, $precision);
}
public static function weight(mixed $billFreight, mixed $calcFreight, mixed $billWeight, mixed $chargeWeight, mixed $settled): ?float
{
if ($settled === null) return null;
if ($billFreight !== null && $calcFreight !== null && (float) $billFreight < (float) $calcFreight
&& round((float) $settled, 2) === round((float) $billFreight, 2)) {
return $billWeight === null ? null : (float) $billWeight;
}
return $chargeWeight === null ? null : (float) $chargeWeight;
}
/** @return array{settlement_weight:?float,weight_diff:?float,freight_diff:?float} */
public static function courier(object $line): array
{
$weight = match ($line->resolution) {
'agree' => $line->bill_weight === null ? null : (float) $line->bill_weight,
'exclude', 'defer' => 0.0,
default => in_array($line->category, [...Category::PAYABLE, Category::PRIOR], true)
? (in_array(trim((string) ($line->express_name ?? '')), CourierBillMatcher::ACTUAL_FREIGHT_EXPRESSES, true)
? ($line->self_amount === null || $line->charge_weight === null ? null : (float) $line->charge_weight)
: self::weight($line->bill_amount, $line->calc_freight, $line->bill_weight, $line->charge_weight, $line->self_amount)) : null,
};
return ['settlement_weight' => $weight, 'weight_diff' => self::difference($line->bill_weight, $weight, 4),
'freight_diff' => self::difference($line->bill_amount, $line->self_amount)];
}
}