mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 16:21:01 -04:00
Updates for ledger calculations
This commit is contained in:
parent
fbb42bd880
commit
5e1d45436e
@ -174,17 +174,20 @@ class SendRemindersCron extends Command
|
|||||||
$invoice->calc()->getInvoice()->save();
|
$invoice->calc()->getInvoice()->save();
|
||||||
$invoice->fresh();
|
$invoice->fresh();
|
||||||
// $invoice->service()->deletePdf()->save();
|
// $invoice->service()->deletePdf()->save();
|
||||||
if ($invoice->client->getSetting('enable_e_invoice')) {
|
// if ($invoice->client->getSetting('enable_e_invoice')) {
|
||||||
$invoice->service()->deleteEInvoice()->save();
|
// $invoice->service()->deleteEInvoice()->save();
|
||||||
}
|
// }
|
||||||
|
|
||||||
/* Refresh the client here to ensure the balance is fresh */
|
/* Refresh the client here to ensure the balance is fresh */
|
||||||
$client = $invoice->client;
|
$client = $invoice->client;
|
||||||
$client = $client->fresh();
|
$client = $client->fresh();
|
||||||
|
|
||||||
nlog('adjusting client balance and invoice balance by '.($invoice->balance - $temp_invoice_balance));
|
$client->service()->calculateBalance();
|
||||||
$client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
|
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Update adjustment for invoice {$invoice->number}");
|
||||||
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee 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;
|
return $invoice;
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ class UpdateLedger implements ShouldQueue
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
public $tries = 1;
|
public $tries = 1;
|
||||||
|
|
||||||
public $deleteWhenMissingModels = true;
|
public $deleteWhenMissingModels = true;
|
||||||
|
|
||||||
public function __construct(private int $company_ledger_id, private float $start_amount, private string $company_key, private string $db)
|
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
|
public function handle() :void
|
||||||
{
|
{
|
||||||
|
nlog("Updating company ledger for client ". $this->company_ledger_id);
|
||||||
|
|
||||||
MultiDB::setDb($this->db);
|
MultiDB::setDb($this->db);
|
||||||
|
|
||||||
$cl = CompanyLedger::find($this->company_ledger_id);
|
$cl = CompanyLedger::find($this->company_ledger_id);
|
||||||
@ -50,51 +53,17 @@ class UpdateLedger implements ShouldQueue
|
|||||||
->where('id', '<', $cl->id)
|
->where('id', '<', $cl->id)
|
||||||
->where('company_id', $cl->company_id)
|
->where('company_id', $cl->company_id)
|
||||||
->where('client_id', $cl->client_id)
|
->where('client_id', $cl->client_id)
|
||||||
|
->where('balance', '!=', 0)
|
||||||
->orderBy('id', 'DESC')
|
->orderBy('id', 'DESC')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$cl->balance = $parent_ledger ? $parent_ledger->balance + $cl->adjustment : 0;
|
$cl->balance = ($parent_ledger ? $parent_ledger->balance : 0) + $cl->adjustment;
|
||||||
$cl->save();
|
$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()
|
public function middleware()
|
||||||
{
|
{
|
||||||
return [(new WithoutOverlapping($this->company_key))->dontRelease()];
|
return [new WithoutOverlapping($this->company_key)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,8 +312,11 @@ class SendReminders implements ShouldQueue
|
|||||||
/**Refresh Invoice values*/
|
/**Refresh Invoice values*/
|
||||||
$invoice = $invoice->calc()->getInvoice();
|
$invoice = $invoice->calc()->getInvoice();
|
||||||
|
|
||||||
$invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
|
$invoice->client->service()->calculateBalance();
|
||||||
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
|
$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;
|
return $invoice;
|
||||||
}
|
}
|
||||||
|
@ -307,9 +307,13 @@ class ReminderJob implements ShouldQueue
|
|||||||
/**Refresh Invoice values*/
|
/**Refresh Invoice values*/
|
||||||
$invoice = $invoice->calc()->getInvoice();
|
$invoice = $invoice->calc()->getInvoice();
|
||||||
|
|
||||||
nlog('adjusting client balance and invoice balance by #'.$invoice->number.' '.($invoice->balance - $temp_invoice_balance));
|
// 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->client->service()->updateBalance($invoice->balance - $temp_invoice_balance);
|
||||||
$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}");
|
||||||
|
|
||||||
|
$invoice->client->service()->calculateBalance();
|
||||||
|
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
|
||||||
|
|
||||||
|
|
||||||
return $invoice;
|
return $invoice;
|
||||||
}
|
}
|
||||||
|
@ -199,7 +199,7 @@ class BaseRepository
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nlog($model->toArray());
|
|
||||||
$model->saveQuietly();
|
$model->saveQuietly();
|
||||||
|
|
||||||
/* Model now persisted, now lets do some child tasks */
|
/* Model now persisted, now lets do some child tasks */
|
||||||
@ -291,12 +291,9 @@ nlog($model->toArray());
|
|||||||
/* Perform model specific tasks */
|
/* Perform model specific tasks */
|
||||||
if ($model instanceof Invoice) {
|
if ($model instanceof Invoice) {
|
||||||
if ($model->status_id != Invoice::STATUS_DRAFT) {
|
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->service()->updateStatus()->save();
|
||||||
// $model->client->service()->updateBalance(($state['finished_amount'] - $state['starting_amount']))->save();
|
|
||||||
$model->client->service()->calculateBalance();
|
$model->client->service()->calculateBalance();
|
||||||
$model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number} by {$state['starting_amount']}");
|
$model->ledger()->mutateInvoiceBalance($state['starting_amount'], "Update adjustment for invoice {$model->number}");
|
||||||
// $model->ledger()->updateInvoiceBalance(($state['finished_amount'] - $state['starting_amount']), "Update adjustment for invoice {$model->number}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! $model->design_id) {
|
if (! $model->design_id) {
|
||||||
|
@ -96,14 +96,19 @@ class AddGatewayFee extends AbstractService
|
|||||||
if (floatval($new_balance) - floatval($balance) != 0) {
|
if (floatval($new_balance) - floatval($balance) != 0) {
|
||||||
$adjustment = $new_balance - $balance;
|
$adjustment = $new_balance - $balance;
|
||||||
|
|
||||||
$this->invoice
|
// $this->invoice
|
||||||
->client
|
// ->client
|
||||||
->service()
|
// ->service()
|
||||||
->updateBalance($adjustment);
|
// ->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;
|
return $this->invoice;
|
||||||
@ -142,15 +147,21 @@ class AddGatewayFee extends AbstractService
|
|||||||
if (floatval($new_balance) - floatval($balance) != 0) {
|
if (floatval($new_balance) - floatval($balance) != 0) {
|
||||||
$adjustment = $new_balance - $balance;
|
$adjustment = $new_balance - $balance;
|
||||||
|
|
||||||
$this->invoice
|
// $this->invoice
|
||||||
->client
|
// ->client
|
||||||
->service()
|
// ->service()
|
||||||
->updateBalance($adjustment * -1)
|
// ->updateBalance($adjustment * -1)
|
||||||
->save();
|
// ->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;
|
return $this->invoice;
|
||||||
|
@ -39,12 +39,18 @@ class HandleCancellation extends AbstractService
|
|||||||
$this->backupCancellation($adjustment);
|
$this->backupCancellation($adjustment);
|
||||||
|
|
||||||
//set invoice balance to 0
|
//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->balance = 0;
|
||||||
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
|
$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();
|
$this->invoice->service()->workFlow()->save();
|
||||||
|
|
||||||
@ -63,7 +69,13 @@ class HandleCancellation extends AbstractService
|
|||||||
/* Will turn the negative cancellation amount to a positive adjustment*/
|
/* Will turn the negative cancellation amount to a positive adjustment*/
|
||||||
$adjustment = $cancellation->adjustment * -1;
|
$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();
|
$this->invoice->fresh();
|
||||||
|
|
||||||
/* Reverse the invoice status and balance */
|
/* 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()->updateBalance($adjustment)->save();
|
||||||
|
|
||||||
|
$this->invoice->client->service()->calculateBalance();
|
||||||
|
|
||||||
|
|
||||||
/* Pop the cancellation out of the backup*/
|
/* Pop the cancellation out of the backup*/
|
||||||
$backup = $this->invoice->backup;
|
$backup = $this->invoice->backup;
|
||||||
unset($backup->cancellation);
|
unset($backup->cancellation);
|
||||||
|
@ -49,8 +49,11 @@ class HandleRestore extends AbstractService
|
|||||||
}
|
}
|
||||||
|
|
||||||
//adjust ledger balance
|
//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
|
$this->invoice->client
|
||||||
->service()
|
->service()
|
||||||
->updateBalanceAndPaidToDate($this->invoice->balance, $this->invoice->paid_to_date)
|
->updateBalanceAndPaidToDate($this->invoice->balance, $this->invoice->paid_to_date)
|
||||||
|
@ -67,7 +67,8 @@ class HandleReversal extends AbstractService
|
|||||||
|
|
||||||
/* Set invoice balance to 0 */
|
/* Set invoice balance to 0 */
|
||||||
if ($this->invoice->balance != 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;
|
$this->invoice->balance = 0;
|
||||||
|
@ -452,15 +452,19 @@ class InvoiceService
|
|||||||
if ((int) $pre_count != (int) $post_count) {
|
if ((int) $pre_count != (int) $post_count) {
|
||||||
$adjustment = $balance - $new_balance;
|
$adjustment = $balance - $new_balance;
|
||||||
|
|
||||||
$this->invoice
|
// $this->invoice
|
||||||
->client
|
// ->client
|
||||||
->service()
|
// ->service()
|
||||||
->updateBalance($adjustment * -1)
|
// ->updateBalance($adjustment * -1)
|
||||||
->save();
|
// ->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;
|
return $this;
|
||||||
|
@ -41,9 +41,14 @@ class MarkSent extends AbstractService
|
|||||||
->save();
|
->save();
|
||||||
|
|
||||||
/*Update ledger*/
|
/*Update ledger*/
|
||||||
$this->invoice
|
// $this->invoice
|
||||||
->ledger()
|
// ->ledger()
|
||||||
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
|
// ->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 */
|
/* Perform additional actions on invoice */
|
||||||
$this->invoice
|
$this->invoice
|
||||||
@ -54,7 +59,7 @@ class MarkSent extends AbstractService
|
|||||||
->save();
|
->save();
|
||||||
|
|
||||||
/*Adjust client balance*/
|
/*Adjust client balance*/
|
||||||
$this->invoice->client->service()->updateBalance($adjustment)->save();
|
// $this->invoice->client->service()->updateBalance($adjustment)->save();
|
||||||
|
|
||||||
$this->invoice->markInvitationsSent();
|
$this->invoice->markInvitationsSent();
|
||||||
|
|
||||||
|
@ -97,10 +97,14 @@ class DeletePayment
|
|||||||
->updatePaidToDate($net_deletable * -1)
|
->updatePaidToDate($net_deletable * -1)
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
$paymentable_invoice->ledger()
|
// $paymentable_invoice->ledger()
|
||||||
->updateInvoiceBalance($net_deletable, "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();
|
// ->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
|
$this->payment
|
||||||
->client
|
->client
|
||||||
->service()
|
->service()
|
||||||
|
@ -290,9 +290,13 @@ class RefundPayment
|
|||||||
->updatePaidToDate($refunded_invoice['amount'] * -1)
|
->updatePaidToDate($refunded_invoice['amount'] * -1)
|
||||||
->save();
|
->save();
|
||||||
|
|
||||||
|
// $invoice->ledger()
|
||||||
|
// ->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
|
||||||
|
// ->save();
|
||||||
|
|
||||||
$invoice->ledger()
|
$invoice->ledger()
|
||||||
->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
|
->mutateInvoiceBalance($invoice->amount, "Refund of payment # {$this->payment->number}");
|
||||||
->save();
|
|
||||||
|
|
||||||
if ($invoice->amount == $invoice->balance) {
|
if ($invoice->amount == $invoice->balance) {
|
||||||
$invoice->service()->setStatus(Invoice::STATUS_SENT);
|
$invoice->service()->setStatus(Invoice::STATUS_SENT);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user