mirror of
				https://github.com/crater-invoice/crater.git
				synced 2025-11-04 06:23:17 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			182 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			182 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace Crater\Http\Requests;
 | 
						|
 | 
						|
use Crater\Models\Address;
 | 
						|
use Illuminate\Foundation\Http\FormRequest;
 | 
						|
use Illuminate\Support\Arr;
 | 
						|
use Illuminate\Support\Facades\Auth;
 | 
						|
use Illuminate\Validation\Rule;
 | 
						|
 | 
						|
class CustomerRequest 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 = [
 | 
						|
            'name' => [
 | 
						|
                'required',
 | 
						|
            ],
 | 
						|
            'email' => [
 | 
						|
                'email',
 | 
						|
                'nullable',
 | 
						|
                Rule::unique('customers')->where('company_id', $this->header('company'))
 | 
						|
            ],
 | 
						|
            'password' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'phone' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'company_name' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'contact_name' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'website' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'prefix' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'enable_portal' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'currency_id' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.name' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.address_street_1' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.address_street_2' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.city' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.state' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.country_id' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.zip' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.phone' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'billing.fax' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.name' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.address_street_1' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.address_street_2' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.city' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.state' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.country_id' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.zip' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.phone' => [
 | 
						|
                'nullable',
 | 
						|
            ],
 | 
						|
            'shipping.fax' => [
 | 
						|
                'nullable',
 | 
						|
            ]
 | 
						|
        ];
 | 
						|
 | 
						|
        if ($this->isMethod('PUT') && $this->email != null) {
 | 
						|
            $rules['email'] = [
 | 
						|
                'email',
 | 
						|
                'nullable',
 | 
						|
                Rule::unique('customers')->ignore($this->route('customer')->id),
 | 
						|
            ];
 | 
						|
        };
 | 
						|
 | 
						|
        return $rules;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getCustomerPayload()
 | 
						|
    {
 | 
						|
        return collect($this->validated())
 | 
						|
            ->only([
 | 
						|
                'name',
 | 
						|
                'email',
 | 
						|
                'currency_id',
 | 
						|
                'password',
 | 
						|
                'phone',
 | 
						|
                'prefix',
 | 
						|
                'company_name',
 | 
						|
                'contact_name',
 | 
						|
                'website',
 | 
						|
                'enable_portal',
 | 
						|
                'estimate_prefix',
 | 
						|
                'payment_prefix',
 | 
						|
                'invoice_prefix',
 | 
						|
            ])
 | 
						|
            ->merge([
 | 
						|
                'creator_id' => $this->user()->id,
 | 
						|
                'company_id' => $this->header('company'),
 | 
						|
            ])
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    public function getShippingAddress()
 | 
						|
    {
 | 
						|
        return collect($this->shipping)
 | 
						|
            ->merge([
 | 
						|
                'type' => Address::SHIPPING_TYPE
 | 
						|
            ])
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    public function getBillingAddress()
 | 
						|
    {
 | 
						|
        return collect($this->billing)
 | 
						|
            ->merge([
 | 
						|
                'type' => Address::BILLING_TYPE
 | 
						|
            ])
 | 
						|
            ->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    public function hasAddress(array $address)
 | 
						|
    {
 | 
						|
        $data = Arr::where($address, function ($value, $key) {
 | 
						|
            return isset($value);
 | 
						|
        });
 | 
						|
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
}
 |