生成采购单基础

This commit is contained in:
liu
2026-08-11 21:08:01 +08:00
parent 06c7bd9f88
commit 5d26f04ba2
13 changed files with 362 additions and 68 deletions
+3 -5
View File
@@ -7,7 +7,6 @@ use App\Models\PurchaseAllocationModel;
use App\Models\PurchaseOrderItemModel;
use App\Models\PurchaseOrderModel;
use App\Models\StoreOrderItemModel;
use App\Models\StoreOrderModel;
use Illuminate\Support\Facades\DB;
/**
@@ -15,7 +14,7 @@ use Illuminate\Support\Facades\DB;
*
* 流程(事务内):
* 1. 采购单须已录入实际金额(存在 amount>0 的明细),否则拒绝
* 2. 每个采购明细溯源采购日当天、已汇总订单」中该商品的订货明细
* 2. 每个采购明细按 purchase_id 溯源采购单合并的门店订单明细(生成采购单时回写)
* 3. 按订货数量比例分摊实际金额/数量/重量:bcmul(item.amount, bcdiv(item_qty, total_qty, 6), 2)
* 尾差修正——最后一行承担舍入差额,保证 Σallocation.amount === item.amount(金额守恒)
* 4. 重复分摊先删旧记录再重建(幂等)
@@ -38,11 +37,10 @@ class PurchaseAllocateService
throw new RepositoryException('采购单尚未录入实际金额,无法分摊');
}
// 2. 溯源采购日当天「已汇总」订单的订货明细,按商品分组
// 2. 按 purchase_id 溯源采购单合并的门店订单明细,按商品分组
$orderItems = StoreOrderItemModel::query()
->join('store_order', 'store_order.id', '=', 'store_order_item.order_id')
->whereDate('store_order.order_date', $purchase->purchase_date)
->where('store_order.status', StoreOrderModel::STATUS_SUMMARIZED)
->where('store_order.purchase_id', $purchase->id)
->select('store_order_item.*')
->get()
->groupBy('product_id');