客户等级设置

This commit is contained in:
liu
2026-08-17 16:27:15 +08:00
parent 2eb27127c9
commit a06c45dc49
31 changed files with 548 additions and 1142 deletions
@@ -2,18 +2,13 @@
namespace App\Http\Requests\Product;
use App\Models\ProductPriceModel;
use Closure;
use Illuminate\Validation\Validator;
use Modules\Common\Http\Requests\BaseFormRequest;
/**
* 批量调价 验证(A2 价格矩阵编辑提交)
*
* updates 每行支持三类更新(可混合同一行)
* 等级售价 = 成本价 × (100 + 等级上浮比例) / 100,矩阵仅支持批量调整成本价
* 成本行 {product_id, cost_price}
* 固定价行 {product_id, level_id, price_type?:0, price}
* 百分比行 {product_id, level_id, price_type:1, percent}price 可选,等价固定价)
*/
class BatchPriceRequest extends BaseFormRequest
{
@@ -24,54 +19,10 @@ class BatchPriceRequest extends BaseFormRequest
return [
'updates' => 'required|array|min:1',
'updates.*.product_id' => 'required|integer|exists:product,id',
'updates.*.cost_price' => 'nullable|numeric|min:0|max:99999999',
'updates.*.level_id' => 'nullable|integer|exists:customer_level,id',
'updates.*.price_type' => 'nullable|integer|in:0,1',
'updates.*.price' => 'nullable|numeric|min:0|max:99999999',
'updates.*.percent' => 'nullable|numeric|min:0|max:999.99',
'updates.*.cost_price' => 'required|numeric|min:0|max:99999999',
];
}
/**
* 行级交叉校验(在 after() 内按实际数据逐行判断,避免 required_with* 通配符参数解析不可靠):
* - 每行必须至少包含成本价或等级价格更新
* - 出现等级字段(price/percent/price_type)时必须带 level_id
* - 等级行必须有 price 或 percent
* - 成本百分比计价(price_type=1)时上浮百分点必填
*/
public function after(): Closure
{
return function (Validator $validator): void {
$data = (array) $validator->getData();
foreach ((array) ($data['updates'] ?? []) as $index => $row) {
$row = (array) $row;
$hasCost = array_key_exists('cost_price', $row) && $row['cost_price'] !== null && $row['cost_price'] !== '';
$hasLevel = isset($row['level_id']);
$hasPrice = array_key_exists('price', $row) && $row['price'] !== null && $row['price'] !== '';
$hasPercent = array_key_exists('percent', $row) && $row['percent'] !== null && $row['percent'] !== '';
$hasType = array_key_exists('price_type', $row);
if (! $hasCost && ! $hasLevel) {
$validator->errors()->add("updates.{$index}", '调价行缺少成本价或等级价格');
continue;
}
if (($hasPrice || $hasPercent || $hasType) && ! $hasLevel) {
$validator->errors()->add("updates.{$index}.level_id", '等级价格行缺少客户等级');
continue;
}
if ($hasLevel && ! $hasPrice && ! $hasPercent) {
$validator->errors()->add("updates.{$index}", '等级价格行缺少单价或上浮百分点');
continue;
}
$priceType = (int) ($row['price_type'] ?? ProductPriceModel::PRICE_TYPE_FIXED);
if ($priceType === ProductPriceModel::PRICE_TYPE_PERCENT && ! $hasPercent) {
$validator->errors()->add("updates.{$index}.percent", '按成本百分比计价时必须填写上浮百分点');
}
}
};
}
public function messages(): array
{
return [
@@ -79,15 +30,9 @@ class BatchPriceRequest extends BaseFormRequest
'updates.min' => '请至少提交一条价格调整',
'updates.*.product_id.required' => '调价行缺少商品',
'updates.*.product_id.exists' => '商品不存在',
'updates.*.cost_price.required' => '调价行缺少成本价',
'updates.*.cost_price.numeric' => '成本价必须为数字',
'updates.*.cost_price.min' => '成本价不能小于 0',
'updates.*.level_id.exists' => '客户等级不存在',
'updates.*.price_type.in' => '计价类型不正确',
'updates.*.price.numeric' => '单价必须为数字',
'updates.*.price.min' => '单价不能小于 0',
'updates.*.percent.numeric' => '上浮百分点必须为数字',
'updates.*.percent.min' => '上浮百分点不能小于 0',
'updates.*.percent.max' => '上浮百分点不能超过 999.99',
];
}
}
@@ -3,7 +3,6 @@
namespace App\Http\Requests\Product;
use App\Models\ProductCategoryModel;
use App\Models\ProductPriceModel;
use App\Models\SupplierModel;
use Closure;
use Illuminate\Validation\Rules\Exists;
@@ -12,7 +11,7 @@ use Modules\Common\Http\Requests\BaseFormRequest;
use Modules\SystemTool\Models\SysFileModel;
/**
* 商品档案 创建/编辑 验证(含多等级价格 prices 数组
* 商品档案 创建/编辑 验证(售价按客户等级上浮比例换算,不再维护等级价格行
*/
class ProductFormRequest extends BaseFormRequest
{
@@ -35,16 +34,11 @@ class ProductFormRequest extends BaseFormRequest
'status' => 'nullable|integer|in:0,1',
'cost_price' => 'nullable|numeric|min:0|max:99999999',
'remark' => 'nullable|string|max:255',
'prices' => 'nullable|array',
'prices.*.level_id' => 'required|integer|exists:customer_level,id',
'prices.*.price_type' => 'nullable|integer|in:0,1',
'prices.*.price' => 'required|numeric|min:0|max:99999999',
'prices.*.percent' => 'nullable|numeric|min:0|max:999.99',
];
}
/**
* 交叉校验:商品只能挂在末级分类;按成本百分比计价(price_type=1)时上浮百分点必填
* 交叉校验:商品只能挂在末级分类
* Laravel 12 FormRequest 的 after() 需返回单个 Closure,由容器 call 后注册到 Validator
*/
public function after(): Closure
@@ -55,13 +49,6 @@ class ProductFormRequest extends BaseFormRequest
if ($categoryId > 0 && ProductCategoryModel::where('parent_id', $categoryId)->exists()) {
$validator->errors()->add('category_id', '该分类下存在子分类,请选择末级分类');
}
foreach ((array) ($data['prices'] ?? []) as $index => $row) {
$priceType = (int) ($row['price_type'] ?? ProductPriceModel::PRICE_TYPE_FIXED);
if ($priceType === ProductPriceModel::PRICE_TYPE_PERCENT
&& (! array_key_exists('percent', (array) $row) || $row['percent'] === null || $row['percent'] === '')) {
$validator->errors()->add("prices.{$index}.percent", '按成本百分比计价时必须填写上浮百分点');
}
}
};
}
@@ -74,17 +61,8 @@ class ProductFormRequest extends BaseFormRequest
'category_id.exists' => '商品分类不存在',
'supplier_id.exists' => '供应商不存在',
'status.in' => '状态值不正确',
'prices.*.level_id.required' => '价格行缺少客户等级',
'prices.*.level_id.exists' => '客户等级不存在',
'prices.*.price.required' => '价格行缺少单价',
'prices.*.price.numeric' => '单价必须为数字',
'prices.*.price.min' => '单价不能小于 0',
'cost_price.numeric' => '成本价必须为数字',
'cost_price.min' => '成本价不能小于 0',
'prices.*.price_type.in' => '计价类型不正确',
'prices.*.percent.numeric' => '上浮百分点必须为数字',
'prices.*.percent.min' => '上浮百分点不能小于 0',
'prices.*.percent.max' => '上浮百分点不能超过 999.99',
];
}
}