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
@@ -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