价格增加百分比上浮
This commit is contained in:
@@ -45,11 +45,12 @@ class CartController extends BaseMiniController
|
||||
if ($product === null) {
|
||||
throw new RepositoryException('商品不存在或已下架,请刷新后重试');
|
||||
}
|
||||
$price = ProductPriceModel::query()
|
||||
// 存在性校验与计价类型无关(百分比行 price 可能为 0 也能加购)
|
||||
$hasPrice = ProductPriceModel::query()
|
||||
->where('product_id', $productId)
|
||||
->where('level_id', $store->level_id)
|
||||
->value('price');
|
||||
if ($price === null) {
|
||||
->exists();
|
||||
if (! $hasPrice) {
|
||||
throw new RepositoryException('商品「' . $product->name . '」未设置您所在等级的价格,无法加购');
|
||||
}
|
||||
|
||||
@@ -115,10 +116,11 @@ class CartController extends BaseMiniController
|
||||
->unique()->values()->all();
|
||||
|
||||
$products = ProductModel::withTrashed()->whereIn('id', $productIds)->get()->keyBy('id');
|
||||
$prices = ProductPriceModel::query()
|
||||
$priceRows = ProductPriceModel::query()
|
||||
->where('level_id', $store->level_id)
|
||||
->whereIn('product_id', $productIds)
|
||||
->pluck('price', 'product_id');
|
||||
->get(['product_id', 'price', 'price_type', 'percent'])
|
||||
->keyBy('product_id');
|
||||
|
||||
// 图片一次查回(避免 ProductModel::$appends images_arr 的 N+1)。
|
||||
// 注意:image_ids 有 imageIds Attribute 访问器(get 返回数组),需取原始值
|
||||
@@ -140,7 +142,16 @@ class CartController extends BaseMiniController
|
||||
foreach ($rows as $row) {
|
||||
$product = $products->get($row->product_id);
|
||||
$productOn = $product !== null && $product->status === ProductModel::STATUS_ON;
|
||||
$price = $prices->get($row->product_id); // string|null
|
||||
// 实际价(百分比计价行按成本价上浮换算);未设等级价为 null
|
||||
$priceRow = $priceRows->get($row->product_id);
|
||||
$price = $priceRow === null
|
||||
? null
|
||||
: ProductPriceModel::calcActualPrice(
|
||||
(int) $priceRow->price_type,
|
||||
$priceRow->price,
|
||||
$priceRow->percent,
|
||||
(float) ($product?->cost_price ?? 0),
|
||||
);
|
||||
$buyable = $productOn && $price !== null;
|
||||
$quantity = (string) $row->quantity;
|
||||
|
||||
|
||||
@@ -47,10 +47,11 @@ class OrderController extends BaseMiniController
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$prices = ProductPriceModel::query()
|
||||
$priceRows = ProductPriceModel::query()
|
||||
->where('level_id', $store->level_id)
|
||||
->whereIn('product_id', $productIds)
|
||||
->pluck('price', 'product_id');
|
||||
->get(['product_id', 'price', 'price_type', 'percent'])
|
||||
->keyBy('product_id');
|
||||
|
||||
$totalQuantity = '0';
|
||||
$totalAmount = '0';
|
||||
@@ -62,11 +63,18 @@ class OrderController extends BaseMiniController
|
||||
if ($product === null) {
|
||||
throw new RepositoryException('存在已下架或不存在的商品,请刷新后重试');
|
||||
}
|
||||
if (! isset($prices[$productId])) {
|
||||
if (! isset($priceRows[$productId])) {
|
||||
throw new RepositoryException('商品「' . $product->name . '」未设置您所在等级的价格,无法下单');
|
||||
}
|
||||
|
||||
$price = (string) $prices[$productId];
|
||||
// 实际价(百分比计价行按成本价上浮换算,$products 已含 cost_price)
|
||||
$priceRow = $priceRows[$productId];
|
||||
$price = ProductPriceModel::calcActualPrice(
|
||||
(int) $priceRow->price_type,
|
||||
$priceRow->price,
|
||||
$priceRow->percent,
|
||||
$product->cost_price,
|
||||
);
|
||||
$quantity = (string) $row['quantity'];
|
||||
$amount = bcmul($price, $quantity, 2);
|
||||
$totalQuantity = bcadd($totalQuantity, $quantity, 2);
|
||||
|
||||
@@ -88,9 +88,10 @@ class ProductController extends BaseMiniController
|
||||
->paginate($pageSize)
|
||||
->toArray();
|
||||
|
||||
// 扁平化价格:prices[0].price → price(未设等级价为 null)
|
||||
// 扁平化价格:prices[0].actual_price → price(访问器经 toArray 自动输出换算后实际价;未设等级价为 null)
|
||||
// unset prices 同时移除 price_type/percent,门店端无法反推成本;cost_price 已被 $hidden 过滤
|
||||
foreach ($data['data'] as &$row) {
|
||||
$row['price'] = $row['prices'][0]['price'] ?? null;
|
||||
$row['price'] = $row['prices'][0]['actual_price'] ?? null;
|
||||
unset($row['prices']);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class ProductController extends BaseController
|
||||
|
||||
protected array $quickSearchField = ['name', 'spec'];
|
||||
|
||||
/** A1 商品列表(含分类/供应商/各等级价格) */
|
||||
/** A1 商品列表(含分类/供应商/各等级价格;cost_price 在 $hidden 中,后台列表需显式恢复) */
|
||||
#[GetRoute(authorize: 'query')]
|
||||
public function query(Request $request): JsonResponse
|
||||
{
|
||||
@@ -50,9 +50,9 @@ class ProductController extends BaseController
|
||||
)
|
||||
->orderBy('sort')
|
||||
->orderBy('id', 'desc')
|
||||
->paginate($pageSize)
|
||||
->toArray();
|
||||
return $this->success($data);
|
||||
->paginate($pageSize);
|
||||
$data->getCollection()->makeVisible('cost_price');
|
||||
return $this->success($data->toArray());
|
||||
}
|
||||
|
||||
/** 上传商品分类图片文件 */
|
||||
@@ -85,6 +85,8 @@ class ProductController extends BaseController
|
||||
'product_id' => $product->id,
|
||||
'level_id' => (int) $row['level_id'],
|
||||
'price' => $row['price'],
|
||||
'price_type' => (int) ($row['price_type'] ?? ProductPriceModel::PRICE_TYPE_FIXED),
|
||||
'percent' => $row['percent'] ?? 0,
|
||||
]);
|
||||
}
|
||||
return $product;
|
||||
@@ -114,7 +116,11 @@ class ProductController extends BaseController
|
||||
$levelIds[] = $levelId;
|
||||
ProductPriceModel::updateOrCreate(
|
||||
['product_id' => $product->id, 'level_id' => $levelId],
|
||||
['price' => $row['price']],
|
||||
[
|
||||
'price' => $row['price'],
|
||||
'price_type' => (int) ($row['price_type'] ?? ProductPriceModel::PRICE_TYPE_FIXED),
|
||||
'percent' => $row['percent'] ?? 0,
|
||||
],
|
||||
);
|
||||
}
|
||||
$product->prices()->whereNotIn('level_id', $levelIds)->delete();
|
||||
@@ -140,12 +146,14 @@ class ProductController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* A2 价格矩阵:行=商品(支持 category_id / keyword 过滤 + page/pageSize 服务端分页),列=全部启用等级,值=price(缺失为 null)
|
||||
* A2 价格矩阵:行=商品(支持 category_id / keyword 过滤 + page/pageSize 服务端分页),
|
||||
* 列=全部启用等级,值=实际销售价(缺失为 null);行内含 cost_price 与每等级
|
||||
* price_type_{levelId} / percent_{levelId},供前端判断计价类型与联动重算
|
||||
*/
|
||||
#[GetRoute('/priceMatrix', 'query')]
|
||||
public function priceMatrix(Request $request): JsonResponse
|
||||
{
|
||||
$query = ProductModel::query()->with('prices:id,product_id,level_id,price');
|
||||
$query = ProductModel::query()->with('prices:id,product_id,level_id,price,price_type,percent');
|
||||
if (($categoryId = (int) $request->input('category_id', 0)) > 0) {
|
||||
$query->where('category_id', $categoryId);
|
||||
}
|
||||
@@ -172,11 +180,20 @@ class ProductController extends BaseController
|
||||
'name' => $product->name,
|
||||
'spec' => $product->spec,
|
||||
'unit' => $product->unit,
|
||||
'cost_price' => (float) $product->cost_price,
|
||||
];
|
||||
foreach ($levels as $level) {
|
||||
$row['price_' . $level->id] = isset($priceMap[$level->id])
|
||||
? (float) $priceMap[$level->id]->price
|
||||
$price = $priceMap[$level->id] ?? null;
|
||||
$row['price_' . $level->id] = $price
|
||||
? (float) ProductPriceModel::calcActualPrice(
|
||||
(int) $price->price_type,
|
||||
$price->price,
|
||||
$price->percent,
|
||||
$product->cost_price,
|
||||
)
|
||||
: null;
|
||||
$row['price_type_' . $level->id] = $price ? (int) $price->price_type : ProductPriceModel::PRICE_TYPE_FIXED;
|
||||
$row['percent_' . $level->id] = $price ? (float) $price->percent : 0;
|
||||
}
|
||||
return $row;
|
||||
});
|
||||
@@ -189,7 +206,8 @@ class ProductController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* A2 批量调价:事务写入,写完后给受影响门店生成 Notice(type=price)
|
||||
* A2 批量调价:三类更新行(成本价 / 固定价 / 成本百分比,可混合同一行),事务写入,
|
||||
* 写完后给受影响门店生成 Notice(type=price)
|
||||
*/
|
||||
#[PutRoute('/batchPrice', 'batchPrice')]
|
||||
public function batchPrice(BatchPriceRequest $request): JsonResponse
|
||||
@@ -200,28 +218,52 @@ class ProductController extends BaseController
|
||||
$productIds = [];
|
||||
$levelIds = [];
|
||||
foreach ($updates as $row) {
|
||||
ProductPriceModel::updateOrCreate(
|
||||
['product_id' => (int) $row['product_id'], 'level_id' => (int) $row['level_id']],
|
||||
['price' => $row['price']],
|
||||
);
|
||||
$productIds[(int) $row['product_id']] = true;
|
||||
$levelIds[(int) $row['level_id']] = true;
|
||||
$productId = (int) $row['product_id'];
|
||||
$productIds[$productId] = true;
|
||||
|
||||
// 分支1:成本价更新(百分比计价的基数,可与等级价行同在一行)
|
||||
if (array_key_exists('cost_price', $row) && $row['cost_price'] !== null) {
|
||||
ProductModel::whereKey($productId)->update(['cost_price' => $row['cost_price']]);
|
||||
}
|
||||
|
||||
// 分支2/3:等级价格行(固定价或成本百分比,按 price_type 区分)
|
||||
if (isset($row['level_id'])) {
|
||||
$levelId = (int) $row['level_id'];
|
||||
$levelIds[$levelId] = true;
|
||||
ProductPriceModel::updateOrCreate(
|
||||
['product_id' => $productId, 'level_id' => $levelId],
|
||||
[
|
||||
'price' => $row['price'] ?? 0,
|
||||
'price_type' => (int) ($row['price_type'] ?? ProductPriceModel::PRICE_TYPE_FIXED),
|
||||
'percent' => $row['percent'] ?? 0,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 成本价变更只影响「该商品下百分比计价」的等级;受影响门店等级 = 等级更新行 ∪ 百分比行等级
|
||||
$percentLevelIds = ProductPriceModel::query()
|
||||
->whereIn('product_id', array_keys($productIds))
|
||||
->where('price_type', ProductPriceModel::PRICE_TYPE_PERCENT)
|
||||
->pluck('level_id')
|
||||
->merge($levelIds)
|
||||
->unique()
|
||||
->all();
|
||||
|
||||
$productNames = ProductModel::whereIn('id', array_keys($productIds))
|
||||
->pluck('name')
|
||||
->implode('、');
|
||||
$content = mb_substr('以下商品价格已调整:' . $productNames . ',下次登录小程序后按新价格显示', 0, 500);
|
||||
|
||||
// 受影响门店:客户等级在本次调价等级范围内的正常门店,通知其绑定的正常用户
|
||||
// 受影响门店:客户等级在受影响等级范围内的正常门店,通知其绑定的正常用户
|
||||
$userIds = UserModel::query()
|
||||
->where('type', UserModel::TYPE_STORE)
|
||||
->where('status', UserModel::STATUS_NORMAL)
|
||||
->whereIn('store_id', function ($q) use ($levelIds) {
|
||||
->whereIn('store_id', function ($q) use ($percentLevelIds) {
|
||||
$q->select('id')
|
||||
->from('store')
|
||||
->where('status', StoreModel::STATUS_NORMAL)
|
||||
->whereIn('level_id', array_keys($levelIds));
|
||||
->whereIn('level_id', $percentLevelIds);
|
||||
})
|
||||
->pluck('id');
|
||||
|
||||
|
||||
@@ -2,10 +2,18 @@
|
||||
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
use App\Models\ProductPriceModel;
|
||||
use Closure;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Modules\Common\Http\Requests\BaseFormRequest;
|
||||
|
||||
/**
|
||||
* 批量调价 验证(A2 价格矩阵编辑提交)
|
||||
*
|
||||
* updates 每行支持三类更新(可混合同一行):
|
||||
* 成本行 {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
|
||||
{
|
||||
@@ -16,11 +24,54 @@ class BatchPriceRequest extends BaseFormRequest
|
||||
return [
|
||||
'updates' => 'required|array|min:1',
|
||||
'updates.*.product_id' => 'required|integer|exists:product,id',
|
||||
'updates.*.level_id' => 'required|integer|exists:customer_level,id',
|
||||
'updates.*.price' => 'required|numeric|min:0',
|
||||
'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',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 行级交叉校验(在 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 [
|
||||
@@ -28,11 +79,15 @@ class BatchPriceRequest extends BaseFormRequest
|
||||
'updates.min' => '请至少提交一条价格调整',
|
||||
'updates.*.product_id.required' => '调价行缺少商品',
|
||||
'updates.*.product_id.exists' => '商品不存在',
|
||||
'updates.*.level_id.required' => '调价行缺少客户等级',
|
||||
'updates.*.cost_price.numeric' => '成本价必须为数字',
|
||||
'updates.*.cost_price.min' => '成本价不能小于 0',
|
||||
'updates.*.level_id.exists' => '客户等级不存在',
|
||||
'updates.*.price.required' => '调价行缺少单价',
|
||||
'updates.*.price_type.in' => '计价类型不正确',
|
||||
'updates.*.price.numeric' => '单价必须为数字',
|
||||
'updates.*.price.min' => '单价不能小于 0',
|
||||
'updates.*.percent.numeric' => '上浮百分点必须为数字',
|
||||
'updates.*.percent.min' => '上浮百分点不能小于 0',
|
||||
'updates.*.percent.max' => '上浮百分点不能超过 999.99',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
use App\Models\ProductCategoryModel;
|
||||
use App\Models\ProductPriceModel;
|
||||
use App\Models\SupplierModel;
|
||||
use Closure;
|
||||
use Illuminate\Validation\Rules\Exists;
|
||||
use Illuminate\Validation\Validator;
|
||||
use Modules\Common\Http\Requests\BaseFormRequest;
|
||||
use Modules\SystemTool\Models\SysFileModel;
|
||||
|
||||
@@ -30,13 +33,34 @@ class ProductFormRequest extends BaseFormRequest
|
||||
'shelf_life' => 'nullable|integer|min:0',
|
||||
'stock' => 'nullable|integer|min:0',
|
||||
'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' => 'required|numeric|min:0',
|
||||
'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
|
||||
{
|
||||
return function (Validator $validator): void {
|
||||
$data = (array) $validator->getData();
|
||||
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", '按成本百分比计价时必须填写上浮百分点');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
@@ -51,6 +75,12 @@ class ProductFormRequest extends BaseFormRequest
|
||||
'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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ class ProductModel extends Model
|
||||
'shelf_life',
|
||||
'stock',
|
||||
'status',
|
||||
'cost_price',
|
||||
'remark',
|
||||
];
|
||||
|
||||
@@ -47,12 +48,19 @@ class ProductModel extends Model
|
||||
'stock' => 'integer',
|
||||
'sort' => 'integer',
|
||||
'status' => 'integer',
|
||||
'cost_price' => 'decimal:2',
|
||||
'created_at' => 'datetime:Y-m-d H:i:s',
|
||||
'updated_at' => 'datetime:Y-m-d H:i:s',
|
||||
];
|
||||
|
||||
protected $appends = ['images_arr'];
|
||||
|
||||
/**
|
||||
* 成本价属商业敏感数据,默认不随 toArray 输出(防止泄漏到小程序/供应商端);
|
||||
* 后台管理接口需在查询结果上调用 makeVisible('cost_price') 恢复。
|
||||
*/
|
||||
protected $hidden = ['cost_price'];
|
||||
|
||||
/**
|
||||
* 封面图片
|
||||
*/
|
||||
|
||||
@@ -8,11 +8,18 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* 商品价格模型(同一商品按客户等级定价,联合键 product_id + level_id)
|
||||
*
|
||||
* 计价类型:固定价(price 即实际单价)或成本百分比(实际单价 = 成本价 × (100 + percent) / 100)
|
||||
*/
|
||||
class ProductPriceModel extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/** 计价类型:固定价 */
|
||||
public const int PRICE_TYPE_FIXED = 0;
|
||||
/** 计价类型:成本百分比(按成本价上浮 percent 百分点) */
|
||||
public const int PRICE_TYPE_PERCENT = 1;
|
||||
|
||||
protected $table = 'product_price';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
@@ -20,14 +27,21 @@ class ProductPriceModel extends Model
|
||||
'product_id',
|
||||
'level_id',
|
||||
'price',
|
||||
'price_type',
|
||||
'percent',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'product_id' => 'integer',
|
||||
'level_id' => 'integer',
|
||||
'price' => 'decimal:2',
|
||||
'price_type' => 'integer',
|
||||
'percent' => 'decimal:2',
|
||||
];
|
||||
|
||||
/** 序列化时附带实际销售价(后台列表/小程序列表直接展示) */
|
||||
protected $appends = ['actual_price'];
|
||||
|
||||
/**
|
||||
* 所属商品
|
||||
*/
|
||||
@@ -51,4 +65,44 @@ class ProductPriceModel extends Model
|
||||
{
|
||||
return $query->where('product_id', $productId)->where('level_id', $levelId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算实际销售价(统一换算入口,金额走 bcmath 保证两位小数精度)
|
||||
*
|
||||
* 固定价返回 price 原值;成本百分比返回 cost × (100 + percent) / 100(四舍五入保留两位)。
|
||||
*
|
||||
* @param string|int|float $price 固定价(decimal cast 后为 '5.50' 形式字符串)
|
||||
* @param string|int|float $percent 成本上浮百分点(30 = 上浮 30%)
|
||||
* @param string|int|float $costPrice 商品成本价(decimal cast 字符串)
|
||||
* @return string 两位小数字符串,如 '13.05'
|
||||
*/
|
||||
public static function calcActualPrice(
|
||||
int $priceType,
|
||||
string|int|float $price,
|
||||
string|int|float $percent,
|
||||
string|int|float $costPrice,
|
||||
): string {
|
||||
if ($priceType === self::PRICE_TYPE_PERCENT) {
|
||||
$multiplier = bcadd('100', (string) $percent, 4);
|
||||
return bcdiv(bcmul((string) $costPrice, $multiplier, 4), '100', 2);
|
||||
}
|
||||
// 固定价:归一化为两位小数字符串
|
||||
return bcadd((string) $price, '0', 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际销售价访问器(供 toArray 输出 actual_price)
|
||||
*
|
||||
* 依赖 product 关系取成本价;prices 经商品 eager load 加载时逆向关系自动填充,无 N+1。
|
||||
* 注意:单独序列化本模型且未加载 product 关系时会触发一次查询,成本价缺失按 0 兜底。
|
||||
*/
|
||||
protected function getActualPriceAttribute(): string
|
||||
{
|
||||
return self::calcActualPrice(
|
||||
(int) $this->price_type,
|
||||
(string) $this->price,
|
||||
(string) $this->percent,
|
||||
(string) ($this->product?->cost_price ?? 0),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ use Illuminate\Support\Facades\DB;
|
||||
* 流程(事务内):
|
||||
* 1. 行锁查询当日全部「待汇总」订单(无则报错;状态条件天然排除已汇总订单,幂等)
|
||||
* 2. 展开明细按商品聚合(Σquantity,快照品名/规格;供应商取商品默认供应商)
|
||||
* 3. 估算单价 = 该商品最低等级价(product_price MIN),amount = quantity × 估算单价
|
||||
* 3. 估算单价 = 该商品最低实际等级价(按计价类型换算后取 min,PHP 侧兼容 MySQL/SQLite),amount = quantity × 估算单价
|
||||
* 4. 创建采购单头(PO 单号,estimate_amount = Σitems.amount)
|
||||
* 5. 明细按「分类 sort → 商品 sort」排序写入 sort 行号
|
||||
* 6. 源订单批量回写 status = 已汇总
|
||||
@@ -78,12 +78,29 @@ class PurchaseGenerateService
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
// 3. 估算单价 = 最低等级价
|
||||
$minPrices = ProductPriceModel::query()
|
||||
// 3. 估算单价 = 最低实际等级价(逐行按计价类型换算后取 min;数量级 = 当日 SKU × 等级,可控)
|
||||
$priceRows = ProductPriceModel::query()
|
||||
->whereIn('product_id', array_keys($aggregated))
|
||||
->groupBy('product_id')
|
||||
->selectRaw('product_id, MIN(price) as min_price')
|
||||
->pluck('min_price', 'product_id');
|
||||
->get(['product_id', 'price', 'price_type', 'percent'])
|
||||
->groupBy('product_id');
|
||||
|
||||
$minPrices = [];
|
||||
foreach ($aggregated as $productId => $item) {
|
||||
$product = $products->get($productId);
|
||||
$minPrice = null;
|
||||
foreach ($priceRows->get($productId, collect()) as $priceRow) {
|
||||
$actual = ProductPriceModel::calcActualPrice(
|
||||
(int) $priceRow->price_type,
|
||||
$priceRow->price,
|
||||
$priceRow->percent,
|
||||
(float) ($product->cost_price ?? 0),
|
||||
);
|
||||
if ($minPrice === null || bccomp($actual, $minPrice, 2) < 0) {
|
||||
$minPrice = $actual;
|
||||
}
|
||||
}
|
||||
$minPrices[$productId] = $minPrice ?? '0';
|
||||
}
|
||||
|
||||
// 组装明细行并按「分类 sort → 商品 sort」排序
|
||||
$rows = [];
|
||||
|
||||
Reference in New Issue
Block a user