CaiWuXiTong/modules/EcommerceFinance/Services/SupplierPayableForecastSyncService.php
苏木 bda9f5868e feat(供应商货款预估): 增加日结与月初差异复核 (#1556)
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-30 11:39:09 +08:00

176 lines
6.3 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Services;
use Illuminate\Support\Carbon;
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastRepository;
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
use RuntimeException;
class SupplierPayableForecastSyncService
{
public function __construct(
private readonly SupplierPayableForecastRepository $repository,
private readonly BusinessMirrorSynchronizer $mirrors,
private readonly ShippingReplicaSynchronizer $shipping,
private readonly SupplierPayableForecastSyncRunStore $runs,
) {
}
/** @return array<string,mixed> */
public function closeDay(Carbon $day): array
{
$day = $day->copy()->startOfDay();
$before = $this->summary($day, $day);
$runId = $this->runs->start('daily', $day, $day);
$details = [
'scope' => 'previous_day',
'cutoff' => $day->copy()->endOfDay()->format('Y-m-d H:i:s'),
];
try {
$details['purchase_sync'] = $this->syncForecastSources(false);
$details['shipping_sync'] = $this->shipping->sync(
$day->copy()->startOfDay(),
$day->copy()->addDay()->startOfDay(),
2000,
null,
true,
);
} catch (\Throwable $exception) {
$this->runs->fail($runId, $exception->getMessage());
throw $exception;
}
$after = $this->summary($day, $day);
$this->runs->complete($runId, $before, $after, $details);
return [
'run_id' => $runId,
'run_type' => 'daily',
'period_start' => $day->toDateString(),
'period_end' => $day->toDateString(),
'before' => $before,
'after' => $after,
'diff' => $this->difference($before, $after),
];
}
/** @return array<string,mixed> */
public function reconcileMonth(Carbon $month): array
{
$start = $month->copy()->startOfMonth()->startOfDay();
$end = $month->copy()->endOfMonth()->startOfDay();
$before = $this->summary($start, $end);
$runId = $this->runs->start('monthly', $start, $end);
$details = [
'scope' => 'previous_month_full',
'cutoff' => $end->copy()->endOfDay()->format('Y-m-d H:i:s'),
];
try {
$details['purchase_sync'] = $this->syncForecastSources(true);
$details['shipping_sync'] = $this->shipping->sync(
$start->copy()->startOfDay(),
$end->copy()->addDay()->startOfDay(),
2000,
null,
true,
);
} catch (\Throwable $exception) {
$this->runs->fail($runId, $exception->getMessage());
throw $exception;
}
$after = $this->summary($start, $end);
$this->runs->complete($runId, $before, $after, $details);
return [
'run_id' => $runId,
'run_type' => 'monthly',
'period_start' => $start->toDateString(),
'period_end' => $end->toDateString(),
'before' => $before,
'after' => $after,
'diff' => $this->difference($before, $after),
];
}
/** @return array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float} */
private function summary(Carbon $start, Carbon $end): array
{
$result = $this->repository->goodsDetails([
'date_from' => $start->copy()->startOfDay()->format('Y-m-d H:i:s'),
'date_to' => $end->copy()->endOfDay()->format('Y-m-d H:i:s'),
'keyword' => '',
], 1, 1);
return [
'total' => $result['total'],
'quantity' => $result['quantity'],
'amount' => $result['amount'],
'estimated_amount_after_discount' => $result['estimated_amount_after_discount'],
];
}
/** @return array<string,array<string,mixed>> */
private function syncForecastSources(bool $full): array
{
return [
'purchase_receiving' => $this->syncDataset(
SupplierPayableForecastRepository::HEAD_DATASET,
$full,
),
'purchase_receiving_item' => $this->syncDataset(
SupplierPayableForecastRepository::ITEM_DATASET,
$full,
),
'price_adjustment_log' => $this->syncDataset(
SupplierPayableForecastRepository::PRICE_DATASET,
$full,
),
'current_price' => $this->syncDataset(
SupplierPayableForecastRepository::CURRENT_PRICE_DATASET,
$full,
),
];
}
/** @return array<string,mixed> */
private function syncDataset(string $dataset, bool $full): array
{
for ($attempt = 1; $attempt <= 120; $attempt++) {
$result = $this->mirrors->sync($dataset, $full, 2000, 20);
if ($result['status'] === 'succeeded') {
return $result + ['attempts' => $attempt];
}
if ($result['status'] !== 'running') {
throw new RuntimeException(
'供应商货款预估同步未完成:' . $dataset . ' / ' . $result['status'],
);
}
$full = $result['mode'] === 'full';
}
throw new RuntimeException('供应商货款预估同步超过最大续传轮次:' . $dataset);
}
/**
* @param array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float} $before
* @param array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float} $after
* @return array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float}
*/
private function difference(array $before, array $after): array
{
return [
'total' => $after['total'] - $before['total'],
'quantity' => round($after['quantity'] - $before['quantity'], 2),
'amount' => round($after['amount'] - $before['amount'], 2),
'estimated_amount_after_discount' => round(
$after['estimated_amount_after_discount'] - $before['estimated_amount_after_discount'],
2,
),
];
}
}