New-Thanasoft/thanasoft-back/app/Http/Requests/UpdateInvoiceRequest.php
2026-05-22 15:57:50 +03:00

89 lines
4.1 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateInvoiceRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$invoiceId = $this->route('invoice');
return [
'client_id' => 'nullable|exists:clients,id',
'sous_traitant_id' => 'nullable|exists:sous_traitants,id',
'group_id' => 'nullable|exists:client_groups,id',
'source_quote_id' => 'nullable|exists:quotes,id',
'invoice_number' => 'sometimes|string|max:191|unique:invoices,invoice_number,' . $invoiceId,
'status' => 'sometimes|in:brouillon,emise,envoyee,partiellement_payee,payee,echue,annulee,avoir',
'invoice_date' => 'sometimes|date',
'due_date' => 'nullable|date|after_or_equal:invoice_date',
'currency' => 'sometimes|string|size:3',
'total_ht' => 'sometimes|numeric|min:0',
'total_tva' => 'sometimes|numeric|min:0',
'total_ttc' => 'sometimes|numeric|min:0',
'e_invoicing_channel_id' => 'nullable|exists:e_invoicing_channels,id',
'e_invoice_status' => 'nullable|in:cree,transmis,accepte,refuse,en_litige,acquitte,archive',
];
}
public function messages(): array
{
return [
'client_id.exists' => 'Le client selectionne est invalide.',
'sous_traitant_id.exists' => 'Le sous-traitant selectionne est invalide.',
'group_id.exists' => 'Le groupe selectionne est invalide.',
'source_quote_id.exists' => 'Le devis source est invalide.',
'invoice_number.string' => 'Le numero de facture doit etre une chaine de caracteres.',
'invoice_number.max' => 'Le numero de facture ne doit pas depasser 191 caracteres.',
'invoice_number.unique' => 'Ce numero de facture existe deja.',
'status.in' => 'Le statut selectionne est invalide.',
'invoice_date.date' => 'La date de la facture n est pas valide.',
'due_date.date' => 'La date d echeance n est pas valide.',
'due_date.after_or_equal' => 'La date d echeance doit etre posterieure ou egale a la date de la facture.',
'currency.size' => 'La devise doit comporter 3 caracteres.',
'total_ht.numeric' => 'Le total HT doit etre un nombre.',
'total_ht.min' => 'Le total HT ne peut pas etre negatif.',
'total_tva.numeric' => 'Le total TVA doit etre un nombre.',
'total_tva.min' => 'Le total TVA ne peut pas etre negatif.',
'total_ttc.numeric' => 'Le total TTC doit etre un nombre.',
'total_ttc.min' => 'Le total TTC ne peut pas etre negatif.',
'e_invoicing_channel_id.exists' => 'Le canal de facturation electronique est invalide.',
'e_invoice_status.in' => 'Le statut de facturation electronique est invalide.',
];
}
public function withValidator($validator): void
{
$validator->after(function ($validator) {
if (! $this->hasAny(['client_id', 'sous_traitant_id', 'group_id'])) {
return;
}
$hasClient = filled($this->input('client_id'));
$hasSousTraitant = filled($this->input('sous_traitant_id'));
$hasGroup = filled($this->input('group_id'));
if (! $hasClient && ! $hasSousTraitant && ! $hasGroup) {
$message = 'Un client, un sous-traitant ou un groupe de clients est obligatoire.';
$validator->errors()->add('client_id', $message);
$validator->errors()->add('sous_traitant_id', $message);
$validator->errors()->add('group_id', $message);
}
if ((int) $hasClient + (int) $hasSousTraitant + (int) $hasGroup > 1) {
$message = 'Selectionnez soit un client, soit un sous-traitant, soit un groupe de clients.';
$validator->errors()->add('client_id', $message);
$validator->errors()->add('sous_traitant_id', $message);
$validator->errors()->add('group_id', $message);
}
});
}
}