150 lines
6.3 KiB
PHP
150 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Mockery;
|
|
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastRepository;
|
|
use Modules\EcommerceFinance\Services\BusinessMirrorSynchronizer;
|
|
use Modules\EcommerceFinance\Services\Jushuitan\ShippingReplicaSynchronizer;
|
|
use Modules\EcommerceFinance\Services\SupplierPayableForecastSyncRunStore;
|
|
use Modules\EcommerceFinance\Services\SupplierPayableForecastSyncService;
|
|
use Tests\TestCase;
|
|
|
|
class SupplierPayableForecastSyncServiceTest extends TestCase
|
|
{
|
|
public function test_daily_close_syncs_the_previous_day_and_records_the_difference(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-12-01 07:00:00', 'Asia/Shanghai'));
|
|
$day = Carbon::parse('2026-11-30', 'Asia/Shanghai');
|
|
|
|
$repository = Mockery::mock(SupplierPayableForecastRepository::class);
|
|
$repository->shouldReceive('goodsDetails')->twice()->andReturnValues([
|
|
$this->summary(9, 12, 1200, 0),
|
|
$this->summary(10, 15, 1500, 80),
|
|
]);
|
|
|
|
$syncCalls = [];
|
|
$mirrors = Mockery::mock(BusinessMirrorSynchronizer::class);
|
|
$mirrors->shouldReceive('sync')
|
|
->times(4)
|
|
->andReturnUsing(function (string $dataset, bool $full, int $batch, int $maxBatches) use (&$syncCalls): array {
|
|
$syncCalls[] = [$dataset, $full, $batch, $maxBatches];
|
|
|
|
return ['status' => 'succeeded', 'mode' => $full ? 'full' : 'incremental', 'batches' => 1, 'rows' => 1, 'dataset' => $dataset];
|
|
});
|
|
$this->assertSame([], $syncCalls);
|
|
|
|
$shipping = Mockery::mock(ShippingReplicaSynchronizer::class);
|
|
$shipping->shouldReceive('sync')
|
|
->once()
|
|
->withArgs(static fn (Carbon $from, Carbon $to, int $limit, mixed $progress, bool $skipFreight): bool =>
|
|
$from->format('Y-m-d H:i:s') === '2026-11-30 00:00:00'
|
|
&& $to->format('Y-m-d H:i:s') === '2026-12-01 00:00:00'
|
|
&& $limit === 2000
|
|
&& $progress === null
|
|
&& $skipFreight
|
|
)
|
|
->andReturn(['processed' => 1, 'autoship' => 0, 'outbound' => 0, 'purchase_out' => 0]);
|
|
|
|
$result = (new SupplierPayableForecastSyncService(
|
|
$repository,
|
|
$mirrors,
|
|
$shipping,
|
|
app(SupplierPayableForecastSyncRunStore::class),
|
|
))->closeDay($day);
|
|
|
|
$this->assertSame('daily', $result['run_type']);
|
|
$this->assertSame(1, $result['diff']['total']);
|
|
$this->assertSame(3.0, $result['diff']['quantity']);
|
|
$this->assertSame(300.0, $result['diff']['amount']);
|
|
$this->assertSame(80.0, $result['diff']['estimated_amount_after_discount']);
|
|
$this->assertSame([
|
|
SupplierPayableForecastRepository::HEAD_DATASET,
|
|
SupplierPayableForecastRepository::ITEM_DATASET,
|
|
SupplierPayableForecastRepository::PRICE_DATASET,
|
|
SupplierPayableForecastRepository::CURRENT_PRICE_DATASET,
|
|
], array_column($syncCalls, 0));
|
|
$this->assertSame([false, false, false, false], array_column($syncCalls, 1));
|
|
|
|
$this->assertDatabaseHas('supplier_payable_forecast_sync_runs', [
|
|
'run_type' => 'daily',
|
|
'period_start' => '2026-11-30',
|
|
'period_end' => '2026-11-30',
|
|
'status' => 'succeeded',
|
|
'diff_total' => 1,
|
|
]);
|
|
}
|
|
|
|
public function test_monthly_reconciliation_uses_full_purchase_sync_and_records_difference(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-12-01 07:10:00', 'Asia/Shanghai'));
|
|
$month = Carbon::parse('2026-11-01', 'Asia/Shanghai');
|
|
|
|
$repository = Mockery::mock(SupplierPayableForecastRepository::class);
|
|
$repository->shouldReceive('goodsDetails')->twice()->andReturnValues([
|
|
$this->summary(100, 1000, 100000, 8000),
|
|
$this->summary(102, 1002, 100200, 8002),
|
|
]);
|
|
|
|
$fullFlags = [];
|
|
$mirrors = Mockery::mock(BusinessMirrorSynchronizer::class);
|
|
$mirrors->shouldReceive('sync')
|
|
->times(4)
|
|
->andReturnUsing(function (string $dataset, bool $full) use (&$fullFlags): array {
|
|
$fullFlags[] = [$dataset, $full];
|
|
|
|
return ['status' => 'succeeded', 'mode' => $full ? 'full' : 'incremental', 'batches' => 1, 'rows' => 1, 'dataset' => $dataset];
|
|
});
|
|
|
|
$shipping = Mockery::mock(ShippingReplicaSynchronizer::class);
|
|
$shipping->shouldReceive('sync')
|
|
->once()
|
|
->withArgs(static fn (Carbon $from, Carbon $to): bool =>
|
|
$from->format('Y-m-d H:i:s') === '2026-11-01 00:00:00'
|
|
&& $to->format('Y-m-d H:i:s') === '2026-12-01 00:00:00'
|
|
)
|
|
->andReturn(['processed' => 1]);
|
|
|
|
$result = (new SupplierPayableForecastSyncService(
|
|
$repository,
|
|
$mirrors,
|
|
$shipping,
|
|
app(SupplierPayableForecastSyncRunStore::class),
|
|
))->reconcileMonth($month);
|
|
|
|
$this->assertSame('monthly', $result['run_type']);
|
|
$this->assertSame(2, $result['diff']['total']);
|
|
$this->assertSame(2.0, $result['diff']['quantity']);
|
|
$this->assertSame(200.0, $result['diff']['amount']);
|
|
$this->assertSame(2.0, $result['diff']['estimated_amount_after_discount']);
|
|
$this->assertSame([
|
|
SupplierPayableForecastRepository::HEAD_DATASET => true,
|
|
SupplierPayableForecastRepository::ITEM_DATASET => true,
|
|
SupplierPayableForecastRepository::PRICE_DATASET => true,
|
|
SupplierPayableForecastRepository::CURRENT_PRICE_DATASET => true,
|
|
], array_column($fullFlags, 1, 0));
|
|
$this->assertDatabaseHas('supplier_payable_forecast_sync_runs', [
|
|
'run_type' => 'monthly',
|
|
'period_start' => '2026-11-01',
|
|
'period_end' => '2026-11-30',
|
|
'status' => 'succeeded',
|
|
'diff_total' => 2,
|
|
]);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function summary(int $total, float $quantity, float $amount, float $estimated): array
|
|
{
|
|
return [
|
|
'total' => $total,
|
|
'quantity' => $quantity,
|
|
'amount' => $amount,
|
|
'estimated_amount_after_discount' => $estimated,
|
|
'rows' => [],
|
|
'page' => 1,
|
|
'per_page' => 1,
|
|
];
|
|
}
|
|
}
|