2022-05-29 04:06:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\PurchaseOrder;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
|
|
|
use App\Models\PurchaseOrder;
|
2022-07-01 08:56:36 +02:00
|
|
|
use App\Utils\Traits\CleanLineItems;
|
2022-05-29 04:06:18 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
|
|
|
|
class StorePurchaseOrderRequest extends Request
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2022-07-01 08:56:36 +02:00
|
|
|
use CleanLineItems;
|
|
|
|
|
2022-05-29 04:06:18 +02:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return auth()->user()->can('create', PurchaseOrder::class);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
$rules = [];
|
|
|
|
|
2022-06-10 10:00:07 +02:00
|
|
|
$rules['vendor_id'] = 'bail|required|exists:vendors,id,company_id,'.auth()->user()->company()->id.',is_deleted,0';
|
2022-05-29 04:06:18 +02:00
|
|
|
|
|
|
|
$rules['number'] = ['nullable', Rule::unique('purchase_orders')->where('company_id', auth()->user()->company()->id)];
|
|
|
|
$rules['discount'] = 'sometimes|numeric';
|
|
|
|
$rules['is_amount_discount'] = ['boolean'];
|
|
|
|
$rules['line_items'] = 'array';
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:59:25 +02:00
|
|
|
public function prepareForValidation()
|
2022-05-29 04:06:18 +02:00
|
|
|
{
|
|
|
|
$input = $this->all();
|
|
|
|
|
|
|
|
$input = $this->decodePrimaryKeys($input);
|
|
|
|
|
2022-07-01 08:56:36 +02:00
|
|
|
if (isset($input['line_items']) && is_array($input['line_items']))
|
|
|
|
$input['line_items'] = isset($input['line_items']) ? $this->cleanItems($input['line_items']) : [];
|
|
|
|
|
|
|
|
$input['amount'] = 0;
|
|
|
|
$input['balance'] = 0;
|
|
|
|
|
2022-05-29 04:06:18 +02:00
|
|
|
$this->replace($input);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|