feat: 业绩明细更新后自动重算商品成本 (#1448)
This commit is contained in:
parent
523b6c2db2
commit
3ce42d564e
@ -12,6 +12,7 @@ use App\Business\PlantingOrderLabelLookup;
|
||||
use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap;
|
||||
use Modules\EcommerceFinance\Services\AccountMappingStore;
|
||||
use Modules\EcommerceFinance\Services\LedgerRemarkFilter;
|
||||
use Modules\EcommerceFinance\Services\ProductCostAutoRefresh;
|
||||
use Modules\EcommerceFinance\Services\StoreReportMirrorSynchronizer;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
@ -226,6 +227,13 @@ class SyncStoreReceiptOrderMatches extends Command
|
||||
if ($result === self::SUCCESS && ! (bool) $this->option('planting-only')) {
|
||||
$this->refreshStoreReportPaidSales();
|
||||
}
|
||||
if (
|
||||
$result === self::SUCCESS
|
||||
&& ! (bool) $this->option('dry-run')
|
||||
&& (int) $this->option('limit') === 0
|
||||
) {
|
||||
$this->refreshProductCosts();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
@ -1076,6 +1084,37 @@ class SyncStoreReceiptOrderMatches extends Command
|
||||
: sprintf(' 店铺维度报表销量汇总:业绩明细无变化,沿用 %d 行。', $result['stored_rows']));
|
||||
}
|
||||
|
||||
/** 业绩明细写入后,按同一账期和平台自动重算商品成本。 */
|
||||
private function refreshProductCosts(): void
|
||||
{
|
||||
try {
|
||||
$result = app(ProductCostAutoRefresh::class)->refresh(
|
||||
trim((string) $this->argument('month')),
|
||||
(bool) $this->option('planting-only')
|
||||
? ''
|
||||
: trim((string) $this->option('platform')),
|
||||
);
|
||||
} catch (Throwable $exception) {
|
||||
$this->warn('商品成本自动更新失败:' . $exception->getMessage());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($result['status'] === 'skipped') {
|
||||
return;
|
||||
}
|
||||
if ($result['status'] !== 'succeeded') {
|
||||
$this->warn('商品成本自动更新失败,请查看日志或稍后手动重跑。');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->line(sprintf(
|
||||
' 商品成本自动更新:已完成 %d 个平台。',
|
||||
count($result['platforms']),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/** 只回填「种菜金额 / 标签」:按种菜明细表与聚水潭种菜标签的并集判断。 */
|
||||
private function backfillPlantingAmounts(string $reportMonth): int
|
||||
|
||||
@ -6,6 +6,8 @@
|
||||
|
||||
本清单是商品成本同步的固定校验口径。每月重跑时先完成商品月加权平均单价,再执行商品成本同步;同一规则不得在单月任务中临时改变。
|
||||
|
||||
业绩明细 `store-receipt:order-match:sync` 写入成功后,会自动重算同一账期、同一平台的商品成本;种菜全平台回填会按该账期实际存在的平台逐一重算。调试用的 `--dry-run` 和 `--limit` 不触发自动重算。
|
||||
|
||||
仓库映射表新增或修改后,如需只刷新仓库简称且不改动数量、单价和成本金额,使用:
|
||||
|
||||
```text
|
||||
|
||||
128
modules/EcommerceFinance/Services/ProductCostAutoRefresh.php
Normal file
128
modules/EcommerceFinance/Services/ProductCostAutoRefresh.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 业绩明细写入成功后自动重算对应账期、平台的商品成本。
|
||||
*/
|
||||
class ProductCostAutoRefresh
|
||||
{
|
||||
private const PLATFORM_KEYS = [
|
||||
'淘系支付宝' => 'taobao-alipay',
|
||||
'淘系微信' => 'taobao-wechat',
|
||||
'拼多多' => 'pinduoduo',
|
||||
'得物' => 'dewu',
|
||||
'京东网银钱包' => 'jd-wallet',
|
||||
'京东账单' => 'jd-transactions',
|
||||
'京喜账单' => 'jingxi',
|
||||
'抖音' => 'douyin',
|
||||
];
|
||||
|
||||
/** @param null|Closure(list<string>): array{success:bool,output:string} $runner */
|
||||
public function __construct(private readonly ?Closure $runner = null)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{status:string,platforms:list<string>,results:list<array<string,mixed>>}
|
||||
*/
|
||||
public function refresh(string $month, string $platform = ''): array
|
||||
{
|
||||
if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) {
|
||||
throw new InvalidArgumentException('商品成本自动更新月份格式应为 YYYY-MM。');
|
||||
}
|
||||
|
||||
$keys = $platform !== ''
|
||||
? [$platform]
|
||||
: $this->platformKeysForMonth($month);
|
||||
if ($keys === []) {
|
||||
return ['status' => 'skipped', 'platforms' => [], 'results' => []];
|
||||
}
|
||||
|
||||
$results = [];
|
||||
foreach ($keys as $key) {
|
||||
$command = [
|
||||
PHP_BINARY ?: 'php',
|
||||
'artisan',
|
||||
'store-receipt:product-cost:sync',
|
||||
$month,
|
||||
'--platform=' . $key,
|
||||
'--chunk=1000',
|
||||
];
|
||||
$result = $this->run($command);
|
||||
$results[] = ['platform' => $key] + $result;
|
||||
if (! $result['success']) {
|
||||
Log::warning('业绩明细更新后商品成本自动重算失败。', [
|
||||
'month' => $month,
|
||||
'platform' => $key,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => collect($results)->every(static fn (array $result): bool => $result['success'])
|
||||
? 'succeeded'
|
||||
: 'failed',
|
||||
'platforms' => $keys,
|
||||
'results' => $results,
|
||||
];
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function platformKeysForMonth(string $month): array
|
||||
{
|
||||
$labels = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'))
|
||||
->table('store_receipt_order_match_rows')
|
||||
->where('report_month', $month . '-01')
|
||||
->whereNotNull('platform')
|
||||
->where('platform', '!=', '')
|
||||
->distinct()
|
||||
->orderBy('platform')
|
||||
->pluck('platform')
|
||||
->all();
|
||||
|
||||
$keys = [];
|
||||
foreach ($labels as $label) {
|
||||
$key = self::PLATFORM_KEYS[trim((string) $label)] ?? null;
|
||||
if ($key !== null) {
|
||||
$keys[$key] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($keys);
|
||||
}
|
||||
|
||||
/** @param list<string> $command
|
||||
* @return array{success:bool,output:string}
|
||||
*/
|
||||
protected function run(array $command): array
|
||||
{
|
||||
if ($this->runner !== null) {
|
||||
return ($this->runner)($command);
|
||||
}
|
||||
|
||||
try {
|
||||
$process = new Process($command, base_path());
|
||||
$process->setTimeout(7200);
|
||||
$process->run();
|
||||
|
||||
return [
|
||||
'success' => $process->isSuccessful(),
|
||||
'output' => mb_substr(
|
||||
trim($process->getOutput() . "\n" . $process->getErrorOutput()),
|
||||
0,
|
||||
4000,
|
||||
),
|
||||
];
|
||||
} catch (Throwable $exception) {
|
||||
return ['success' => false, 'output' => $exception->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
50
tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php
Normal file
50
tests/Unit/EcommerceFinance/ProductCostAutoRefreshTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\EcommerceFinance;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Modules\EcommerceFinance\Services\ProductCostAutoRefresh;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProductCostAutoRefreshTest extends TestCase
|
||||
{
|
||||
public function test_it_runs_product_cost_for_the_updated_month_and_platform(): void
|
||||
{
|
||||
$calls = [];
|
||||
$service = new ProductCostAutoRefresh(
|
||||
static function (array $command) use (&$calls): array {
|
||||
$calls[] = $command;
|
||||
|
||||
return ['success' => true, 'output' => 'ok'];
|
||||
},
|
||||
);
|
||||
|
||||
$result = $service->refresh('2026-10', 'taobao-alipay');
|
||||
|
||||
$this->assertSame('succeeded', $result['status']);
|
||||
$this->assertSame(['taobao-alipay'], $result['platforms']);
|
||||
$this->assertCount(1, $calls);
|
||||
$this->assertContains('store-receipt:product-cost:sync', $calls[0]);
|
||||
$this->assertContains('2026-10', $calls[0]);
|
||||
$this->assertContains('--platform=taobao-alipay', $calls[0]);
|
||||
}
|
||||
|
||||
public function test_it_reports_a_failed_product_cost_refresh(): void
|
||||
{
|
||||
$service = new ProductCostAutoRefresh(
|
||||
static fn (array $command): array => ['success' => false, 'output' => 'failed'],
|
||||
);
|
||||
|
||||
$result = $service->refresh('2026-10', 'pinduoduo');
|
||||
|
||||
$this->assertSame('failed', $result['status']);
|
||||
$this->assertFalse($result['results'][0]['success']);
|
||||
}
|
||||
|
||||
public function test_it_rejects_an_invalid_month(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
(new ProductCostAutoRefresh())->refresh('2026-10-01', 'taobao-alipay');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user