From a43688fd30136079494d4e82c89fbfe4c3035b78 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 23 Sep 2020 20:52:54 +1000 Subject: [PATCH] Append DELETED to invoice number on delete action --- app/Repositories/InvoiceRepository.php | 4 +- app/Services/Invoice/InvoiceService.php | 8 +++ app/Services/Invoice/MarkInvoiceDeleted.php | 71 ++++++++++++++++++++ app/Utils/Traits/GeneratesCounter.php | 10 +++ app/Utils/Traits/Recurring/HasRecurrence.php | 3 +- 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/Services/Invoice/MarkInvoiceDeleted.php diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 287b9dc308b1..8bcfc705d3f2 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -83,7 +83,9 @@ class InvoiceRepository extends BaseRepository return; } - $invoice->service()->handleCancellation()->save(); + $invoice->service()->markDeleted()->handleCancellation()->save(); + + $invoice = parent::delete($invoice); diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index a7e52f7d63ce..11af9ed337f8 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -24,6 +24,7 @@ use App\Services\Invoice\CreateInvitations; use App\Services\Invoice\GetInvoicePdf; use App\Services\Invoice\HandleCancellation; use App\Services\Invoice\HandleReversal; +use App\Services\Invoice\MarkInvoiceDeleted; use App\Services\Invoice\MarkInvoicePaid; use App\Services\Invoice\MarkSent; use App\Services\Invoice\TriggeredActions; @@ -154,6 +155,13 @@ class InvoiceService return $this; } + public function markDeleted() + { + $this->invoice = (new MarkInvoiceDeleted($this->invoice))->run(); + + return $this; + } + public function reverseCancellation() { $this->invoice = (new HandleCancellation($this->invoice))->reverse(); diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php new file mode 100644 index 000000000000..2d98ed2ed4fb --- /dev/null +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -0,0 +1,71 @@ +invoice = $invoice; + } + + public function run() + { + $check = false; + $x=0; + + do { + + $number = $this->calcNumber($x); + $check = $this->checkNumberAvailable(Invoice::class, $this->invoice, $number); + $x++; + + } while (!$check); + + $this->invoice->number = $number; + + return $this->invoice; + } + + + private function calcNumber($x) + { + if($x==0) + $number = $this->invoice->number . '_' . ctrans('texts.deleted'); + else + $number = $this->invoice->number . '_' . ctrans('texts.deleted') . '_'. $x; + + return $number; + + } + +} diff --git a/app/Utils/Traits/GeneratesCounter.php b/app/Utils/Traits/GeneratesCounter.php index 8716eb374737..bf801b7fd13a 100644 --- a/app/Utils/Traits/GeneratesCounter.php +++ b/app/Utils/Traits/GeneratesCounter.php @@ -306,6 +306,16 @@ trait GeneratesCounter return $number; } + + /*Check if a number is available for use. */ + public function checkNumberAvailable($class, $entity, $number) :bool + { + if($entity = $class::whereCompanyId($entity->company_id)->whereNumber($number)->withTrashed()->first()) + return false; + + return true; + } + /** * Saves counters at both the company and client level. * diff --git a/app/Utils/Traits/Recurring/HasRecurrence.php b/app/Utils/Traits/Recurring/HasRecurrence.php index 6560e18dd956..2b94ef18bd4a 100644 --- a/app/Utils/Traits/Recurring/HasRecurrence.php +++ b/app/Utils/Traits/Recurring/HasRecurrence.php @@ -53,7 +53,8 @@ trait HasRecurrence */ public function setDayOfMonth($date, $day_of_month) { - + $data = Carbon::parse($date); + $set_date = $date->copy()->setUnitNoOverflow('day', $day_of_month, 'month'); //If the set date is less than the original date we need to add a month.