Claude d3b70c4f4a 代发运费小汇总:不付的分类和厂家附加费各占一行,原始合计 = 厂家账单合计
- 运费行:原始 = 按我方结算那些单厂家开的运费,简尚核对 = 最终结算运费
- 厂家开了运费、这个月不付的按分类各加一行(快递不符、重复、匹配不到、未发出、跨月订单-X月):原始 = 厂家开的,简尚核对 = 0
- 厂家按模板填的「附加费」表:金额合计放「厂家账单-附加费」一行(手工汇总里已有带「附加费」的类目就填那一行),简尚核对空着
- 上月手工汇总里的分类行、附加费行这个月没有就不留空行
- 附加费类型「拦截退回」归拦截费

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UNTa9mVziToe5w52mmrrar
2026-12-03 12:10:17 +08:00

134 lines
6.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\DropshipFreight;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
/**
* 代发各厂家汇总里的费用列:快递运费、快递记账、补运费、补记账、退回费、拦截费、改地址费、翻箱费、超长费、合计运费、备注。
* - 快递运费 = 结算运费(按我方核算),不能改;
* - 其余各列先按厂家上传模板「附加费」表的附加费类型自动带入,财务可以逐家改(清空就回到自动值);
* - 合计运费 = 快递运费 + 补运费 + 退回费 + 拦截费 + 改地址费 + 翻箱费 + 超长费 − 快递记账 + 补记账。
* 快递记账是扣款,按正数记、表里显示成负数。
* 手工改的值按月存在 supplier_payable_overrides(dataset = dropship-freight-fees:YYYY-MM,记录键 = 厂家 id)。
*/
final class DropshipFees
{
public const DATASET = 'dropship-freight-fees:';
public const COLUMNS = ['freight' => '快递运费', 'booking' => '快递记账', 'extra_freight' => '补运费', 'extra_booking' => '补记账',
'returned' => '退回费', 'intercept' => '拦截费', 'readdress' => '改地址费', 'repack' => '翻箱费', 'overlength' => '超长费'];
/** 财务可以改的列(快递运费取结算运费,不在这里改)。 */
public const EDITABLE = ['booking', 'extra_freight', 'extra_booking', 'returned', 'intercept', 'readdress', 'repack', 'overlength'];
/** 附加费类型里出现这些字就归到这一列,按顺序认(「补记账」先于「记账」,「拦截退回」算拦截费)。 */
private const MATCH = ['extra_booking' => ['补记账'], 'booking' => ['记账'], 'extra_freight' => ['补运费', '补运'],
'intercept' => ['拦截'], 'readdress' => ['改地址', '改址'], 'repack' => ['翻箱'], 'overlength' => ['超长'], 'returned' => ['退回', '退件', '退货']];
public function __construct(
private readonly DropshipFreightStore $store,
private readonly SupplierPayableOverrideStore $overrides,
) {}
public static function classify(string $type): ?string
{
foreach (self::MATCH as $key => $words) {
foreach ($words as $word) {
if (str_contains($type, $word)) {
return $key;
}
}
}
return null;
}
/** @param array<string,float|int|null> $v */
public static function total(array $v): float
{
$plus = ['freight', 'extra_freight', 'returned', 'intercept', 'readdress', 'repack', 'overlength', 'extra_booking'];
return round(array_sum(array_map(static fn (string $k): float => (float) ($v[$k] ?? 0), $plus)) - abs((float) ($v['booking'] ?? 0)), 2);
}
/**
* 各厂家这个月的费用列。
*
* @param array<int,float> $settle 厂家 id => 结算运费
* @return array<int,array{values:array<string,float>,auto:array<string,float>,manual:array<string,bool>,other:array<string,float>,note:string,total:float}>
*/
public function rows(string $month, array $settle, int $companyId): array
{
$auto = $this->auto($month);
$manual = [];
foreach ($this->overrides->listRows(self::DATASET . $month, $companyId) as $row) {
$manual[(int) $row['_action_key']] = $row;
}
$result = [];
foreach ($settle as $factoryId => $freight) {
$values = ['freight' => round((float) $freight, 2)];
$flags = [];
foreach (self::EDITABLE as $key) {
$set = $manual[$factoryId]['fees'][$key] ?? null;
$flags[$key] = $set !== null;
$values[$key] = round((float) ($set ?? $auto[$factoryId]['fees'][$key] ?? 0), 2);
}
$values['booking'] = abs($values['booking']);
$autoValues = array_map(static fn ($v): float => round((float) $v, 2), $auto[$factoryId]['fees'] ?? []);
if (isset($autoValues['booking'])) {
$autoValues['booking'] = abs($autoValues['booking']);
}
$result[$factoryId] = ['values' => $values, 'auto' => $autoValues, 'manual' => $flags, 'other' => $auto[$factoryId]['other'] ?? [],
'note' => (string) ($manual[$factoryId]['note'] ?? ''), 'total' => self::total($values)];
}
return $result;
}
/**
* 保存手工改的值:空着的列回到自动值。
*
* @param array<string,mixed> $fees 列 => 金额(空 = 自动)
*/
public function save(string $month, int $factoryId, array $fees, string $note, int $companyId, ?int $userId): void
{
$values = [];
foreach (self::EDITABLE as $key) {
$raw = trim((string) ($fees[$key] ?? ''));
if ($raw !== '') {
$values[$key] = round($key === 'booking' ? abs((float) $raw) : (float) $raw, 2);
}
}
$this->overrides->save(self::DATASET . $month, (string) $factoryId, ['fees' => $values, 'note' => mb_substr(trim($note), 0, 255)],
$companyId, $userId, 'dropship_fees');
}
/**
* 厂家上传的账单里「附加费」表按类型汇总的金额。
*
* @return array<int,array{fees:array<string,float>,other:array<string,float>}>
*/
public function auto(string $month): array
{
$result = [];
$files = $this->store->db()->table('dropship_bill_files')->where('bill_month', $month)->where('status', 'parsed')
->whereNotNull('factory_id')->get(['factory_id', 'sheets']);
foreach ($files as $file) {
foreach ((array) json_decode((string) $file->sheets, true) as $sheet) {
foreach ((array) ($sheet['fees'] ?? []) as $type => $amount) {
$id = (int) $file->factory_id;
$key = self::classify((string) $type);
if ($key === null) {
$result[$id]['other'][(string) $type] = round(($result[$id]['other'][(string) $type] ?? 0) + (float) $amount, 2);
} else {
$result[$id]['fees'][$key] = round(($result[$id]['fees'][$key] ?? 0) + (float) $amount, 2);
}
}
}
}
return $result;
}
}