Send notification to admin after customer pay the invoice
This commit is contained in:
		@@ -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,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user