From 02cd79651e36b8451ca0d973591b95070324f912 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 15 Nov 2021 15:57:24 +1100 Subject: [PATCH] Add triggered actions --- app/Services/Credit/TriggeredActions.php | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 app/Services/Credit/TriggeredActions.php diff --git a/app/Services/Credit/TriggeredActions.php b/app/Services/Credit/TriggeredActions.php new file mode 100644 index 000000000000..bf7ef3c05484 --- /dev/null +++ b/app/Services/Credit/TriggeredActions.php @@ -0,0 +1,76 @@ +request = $request; + + $this->credit = $credit; + } + + public function run() + { + // if ($this->request->has('auto_bill') && $this->request->input('auto_bill') == 'true') { + // $this->credit = $this->credit->service()->autoBill()->save(); + // } + + // if ($this->request->has('paid') && $this->request->input('paid') == 'true') { + // $this->credit = $this->credit->service()->markPaid()->save(); + // } + + // if ($this->request->has('amount_paid') && is_numeric($this->request->input('amount_paid')) ) { + // $this->credit = $this->credit->service()->applyPaymentAmount($this->request->input('amount_paid'))->save(); + // } + + if ($this->request->has('send_email') && $this->request->input('send_email') == 'true') { + $this->sendEmail(); + } + + if ($this->request->has('mark_sent') && $this->request->input('mark_sent') == 'true') { + $this->credit = $this->credit->service()->markSent()->save(); + } + + + return $this->credit; + } + + private function sendEmail() + { + + $reminder_template = $this->credit->calculateTemplate('credit'); + + $this->credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($reminder_template) { + EmailEntity::dispatch($invitation, $this->credit->company, $reminder_template); + }); + + if ($this->credit->invitations->count() > 0) { + event(new CreditWasEmailed($this->credit->invitations->first(), $this->credit->company, Ninja::eventVars(), 'credit')); + } + } +}