Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions app/Hooks/proforma.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
<?php

use App\Mail\InvoicePaidMail;
use App\Models\Invoice;
use App\Models\Setting;
use App\Services\ProformaService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

$sendProformaPaidMail = static function (Invoice $proforma, ?Invoice $vat, ?string $transactionId): void {
$email = $proforma->client?->billingEmail();

if (! $email) {
return;
}

try {
Mail::to($email)->queue(new InvoicePaidMail($proforma, $transactionId, $vat));
} catch (Throwable $e) {
Log::error('Proforma: could not queue paid email for #'.$proforma->id.': '.$e->getMessage());
}
};

/**
* A paid proforma is not a tax document: issue the corresponding VAT invoice,
* then re-fire InvoicePaid for it so the KSeF hook (and any other module)
* sees the VAT invoice.
*
* The normal paid mail and the separate VAT-created mail are suppressed for
* this flow. One message tells the customer the proforma is paid and carries
* the VAT invoice. A top-up has no VAT invoice and keeps the ordinary paid
* message; a supply whose VAT invoice could not be issued still gets that
* ordinary confirmation rather than silence.
*/
add_hook('InvoicePaid', function (array $params): void {
add_hook('InvoicePaid', function (array $params) use ($sendProformaPaidMail): void {
$invoice = $params['invoice'] ?? null;

if (! $invoice instanceof Invoice) {
Expand All @@ -20,13 +43,31 @@
return;
}

$hasSupply = ! $invoice->items()->where('type', 'AddFunds')->exists();
$combined = Setting::get('ProformaCombinedPaidEmail', '0') === '1';
$transactionId = $params['transactionId'] ?? null;

try {
$vat = app(ProformaService::class)->issueVatInvoice($invoice);

if ($vat) {
run_hook('InvoicePaid', ['invoice' => $vat, 'transactionId' => $params['transactionId'] ?? null]);
run_hook('InvoicePaid', ['invoice' => $vat, 'transactionId' => $transactionId]);

if ($combined && $vat->wasRecentlyCreated) {
$sendProformaPaidMail($invoice, $vat, $transactionId);
}

return;
}
} catch (\Throwable $e) {

if ($combined && $hasSupply) {
$sendProformaPaidMail($invoice, null, $transactionId);
}
} catch (Throwable $e) {
Log::error('Proforma: could not issue VAT invoice for #'.$invoice->id.': '.$e->getMessage());

if ($combined && $hasSupply) {
$sendProformaPaidMail($invoice, null, $transactionId);
}
}
});
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function paymentMethods(): array
'MaxMindAccountId', 'MaxMindEnabled', 'MaxMindLicenseKey',
'TwilioAccountSid', 'TwilioAuthToken', 'TwilioVerifyEnabled', 'TwilioVerifyServiceSid',
'WhatsappVerifyEnabled', 'WhatsappBusinessNumber', 'WhatsappAppSecret', 'WhatsappWebhookVerifyToken',
'ProformaEnabled', 'ProformaNumberFormat', 'HidePaidProformas',
'ProformaEnabled', 'ProformaNumberFormat', 'HidePaidProformas', 'ProformaCombinedPaidEmail',
'LateFeeAmount', 'LateFeeMinDays', 'LateFeeType',
'MailEnabled', 'MailType', 'MaintenanceMode', 'PhoneNumber',
'SMTPHost', 'SMTPPassword', 'SMTPPort', 'SMTPSecurity', 'SMTPUsername',
Expand Down
50 changes: 43 additions & 7 deletions app/Listeners/SendNotificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\Mail\TicketOpenedMail;
use App\Mail\TicketReplyMail;
use App\Models\Admin;
use App\Models\Invoice;
use App\Models\Setting;
use App\Models\Ticket;
use App\Services\AddonManager;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function handleInvoiceCreated(InvoiceCreated $event): void
$ksefActive = app(AddonManager::class)->isActive('ksef');
$isVat = ($event->invoice->type ?? 'vat') === 'vat';

if (! ($isVat && $ksefActive)) {
if (! ($isVat && $ksefActive) && ! ($this->combinedProformaEmailEnabled() && $this->isVatFromProforma($event->invoice))) {
try {
$email = $event->invoice->client?->billingEmail();
if ($email) {
Expand All @@ -98,13 +99,15 @@ public function handleInvoiceCreated(InvoiceCreated $event): void

public function handleInvoicePaid(InvoicePaid $event): void
{
try {
$email = $event->invoice->client?->billingEmail();
if ($email) {
Mail::to($email)->queue(new InvoicePaidMail($event->invoice, $event->transactionId));
if (! $this->paidEmailComesFromProformaFlow($event->invoice)) {
try {
$email = $event->invoice->client?->billingEmail();
if ($email) {
Mail::to($email)->queue(new InvoicePaidMail($event->invoice, $event->transactionId));
}
} catch (\Throwable $e) {
Log::error('SendNotification: InvoicePaid email failed', ['error' => $e->getMessage()]);
}
} catch (\Throwable $e) {
Log::error('SendNotification: InvoicePaid email failed', ['error' => $e->getMessage()]);
}

$this->dispatchNotification('invoice.paid', [
Expand All @@ -116,6 +119,39 @@ public function handleInvoicePaid(InvoicePaid $event): void
]);
}

/**
* A VAT invoice issued from a proforma gets the combined proforma-paid
* email from the proforma hook instead of a separate invoice-created mail.
*/
private function isVatFromProforma(Invoice $invoice): bool
{
return ($invoice->type ?? 'vat') === 'vat' && $invoice->source_invoice_id !== null;
}

private function combinedProformaEmailEnabled(): bool
{
return Setting::get('ProformaCombinedPaidEmail', '0') === '1';
}

/**
* The paid email for a normal proforma is sent by the proforma hook with
* the VAT invoice attached when the combined option is on. A top-up has no
* VAT invoice, so it keeps the ordinary paid email. The VAT invoice itself
* is covered by the combined message as well.
*/
private function paidEmailComesFromProformaFlow(Invoice $invoice): bool
{
if (! $this->combinedProformaEmailEnabled()) {
return false;
}

if ($invoice->isProforma()) {
return ! $invoice->items()->where('type', 'AddFunds')->exists();
}

return $this->isVatFromProforma($invoice);
}

public function handleTicketOpened(TicketOpened $event): void
{
try {
Expand Down
46 changes: 41 additions & 5 deletions app/Mail/InvoicePaidMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace App\Mail;

use App\Models\Invoice;
use App\Mail\Concerns\LocalizesToRecipient;
use App\Models\Setting;
use App\Models\Invoice;
use App\Services\InvoicePdfService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class InvoicePaidMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
use LocalizesToRecipient;
use Queueable, SerializesModels;

public function __construct(
public Invoice $invoice,
public ?string $transactionId = null
public ?string $transactionId = null,
public ?Invoice $vatInvoice = null
) {
$this->localizeTo($this->invoice);
}
Expand All @@ -28,11 +30,30 @@ public function envelope(): Envelope
{
$num = $this->invoice->invoice_num ?? $this->invoice->id;

if ($this->vatInvoice) {
return new Envelope(subject: __('email.invoice.proforma_paid_subject', [
'proforma' => $num,
'invoice' => $this->vatInvoice->invoice_num ?? $this->vatInvoice->id,
]));
}

return new Envelope(subject: __('email.invoice.paid_subject', ['number' => $num]));
}

public function content(): Content
{
if ($this->vatInvoice) {
return new Content(
view: 'emails.proforma-paid',
with: [
'invoice' => $this->invoice,
'vatInvoice' => $this->vatInvoice,
'transactionId' => $this->transactionId,
'companyName' => company_name(),
],
);
}

return new Content(
view: 'emails.invoice-paid',
with: [
Expand All @@ -42,4 +63,19 @@ public function content(): Content
],
);
}

public function attachments(): array
{
if (! $this->vatInvoice) {
return [];
}

$pdf = app(InvoicePdfService::class)->generate($this->vatInvoice);
$num = str_replace(['/', '\\'], '-', (string) ($this->vatInvoice->invoice_num ?? $this->vatInvoice->id));

return [
Attachment::fromData(fn () => $pdf->output(), "vat-invoice-{$num}.pdf")
->withMime('application/pdf'),
];
}
}
2 changes: 2 additions & 0 deletions lang/de/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,8 @@
'proforma_number_format' => 'Proforma-Nummerierungsschema',
'proforma_number_last' => 'Letzte Proforma',
'hide_paid_proformas' => 'Bezahlte Proformas ausblenden',
'proforma_combined_paid_email' => 'Eine gemeinsame E-Mail nach Zahlung einer Proforma senden',
'proforma_combined_paid_email_hint' => 'Standardmäßig aus: Der Kunde erhält die Proforma-Zahlungsbestätigung und die Umsatzsteuerrechnung getrennt. Wenn aktiviert, bestätigt eine E-Mail die Proforma-Zahlung und enthält die Umsatzsteuerrechnung als Anhang. Aufladungen ohne Umsatzsteuerrechnung behalten die normale Zahlungsbestätigung.',
'invoice_number_last' => 'Letzte Rechnung',
'invoice_number_preview' => 'Vorschau',
'invoice_number_reset_year' => 'Nummerierung jedes Jahr zurücksetzen',
Expand Down
7 changes: 7 additions & 0 deletions lang/de/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'overdue_subject' => 'Rechnung überfällig – Nr. :number',
'paid_body' => 'Vielen Dank! Wir haben Ihre Zahlung erhalten.',
'paid_subject' => 'Zahlungseingang bestätigt – Rechnung Nr. :number',
'proforma_paid_subject' => 'Proforma #:proforma bezahlt – Umsatzsteuerrechnung #:invoice',
'reminder_body' => 'Wir möchten Sie daran erinnern, dass Ihre Rechnung bald fällig wird.',
'reminder_subject' => 'Zahlungserinnerung – Rechnung Nr. :number',
],
Expand Down Expand Up @@ -145,6 +146,12 @@
'received' => 'Wir haben Ihre Zahlung erhalten. Vielen Dank!',
'transaction_id' => 'Transaktions-ID',
],
'proforma_paid' => [
'received' => 'Wir haben Ihre Zahlung für Proforma #:proforma erhalten.',
'vat_ready' => 'Ihre Umsatzsteuerrechnung #:invoice wurde ausgestellt und ist dieser Nachricht beigefügt.',
'proforma_number' => 'Proformanummer',
'vat_invoice_number' => 'Umsatzsteuerrechnungsnummer',
],
'order' => [
'confirmation_body' => 'Vielen Dank für Ihre Bestellung! Wir bearbeiten sie bereits.',
'confirmation_subject' => 'Bestellbestätigung – Nr. :number',
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,8 @@
'proforma_number_format' => 'Proforma numbering scheme',
'proforma_number_last' => 'Last proforma',
'hide_paid_proformas' => 'Hide paid proformas',
'proforma_combined_paid_email' => 'Send one combined email after a proforma is paid',
'proforma_combined_paid_email_hint' => 'Off by default: the customer receives the proforma-paid email and the VAT invoice email separately. When on, one email confirms the proforma payment and attaches the VAT invoice. Top-ups without a VAT invoice keep the normal paid email.',
'invoice_number_last' => 'Last invoice',
'invoice_number_preview' => 'Preview',
'invoice_number_reset_year' => 'Reset numbering each year',
Expand Down
7 changes: 7 additions & 0 deletions lang/en/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'overdue_subject' => 'Invoice Overdue - #:number',
'paid_body' => 'Thank you! Your payment has been received.',
'paid_subject' => 'Invoice Payment Confirmation - #:number',
'proforma_paid_subject' => 'Proforma #:proforma paid - VAT invoice #:invoice',
'reminder_body' => 'This is a friendly reminder that your invoice is due soon.',
'reminder_subject' => 'Payment Reminder - Invoice #:number',
],
Expand Down Expand Up @@ -146,6 +147,12 @@
'received' => 'We have received your payment. Thank you!',
'transaction_id' => 'Transaction ID',
],
'proforma_paid' => [
'received' => 'We have received your payment for proforma #:proforma.',
'vat_ready' => 'Your VAT invoice #:invoice has been issued and is attached to this message.',
'proforma_number' => 'Proforma number',
'vat_invoice_number' => 'VAT invoice number',
],
'order' => [
'confirmation_body' => 'Thank you for your order! We are processing it now.',
'confirmation_subject' => 'Order Confirmation - #:number',
Expand Down
2 changes: 2 additions & 0 deletions lang/pl/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,8 @@
'proforma_number_format' => 'Schemat numeracji proform',
'proforma_number_last' => 'Ostatnia proforma',
'hide_paid_proformas' => 'Nie pokazuj opłaconych proform',
'proforma_combined_paid_email' => 'Wysyłaj jeden wspólny e-mail po opłaceniu proformy',
'proforma_combined_paid_email_hint' => 'Domyślnie wysyłane są dwa e-maile. Po włączeniu jeden e-mail zawiera potwierdzenie i fakturę VAT w załączniku.',
'invoice_number_last' => 'Ostatnia faktura',
'invoice_number_preview' => 'Podgląd',
'invoice_number_reset_year' => 'Resetuj numerację co roku',
Expand Down
7 changes: 7 additions & 0 deletions lang/pl/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'overdue_subject' => 'Przeterminowana faktura – #:number',
'paid_body' => 'Dziękujemy! Twoja płatność została odebrana.',
'paid_subject' => 'Potwierdzenie płatności faktury – #:number',
'proforma_paid_subject' => 'Proforma #:proforma opłacona – faktura VAT #:invoice',
'reminder_body' => 'To przyjazne przypomnienie, że Twoja faktura wkrótce będzie wymagać zapłaty.',
'reminder_subject' => 'Przypomnienie o płatności – faktura #:number',
],
Expand Down Expand Up @@ -146,6 +147,12 @@
'received' => 'Otrzymaliśmy Twoją płatność. Dziękujemy!',
'transaction_id' => 'Identyfikator transakcji',
],
'proforma_paid' => [
'received' => 'Otrzymaliśmy płatność za proformę #:proforma.',
'vat_ready' => 'Faktura VAT #:invoice została wystawiona i jest załączona do tej wiadomości.',
'proforma_number' => 'Numer proformy',
'vat_invoice_number' => 'Numer faktury VAT',
],
'order' => [
'confirmation_body' => 'Dziękujemy za Twoje zamówienie! Właśnie je przetwarzamy.',
'confirmation_subject' => 'Potwierdzenie zamówienia – #:number',
Expand Down
2 changes: 2 additions & 0 deletions lang/tr/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3662,6 +3662,8 @@
'products.zero_tax' => '0%',
'products.unit' => 'Birim',
'settings.hide_paid_proformas' => 'Ödenmiş proformaları gizle',
'settings.proforma_combined_paid_email' => 'Proforma ödendikten sonra tek bir birleşik e-posta gönder',
'settings.proforma_combined_paid_email_hint' => 'Varsayılan olarak kapalı: müşteri proforma ödeme e-postasını ve KDV faturası e-postasını ayrı ayrı alır. Açıkken tek bir e-posta proforma ödemesini onaylar ve KDV faturasını ekler. KDV faturası olmayan bakiyeler normal ödeme e-postasını almaya devam eder.',
'clients.provision_now' => 'Hesabı sunucuda şimdi oluştur',
'clients.provision_now_hint' => 'İşaretlenirse seçili sunucu modülü Create komutunu çalıştırıp gerçek bir hesap açar. Yalnızca mevcut bir hizmeti kaydetmek (göç) için işaretlemeyin.',
'clients.service_provisioned' => 'Hizmet eklendi ve sunucuda oluşturuldu.',
Expand Down
7 changes: 7 additions & 0 deletions lang/tr/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'overdue_subject' => 'Fatura Vadesi Geçti - #:number',
'paid_body' => 'Teşekkürler! Ödemeniz alındı.',
'paid_subject' => 'Fatura Ödemesi Alındı - #:number',
'proforma_paid_subject' => 'Proforma #:proforma ödendi – KDV faturası #:invoice',
'reminder_body' => 'Faturanızın ödeme tarihinin yaklaştığını hatırlatmak isteriz.',
'reminder_subject' => 'Ödeme Hatırlatması - Fatura #:number',
],
Expand Down Expand Up @@ -145,6 +146,12 @@
'received' => 'Ödemenizi aldık. Teşekkür ederiz!',
'transaction_id' => 'İşlem No',
],
'proforma_paid' => [
'received' => 'Proforma #:proforma için ödemenizi aldık.',
'vat_ready' => 'KDV faturanız #:invoice düzenlendi ve bu mesaja eklendi.',
'proforma_number' => 'Proforma numarası',
'vat_invoice_number' => 'KDV fatura numarası',
],
'order' => [
'confirmation_body' => 'Siparişiniz için teşekkürler! Şu anda işleme alıyoruz.',
'confirmation_subject' => 'Sipariş Onayı - #:number',
Expand Down
2 changes: 2 additions & 0 deletions lang/zh/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,8 @@
'proforma_number_format' => '形式发票编号方案',
'proforma_number_last' => '最后一张形式发票',
'hide_paid_proformas' => '隐藏已付形式发票',
'proforma_combined_paid_email' => '形式发票付款后发送一封合并邮件',
'proforma_combined_paid_email_hint' => '默认关闭:客户会分别收到形式发票付款邮件和增值税发票邮件。开启后,一封邮件确认形式发票付款并附上增值税发票。没有增值税发票的充值仍使用普通付款邮件。',
'invoice_number_last' => '最后一张账单',
'invoice_number_preview' => '预览',
'invoice_number_reset_year' => '每年重置编号',
Expand Down
7 changes: 7 additions & 0 deletions lang/zh/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'overdue_subject' => '发票逾期 - #:number',
'paid_body' => '感谢您!我们已收到付款。',
'paid_subject' => '发票付款确认 - #:number',
'proforma_paid_subject' => '形式发票 #:proforma 已付款 - 增值税发票 #:invoice',
'reminder_body' => '温馨提醒:您的发票即将到期。',
'reminder_subject' => '付款提醒 - 发票 #:number',
],
Expand Down Expand Up @@ -145,6 +146,12 @@
'received' => '我们已收到您的付款,谢谢!',
'transaction_id' => '交易编号',
],
'proforma_paid' => [
'received' => '我们已收到形式发票 #:proforma 的付款。',
'vat_ready' => '您的增值税发票 #:invoice 已开具,并附在此邮件中。',
'proforma_number' => '形式发票编号',
'vat_invoice_number' => '增值税发票编号',
],
'order' => [
'confirmation_body' => '感谢您的订购!我们正在处理您的订单。',
'confirmation_subject' => '订单确认 - #:number',
Expand Down
Loading
Loading