diff --git a/app/Console/Commands/SendRemindersCron.php b/app/Console/Commands/SendRemindersCron.php index 0aa8bf52dd37..76e6dc029286 100644 --- a/app/Console/Commands/SendRemindersCron.php +++ b/app/Console/Commands/SendRemindersCron.php @@ -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; } diff --git a/app/Jobs/Ledger/UpdateLedger.php b/app/Jobs/Ledger/UpdateLedger.php index 5c464746fd39..08d81c5785e2 100644 --- a/app/Jobs/Ledger/UpdateLedger.php +++ b/app/Jobs/Ledger/UpdateLedger.php @@ -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()); diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 030ae3486c94..a83364dd2c33 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -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; } diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index 6f0bd6d17a65..b6f0c7b00580 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -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; } diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index fbc6a65bfc19..b91ef03e6851 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -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(); } diff --git a/app/Services/Invoice/HandleCancellation.php b/app/Services/Invoice/HandleCancellation.php index 13ea7de5d03a..85b2eee73168 100644 --- a/app/Services/Invoice/HandleCancellation.php +++ b/app/Services/Invoice/HandleCancellation.php @@ -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(); diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index 435234b898ac..823301fb9ce3 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -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 diff --git a/app/Services/Invoice/HandleReversal.php b/app/Services/Invoice/HandleReversal.php index 1ccce45f9b18..4c3076fd6204 100644 --- a/app/Services/Invoice/HandleReversal.php +++ b/app/Services/Invoice/HandleReversal.php @@ -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; diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 4d237e7a43c1..8e1d2d8138da 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -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(); } diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 954e8e90d2d1..b9541b04b4cf 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -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(); diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index a9ba12f99123..1d7295b43661 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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 diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index c8b6fb9ec82b..b12210375d4f 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -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) { diff --git a/tests/Integration/CompanyLedgerTest.php b/tests/Integration/CompanyLedgerTest.php index 2a63c5cb21d7..09ef9929590c 100644 --- a/tests/Integration/CompanyLedgerTest.php +++ b/tests/Integration/CompanyLedgerTest.php @@ -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); }