mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-10-28 12:11:08 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Crater\Http\Requests;
 | |
| 
 | |
| use Crater\Models\CompanySetting;
 | |
| use Crater\Models\Customer;
 | |
| use Illuminate\Foundation\Http\FormRequest;
 | |
| use Illuminate\Validation\Rule;
 | |
| 
 | |
| class PaymentRequest extends FormRequest
 | |
| {
 | |
|     /**
 | |
|      * Determine if the user is authorized to make this request.
 | |
|      *
 | |
|      * @return bool
 | |
|      */
 | |
|     public function authorize()
 | |
|     {
 | |
|         return true;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the validation rules that apply to the request.
 | |
|      *
 | |
|      * @return array
 | |
|      */
 | |
|     public function rules()
 | |
|     {
 | |
|         $rules = [
 | |
|             'payment_date' => [
 | |
|                 'required',
 | |
|             ],
 | |
|             'customer_id' => [
 | |
|                 'required',
 | |
|             ],
 | |
|             'exchange_rate' => [
 | |
|                 'nullable'
 | |
|             ],
 | |
|             'amount' => [
 | |
|                 'required',
 | |
|             ],
 | |
|             'payment_number' => [
 | |
|                 'required',
 | |
|                 Rule::unique('payments')->where('company_id', $this->header('company'))
 | |
|             ],
 | |
|             'invoice_id' => [
 | |
|                 'nullable',
 | |
|             ],
 | |
|             'payment_method_id' => [
 | |
|                 'nullable',
 | |
|             ],
 | |
|             'notes' => [
 | |
|                 'nullable',
 | |
|             ],
 | |
|         ];
 | |
| 
 | |
|         if ($this->isMethod('PUT')) {
 | |
|             $rules['payment_number'] = [
 | |
|                 'required',
 | |
|                 Rule::unique('payments')
 | |
|                     ->ignore($this->route('payment')->id)
 | |
|                     ->where('company_id', $this->header('company')),
 | |
|             ];
 | |
|         }
 | |
| 
 | |
|         $companyCurrency = CompanySetting::getSetting('currency', $this->header('company'));
 | |
| 
 | |
|         $customer = Customer::find($this->customer_id);
 | |
| 
 | |
|         if ($customer && $companyCurrency) {
 | |
|             if ((string)$customer->currency_id !== $companyCurrency) {
 | |
|                 $rules['exchange_rate'] = [
 | |
|                     'required',
 | |
|                 ];
 | |
|             };
 | |
|         }
 | |
| 
 | |
|         return $rules;
 | |
|     }
 | |
| 
 | |
|     public function getPaymentPayload()
 | |
|     {
 | |
|         $company_currency = CompanySetting::getSetting('currency', $this->header('company'));
 | |
|         $current_currency = $this->currency_id;
 | |
|         $exchange_rate = $company_currency != $current_currency ? $this->exchange_rate : 1;
 | |
|         $currency = Customer::find($this->customer_id)->currency_id;
 | |
| 
 | |
|         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' => $currency
 | |
|             ])
 | |
|             ->toArray();
 | |
|     }
 | |
| }
 |