From fceaab9e4021ee112ac59bb75bc7f50b450c24bb Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 21:46:36 +1100 Subject: [PATCH] Working on invoice delete restore refactor --- app/Http/Controllers/InvoiceController.php | 3 +- app/Repositories/InvoiceRepository.php | 2 +- app/Services/Invoice/HandleInvoiceRestore.php | 42 ------- app/Services/Invoice/HandleRestore.php | 111 ++++++++++++++++++ app/Services/Invoice/InvoiceService.php | 4 +- app/Services/Invoice/MarkInvoiceDeleted.php | 8 ++ 6 files changed, 123 insertions(+), 47 deletions(-) delete mode 100644 app/Services/Invoice/HandleInvoiceRestore.php create mode 100644 app/Services/Invoice/HandleRestore.php diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index b45b9a3c26f2..8ee5573f0d2d 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -102,7 +102,6 @@ class InvoiceController extends BaseController * response=422, * description="Validation error", * @OA\JsonContent(ref="#/components/schemas/ValidationError"), - * ), * @OA\Response( * response="default", @@ -691,7 +690,7 @@ class InvoiceController extends BaseController break; case 'delete': //need to make sure the invoice is cancelled first!! - $invoice->service()->handleCancellation()->save(); + //$invoice->service()->handleCancellation()->save(); $this->invoice_repo->delete($invoice); diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index afa11a0e3e60..a491b4c8c2f5 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -94,7 +94,7 @@ class InvoiceRepository extends BaseRepository } // reversed delete invoice actions - $invoice = $invoice->service()->handeRestore()->save() + $invoice = $invoice->service()->handleRestore()->save(); parent::restore($invoice); diff --git a/app/Services/Invoice/HandleInvoiceRestore.php b/app/Services/Invoice/HandleInvoiceRestore.php deleted file mode 100644 index c30abfa07608..000000000000 --- a/app/Services/Invoice/HandleInvoiceRestore.php +++ /dev/null @@ -1,42 +0,0 @@ -invoice = $invoice; - } - - public function run() - { - - //determine whether we need to un-delete payments OR just modify the payment amount /applied balances. - - //adjust ledger balance - - //adjust paid to dates - - //restore paymentables - - } - -} - diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php new file mode 100644 index 000000000000..d647767355bc --- /dev/null +++ b/app/Services/Invoice/HandleRestore.php @@ -0,0 +1,111 @@ +invoice = $invoice; + } + + public function run() + { + + if(!$this->invoice->is_deleted) + return $this->invoice; + + //determine whether we need to un-delete payments OR just modify the payment amount /applied balances. + + foreach($this->invoice->payments as $payment) + { + + $payment->restore(); + + $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->restore(); + + $payment_amount = $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(\DB::raw('amount')); + + info($payment->amount . " == " . $payment_amount); + + if($payment->amount == $payment_amount) { + + $payment->is_deleted = false; + $payment->save(); + + } + else { + + $payment->is_deleted = false; + $payment->amount += $this->payment_total; + $payment->applied += $this->payment_total; + $payment->save(); + } + + $this->payment_total += $payment_amount; + + } + + + + + //adjust ledger balance + $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(); + + $this->windBackInvoiceNumber(); + + return $this->invoice; + } + + + private function windBackInvoiceNumber() + { + + $findme = '_' . ctrans('texts.deleted'); + + $pos = strpos($this->invoice->number, $findme); + + $new_invoice_number = substr($this->invoice->number, 0, $pos); + + try { + $this->invoice->number = $new_invoice_number; + $this->invoice->save(); + } + catch(\Exception $e){ + info("I could not wind back the invoice number"); + } + + } +} + diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index e4dfc07cb441..ed0c19c3af2b 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -164,9 +164,9 @@ class InvoiceService return $this; } - public function handeRestore() + public function handleRestore() { - $this->invoice = (new HandleInvoiceRestore($this->invoice))->run(); + $this->invoice = (new HandleRestore($this->invoice))->run(); return $this; } diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 9d514e73c4a7..7fdf0ee3f8c2 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -44,6 +44,7 @@ class MarkInvoiceDeleted extends AbstractService ->deletePaymentables() ->adjustPayments() ->adjustPaidToDate() + ->adjustBalance() ->adjustLedger(); return $this->invoice; @@ -63,6 +64,13 @@ class MarkInvoiceDeleted extends AbstractService return $this; } + private function adjustBalance() + { + $this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save(); + + return $this; + } + private function adjustPayments() { //if total payments = adjustment amount - that means we need to delete the payments as well.