Your IP : 216.73.216.91


Current Path : /var/www/html/LLCF-APP/app/Http/Requests/
Upload File :
Current File : /var/www/html/LLCF-APP/app/Http/Requests/BankTransactionFormRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BankTransactionFormRequest extends FormRequest
{
    public function authorize(): bool
    {
        return auth()->guard('admin')->check();
    }

    public function rules(): array
    {
        return [
            'bank_account_id' => 'required|exists:bank_accounts,id',
            'transaction_date' => 'required|date',
            'description' => 'nullable|string|max:500',
            'cheque_no' => 'nullable|string|max:50',
            'reference_no' => 'nullable|string|max:100',
            'debit' => 'nullable|numeric|min:0',
            'credit' => 'nullable|numeric|min:0',
            'balance' => 'nullable|numeric|min:0'
        ];
    }

    public function messages(): array
    {
        return [
            'bank_account_id.required' => 'Please select a bank account.',
            'bank_account_id.exists' => 'Selected bank account does not exist.',
            'transaction_date.required' => 'Transaction date is required.',
            'transaction_date.date' => 'Please enter a valid date.',
            'cheque_no.max' => 'Cheque number cannot exceed 50 characters.',
            'reference_no.max' => 'Reference number cannot exceed 100 characters.',
            'debit.numeric' => 'Debit must be a valid number.',
            'debit.min' => 'Debit cannot be negative.',
            'credit.numeric' => 'Credit must be a valid number.',
            'credit.min' => 'Credit cannot be negative.',
            'balance.numeric' => 'Balance must be a valid number.',
            'balance.min' => 'Balance cannot be negative.',
        ];
    }

    /**
     * Prepare data for validation
     * Map transaction_type to debit or credit if needed
     */
    protected function prepareForValidation()
    {
        if ($this->filled('transaction_type')) {
            if ($this->transaction_type === 'credit') {
                $this->merge([
                    'credit' => $this->input('credit', 0),
                    'debit' => 0,
                ]);
            } else {
                $this->merge([
                    'debit' => $this->input('debit', 0),
                    'credit' => 0,
                ]);
            }
        }
    }
}