From 5467fc64b1d0a94658aa690c24aae5bbea48f51b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 15 May 2019 15:03:18 +1000 Subject: [PATCH] Invoice activity listeners --- .../UpdateCompanyLedgerWithInvoice.php | 3 +- .../UpdateCompanyLedgerWithPayment.php | 7 +++ .../Activity/CreatedClientActivity.php | 8 +-- .../Invoice/CreateInvoiceActivity.php | 53 +++++++++++++++++++ .../Invoice/UpdateInvoiceActivity.php | 53 +++++++++++++++++++ app/Models/Payment.php | 5 ++ app/Providers/EventServiceProvider.php | 13 +++-- app/Repositories/InvoiceRepository.php | 15 +++++- 8 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 app/Listeners/Invoice/CreateInvoiceActivity.php create mode 100644 app/Listeners/Invoice/UpdateInvoiceActivity.php diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index eed4888ba6f3..2c0cb61a1675 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -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); } diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php index 1ce6bcd37e2b..7aaacb3774f3 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php @@ -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 diff --git a/app/Listeners/Activity/CreatedClientActivity.php b/app/Listeners/Activity/CreatedClientActivity.php index b1446234ef01..6910a6340df4 100644 --- a/app/Listeners/Activity/CreatedClientActivity.php +++ b/app/Listeners/Activity/CreatedClientActivity.php @@ -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); } } diff --git a/app/Listeners/Invoice/CreateInvoiceActivity.php b/app/Listeners/Invoice/CreateInvoiceActivity.php new file mode 100644 index 000000000000..adf71498483d --- /dev/null +++ b/app/Listeners/Invoice/CreateInvoiceActivity.php @@ -0,0 +1,53 @@ +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); + } +} diff --git a/app/Listeners/Invoice/UpdateInvoiceActivity.php b/app/Listeners/Invoice/UpdateInvoiceActivity.php new file mode 100644 index 000000000000..ddbc5888a29e --- /dev/null +++ b/app/Listeners/Invoice/UpdateInvoiceActivity.php @@ -0,0 +1,53 @@ +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); + } +} diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 0e286348a4cd..d9571d79014d 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -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'); + } } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index c6cb7f8ed3cd..1379be8b0651 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -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 => [ + ], ]; diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index face39fcc65e..ba8615175d1c 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -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; }