CaiWuXiTong/tests/Unit/EcommerceFinance/SupplierPayableForecastSummaryRepositoryTest.php
苏木 bf8778bfcd feat(供应商货款预估): 接入本月汇总表 (#1559)
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-30 13:56:12 +08:00

286 lines
13 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastRepository;
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastSummaryRepository;
use Tests\TestCase;
class SupplierPayableForecastSummaryRepositoryTest extends TestCase
{
protected function tearDown(): void
{
Carbon::setTestNow();
parent::tearDown();
}
public function test_summary_combines_forecast_details_and_uses_the_requested_formulas(): void
{
Carbon::setTestNow(Carbon::parse('2026-11-30 12:00:00', 'Asia/Shanghai'));
$this->createTables();
$this->seedReadyStates();
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::HEAD_TABLE)->insert([
[
'Id' => 1,
'io_id' => 'IN-SUMMARY-A',
'po_id' => 1001,
'io_date' => '2026-11-10 10:00:00',
'warehouse' => '正常仓',
'supplier_name' => '供应商甲',
],
[
'Id' => 2,
'io_id' => 'IN-SUMMARY-B',
'po_id' => 1002,
'io_date' => '2026-11-11 10:00:00',
'warehouse' => '正常仓',
'supplier_name' => '彩艺',
],
[
'Id' => 3,
'io_id' => 'IN-OCTOBER',
'po_id' => 1003,
'io_date' => '2026-10-20 10:00:00',
'warehouse' => '正常仓',
'supplier_name' => '历史供应商',
],
]);
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::ITEM_TABLE)->insert([
[
'Id' => 1,
'io_id' => 'IN-SUMMARY-A',
'sku_id' => 'SKU-GOODS-A',
'i_id' => 'STYLE-GOODS-A',
'name' => '甲货款',
'qty' => 2,
'cost_price' => 8,
'cost_amount' => 16,
],
[
'Id' => 2,
'io_id' => 'IN-SUMMARY-B',
'sku_id' => 'SKU-GOODS-B',
'i_id' => 'STYLE-GOODS-B',
'name' => '乙货款',
'qty' => 1,
'cost_price' => 100,
'cost_amount' => 100,
],
[
'Id' => 3,
'io_id' => 'IN-OCTOBER',
'sku_id' => 'SKU-OCTOBER',
'i_id' => 'STYLE-OCTOBER',
'name' => '历史货款',
'qty' => 1,
'cost_price' => 9,
'cost_amount' => 9,
],
]);
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::PRICE_TABLE)->insert([
['Id' => 1, 'skucode' => 'SKU-GOODS-A', 'priceType' => '1', 'afterPrice' => 8, 'effectStartTime' => '2026-10-01 00:00:00'],
['Id' => 2, 'skucode' => 'SKU-RETURN-A', 'priceType' => '1', 'afterPrice' => 8, 'effectStartTime' => '2026-10-01 00:00:00'],
['Id' => 3, 'skucode' => 'SKU-GOODS-B', 'priceType' => '1', 'afterPrice' => 100, 'effectStartTime' => '2026-10-01 00:00:00'],
['Id' => 4, 'skucode' => 'SKU-OCTOBER', 'priceType' => '1', 'afterPrice' => 9, 'effectStartTime' => '2026-10-01 00:00:00'],
]);
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::PRODUCT_TABLE)->insert([
['autoid' => 1, 'sku_id' => 'SKU-GOODS-A', 'sku_type' => 'normal', 'supplier_name' => '供应商甲', 'cost_price' => 8],
['autoid' => 2, 'sku_id' => 'SKU-RETURN-A', 'sku_type' => 'normal', 'supplier_name' => '供应商甲', 'cost_price' => 8],
['autoid' => 3, 'sku_id' => 'SKU-GOODS-B', 'sku_type' => 'normal', 'supplier_name' => '彩艺', 'cost_price' => 100],
]);
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::RETURN_HEAD_TABLE)->insert([
'Id' => 1,
'io_id' => 5001,
'io_date' => '2026-11-12 10:00:00',
'status' => 'Confirmed',
'warehouse' => '正常仓',
]);
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::RETURN_ITEM_TABLE)->insert([
'Id' => 1,
'io_id' => 5001,
'ioi_id' => 'RETURN-ITEM-1',
'sku_id' => 'SKU-RETURN-A',
'i_id' => 'STYLE-RETURN-A',
'name' => '甲退货',
'qty' => 1,
'cost_price' => 8,
'cost_amount' => 8,
]);
DB::connection('business_mirror')->table(SupplierPayableForecastSummaryRepository::METADATA_TABLE)->insert([
[
'id' => 1,
'Month' => '2026-11',
'company' => '供应商甲公司',
'supplier' => '供应商甲',
'ExpFieght' => 1000,
'loan' => 999999,
'refund' => 888888,
'advloan' => 500,
'invoiceDct' => 300,
'boardDct' => 700,
'specimenDct' => 200,
'guessPrice' => 999999,
'unpriced' => 400,
],
[
'id' => 2,
'Month' => '2026-11',
'company' => '彩艺家具有限公司',
'supplier' => '彩艺',
'ExpFieght' => 0,
'loan' => 0,
'refund' => 0,
'advloan' => 0,
'invoiceDct' => 0,
'boardDct' => 0,
'specimenDct' => 0,
'guessPrice' => 0,
'unpriced' => 0,
],
]);
$result = app(SupplierPayableForecastSummaryRepository::class)->paginate([], 1);
$this->assertTrue($result['ready']);
$this->assertSame(['2026-11', '2026-10'], $result['months']);
$this->assertSame('2026-11', $result['filters']['month']);
$this->assertSame(2, $result['total']);
$this->assertSame(116.0, $result['totals']['goods_amount']);
$this->assertSame(8.0, $result['totals']['return_amount']);
$this->assertSame(108.0, $result['totals']['subtotal']);
$this->assertSame(-192.0, $result['totals']['supplier_balance']);
$supplierA = collect($result['rows'])->firstWhere('supplier', '供应商甲');
$this->assertSame('供应商甲公司', $supplierA['company']);
$this->assertSame(16.0, $supplierA['goods_amount']);
$this->assertSame(8.0, $supplierA['return_amount']);
$this->assertSame(8.0, $supplierA['subtotal']);
$this->assertSame(-292.0, $supplierA['supplier_balance']);
$this->assertSame(0.0, $supplierA['estimated_board_deduction']);
$this->assertNull($supplierA['estimated_amount']);
$filtered = app(SupplierPayableForecastSummaryRepository::class)->paginate([
'month' => '2026-11',
'keyword' => '彩艺',
], 1);
$this->assertSame(1, $filtered['total']);
$this->assertSame('彩艺', $filtered['rows'][0]['supplier']);
$this->assertSame(100.0, $filtered['rows'][0]['goods_amount']);
$this->assertSame(0.0, $filtered['rows'][0]['return_amount']);
$this->assertSame(100.0, $filtered['rows'][0]['supplier_balance']);
}
public function test_summary_is_empty_until_forecast_details_are_initialized(): void
{
Carbon::setTestNow(Carbon::parse('2026-11-30 12:00:00', 'Asia/Shanghai'));
$this->createTables();
$result = app(SupplierPayableForecastSummaryRepository::class)->paginate([], 1);
$this->assertFalse($result['ready']);
$this->assertSame('pending', $result['sync_status']['code']);
$this->assertSame([], $result['rows']);
$this->assertSame(0, $result['total']);
}
private function seedReadyStates(): void
{
$now = now();
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
['dataset_key' => SupplierPayableForecastRepository::HEAD_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now],
['dataset_key' => SupplierPayableForecastRepository::ITEM_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now],
['dataset_key' => SupplierPayableForecastRepository::PRICE_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now],
['dataset_key' => SupplierPayableForecastRepository::RETURN_HEAD_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now],
['dataset_key' => SupplierPayableForecastRepository::RETURN_ITEM_DATASET, 'status' => 'succeeded', 'initial_completed_at' => $now, 'last_succeeded_at' => $now],
]);
}
private function createTables(): void
{
$schema = Schema::connection('business_mirror');
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
$table->id();
$table->string('dataset_key')->unique();
$table->string('status', 32);
$table->dateTime('initial_completed_at')->nullable();
$table->dateTime('last_succeeded_at')->nullable();
});
$schema->create(SupplierPayableForecastRepository::HEAD_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->string('io_id', 100);
$table->unsignedBigInteger('po_id')->nullable();
$table->dateTime('io_date');
$table->string('warehouse', 255)->nullable();
$table->string('supplier_name', 255)->nullable();
});
$schema->create(SupplierPayableForecastRepository::ITEM_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->string('io_id', 100);
$table->string('sku_id', 100)->nullable();
$table->string('i_id', 100)->nullable();
$table->string('name', 255)->nullable();
$table->decimal('qty', 18, 2)->nullable();
$table->decimal('cost_price', 18, 2)->nullable();
$table->decimal('cost_amount', 18, 2)->nullable();
});
$schema->create(SupplierPayableForecastRepository::PRICE_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->string('skucode', 100)->nullable();
$table->string('priceType', 20)->nullable();
$table->decimal('afterPrice', 18, 2)->nullable();
$table->dateTime('effectStartTime')->nullable();
});
$schema->create(SupplierPayableForecastRepository::PRODUCT_TABLE, function (Blueprint $table): void {
$table->unsignedBigInteger('autoid');
$table->string('sku_id', 100)->nullable();
$table->string('sku_type', 32)->nullable();
$table->decimal('cost_price', 18, 2)->nullable();
$table->string('supplier_name', 255)->nullable();
});
$schema->create(SupplierPayableForecastRepository::RETURN_HEAD_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->unsignedBigInteger('io_id')->nullable();
$table->dateTime('io_date')->nullable();
$table->string('status', 50)->nullable();
$table->string('warehouse', 255)->nullable();
$table->text('remark')->nullable();
$table->string('labels', 255)->nullable();
$table->string('l_id', 100)->nullable();
$table->string('creator_name', 255)->nullable();
});
$schema->create(SupplierPayableForecastRepository::RETURN_ITEM_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->string('ioi_id', 100)->nullable();
$table->string('sku_id', 100)->nullable();
$table->string('i_id', 100)->nullable();
$table->string('name', 500)->nullable();
$table->decimal('qty', 18, 2)->nullable();
$table->decimal('cost_price', 18, 2)->nullable();
$table->decimal('cost_amount', 18, 2)->nullable();
$table->text('remark')->nullable();
$table->unsignedBigInteger('io_id')->nullable();
});
$schema->create(SupplierPayableForecastSummaryRepository::METADATA_TABLE, function (Blueprint $table): void {
$table->unsignedBigInteger('id')->primary();
$table->string('company', 255)->nullable();
$table->string('supplier', 255)->nullable();
$table->decimal('ExpFieght', 18, 2)->nullable();
$table->decimal('loan', 18, 2)->nullable();
$table->decimal('refund', 18, 2)->nullable();
$table->decimal('advloan', 18, 2)->nullable();
$table->decimal('invoiceDct', 18, 2)->nullable();
$table->decimal('boardDct', 18, 2)->nullable();
$table->decimal('specimenDct', 18, 2)->nullable();
$table->decimal('guessPrice', 18, 2)->nullable();
$table->decimal('unpriced', 18, 2)->nullable();
$table->string('Month', 7)->nullable();
});
}
}