From a0fab8d22dfc906d93714f3bf56b06acb2a6634a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 4 Aug 2023 07:19:26 +1000 Subject: [PATCH] Fixes for recurring invoice email logic --- app/Jobs/RecurringInvoice/SendRecurring.php | 49 ++++++++++++------- app/Services/Invoice/AutoBillInvoice.php | 52 --------------------- app/Services/Pdf/PdfBuilder.php | 6 +-- app/Services/Pdf/PdfMock.php | 1 - 4 files changed, 35 insertions(+), 73 deletions(-) diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index c398f0f2d363..d82df8f7b235 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -110,33 +110,50 @@ class SendRecurring implements ShouldQueue if ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $invoice->client->getSetting('auto_email_invoice')) { nlog("attempting to autobill {$invoice->number}"); AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2)); + + //edge case to support where online payment notifications are not enabled + if(!$invoice->client->getSetting('client_online_payment_notification')){ + $this->sendRecurringEmails($invoice); + } } elseif ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_due_date' && $invoice->client->getSetting('auto_email_invoice') && ($invoice->due_date && Carbon::parse($invoice->due_date)->startOfDay()->lte(now()->startOfDay()))) { nlog("attempting to autobill {$invoice->number}"); AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2)); - } - elseif ($invoice->client->getSetting('auto_email_invoice')) { - //Admin notification for recurring invoice sent. - if ($invoice->invitations->count() >= 1) { - $invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice'); + + //edge case to support where online payment notifications are not enabled + if(!$invoice->client->getSetting('client_online_payment_notification')) { + $this->sendRecurringEmails($invoice); } - $invoice->invitations->each(function ($invitation) use ($invoice) { - if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) { - try { - EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1, 2)); - } catch (\Exception $e) { - nlog($e->getMessage()); - } - - nlog("Firing email for invoice {$invoice->number}"); - } - }); + } + elseif ($invoice->client->getSetting('auto_email_invoice')) { + $this->sendRecurringEmails($invoice); } } + private function sendRecurringEmails(Invoice $invoice) + { + //Admin notification for recurring invoice sent. + if ($invoice->invitations->count() >= 1) { + $invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice'); + } + + $invoice->invitations->each(function ($invitation) use ($invoice) { + if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) { + try { + EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1, 2)); + } catch (\Exception $e) { + nlog($e->getMessage()); + } + + nlog("Firing email for invoice {$invoice->number}"); + } + }); + + } + /** * Only create the invitations that are defined on the recurring invoice. * @param Invoice $invoice diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index d67b4fa8607f..c05aecf18ba4 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -310,25 +310,6 @@ class AutoBillInvoice extends AbstractService return $this; } - // private function applyPaymentToCredit($credit, $amount) :Credit - // { - // $credit_item = new InvoiceItem; - // $credit_item->type_id = '1'; - // $credit_item->product_key = ctrans('texts.credit'); - // $credit_item->notes = ctrans('texts.credit_payment', ['invoice_number' => $this->invoice->number]); - // $credit_item->quantity = 1; - // $credit_item->cost = $amount * -1; - - // $credit_items = $credit->line_items; - // $credit_items[] = $credit_item; - - // $credit->line_items = $credit_items; - - // $credit = $credit->calc()->getCredit(); - // $credit->save(); - - // return $credit; - // } /** * Harvests a client gateway token which passes the @@ -371,37 +352,4 @@ class AutoBillInvoice extends AbstractService return false; } - /** - * Adds a gateway fee to the invoice. - * - * @param float $fee The fee amount. - * @return AutoBillInvoice - * @deprecated / unused - */ - // private function addFeeToInvoice(float $fee) - // { - // //todo if we increase the invoice balance here, we will also need to adjust UP the client balance and ledger? - // $starting_amount = $this->invoice->amount; - - // $item = new InvoiceItem; - // $item->quantity = 1; - // $item->cost = $fee; - // $item->notes = ctrans('texts.online_payment_surcharge'); - // $item->type_id = 3; - - // $items = (array) $this->invoice->line_items; - // $items[] = $item; - - // $this->invoice->line_items = $items; - // $this->invoice->saveQuietly(); - - // $this->invoice = $this->invoice->calc()->getInvoice()->saveQuietly(); - - // if ($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT) { - // $this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save(); - // $this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, "Invoice {$this->invoice->number} balance updated after stale gateway fee removed")->save(); - // } - - // return $this; - // } } diff --git a/app/Services/Pdf/PdfBuilder.php b/app/Services/Pdf/PdfBuilder.php index 0e55f97433f7..819130cff9ef 100644 --- a/app/Services/Pdf/PdfBuilder.php +++ b/app/Services/Pdf/PdfBuilder.php @@ -39,7 +39,6 @@ class PdfBuilder /** * The DOM Document; * - * @var $document */ public DomDocument $document; @@ -619,7 +618,6 @@ class PdfBuilder * * @param mixed $items * @param string $table_type - * @param mixed|null $custom_fields * * @return array */ @@ -1313,7 +1311,7 @@ class PdfBuilder * Generates the custom values for the * entity. * - * @param array + * @param array $variables * @return array */ public function genericDetailsBuilder(array $variables): array @@ -1470,7 +1468,7 @@ class PdfBuilder * Passes an array of items by reference * and performs a nl2br * - * @param array + * @param array $variables * @return void * */ diff --git a/app/Services/Pdf/PdfMock.php b/app/Services/Pdf/PdfMock.php index 06a572c0680d..34ac517d975e 100644 --- a/app/Services/Pdf/PdfMock.php +++ b/app/Services/Pdf/PdfMock.php @@ -183,7 +183,6 @@ class PdfMock /** * getStubVariables * - * @return void */ public function getStubVariables() {