Merge pull request #501 from cuneytsenturk/1.3-dev
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,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										83
									
								
								app/Notifications/Customer/Invoice.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								app/Notifications/Customer/Invoice.php
									
									
									
									
									
										Normal 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,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,6 +7,7 @@ use App\Events\InvoicePaid;
 | 
			
		||||
use Illuminate\Http\Response;
 | 
			
		||||
use Illuminate\Routing\Controller;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
use App\Http\Requests\Customer\InvoicePayment as PaymentRequest;
 | 
			
		||||
use App\Http\Requests\Customer\InvoiceConfirm as ConfirmRequest;
 | 
			
		||||
 | 
			
		||||
@@ -44,4 +45,25 @@ class OfflinePayment extends Controller
 | 
			
		||||
            'html' => $html,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function confirm(Invoice $invoice, Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        $message = trans('messages.success.added', ['type' => trans_choice('general.customers', 1)]);
 | 
			
		||||
 | 
			
		||||
        flash($message)->success();
 | 
			
		||||
 | 
			
		||||
        $request_invoice_paid = [
 | 
			
		||||
            'amount' => $invoice->amount,
 | 
			
		||||
            'currency_code' => $invoice->currency_code,
 | 
			
		||||
            'currency_rate' => $invoice->currency_rate,
 | 
			
		||||
            'payment_method' => $request['payment_method'],
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        event(new InvoicePaid($invoice, $request_invoice_paid));
 | 
			
		||||
 | 
			
		||||
        return response()->json([
 | 
			
		||||
            'error' => false,
 | 
			
		||||
            'success' => true,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,4 +9,5 @@ Route::group(['middleware' => ['web', 'auth', 'language', 'adminmenu', 'permissi
 | 
			
		||||
 | 
			
		||||
Route::group(['middleware' => ['web', 'auth', 'language', 'customermenu', 'permission:read-customer-panel'], 'prefix' => 'customers', 'namespace' => 'Modules\OfflinePayment\Http\Controllers'], function () {
 | 
			
		||||
    Route::get('invoices/{invoice}/offlinepayment', 'OfflinePayment@show');
 | 
			
		||||
    Route::post('invoices/{invoice}/offlinepayment/confirm', 'OfflinePayment@confirm');
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -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',
 | 
			
		||||
    ],
 | 
			
		||||
];
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user