diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 1af1b10a01ca..9866f43133ae 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -687,8 +687,8 @@ class InvoiceController extends BaseController break; case 'delete': //need to make sure the invoice is cancelled first!! - //$invoice->service()->handleCancellation()->save(); - + //$invoice->service()->handleCancellation()s->save(); +nlog("inside delete"); $this->invoice_repo->delete($invoice); if (! $bulk) { diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 6ee7b715d67a..25762ee7f5d4 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -64,7 +64,6 @@ class PaymentRepository extends BaseRepository */ private function applyPayment(array $data, Payment $payment): ?Payment { -nlog($data); $is_existing_payment = true; diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index ddee068ba1bd..fa346d3fb571 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -71,14 +71,15 @@ 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(); //adjust paid to dates $this->invoice->client->service()->updatePaidToDate($this->payment_total)->save(); $this->invoice->client->service()->updateBalance($this->invoice->balance)->save(); - $this->invoice->ledger()->updatePaymentBalance($this->payment_total, 'Restored payment for invoice {$this->invoice->number}')->save(); + // you only need to touch the ledger ONCE per transaction. + // $this->invoice->ledger()->updatePaymentBalance($this->payment_total*-1, "Restored payment for invoice {$this->invoice->number}")->save(); $this->windBackInvoiceNumber(); diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 34fa1d39d28d..6b6e668feb51 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -36,9 +36,6 @@ class MarkInvoiceDeleted extends AbstractService if ($this->invoice->is_deleted) { return $this->invoice; } - - // if(in_array($this->invoice->status_id, ['currencies', 'industries', 'languages', 'countries', 'banks'])) - // return $this-> $this->cleanup() ->setAdjustmentAmount() @@ -53,25 +50,26 @@ class MarkInvoiceDeleted extends AbstractService private function adjustLedger() { - $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1); + $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1, 'Invoice Deleted - reducing ledger balance'); //reduces the payment balance by payment totals return $this; } private function adjustPaidToDate() { - $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); + $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); //reduces the paid to date by the payment totals return $this; } private function adjustBalance() { - $this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save(); + $this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save(); //reduces the client balance by the invoice amount. return $this; } + /* Adjust the payment amounts */ private function adjustPayments() { //if total payments = adjustment amount - that means we need to delete the payments as well. @@ -79,6 +77,7 @@ class MarkInvoiceDeleted extends AbstractService if ($this->adjustment_amount == $this->total_payments) { $this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]); } else { + //adjust payments down by the amount applied to the invoice payment. $this->invoice->payments->each(function ($payment) { @@ -96,6 +95,12 @@ class MarkInvoiceDeleted extends AbstractService return $this; } + /** + * Set the values of two variables + * + * $this->adjustment_amount - sum of the invoice paymentables + * $this->total_payments - sum of the invoice payments + */ private function setAdjustmentAmount() { foreach ($this->invoice->payments as $payment) { @@ -111,6 +116,12 @@ class MarkInvoiceDeleted extends AbstractService return $this; } + /* + * + * This sets the invoice number to _deleted + * and also removes the links to existing entities + * + */ private function cleanup() { $check = false; @@ -143,7 +154,7 @@ class MarkInvoiceDeleted extends AbstractService return $number; } - + /* Touches all paymentables as deleted */ private function deletePaymentables() { $this->invoice->payments->each(function ($payment) { diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 53337b6838bd..4b0a096ae3fe 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -47,7 +47,7 @@ class LedgerService return $this; } - public function updatePaymentBalance($adjustment) + public function updatePaymentBalance($adjustment, $notes = '') { $balance = 0; @@ -63,6 +63,7 @@ class LedgerService $company_ledger->adjustment = $adjustment; $company_ledger->balance = $balance + $adjustment; $company_ledger->activity_id = Activity::UPDATE_PAYMENT; + $company_ledger->notes = $notes; $company_ledger->save(); $this->entity->company_ledger()->save($company_ledger);