158 lines
10 KiB
PHP
158 lines
10 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\SupplierBookkeepingValuationRepository as Repository;
|
||
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
|
||
use Tests\TestCase;
|
||
|
||
class SupplierBookkeepingValuationRepositoryTest extends TestCase
|
||
{
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
config(['jushuitan.connection' => 'business_mirror', 'upstream_mirrors.connection' => 'business_mirror']);
|
||
// The production dataset key contains dots; configure the full dataset map directly.
|
||
config(['upstream_mirrors.datasets' => [ProductCatalogMirrorRepository::DATASET_KEY => [
|
||
'source_key' => 'operations_replica', 'schema' => 'rpt', 'object' => 'jst_sku',
|
||
'target' => 'fixture_valuation_skus', 'keys' => ['autoid'],
|
||
]]]);
|
||
$schema = Schema::connection('business_mirror');
|
||
if (!$schema->hasTable('supplier_bookkeeping_shipping_months')) {
|
||
(require base_path('database/migrations/2026_11_07_100000_create_supplier_bookkeeping_shipping_mirror.php'))->up();
|
||
}
|
||
$schema->create('fixture_valuation_skus', function (Blueprint $table) {
|
||
$table->integer('autoid');
|
||
foreach (['_mirror_key', 'sku_id', 'sku_type', 'cost_price'] as $column) $table->string($column)->nullable();
|
||
});
|
||
if (!$schema->hasTable('business_mirror_table_states')) {
|
||
$schema->create('business_mirror_table_states', function (Blueprint $table) {
|
||
$table->string('dataset_key')->primary();
|
||
$table->dateTime('initial_completed_at')->nullable();
|
||
});
|
||
}
|
||
DB::connection('business_mirror')->table('business_mirror_table_states')->updateOrInsert(
|
||
['dataset_key' => ProductCatalogMirrorRepository::DATASET_KEY], ['initial_completed_at' => '2026-10-01 00:00:00']);
|
||
}
|
||
|
||
public function test_queries_exact_references_and_six_month_dates_without_borrowing_same_order_packages(): void
|
||
{
|
||
$db = DB::connection('business_mirror');
|
||
$db->table('supplier_bookkeeping_shipping_months')->insert(['source_month' => '2026-10-01', 'active_generation' => 'active']);
|
||
foreach ([['EXP-1', '2026-05-01'], ['EXP-1', '2026-04-30'], ['EXP-1', '2026-11-01'], ['EXP-2', '2026-10-20']] as $i => [$tracking, $date]) {
|
||
$db->table('supplier_bookkeeping_shipping_rows')->insert(['generation' => 'active', 'source_row' => $i+2,
|
||
'express_no' => $tracking, 'old_express_no' => 'OLD-'.$tracking, 'trade_no' => 'ORDER-1', 'package_no' => '', 'unshipped' => '',
|
||
'barcode' => 'SKU-A', 'qty' => 1, 'freight' => '8.50', 'ship_time' => $date . ' 00:00:00']);
|
||
foreach (['tracking' => [$tracking, 'OLD-'.$tracking], 'order' => ['ORDER-1']] as $kind => $refs) foreach ($refs as $ref) {
|
||
$db->table('supplier_bookkeeping_shipping_refs')->insert(['generation' => 'active', 'source_row' => $i+2, 'kind' => $kind, 'reference' => $ref]);
|
||
}
|
||
}
|
||
$repo = app(Repository::class);
|
||
$rows = $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => 'EXP-1', 'online_order_no' => 'ORDER-1']]);
|
||
$this->assertCount(1, $rows);
|
||
$this->assertSame('EXP-1', $rows[0]['express_no']);
|
||
$this->assertCount(1, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => 'OLD-EXP-1']]));
|
||
$this->assertCount(1, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => " '@EXP-1 ", 'online_order_no' => 'ORDER-1']]));
|
||
$this->assertSame(['2026-10'], $repo->readyMonths());
|
||
$all = $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => '', 'online_order_no' => 'ORDER-1']]);
|
||
$this->assertCount(2, $all);
|
||
$this->assertCount(2, $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => " '@ ", 'online_order_no' => '@ORDER-1’']]));
|
||
$this->assertSame([], $repo->shipmentsFor([['started_at' => '2026-10-15', 'tracking_no' => '', 'online_order_no' => '']]));
|
||
}
|
||
|
||
private function syncedShipping(): void
|
||
{
|
||
$schema = Schema::connection('business_mirror');
|
||
// CreatesApplication 强制使用内存 SQLite;替换迁移已有表为本测试的最少字段 fixture。
|
||
foreach (['jst_oms_shipping', 'jst_sync_state', 'courier_unshipped_waybills'] as $table) $schema->dropIfExists($table);
|
||
$schema->create('jst_oms_shipping', function (Blueprint $table) {
|
||
$table->integer('id');
|
||
$table->integer('source_id');
|
||
foreach (['express_no', 'front_no', 'trade_no', 'barcode', 'qty', 'freight', 'ship_time', 'pkg_count'] as $column) $table->string($column)->nullable();
|
||
});
|
||
$schema->create('jst_sync_state', function (Blueprint $table) {
|
||
$table->string('dataset_key');
|
||
$table->dateTime('last_succeeded_at')->nullable();
|
||
});
|
||
DB::connection('business_mirror')->table('jst_sync_state')->insert(['dataset_key' => 'jst.sctrade', 'last_succeeded_at' => '2026-11-20']);
|
||
if (!$schema->hasTable('courier_unshipped_waybills')) {
|
||
$schema->create('courier_unshipped_waybills', function (Blueprint $table) {
|
||
$table->string('waybill_no');
|
||
$table->string('source');
|
||
});
|
||
}
|
||
}
|
||
|
||
public function test_missing_month_reads_exact_synced_tracking_and_replacement_without_borrowing_other_parcels(): void
|
||
{
|
||
$this->syncedShipping();
|
||
$db = DB::connection('business_mirror');
|
||
foreach ([['EXP-NEW', 'EXP-OLD', '2026-11-02'], ['EXP-OTHER', '', '2026-11-02'], ['EXP-NEW', '', '2026-05-31'], ['EXP-NEW', '', '2026-12-01']] as $i => [$tracking, $old, $date]) {
|
||
$db->table('jst_oms_shipping')->insert(['id' => $i+1, 'source_id' => $i+1, 'express_no' => $tracking, 'front_no' => $old,
|
||
'trade_no' => 'ORDER-1', 'barcode' => 'SKU-A', 'qty' => '2', 'freight' => '8.50', 'ship_time' => $date . ' 00:00:00', 'pkg_count' => '1/1']);
|
||
}
|
||
$repo = app(Repository::class);
|
||
foreach (['EXP-NEW', " '@EXP-OLD "] as $tracking) {
|
||
$rows = $repo->shipmentsFor([['started_at' => '2026-11-15', 'tracking_no' => $tracking, 'online_order_no' => 'ORDER-1']]);
|
||
$this->assertCount(1, $rows);
|
||
$this->assertSame('synced', $rows[0]['valuation_source']);
|
||
$this->assertSame(['EXP-NEW', 'EXP-OLD'], $rows[0]['tracking_numbers']);
|
||
$this->assertSame('8.50', $rows[0]['freight']);
|
||
$this->assertArrayNotHasKey('valuation_error', $rows[0]);
|
||
}
|
||
$this->assertSame([], $repo->shipmentsFor([['started_at' => '2026-11-15', 'tracking_no' => 'MISSING', 'online_order_no' => 'ORDER-1']]));
|
||
$this->assertSame([], $repo->shipmentsFor([['started_at' => '2026-11-15', 'tracking_no' => '', 'online_order_no' => 'ORDER-1']]));
|
||
$db->table('jst_sync_state')->update(['last_succeeded_at' => null]);
|
||
$this->assertSame([], $repo->shipmentsFor([['started_at' => '2026-11-15', 'tracking_no' => 'EXP-NEW']]));
|
||
}
|
||
|
||
public function test_synced_packages_keep_unshipped_and_incomplete_detail_evidence(): void
|
||
{
|
||
$this->syncedShipping();
|
||
$db = DB::connection('business_mirror');
|
||
$db->table('jst_oms_shipping')->insert(['id' => 1, 'source_id' => 1, 'express_no' => 'EXP-1', 'trade_no' => 'ORDER-1',
|
||
'barcode' => 'SKU-A', 'qty' => '2', 'freight' => '8.50', 'ship_time' => '2026-11-02', 'pkg_count' => '1/2']);
|
||
$db->table('courier_unshipped_waybills')->insert(['source' => 'warehouse', 'waybill_no' => 'EXP-1']);
|
||
$rows = app(Repository::class)->shipmentsFor([['started_at' => '2026-11-15', 'tracking_no' => 'EXP-1']]);
|
||
$this->assertSame('EXP-1', $rows[0]['unshipped']);
|
||
$this->assertStringContainsString('尚未齐全', $rows[0]['valuation_error']);
|
||
}
|
||
|
||
public function test_uses_latest_normal_product_cost_and_decodes_sku_plus_without_treating_missing_as_zero(): void
|
||
{
|
||
DB::connection('business_mirror')->table('fixture_valuation_skus')->insert([
|
||
['autoid' => 1, '_mirror_key' => '1', 'sku_id' => 'SKU-A', 'sku_type' => 'normal', 'cost_price' => '10.00'],
|
||
['autoid' => 2, '_mirror_key' => '2', 'sku_id' => 'SKU-A', 'sku_type' => 'normal', 'cost_price' => '20.00'],
|
||
['autoid' => 3, '_mirror_key' => '3', 'sku_id' => 'SKU-A', 'sku_type' => 'combination', 'cost_price' => '90.00'],
|
||
['autoid' => 4, '_mirror_key' => '4', 'sku_id' => 'SKU_X002B_B', 'sku_type' => 'normal', 'cost_price' => '15.00'],
|
||
]);
|
||
$costs = app(Repository::class)->costsFor([" '@sku-a ", '@SKU+B’', 'MISSING']);
|
||
$this->assertSame('20.00', $costs['SKU-A']);
|
||
$this->assertSame('15.00', $costs['SKU+B']);
|
||
$this->assertArrayNotHasKey('MISSING', $costs);
|
||
}
|
||
|
||
public function test_cleans_only_outer_copy_symbols_and_preserves_identifier_content(): void
|
||
{
|
||
$this->assertSame(['JDVB001', '0001234567890123456789', 'SKU-A+B', 'A@B'], Repository::numbers("\u{FEFF}'@JDVB001’ , '@0001234567890123456789";\u{200B}@SKU-A+B\u{00A0}\nA@B"));
|
||
$this->assertSame('SKU-A+B', Repository::sku(" ’@sku-a_x002b_b' "));
|
||
$this->assertSame([], Repository::numbers(" '@,@’ "));
|
||
}
|
||
|
||
public function test_latest_cost_wins_when_encoded_sku_variant_falls_in_another_query_batch(): void
|
||
{
|
||
DB::connection('business_mirror')->table('fixture_valuation_skus')->insert([
|
||
['autoid' => 1, '_mirror_key' => '1', 'sku_id' => 'SKU+B', 'sku_type' => 'normal', 'cost_price' => '10.00'],
|
||
['autoid' => 2, '_mirror_key' => '2', 'sku_id' => 'SKU_X002B_B', 'sku_type' => 'normal', 'cost_price' => '25.00'],
|
||
]);
|
||
// 74 pairs + one uppercase-only numeric SKU place SKU+B at the 150-value boundary.
|
||
$skus = array_map(static fn($i) => 'ITEM-'.$i, range(1, 74));
|
||
$skus[] = '123';
|
||
$skus[] = 'SKU+B';
|
||
$this->assertSame('25.00', app(Repository::class)->costsFor($skus)['SKU+B']);
|
||
}
|
||
}
|