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
@@ -6,6 +6,7 @@ use App\Modules\Sales\Application\DTOs\QuickSaleSummary;
use App\Modules\Sales\Application\UseCases\CloseCashDay;
use App\Modules\Sales\Application\UseCases\CreditQuickSale;
use App\Modules\Sales\Application\UseCases\ReopenCashDay;
use App\Modules\Sales\Application\UseCases\ResendQuickSaleReceipt;
use App\Modules\Sales\Domain\Exceptions\CashDayClosed;
use App\Modules\Sales\Domain\Exceptions\CashDayInFuture;
use App\Modules\Sales\Domain\Exceptions\CashDayNotClosed;
@@ -13,6 +14,7 @@ use App\Modules\Sales\Domain\Exceptions\CreditCannotBeCredited;
use App\Modules\Sales\Domain\Exceptions\QuickSaleAlreadyCredited;
use App\Modules\Sales\Domain\Exceptions\QuickSaleNotFound;
use App\Modules\Sales\Presentation\Livewire\Concerns\FormatsSales;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use App\Modules\Users\Application\Contracts\Clock;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
@@ -27,7 +29,8 @@ use Livewire\Component;
* A day's quick sales and its cash day (kassapäivä). Sellers see their own
* sales, management everyone's. Anyone selling may close the day, which
* forms its balance; management may reopen it. Sales are credited, never
* deleted.
* deleted. An emailed receipt's state is shown on its row, and a receipt
* can be emailed again, to a corrected address if need be.
*/
new class extends Component
{
@@ -52,6 +55,11 @@ new class extends Component
#[Locked]
public string $statusMessage = '';
#[Locked]
public ?int $resendSaleId = null;
public string $resendEmail = '';
public function mount(Clock $clock): void
{
$this->authorize('sales.use');
@@ -155,6 +163,42 @@ new class extends Component
$this->js("document.getElementById('credit-dialog').close(); document.getElementById('sales-status').focus()");
}
public function confirmResend(int $saleId): void
{
$sale = app(QuickSaleQueries::class)->findSale($saleId) ?? abort(404);
$this->authorize('sales.view-sale', $sale->sellerId);
$this->resendSaleId = $saleId;
$this->resendEmail = $sale->receiptEmail ?? '';
$this->resetErrorBag('resendEmail');
$this->js("document.getElementById('resend-dialog').showModal()");
}
public function resend(ResendQuickSaleReceipt $resendQuickSaleReceipt): void
{
$sale = app(QuickSaleQueries::class)->findSale((int) $this->resendSaleId) ?? abort(404);
$this->authorize('sales.view-sale', $sale->sellerId);
$this->validate(
['resendEmail' => ['required', 'string', 'email', 'max:255']],
attributes: ['resendEmail' => __('sales.sell.receipt_email')],
);
try {
$resendQuickSaleReceipt->execute($sale->id, $this->resendEmail);
} catch (InvalidEmailAddress) {
$this->addError('resendEmail', __('sales.errors.receipt_email'));
return;
}
$this->statusMessage = __('sales.receipt_mail.resent', ['number' => $sale->id, 'email' => mb_strtolower(trim($this->resendEmail))]);
$this->resendSaleId = null;
$this->resendEmail = '';
unset($this->sales);
$this->js("document.getElementById('resend-dialog').close(); document.getElementById('sales-status').focus()");
}
public function confirmClose(): void
{
$this->authorize('sales.close-day');
@@ -425,6 +469,29 @@ new class extends Component
<x-icon name="document-text" class="size-4" aria-hidden="true" />
{{ __('sales.receipt_link') }}
</a>
@if ($sale->receiptStatus() !== null)
<span class="mr-2 inline-flex items-center gap-1 text-xs">
@if ($sale->receiptStatus() === 'failed')
<x-icon name="exclamation-triangle" class="size-4 text-red-700 dark:text-red-400" aria-hidden="true" />
@elseif ($sale->receiptStatus() === 'sent')
<x-icon name="envelope" class="size-4" aria-hidden="true" />
@else
<x-icon name="clock" class="size-4" aria-hidden="true" />
@endif
{{ __('sales.receipt_mail.status_'.$sale->receiptStatus()) }}
</span>
@endif
@can('sales.view-sale', $sale->sellerId)
<x-button
wire:click="confirmResend({{ $sale->id }})"
subtle
color="black"
sm
icon="envelope"
:text="__('sales.receipt_mail.send')"
:aria-label="__('sales.receipt_mail.send_named', ['number' => $sale->id])"
/>
@endcan
@if ($sale->canBeCredited())
@can('sales.credit', $sale->sellerId)
<x-button
@@ -454,6 +521,17 @@ new class extends Component
</x-slot:actions>
</x-confirm-dialog>
<x-confirm-dialog id="resend-dialog" :title="__('sales.receipt_mail.send_title')">
@if ($resendSaleId !== null)
<p class="mb-3">{{ __('sales.receipt_mail.send_text', ['number' => $resendSaleId]) }}</p>
@endif
<x-input id="resend-email" wire:model="resendEmail" type="email" inputmode="email" autocomplete="off" :label="__('sales.sell.receipt_email')" />
<x-slot:actions>
<x-button wire:click="resend" icon="envelope" :text="__('sales.receipt_mail.send_confirm')" />
</x-slot:actions>
</x-confirm-dialog>
<x-confirm-dialog id="close-dialog" :title="__('sales.cash_day.close_title')">
<p>{{ __('sales.cash_day.close_text') }}</p>
<p class="mt-2 font-medium">
@@ -14,6 +14,7 @@ use App\Modules\Sales\Domain\Exceptions\QuickSaleCustomerNotFound;
use App\Modules\Sales\Domain\Exceptions\QuickSaleEmpty;
use App\Modules\Sales\Domain\ValueObjects\QuickSaleLine;
use App\Modules\Sales\Presentation\Livewire\Concerns\FormatsSales;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
@@ -22,8 +23,8 @@ use Livewire\Component;
/**
* A quick sale at the counter (pikamyynti), made for phones: each press of
* a product button adds one to the sale's list, where quantities can be
* lowered, raised or removed; then the payment method and an optional
* customer. After saving, the form empties for the next customer and the
* lowered, raised or removed; then the payment method, an optional
* customer and an optional address the PDF receipt is emailed to. After saving, the form empties for the next customer and the
* result is announced with a link to the PDF receipt. Prices are consumer
* prices with VAT, which everyone may see.
*/
@@ -44,6 +45,8 @@ new class extends Component
public string $customerSearch = '';
public string $receiptEmail = '';
#[Locked]
public ?int $customerId = null;
@@ -204,6 +207,11 @@ new class extends Component
$this->customerId = $customer->id;
$this->customerName = $customer->name;
$this->customerSearch = '';
if (trim($this->receiptEmail) === '') {
$this->receiptEmail = $this->customerEmail($customer->id) ?? '';
}
$this->js("document.getElementById('change-customer')?.focus()");
}
}
@@ -222,10 +230,13 @@ new class extends Component
$this->hoursMessage = '';
$this->savedSaleId = null;
$this->validate(['paymentMethod' => ['required', Rule::enum(PaymentMethod::class)]]);
$this->validate([
'paymentMethod' => ['required', Rule::enum(PaymentMethod::class)],
'receiptEmail' => ['nullable', 'string', 'email', 'max:255'],
], attributes: ['receiptEmail' => __('sales.sell.receipt_email')]);
try {
$result = $sellQuickSale->execute((int) auth()->id(), new SaleData($this->quantities, $this->paymentMethod, $this->customerId));
$result = $sellQuickSale->execute((int) auth()->id(), new SaleData($this->quantities, $this->paymentMethod, $this->customerId, trim($this->receiptEmail) === '' ? null : $this->receiptEmail));
} catch (QuickSaleEmpty) {
$this->addError('quantities', __('sales.errors.empty'));
@@ -248,6 +259,10 @@ new class extends Component
} catch (CashDayClosed) {
$this->addError('sale', __('sales.errors.day_closed'));
return;
} catch (InvalidEmailAddress) {
$this->addError('receiptEmail', __('sales.errors.receipt_email'));
return;
}
@@ -258,12 +273,16 @@ new class extends Component
'method' => __('sales.payment_methods.'.$this->paymentMethod),
]);
if ($result->receiptEmail !== null) {
$this->savedMessage .= ' '.__('sales.sell.receipt_queued', ['email' => $result->receiptEmail]);
}
$this->hoursMessage = trim(
($result->bookedQuarters > 0 ? __('sales.sell.hours_booked', ['hours' => $this->hours($result->bookedQuarters)]) : '')
.' '.($result->unbookedQuarters > 0 ? __('sales.sell.hours_not_booked', ['hours' => $this->hours($result->unbookedQuarters)]) : ''),
);
$this->reset('quantities', 'paymentMethod', 'customerSearch', 'customerId', 'customerName', 'cartMessage');
$this->reset('quantities', 'paymentMethod', 'customerSearch', 'customerId', 'customerName', 'cartMessage', 'receiptEmail');
$this->forgetLines();
$this->js("document.getElementById('sale-saved')?.focus()");
@@ -295,6 +314,21 @@ new class extends Component
$this->js("const field = document.getElementById('quantity-{$productId}'); if (field) { field.value = '{$quantity}'; }");
}
/**
* The first email address among the customer's contacts, to prefill
* the receipt address.
*/
private function customerEmail(int $customerId): ?string
{
foreach (app(CustomerQueries::class)->contactsOf($customerId) as $contact) {
if ($contact->email !== null && $contact->email !== '') {
return $contact->email;
}
}
return null;
}
private function forgetLines(): void
{
unset($this->lines, $this->totalCents);
@@ -482,6 +516,15 @@ new class extends Component
@endif
</div>
<x-input
wire:model="receiptEmail"
type="email"
inputmode="email"
autocomplete="off"
:label="__('sales.sell.receipt_email')"
:hint="__('sales.sell.receipt_email_hint')"
/>
<x-button submit lg class="w-full" :text="__('sales.sell.save')" />
</form>
@endif