diff --git a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php index c7bd214081..a5a4bc9f93 100644 --- a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php +++ b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php @@ -27,22 +27,33 @@ class RestoreDisabledRule implements ValidationRule public function validate(string $attribute, mixed $value, Closure $fail): void { - if (empty($value) ||!in_array($value, ['delete', 'restore'])) { + $user = auth()->user(); + $company = $user->company(); + + + if (empty($value) || !$company->verifactuEnabled()) { return; } - $user = auth()->user(); - - $company = $user->company(); + $base_query = Invoice::withTrashed() + ->whereIn('id', $this->transformKeys(request()->ids)) + ->company(); + + $restore_query = clone $base_query; + $delete_query = clone $base_query; + + $mutated_query = $delete_query->where(function ($q){ + $q->whereNotNull('backup->parent_invoice_id')->orWhere('backup->child_invoice_ids', '!=', []); + }); /** For verifactu, we do not allow restores of deleted invoices */ - if($company->verifactuEnabled() && $value == 'restore' &&Invoice::withTrashed()->whereIn('id', $this->transformKeys(request()->ids))->where('company_id', $company->id)->where('is_deleted', true)->exists()) { + if($value == 'restore' && $restore_query->where('is_deleted', true)->exists()) { $fail(ctrans('texts.restore_disabled_verifactu')); } - - if ($company->verifactuEnabled() && $value == 'delete' && Invoice::withTrashed()->whereIn('id', $this->transformKeys(request()->ids))->where('company_id', $company->id)->where('status_id', Invoice::STATUS_CANCELLED)->exists()) { + elseif(in_array($value, ['delete', 'cancel']) && $delete_query->exists()) { + nlog($delete_query->pluck('backup')->toArray()); // any verifactu invoices that have a parent can NEVER be deleted. The parent can also NEVER be deleted $fail(ctrans('texts.delete_disabled_verifactu')); } - } + } diff --git a/app/Http/ValidationRules/Invoice/VerifactuAmountCheck.php b/app/Http/ValidationRules/Invoice/VerifactuAmountCheck.php index b8e209d033..92c2e71886 100644 --- a/app/Http/ValidationRules/Invoice/VerifactuAmountCheck.php +++ b/app/Http/ValidationRules/Invoice/VerifactuAmountCheck.php @@ -38,8 +38,6 @@ class VerifactuAmountCheck implements ValidationRule $company = $user->company(); - $start = microtime(true); - /** For verifactu, we do not allow restores of deleted invoices */ if ($company->verifactuEnabled()) { $invoice = false; @@ -48,16 +46,13 @@ class VerifactuAmountCheck implements ValidationRule $child_invoice_count = 0; if(isset($this->input['modified_invoice_id'])) { - $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($this->input['modified_invoice_id'])); + $invoice = Invoice::withTrashed()->where('id', $this->decodePrimaryKey($this->input['modified_invoice_id']))->company()->firstOrFail(); $child_invoices = Invoice::withTrashed() ->whereIn('id', $this->transformKeys($invoice->backup->child_invoice_ids->toArray())) ->get(); $child_invoice_totals = round($child_invoices->sum('amount'), 2); $child_invoice_count = $child_invoices->count(); - // if($child_invoice_totals + $invoice->amount < 0) { - // $fail("Negative invoices can only be linked to existing invoices"); - // } } $items = collect($this->input['line_items'])->map(function ($item) use($company){ @@ -89,12 +84,6 @@ class VerifactuAmountCheck implements ValidationRule $total = $items->sum() - $total_discount; - nlog("total " . $total); - nlog(!$invoice); - nlog($total); - nlog($child_invoice_totals); -nlog($invoice->amount ?? 0); - if($total < 0 && !$invoice) { $fail("Negative invoices {$total} can only be linked to existing invoices"); } @@ -104,6 +93,5 @@ nlog($invoice->amount ?? 0); } } - nlog(microtime(true) - $start); } } diff --git a/lang/en/texts.php b/lang/en/texts.php index ed1a8d977d..0848ebbfd0 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5574,7 +5574,7 @@ $lang = array( 'selected_products' => 'Selected Products', 'create_company_error_unauthorized' => 'You are not authorized to create a company. Only the account owner can create a company.', 'restore_disabled_verifactu' => 'You cannot restore an invoice once it has been deleted', - 'delete_disabled_verifactu' => 'You cannot delete an invoice once it has been cancelled', + 'delete_disabled_verifactu' => 'You cannot delete an invoice once it has been cancelled or modified', ); return $lang; diff --git a/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php b/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php index 8f19735b54..9375f2b203 100644 --- a/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php +++ b/tests/Feature/EInvoice/Verifactu/VerifactuApiTest.php @@ -632,7 +632,6 @@ class VerifactuApiTest extends TestCase $this->company->settings = $settings; $this->company->save(); - $data = [ 'action' => 'cancel', 'ids' => [$invoice->hashed_id],