采购单优化
This commit is contained in:
@@ -4,22 +4,29 @@ namespace App\Http\Controllers\Purchase;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Exports\PurchaseOrderExport;
|
||||
use App\Exports\PurchaseStoreExport;
|
||||
use App\Exports\PurchaseSupplierExport;
|
||||
use App\Http\Requests\Purchase\PurchaseBillGenerateRequest;
|
||||
use App\Http\Requests\Purchase\PurchaseCellUpdateRequest;
|
||||
use App\Http\Requests\Purchase\PurchaseRowUpdateRequest;
|
||||
use App\Http\Requests\Purchase\PurchaseStoreItemRequest;
|
||||
use App\Models\BillModel;
|
||||
use App\Models\CustomerLevelModel;
|
||||
use App\Models\ProductModel;
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use App\Models\SupplierModel;
|
||||
use App\Services\BillGenerateService;
|
||||
use App\Services\ItemImageResolver;
|
||||
use App\Services\PurchaseGenerateService;
|
||||
use App\Services\PurchaseItemService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\AnnoRoute\Attribute\DeleteRoute;
|
||||
use Modules\AnnoRoute\Attribute\GetRoute;
|
||||
use Modules\AnnoRoute\Attribute\PostRoute;
|
||||
use Modules\AnnoRoute\Attribute\PutRoute;
|
||||
@@ -98,6 +105,8 @@ class PurchaseOrderController extends BaseController
|
||||
$quantity = 0;
|
||||
// 采购总重量
|
||||
$weight = '0';
|
||||
// 采购总金额(Σ明细 amount,参考零售价按 金额÷数量 加权)
|
||||
$amount = '0';
|
||||
// 门店明细
|
||||
$cellsMap = [];
|
||||
|
||||
@@ -106,6 +115,7 @@ class PurchaseOrderController extends BaseController
|
||||
// 累加总量
|
||||
$quantity += (int) $item->quantity;
|
||||
$weight = bcadd($weight, (string) $item->weight, 3);
|
||||
$amount = bcadd($amount, (string) $item->amount, 2);
|
||||
|
||||
// 按门店合并
|
||||
if (!isset($cellsMap[$storeId])) {
|
||||
@@ -127,6 +137,7 @@ class PurchaseOrderController extends BaseController
|
||||
'cost_price' => $first->cost_price, // 成本价
|
||||
'quantity' => $quantity,
|
||||
'weight' => (float) $weight,
|
||||
'amount' => $amount,
|
||||
'cells' => $cellsMap,
|
||||
'category_sort' => (int) ($product->category->sort ?? 9999),
|
||||
'product_sort' => (int) ($product->sort ?? 9999),
|
||||
@@ -247,22 +258,68 @@ class PurchaseOrderController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出采购单 Excel 表格
|
||||
* 导出采购单商品明细 Excel 表格(系统全部商品行,支持按供应商筛选)
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
#[GetRoute(route: '/{id}/export', authorize: 'export', where: ['id' => '[0-9]+'])]
|
||||
public function export(int $id): Response
|
||||
public function export(int $id, Request $request): Response
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$supplierId = (int) $request->query('supplier_id', 0);
|
||||
|
||||
return Excel::download(
|
||||
new PurchaseOrderExport($purchase),
|
||||
$purchase->purchase_no . '_采购单.xlsx',
|
||||
);
|
||||
$filename = $purchase->purchase_no . '_采购单.xlsx';
|
||||
if ($supplierId > 0) {
|
||||
$supplier = SupplierModel::withTrashed()->find($supplierId);
|
||||
$filename = $purchase->purchase_no . '_采购单_' . ($supplier->name ?? ('供应商' . $supplierId)) . '.xlsx';
|
||||
}
|
||||
|
||||
return Excel::download(new PurchaseOrderExport($purchase, $supplierId), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出门店购买详情(多工作表:每门店一个工作表;?store_id= 单门店导出)
|
||||
*/
|
||||
#[GetRoute(route: '/{id}/exportStores', authorize: 'export', where: ['id' => '[0-9]+'])]
|
||||
public function exportStores(int $id, Request $request): Response
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$storeId = (int) $request->query('store_id', 0);
|
||||
|
||||
$filename = $purchase->purchase_no . '_门店购买详情.xlsx';
|
||||
if ($storeId > 0) {
|
||||
$store = StoreModel::withTrashed()->find($storeId);
|
||||
$filename = $purchase->purchase_no . '_门店购买详情_' . ($store->name ?? ('门店' . $storeId)) . '.xlsx';
|
||||
}
|
||||
|
||||
return Excel::download(new PurchaseStoreExport($purchase, $storeId), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出供应商采购明细(多工作表:每供应商一个工作表;?supplier_id= 单供应商导出)
|
||||
*/
|
||||
#[GetRoute(route: '/{id}/exportSuppliers', authorize: 'export', where: ['id' => '[0-9]+'])]
|
||||
public function exportSuppliers(int $id, Request $request): Response
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$supplierId = (int) $request->query('supplier_id', 0);
|
||||
|
||||
$filename = $purchase->purchase_no . '_供应商采购明细.xlsx';
|
||||
if ($supplierId > 0) {
|
||||
$supplier = SupplierModel::withTrashed()->find($supplierId);
|
||||
$filename = $purchase->purchase_no . '_供应商采购明细_' . ($supplier->name ?? ('供应商' . $supplierId)) . '.xlsx';
|
||||
}
|
||||
|
||||
return Excel::download(new PurchaseSupplierExport($purchase, $supplierId), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -348,68 +405,191 @@ class PurchaseOrderController extends BaseController
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
|
||||
$items = StoreOrderItemModel::query()
|
||||
->join('store_order', 'store_order.id', '=', 'store_order_item.order_id')
|
||||
->where('store_order_item.purchase_id', $purchase->id)
|
||||
->where('store_order_item.store_id', $storeId)
|
||||
->whereNull('store_order.deleted_at')
|
||||
->select('store_order_item.*')
|
||||
->orderBy('store_order_item.id')
|
||||
->get();
|
||||
|
||||
// 排序键:分类 sort → 商品 sort(与明细矩阵同序)
|
||||
$products = ProductModel::withTrashed()
|
||||
->with('category:id,sort')
|
||||
->whereIn('id', $items->pluck('product_id')->unique())
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$rows = [];
|
||||
foreach ($items->groupBy('product_id') as $productId => $group) {
|
||||
$product = $products->get((int) $productId);
|
||||
$first = $group->first();
|
||||
// 采购总数量(包数)
|
||||
$quantity = 0;
|
||||
// 预计金额 = Σ 明细 amount
|
||||
$amount = '0';
|
||||
// 总重量
|
||||
$weight = '0';
|
||||
foreach ($group as $item) {
|
||||
$weight = bcadd($weight, (string) $item->weight, 3);
|
||||
$quantity += (int) $item->quantity;
|
||||
$amount = bcadd($amount, (string) $item->amount, 2);
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'product_id' => (int) $productId,
|
||||
'product_name' => $first->product_name,
|
||||
'product_spec' => $first->product_spec, // 包规
|
||||
'unit' => $first->unit, // 单位
|
||||
'price' => $quantity > 0 // 加权平均单价
|
||||
? bcdiv($amount, (string) $quantity, 2)
|
||||
: (string) $first->price,
|
||||
'quantity' => $quantity,
|
||||
'amount' => $amount,
|
||||
'weight' => $weight,
|
||||
'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']]);
|
||||
|
||||
$store = StoreModel::withTrashed()->find($storeId);
|
||||
|
||||
return $this->success([
|
||||
'store' => $store ? ['id' => $store->id, 'name' => $store->name] : null,
|
||||
'items' => array_map(static function (array $row): array {
|
||||
unset($row['category_sort'], $row['product_sort']);
|
||||
return $row;
|
||||
}, $rows),
|
||||
'items' => app(PurchaseItemService::class)->storeSummaryRows($purchase->id, $storeId),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 门店购买详情:新增单品(挂靠该门店在采购单中的最新一笔订单,
|
||||
* 单价按门店等级上浮比例换算,无等级按成本价兜底)
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[PostRoute(route: '/{id}/store/{storeId}/item', authorize: 'update', where: ['id' => '[0-9]+', 'storeId' => '[0-9]+'])]
|
||||
public function storeItemStore(int $id, int $storeId, PurchaseStoreItemRequest $request): JsonResponse
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$this->assertPurchaseEditable($purchase);
|
||||
|
||||
$validated = $request->validated();
|
||||
$productId = (int) $validated['product_id'];
|
||||
$product = ProductModel::find($productId);
|
||||
if ($product === null) {
|
||||
throw new RepositoryException('商品不存在或已被删除,无法添加');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($purchase, $storeId, $product, $validated) {
|
||||
// 该门店在采购单中的最新一笔订单
|
||||
$order = StoreOrderModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('store_id', $storeId)
|
||||
->orderByDesc('id')
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if ($order === null) {
|
||||
throw new RepositoryException('该门店不在此采购单中,无法添加单品');
|
||||
}
|
||||
|
||||
$duplicated = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('store_id', $storeId)
|
||||
->where('product_id', $product->id)
|
||||
->exists();
|
||||
if ($duplicated) {
|
||||
throw new RepositoryException('该商品已在此门店采购明细中,请直接修改数量');
|
||||
}
|
||||
|
||||
// 单价:门店等级上浮换算价(无等级按成本价兜底)
|
||||
$store = StoreModel::withTrashed()->find($storeId);
|
||||
$level = ($store !== null && $store->level_id > 0) ? $store->level : null;
|
||||
$price = $level !== null
|
||||
? CustomerLevelModel::calcLevelPrice($product->cost_price, $level->percent)
|
||||
: bcadd((string) $product->cost_price, '0', 2);
|
||||
$quantity = (int) $validated['quantity'];
|
||||
|
||||
$item = StoreOrderItemModel::create([
|
||||
'order_id' => $order->id,
|
||||
'purchase_id' => $purchase->id,
|
||||
'bill_id' => 0,
|
||||
'store_id' => $storeId,
|
||||
'product_id' => $product->id,
|
||||
'category_id' => (int) $product->category_id,
|
||||
'supplier_id' => (int) $product->supplier_id,
|
||||
'product_name' => $product->name,
|
||||
'product_spec' => $product->spec,
|
||||
'unit' => (string) $product->unit,
|
||||
'price' => $price,
|
||||
'image_ids' => implode(',', (array) $product->image_ids),
|
||||
'content' => (string) $product->content,
|
||||
'shelf_life' => (int) $product->shelf_life,
|
||||
'quantity' => $quantity,
|
||||
'weight' => bcadd((string) ($validated['weight'] ?? 0), '0', 3),
|
||||
'amount' => bcmul($price, (string) $quantity, 2),
|
||||
'cost_price' => (string) $product->cost_price,
|
||||
'remark' => '',
|
||||
]);
|
||||
|
||||
$service = app(PurchaseItemService::class);
|
||||
$service->recalcOrder($order->id);
|
||||
$service->recalcPurchase($purchase->id);
|
||||
|
||||
return $this->success(['id' => $item->id], '已添加单品');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 门店购买详情:修改单品(数量/称重/单价);同门店同商品多笔订单明细时合并到最早一条
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[PutRoute(route: '/{id}/store/{storeId}/item/{productId}', authorize: 'update', where: ['id' => '[0-9]+', 'storeId' => '[0-9]+', 'productId' => '[0-9]+'])]
|
||||
public function storeItemUpdate(int $id, int $storeId, int $productId, PurchaseStoreItemRequest $request): JsonResponse
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$this->assertPurchaseEditable($purchase);
|
||||
|
||||
$validated = $request->validated();
|
||||
|
||||
return DB::transaction(function () use ($purchase, $storeId, $productId, $validated) {
|
||||
$items = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('store_id', $storeId)
|
||||
->where('product_id', $productId)
|
||||
->orderBy('id')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('该门店在此采购单中无此商品明细');
|
||||
}
|
||||
|
||||
$affectedOrderIds = $items->pluck('order_id')->unique()->all();
|
||||
|
||||
// 多笔订单明细合并到最早一条
|
||||
$survivor = $items->first();
|
||||
foreach ($items->skip(1) as $extra) {
|
||||
$extra->delete();
|
||||
}
|
||||
|
||||
$survivor->quantity = (int) $validated['quantity'];
|
||||
if (isset($validated['price'])) {
|
||||
$survivor->price = bcadd((string) $validated['price'], '0', 2);
|
||||
}
|
||||
if (isset($validated['weight'])) {
|
||||
$survivor->weight = bcadd((string) $validated['weight'], '0', 3);
|
||||
}
|
||||
$survivor->amount = bcmul((string) $survivor->quantity, (string) $survivor->price, 2);
|
||||
$survivor->save();
|
||||
|
||||
$service = app(PurchaseItemService::class);
|
||||
foreach ($affectedOrderIds as $orderId) {
|
||||
$service->recalcOrder((int) $orderId);
|
||||
}
|
||||
$service->recalcPurchase($purchase->id);
|
||||
|
||||
return $this->success([], '单品已更新');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 门店购买详情:移除单品(该门店此商品的全部订货明细一并删除)
|
||||
*
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[DeleteRoute(route: '/{id}/store/{storeId}/item/{productId}', authorize: 'update', where: ['id' => '[0-9]+', 'storeId' => '[0-9]+', 'productId' => '[0-9]+'])]
|
||||
public function storeItemDelete(int $id, int $storeId, int $productId): JsonResponse
|
||||
{
|
||||
$purchase = PurchaseOrderModel::find($id);
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
$this->assertPurchaseEditable($purchase);
|
||||
|
||||
DB::transaction(function () use ($purchase, $storeId, $productId) {
|
||||
$items = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('store_id', $storeId)
|
||||
->where('product_id', $productId)
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('该门店在此采购单中无此商品明细');
|
||||
}
|
||||
|
||||
$affectedOrderIds = $items->pluck('order_id')->unique()->all();
|
||||
foreach ($items as $item) {
|
||||
$item->delete();
|
||||
}
|
||||
|
||||
$service = app(PurchaseItemService::class);
|
||||
foreach ($affectedOrderIds as $orderId) {
|
||||
$service->recalcOrder((int) $orderId);
|
||||
}
|
||||
$service->recalcPurchase($purchase->id);
|
||||
});
|
||||
|
||||
return $this->success([], '已移除单品');
|
||||
}
|
||||
|
||||
/**
|
||||
* 账单生成预览
|
||||
*/
|
||||
@@ -489,7 +669,7 @@ class PurchaseOrderController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品行修改:品名/供应商/包规/单位/成本
|
||||
* 商品行修改:品名/供应商/包规/单位/成本(同步更新商品档案;商品已删除则跳过档案同步)
|
||||
* @throws Throwable
|
||||
*/
|
||||
#[PutRoute(route: '/{id}/row/{productId}', authorize: 'update', where: ['id' => '[0-9]+', 'productId' => '[0-9]+'])]
|
||||
@@ -499,31 +679,45 @@ class PurchaseOrderController extends BaseController
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
if ($purchase->status !== PurchaseOrderModel::STATUS_PENDING) {
|
||||
throw new RepositoryException('采购单已完成,不允许修改明细');
|
||||
}
|
||||
$this->assertPurchaseEditable($purchase);
|
||||
|
||||
$validated = $request->validated();
|
||||
|
||||
$items = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('product_id', $productId)
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('该采购单下无此商品的订货明细');
|
||||
}
|
||||
foreach ($items as $item) {
|
||||
$item->fill($validated)->save();
|
||||
}
|
||||
return DB::transaction(function () use ($purchase, $productId, $validated) {
|
||||
$items = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->where('product_id', $productId)
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
if ($items->isEmpty()) {
|
||||
throw new RepositoryException('该采购单下无此商品的订货明细');
|
||||
}
|
||||
foreach ($items as $item) {
|
||||
$item->fill($validated)->save();
|
||||
}
|
||||
|
||||
// 重算采购单成本
|
||||
$query = StoreOrderItemModel::query()->where('purchase_id', $purchase->id);
|
||||
$purchase->total_quantity = $query->sum('quantity');
|
||||
$purchase->estimate_amount = $query->sum(DB::raw('quantity * cost_price'));
|
||||
$purchase->save();
|
||||
// 同步保存到商品档案(软删除商品跳过,明细照常更新)
|
||||
$product = ProductModel::find($productId);
|
||||
$productSynced = $product !== null;
|
||||
if ($productSynced) {
|
||||
$product->update([
|
||||
'name' => $validated['product_name'],
|
||||
'supplier_id' => $validated['supplier_id'],
|
||||
'spec' => $validated['product_spec'],
|
||||
'unit' => $validated['unit'],
|
||||
'cost_price' => $validated['cost_price'],
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success(['count' => $items->count()], '已同步 ' . $items->count() . ' 条订货明细');
|
||||
app(PurchaseItemService::class)->recalcPurchase($purchase->id);
|
||||
|
||||
return $this->success(
|
||||
['count' => $items->count()],
|
||||
$productSynced
|
||||
? '已同步 ' . $items->count() . ' 条订货明细与商品档案'
|
||||
: '已同步 ' . $items->count() . ' 条订货明细;商品已删除,档案未同步'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -544,31 +738,31 @@ class PurchaseOrderController extends BaseController
|
||||
if (empty($purchase)) {
|
||||
throw new RepositoryException('采购单不存在');
|
||||
}
|
||||
if ($purchase->status !== PurchaseOrderModel::STATUS_PENDING) {
|
||||
throw new RepositoryException('采购单已完成,不允许修改明细');
|
||||
}
|
||||
$this->assertPurchaseEditable($purchase);
|
||||
|
||||
$item->quantity = $validated['quantity'];
|
||||
$item->price = $validated['price'];
|
||||
if (array_key_exists('weight', $validated)) {
|
||||
$item->weight = bcadd((string) $validated['weight'], '0', 3);
|
||||
}
|
||||
$item->amount = bcmul($validated['quantity'], $validated['price'], 3);
|
||||
$item->amount = bcmul((string) $validated['quantity'], (string) $validated['price'], 2);
|
||||
$item->save();
|
||||
|
||||
// 重算订单金额
|
||||
$order = StoreOrderModel::query()->lockForUpdate()->find($item->order_id);
|
||||
$order->total_weight = $order->items()->sum('weight');
|
||||
$order->total_amount = $order->items()->sum('amount');
|
||||
$order->save();
|
||||
|
||||
// 重算采购单重量
|
||||
$purchase->total_weight = StoreOrderItemModel::query()
|
||||
->where('purchase_id', $purchase->id)
|
||||
->sum('weight');
|
||||
$purchase->save();
|
||||
$service = app(PurchaseItemService::class);
|
||||
$service->recalcOrder((int) $item->order_id);
|
||||
$service->recalcPurchase($purchase->id);
|
||||
|
||||
return $this->success();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 采购单编辑闸:仅进行中(待采购)允许修改明细
|
||||
*/
|
||||
private function assertPurchaseEditable(PurchaseOrderModel $purchase): void
|
||||
{
|
||||
if ($purchase->status !== PurchaseOrderModel::STATUS_PENDING) {
|
||||
throw new RepositoryException('采购单已完成,不允许修改明细');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user