mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 14:04:38 -04:00
Invoice activity listeners
This commit is contained in:
parent
c3a94a9add
commit
5467fc64b1
@ -59,7 +59,8 @@ class UpdateCompanyLedgerWithInvoice
|
|||||||
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
||||||
$company_ledger->client_id = $this->invoice->client_id;
|
$company_ledger->client_id = $this->invoice->client_id;
|
||||||
$company_ledger->balance = $balance + $this->adjustment;
|
$company_ledger->balance = $balance + $this->adjustment;
|
||||||
|
$company_ledger->save();
|
||||||
|
|
||||||
$this->invoice->company_ledger()->save($company_ledger);
|
$this->invoice->company_ledger()->save($company_ledger);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,12 @@ namespace App\Jobs\Company;
|
|||||||
use App\Factory\CompanyLedgerFactory;
|
use App\Factory\CompanyLedgerFactory;
|
||||||
use App\Models\CompanyLedger;
|
use App\Models\CompanyLedger;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Payment;
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for update company ledger with payment.
|
||||||
|
*/
|
||||||
class UpdateCompanyLedgerWithPayment
|
class UpdateCompanyLedgerWithPayment
|
||||||
{
|
{
|
||||||
use Dispatchable;
|
use Dispatchable;
|
||||||
@ -23,6 +27,7 @@ class UpdateCompanyLedgerWithPayment
|
|||||||
public $adjustment;
|
public $adjustment;
|
||||||
|
|
||||||
public $payment
|
public $payment
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new job instance.
|
* Create a new job instance.
|
||||||
*
|
*
|
||||||
@ -47,6 +52,7 @@ class UpdateCompanyLedgerWithPayment
|
|||||||
{
|
{
|
||||||
$balance = 0;
|
$balance = 0;
|
||||||
|
|
||||||
|
/* Get the last record for the client and set the current balance*/
|
||||||
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
|
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
|
||||||
->whereCompanyId($this->payment->company_id)
|
->whereCompanyId($this->payment->company_id)
|
||||||
->orderBy('id', 'DESC')
|
->orderBy('id', 'DESC')
|
||||||
@ -59,6 +65,7 @@ class UpdateCompanyLedgerWithPayment
|
|||||||
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
||||||
$company_ledger->client_id = $this->payment->client_id;
|
$company_ledger->client_id = $this->payment->client_id;
|
||||||
$company_ledger->balance = $balance + $this->adjustment;
|
$company_ledger->balance = $balance + $this->adjustment;
|
||||||
|
$company_ledger->save();
|
||||||
|
|
||||||
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here
|
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here
|
||||||
|
|
||||||
|
@ -18,15 +18,15 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||||||
|
|
||||||
class CreatedClientActivity
|
class CreatedClientActivity
|
||||||
{
|
{
|
||||||
protected $activityRepo;
|
protected $activity_repo;
|
||||||
/**
|
/**
|
||||||
* Create the event listener.
|
* Create the event listener.
|
||||||
*
|
*
|
||||||
* @return void
|
* @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->company_id = $event->client->company_id;
|
||||||
$fields->activity_type_id = Activity::CREATE_CLIENT;
|
$fields->activity_type_id = Activity::CREATE_CLIENT;
|
||||||
|
|
||||||
$this->activityRepo->save($fields, $event->client);
|
$this->activity_repo->save($fields, $event->client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
53
app/Listeners/Invoice/CreateInvoiceActivity.php
Normal file
53
app/Listeners/Invoice/CreateInvoiceActivity.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
53
app/Listeners/Invoice/UpdateInvoiceActivity.php
Normal file
53
app/Listeners/Invoice/UpdateInvoiceActivity.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -55,4 +55,9 @@ class Payment extends BaseModel
|
|||||||
{
|
{
|
||||||
return $this->morphedByMany(Invoice::class, 'paymentable');
|
return $this->morphedByMany(Invoice::class, 'paymentable');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function company_ledger()
|
||||||
|
{
|
||||||
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,13 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Events\Client\ClientWasCreated;
|
use App\Events\Client\ClientWasCreated;
|
||||||
|
use App\Events\Invoice\InvoiceWasCreated;
|
||||||
use App\Events\Invoice\InvoiceWasMarkedSent;
|
use App\Events\Invoice\InvoiceWasMarkedSent;
|
||||||
|
use App\Events\Invoice\InvoiceWasUpdated;
|
||||||
use App\Events\Payment\PaymentWasCreated;
|
use App\Events\Payment\PaymentWasCreated;
|
||||||
use App\Events\User\UserCreated;
|
use App\Events\User\UserCreated;
|
||||||
use App\Listeners\Activity\PaymentCreatedActivity;
|
|
||||||
use App\Listeners\Activity\CreatedClientActivity;
|
use App\Listeners\Activity\CreatedClientActivity;
|
||||||
|
use App\Listeners\Activity\PaymentCreatedActivity;
|
||||||
use App\Listeners\Invoice\CreateInvoiceInvitations;
|
use App\Listeners\Invoice\CreateInvoiceInvitations;
|
||||||
use App\Listeners\SendVerificationNotification;
|
use App\Listeners\SendVerificationNotification;
|
||||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||||
@ -57,10 +59,15 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
],
|
],
|
||||||
|
|
||||||
//Invoices
|
//Invoices
|
||||||
[
|
|
||||||
InvoiceWasMarkedSent::class => [
|
InvoiceWasMarkedSent::class => [
|
||||||
CreateInvoiceInvitations::class,
|
CreateInvoiceInvitations::class,
|
||||||
]
|
],
|
||||||
|
InvoiceWasUpdated::class => [
|
||||||
|
|
||||||
|
],
|
||||||
|
InvoiceWasCreated::class => [
|
||||||
|
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
|
use App\Events\Invoice\InvoiceWasCreated;
|
||||||
|
use App\Events\Invoice\InvoiceWasUpdated;
|
||||||
use App\Helpers\Invoice\InvoiceCalc;
|
use App\Helpers\Invoice\InvoiceCalc;
|
||||||
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
@ -42,6 +44,12 @@ class InvoiceRepository extends BaseRepository
|
|||||||
*/
|
*/
|
||||||
public function save($data, Invoice $invoice) : ?Invoice
|
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......*/
|
/* Always carry forward the initial invoice amount this is important for tracking client balance changes later......*/
|
||||||
$starting_amount = $invoice->amount;
|
$starting_amount = $invoice->amount;
|
||||||
|
|
||||||
@ -55,11 +63,16 @@ class InvoiceRepository extends BaseRepository
|
|||||||
|
|
||||||
$invoice->save();
|
$invoice->save();
|
||||||
|
|
||||||
|
if($new_invoice)
|
||||||
|
event(new InvoiceWasCreated($invoice));
|
||||||
|
else
|
||||||
|
event(new InvoiceWasUpdated($invoice));
|
||||||
|
|
||||||
$finished_amount = $invoice->amount;
|
$finished_amount = $invoice->amount;
|
||||||
|
|
||||||
if($finished_amount != $starting_amount)
|
if($finished_amount != $starting_amount)
|
||||||
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice);
|
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice);
|
||||||
|
|
||||||
return $invoice;
|
return $invoice;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user