fix: 已退回存量报价不再占用执行区间

This commit is contained in:
limeng 2026-10-24 17:22:04 +08:00
parent 054b239867
commit d9469d2d9a
4 changed files with 81 additions and 2 deletions

View File

@ -95,6 +95,12 @@ final class CourierQuoteChangeService
}
return DB::transaction(function () use ($change, $userId): CourierQuoteChange {
if ($change->isStockQuote() && $change->status === CourierQuoteChange::STATUS_RETURNED) {
// 退回期间可能已重新录入,恢复待审核前重新取得日期占用。
\App\Models\Common\Company::whereKey(company_id())->lockForUpdate()->firstOrFail();
$entry = CourierStockQuoteEntry::findOrFail(data_get($change->quote_payload, 'stock_quote_entry_id'));
app(CourierStockQuoteEntryService::class)->assertPeriod(['code' => $entry->courier_expcode, 'name' => $entry->courier_name], $entry->quote_type, $change->effective_date->toDateString(), $change->end_date?->toDateString(), $entry->id);
}
$from = $change->status;
$retrospective = ! $change->isStockQuote() && $this->isRetrospectiveDate($change->effective_date);
$change->update([

View File

@ -3,6 +3,7 @@
namespace App\Support;
use App\Models\Business\CourierOnboarding;
use App\Models\Business\CourierQuoteChange;
use App\Models\Business\CourierStockQuoteEntry;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Http\UploadedFile;
@ -73,8 +74,9 @@ final class CourierStockQuoteEntryService
public function existingIdentities(?int $exceptId = null): array
{
return CourierStockQuoteEntry::withTrashed()->with(['change:id,company_id', 'change.version:id,company_id,source_change_id,status'])->when($exceptId !== null, fn ($query) => $query->where('id', '!=', $exceptId))
->get(['id', 'company_id', 'source_change_id', 'courier_expcode', 'courier_name', 'quote_type', 'effective_date', 'end_date'])->filter(fn ($entry) => !$entry->change?->version?->isVoided())
return CourierStockQuoteEntry::withTrashed()->with(['change:id,company_id,status', 'change.version:id,company_id,source_change_id,status'])->when($exceptId !== null, fn ($query) => $query->where('id', '!=', $exceptId))
->get(['id', 'company_id', 'source_change_id', 'courier_expcode', 'courier_name', 'quote_type', 'effective_date', 'end_date'])
->filter(fn ($entry) => $entry->change?->status !== CourierQuoteChange::STATUS_RETURNED && !$entry->change?->version?->isVoided())
->map(fn ($entry) => ['code' => $entry->courier_expcode, 'name' => $entry->courier_name, 'quote_type' => $entry->quote_type,
'effective_date' => $entry->effective_date?->toDateString(), 'end_date' => $entry->end_date?->toDateString()])->all();
}

View File

@ -83,6 +83,38 @@ class CourierStockQuoteBatchTest extends FeatureTestCase
$this->assertSame('2026-10-01', $entries[1]->change->effective_date->toDateString());
}
public function test_returned_open_ended_quote_does_not_block_four_new_batch_periods(): void
{
$oldBatch = $this->preview(['EXP-001']);
$this->postJson(route('business.courier-stock-quotes.batch-save', $oldBatch), $this->selection(['EXP-001']))->assertOk();
$old = CourierStockQuoteEntry::firstOrFail();
$files = $old->files;
$changes = app(CourierQuoteChangeService::class);
$changes->submit($old->change, (int) $this->user->id);
$changes->returnForChanges($old->change->fresh(), '退回后按实际区间重新录入', (int) $this->user->id);
$batch = $this->preview(['EXP-001']);
$service = app(CourierStockQuoteBatchService::class);
$rows = $service->rows($service->load($batch, (int) $this->user->id), app(\App\Support\CourierStockQuoteEntryService::class)->courierOptions());
$this->assertFalse($rows[0]['duplicate']);
$this->get(route('business.courier-stock-quotes.batch', $batch))->assertOk()->assertDontSeeText('已录入,默认跳过');
$selection = $this->selection(['EXP-001']);
$selection['submit_for_review'] = $selection['confirmed'] = true;
$selection['entries'][0]['periods'] = [
['effective_date' => '2026-10-01', 'end_date' => '2026-10-04'],
['effective_date' => '2026-10-11', 'end_date' => '2026-10-14'],
['effective_date' => '2026-10-21', 'end_date' => '2026-10-24'],
['effective_date' => '2026-11-01', 'end_date' => '2026-11-04'],
];
$this->postJson(route('business.courier-stock-quotes.batch-save', $batch), $selection)->assertOk();
$this->assertDatabaseCount('business_courier_stock_quote_entries', 5);
$this->assertSame(4, CourierQuoteChange::where('status', CourierQuoteChange::STATUS_PENDING_REVIEW)->count());
$this->assertSame(CourierQuoteChange::STATUS_RETURNED, $old->change->fresh()->status);
$this->assertSame($files, $old->fresh()->files);
Storage::disk('uploads')->assertExists($files[0]['path']);
$this->assertDatabaseCount('business_courier_quote_versions', 0);
}
public function test_bulk_save_reuses_rule_groups_overrides_dates_and_preserves_separate_prices(): void
{
$batch = $this->preview(['EXP-001', 'EXP-002', '说明']);

View File

@ -185,6 +185,45 @@ class CourierStockQuoteEntryTest extends FeatureTestCase
->assertSessionHasErrors('courier_expcode');
}
public function test_returned_quote_releases_period_and_replacement_preserves_review_overlap_protection(): void
{
$old = $this->createEntry();
$files = $old->files;
$changes = app(CourierQuoteChangeService::class);
$changes->submit($old->change, (int) $this->user->id);
$reviewer = $this->reviewer();
$changes->returnForChanges($old->change->fresh(), '原报价执行日期需重新录入', (int) $reviewer->id);
$replacement = $this->createEntry();
$this->assertNotSame($old->id, $replacement->id);
$this->assertSame(CourierQuoteChange::STATUS_RETURNED, $old->change->fresh()->status);
$this->assertSame($files, $old->fresh()->files);
Storage::disk('uploads')->assertExists($files[0]['path']);
$this->withExceptionHandling()->loginAs()->post(route('business.courier-quote-changes.submit', $old->change))
->assertSessionHasErrors('courier_name');
$this->assertSame(CourierQuoteChange::STATUS_RETURNED, $old->change->fresh()->status);
$changes->submit($replacement->change, (int) $this->user->id);
$this->post(route('business.courier-stock-quotes.store'), $this->payload())->assertSessionHasErrors('courier_name');
$changes->approve($replacement->change->fresh(), (int) $reviewer->id);
$this->post(route('business.courier-stock-quotes.store'), $this->payload())->assertSessionHasErrors('courier_name');
$this->assertDatabaseCount('business_courier_stock_quote_entries', 2);
$this->assertDatabaseCount('business_courier_quote_versions', 1);
}
public function test_returned_quote_can_be_resubmitted_when_its_period_is_still_available(): void
{
$entry = $this->createEntry();
$changes = app(CourierQuoteChangeService::class);
$changes->submit($entry->change, (int) $this->user->id);
$changes->returnForChanges($entry->change->fresh(), '重新核对后提交', (int) $this->reviewer()->id);
$this->loginAs()->post(route('business.courier-quote-changes.submit', $entry->change->fresh()))
->assertSessionHasNoErrors()->assertRedirect();
$this->assertSame(CourierQuoteChange::STATUS_PENDING_REVIEW, $entry->change->fresh()->status);
$this->assertDatabaseCount('business_courier_stock_quote_entries', 1);
$this->assertDatabaseCount('business_courier_quote_versions', 0);
}
public function test_multi_sheet_upload_requires_exact_sheet_and_invalid_workbooks_leave_no_record(): void
{
$this->withExceptionHandling()->loginAs()->post(route('business.courier-stock-quotes.store'), [...$this->payload(), 'source_quote' => $this->workbook(true)])