438 lines
20 KiB
PHP
438 lines
20 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(24.0, $result['amount']);
|
|
$this->assertSame(20.8, $result['estimated_amount_after_discount']);
|
|
$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('供应商甲', $priced['supplier']);
|
|
$this->assertSame('2026-10', $priced['summary_month']);
|
|
$this->assertSame('供应商甲SKU-A', $priced['supplierGoodsCode']);
|
|
$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(2.0, $keyword['quantity']);
|
|
$this->assertSame(24.0, $keyword['amount']);
|
|
$this->assertSame(16.0, $keyword['estimated_amount_after_discount']);
|
|
$this->assertSame('SKU-A', $keyword['rows'][0]['productCode']);
|
|
|
|
$exportRows = iterator_to_array(app(SupplierPayableForecastRepository::class)->goodsRowsForExport([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
], 1));
|
|
$this->assertCount(2, $exportRows);
|
|
$this->assertSame(['SKU-B', 'SKU-A'], array_column($exportRows, 'productCode'));
|
|
$this->assertSame('2026-10', $exportRows[1]['summary_month']);
|
|
$this->assertSame('供应商甲SKU-A', $exportRows[1]['supplierGoodsCode']);
|
|
}
|
|
|
|
public function test_goods_date_filters_are_inclusive_to_the_second_and_keep_legacy_date_ranges(): void
|
|
{
|
|
$this->createTables();
|
|
$this->seedRows();
|
|
$repository = app(SupplierPayableForecastRepository::class);
|
|
|
|
$exact = $repository->goodsDetails([
|
|
'date_from' => '2026-10-02 09:00:00',
|
|
'date_to' => '2026-10-02 09:00:00',
|
|
'keyword' => '',
|
|
], 1);
|
|
$this->assertSame(1, $exact['total']);
|
|
$this->assertSame('IN-1', $exact['rows'][0]['stockNo']);
|
|
$this->assertSame('2026-10-02 09:00:00', $exact['rows'][0]['date']);
|
|
|
|
$afterBoundary = $repository->goodsDetails([
|
|
'date_from' => '2026-10-02 09:00:01',
|
|
'date_to' => '2026-10-02 09:00:02',
|
|
'keyword' => '',
|
|
], 1);
|
|
$this->assertSame(0, $afterBoundary['total']);
|
|
|
|
$legacy = $repository->goodsDetails([
|
|
'date_from' => '2026-10-02',
|
|
'date_to' => '2026-10-02',
|
|
'keyword' => '',
|
|
], 1);
|
|
$this->assertSame(1, $legacy['total']);
|
|
$this->assertSame('IN-1', $legacy['rows'][0]['stockNo']);
|
|
}
|
|
|
|
public function test_goods_details_fall_back_to_current_pricing_when_no_change_exists(): void
|
|
{
|
|
$this->createTables();
|
|
$this->seedRows();
|
|
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::CURRENT_PRICE_TABLE)->insert([
|
|
['Id' => 11, 'skuCode' => 'SKU-A', 'purchasePrice' => 99, 'effectTime' => '2026-09-01 00:00:00'],
|
|
['Id' => 12, 'skuCode' => 'SKU-B', 'purchasePrice' => 7, 'effectTime' => '2026-09-01 00:00:00'],
|
|
['Id' => 13, 'skuCode' => 'SKU-B', 'purchasePrice' => 88, 'effectTime' => '2027-01-01 00:00:00'],
|
|
]);
|
|
|
|
$result = app(SupplierPayableForecastRepository::class)->goodsDetails([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
], 1);
|
|
|
|
$this->assertSame(31.0, $result['amount']);
|
|
$changed = collect($result['rows'])->firstWhere('productCode', 'SKU-A');
|
|
$this->assertSame(12.0, $changed['unitPrice']);
|
|
$this->assertSame('change', $changed['priceSource']);
|
|
$this->assertSame('2026-10-10 00:00:00', $changed['priceEffectTime']);
|
|
|
|
$priced = collect($result['rows'])->firstWhere('productCode', 'SKU-B');
|
|
$this->assertSame(7.0, $priced['unitPrice']);
|
|
$this->assertSame(7.0, $priced['amount']);
|
|
$this->assertSame('pricing', $priced['priceSource']);
|
|
$this->assertSame('2026-09-01 00:00:00', $priced['priceEffectTime']);
|
|
$this->assertTrue($priced['priceMatched']);
|
|
|
|
$exportRows = iterator_to_array(app(SupplierPayableForecastRepository::class)->goodsRowsForExport([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
], 1));
|
|
$exportPriced = collect($exportRows)->firstWhere('productCode', 'SKU-B');
|
|
$this->assertSame(7.0, $exportPriced['unitPrice']);
|
|
$this->assertSame(7.0, $exportPriced['amount']);
|
|
}
|
|
|
|
public function test_goods_details_merge_dropshipping_shipping_rows_with_payable_columns(): void
|
|
{
|
|
$this->createTables();
|
|
DB::connection('business_mirror')->table(SupplierPayableForecastRepository::PRICE_TABLE)->insert([
|
|
'Id' => 10,
|
|
'skucode' => 'SKU-SHIP',
|
|
'priceType' => '1',
|
|
'afterPrice' => 15,
|
|
'effectStartTime' => '2026-10-01 08:00:00',
|
|
]);
|
|
foreach ([
|
|
[
|
|
'id' => 1,
|
|
'source_id' => 9001,
|
|
'sku_source_id' => 101,
|
|
'ship_time' => '2026-10-04 10:00:00',
|
|
'warehouse_name' => '工厂-河北雅柏代发仓',
|
|
'supplier_name' => '代发供应商',
|
|
'barcode' => 'SKU-SHIP',
|
|
'product_code' => 'STYLE-SHIP',
|
|
'product_name' => '代发商品',
|
|
'qty' => 3,
|
|
'express_name1' => '代发快递',
|
|
'express_no' => 'SF123456',
|
|
'seller_memo' => '代发备注',
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'source_id' => 9002,
|
|
'sku_source_id' => 102,
|
|
'ship_time' => '2026-10-05 10:00:00',
|
|
'warehouse_name' => '简尚-崔桥成品仓',
|
|
'supplier_name' => '普通仓供应商',
|
|
'barcode' => 'SKU-NORMAL',
|
|
'product_code' => 'STYLE-NORMAL',
|
|
'product_name' => '普通仓商品',
|
|
'qty' => 9,
|
|
],
|
|
[
|
|
'id' => 3,
|
|
'source_id' => 9003,
|
|
'sku_source_id' => 103,
|
|
'ship_time' => '2026-10-06 10:00:00',
|
|
'warehouse_name' => '工厂-山东彩艺代发仓',
|
|
'supplier_name' => '剔除供应商',
|
|
'barcode' => 'DF-SHOULD-NOT-APPEAR',
|
|
'product_code' => 'STYLE-DF',
|
|
'product_name' => 'DF 商品',
|
|
'qty' => 7,
|
|
],
|
|
] as $shippingRow) {
|
|
DB::connection('business_mirror')
|
|
->table(SupplierPayableForecastRepository::SHIPPING_TABLE)
|
|
->insert($shippingRow);
|
|
}
|
|
|
|
$result = app(SupplierPayableForecastRepository::class)->goodsDetails([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
], 1);
|
|
|
|
$this->assertSame(1, $result['total']);
|
|
$this->assertSame(3.0, $result['quantity']);
|
|
$this->assertSame(45.0, $result['amount']);
|
|
$this->assertSame(0.0, $result['estimated_amount_after_discount']);
|
|
$this->assertSame(1, $result['supplier_count']);
|
|
|
|
$row = $result['rows'][0];
|
|
$this->assertSame('shipping', $row['sourceType']);
|
|
$this->assertSame('2026-10', $row['summary_month']);
|
|
$this->assertSame('', $row['stockNo']);
|
|
$this->assertSame('', $row['purchaseNo']);
|
|
$this->assertSame('2026-10-04 10:00:00', $row['date']);
|
|
$this->assertSame('代发供应商', $row['supplier']);
|
|
$this->assertSame('STYLE-SHIP', $row['skuCode']);
|
|
$this->assertSame('SKU-SHIP', $row['productCode']);
|
|
$this->assertSame('代发商品', $row['skuName']);
|
|
$this->assertSame(3.0, $row['qty']);
|
|
$this->assertSame(15.0, $row['unitPrice']);
|
|
$this->assertSame(45.0, $row['amount']);
|
|
$this->assertNull($row['guessUnitPrice']);
|
|
$this->assertNull($row['guessAmount']);
|
|
$this->assertSame('代发供应商SKU-SHIP', $row['supplierGoodsCode']);
|
|
$this->assertSame('代发备注', $row['remark']);
|
|
$this->assertSame('工厂-河北雅柏代发仓', $row['warehouse']);
|
|
$this->assertSame('代发快递', $row['expressName']);
|
|
$this->assertSame('SF123456', $row['expressNo']);
|
|
$this->assertTrue($row['priceMatched']);
|
|
|
|
$keyword = app(SupplierPayableForecastRepository::class)->goodsDetails([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => 'STYLE-SHIP',
|
|
], 1);
|
|
$this->assertSame(1, $keyword['total']);
|
|
$this->assertSame('shipping', $keyword['rows'][0]['sourceType']);
|
|
|
|
$exportRows = iterator_to_array(app(SupplierPayableForecastRepository::class)->goodsRowsForExport([
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
], 1));
|
|
$this->assertCount(1, $exportRows);
|
|
$this->assertSame('SKU-SHIP', $exportRows[0]['productCode']);
|
|
$this->assertSame('代发供应商SKU-SHIP', $exportRows[0]['supplierGoodsCode']);
|
|
}
|
|
|
|
public function test_goods_details_and_export_merge_both_sources_without_skipping_or_duplicating_rows(): void
|
|
{
|
|
$this->createTables();
|
|
$this->seedRows();
|
|
foreach ([
|
|
[
|
|
'id' => 11,
|
|
'source_id' => 5011,
|
|
'sku_source_id' => 5011,
|
|
'ship_time' => '2026-10-04 09:00:00',
|
|
'warehouse_name' => '工厂-山东彩艺代发仓',
|
|
'supplier_name' => '供应商甲',
|
|
'barcode' => 'SKU-A',
|
|
'product_code' => 'STYLE-A',
|
|
'product_name' => '代发商品甲',
|
|
'qty' => 1,
|
|
],
|
|
[
|
|
'id' => 12,
|
|
'source_id' => 5012,
|
|
'sku_source_id' => 5012,
|
|
'ship_time' => '2026-10-01 09:00:00',
|
|
'warehouse_name' => '工厂-河北雅柏代发仓',
|
|
'supplier_name' => '供应商甲',
|
|
'barcode' => 'SKU-A',
|
|
'product_code' => 'STYLE-A',
|
|
'product_name' => '代发商品乙',
|
|
'qty' => 2,
|
|
],
|
|
] as $shippingRow) {
|
|
DB::connection('business_mirror')
|
|
->table(SupplierPayableForecastRepository::SHIPPING_TABLE)
|
|
->insert($shippingRow);
|
|
}
|
|
|
|
$repository = app(SupplierPayableForecastRepository::class);
|
|
$filters = [
|
|
'date_from' => '2026-10-01 00:00:00',
|
|
'date_to' => '',
|
|
'keyword' => '',
|
|
];
|
|
$first = $repository->goodsDetails($filters, 1, 2);
|
|
$second = $repository->goodsDetails($filters, 2, 2);
|
|
|
|
$this->assertSame(4, $first['total']);
|
|
$this->assertSame(['2026-10-04 09:00:00', '2026-10-03 09:00:00'], array_column($first['rows'], 'date'));
|
|
$this->assertSame(['2026-10-02 09:00:00', '2026-10-01 09:00:00'], array_column($second['rows'], 'date'));
|
|
$this->assertSame(
|
|
['2026-10-04 09:00:00', '2026-10-03 09:00:00', '2026-10-02 09:00:00', '2026-10-01 09:00:00'],
|
|
array_column(iterator_to_array($repository->goodsRowsForExport($filters, 1)), 'date'),
|
|
);
|
|
}
|
|
|
|
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();
|
|
});
|
|
if (! $schema->hasTable(SupplierPayableForecastRepository::CURRENT_PRICE_TABLE)) {
|
|
$schema->create(SupplierPayableForecastRepository::CURRENT_PRICE_TABLE, function (Blueprint $table): void {
|
|
$table->bigIncrements('Id');
|
|
$table->string('skuCode', 100)->nullable();
|
|
$table->decimal('purchasePrice', 18, 2)->nullable();
|
|
$table->dateTime('effectTime')->nullable();
|
|
});
|
|
}
|
|
if (! $schema->hasTable(SupplierPayableForecastRepository::SHIPPING_TABLE)) {
|
|
$schema->create(SupplierPayableForecastRepository::SHIPPING_TABLE, function (Blueprint $table): void {
|
|
$table->bigIncrements('id');
|
|
$table->unsignedBigInteger('source_id')->nullable();
|
|
$table->unsignedBigInteger('sku_source_id')->nullable();
|
|
$table->string('express_no', 64)->nullable();
|
|
$table->string('trade_no', 64)->nullable();
|
|
$table->string('barcode', 128)->nullable();
|
|
$table->string('product_name', 512)->nullable();
|
|
$table->integer('qty')->nullable();
|
|
$table->string('express_name1', 128)->nullable();
|
|
$table->string('express_name2', 128)->nullable();
|
|
$table->string('warehouse_name', 128)->nullable();
|
|
$table->dateTime('ship_time')->nullable();
|
|
$table->string('supplier_name', 128)->nullable();
|
|
$table->longText('seller_memo')->nullable();
|
|
$table->decimal('amount', 18, 2)->nullable();
|
|
$table->string('product_code', 128)->nullable();
|
|
$table->string('package_no', 64)->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' => $ioId === 'IN-2' ? null : $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'],
|
|
]);
|
|
}
|
|
}
|