Invoice activity listeners

This commit is contained in:
David Bomba 2019-05-15 15:03:18 +10:00
parent c3a94a9add
commit 5467fc64b1
8 changed files with 148 additions and 9 deletions

View File

@ -59,7 +59,8 @@ class UpdateCompanyLedgerWithInvoice
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
$company_ledger->client_id = $this->invoice->client_id;
$company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save();
$this->invoice->company_ledger()->save($company_ledger);
}

View File

@ -14,8 +14,12 @@ namespace App\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use App\Models\Payment;
use Illuminate\Foundation\Bus\Dispatchable;
/**
* Class for update company ledger with payment.
*/
class UpdateCompanyLedgerWithPayment
{
use Dispatchable;
@ -23,6 +27,7 @@ class UpdateCompanyLedgerWithPayment
public $adjustment;
public $payment
/**
* Create a new job instance.
*
@ -47,6 +52,7 @@ class UpdateCompanyLedgerWithPayment
{
$balance = 0;
/* Get the last record for the client and set the current balance*/
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
->whereCompanyId($this->payment->company_id)
->orderBy('id', 'DESC')
@ -59,6 +65,7 @@ class UpdateCompanyLedgerWithPayment
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
$company_ledger->client_id = $this->payment->client_id;
$company_ledger->balance = $balance + $this->adjustment;
$company_ledger->save();
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here

View File

@ -18,15 +18,15 @@ use Illuminate\Queue\InteractsWithQueue;
class CreatedClientActivity
{
protected $activityRepo;
protected $activity_repo;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(ActivityRepository $activityRepo)
public function __construct(ActivityRepository $activity_repo)
{
$this->activityRepo = $activityRepo;
$this->activity_repo = $activity_repo;
}
/**
@ -45,6 +45,6 @@ class CreatedClientActivity
$fields->company_id = $event->client->company_id;
$fields->activity_type_id = Activity::CREATE_CLIENT;
$this->activityRepo->save($fields, $event->client);
$this->activity_repo->save($fields, $event->client);
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Listeners\Invoice;
use App\Models\ClientContact;
use App\Models\InvoiceInvitation;
use App\Repositories\ActivityRepository;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class CreateInvoiceActivity
{
protected $activity_repo;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(ActivityRepository $activity_repo)
{
$this->activity_repo = $activity_repo;
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
$fields = new \stdClass;
$fields->client_id = $event->invoice->id;
$fields->user_id = $event->invoice->user_id;
$fields->company_id = $event->invoice->company_id;
$fields->activity_type_id = Activity::CREATE_INVOIE;
$this->activity_repo->save($fields, $event->invoice);
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Listeners\Invoice;
use App\Models\ClientContact;
use App\Models\InvoiceInvitation;
use App\Repositories\ActivityRepository;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class UpdateInvoiceActivity
{
protected $activity_repo;
/**
* Create the event listener.
*
* @return void
*/
public function __construct(ActivityRepository $activity_repo)
{
$this->activity_repo = $activity_repo;
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
$fields = new \stdClass;
$fields->client_id = $event->invoice->id;
$fields->user_id = $event->invoice->user_id;
$fields->company_id = $event->invoice->company_id;
$fields->activity_type_id = Activity::UPDATE_INVOICE;
$this->activity_repo->save($fields, $event->invoice);
}
}

View File

@ -55,4 +55,9 @@ class Payment extends BaseModel
{
return $this->morphedByMany(Invoice::class, 'paymentable');
}
public function company_ledger()
{
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
}
}

View File

@ -12,11 +12,13 @@
namespace App\Providers;
use App\Events\Client\ClientWasCreated;
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Events\Payment\PaymentWasCreated;
use App\Events\User\UserCreated;
use App\Listeners\Activity\PaymentCreatedActivity;
use App\Listeners\Activity\CreatedClientActivity;
use App\Listeners\Activity\PaymentCreatedActivity;
use App\Listeners\Invoice\CreateInvoiceInvitations;
use App\Listeners\SendVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@ -57,10 +59,15 @@ class EventServiceProvider extends ServiceProvider
],
//Invoices
[
InvoiceWasMarkedSent::class => [
CreateInvoiceInvitations::class,
]
],
InvoiceWasUpdated::class => [
],
InvoiceWasCreated::class => [
],
];

View File

@ -11,6 +11,8 @@
namespace App\Repositories;
use App\Events\Invoice\InvoiceWasCreated;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Invoice\InvoiceCalc;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Models\Invoice;
@ -42,6 +44,12 @@ class InvoiceRepository extends BaseRepository
*/
public function save($data, Invoice $invoice) : ?Invoice
{
/* Test if this is a new invoice or existing */
$new_invoice = true;
if(isset($invoice->id))
$new_invoice = false;
/* Always carry forward the initial invoice amount this is important for tracking client balance changes later......*/
$starting_amount = $invoice->amount;
@ -55,11 +63,16 @@ class InvoiceRepository extends BaseRepository
$invoice->save();
if($new_invoice)
event(new InvoiceWasCreated($invoice));
else
event(new InvoiceWasUpdated($invoice));
$finished_amount = $invoice->amount;
if($finished_amount != $starting_amount)
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice);
return $invoice;
}