feat(运费账单): 推送去重与老账单附加费补读 #1690
@ -511,7 +511,19 @@ final class CourierBillController extends Controller
|
||||
$companyId = (int) (company_id() ?: 1);
|
||||
$sent = $failed = [];
|
||||
$skipped = 0;
|
||||
// 批量推送:这个月已经推过、已经有账单的跳过;单独推某一家照发
|
||||
$pushes = app(\App\Support\CourierBills\CourierUploadPushes::class);
|
||||
$done = empty($data['group']) ? $pushes->done($data['month'], $companyId) : ['pushed' => [], 'uploaded' => []];
|
||||
$alreadyPushed = $alreadyUploaded = 0;
|
||||
foreach ($groups as $g) {
|
||||
if (isset($done['uploaded'][(int) $g->id])) {
|
||||
$alreadyUploaded++;
|
||||
continue;
|
||||
}
|
||||
if (isset($done['pushed'][(int) $g->id])) {
|
||||
$alreadyPushed++;
|
||||
continue;
|
||||
}
|
||||
if (! isset($configured[$g->name])) {
|
||||
$skipped++;
|
||||
continue;
|
||||
@ -520,6 +532,7 @@ final class CourierBillController extends Controller
|
||||
$notifier->pushCourierUploadLink((string) $g->name, $data['month'],
|
||||
app(\App\Support\CourierBills\CourierUploadLink::class)->pageUrl($companyId, (int) $g->id, $data['month']));
|
||||
$sent[] = $g->name;
|
||||
$pushes->record($data['month'], (int) $g->id, $companyId, user()?->id);
|
||||
} catch (Throwable $error) {
|
||||
report($error);
|
||||
$failed[] = $g->name . '(发送失败,请检查群机器人配置)';
|
||||
@ -531,6 +544,8 @@ final class CourierBillController extends Controller
|
||||
|
||||
return $this->back($request, [], '已推送 ' . $data['month'] . ' 上传链接 ' . count($sent) . ' 家' . ($sent !== [] ? ':' . implode('、', array_slice($sent, 0, 30)) : '')
|
||||
. ($failed !== [] ? ';失败 ' . count($failed) . ' 家:' . implode('、', $failed) : '')
|
||||
. ($alreadyUploaded > 0 ? ';' . $alreadyUploaded . ' 家这个月已经有账单,没再推' : '')
|
||||
. ($alreadyPushed > 0 ? ';' . $alreadyPushed . ' 家这个月已经推过,没再推(要重发到「对账分组」里单独点)' : '')
|
||||
. ($skipped > 0 && empty($data['group']) ? ';' . $skipped . ' 家没配钉钉群机器人,没推' : '') . '。');
|
||||
}
|
||||
|
||||
|
||||
34
app/Support/CourierBills/CourierUploadPushes.php
Normal file
34
app/Support/CourierBills/CourierUploadPushes.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\CourierBills;
|
||||
|
||||
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
|
||||
|
||||
/**
|
||||
* 快递上传链接推送记录:批量「推送上传链接到快递群」时,这个月已经推过的、已经有账单的分组跳过,免得重复打扰;
|
||||
* 在「对账分组」里单独点某一家照发。记录按月存在 supplier_payable_overrides(dataset = courier-upload-link-pushed:YYYY-MM,记录键 = 分组 id)。
|
||||
*/
|
||||
final class CourierUploadPushes
|
||||
{
|
||||
public const DATASET = 'courier-upload-link-pushed:';
|
||||
|
||||
public function __construct(private readonly SupplierPayableOverrideStore $store) {}
|
||||
|
||||
/** @return array{pushed:array<int,true>,uploaded:array<int,true>} 这个月已推过 / 已有账单的分组 id */
|
||||
public function done(string $month, int $companyId): array
|
||||
{
|
||||
$pushed = [];
|
||||
foreach ($this->store->listRows(self::DATASET . $month, $companyId) as $row) {
|
||||
$pushed[(int) $row['_action_key']] = true;
|
||||
}
|
||||
$uploaded = CourierBillMatcher::db()->table('courier_bill_statements')->where('bill_month', $month)->where('line_count', '>', 0)
|
||||
->distinct()->pluck('group_id')->mapWithKeys(static fn ($id): array => [(int) $id => true])->all();
|
||||
|
||||
return ['pushed' => $pushed, 'uploaded' => $uploaded];
|
||||
}
|
||||
|
||||
public function record(string $month, int $groupId, int $companyId, ?int $userId): void
|
||||
{
|
||||
$this->store->save(self::DATASET . $month, (string) $groupId, ['pushed_at' => now()->toDateTimeString()], $companyId, $userId, 'courier_upload_link');
|
||||
}
|
||||
}
|
||||
@ -81,7 +81,7 @@ final class FreightBillTemplate
|
||||
$result = [];
|
||||
try {
|
||||
foreach ($this->reader->sheets($path, $extension) as $name => $rows) {
|
||||
if (trim((string) $name) !== self::SURCHARGE_SHEET) {
|
||||
if (! str_contains(trim((string) $name), self::SURCHARGE_SHEET)) { // 「附加费」「厂家账单-附加费」都认
|
||||
continue;
|
||||
}
|
||||
$header = null;
|
||||
|
||||
@ -178,12 +178,11 @@ final class DropshipExporter
|
||||
$fee = 0.0;
|
||||
$hasFee = false;
|
||||
foreach ($this->store->db()->table('dropship_bill_files')->where('factory_id', $factory->id)->where('bill_month', $month)
|
||||
->where('status', 'parsed')->pluck('sheets') as $sheets) {
|
||||
foreach ((array) json_decode((string) $sheets, true) as $sheet) {
|
||||
if (! empty($sheet['fees'])) {
|
||||
$fee += array_sum(array_map('floatval', (array) $sheet['fees']));
|
||||
$hasFee = true;
|
||||
}
|
||||
->where('status', 'parsed')->get(['id', 'stored_path', 'sheets']) as $file) {
|
||||
$fees = $this->store->feesFor($file);
|
||||
if ($fees !== []) {
|
||||
$fee += array_sum($fees);
|
||||
$hasFee = true;
|
||||
}
|
||||
}
|
||||
// 上月手工汇总里的分类行(快递不符、未发出、跨月订单-X月……)这个月没有就不留空行
|
||||
|
||||
@ -113,17 +113,15 @@ final class DropshipFees
|
||||
{
|
||||
$result = [];
|
||||
$files = $this->store->db()->table('dropship_bill_files')->where('bill_month', $month)->where('status', 'parsed')
|
||||
->whereNotNull('factory_id')->get(['factory_id', 'sheets']);
|
||||
->whereNotNull('factory_id')->get(['id', 'factory_id', 'stored_path', '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);
|
||||
}
|
||||
foreach ($this->store->feesFor($file) 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,6 +32,58 @@ final class DropshipFreightStore
|
||||
private readonly CourierBillStore $courierBills,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 这份账单「附加费」表按类型的金额(类型 => 金额)。入库时记在 sheets 里;之前入库的老文件现读原文件补记一次。
|
||||
*
|
||||
* @return array<string,float>
|
||||
*/
|
||||
public function feesFor(object $file): array
|
||||
{
|
||||
$sheets = (array) json_decode((string) ($file->sheets ?? ''), true);
|
||||
$surcharge = array_filter($sheets, static fn ($sheet): bool => is_array($sheet)
|
||||
&& str_contains((string) ($sheet['sheet'] ?? ''), FreightBillTemplate::SURCHARGE_SHEET));
|
||||
if ($surcharge === []) {
|
||||
return []; // sheets 里列着原文件每张表,没有附加费表就不用读文件
|
||||
}
|
||||
$fees = [];
|
||||
if (array_filter($surcharge, static fn (array $sheet): bool => array_key_exists('fees', $sheet)) !== []) {
|
||||
foreach ($surcharge as $sheet) {
|
||||
foreach ((array) ($sheet['fees'] ?? []) as $type => $amount) {
|
||||
$fees[(string) $type] = round(($fees[(string) $type] ?? 0) + (float) $amount, 2);
|
||||
}
|
||||
}
|
||||
|
||||
return $fees;
|
||||
}
|
||||
if (empty($file->stored_path) || ! Storage::disk(self::DISK)->exists((string) $file->stored_path)) {
|
||||
return [];
|
||||
}
|
||||
$fees = (new FreightBillTemplate(new BillWorkbookReader()))->surchargesByType(Storage::disk(self::DISK)->path((string) $file->stored_path),
|
||||
pathinfo((string) $file->stored_path, PATHINFO_EXTENSION));
|
||||
if (! empty($file->id)) {
|
||||
$this->db()->table('dropship_bill_files')->where('id', $file->id)
|
||||
->update(['sheets' => json_encode(self::withFees($sheets, $fees), JSON_UNESCAPED_UNICODE)]);
|
||||
}
|
||||
|
||||
return $fees;
|
||||
}
|
||||
|
||||
/** 把附加费金额记到 sheets 里「附加费」那张表的记录上(没有附加费也记个空的,下次不用再读文件);不新增记录,导出按记录数给原表起名。 */
|
||||
private static function withFees(array $sheets, array $fees): array
|
||||
{
|
||||
$note = $fees === [] ? '没有附加费' : '附加费按类型记入各厂家汇总和小汇总';
|
||||
$done = false;
|
||||
foreach ($sheets as $i => $sheet) {
|
||||
if (! $done && is_array($sheet) && str_contains((string) ($sheet['sheet'] ?? ''), FreightBillTemplate::SURCHARGE_SHEET)) {
|
||||
$sheets[$i]['fees'] = $fees;
|
||||
$sheets[$i]['note'] = $note;
|
||||
$done = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $sheets;
|
||||
}
|
||||
|
||||
public function db(): ConnectionInterface
|
||||
{
|
||||
return DB::connection('business_mirror');
|
||||
@ -335,20 +387,7 @@ final class DropshipFreightStore
|
||||
// 按上传模板填的「附加费」表:按类型记下金额,各厂家汇总里自动带入退回费、拦截费等列
|
||||
$fees = (new FreightBillTemplate(new BillWorkbookReader()))->surchargesByType(Storage::disk(self::DISK)->path($file->stored_path),
|
||||
pathinfo($file->stored_path, PATHINFO_EXTENSION));
|
||||
if ($fees !== []) {
|
||||
$found = false;
|
||||
foreach ($result['sheets'] as &$sheet) {
|
||||
if ($sheet['sheet'] === FreightBillTemplate::SURCHARGE_SHEET) {
|
||||
$sheet['fees'] = $fees;
|
||||
$sheet['note'] = '附加费按类型记入各厂家汇总';
|
||||
$found = true;
|
||||
}
|
||||
}
|
||||
unset($sheet);
|
||||
if (! $found) {
|
||||
$result['sheets'][] = ['sheet' => FreightBillTemplate::SURCHARGE_SHEET, 'read' => false, 'note' => '附加费按类型记入各厂家汇总', 'fees' => $fees];
|
||||
}
|
||||
}
|
||||
$result['sheets'] = self::withFees($result['sheets'], $fees);
|
||||
$notes = [];
|
||||
if ($lines === []) {
|
||||
$notes[] = '没读到厂家运费:要有一张表同时有单号列(快递单号/运单号/单号…)和运费列(快递运费/运费/快递费…),且运费不为空';
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
<section class="cbr-panel" aria-label="让快递自己上传">
|
||||
<h3>让快递自己上传账单</h3>
|
||||
<form method="POST" action="{{ $action('push-upload-links') }}" class="cbr-panel-body cbr-form"
|
||||
onsubmit="return confirm('往所有配了钉钉对账群机器人的快递群推送上传链接?')">
|
||||
onsubmit="return confirm('往配了钉钉对账群机器人、这个月还没推过也还没有账单的快递群推送上传链接?')">
|
||||
@csrf
|
||||
<label>账单月份<input type="month" name="month" value="{{ $cbPushMonth ?? $month }}" required></label>
|
||||
<button type="submit" class="cbr-primary">推送上传链接到快递群</button>
|
||||
<span class="cbr-muted">对账分组已复用工作平台「应付群机器人配置」中对应快递的群机器人;未匹配的可在「对账分组」里补充。这里一键往已配置的群推本月的免登录上传链接。
|
||||
<span class="cbr-muted">对账分组已复用工作平台「应付群机器人配置」中对应快递的群机器人;未匹配的可在「对账分组」里补充。这里一键往已配置的群推本月的免登录上传链接;这个月已经推过、已经有账单的不再推(要重发某一家,到「对账分组」里单独点)。
|
||||
快递点开链接、下载模板(单号、省、重量、运费、快递名称、面单、费率)填好上传,后台校验读入后进入对账;链接只能传那一家那个月,30 天有效。已有 {{ collect($cbRobots ?? [])->where('configured', true)->count() }} 家配了机器人。</span>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
@ -424,6 +424,9 @@ final class DropshipFreightTest extends FeatureTestCase
|
||||
// 「拦截退回」算拦截费
|
||||
$this->assertSame('intercept', \App\Support\DropshipFreight\DropshipFees::classify('拦截退回'));
|
||||
|
||||
// 补丁前入库的老文件 sheets 里没记附加费:导出时现读原文件补记
|
||||
$sheets = collect(json_decode((string) $db->table('dropship_bill_files')->value('sheets'), true))->map(fn (array $x) => \Illuminate\Support\Arr::except($x, 'fees'))->all();
|
||||
$db->table('dropship_bill_files')->update(['sheets' => json_encode($sheets, JSON_UNESCAPED_UNICODE)]);
|
||||
// 导出的小汇总:附加费表金额合计放「厂家账单-附加费」一行,简尚核对空着
|
||||
$out = tempnam(sys_get_temp_dir(), 'dropship_export_');
|
||||
$handle = fopen($out, 'wb');
|
||||
@ -443,6 +446,9 @@ final class DropshipFreightTest extends FeatureTestCase
|
||||
$this->assertEquals(6.5, $sheet->getCell('D5')->getCalculatedValue());
|
||||
$book->disconnectWorksheets();
|
||||
@unlink($out);
|
||||
$file = $db->table('dropship_bill_files')->first();
|
||||
$this->assertStringContainsString('"fees"', (string) $file->sheets);
|
||||
$this->assertSame(['退回费' => 3.0, '拦截费' => 2.0, '上楼费' => 1.5], app(DropshipFreightStore::class)->feesFor($file));
|
||||
}
|
||||
|
||||
public function test_export_keeps_uncalculated_settlement_blank(): void
|
||||
|
||||
@ -288,6 +288,21 @@ final class PortalFreightBillTest extends FeatureTestCase
|
||||
$this->assertStringContainsString('运费账单上传 · 曹县韵达', $text);
|
||||
preg_match('/\[打开账单上传页\]\(([^)]+)\)/', $text, $m);
|
||||
$link = $m[1];
|
||||
// 再点批量推送:这个月已经推过的不再推;单独点这一家照发
|
||||
$count = count(Http::recorded());
|
||||
$this->post($action('push-upload-links'), ['month' => '2026-11'])
|
||||
->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '已推送 2026-11 上传链接 0 家') && str_contains($m, '1 家这个月已经推过'));
|
||||
$this->assertCount($count, Http::recorded());
|
||||
$this->post($action('push-upload-links'), ['month' => '2026-11', 'group' => $groupId])->assertSessionHas('courier_bills_message');
|
||||
$this->assertCount($count + 1, Http::recorded());
|
||||
// 别的月份没推过,照推;这个月已经有账单的也跳过
|
||||
$db->table('courier_bill_statements')->insert(['group_id' => $groupId, 'bill_month' => '2026-09', 'line_count' => 3, 'created_at' => now(), 'updated_at' => now()]);
|
||||
$this->post($action('push-upload-links'), ['month' => '2026-09'])
|
||||
->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '1 家这个月已经有账单'));
|
||||
$this->post($action('push-upload-links'), ['month' => '2026-08'])
|
||||
->assertSessionHas('courier_bills_message', fn (string $m): bool => str_contains($m, '上传链接 1 家'));
|
||||
$this->assertCount($count + 2, Http::recorded());
|
||||
$db->table('courier_bill_statements')->where('bill_month', '2026-09')->delete();
|
||||
|
||||
auth()->logout();
|
||||
$this->get($link . '&month=2026-10')->assertOk()->assertSeeText('2026年11月');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user