CaiWuXiTong/tests/Unit/EcommerceFinance/SupplierPayableForecastRepositoryTest.php
苏木 205cdfe50e feat(供应商货款预估): 接入采购入库与变价价格 (#1347)
接入聚水潭采购入库明细、固定剔除规则和工作平台最新变价后采购价。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-21 15:59:27 +08:00

160 lines
7.5 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\SupplierPayableForecastRepository;
use Tests\TestCase;
class SupplierPayableForecastRepositoryTest extends TestCase
{
public function test_goods_details_apply_fixed_exclusions_and_latest_change_price(): void
{
$this->createTables();
$this->seedRows();
$result = app(SupplierPayableForecastRepository::class)->goodsDetails([
'date_from' => '2026-10-01',
'date_to' => '',
'keyword' => '',
], 1);
$this->assertSame(2, $result['total']);
$this->assertSame(3.0, $result['quantity']);
$this->assertSame(2, $result['supplier_count']);
$this->assertSame(['SKU-B', 'SKU-A'], array_column($result['rows'], 'productCode'));
$priced = collect($result['rows'])->firstWhere('productCode', 'SKU-A');
$this->assertSame('STYLE-A', $priced['skuCode']);
$this->assertSame(2.0, $priced['qty']);
$this->assertSame(12.0, $priced['unitPrice']);
$this->assertSame(24.0, $priced['amount']);
$this->assertSame(10.0, $priced['guessUnitPrice']);
$this->assertSame(20.0, $priced['guessAmount']);
$this->assertTrue($priced['priceMatched']);
$unpriced = collect($result['rows'])->firstWhere('productCode', 'SKU-B');
$this->assertSame(0.0, $unpriced['unitPrice']);
$this->assertSame(0.0, $unpriced['amount']);
$this->assertSame(6.0, $unpriced['guessAmount']);
$this->assertFalse($unpriced['priceMatched']);
$keyword = app(SupplierPayableForecastRepository::class)->goodsDetails([
'date_from' => '2026-10-01',
'date_to' => '',
'keyword' => 'SKU-A',
], 1);
$this->assertSame(1, $keyword['total']);
$this->assertSame('SKU-A', $keyword['rows'][0]['productCode']);
}
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->text('remark')->nullable();
$table->string('warehouse', 255)->nullable();
$table->string('logistics_company', 255)->nullable();
$table->string('status', 50)->nullable();
$table->string('supplier_name', 255)->nullable();
$table->dateTime('created')->nullable();
$table->dateTime('updateTime')->nullable();
});
$schema->create(SupplierPayableForecastRepository::ITEM_TABLE, function (Blueprint $table): void {
$table->bigIncrements('Id');
$table->string('io_id', 100);
$table->string('ioi_id', 100)->nullable();
$table->string('sku_id', 100)->nullable();
$table->string('i_id', 100)->nullable();
$table->string('name', 255)->nullable();
$table->integer('qty')->nullable();
$table->decimal('cost_price', 18, 2)->nullable();
$table->decimal('cost_amount', 18, 2)->nullable();
$table->text('remark')->nullable();
$table->decimal('tax_rate', 8, 2)->nullable();
$table->string('unit', 50)->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();
});
}
private function seedRows(): 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],
]);
$heads = [
[1, 'IN-1', 101, '2026-10-02 09:00:00', '正常仓', '供应商甲'],
[2, 'IN-2', 102, '2026-10-03 09:00:00', '正常仓', '供应商乙'],
[3, 'IN-OLD', 103, '2026-09-30 09:00:00', '正常仓', '供应商甲'],
[4, 'IN-ACCESSORY', 104, '2026-10-04 09:00:00', '配件单仓', '供应商甲'],
[5, 'IN-BLANK-PO', null, '2026-10-05 09:00:00', '正常仓', '供应商甲'],
[6, 'IN-VIRTUAL', 106, '2026-10-06 09:00:00', '正常仓', '虚拟'],
[7, 'IN-DELIVERY', 107, '2026-10-07 09:00:00', '正常仓', '简尚送货'],
[8, 'IN-DF', 108, '2026-10-08 09:00:00', '正常仓', '供应商甲'],
];
foreach ($heads as [$id, $ioId, $poId, $date, $warehouse, $supplier]) {
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::HEAD_TABLE)->insert([
'Id' => $id,
'io_id' => $ioId,
'po_id' => $poId,
'io_date' => $date,
'warehouse' => $warehouse,
'supplier_name' => $supplier,
]);
}
$items = [
[1, 'IN-1', 'SKU-A', 'STYLE-A', 2, 10],
[2, 'IN-2', 'SKU-B', 'STYLE-B', 1, 6],
[3, 'IN-OLD', 'SKU-A', 'STYLE-A', 9, 10],
[4, 'IN-ACCESSORY', 'SKU-C', 'STYLE-C', 1, 10],
[5, 'IN-BLANK-PO', 'SKU-D', 'STYLE-D', 1, 10],
[6, 'IN-VIRTUAL', 'SKU-E', 'STYLE-E', 1, 10],
[7, 'IN-DELIVERY', 'SKU-F', 'STYLE-F', 1, 10],
[8, 'IN-DF', 'DF-SKU', 'DF-STYLE', 1, 10],
];
foreach ($items as [$id, $ioId, $skuId, $styleId, $qty, $costPrice]) {
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::ITEM_TABLE)->insert([
'Id' => $id,
'io_id' => $ioId,
'ioi_id' => 'ITEM-' . $id,
'sku_id' => $skuId,
'i_id' => $styleId,
'name' => $styleId . ' 商品',
'qty' => $qty,
'cost_price' => $costPrice,
'cost_amount' => $qty * $costPrice,
]);
}
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::PRICE_TABLE)->insert([
['Id' => 1, 'skucode' => 'SKU-A', 'priceType' => '1', 'afterPrice' => 8, 'effectStartTime' => '2026-09-01 00:00:00'],
['Id' => 2, 'skucode' => 'SKU-A', 'priceType' => '1', 'afterPrice' => 12, 'effectStartTime' => '2026-10-10 00:00:00'],
['Id' => 3, 'skucode' => 'SKU-A', 'priceType' => '0', 'afterPrice' => 99, 'effectStartTime' => '2026-10-12 00:00:00'],
]);
}
}