Fixes for ledger adjustments

This commit is contained in:
David Bomba 2021-01-21 15:05:05 +11:00
parent ff4f2f3953
commit 05ea7f092a
5 changed files with 25 additions and 13 deletions

View File

@ -687,8 +687,8 @@ class InvoiceController extends BaseController
break;
case 'delete':
//need to make sure the invoice is cancelled first!!
//$invoice->service()->handleCancellation()->save();
//$invoice->service()->handleCancellation()s->save();
nlog("inside delete");
$this->invoice_repo->delete($invoice);
if (! $bulk) {

View File

@ -64,7 +64,6 @@ class PaymentRepository extends BaseRepository
*/
private function applyPayment(array $data, Payment $payment): ?Payment
{
nlog($data);
$is_existing_payment = true;

View File

@ -71,14 +71,15 @@ class HandleRestore extends AbstractService
}
//adjust ledger balance
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, 'Restored invoice {$this->invoice->number}')->save();
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Restored invoice {$this->invoice->number}")->save();
//adjust paid to dates
$this->invoice->client->service()->updatePaidToDate($this->payment_total)->save();
$this->invoice->client->service()->updateBalance($this->invoice->balance)->save();
$this->invoice->ledger()->updatePaymentBalance($this->payment_total, 'Restored payment for invoice {$this->invoice->number}')->save();
// you only need to touch the ledger ONCE per transaction.
// $this->invoice->ledger()->updatePaymentBalance($this->payment_total*-1, "Restored payment for invoice {$this->invoice->number}")->save();
$this->windBackInvoiceNumber();

View File

@ -36,9 +36,6 @@ class MarkInvoiceDeleted extends AbstractService
if ($this->invoice->is_deleted) {
return $this->invoice;
}
// if(in_array($this->invoice->status_id, ['currencies', 'industries', 'languages', 'countries', 'banks']))
// return $this->
$this->cleanup()
->setAdjustmentAmount()
@ -53,25 +50,26 @@ class MarkInvoiceDeleted extends AbstractService
private function adjustLedger()
{
$this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1);
$this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1, 'Invoice Deleted - reducing ledger balance'); //reduces the payment balance by payment totals
return $this;
}
private function adjustPaidToDate()
{
$this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save();
$this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); //reduces the paid to date by the payment totals
return $this;
}
private function adjustBalance()
{
$this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save();
$this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save(); //reduces the client balance by the invoice amount.
return $this;
}
/* Adjust the payment amounts */
private function adjustPayments()
{
//if total payments = adjustment amount - that means we need to delete the payments as well.
@ -79,6 +77,7 @@ class MarkInvoiceDeleted extends AbstractService
if ($this->adjustment_amount == $this->total_payments) {
$this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]);
} else {
//adjust payments down by the amount applied to the invoice payment.
$this->invoice->payments->each(function ($payment) {
@ -96,6 +95,12 @@ class MarkInvoiceDeleted extends AbstractService
return $this;
}
/**
* Set the values of two variables
*
* $this->adjustment_amount - sum of the invoice paymentables
* $this->total_payments - sum of the invoice payments
*/
private function setAdjustmentAmount()
{
foreach ($this->invoice->payments as $payment) {
@ -111,6 +116,12 @@ class MarkInvoiceDeleted extends AbstractService
return $this;
}
/*
*
* This sets the invoice number to _deleted
* and also removes the links to existing entities
*
*/
private function cleanup()
{
$check = false;
@ -143,7 +154,7 @@ class MarkInvoiceDeleted extends AbstractService
return $number;
}
/* Touches all paymentables as deleted */
private function deletePaymentables()
{
$this->invoice->payments->each(function ($payment) {

View File

@ -47,7 +47,7 @@ class LedgerService
return $this;
}
public function updatePaymentBalance($adjustment)
public function updatePaymentBalance($adjustment, $notes = '')
{
$balance = 0;
@ -63,6 +63,7 @@ class LedgerService
$company_ledger->adjustment = $adjustment;
$company_ledger->balance = $balance + $adjustment;
$company_ledger->activity_id = Activity::UPDATE_PAYMENT;
$company_ledger->notes = $notes;
$company_ledger->save();
$this->entity->company_ledger()->save($company_ledger);