CaiWuXiTong/app/Support/CourierBills/PortalFreightBills.php

106 lines
4.4 KiB
PHP

<?php
namespace App\Support\CourierBills;
use App\Support\DropshipFreight\DropshipFreightStore;
use Throwable;
/**
* 厂家门户里的「运费账单」:代发厂家上传代发运费账单、快递上传快递账单,都按统一模板。
* 代发厂家按厂家名对上代发厂家设置;快递按承运商名对上快递账单对账分组(分组名或小汇总承运商)。
*/
final class PortalFreightBills
{
public function __construct(
private readonly CourierBillStore $courier,
private readonly DropshipFreightStore $dropship,
) {}
public function factory(string $name): ?object
{
try {
return collect($this->dropship->factories())->first(static fn (object $f): bool => trim((string) $f->name) === trim($name));
} catch (Throwable) {
return null;
}
}
/** @return list<object> 这个承运商名对应的快递对账分组 */
public function groups(string $name): array
{
$name = trim($name);
try {
return array_values(array_filter($this->courier->groups(), static fn (object $g): bool => $name !== ''
&& (trim((string) $g->name) === $name || trim((string) ($g->summary_carrier ?? '')) === $name)));
} catch (Throwable) {
return [];
}
}
/** 财务代传入口包含尚未上传货款对账单的代发厂家与快递。 */
public function names(): array
{
try {
return array_values(array_unique([
...array_map(static fn ($f) => (string) $f->name, $this->dropship->factories()),
...array_map(static fn ($g) => (string) $g->name, $this->courier->groups()),
]));
} catch (Throwable) {
return [];
}
}
/**
* 钉钉组织里能认作「快递」的部门名:快递对账分组名和小汇总承运商名。
*
* @param list<string> $names
* @return list<string>
*/
public static function courierNames(array $names): array
{
$names = array_values(array_unique(array_filter(array_map('trim', $names))));
if ($names === []) {
return [];
}
try {
$db = CourierBillMatcher::db();
$found = $db->table('courier_bill_groups')->whereIn('name', $names)->pluck('name')
->merge($db->table('courier_bill_groups')->whereIn('summary_carrier', $names)->pluck('summary_carrier'))
->map(static fn ($n): string => trim((string) $n))->unique()->values()->all();
} catch (Throwable) {
return [];
}
return array_values(array_intersect($names, $found));
}
/** @return array{factory:?object,groups:list<object>,files:list<array<string,mixed>>} */
public function panel(string $name): array
{
$factory = $this->factory($name);
$groups = $this->groups($name);
$files = app(PortalFreightBillImports::class)->recent((int) company_id(), $name);
try {
if ($factory !== null) {
foreach ($this->dropship->db()->table('dropship_bill_files')->where('factory_id', $factory->id)->orderByDesc('id')->limit(10)
->get() as $f) {
$files[] = ['kind' => '代发运费', 'month' => $f->bill_month, 'name' => $f->original_name, 'count' => (int) $f->waybill_count,
'amount' => (float) $f->amount, 'status' => $f->status, 'message' => $f->message, 'at' => (string) $f->created_at];
}
}
if ($groups !== []) {
$ids = array_map(static fn (object $g): int => (int) $g->id, $groups);
foreach ($this->courier->db()->table('courier_bill_files')->whereIn('group_id', $ids)->orderByDesc('id')->limit(10)->get() as $f) {
$files[] = ['kind' => '快递账单', 'month' => $f->bill_month, 'name' => $f->original_name, 'count' => (int) $f->row_count,
'amount' => (float) $f->amount, 'status' => $f->status, 'message' => $f->message, 'at' => (string) $f->created_at];
}
}
} catch (Throwable) {
// 表没建好时只是不显示上传记录
}
usort($files, static fn (array $a, array $b): int => strcmp($b['at'], $a['at']));
return ['factory' => $factory, 'groups' => $groups, 'files' => array_slice($files, 0, 10)];
}
}