136 lines
11 KiB
PHP
136 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Models\Auth\Permission;
|
|
use App\Support\CourierBills\{CourierBillMatcher, FreightConfirmation, CourierFreightLink};
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
|
use Tests\Feature\FeatureTestCase;
|
|
final class FreightConfirmationTest extends FeatureTestCase
|
|
{
|
|
public function test_courier_push_is_isolated_versioned_and_feedback_does_not_change_settlement(): void
|
|
{
|
|
$this->withoutMiddleware(\Illuminate\Routing\Middleware\ThrottleRequests::class);
|
|
$this->company->makeCurrent(true);
|
|
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
|
$this->loginAs()->withExceptionHandling();
|
|
$db = CourierBillMatcher::db();
|
|
$group = $db->table('courier_bill_groups')->insertGetId(['name' => '测试快递', 'express_names' => '["测试快递"]', 'created_at' => now(), 'updated_at' => now()]);
|
|
$id = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'open', 'created_at' => now(), 'updated_at' => now()]);
|
|
$file = $db->table('courier_bill_files')->insertGetId(['bill_month' => '2026-11', 'original_name' => 'example.csv', 'stored_path' => 'none', 'status' => 'parsed', 'created_at' => now(), 'updated_at' => now()]);
|
|
$db->table('courier_bill_lines')->insert(['statement_id' => $id, 'file_id' => $file, 'sheet' => '明细', 'row_no' => 2, 'waybill_no' => 'TEST001', 'bill_amount' => 15, 'self_amount' => 12, 'category' => 'diff', 'express_name' => '测试快递', 'raw' => '[]']);
|
|
$db->table('courier_bill_summary_items')->insert(['statement_id' => $id, 'label' => '11月运费', 'bill_amount' => 15, 'self_amount' => 12, 'manual' => false, 'sort' => 1]);
|
|
$action = fn ($name) => route('business.courier-payables-final.courier-bills.' . $name, ['statement' => $id, 'owner' => $id]);
|
|
Http::fake(['*' => Http::response(['errcode' => 0])]);
|
|
app(SupplierDingTalkNotifier::class)->save('测试快递', 'https://oapi.dingtalk.com/robot/send?access_token=test', '', false);
|
|
$this->post($action('push-confirmation'))->assertSessionHasErrors('confirmation');
|
|
$db->table('courier_bill_statements')->where('id', $id)->update(['status' => 'confirmed']);
|
|
$this->post($action('push-confirmation'))->assertSessionHas('courier_bills_message');
|
|
$c = app(FreightConfirmation::class);
|
|
$company = (int) $this->company->id;
|
|
$this->assertSame('pushed', $c->state('2026-11', $id, $company)['status']);
|
|
$links = app(CourierFreightLink::class);
|
|
$url = $links->pageUrl($company, $id, '2026-11', 1);
|
|
$page = $this->get($url)->assertOk()->assertSeeText('TEST001');
|
|
$token = basename(parse_url($url, PHP_URL_PATH));
|
|
$this->assertNull($links->resolveToken($token, $company + 1));
|
|
// Changing live rows cannot change an already-pushed version.
|
|
$db->table('courier_bill_lines')->where('statement_id', $id)->update(['waybill_no' => 'CHANGED', 'self_amount' => 99]);
|
|
$this->get($url)->assertOk()->assertSeeText('TEST001')->assertDontSeeText('CHANGED')->assertSeeText('12.00');
|
|
$submit = $links->signedRoute('signed.courier-freight.submit', $token, $company);
|
|
$this->post($submit, ['other' => 'CHANGED 不在本版'])->assertSessionHasErrors('dispute');
|
|
$this->post($submit, ['d' => ['TEST001']])->assertSessionHasErrors('dispute');
|
|
Http::swap(new \Illuminate\Http\Client\Factory());
|
|
Http::fake(['*' => Http::response(['errcode' => 310000, 'errmsg' => 'fake failure'])]);
|
|
$this->post($submit, ['draft' => json_encode(['TEST001' => '核实重量'])])->assertSessionHas('success');
|
|
$this->assertSame('disputed', $c->state('2026-11', $id, $company)['status']);
|
|
$this->assertSame('failed', $c->state('2026-11', $id, $company)['receipt_status']);
|
|
$this->post($submit)->assertSessionHasErrors('dispute');
|
|
Http::swap(new \Illuminate\Http\Client\Factory());
|
|
Http::fake(['*' => Http::response(['errcode' => 0])]);
|
|
$this->post($action('retry-receipt'), ['month' => '2026-11'])->assertSessionHas('courier_bills_message');
|
|
$this->assertSame('sent', $c->state('2026-11', $id, $company)['receipt_status']);
|
|
$review = ['month' => '2026-11', 'version' => 1, 'waybill' => 'TEST001', 'decision' => 'reject', 'reply' => '依据原始称重不采纳'];
|
|
$this->post($action('review-feedback'), $review)->assertSessionHas('courier_bills_message');
|
|
$this->assertSame('reject', $c->disputes('2026-11', $id, $company, 1)[0]['decision']);
|
|
$this->assertSame(99.0, (float) $db->table('courier_bill_lines')->where('statement_id', $id)->value('self_amount'));
|
|
$this->post($action('review-feedback'), $review)->assertSessionHasErrors('confirmation');
|
|
$this->get($url)->assertOk()->assertSeeText('依据原始称重不采纳');
|
|
$db->table('courier_bill_lines')->where('statement_id', $id)->update(['waybill_no' => 'changed']);
|
|
$this->post($action('push-confirmation'))->assertSessionHas('courier_bills_message');
|
|
$this->get($url)->assertOk()->assertSeeText('不是最新一次推送');
|
|
$this->post($submit)->assertSessionHasErrors('dispute');
|
|
$this->get($links->pageUrl($company, $id, '2026-11', 2))->assertOk()->assertSeeText('changed');
|
|
$c->submit('2026-11',$id,2,['CHANGED'=>'按原单号保存'],$company);
|
|
$this->assertSame('changed',$c->disputes('2026-11',$id,$company,2)[0]['waybill']);
|
|
$this->assertCount(2, $c->history('2026-11', $id, $company));
|
|
$this->travel(31)->days();
|
|
$this->get($url)->assertForbidden();
|
|
$this->travelBack();
|
|
}
|
|
|
|
public function test_dropship_factory_pages_handle_the_largest_current_factory(): void
|
|
{
|
|
$this->company->makeCurrent(true);
|
|
$this->user->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
|
$this->loginAs()->withExceptionHandling();
|
|
$db = CourierBillMatcher::db();
|
|
$id = $db->table('dropship_factories')->insertGetId(['name' => '规模厂家', 'warehouse' => '规模厂家代发仓', 'keywords' => '规模厂家', 'created_at' => now(), 'updated_at' => now()]);
|
|
$file = $db->table('dropship_bill_files')->insertGetId(['factory_id' => $id, 'bill_month' => '2026-11', 'original_name' => 'fixture.csv', 'stored_path' => 'fixture', 'status' => 'parsed', 'created_at' => now(), 'updated_at' => now()]);
|
|
for ($offset = 0; $offset < 9730; $offset += 200) {
|
|
$rows = [];
|
|
for ($i = $offset + 1; $i <= min($offset + 200, 9730); $i++) {
|
|
$rows[] = ['file_id' => $file, 'factory_id' => $id, 'bill_month' => '2026-11', 'waybill_no' => 'DFPERF' . str_pad((string) $i, 6, '0', STR_PAD_LEFT), 'amount' => 12, 'sheet' => '明细', 'row_no' => $i + 1];
|
|
}
|
|
$db->table('dropship_bill_lines')->insert($rows);
|
|
}
|
|
$url = route('business.pages.show', ['section' => 'courier-payables-final', 'page' => 'dropship-freight-reconciliation', 'df_view' => 'factory', 'factory' => $id, 'month' => '2026-11']);
|
|
$queries = 0; $sqlMs = 0;
|
|
\Illuminate\Support\Facades\DB::listen(function ($q) use (&$queries, &$sqlMs) { $queries++; $sqlMs += $q->time; });
|
|
$start = microtime(true);
|
|
$this->get($url)->assertOk()->assertSeeText('规模厂家')->assertSeeText('DFPERF000100')->assertDontSeeText('DFPERF000101');
|
|
$this->get($url . '&page=2')->assertOk()->assertSeeText('DFPERF000101')->assertDontSeeText('DFPERF000001');
|
|
$this->get($url . '&q=DFPERF009730')->assertOk()->assertSeeText('DFPERF009730')->assertDontSeeText('DFPERF000001');
|
|
fwrite(STDERR, sprintf("\nDROPSHIP_PERF rows=9730 first_second_search=%.3fs peak_mb=%.1f queries=%d sql_ms=%.1f rss_peak_mb=%.1f\n", microtime(true) - $start, memory_get_peak_usage(true) / 1048576, $queries, $sqlMs, (getrusage()['ru_maxrss']??0)/1024));
|
|
}
|
|
|
|
public function test_representative_snapshot_paging_search_and_streaming_export(): void
|
|
{
|
|
$this->withoutMiddleware(\Illuminate\Routing\Middleware\ThrottleRequests::class);
|
|
$this->company->makeCurrent(true);
|
|
$db = CourierBillMatcher::db();
|
|
$group = $db->table('courier_bill_groups')->insertGetId(['name' => '规模测试', 'express_names' => '[]', 'created_at' => now(), 'updated_at' => now()]);
|
|
$id = $db->table('courier_bill_statements')->insertGetId(['group_id' => $group, 'bill_month' => '2026-11', 'status' => 'confirmed', 'created_at' => now(), 'updated_at' => now()]);
|
|
$c = app(FreightConfirmation::class);
|
|
$company = (int) $this->company->id;
|
|
$summary = ['billed' => 24566, 'bill' => 368490, 'settle' => 294792, 'diff' => 73698];
|
|
$party = (object) ['id' => $id, 'name' => '规模测试'];
|
|
$c->check('2026-11', $party, $summary, true, $company, null, 'test');
|
|
$rows = (function () {
|
|
for ($i = 1; $i <= 24566; $i++) {
|
|
yield ['waybill' => 'PERF' . str_pad((string) $i, 6, '0', STR_PAD_LEFT), 'ship_time' => '2026-11-01', 'express' => '规模快递', 'bill' => 15.0, 'final' => 12.0, 'status' => 'diff', 'label' => '一致但运费有差', 'note' => '测试', 'formula' => 'INTERNAL-SECRET'];
|
|
}
|
|
})();
|
|
$start = microtime(true);
|
|
$queries = 0; $sqlMs = 0;
|
|
\Illuminate\Support\Facades\DB::listen(function ($q) use (&$queries, &$sqlMs) { $queries++; $sqlMs += $q->time; });
|
|
$c->startPush('2026-11', $party, $summary, 24566, $company, null, 'test', $rows);
|
|
$c->markPushed('2026-11', $id, $company);
|
|
$this->assertSame(24566, $c->snapshotQuery('2026-11', $id, 1, $company)->count());
|
|
$links = app(CourierFreightLink::class);
|
|
$url = $links->pageUrl($company, $id, '2026-11', 1);
|
|
$token = basename(parse_url($url, PHP_URL_PATH));
|
|
$read = microtime(true);
|
|
$this->get($url)->assertOk()->assertSeeText('PERF000050')->assertDontSeeText('PERF000051')->assertDontSeeText('INTERNAL-SECRET');
|
|
$this->get($links->signedRoute('signed.courier-freight.show', $token, $company, null, ['page' => 2]))->assertOk()->assertSeeText('PERF000051')->assertDontSeeText('PERF000001');
|
|
$this->post($links->signedRoute('signed.courier-freight.view', $token, $company), ['q' => 'PERF024566', 'filter' => 'all'])->assertOk()->assertSeeText('PERF024566')->assertDontSeeText('PERF000001');
|
|
$readSeconds = microtime(true) - $read;
|
|
$export = microtime(true);
|
|
$response = $this->get($links->signedRoute('signed.courier-freight.excel', $token, $company))->assertOk();
|
|
$bytes = strlen($response->streamedContent());
|
|
fwrite(STDERR, sprintf("\nCONFIRMATION_PERF rows=24566 snapshot=%.3fs pages_and_search=%.3fs export=%.3fs xlsx_bytes=%d peak_mb=%.1f queries=%d sql_ms=%.1f rss_peak_mb=%.1f\n", $read - $start, $readSeconds, microtime(true) - $export, $bytes, memory_get_peak_usage(true) / 1048576, $queries, $sqlMs, (getrusage()['ru_maxrss']??0)/1024));
|
|
$this->assertGreaterThan(1000, $bytes);
|
|
}
|
|
}
|