Updates for ledger calculations

This commit is contained in:
David Bomba 2023-11-07 14:54:44 +11:00
parent fbb42bd880
commit 5e1d45436e
13 changed files with 113 additions and 90 deletions

View File

@ -174,17 +174,20 @@ class SendRemindersCron extends Command
$invoice->calc()->getInvoice()->save();
$invoice->fresh();
// $invoice->service()->deletePdf()->save();
if ($invoice->client->getSetting('enable_e_invoice')) {
$invoice->service()->deleteEInvoice()->save();
}
// if ($invoice->client->getSetting('enable_e_invoice')) {
// $invoice->service()->deleteEInvoice()->save();
// }
/* Refresh the client here to ensure the balance is fresh */
$client = $invoice->client;
$client = $client->fresh();
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}");
$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}");
return $invoice;
}

View File

@ -25,6 +25,7 @@ class UpdateLedger implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
public $deleteWhenMissingModels = true;
public function __construct(private int $company_ledger_id, private float $start_amount, private string $company_key, private string $db)
@ -39,6 +40,8 @@ class UpdateLedger implements ShouldQueue
*/
public function handle() :void
{
nlog("Updating company ledger for client ". $this->company_ledger_id);
MultiDB::setDb($this->db);
$cl = CompanyLedger::find($this->company_ledger_id);
@ -50,51 +53,17 @@ class UpdateLedger implements ShouldQueue
->where('id', '<', $cl->id)
->where('company_id', $cl->company_id)
->where('client_id', $cl->client_id)
->where('balance', '!=', 0)
->orderBy('id', 'DESC')
->first();
$cl->balance = $parent_ledger ? $parent_ledger->balance + $cl->adjustment : 0;
$cl->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $cl->adjustment;
$cl->save();
// CompanyLedger::query()
// ->where('company_id', $cl->company_id)
// ->where('client_id', $cl->client_id)
// ->where('balance', 0)
// ->orderBy('updated_at', 'ASC')
// ->cursor()
// ->each(function ($company_ledger) {
// $last_record = null;
// if ($company_ledger->balance == 0) {
// $last_record = CompanyLedger::query()
// ->where('company_id', $company_ledger->company_id)
// ->where('client_id', $company_ledger->client_id)
// ->where('balance', '!=', 0)
// ->orderBy('id', 'DESC')
// ->first();
// if (! $last_record) {
// $last_record = CompanyLedger::query()
// ->where('company_id', $company_ledger->company_id)
// ->where('client_id', $company_ledger->client_id)
// ->orderBy('id', 'DESC')
// ->first();
// }
// }
// if($last_record) {
// $company_ledger->balance = $last_record->balance + $company_ledger->adjustment;
// $company_ledger->save();
// }
// });
}
public function middleware()
{
return [(new WithoutOverlapping($this->company_key))->dontRelease()];
return [new WithoutOverlapping($this->company_key)];
}
}

View File

@ -312,8 +312,11 @@ class SendReminders implements ShouldQueue
/**Refresh Invoice values*/
$invoice = $invoice->calc()->getInvoice();
$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->client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
// $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}");
return $invoice;
}

View File

@ -307,9 +307,13 @@ class ReminderJob implements ShouldQueue
/**Refresh Invoice values*/
$invoice = $invoice->calc()->getInvoice();
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}");
// 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}");
return $invoice;
}

View File

@ -199,7 +199,7 @@ class BaseRepository
});
}
}
nlog($model->toArray());
$model->saveQuietly();
/* Model now persisted, now lets do some child tasks */
@ -291,12 +291,9 @@ nlog($model->toArray());
/* Perform model specific tasks */
if ($model instanceof Invoice) {
if ($model->status_id != Invoice::STATUS_DRAFT) {
// if (($state['finished_amount'] != $state['starting_amount']) && ($model->status_id != Invoice::STATUS_DRAFT)) {
$model->service()->updateStatus()->save();
// $model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
$model->client->service()->calculateBalance();
$model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number} by {$state['starting_amount']}");
// $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
$model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number}");
}
if (! $model->design_id) {

View File

@ -96,14 +96,19 @@ class AddGatewayFee extends AbstractService
if (floatval($new_balance) - floatval($balance) != 0) {
$adjustment = $new_balance - $balance;
$this->invoice
->client
->service()
->updateBalance($adjustment);
// $this->invoice
// ->client
// ->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');
}
return $this->invoice;
@ -142,15 +147,21 @@ class AddGatewayFee extends AbstractService
if (floatval($new_balance) - floatval($balance) != 0) {
$adjustment = $new_balance - $balance;
$this->invoice
->client
->service()
->updateBalance($adjustment * -1)
->save();
// $this->invoice
// ->client
// ->service()
// ->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');
}
return $this->invoice;

View File

@ -39,12 +39,18 @@ class HandleCancellation extends AbstractService
$this->backupCancellation($adjustment);
//set invoice balance to 0
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
// $this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->balance = 0;
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
$this->invoice->client->service()->updateBalance($adjustment)->save();
// $this->invoice->client->service()->updateBalance($adjustment)->save();
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for cancellation of Invoice {$this->invoice->number}");
$this->invoice->service()->workFlow()->save();
@ -63,7 +69,13 @@ 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->fresh();
/* Reverse the invoice status and balance */
@ -72,6 +84,9 @@ class HandleCancellation extends AbstractService
$this->invoice->client->service()->updateBalance($adjustment)->save();
$this->invoice->client->service()->calculateBalance();
/* Pop the cancellation out of the backup*/
$backup = $this->invoice->backup;
unset($backup->cancellation);

View File

@ -49,8 +49,11 @@ 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}");
//@todo
$this->invoice->client
->service()
->updateBalanceAndPaidToDate($this->invoice->balance, $this->invoice->paid_to_date)

View File

@ -67,7 +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()->updateInvoiceBalance($balance_remaining * -1, $notes)->save();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, $notes);
}
$this->invoice->balance = 0;

View File

@ -452,15 +452,19 @@ class InvoiceService
if ((int) $pre_count != (int) $post_count) {
$adjustment = $balance - $new_balance;
$this->invoice
->client
->service()
->updateBalance($adjustment * -1)
->save();
// $this->invoice
// ->client
// ->service()
// ->updateBalance($adjustment * -1)
// ->save();
// $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->client->service()->calculateBalance();
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee');
}
return $this;

View File

@ -41,9 +41,14 @@ 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->client->service()->calculateBalance();
/* Perform additional actions on invoice */
$this->invoice
@ -54,7 +59,7 @@ class MarkSent extends AbstractService
->save();
/*Adjust client balance*/
$this->invoice->client->service()->updateBalance($adjustment)->save();
// $this->invoice->client->service()->updateBalance($adjustment)->save();
$this->invoice->markInvitationsSent();

View File

@ -97,10 +97,14 @@ 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()
// ->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
->client
->service()

View File

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