diff --git a/app/Console/Commands/SendRemindersCron.php b/app/Console/Commands/SendRemindersCron.php index ab55404fec2c..0aa8bf52dd37 100644 --- a/app/Console/Commands/SendRemindersCron.php +++ b/app/Console/Commands/SendRemindersCron.php @@ -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; } diff --git a/app/Jobs/Ledger/UpdateLedger.php b/app/Jobs/Ledger/UpdateLedger.php index bf4430990a8a..54cda67b14f9 100644 --- a/app/Jobs/Ledger/UpdateLedger.php +++ b/app/Jobs/Ledger/UpdateLedger.php @@ -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)]; } } diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 884a555cf436..030ae3486c94 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -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; } diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index 6974e5825910..6f0bd6d17a65 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -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; } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index 8442134a1702..6e7b3b471a96 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -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) { diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 9716e558980f..fbc6a65bfc19 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -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; diff --git a/app/Services/Invoice/HandleCancellation.php b/app/Services/Invoice/HandleCancellation.php index 2fcd20013fee..62b2ca60ac2e 100644 --- a/app/Services/Invoice/HandleCancellation.php +++ b/app/Services/Invoice/HandleCancellation.php @@ -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); diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index ee1c94717a34..435234b898ac 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -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) diff --git a/app/Services/Invoice/HandleReversal.php b/app/Services/Invoice/HandleReversal.php index 2b1410950e12..1ccce45f9b18 100644 --- a/app/Services/Invoice/HandleReversal.php +++ b/app/Services/Invoice/HandleReversal.php @@ -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; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index e1947d9dbdbc..4d237e7a43c1 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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; diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 79c2fa3fbd82..f73e3fe65e3d 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -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(); diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index caac1125f57f..a9ba12f99123 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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() diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index b06833eeff0d..c8b6fb9ec82b 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -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);