Change number of emails sent when auto billing recurring invoices

This commit is contained in:
David Bomba 2023-04-18 19:56:31 +10:00
parent 6329484771
commit 50eceaf9d1
2 changed files with 25 additions and 12 deletions

View File

@ -11,13 +11,14 @@
namespace App\Jobs\Cron; namespace App\Jobs\Cron;
use App\Libraries\MultiDB;
use App\Models\Invoice; use App\Models\Invoice;
use App\Libraries\MultiDB;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use App\Jobs\Entity\EmailEntity;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class AutoBill implements ShouldQueue class AutoBill implements ShouldQueue
{ {
@ -25,19 +26,13 @@ class AutoBill implements ShouldQueue
public $tries = 1; public $tries = 1;
public int $invoice_id;
public string $db;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(int $invoice_id, ?string $db) public function __construct(public int $invoice_id, public ?string $db, public bool $send_email_on_failure = false)
{ {
$this->invoice_id = $invoice_id;
$this->db = $db;
} }
/** /**
@ -61,6 +56,24 @@ class AutoBill implements ShouldQueue
$invoice->service()->autoBill(); $invoice->service()->autoBill();
} catch (\Exception $e) { } catch (\Exception $e) {
nlog("Failed to capture payment for {$this->invoice_id} ->".$e->getMessage()); nlog("Failed to capture payment for {$this->invoice_id} ->".$e->getMessage());
if($this->send_email_on_failure)
{
$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}");
}
});
}
} }
} }
} }

View File

@ -116,11 +116,11 @@ class SendRecurring implements ShouldQueue
//auto bill, BUT NOT DRAFTS!! //auto bill, BUT NOT DRAFTS!!
if ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $invoice->client->getSetting('auto_email_invoice')) { 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}"); nlog("attempting to autobill {$invoice->number}");
AutoBill::dispatch($invoice->id, $this->db)->delay(rand(1, 2)); AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2));
} }
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()))) { 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}"); nlog("attempting to autobill {$invoice->number}");
AutoBill::dispatch($invoice->id, $this->db)->delay(rand(1, 2)); AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2));
} }
elseif ($invoice->client->getSetting('auto_email_invoice')) { elseif ($invoice->client->getSetting('auto_email_invoice')) {
//Admin notification for recurring invoice sent. //Admin notification for recurring invoice sent.