CaiWuXiTong/tests/Unit/Business/KukaReconciliationPageDataTest.php
chenxi 2ea6326bdf fix(顾家对账): 修复数字订单号收货匹配索引失效 (#1279)
数字订单号保持字符串绑定,避免 varchar 索引隐式数值转换;补充回归测试。
2026-11-19 17:20:57 +08:00

262 lines
11 KiB
PHP

<?php
namespace Tests\Unit\Business;
use App\Business\KukaPostingReconciliation\KukaReconciliationPageData;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class KukaReconciliationPageDataTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config([
'database.connections.business_mirror' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'upstream_mirrors.connection' => 'business_mirror',
'jushuitan.connection' => 'business_mirror',
]);
DB::purge('business_mirror');
$schema = Schema::connection('business_mirror');
$schema->create('kuka_posting_base_imports', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->unsignedBigInteger('company_id');
$table->char('period', 7);
$table->timestamp('completed_at')->nullable();
});
$schema->create('kuka_posting_base_rows', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->unsignedBigInteger('company_id');
$table->char('period', 7);
$table->longText('source_values');
$table->unsignedInteger('source_row_number');
});
$schema->create('kuka_posting_quotation_rows', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->unsignedBigInteger('company_id');
$table->string('material_number');
$table->string('supplier_account')->nullable();
$table->string('supplier_name')->nullable();
$table->string('record_status')->nullable();
});
$schema->create('jst_oms_shipping', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('trade_no')->nullable();
$table->string('barcode')->nullable();
$table->string('product_code')->nullable();
$table->string('receiver_name')->nullable();
$table->string('receiver_state')->nullable();
$table->string('receiver_city')->nullable();
$table->string('receiver_district')->nullable();
$table->longText('receiver_address')->nullable();
$table->string('receiver_mobile')->nullable();
$table->string('receiver_phone')->nullable();
$table->string('buyer_nick')->nullable();
$table->longText('buyer_memo')->nullable();
$table->longText('seller_memo')->nullable();
$table->string('express_no')->nullable();
$table->string('express_name1')->nullable();
$table->decimal('freight', 18, 2)->nullable();
});
}
public function test_builds_rule_based_rows_for_each_reconciliation_view(): void
{
$connection = DB::connection('business_mirror');
$connection->enableQueryLog();
$numericOrderNo = '3317157192129049568';
$connection->table('kuka_posting_base_imports')->insert([
'company_id' => 1,
'period' => '2026-10',
'completed_at' => '2026-11-01 08:00:00',
]);
$this->insertBaseRow(2, $this->payload($numericOrderNo, 'P-1', 'S-1', 'I-1', 'M-1', '2', '10.00'));
$this->insertBaseRow(3, $this->payload('O-2', 'P-2', 'S-2', 'I-2', 'M-1', '1', '10.00'));
$this->insertBaseRow(4, $this->payload('O-3', 'P-3', 'S-3', 'I-3', 'M-2', '', ''));
$this->insertBaseRow(5, $this->payload('O-4', 'P-4', 'S-4', 'I-4', '', '1', '10.00'));
$connection->table('kuka_posting_quotation_rows')->insert([
'company_id' => 1,
'material_number' => 'M-1',
'supplier_account' => '435421',
'supplier_name' => '常州欧伯娜家居有限公司',
'record_status' => '生效',
]);
$connection->table('jst_oms_shipping')->insert([
[
'trade_no' => $numericOrderNo,
'barcode' => 'P-1',
'product_code' => 'S-1',
'receiver_name' => '张三',
'receiver_state' => '江苏省',
'receiver_city' => '常州市',
'receiver_district' => '武进区',
'receiver_address' => '江苏省常州市武进区测试路1号',
'receiver_mobile' => '13800000000',
'receiver_phone' => '',
'buyer_nick' => 'buyer-1',
'buyer_memo' => '买家留言',
'seller_memo' => '卖家备注',
'express_no' => 'EXPRESS-1',
'express_name1' => '顺丰',
'freight' => '8.00',
],
[
'trade_no' => 'O-2',
'barcode' => 'P-2',
'product_code' => 'S-2',
'receiver_name' => '李四',
'receiver_state' => '浙江省',
'receiver_city' => '杭州市',
'receiver_district' => '西湖区',
'receiver_address' => '浙江省杭州市西湖区测试路2号',
'receiver_mobile' => '',
'receiver_phone' => '',
'buyer_nick' => 'buyer-2',
'buyer_memo' => '',
'seller_memo' => '',
'express_no' => 'EXPRESS-2',
'express_name1' => '中通',
'freight' => '6.00',
],
]);
$data = app(KukaReconciliationPageData::class)->build(
Request::create('/', 'GET', ['period' => '2026-10']),
1,
);
$this->assertNull($data['error']);
$this->assertSame(4, $data['summary']['source_rows']);
$this->assertSame(3, $data['summary']['valid_material_rows']);
$this->assertSame(2, $data['summary']['generated_rows']);
$this->assertSame(1, $data['summary']['material_count']);
$this->assertSame(1, $data['summary']['skipped_missing_material_rows']);
$this->assertSame(1, $data['summary']['skipped_zero_quantity_rows']);
$this->assertSame('30.00', $data['summary']['supplier_amount_total']);
$this->assertSame('33.33', $data['summary']['sales_amount_total']);
$this->assertSame(2, $data['summary']['shipping_matched_rows']);
$this->assertSame(1, $data['summary']['receiver_phone_rows']);
$shippingBindings = [];
foreach ($connection->getQueryLog() as $query) {
if (! str_contains((string) $query['query'], 'jst_oms_shipping')) {
continue;
}
$shippingBindings = array_merge($shippingBindings, $query['bindings']);
}
$this->assertContains($numericOrderNo, $shippingBindings, true);
$this->assertNotContains((int) $numericOrderNo, $shippingBindings, true);
$this->assertSame([[
'2026', '10', 'M-1', '测试物料', '3.0000', '10.00', '30.00',
]], $data['views']['posting-data']['rows']);
$this->assertSame(2, $data['views']['toc-orders']['total']);
$virtual = $data['views']['lingshan-virtual-posting']['rows'][0];
$this->assertSame('1', $virtual[2]);
$this->assertSame('M-1', $virtual[3]);
$this->assertSame('20.00', $virtual[7]);
$this->assertSame('11.11', $virtual[8]);
$this->assertSame('22.22', $virtual[9]);
$this->assertSame('常州欧伯娜家居有限公司', $virtual[10]);
$this->assertSame('435421', $virtual[11]);
$this->assertSame($numericOrderNo, $virtual[12]);
$this->assertSame('INTERNAL-8', $virtual[13]);
$this->assertSame('EXPRESS-1', $virtual[14]);
$erp = $data['views']['erp-product-import']['rows'][0];
$this->assertSame('13800000000', $erp[18]);
$this->assertSame('江苏省常州市武进区测试路1号', $erp[25]);
$this->assertSame('顺丰', $erp[26]);
$this->assertSame('8.00', $erp[27]);
$this->assertSame('buyer-1', $erp[31]);
$invoice = $data['views']['invoice-entry-template']['rows'][0];
$this->assertSame('宁波顾家望业家居有限公司', $invoice[2]);
$this->assertSame('1050201010000000000', $invoice[7]);
$this->assertSame('22.22', $invoice[10]);
$this->assertSame('0.13', $invoice[11]);
$inventory = $data['views']['inventory-management-template']['rows'][0];
$this->assertSame('435421', $inventory[2]);
$this->assertSame('OA01207289', $inventory[3]);
$this->assertSame('M-1', $inventory[5]);
$this->assertSame('3.0000', $inventory[7]);
$this->assertSame('3.0000', $inventory[8]);
$this->assertSame('3.0000', $inventory[27]);
}
/**
* @return array<string, string>
*/
private function payload(
string $orderNo,
string $productCode,
string $styleCode,
string $itemNumber,
string $materialNumber,
string $quantity,
string $cost,
): array {
return [
'内部订单号' => 'INTERNAL-'.substr($orderNo, -1),
'线上订单号' => $orderNo,
'原始线上订单号' => $orderNo,
'订单状态' => '已发货',
'发货仓' => '测试仓',
'店铺' => '天猫-HX旗舰店101',
'买家账号' => 'buyer-'.substr($orderNo, -1),
'卖家备注' => '原卖家备注',
'订单日期' => '2026-10-31 10:00:00',
'付款日期' => '2026-10-31 10:05:00',
'发货日期' => '2026-10-31 18:00:00',
'省' => '江苏省',
'市' => '常州市',
'区县' => '武进区',
'订单快递公司' => '原快递',
'订单快递单号' => 'ORIGINAL-EXPRESS',
'收货人' => '原收货人',
'商品编码' => $productCode,
'款式编码' => $styleCode,
'货号' => $itemNumber,
'型号' => '测试物料',
'商品名称' => '测试商品',
'颜色规格' => '测试颜色',
'规格型号' => 'TEST-SPEC',
'材质' => '板材',
'业务员' => '业务员A',
'运费支出' => '5.00',
'物料编号' => $materialNumber,
'成本' => $cost,
'实发数量' => $quantity,
'发货金额' => $quantity === '' || $cost === ''
? ''
: (string) ((float) $quantity * (float) $cost),
];
}
/**
* @param array<string, string> $payload
*/
private function insertBaseRow(int $sourceRowNumber, array $payload): void
{
DB::connection('business_mirror')->table('kuka_posting_base_rows')->insert([
'company_id' => 1,
'period' => '2026-10',
'source_values' => json_encode(
$payload,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
),
'source_row_number' => $sourceRowNumber,
]);
}
}