diff --git a/app/Http/Requests/Invoice/BulkInvoiceRequest.php b/app/Http/Requests/Invoice/BulkInvoiceRequest.php index 537eaf36e2..7b68f034de 100644 --- a/app/Http/Requests/Invoice/BulkInvoiceRequest.php +++ b/app/Http/Requests/Invoice/BulkInvoiceRequest.php @@ -14,6 +14,7 @@ namespace App\Http\Requests\Invoice; use App\Http\Requests\Request; use App\Exceptions\DuplicatePaymentException; use App\Http\ValidationRules\Invoice\RestoreDisabledRule; +use Illuminate\Validation\Rule; class BulkInvoiceRequest extends Request { @@ -24,9 +25,12 @@ class BulkInvoiceRequest extends Request public function rules() { + /** @var \App\Models\User $user */ + $user = auth()->user(); + return [ - 'action' => ['required', 'string', new RestoreDisabledRule()], - 'ids' => 'required|array', + 'action' => ['required', 'bail','string', new RestoreDisabledRule()], + 'ids' => ['required', 'bail', 'array'], 'email_type' => 'sometimes|in:reminder1,reminder2,reminder3,reminder_endless,custom1,custom2,custom3,invoice,quote,credit,payment,payment_partial,statement,purchase_order', 'template' => 'sometimes|string', 'template_id' => 'sometimes|string', diff --git a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php index 876c730258..6913967e4f 100644 --- a/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php +++ b/app/Http/ValidationRules/Invoice/RestoreDisabledRule.php @@ -13,6 +13,8 @@ namespace App\Http\ValidationRules\Invoice; use Closure; +use App\Models\Invoice; +use App\Utils\Traits\MakesHash; use Illuminate\Contracts\Validation\ValidationRule; /** @@ -20,19 +22,21 @@ use Illuminate\Contracts\Validation\ValidationRule; */ class RestoreDisabledRule implements ValidationRule { + use MakesHash; + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (empty($value) || $value != 'restore') { return; } $user = auth()->user(); + $company = $user->company(); - /** For verifactu, we do not allow restores */ - if($company->settings->e_invoice_type == 'verifactu') { + /** For verifactu, we do not allow restores of deleted invoices */ + if($company->verifactuEnabled() && Invoice::withTrashed()->whereIn('id', $this->transformKeys(request()->ids))->where('company_id', $company->id)->where('is_deleted', true)->exists()) { $fail(ctrans('texts.restore_disabled_verifactu')); } diff --git a/app/Models/Company.php b/app/Models/Company.php index d104f82432..a1ce95e618 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -1030,4 +1030,18 @@ class Company extends BaseModel { return !$this->account->is_flagged && $this->account->e_invoice_quota > 0 && isset($this->legal_entity_id) && isset($this->tax_data->acts_as_sender) && $this->tax_data->acts_as_sender; } + + /** + * verifactuEnabled + * + * Returns a flag if the current company is using verifactu as the e-invoice provider + * + * @return bool + */ + public function verifactuEnabled(): bool + { + return once(function () { + return $this->getSetting('e_invoice_type') == 'verifactu'; + }); + } } diff --git a/app/Services/Invoice/HandleCancellation.php b/app/Services/Invoice/HandleCancellation.php index b8a1672bf7..78d2196c2f 100644 --- a/app/Services/Invoice/HandleCancellation.php +++ b/app/Services/Invoice/HandleCancellation.php @@ -22,9 +22,8 @@ class HandleCancellation extends AbstractService { use GeneratesCounter; - public function __construct(private Invoice $invoice) + public function __construct(private Invoice $invoice, private ?string $reason = null) { - $this->invoice = $invoice; } public function run() diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index b1a405b7d0..a75af86a91 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -233,11 +233,11 @@ class InvoiceService return $this; } - public function handleCancellation() + public function handleCancellation(?string $reason = null) { $this->removeUnpaidGatewayFees(); - $this->invoice = (new HandleCancellation($this->invoice))->run(); + $this->invoice = (new HandleCancellation($this->invoice, $reason))->run(); return $this; }