209 lines
8.2 KiB
PHP
209 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace app\common\validate;
|
|
|
|
use think\Validate;
|
|
|
|
class StockOrdersValidate extends Validate
|
|
{
|
|
/**
|
|
* 验证规则
|
|
*/
|
|
protected $rule = [
|
|
'id' => 'number|egt:0',
|
|
'order_no' => 'require|max:40|unique:stock_orders',
|
|
'pi_no' => 'require|max:64',
|
|
'biz_date' => 'require',
|
|
'salesperson_id' => 'number|egt:0',
|
|
'salesperson_snapshot' => 'max:64',
|
|
'clearance_country_id' => 'require|number|egt:0',
|
|
'clearance_country_snapshot' => 'require|max:64',
|
|
'customer_source_id' => 'require|number|egt:0',
|
|
'customer_source_snapshot' => 'require|max:64',
|
|
'customer_id' => 'require|number|egt:0',
|
|
'customer_snapshot' => 'require|max:128',
|
|
'customer_country_id' => 'require|number|egt:0',
|
|
'customer_country_snapshot' => 'require|max:64',
|
|
'declarant_name' => 'require|max:64',
|
|
'eta_ship_date' => 'number|egt:0',
|
|
'shipping_method_id' => 'require|number',
|
|
'shipping_method_snapshot' => 'require|max:32',
|
|
'cif_total_amount' => 'require|float|egt:0',
|
|
'domestic_port_id' => 'require|number|egt:0',
|
|
'domestic_port_snapshot' => 'require|max:64',
|
|
'trade_term_id' => 'require|number|egt:0',
|
|
'trade_term_snapshot' => 'require|max:64',
|
|
'clearance_port_id' => 'require|number|egt:0',
|
|
'clearance_port_snapshot' => 'require|max:64',
|
|
'latest_arrival_date' => 'number|egt:0',
|
|
'contract_exchange_rate' => 'require|float|egt:0',
|
|
'settlement_currency' => 'require|length:3|alpha',
|
|
'remark' => 'max:255',
|
|
'contract' => 'max:255',
|
|
'is_use' => 'in:0,1',
|
|
'object_id' => 'number|egt:0',
|
|
'is_final' => 'in:0,1',
|
|
'approval_status' => 'in:0,1,2,3',
|
|
'status' => 'in:0,1',
|
|
'file_url' => 'max:255',
|
|
'pick_up_date' => 'number|egt:0',
|
|
'collect_all_date' => 'number|egt:0',
|
|
'created_at' => 'number|egt:0',
|
|
'updated_at' => 'number|egt:0',
|
|
'deleted_at' => 'number|egt:0',
|
|
];
|
|
|
|
/**
|
|
* 错误提示信息
|
|
*/
|
|
protected $message = [
|
|
'order_no.require' => '备货单编号不能为空',
|
|
'order_no.max' => '备货单编号长度不能超过40个字符',
|
|
'order_no.unique' => '备货单编号已存在',
|
|
'pi_no.require' => 'PI号不能为空',
|
|
'pi_no.max' => 'PI号长度不能超过64个字符',
|
|
'biz_date.require' => '单据日期不能为空',
|
|
'clearance_country_id.require' => '清关目的国ID不能为空',
|
|
'clearance_country_snapshot.require' => '清关目的国名称不能为空',
|
|
'customer_source_id.require' => '客户来源ID不能为空',
|
|
'customer_source_snapshot.require' => '客户来源名称不能为空',
|
|
'customer_id.require' => '客户ID不能为空',
|
|
'customer_snapshot.require' => '客户名称不能为空',
|
|
'customer_country_id.require' => '客户所在国ID不能为空',
|
|
'customer_country_snapshot.require' => '客户所在国名称不能为空',
|
|
'declarant_name.require' => '清关人不能为空',
|
|
'shipping_method_id.require' => '出运方式ID不能为空',
|
|
'shipping_method_snapshot.require' => '出运方式不能为空',
|
|
'cif_total_amount.require' => '合计到岸价不能为空',
|
|
'cif_total_amount.float' => '合计到岸价必须为数字',
|
|
'cif_total_amount.egt' => '合计到岸价不能为负数',
|
|
'domestic_port_id.require' => '国内收货港ID不能为空',
|
|
'domestic_port_snapshot.require' => '国内收货港名称不能为空',
|
|
'trade_term_id.require' => '贸易条款ID不能为空',
|
|
'trade_term_snapshot.require' => '贸易条款名称不能为空',
|
|
'clearance_port_id.require' => '清关目的港ID不能为空',
|
|
'clearance_port_snapshot.require' => '清关目的港名称不能为空',
|
|
'contract_exchange_rate.require' => '合同汇率不能为空',
|
|
'contract_exchange_rate.float' => '合同汇率必须为数字',
|
|
'contract_exchange_rate.egt' => '合同汇率不能为负数',
|
|
'settlement_currency.require' => '结算币种不能为空',
|
|
'settlement_currency.length' => '结算币种必须为3个字符',
|
|
'settlement_currency.alpha' => '结算币种必须为字母',
|
|
'approval_status.in' => '审批状态值不正确',
|
|
'status.in' => '状态值不正确',
|
|
'is_final.in' => '是否终版值不正确',
|
|
'is_use.in' => '是否开过终版值不正确',
|
|
];
|
|
|
|
/**
|
|
* 验证场景
|
|
*/
|
|
protected $scene = [
|
|
// 创建场景
|
|
'store' => [
|
|
'pi_no', 'biz_date',
|
|
'clearance_country_id', 'clearance_country_snapshot', 'customer_source_id',
|
|
'customer_source_snapshot', 'customer_id', 'customer_snapshot',
|
|
'customer_country_id', 'customer_country_snapshot', 'declarant_name',
|
|
'shipping_method_id', 'shipping_method_snapshot',
|
|
'domestic_port_id', 'domestic_port_snapshot',
|
|
'trade_term_id', 'trade_term_snapshot', 'clearance_port_id',
|
|
'clearance_port_snapshot', 'contract_exchange_rate',
|
|
'settlement_currency'
|
|
],
|
|
|
|
// 更新场景
|
|
'update' => [
|
|
'pi_no', 'biz_date',
|
|
'clearance_country_id', 'clearance_country_snapshot', 'customer_source_id',
|
|
'customer_source_snapshot', 'customer_id', 'customer_snapshot',
|
|
'customer_country_id', 'customer_country_snapshot', 'declarant_name',
|
|
'shipping_method_id', 'shipping_method_snapshot',
|
|
'domestic_port_id', 'domestic_port_snapshot',
|
|
'trade_term_id', 'trade_term_snapshot', 'clearance_port_id',
|
|
'clearance_port_snapshot', 'contract_exchange_rate',
|
|
'settlement_currency'
|
|
],
|
|
|
|
// 提交审批场景
|
|
'submit' => [
|
|
'id' => 'require|number|egt:1',
|
|
'status' => 'require|in:1'
|
|
],
|
|
|
|
// 审批场景
|
|
'approve' => [
|
|
'id' => 'require|number|egt:1',
|
|
'approval_status' => 'require|in:1,2,3'
|
|
],
|
|
|
|
// 状态更新场景
|
|
'status' => [
|
|
'id' => 'require|number|egt:1',
|
|
'status' => 'require|in:0,1'
|
|
],
|
|
|
|
// 简略验证场景(用于快速验证)
|
|
'simple' => [
|
|
'order_no', 'biz_date', 'cif_total_amount', 'settlement_currency'
|
|
]
|
|
];
|
|
|
|
/**
|
|
* 自定义验证规则 - 验证时间戳是否合理
|
|
*/
|
|
protected function checkTimestamp($value, $rule, $data = [])
|
|
{
|
|
if ($value === 0 || $value === '0') {
|
|
return true;
|
|
}
|
|
|
|
// 时间戳应该在1970年到现在+1年的范围内
|
|
$minTimestamp = 0;
|
|
$maxTimestamp = time() + (365 * 24 * 60 * 60);
|
|
|
|
return $value >= $minTimestamp && $value <= $maxTimestamp;
|
|
}
|
|
|
|
/**
|
|
* 自定义验证规则 - 验证金额格式
|
|
*/
|
|
protected function checkMoney($value, $rule, $data = [])
|
|
{
|
|
if (!is_numeric($value)) {
|
|
return false;
|
|
}
|
|
|
|
// 验证小数点位数不超过2位
|
|
if (strpos($value, '.') !== false) {
|
|
$decimalPart = explode('.', $value)[1];
|
|
if (strlen($decimalPart) > 2) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 场景验证前回调 - 添加自定义规则
|
|
*/
|
|
// protected function sceneBefore($scene)
|
|
// {
|
|
// // 为时间戳字段添加自定义验证
|
|
// $timestampFields = ['biz_date', 'eta_ship_date', 'latest_arrival_date', 'pick_up_date', 'collect_all_date', 'created_at', 'updated_at', 'deleted_at'];
|
|
// foreach ($timestampFields as $field) {
|
|
// if (isset($this->scene[$scene][$field])) {
|
|
// $this->rule[$field] .= '|checkTimestamp';
|
|
// }
|
|
// }
|
|
|
|
// // 为金额字段添加自定义验证
|
|
// $moneyFields = ['cif_total_amount', 'contract_exchange_rate'];
|
|
// foreach ($moneyFields as $field) {
|
|
// if (isset($this->scene[$scene][$field])) {
|
|
// $this->rule[$field] .= '|checkMoney';
|
|
// }
|
|
// }
|
|
// }
|
|
} |