From bcb21fb23464a9cfdbfb0dfd8111ff3b1a6682cc Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 2 Dec 2020 22:01:50 +1100 Subject: [PATCH 1/9] Working on invoice deletion --- app/Services/Credit/MarkSent.php | 1 + app/Services/Invoice/MarkInvoiceDeleted.php | 50 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index fdd6e1d891bf..1591feb095cb 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -43,6 +43,7 @@ class MarkSent ->service() ->setStatus(Credit::STATUS_SENT) ->applyNumber() + ->adjustBalance($this->credit->amount) ->save(); diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index cc830756a21c..e7b55b669928 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -21,6 +21,8 @@ class MarkInvoiceDeleted extends AbstractService private $invoice; + private $adjustment_amount = 0; + public function __construct(Invoice $invoice) { $this->invoice = $invoice; @@ -28,7 +30,36 @@ class MarkInvoiceDeleted extends AbstractService public function run() { + 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() + ->deletePaymentables(); + } + + + private function setAdjustmentAmount() + { + + foreach ($this->invoice->payments as $payment) { + $this->adjustment_amount += $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('amount')); + } + + return $this; + } + + private function cleanup() + { + $check = false; + $x=0; do { @@ -43,10 +74,10 @@ class MarkInvoiceDeleted extends AbstractService $this->invoice->tasks()->update(['invoice_id' => null]); $this->invoice->expenses()->update(['invoice_id' => null]); - return $this->invoice; + return $this; + } - private function calcNumber($x) { if ($x==0) { @@ -57,4 +88,19 @@ class MarkInvoiceDeleted extends AbstractService return $number; } + + + private function deletePaymentables() + { + + $this->invoice->payments->each(function ($payment){ + $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->update(['deleted_at' => now()]); + }); + + + return $this; + } } From 48f2f469d30157dfc3c36e5ce2385212adcf0db1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 14:11:24 +1100 Subject: [PATCH 2/9] refactor for invoice deletion --- app/Services/Invoice/MarkInvoiceDeleted.php | 52 ++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index e7b55b669928..489c0f2566fd 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -23,6 +23,8 @@ class MarkInvoiceDeleted extends AbstractService private $adjustment_amount = 0; + private $total_payments = 0; + public function __construct(Invoice $invoice) { $this->invoice = $invoice; @@ -38,9 +40,54 @@ class MarkInvoiceDeleted extends AbstractService $this->cleanup() ->setAdjustmentAmount() - ->deletePaymentables(); + ->deletePaymentables() + ->adjustPayments() + ->adjustPaidToDate() + ->adjustLedger(); } + private function adjustLedger() + { + $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1); + + return $this; + } + + private function adjustPaidToDate() + { + $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); + + return $this; + } + + private function adjustPayments() + { + //if total payments = adjustment amount - that means we need to delete the payments as well. + + if($this->adjustment_amount == $this->total_payments) { + + $this->invoice->payments()->update(['deleted_at'=> now(), 'is_deleted' => true]); + + } + else { + //adjust payments down by the amount applied to the invoice payment. + + $this->invoice->payments->each(function ($payment){ + + $payment_adjustment = $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('amount')); + + $payment->amount -= $payment_adjustment; + $payment->applied -= $payment_adjustment; + $payment->save(); + + }); + } + + return $this; + } private function setAdjustmentAmount() { @@ -52,6 +99,9 @@ class MarkInvoiceDeleted extends AbstractService ->sum(DB::raw('amount')); } + + $this->total_payments = $this->invoice->payments->sum('amount'); + return $this; } From 73c947491229892367b8cf2eedd4cf66e1b583e5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 14:56:00 +1100 Subject: [PATCH 3/9] Working on invoice delete refactor --- app/Repositories/InvoiceRepository.php | 3 ++- app/Services/Invoice/MarkInvoiceDeleted.php | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 40b5e6af9f83..b6a53c24546d 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -69,7 +69,8 @@ class InvoiceRepository extends BaseRepository return $invoice; } - $invoice->service()->markDeleted()->handleCancellation()->save(); +// $invoice->service()->markDeleted()->handleCancellation()->save(); + $invoice->service()->markDeleted()->save(); parent::delete($invoice); diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 489c0f2566fd..9d514e73c4a7 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -14,6 +14,7 @@ namespace App\Services\Invoice; use App\Models\Invoice; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Support\Facades\DB; class MarkInvoiceDeleted extends AbstractService { @@ -44,12 +45,14 @@ class MarkInvoiceDeleted extends AbstractService ->adjustPayments() ->adjustPaidToDate() ->adjustLedger(); + + return $this->invoice; } private function adjustLedger() { $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1); - + return $this; } @@ -66,7 +69,7 @@ class MarkInvoiceDeleted extends AbstractService if($this->adjustment_amount == $this->total_payments) { - $this->invoice->payments()->update(['deleted_at'=> now(), 'is_deleted' => true]); + $this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]); } else { From f96bde6fca77f29dafb21c7816a0be0beabf638e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 15:20:39 +1100 Subject: [PATCH 4/9] Refactor invoice delete/restore --- app/Repositories/InvoiceRepository.php | 14 ++++++- app/Services/Invoice/HandleInvoiceRestore.php | 42 +++++++++++++++++++ app/Services/Invoice/InvoiceService.php | 7 ++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/Services/Invoice/HandleInvoiceRestore.php diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index b6a53c24546d..f55827cb99fb 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -70,13 +70,25 @@ class InvoiceRepository extends BaseRepository } // $invoice->service()->markDeleted()->handleCancellation()->save(); - $invoice->service()->markDeleted()->save(); + $invoice = $invoice->service()->markDeleted()->save(); parent::delete($invoice); return $invoice; } + public function restore() :Invoice + { + if(!$invoice->is_deleted) + return $invoice + + $invoice = $invoice->service()->handeRestore()->save() + + parent::restore($invoice); + + return $invoice; + } + public function reverse() { } diff --git a/app/Services/Invoice/HandleInvoiceRestore.php b/app/Services/Invoice/HandleInvoiceRestore.php new file mode 100644 index 000000000000..c30abfa07608 --- /dev/null +++ b/app/Services/Invoice/HandleInvoiceRestore.php @@ -0,0 +1,42 @@ +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/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 2015fad59cb5..e4dfc07cb441 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -164,6 +164,13 @@ class InvoiceService return $this; } + public function handeRestore() + { + $this->invoice = (new HandleInvoiceRestore($this->invoice))->run(); + + return $this; + } + public function reverseCancellation() { $this->invoice = (new HandleCancellation($this->invoice))->reverse(); From d30ef031a298947c0c1a11d6c0d55a762dc369c3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 15:30:32 +1100 Subject: [PATCH 5/9] refactor invoice deletion --- app/Repositories/InvoiceRepository.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index f55827cb99fb..afa11a0e3e60 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -77,11 +77,23 @@ class InvoiceRepository extends BaseRepository return $invoice; } - public function restore() :Invoice + /** + * Handles the restoration on a deleted invoice. + * + * @param [type] $invoice [description] + * @return [type] [description] + */ + public function restore($invoice) :Invoice { - if(!$invoice->is_deleted) - return $invoice + //if we have just archived, only perform a soft restore + if(!$invoice->is_deleted) { + parent::restore($invoice); + + return $invoice; + } + + // reversed delete invoice actions $invoice = $invoice->service()->handeRestore()->save() parent::restore($invoice); From fceaab9e4021ee112ac59bb75bc7f50b450c24bb Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 21:46:36 +1100 Subject: [PATCH 6/9] 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. From 02c810633abc7cf0953738ec5d570a532c2e8863 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 21:46:49 +1100 Subject: [PATCH 7/9] Working on invoice delete restore refactor --- app/Services/Invoice/MarkInvoiceDeleted.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index 7fdf0ee3f8c2..137bce5bb924 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -76,9 +76,9 @@ class MarkInvoiceDeleted extends AbstractService //if total payments = adjustment amount - that means we need to delete the payments as well. 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. From 528a02db6f5837fc5efe30c895aca67289fac62b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Dec 2020 22:00:34 +1100 Subject: [PATCH 8/9] fixes for mail order --- .gitignore | 2 +- app/Mail/BouncedEmail.php | 2 +- app/Mail/DownloadInvoices.php | 2 +- app/Mail/ExistingMigration.php | 2 +- app/Mail/MigrationCompleted.php | 2 +- app/Mail/MigrationFailed.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 39498b28e145..04ca0e2494a5 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ local_version.txt /resources/assets/bower /public/logo - +/storage/* .env.dusk.local /public/vendors/* *.log diff --git a/app/Mail/BouncedEmail.php b/app/Mail/BouncedEmail.php index 7aaedccff532..a1fe1ca162fe 100644 --- a/app/Mail/BouncedEmail.php +++ b/app/Mail/BouncedEmail.php @@ -45,7 +45,7 @@ class BouncedEmail extends Mailable implements ShouldQueue $subject = ctrans("texts.notification_{$entity_type}_bounced_subject", ['invoice' => $invoice->number]); return - $this->from(config('mail.from.name'), config('mail.from.address')) + $this->from(config('mail.from.address'), config('mail.from.name')) ->text() ->subject($subject); diff --git a/app/Mail/DownloadInvoices.php b/app/Mail/DownloadInvoices.php index 2c65d34ee125..e50bccaca845 100644 --- a/app/Mail/DownloadInvoices.php +++ b/app/Mail/DownloadInvoices.php @@ -31,7 +31,7 @@ class DownloadInvoices extends Mailable public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) + return $this->from(config('mail.from.address'), config('mail.from.name')) ->subject(ctrans('texts.download_files')) ->markdown( 'email.admin.download_files', diff --git a/app/Mail/ExistingMigration.php b/app/Mail/ExistingMigration.php index 596947b7fcab..980a62a799bc 100644 --- a/app/Mail/ExistingMigration.php +++ b/app/Mail/ExistingMigration.php @@ -28,7 +28,7 @@ class ExistingMigration extends Mailable public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) + return $this->from(config('mail.from.address'), config('mail.from.name')) ->view('email.migration.existing'); } } diff --git a/app/Mail/MigrationCompleted.php b/app/Mail/MigrationCompleted.php index 358a15a6c3bc..301b91c60ca3 100644 --- a/app/Mail/MigrationCompleted.php +++ b/app/Mail/MigrationCompleted.php @@ -29,7 +29,7 @@ class MigrationCompleted extends Mailable { $data['settings'] = auth()->user()->company()->settings; - return $this->from(config('mail.from.name'), config('mail.from.address')) + return $this->from(config('mail.from.address'), config('mail.from.name')) ->view('email.migration.completed', $data); } } diff --git a/app/Mail/MigrationFailed.php b/app/Mail/MigrationFailed.php index 545736fd0bf3..c44aa461a87d 100644 --- a/app/Mail/MigrationFailed.php +++ b/app/Mail/MigrationFailed.php @@ -32,7 +32,7 @@ class MigrationFailed extends Mailable public function build() { - return $this->from(config('mail.from.name'), config('mail.from.address')) + return $this->from(config('mail.from.address'), config('mail.from.name')) ->view('email.migration.failed'); } } From 76fb186f02b38058002ad9c3e09326d119f28ae9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 4 Dec 2020 07:40:00 +1100 Subject: [PATCH 9/9] Fix for calculating balance with inclusive taxes --- app/Helpers/Invoice/InvoiceSumInclusive.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 8f63230335bf..2356f7612bd0 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -230,14 +230,16 @@ class InvoiceSumInclusive private function setCalculatedAttributes() { /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ - if ($this->invoice->amount != $this->invoice->balance) { - $paid_to_date = $this->invoice->amount - $this->invoice->balance; + if ($this->invoice->status_id != Invoice::STATUS_DRAFT) { + if ($this->invoice->amount != $this->invoice->balance) { + $paid_to_date = $this->invoice->amount - $this->invoice->balance; - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date; - } else { - $this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); + $this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date; + } else { + $this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); + } } - + /* Set new calculated total */ $this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision);