Files
InvoiceShelf/app/Http/Requests/ExpenseRequest.php
Christos Yiakoumettis 3e96297699 Add expense number at Expenses (#406)
* add expense number at expenses

* Re-order expense fields

* Rename expense_number migration

* Add expense_number to tests

---------

Co-authored-by: Darko Gjorgjijoski <dg@darkog.com>
2025-09-02 03:20:27 +02:00

91 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\CompanySetting;
use Illuminate\Foundation\Http\FormRequest;
class ExpenseRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$companyCurrency = CompanySetting::getSetting('currency', $this->header('company'));
$rules = [
'expense_date' => [
'required',
],
'expense_number' => [
'nullable',
'string',
'max:255',
],
'expense_category_id' => [
'required',
],
'exchange_rate' => [
'nullable',
],
'payment_method_id' => [
'nullable',
],
'amount' => [
'required',
],
'customer_id' => [
'nullable',
],
'notes' => [
'nullable',
],
'currency_id' => [
'required',
],
'attachment_receipt' => [
'nullable',
'file',
'mimes:jpg,png,pdf,doc,docx,xls,xlsx,ppt,pptx',
'max:20000',
],
];
if ($companyCurrency && $this->currency_id) {
if ($companyCurrency !== $this->currency_id) {
$rules['exchange_rate'] = [
'required',
];
}
}
return $rules;
}
public function getExpensePayload()
{
$company_currency = CompanySetting::getSetting('currency', $this->header('company'));
$current_currency = $this->currency_id;
$exchange_rate = $company_currency != $current_currency ? $this->exchange_rate : 1;
return collect($this->validated())
->merge([
'creator_id' => $this->user()->id,
'company_id' => $this->header('company'),
'exchange_rate' => $exchange_rate,
'base_amount' => $this->amount * $exchange_rate,
'currency_id' => $current_currency,
])
->toArray();
}
}