Send notification to admin after customer pay the invoice

This commit is contained in:
cuneytsenturk 2018-09-26 12:08:11 +03:00
parent de10f1fbce
commit 5d11df516d
3 changed files with 128 additions and 8 deletions

View File

@ -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,

View File

@ -0,0 +1,83 @@
<?php
namespace App\Notifications\Customer;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class Invoice extends Notification
{
/**
* The bill model.
*
* @var object
*/
public $invoice;
public $invoice_payment;
/**
* Create a notification instance.
*
* @param object $invoice
*/
public function __construct($invoice, $invoice_payment)
{
$this->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,
];
}
}

View File

@ -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',
],
];