Rollback for ledger adjustments

This commit is contained in:
David Bomba 2023-11-07 21:58:52 +11:00
parent 2c5b94c474
commit db826a6a34
13 changed files with 69 additions and 76 deletions

View File

@ -172,22 +172,14 @@ class SendRemindersCron extends Command
/**Refresh Invoice values*/
$invoice->calc()->getInvoice()->save();
$invoice->fresh();
// $invoice->service()->deletePdf()->save();
// if ($invoice->client->getSetting('enable_e_invoice')) {
// $invoice->service()->deleteEInvoice()->save();
// }
$invoice = $invoice->fresh();
/* Refresh the client here to ensure the balance is fresh */
$client = $invoice->client;
$client = $client->fresh();
$client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Update adjustment for invoice {$invoice->number}");
// nlog('adjusting client balance and invoice balance by '.($invoice->balance - $temp_invoice_balance));
// $client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
return $invoice;
}

View File

@ -46,12 +46,6 @@ class UpdateLedger implements ShouldQueue
$cl = CompanyLedger::find($this->company_ledger_id);
// $ledger_item = CompanyLedger::query()
// ->where('company_id', $cl->company_id)
// ->where('client_id', $cl->client_id)
// ->where('company_ledgerable_id', $cl->company_ledgerable_id)
// ->where('company_ledgerable_type', $cl->company_ledgerable_type)
// ->exists();
$ledger_item = $cl->company_ledgerable->company_ledger()->count() == 1;
nlog($cl->company_ledgerable->company_ledger()->count());

View File

@ -312,11 +312,10 @@ class SendReminders implements ShouldQueue
/**Refresh Invoice values*/
$invoice = $invoice->calc()->getInvoice();
$invoice->client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
// $invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
return $invoice;
}

View File

@ -309,11 +309,8 @@ class ReminderJob implements ShouldQueue
// nlog('adjusting client balance and invoice balance by #'.$invoice->number.' '.($invoice->balance - $temp_invoice_balance));
// $invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance);
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
return $invoice;
}

View File

@ -101,13 +101,11 @@ class AddGatewayFee extends AbstractService
// ->service()
// ->updateBalance($adjustment);
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee');
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment or Late Fee ({$adjustment}) Invoice {$this->invoice->number}");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee');
$this->invoice->client->service()->calculateBalance();
}
@ -153,14 +151,11 @@ $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustme
// ->updateBalance($adjustment * -1)
// ->save();
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment * -1, 'Adjustment for adding gateway DISCOUNT');
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment or Late Fee (-{$adjustment}) Invoice {$this->invoice->number}");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment * -1, 'Adjustment for adding gateway DISCOUNT');
$this->invoice->client->service()->calculateBalance();
}

View File

@ -34,7 +34,7 @@ class HandleCancellation extends AbstractService
return $this->invoice;
}
$adjustment = $this->invoice->balance * -1;
$adjustment =($this->invoice->balance < 0) ? abs($this->invoice->balance) : $this->invoice->balance * -1;
$this->backupCancellation($adjustment);
@ -64,11 +64,9 @@ class HandleCancellation extends AbstractService
/* Will turn the negative cancellation amount to a positive adjustment*/
$adjustment = $cancellation->adjustment * -1;
// $this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} reversal");
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} reversal");
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} reversal");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} reversal");
$this->invoice->fresh();

View File

@ -49,9 +49,9 @@ 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();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Restored invoice {$this->invoice->number}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Restored invoice {$this->invoice->number}");
//@todo
$this->invoice->client

View File

@ -67,8 +67,8 @@ class HandleReversal extends AbstractService
/* Set invoice balance to 0 */
if ($this->invoice->balance != 0) {
// $this->invoice->ledger()->updateInvoiceBalance($balance_remaining * -1, $notes)->save();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, $notes);
$this->invoice->ledger()->updateInvoiceBalance($balance_remaining * -1, $notes)->save();
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, $notes);
}
$this->invoice->balance = 0;

View File

@ -458,11 +458,11 @@ class InvoiceService
// ->updateBalance($adjustment * -1)
// ->save();
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee');
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee');
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for removing gateway fee {$adjustment} Invoice {$this->invoice->number}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for removing gateway fee {$adjustment} Invoice {$this->invoice->number}");
$this->invoice->client->service()->calculateBalance();
}

View File

@ -41,12 +41,12 @@ class MarkSent extends AbstractService
->save();
/*Update ledger*/
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} marked as sent => {$this->invoice->amount}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} marked as sent => {$this->invoice->amount}");
$this->invoice->client->service()->calculateBalance();

View File

@ -97,12 +97,12 @@ class DeletePayment
->updatePaidToDate($net_deletable * -1)
->save();
// $paymentable_invoice->ledger()
// ->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
// ->save();
$paymentable_invoice->ledger()
->mutateInvoiceBalance($paymentable_invoice->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}");
->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save();
// $paymentable_invoice->ledger()
// ->mutateInvoiceBalance($paymentable_invoice->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}");
//@todo refactor
$this->payment

View File

@ -290,12 +290,12 @@ class RefundPayment
->updatePaidToDate($refunded_invoice['amount'] * -1)
->save();
// $invoice->ledger()
// ->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
// ->save();
$invoice->ledger()
->mutateInvoiceBalance($invoice->amount, "Refund of payment # {$this->payment->number}");
->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
->save();
// $invoice->ledger()
// ->mutateInvoiceBalance($invoice->amount, "Refund of payment # {$this->payment->number}");
if ($invoice->amount == $invoice->balance) {

View File

@ -20,11 +20,12 @@ use App\Models\Invoice;
use App\Models\Payment;
use App\Models\CompanyToken;
use App\Models\ClientContact;
use App\Models\CompanyLedger;
use App\DataMapper\InvoiceItem;
use App\Utils\Traits\MakesHash;
use App\Jobs\Ledger\UpdateLedger;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Jobs\Ledger\UpdateLedger;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -165,22 +166,39 @@ class CompanyLedgerTest extends TestCase
$this->assertEquals(0, $i->company_ledger()->count());
$i->service()->markSent()->save();
$i = $i->fresh();
// $i->service()->markSent()->save();
// $i = $i->fresh();
// \Illuminate\Support\Facades\Bus::fake();
// \Illuminate\Support\Facades\Bus::assertDispatched(UpdateLedger::class);
// // \Illuminate\Support\Facades\Bus::fake();
// // \Illuminate\Support\Facades\Bus::assertDispatched(UpdateLedger::class);
$this->assertEquals(1, $i->company_ledger()->count());
// $this->assertEquals(1, $i->company_ledger()->count());
$cl = $i->company_ledger()->first();
(new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
// $cl = $i->company_ledger()->first();
// (new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
// $cl = $cl->fresh();
// $this->assertEquals(100, $cl->adjustment);
// $this->assertEquals(100, $cl->balance);
// $i->service()->applyPaymentAmount(40, "notes")->save();
// $i = $i->fresh();
// $cl = CompanyLedger::where('client_id', $i->client_id)
// ->orderBy('id', 'desc')
// ->first();
// $cl = $i->company_ledger()->orderBy('id','desc')->first();
// (new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
// $cl = $cl->fresh();
// nlog($cl->toArray());
// $this->assertEquals(-40, $cl->adjustment);
// $this->assertEquals(60, $cl->balance);
$cl = $cl->fresh();
$this->assertEquals(100, $cl->adjustment);
$this->assertEquals(100, $cl->balance);
}