CaiWuXiTong/app/Support/CourierBills/AccessoryDeductions.php

66 lines
2.0 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 Modules\EcommerceFinance\View\AccessoryReshipPageData;
use Throwable;
/**
* 「配件补发扣款」页算好的扣款,按首发快递(快递名称1)汇总,给对账单小汇总用。
* 只取页面上「可扣款」的行(已选定首发快递、配了扣款比例、金额大于 0),和页面合计口径一致。
*/
class AccessoryDeductions
{
/** @var array<string,array<string,float>|null> 同一次请求里按月份缓存 */
private array $cache = [];
/** @return array<string,float>|null 快递名称1 => 扣款金额(元);读不到时为 null */
public function byCourier(string $month): ?array
{
if (! array_key_exists($month, $this->cache)) {
$this->cache[$month] = $this->load($month);
}
return $this->cache[$month];
}
/** @param list<string> $expressNames */
public function total(string $month, array $expressNames): ?float
{
$map = $this->byCourier($month);
if ($map === null) {
return null;
}
$sum = 0.0;
foreach (array_unique($expressNames) as $name) {
$sum += $map[$name] ?? 0.0;
}
return round($sum, 2);
}
/** @return array<string,float>|null */
protected function load(string $month): ?array
{
try {
$result = [];
$page = app(AccessoryReshipPageData::class);
foreach ($page->rows(['month' => $month, 'match_status' => 'ready']) as $row) {
if (! AccessoryReshipPageData::inCategory($row, 'ready')) {
continue;
}
$courier = trim((string) ($row['selected_courier'] ?? ($row['matches'][0]['courier'] ?? '')));
if ($courier !== '') {
$result[$courier] = ($result[$courier] ?? 0) + ((int) $row['deduction_cents']) / 100;
}
}
return $result;
} catch (Throwable $error) {
report($error);
return null;
}
}
}