Working on Company ledger

This commit is contained in:
David Bomba 2019-05-15 14:47:07 +10:00
parent a2a0e6738e
commit 139008ed14
10 changed files with 186 additions and 3 deletions

View File

@ -0,0 +1,31 @@
<?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\Factory;
use App\Models\CompanyLedger;
class CompanyLedgerFactory
{
public static function create(int $company_id, int $user_id) :Client
{
$company_ledger = new CompanyLedger;
$company_ledger->company_id = $company_id;
$company_ledger->user_id = $user_id;
$company_ledger->adjustment = 0;
$company_ledger->balance = 0;
$company_ledger->notes = '';
$company_ledger->hash = '';
$company_ledger->client_id = 0;
return $company_ledger;
}
}

View File

@ -0,0 +1,66 @@
<?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\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateCompanyLedgerWithInvoice
{
use Dispatchable;
public $adjustment;
public $invoice
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, float $adjustment)
{
$this->invoice = $invoice;
$this->adjustment = $adjustment;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$balance = 0;
$ledger = CompanyLedger::whereClientId($this->invoice->client_id)
->whereCompanyId($this->invoice->company_id)
->orderBy('id', 'DESC')
->first();
if($ledger)
$balance = $ledger->balance;
$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;
$this->invoice->company_ledger()->save($company_ledger);
}
}

View File

@ -0,0 +1,66 @@
<?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\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateCompanyLedgerWithPayment
{
use Dispatchable;
public $adjustment;
public $payment
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, float $adjustment)
{
$this->payment = $payment;
$this->adjustment = $adjustment;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$balance = 0;
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
->whereCompanyId($this->payment->company_id)
->orderBy('id', 'DESC')
->first();
if($ledger)
$balance = $ledger->balance;
$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;
$this->payment->company_ledger()->save($company_ledger); //todo add model directive here
}
}

View File

@ -107,5 +107,7 @@ class ApplyPaymentToInvoice implements ShouldQueue
$this->invoice->save();
/* Insert the ledger transaction */
UpdateCompanyLedgerWithPayment::dispatch($this->payment, $adjustment);
}
}

BIN
app/Listeners/Client/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -31,4 +31,9 @@ class CompanyLedger extends BaseModel
{
return $this->belongsTo(Company::class);
}
public function company_ledgerable()
{
return $this->morphTo();
}
}

View File

@ -81,6 +81,11 @@ class Invoice extends BaseModel
return $this->morphToMany(Payment::class, 'paymentable');
}
public function company_ledger()
{
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
}
/* ---------------- */
/* Settings getters */
/* ---------------- */

View File

@ -12,6 +12,7 @@
namespace App\Repositories;
use App\Helpers\Invoice\InvoiceCalc;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Models\Invoice;
use Illuminate\Http\Request;
@ -41,6 +42,8 @@ class InvoiceRepository extends BaseRepository
*/
public function save($data, Invoice $invoice) : ?Invoice
{
/* Always carry forward the initial invoice amount this is important for tracking client balance changes later......*/
$starting_amount = $invoice->amount;
$invoice->fill($data);
@ -52,6 +55,11 @@ class InvoiceRepository extends BaseRepository
$invoice->save();
$finished_amount = $invoice->amount;
if($finished_amount != $starting_amount)
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice);
return $invoice;
}

View File

@ -858,9 +858,10 @@ class CreateUsersTable extends Migration
$table->decimal('adjustment', 13, 2)->nullable();
$table->decimal('balance', 13, 2)->nullable(); //this is the clients balance carried forward
$table->text('notes');
$table->text('hash');
$table->unsignedInteger('company_ledger_id');
$table->string('company_ledger_type');
$table->unsignedInteger('company_ledgerable_id');
$table->string('company_ledgerable_type');
$table->timestamps(6);
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');

View File

@ -81,7 +81,6 @@ trait MockAccountData
$this->invoice = $this->invoice_calc->getInvoice();
$this->invoice->save();
$this->invoice->fresh();
}