feat(backend): 出入库定价字段消歧 + 总利润落库 + 成本仅管理员可见
- 字段重命名(旧列弃用保留,启动幂等回填 BackfillPricingColumns): 入库 unit_price/total_price/total_amount → cost_price/cost_amount/cost_total; 出库同前缀 + 新增 sale_amount(售价小计)/ sale_total(应收)/ profit_total(总利润) - 建单落三值;确认售价联动重算 sale_amount/sale_total/profit_total; 确认进价回填成本后联动重算受影响出库单利润(recalcStockOutProfit) - 出库 List/Get 对 operator/readonly 抹零成本与利润(stripStockOutCost 服务端兜底) - 兼容一版:Create/Update 接受旧 key unit_price 回落(v1.0.87 及之前客户端) - SUM 聚合/价格趋势/导入/种子工具同步切新列;测试全量改名 + 6 个新回归 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JJ1g8XV1YhhmHRzhwWEW7o
This commit is contained in:
@@ -63,8 +63,8 @@ func (s *StockService) ApproveStockIn(shopID, orderID, reviewerID uint64) error
|
||||
}
|
||||
|
||||
var unitPricePtr *float64
|
||||
if itemCopy.UnitPrice != 0 {
|
||||
unitPricePtr = &itemCopy.UnitPrice
|
||||
if itemCopy.CostPrice != 0 {
|
||||
unitPricePtr = &itemCopy.CostPrice
|
||||
}
|
||||
|
||||
// 优先使用明细自带快照列(历史导入单的真实商品信息在此),
|
||||
@@ -130,13 +130,13 @@ func (s *StockService) ApproveStockIn(shopID, orderID, reviewerID uint64) error
|
||||
|
||||
// 自动创建应付账款财务记录
|
||||
{
|
||||
bal := partnerLastBalance(tx, shopID, order.PartnerID) + order.TotalAmount
|
||||
bal := partnerLastBalance(tx, shopID, order.PartnerID) + order.CostTotal
|
||||
orderID := order.ID
|
||||
rec := model.FinanceRecord{
|
||||
ShopID: shopID,
|
||||
PartnerID: order.PartnerID,
|
||||
Type: "payable",
|
||||
Amount: order.TotalAmount,
|
||||
Amount: order.CostTotal,
|
||||
Balance: bal,
|
||||
Status: "open",
|
||||
RefType: "stock_in",
|
||||
@@ -172,11 +172,11 @@ type SaleConfirmItem struct {
|
||||
// ConfirmStockInCost 「确认进价」:调货等场景以 0 价(暂估)入库并已审核/已出库后,
|
||||
// 价格确定时补填真实进价。不反审核、不撤销任何已发生的动作,而是在一个事务内
|
||||
// 前向写补偿:
|
||||
// 1. 入库明细 UnitPrice / TotalPrice 改为真实值
|
||||
// 1. 入库明细 CostPrice / CostAmount 改为真实值
|
||||
// 2. 该明细对应的剩余库存批次 Inventory.UnitPrice 由 NULL→真实值
|
||||
// 3. 该 product 已出库行 StockOutItem.UnitPrice/TotalPrice(成本快照)回填真实值
|
||||
// 3. 该 product 已出库行 StockOutItem.CostPrice/CostAmount(成本快照)回填真实值
|
||||
// (product↔入库明细 1:1,按 product_id 精确命中;不动 SalePrice/应收)
|
||||
// 4. 入库单 TotalAmount 按差额重算
|
||||
// 4. 入库单 CostTotal 按差额重算
|
||||
// 5. 对供应商应付按差额补一条调整流水(滚动余额)
|
||||
//
|
||||
// 仅 approved 单可确认(draft 直接编辑即可);权限:管理员/超管。
|
||||
@@ -202,21 +202,22 @@ func (s *StockService) ConfirmStockInCost(shopID, orderID, userID uint64, role s
|
||||
|
||||
now := time.Now()
|
||||
var totalDiff float64
|
||||
var changedProducts []uint64 // 成本被改的商品:其已出库单的利润需联动重算
|
||||
|
||||
for i := range order.Items {
|
||||
it := &order.Items[i]
|
||||
newPrice, ok := want[it.ID]
|
||||
if !ok || newPrice == it.UnitPrice {
|
||||
if !ok || newPrice == it.CostPrice {
|
||||
continue
|
||||
}
|
||||
oldPrice := it.UnitPrice
|
||||
oldPrice := it.CostPrice
|
||||
diff := (newPrice - oldPrice) * it.Quantity
|
||||
totalDiff += diff
|
||||
|
||||
// 1. 入库明细
|
||||
if err := tx.Model(it).Updates(map[string]interface{}{
|
||||
"unit_price": newPrice,
|
||||
"total_price": newPrice * it.Quantity,
|
||||
"cost_price": newPrice,
|
||||
"cost_amount": newPrice * it.Quantity,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -233,8 +234,8 @@ func (s *StockService) ConfirmStockInCost(shopID, orderID, userID uint64, role s
|
||||
if err := tx.Model(&model.StockOutItem{}).
|
||||
Where("shop_id = ? AND product_id = ?", shopID, it.ProductID).
|
||||
Updates(map[string]interface{}{
|
||||
"unit_price": newPrice,
|
||||
"total_price": gorm.Expr("? * quantity", newPrice),
|
||||
"cost_price": newPrice,
|
||||
"cost_amount": gorm.Expr("? * quantity", newPrice),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -245,15 +246,31 @@ func (s *StockService) ConfirmStockInCost(shopID, orderID, userID uint64, role s
|
||||
tx.Model(&model.Product{}).Where("id = ? AND shop_id = ?", it.ProductID, shopID).
|
||||
Update("purchase_price", newPrice)
|
||||
}
|
||||
if it.ProductID != 0 {
|
||||
changedProducts = append(changedProducts, it.ProductID)
|
||||
}
|
||||
}
|
||||
|
||||
// 3.5 成本快照变了 → 受影响出库单的总利润联动重算
|
||||
if len(changedProducts) > 0 {
|
||||
var outOrderIDs []uint64
|
||||
if err := tx.Model(&model.StockOutItem{}).
|
||||
Where("shop_id = ? AND product_id IN ?", shopID, changedProducts).
|
||||
Distinct().Pluck("order_id", &outOrderIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := recalcStockOutProfit(tx, shopID, outOrderIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if totalDiff == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 4. 入库单总额按差额重算
|
||||
// 4. 入库单应付合计按差额重算
|
||||
if err := tx.Model(&order).
|
||||
Update("total_amount", gorm.Expr("total_amount + ?", totalDiff)).Error; err != nil {
|
||||
Update("cost_total", gorm.Expr("cost_total + ?", totalDiff)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -268,8 +285,22 @@ func (s *StockService) ConfirmStockInCost(shopID, orderID, userID uint64, role s
|
||||
})
|
||||
}
|
||||
|
||||
// recalcStockOutProfit 按行利润口径整单重算总利润:
|
||||
// profit_total = Σ(sale_price>0 ? (sale_price-cost_price)×quantity : 0)。
|
||||
// 确认售价 / 确认进价(成本回填)后调用,保证利润与两侧价格始终一致。
|
||||
func recalcStockOutProfit(tx *gorm.DB, shopID uint64, orderIDs []uint64) error {
|
||||
if len(orderIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tx.Exec(`UPDATE stock_out_orders SET profit_total = (
|
||||
SELECT COALESCE(SUM(CASE WHEN i.sale_price > 0
|
||||
THEN (i.sale_price - i.cost_price) * i.quantity ELSE 0 END), 0)
|
||||
FROM stock_out_items i WHERE i.order_id = stock_out_orders.id
|
||||
) WHERE shop_id = ? AND id IN ?`, shopID, orderIDs).Error
|
||||
}
|
||||
|
||||
// ConfirmStockOutSale 出库「先出后定价」:对 sale_price<=0 的明细写真实售价,
|
||||
// 重算应收(total_amount=Σ售价×数量)并补应收差额流水。仅 admin/superadmin、已审核单可确认。
|
||||
// 重算应收(sale_total=Σ售价小计)与总利润,并补应收差额流水。仅 admin/superadmin、已审核单可确认。
|
||||
func (s *StockService) ConfirmStockOutSale(shopID, orderID, userID uint64, role string, items []SaleConfirmItem) error {
|
||||
if role != "admin" && role != "superadmin" {
|
||||
return ErrForbidden
|
||||
@@ -299,7 +330,11 @@ func (s *StockService) ConfirmStockOutSale(shopID, orderID, userID uint64, role
|
||||
}
|
||||
diff := (newPrice - it.SalePrice) * it.Quantity
|
||||
totalDiff += diff
|
||||
if err := tx.Model(it).Update("sale_price", newPrice).Error; err != nil {
|
||||
// 售价与售价小计同步改,避免「单价×数量 ≠ 金额」的口径漂移
|
||||
if err := tx.Model(it).Updates(map[string]interface{}{
|
||||
"sale_price": newPrice,
|
||||
"sale_amount": newPrice * it.Quantity,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -308,9 +343,12 @@ func (s *StockService) ConfirmStockOutSale(shopID, orderID, userID uint64, role
|
||||
return nil
|
||||
}
|
||||
|
||||
// 应收总额按差额重算
|
||||
// 应收合计按差额重算;总利润整单重算
|
||||
if err := tx.Model(&order).
|
||||
Update("total_amount", gorm.Expr("total_amount + ?", totalDiff)).Error; err != nil {
|
||||
Update("sale_total", gorm.Expr("sale_total + ?", totalDiff)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := recalcStockOutProfit(tx, shopID, []uint64{order.ID}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -400,13 +438,13 @@ func (s *StockService) ApproveStockOut(shopID, orderID, reviewerID uint64) error
|
||||
|
||||
// 自动创建应收账款财务记录
|
||||
{
|
||||
bal := partnerLastBalance(tx, shopID, order.PartnerID) + order.TotalAmount
|
||||
bal := partnerLastBalance(tx, shopID, order.PartnerID) + order.SaleTotal
|
||||
oid := order.ID
|
||||
rec := model.FinanceRecord{
|
||||
ShopID: shopID,
|
||||
PartnerID: order.PartnerID,
|
||||
Type: "receivable",
|
||||
Amount: order.TotalAmount,
|
||||
Amount: order.SaleTotal,
|
||||
Balance: bal,
|
||||
Status: "open",
|
||||
RefType: "stock_out",
|
||||
@@ -497,7 +535,7 @@ func (s *StockService) ReturnStockIn(shopID, orderID, userID uint64, role string
|
||||
return err
|
||||
}
|
||||
it.ReturnedQuantity = it.Quantity
|
||||
returnedAmount += it.TotalPrice
|
||||
returnedAmount += it.CostAmount
|
||||
}
|
||||
|
||||
// 冲减应付(负向调整记录,滚动余额)
|
||||
@@ -549,8 +587,8 @@ func (s *StockService) ReturnStockOut(shopID, orderID, userID uint64, role strin
|
||||
Select("COALESCE(SUM(quantity), 0)").Scan(&qtyBefore)
|
||||
|
||||
var unitPricePtr *float64
|
||||
if it.UnitPrice != 0 {
|
||||
up := it.UnitPrice
|
||||
if it.CostPrice != 0 {
|
||||
up := it.CostPrice
|
||||
unitPricePtr = &up
|
||||
}
|
||||
unit := ""
|
||||
@@ -584,7 +622,7 @@ func (s *StockService) ReturnStockOut(shopID, orderID, userID uint64, role strin
|
||||
return err
|
||||
}
|
||||
it.ReturnedQuantity = it.Quantity
|
||||
// 冲应收按售价口径(与审核建应收 TotalAmount=Σ售价×数量 一致)
|
||||
// 冲应收按售价口径(与审核建应收 SaleTotal=Σ售价×数量 一致)
|
||||
returnedAmount += it.Quantity * it.SalePrice
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user