Email quick sale receipts and send them again from the sales list

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:00:15 +02:00
co-authored by Claude Sonnet 5.5
parent 3faa4cfbc6
commit d160854b79
29 changed files with 679 additions and 34 deletions
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Tests\Fakes;
use App\Modules\Sales\Application\Contracts\ReceiptMailer;
class FakeReceiptMailer implements ReceiptMailer
{
/** @var list<int> */
public array $sent = [];
public function send(int $saleId): void
{
$this->sent[] = $saleId;
}
}
@@ -4,6 +4,7 @@ namespace Tests\Fakes;
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
use App\Modules\Sales\Domain\Entities\QuickSale;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
use DateTimeImmutable;
class InMemoryQuickSaleRepository implements QuickSaleRepository
@@ -11,6 +12,9 @@ class InMemoryQuickSaleRepository implements QuickSaleRepository
/** @var array<int, QuickSale> */
public array $sales = [];
/** @var array<int, string> Sending results by sale id: 'sent' or 'failed'. */
public array $receiptResults = [];
private int $nextId = 1;
public function findById(int $id): ?QuickSale
@@ -31,6 +35,7 @@ class InMemoryQuickSaleRepository implements QuickSaleRepository
$sale->soldOn(),
$sale->lines(),
$sale->creditsSaleId(),
$sale->receiptEmail(),
);
}
@@ -49,4 +54,21 @@ class InMemoryQuickSaleRepository implements QuickSaleRepository
{
return array_values(array_filter($this->sales, fn (QuickSale $sale): bool => $sale->soldOn()->format('Y-m-d') === $day->format('Y-m-d')));
}
public function changeReceiptEmail(int $saleId, EmailAddress $email): void
{
$sale = $this->sales[$saleId];
$this->sales[$saleId] = QuickSale::reconstitute($saleId, $sale->sellerId(), $sale->customerId(), $sale->paymentMethod(), $sale->soldAt(), $sale->soldOn(), $sale->lines(), $sale->creditsSaleId(), $email);
unset($this->receiptResults[$saleId]);
}
public function markReceiptSent(int $saleId, DateTimeImmutable $at): void
{
$this->receiptResults[$saleId] = 'sent';
}
public function markReceiptFailed(int $saleId, DateTimeImmutable $at): void
{
$this->receiptResults[$saleId] = 'failed';
}
}
@@ -7,10 +7,14 @@ use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\Sales\Application\Contracts\QuickSaleQueries;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\CashDayModel;
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleModel;
use App\Modules\Sales\Infrastructure\Receipts\QuickSaleReceiptMail;
use App\Modules\Sales\Infrastructure\Receipts\SendQuickSaleReceipt;
use App\Modules\Shared\Application\DTOs\CompanyDetails;
use App\Modules\Shared\Application\UseCases\CompanyDetailsStore;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Livewire\Livewire;
uses(RefreshDatabase::class);
@@ -285,3 +289,71 @@ it('credits a sale of a closed day on today\'s cash day and reminds of unclosed
expect(QuickSaleModel::query()->where('credits_quick_sale_id', $old->id)->sole()->sold_on)->toBe($this->today)
->and(CashDayModel::query()->where('day', $yesterday)->sole()->total_cents)->toBe(100);
});
it('emails the PDF receipt to the address given at the sale and shows it as sent', function () {
Mail::fake();
sell([$this->photos->id => 1])->assertHasNoErrors();
Livewire::test('sales::sell')
->call('addProduct', $this->photos->id)
->set('receiptEmail', '[email protected]')
->call('save')
->assertHasNoErrors()
->assertSet('savedMessage', fn (string $message) => str_contains($message, 'Kuitti lähetetään osoitteeseen [email protected].'))
->assertSet('receiptEmail', '');
$sale = QuickSaleModel::query()->latest('id')->first();
Mail::assertSent(QuickSaleReceiptMail::class, fn (QuickSaleReceiptMail $mail) => $mail->hasTo('[email protected]')
&& $mail->sale->id === $sale->id
&& count($mail->attachments()) === 1);
Mail::assertSentCount(1);
expect($sale->receipt_sent_at)->not->toBeNull()
->and($sale->getRawOriginal('receipt_email'))->not->toBe('[email protected]')
->and($sale->toArray())->not->toHaveKey('receipt_email');
Livewire::test('sales::index')->assertSee(__('sales.receipt_mail.status_sent'));
});
it('rejects an invalid receipt address', function () {
Livewire::test('sales::sell')
->call('addProduct', $this->photos->id)
->set('receiptEmail', 'ei osoite')
->call('save')
->assertHasErrors('receiptEmail');
expect(QuickSaleModel::query()->count())->toBe(0);
});
it('keeps the sale when sending fails and lets the seller send it again to a corrected address', function () {
Queue::fake();
Livewire::test('sales::sell')
->call('addProduct', $this->photos->id)
->set('receiptEmail', '[email protected]')
->call('save')
->assertHasNoErrors();
$sale = QuickSaleModel::query()->sole();
Queue::assertPushed(SendQuickSaleReceipt::class, fn (SendQuickSaleReceipt $job) => $job->saleId === $sale->id);
(new SendQuickSaleReceipt($sale->id))->failed(new RuntimeException('SMTP down'));
Livewire::test('sales::index')
->assertSee(__('sales.receipt_mail.status_failed'))
->call('confirmResend', $sale->id)
->assertSet('resendEmail', '[email protected]')
->set('resendEmail', '[email protected]')
->call('resend')
->assertHasNoErrors()
->assertSet('statusMessage', __('sales.receipt_mail.resent', ['number' => $sale->id, 'email' => '[email protected]']))
->assertSee(__('sales.receipt_mail.status_pending'));
expect($sale->refresh()->receipt_email)->toBe('[email protected]')
->and($sale->receipt_send_failed_at)->toBeNull();
Queue::assertPushed(SendQuickSaleReceipt::class, 2);
$this->actingAs(User::factory()->employee()->create());
Livewire::test('sales::index')->call('confirmResend', $sale->id)->assertForbidden();
});
+24 -2
View File
@@ -21,10 +21,12 @@ use App\Modules\Sales\Domain\ValueObjects\CashDayBalance;
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
use App\Modules\Sales\Domain\ValueObjects\VatTotal;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeQuickSaleHours;
use Tests\Fakes\FakeQuickSaleProducts;
use Tests\Fakes\FakeReceiptMailer;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryCashDayRepository;
use Tests\Fakes\InMemoryQuickSaleRepository;
@@ -35,11 +37,12 @@ beforeEach(function () {
$this->products = new FakeQuickSaleProducts;
$this->hours = new FakeQuickSaleHours;
$this->customers = new FakeCustomerQueries;
$this->mailer = new FakeReceiptMailer;
$this->clock = new FakeClock(new DateTimeImmutable('2026-11-24'));
$transactions = new FakeTransactionManager;
$this->sell = new SellQuickSale($this->sales, $this->cashDays, $this->products, $this->hours, $this->customers, $transactions, $this->clock);
$this->credit = new CreditQuickSale($this->sales, $this->cashDays, $transactions, $this->clock);
$this->sell = new SellQuickSale($this->sales, $this->cashDays, $this->products, $this->hours, $this->mailer, $this->customers, $transactions, $this->clock);
$this->credit = new CreditQuickSale($this->sales, $this->cashDays, $this->mailer, $transactions, $this->clock);
$this->close = new CloseCashDay($this->cashDays, $this->sales, $transactions, $this->clock);
$this->reopen = new ReopenCashDay($this->cashDays, $transactions, $this->clock);
@@ -162,3 +165,22 @@ it('closes a day without sales with a zero balance', function () {
expect($day->balance())->toEqual(CashDayBalance::empty());
});
it('emails the receipt when an address is given, and the credit receipt to the same address', function () {
$plain = $this->sell->execute(1, new SaleData([1 => 1], 'cash'));
$mailed = $this->sell->execute(1, new SaleData([1 => 1], 'card', receiptEmail: ' [email protected] '));
expect($plain->receiptEmail)->toBeNull()
->and($mailed->receiptEmail)->toBe('[email protected]')
->and($this->mailer->sent)->toBe([$mailed->saleId]);
$this->credit->execute($plain->saleId, 1);
$creditId = $this->credit->execute($mailed->saleId, 1);
expect($this->mailer->sent)->toBe([$mailed->saleId, $creditId])
->and($this->sales->findById($creditId)->receiptEmail()->value)->toBe('[email protected]');
});
it('rejects an invalid receipt address without recording the sale', function () {
$this->sell->execute(1, new SaleData([1 => 1], 'cash', receiptEmail: 'ei-osoite'));
})->throws(InvalidEmailAddress::class);