diff --git a/app/Factory/CompanyLedgerFactory.php b/app/Factory/CompanyLedgerFactory.php new file mode 100644 index 000000000000..0f05fcde2fa5 --- /dev/null +++ b/app/Factory/CompanyLedgerFactory.php @@ -0,0 +1,31 @@ +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; + } +} diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php new file mode 100644 index 000000000000..eed4888ba6f3 --- /dev/null +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -0,0 +1,66 @@ +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); + + } +} diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php new file mode 100644 index 000000000000..1ce6bcd37e2b --- /dev/null +++ b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php @@ -0,0 +1,66 @@ +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 + + } +} diff --git a/app/Jobs/Invoice/ApplyPaymentToInvoice.php b/app/Jobs/Invoice/ApplyPaymentToInvoice.php index af4b47f9d6cb..72248b32fc46 100644 --- a/app/Jobs/Invoice/ApplyPaymentToInvoice.php +++ b/app/Jobs/Invoice/ApplyPaymentToInvoice.php @@ -107,5 +107,7 @@ class ApplyPaymentToInvoice implements ShouldQueue $this->invoice->save(); + /* Insert the ledger transaction */ + UpdateCompanyLedgerWithPayment::dispatch($this->payment, $adjustment); } } diff --git a/app/Listeners/Client/.DS_Store b/app/Listeners/Client/.DS_Store new file mode 100644 index 000000000000..5008ddfcf53c Binary files /dev/null and b/app/Listeners/Client/.DS_Store differ diff --git a/app/Models/CompanyLedger.php b/app/Models/CompanyLedger.php index 57e55e69ba7e..c73033227e5f 100644 --- a/app/Models/CompanyLedger.php +++ b/app/Models/CompanyLedger.php @@ -31,4 +31,9 @@ class CompanyLedger extends BaseModel { return $this->belongsTo(Company::class); } + + public function company_ledgerable() + { + return $this->morphTo(); + } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 125274d3ae37..260cc9cb17cf 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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 */ /* ---------------- */ diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index b130f911b064..face39fcc65e 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -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; } diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index c34d0b07f341..2f1b37f51581 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -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'); diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index a64d167506c2..558542f510e1 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -81,7 +81,6 @@ trait MockAccountData $this->invoice = $this->invoice_calc->getInvoice(); $this->invoice->save(); - $this->invoice->fresh(); }