106 lines
6.0 KiB
PHP
106 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace Tests\Unit\EcommerceFinance;
|
||
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Modules\EcommerceFinance\Repositories\PlantingReimbursementRepository as Repository;
|
||
use Tests\TestCase;
|
||
|
||
/** 取单与聚工单「种菜报销」视图一致:实付金额 − 任务付款金额 − 任务调整金额 = 0 才取;查单说明不在本批的原因。 */
|
||
class PlantingReimbursementPendingRowsTest extends TestCase
|
||
{
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
config([
|
||
'database.connections.business_mirror' => ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => ''],
|
||
'upstream_mirrors.connection' => 'business_mirror',
|
||
'upstream_mirrors.datasets' => [
|
||
Repository::DATASET => [
|
||
'source_key' => 'jiansun', 'schema' => 'dbo', 'object' => 'jst_workOrderInfo',
|
||
'target' => 'fixture_work_orders', 'keys' => ['Id'], 'watermark' => 'updateTime',
|
||
'indexes' => [['woTypeId', 'woNo', 'Id']],
|
||
],
|
||
],
|
||
]);
|
||
Cache::setDefaultDriver('array');
|
||
Cache::flush();
|
||
DB::purge('business_mirror');
|
||
$schema = Schema::connection('business_mirror');
|
||
$schema->create('business_mirror_table_states', function (Blueprint $table): void {
|
||
$table->string('dataset_key')->primary();
|
||
$table->string('status')->nullable();
|
||
$table->dateTime('initial_completed_at')->nullable();
|
||
$table->dateTime('last_succeeded_at')->nullable();
|
||
});
|
||
$schema->create('fixture_work_orders', function (Blueprint $table): void {
|
||
$table->integer('Id');
|
||
foreach (['woNo', 'woTypeId', 'status', 'createby', 'executors', 'completer', 'completeTime', 'created', 'updateTime', 'businessFieldList'] as $column) {
|
||
$table->text($column)->nullable();
|
||
}
|
||
});
|
||
DB::connection('business_mirror')->table('business_mirror_table_states')->insert([
|
||
'dataset_key' => Repository::DATASET, 'status' => 'succeeded',
|
||
'initial_completed_at' => '2026-11-01 00:00:00', 'last_succeeded_at' => '2026-12-01 17:00:00',
|
||
]);
|
||
}
|
||
|
||
private function record(int $id, string $woNo, string $order, string $paid, string $task, ?string $adjust = null, ?string $difference = null, string $status = 'WAIT_CONFIRM'): void
|
||
{
|
||
$fields = [
|
||
['fieldId' => 'orderField', 'type' => 'TEXT', 'value' => $order],
|
||
['fieldId' => 'shopField', 'type' => 'TEXT', 'value' => '天猫-卓禾家具旗舰店'],
|
||
['fieldId' => 'payDateField', 'type' => 'DATETIME', 'value' => '2026-11-27 10:00:00'],
|
||
['fieldId' => 'actualPaidAmountField', 'type' => 'NUMBER', 'value' => $paid],
|
||
['fieldId' => 'NUMBERField1413856620250924131615', 'type' => 'NUMBER', 'value' => $task],
|
||
['fieldId' => 'NUMBERField1413856620251117101250', 'type' => 'NUMBER', 'value' => $task],
|
||
['fieldId' => 'erpOrderStatus', 'type' => 'SELECT', 'value' => 'Sent'],
|
||
];
|
||
if ($adjust !== null) {
|
||
$fields[] = ['fieldId' => 'NUMBERField1413856620251118112538', 'type' => 'NUMBER', 'value' => $adjust];
|
||
}
|
||
if ($difference !== null) {
|
||
$fields[] = ['fieldId' => 'NUMBERField1413856620250924144207', 'type' => 'NUMBER', 'value' => $difference];
|
||
}
|
||
DB::connection('business_mirror')->table('fixture_work_orders')->insert([
|
||
'Id' => $id, 'woNo' => $woNo, 'woTypeId' => Repository::WORK_TYPE_ID, 'status' => $status,
|
||
'createby' => '宋文通', 'created' => '2026-11-27 12:00:00', 'updateTime' => '2026-11-27 12:00:00',
|
||
'businessFieldList' => json_encode($fields, JSON_UNESCAPED_UNICODE),
|
||
]);
|
||
}
|
||
|
||
public function test_only_rows_whose_paid_minus_task_minus_adjustment_is_zero_are_picked(): void
|
||
{
|
||
$this->record(1, 'GD1', '5127887268489023533', '130', '121.04'); // 差 8.96:聚工单待处理选不到
|
||
$this->record(2, 'GD2', '5127896736354004518', '45', '45'); // 0:取
|
||
$this->record(3, 'GD3', '3316609598157006272', '72.92', '63.18', '9.74'); // 调整后为 0:取
|
||
$this->record(4, 'GD4', '3316609598157000001', '50', '50', null, '1.00'); // 聚工单自带差额字段不为 0:不取
|
||
$this->record(5, 'GD5', '3316609598157000002', '50', '50', null, null, 'CONFIRMED');
|
||
|
||
$repository = app(Repository::class);
|
||
$rows = $repository->pendingRows('2026-11-24 00:00:00', '2026-11-30 23:59:59');
|
||
$this->assertSame(['GD2', 'GD3'], array_column($rows, 'work_order_no'));
|
||
$this->assertSame('9.74', $rows[1]['task_adjustment']);
|
||
|
||
$explained = $repository->explain('5127887268489023533', '2026-11-24 00:00:00', '2026-11-30 23:59:59');
|
||
$this->assertCount(1, $explained);
|
||
$this->assertSame('GD1', $explained[0]['work_order_no']);
|
||
$this->assertSame(['实付金额 − 任务付款 − 任务调整金额 = 8.96,不为 0 不取'], $explained[0]['reasons']);
|
||
|
||
$this->assertSame([], $repository->explain('GD2', '2026-11-24 00:00:00', '2026-11-30 23:59:59')[0]['reasons']);
|
||
$this->assertStringContainsString('已完成', $repository->explain('gd5', '2026-11-24 00:00:00', '2026-11-30 23:59:59')[0]['reasons'][0]);
|
||
$this->assertSame([], $repository->explain('9999', '2026-11-24 00:00:00', '2026-11-30 23:59:59'));
|
||
$this->assertSame([], $repository->explain("x' OR 1=1", '2026-11-24 00:00:00', '2026-11-30 23:59:59'));
|
||
}
|
||
|
||
public function test_lookup_rejects_partial_order_numbers(): void
|
||
{
|
||
$this->record(1, 'GD1', '123456789', '45', '45');
|
||
$this->record(2, 'GD2', '91234567890', '45', '45');
|
||
$this->assertSame(['GD1'], array_column(app(Repository::class)->explain('123456789', '2026-11-24 00:00:00', '2026-11-30 23:59:59'), 'work_order_no'));
|
||
}
|
||
}
|