File "StorePurchaseOrderRequest.php"
Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Http/Requests/Admin/StorePurchaseOrderRequest.php
File size: 3.92 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace App\Http\Requests\Admin;
use App\Models\Product;
use App\Models\PurchaseOrder;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Http\FormRequest;
class StorePurchaseOrderRequest extends FormRequest
{
public function authorize()
{
return Gate::allows('purchase_order_create');
}
public function rules()
{
$rules = [
'payment_terms' => [
'required',
'in:'.implode(',', PurchaseOrder::PAYMENT_TERMS)
],
'reference' => [
'nullable',
],
'shipping_address' => [
'required',
],
'billing_address' => [
'required',
],
'shipping_speed' => [
'required',
'in:'. implode(',',PurchaseOrder::SHIPPING_SPEED),
],
'special_notes' => [
'nullable',
],
'products' => [
'required_without:office_supplies',
'array',
],
'products.*.product_id' => [
'required',
'integer',
// 'exists:products,id',
],
'products.*.price_id' => [
'required',
'integer',
// 'exists:product_prices,id'
],
'products.*.variation_id' => [
'nullable',
],
'products.*.product_type' => [
'required',
'in:'. implode(',', array_keys(Product::PRODUCT_TYPE)),
],
'products.*.orders' => [
'required',
'array',
],
'products.*.orders.*' => [
function ($attribute, $value, $fail) {
if (is_array($value)) {
if (array_key_exists('extra_quantity', $value)) {
if ($value['extra_quantity'] < 1)
$fail('The extra_quantity should be greater than zero.');
} else if (array_key_exists('order_id', $value) && array_key_exists('quantity', $value)) {
if ($value['quantity'] < 1)
$fail('The quantity should be greater than zero.');
} else {
$fail('The extra_quantity or order_id and quantity is required for product :position.');
}
} else {
$fail('The extra_quantity or order_id and quantity is required for product :position.');
}
},
],
'office_supplies' => [
'required_without:products',
'array',
],
'office_supplies.*.product_id' => [
'nullable',
'integer',
'exists:office_supplies,id',
],
'office_supplies.*.vendor_id' => [
'nullable',
'integer',
'exists:vendors,id',
],
'office_supplies.*.quantity' => [
'nullable',
'integer',
'min:1'
],
];
return $rules;
}
public function messages()
{
return [
'products.*.product_id.required' => 'Product id for product #:position is required.',
'products.*.product_id.integer' => 'Product id for product #:position must be an integer.',
'products.*.product_id.exists' => 'Product id for product #:position is invalid.',
'products.*.price_id.required' => 'Price id for product #:position is required.',
'products.*.price_id.integer' => 'Price id for product #:position must be an integer.',
'products.*.price_id.exists' => 'Price id for product #:position is invalid.',
];
}
}