采购单优化
This commit is contained in:
@@ -3,10 +3,8 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Models\ProductModel;
|
||||
use App\Models\ProductPriceModel;
|
||||
use App\Models\PurchaseOrderItemModel;
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -15,11 +13,9 @@ use Illuminate\Support\Facades\DB;
|
||||
*
|
||||
* 流程(事务内):
|
||||
* 1. 行锁「已接单」订单(可传 orderIds 只合并指定订单;状态条件天然排除已归集订单,幂等)
|
||||
* 2. 展开明细按商品聚合(Σquantity,快照品名/规格;供应商取商品默认供应商)
|
||||
* 3. 估算单价 = 该商品最低实际等级价(按计价类型换算后取 min,PHP 侧兼容 MySQL/SQLite),amount = quantity × 估算单价
|
||||
* 4. 创建采购单头(PO 单号,estimate_amount = Σitems.amount)
|
||||
* 5. 明细按「分类 sort → 商品 sort」排序写入 sort 行号
|
||||
* 6. 源订单批量回写 status = 采购中、purchase_id = 采购单ID(分摊按 purchase_id 溯源)
|
||||
* 2. 创建采购单头(PO 单号;total_quantity / estimate_amount 取订货明细汇总)
|
||||
* 3. 源订单批量回写 status = 采购中、purchase_id = 采购单ID,
|
||||
* 订货明细同步回写 purchase_id(采购明细直接溯源订货明细,无独立采购明细表)
|
||||
*/
|
||||
class PurchaseGenerateService
|
||||
{
|
||||
@@ -53,96 +49,23 @@ class PurchaseGenerateService
|
||||
throw new RepositoryException('无已接单订单,无法生成采购单');
|
||||
}
|
||||
|
||||
// 2. 展开明细按商品聚合
|
||||
$aggregated = [];
|
||||
$sourceOrderIds = [];
|
||||
foreach ($orders as $order) {
|
||||
$sourceOrderIds[] = $order->id;
|
||||
foreach ($order->items as $item) {
|
||||
$productId = (int) $item->product_id;
|
||||
if (! isset($aggregated[$productId])) {
|
||||
$aggregated[$productId] = [
|
||||
'product_id' => $productId,
|
||||
'product_name' => $item->product_name,
|
||||
'product_spec' => $item->product_spec,
|
||||
'quantity' => '0',
|
||||
];
|
||||
}
|
||||
$aggregated[$productId]['quantity'] = bcadd(
|
||||
$aggregated[$productId]['quantity'],
|
||||
(string) $item->quantity,
|
||||
2
|
||||
);
|
||||
}
|
||||
}
|
||||
$sourceOrderIds = $orders->pluck('id')->all();
|
||||
|
||||
if ($aggregated === []) {
|
||||
// 2. 订货明细汇总(采购单头数据)
|
||||
$items = StoreOrderItemModel::query()->whereIn('order_id', $sourceOrderIds)->get();
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('已接单订单均无明细,无法生成采购单');
|
||||
}
|
||||
|
||||
$products = ProductModel::withTrashed()
|
||||
->with('category:id,sort')
|
||||
->whereIn('id', array_keys($aggregated))
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
// 3. 估算单价 = 最低实际等级价(逐行按计价类型换算后取 min;数量级 = 当日 SKU × 等级,可控)
|
||||
$priceRows = ProductPriceModel::query()
|
||||
->whereIn('product_id', array_keys($aggregated))
|
||||
->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 = [];
|
||||
foreach ($aggregated as $productId => $item) {
|
||||
$product = $products->get($productId);
|
||||
$price = (string) ($minPrices[$productId] ?? '0');
|
||||
$rows[] = [
|
||||
'product_id' => $productId,
|
||||
'supplier_id' => (int) ($product->supplier_id ?? 0),
|
||||
'product_name' => $item['product_name'],
|
||||
'product_spec' => $item['product_spec'],
|
||||
'price' => $price,
|
||||
'quantity' => $item['quantity'],
|
||||
'amount' => bcmul($item['quantity'], $price, 2),
|
||||
'category_sort' => (int) ($product->category->sort ?? 9999),
|
||||
'product_sort' => (int) ($product->sort ?? 9999),
|
||||
];
|
||||
}
|
||||
usort($rows, static fn (array $a, array $b): int =>
|
||||
[$a['category_sort'], $a['product_sort'], $a['product_id']]
|
||||
<=> [$b['category_sort'], $b['product_sort'], $b['product_id']]);
|
||||
|
||||
$estimateAmount = array_reduce(
|
||||
$rows,
|
||||
static fn (string $carry, array $row): string => bcadd($carry, $row['amount'], 2),
|
||||
$totalQuantity = $items->reduce(
|
||||
static fn (string $carry, $item): string => bcadd($carry, (string) $item->quantity, 2),
|
||||
'0'
|
||||
);
|
||||
$totalQuantity = array_reduce(
|
||||
$rows,
|
||||
static fn (string $carry, array $row): string => bcadd($carry, $row['quantity'], 2),
|
||||
$estimateAmount = $items->reduce(
|
||||
static fn (string $carry, $item): string => bcadd($carry, (string) $item->amount, 2),
|
||||
'0'
|
||||
);
|
||||
|
||||
// 4. 采购单头
|
||||
$purchase = PurchaseOrderModel::create([
|
||||
'purchase_no' => $this->billNumberService->make('PO'),
|
||||
'purchase_date' => $date,
|
||||
@@ -154,30 +77,14 @@ class PurchaseGenerateService
|
||||
'operator_id' => $operatorId,
|
||||
]);
|
||||
|
||||
// 5. 采购明细(sort 行号)
|
||||
$sort = 1;
|
||||
foreach ($rows as $row) {
|
||||
PurchaseOrderItemModel::create([
|
||||
'purchase_id' => $purchase->id,
|
||||
'product_id' => $row['product_id'],
|
||||
'supplier_id' => $row['supplier_id'],
|
||||
'product_name' => $row['product_name'],
|
||||
'product_spec' => $row['product_spec'],
|
||||
'price' => $row['price'],
|
||||
'quantity' => $row['quantity'],
|
||||
'weight' => 0,
|
||||
'amount' => $row['amount'],
|
||||
'sort' => $sort++,
|
||||
'is_sent' => PurchaseOrderItemModel::NOT_SENT,
|
||||
]);
|
||||
}
|
||||
|
||||
// 6. 源订单回写「采购中」并关联采购单(分摊按 purchase_id 溯源)
|
||||
// 3. 源订单回写「采购中」并关联采购单;订货明细同步回写 purchase_id
|
||||
StoreOrderModel::whereIn('id', $sourceOrderIds)
|
||||
->update([
|
||||
'status' => StoreOrderModel::STATUS_DELIVERING,
|
||||
'purchase_id' => $purchase->id,
|
||||
]);
|
||||
StoreOrderItemModel::whereIn('order_id', $sourceOrderIds)
|
||||
->update(['purchase_id' => $purchase->id]);
|
||||
|
||||
return $purchase;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user