CluPilotCloud/app/Console/Commands/ArchiveUnexportedInvoices.php

52 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\ArchiveInvoice;
use App\Models\Invoice;
use App\Services\Billing\InvoiceArchive;
use Illuminate\Console\Command;
/**
* The sweep behind the copy-on-issue.
*
* Copying at issue is right and it is not enough. An invoice issued while the
* office connection was down never reaches the archive, the queue eventually
* gives up, and nothing about the invoice tells anybody — nobody opens an
* archive to check whether last Tuesday is in it. This picks up whatever is
* still missing, however long ago it was issued.
*
* Deliberately re-queues rather than writing here: the job already knows how to
* write one, and two code paths that both write an invoice into an archive is
* one more than the number that can be kept correct.
*/
class ArchiveUnexportedInvoices extends Command
{
protected $signature = 'clupilot:archive-invoices {--limit=200}';
protected $description = 'Queue a copy of every invoice that is not on the archive yet';
public function handle(): int
{
if (! InvoiceArchive::enabled()) {
$this->line('No archive path configured — nothing to do.');
return self::SUCCESS;
}
$pending = Invoice::query()
->whereNull('exported_at')
->orderBy('id')
->limit((int) $this->option('limit'))
->get();
foreach ($pending as $invoice) {
ArchiveInvoice::dispatch($invoice);
}
$this->line("Queued {$pending->count()} invoice(s) for the archive.");
return self::SUCCESS;
}
}