2016-07-14 22:37:04 +02:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-14 22:37:04 +02:00
|
|
|
namespace App\Handlers;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Mailers\UserMailer;
|
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class InvoiceEventHandler
|
|
|
|
{
|
2016-07-14 22:37:04 +02:00
|
|
|
/**
|
|
|
|
* @var UserMailer
|
|
|
|
*/
|
|
|
|
protected $userMailer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ContactMailer
|
|
|
|
*/
|
|
|
|
protected $contactMailer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* InvoiceEventHandler constructor.
|
|
|
|
*
|
|
|
|
* @param UserMailer $userMailer
|
|
|
|
* @param ContactMailer $contactMailer
|
|
|
|
*/
|
|
|
|
public function __construct(UserMailer $userMailer, ContactMailer $contactMailer)
|
|
|
|
{
|
|
|
|
$this->userMailer = $userMailer;
|
|
|
|
$this->contactMailer = $contactMailer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $events
|
|
|
|
*/
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
|
|
|
$events->listen('invoice.sent', 'InvoiceEventHandler@onSent');
|
|
|
|
$events->listen('invoice.viewed', 'InvoiceEventHandler@onViewed');
|
|
|
|
$events->listen('invoice.paid', 'InvoiceEventHandler@onPaid');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Invoice $invoice
|
|
|
|
*/
|
|
|
|
public function onSent(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$this->sendNotifications($invoice, 'sent');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Invoice $invoice
|
|
|
|
*/
|
|
|
|
public function onViewed(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$this->sendNotifications($invoice, 'viewed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Payment $payment
|
|
|
|
*/
|
|
|
|
public function onPaid(Payment $payment)
|
|
|
|
{
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
|
|
|
|
$this->sendNotifications($payment->invoice, 'paid', $payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Invoice $invoice
|
|
|
|
* @param $type
|
|
|
|
* @param null $payment
|
|
|
|
*/
|
|
|
|
private function sendNotifications(Invoice $invoice, $type, $payment = null)
|
|
|
|
{
|
|
|
|
foreach ($invoice->account->users as $user) {
|
|
|
|
if ($user->{'notify_' . $type}) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$this->userMailer->sendNotification($user, $invoice, $type, $payment);
|
2016-07-14 22:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|