diff --git a/app/Listeners/Incomes/Invoice/Paid.php b/app/Listeners/Incomes/Invoice/Paid.php index 3aa88633d..9ffa17ce6 100644 --- a/app/Listeners/Incomes/Invoice/Paid.php +++ b/app/Listeners/Incomes/Invoice/Paid.php @@ -7,6 +7,7 @@ use App\Events\InvoicePaid; use App\Models\Income\Invoice; use App\Models\Income\InvoicePayment; use App\Models\Income\InvoiceHistory; +use App\Notifications\Customer\Invoice as Notification; use App\Traits\DateTime; use Date; @@ -27,14 +28,30 @@ class Paid $request = $event->request; $request['invoice_id'] = $invoice->id; - $request['account_id'] = setting('general.default_account'); + + if (!isset($request['company_id'])) { + $request['company_id'] = session('company_id'); + } + + if (!isset($request['account_id'])) { + $request['account_id'] = setting('general.default_account'); + } if (!isset($request['amount'])) { $request['amount'] = $invoice->amount; } - $request['currency_code'] = $invoice->currency_code; - $request['currency_rate'] = $invoice->currency_rate; + if (!isset($request['currency_code'])) { + $request['currency_code'] = $invoice->currency_code; + } + + if (!isset($request['currency_rate'])) { + $request['currency_rate'] = $invoice->currency_rate; + } + + if (!isset($request['notify'])) { + $request['notify'] = 0; + } $request['paid_at'] = Date::parse('now')->format('Y-m-d'); @@ -53,19 +70,34 @@ class Paid $invoice->save(); - InvoicePayment::create($request->input()); + if (!is_array($request)) { + $invoice_payment = InvoicePayment::create($request->input()); + } else { + $invoice_payment = InvoicePayment::create($request); + } $request['status_code'] = $invoice->invoice_status_code; - $request['notify'] = 0; - $desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat()); $desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format(); $request['description'] = $desc_date . ' ' . $desc_amount; - InvoiceHistory::create($request->input()); + if (!is_array($request)) { + $invoice_history = InvoiceHistory::create($request->input()); + } else { + $invoice_history = InvoiceHistory::create($request); + } + + // Customer add payment on invoice send user notification + foreach ($invoice->company->users as $user) { + if (!$user->can('read-notifications')) { + continue; + } + + $user->notify(new Notification($invoice, $invoice_payment)); + } return [ 'success' => true, diff --git a/app/Notifications/Customer/Invoice.php b/app/Notifications/Customer/Invoice.php new file mode 100644 index 000000000..6e7559ea5 --- /dev/null +++ b/app/Notifications/Customer/Invoice.php @@ -0,0 +1,83 @@ +queue = 'high'; + $this->delay = config('queue.connections.database.delay'); + + $this->invoice = $invoice; + $this->invoice_payment = $invoice_payment; + } + + /** + * Get the notification's channels. + * + * @param mixed $notifiable + * @return array|string + */ + public function via($notifiable) + { + return ['mail', 'database']; + } + + /** + * Build the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + $message = (new MailMessage) + ->line(trans('customers.notification.message', ['invoice_number' => $this->invoice->invoice_number, 'amount' => money($this->invoice_payment->amount, $this->invoice_payment->currency_code, true), 'customer' => $this->invoice->customer_name])); + + // Override per company as Laravel doesn't read config + $message->from(config('mail.from.address'), config('mail.from.name')); + + // Attach the PDF file if available + if (isset($this->invoice->pdf_path)) { + $message->attach($this->invoice->pdf_path, [ + 'mime' => 'application/pdf', + ]); + } + + $message->action(trans('customers.notification.button'), url('incomes/invoices', $this->invoice->id, true)); + + return $message; + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'invoice_id' => $this->invoice->id, + 'amount' => $this->invoice->amount, + ]; + } +} diff --git a/resources/lang/en-GB/customers.php b/resources/lang/en-GB/customers.php index cad4bc8b9..13925f49b 100644 --- a/resources/lang/en-GB/customers.php +++ b/resources/lang/en-GB/customers.php @@ -7,5 +7,10 @@ return [ 'error' => [ 'email' => 'The email has already been taken.' - ] + ], + + 'notification' => [ + 'message' => ':customer made :amount payment to invoice number :invoice_number.', + 'button' => 'Show', + ], ];