diff --git a/modules/Ksef/Http/Admin/KsefController.php b/modules/Ksef/Http/Admin/KsefController.php index 20ee7c89e..c2e27873a 100644 --- a/modules/Ksef/Http/Admin/KsefController.php +++ b/modules/Ksef/Http/Admin/KsefController.php @@ -40,10 +40,21 @@ public function index(Request $request) } if ($tab === 'sent') { + // A submission still pending, or one that failed on its first try, + // has no sent_at yet. It is listed in the month it was queued, so + // it stays visible and its Resend button reachable. $records = KsefInvoice::with(['invoice.client', 'correction']) - ->whereYear('sent_at', $year) - ->whereMonth('sent_at', $month) - ->orderByDesc('sent_at') + ->where(function ($query) use ($year, $month): void { + $query->where(function ($sent) use ($year, $month): void { + $sent->whereYear('sent_at', $year)->whereMonth('sent_at', $month); + })->orWhere(function ($queued) use ($year, $month): void { + $queued->whereNull('sent_at') + ->whereYear('created_at', $year) + ->whereMonth('created_at', $month); + }); + }) + ->orderByRaw('COALESCE(sent_at, created_at) DESC') + ->orderByDesc('id') ->paginate(20); } else { $records = KsefImportedInvoice::query() diff --git a/tests/Feature/KsefImportTest.php b/tests/Feature/KsefImportTest.php index 9c259fd9b..293d2418c 100644 --- a/tests/Feature/KsefImportTest.php +++ b/tests/Feature/KsefImportTest.php @@ -184,3 +184,24 @@ function ($token, $endpoint, $subject, $from, $to) use (&$ranges) { ->assertHeader('Content-Type', 'application/xml') ->assertSee('', false); }); + +it('lists a pending or failed submission on the sent tab so it can be resent', function () { + $admin = Admin::factory()->create(); + $client = Client::factory()->create(); + $pending = Invoice::factory()->create(['client_id' => $client->id, 'total' => 10, 'invoice_num' => 'KSEF-PENDING-1']); + $failed = Invoice::factory()->create(['client_id' => $client->id, 'total' => 10, 'invoice_num' => 'KSEF-FAILED-1']); + $old = Invoice::factory()->create(['client_id' => $client->id, 'total' => 10, 'invoice_num' => 'KSEF-OLD-1']); + + KsefInvoice::create(['invoice_id' => $pending->id, 'status' => 'pending']); + $error = KsefInvoice::create(['invoice_id' => $failed->id, 'status' => 'error', 'error_message' => 'timeout']); + $previous = KsefInvoice::create(['invoice_id' => $old->id, 'status' => 'error']); + $previous->forceFill(['created_at' => now()->subMonthsNoOverflow(2)])->save(); + + $this->actingAs($admin, 'admin') + ->get(route('admin.ksef.index')) + ->assertOk() + ->assertSee('KSEF-PENDING-1') + ->assertSee('KSEF-FAILED-1') + ->assertSee(route('admin.ksef.resend', $error), false) + ->assertDontSee('KSEF-OLD-1'); +});