69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class PurchaseOrderRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'warehouse_id' => 'required|integer|exists:warehouses,id',
|
|
'supplier_id' => 'required|integer|exists:suppliers,id',
|
|
'brand_id' => 'nullable|integer|exists:brands,id',
|
|
'expected_arrival' => 'required|date',
|
|
'shipping_method' => 'required|in:express,logistics,self',
|
|
'freight' => 'nullable|numeric|min:0',
|
|
'category' => 'nullable|string|max:50',
|
|
'remark' => 'nullable|string|max:500',
|
|
];
|
|
|
|
// 如果是创建草稿,只需要基础信息
|
|
if ($this->is('api/purchase-orders/draft')) {
|
|
return $rules;
|
|
}
|
|
|
|
// 如果是更新采购单(添加商品),需要商品项
|
|
if ($this->isMethod('put') || $this->isMethod('patch')) {
|
|
return array_merge($rules, [
|
|
'items' => 'required|array|min:1',
|
|
'items.*.sku_code' => 'required|string|max:50',
|
|
'items.*.quantity' => 'required|integer|min:1',
|
|
'items.*.price' => 'required|numeric|min:0',
|
|
]);
|
|
}
|
|
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'warehouse_id.required' => '请选择仓库',
|
|
'supplier_id.required' => '请选择供应商',
|
|
'expected_arrival.required' => '请选择预计到货日期',
|
|
'shipping_method.required' => '请选择运输方式',
|
|
'items.required' => '请至少添加一个商品',
|
|
'items.*.sku_code.required' => '商品编码不能为空',
|
|
'items.*.quantity.required' => '商品数量不能为空',
|
|
'items.*.price.required' => '商品单价不能为空',
|
|
];
|
|
}
|
|
} |