258 lines
9.3 KiB
Go
258 lines
9.3 KiB
Go
package handler
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/http"
|
|||
|
|
"strings"
|
|||
|
|
|
|||
|
|
"bj_power_wms/ent"
|
|||
|
|
"bj_power_wms/ent/inventory"
|
|||
|
|
"bj_power_wms/ent/inventorylock"
|
|||
|
|
"bj_power_wms/ent/outbounddetail"
|
|||
|
|
"bj_power_wms/ent/outboundorder"
|
|||
|
|
"bj_power_wms/internal/svc"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// voidOutboundHandler 出库单作废(2026-11-03 断头路补全,对齐入库作废 inbound_void):
|
|||
|
|
// 人工手工出库(通用出库/科技项目出库)填错物料、批次、数量时的纠错闭环——
|
|||
|
|
// 填写作废原因 → 按本单明细把库存原样回滚 → 单据标记作废(记录保留供追溯)。
|
|||
|
|
//
|
|||
|
|
// 适用范围:仅 outbound_type ∈ {general 通用出库, project 科技项目出库}。
|
|||
|
|
// 备料出库(workorder)/补料出库(refill)/叫料出库(call) 与 MES 备料台账联动,
|
|||
|
|
// 不提供作废——错发统一走「退库确认」逆向闭环(台账 returned_qty 回冲、叫料池额度回补),
|
|||
|
|
// 两套纠错口径不混用,避免台账与叫料池账目失真。
|
|||
|
|
//
|
|||
|
|
// 回滚口径(与出库动作精确互逆):
|
|||
|
|
// - 序列号=是(按 SN):按本单明细的 SN 把库存行「出库」恢复为「在库」(出库前是锁定的恢复为锁定);
|
|||
|
|
// SN 全局唯一一码一物,只恢复本单的 SN。若该 SN 已不在「出库」状态(已被退库回冲等)拒绝,防止双重回冲。
|
|||
|
|
// - 序列号=否(按批次):按本单明细数量加回批次库存行;批次行不存在(已被消耗清零删除)时拒绝并提示走退库。
|
|||
|
|
//
|
|||
|
|
// 安全门禁:已作废单拒绝重复操作;盘点冻结中的批次/SN 拒绝(盘点期间账实冻结)。
|
|||
|
|
func voidOutboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|||
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|||
|
|
var req struct {
|
|||
|
|
OutboundNo string `json:"outboundNo"`
|
|||
|
|
Reason string `json:"reason"`
|
|||
|
|
}
|
|||
|
|
if err := parseJSON(r, &req); err != nil {
|
|||
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
req.OutboundNo = strings.TrimSpace(req.OutboundNo)
|
|||
|
|
req.Reason = strings.TrimSpace(req.Reason)
|
|||
|
|
if req.OutboundNo == "" {
|
|||
|
|
fail(w, http.StatusBadRequest, "出库单号必填")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if req.Reason == "" {
|
|||
|
|
fail(w, http.StatusBadRequest, "作废原因必填")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
ob, err := ctx.EntClient.OutboundOrder.Query().
|
|||
|
|
Where(outboundorder.OutboundNoEQ(req.OutboundNo)).Only(ctx0())
|
|||
|
|
if err != nil {
|
|||
|
|
fail(w, http.StatusNotFound, "出库单不存在:"+req.OutboundNo)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if ob.Voided {
|
|||
|
|
fail(w, http.StatusConflict, "该出库单已作废,请勿重复操作")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if ob.OutboundType != "general" && ob.OutboundType != "project" {
|
|||
|
|
fail(w, http.StatusConflict, "仅「通用出库」「科技项目出库」支持作废;备料/补料/叫料出库请走「退库确认」逆向冲正")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
details, err := ctx.EntClient.OutboundDetail.Query().
|
|||
|
|
Where(outbounddetail.OutboundNoEQ(req.OutboundNo)).All(ctx0())
|
|||
|
|
if err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if len(details) == 0 {
|
|||
|
|
fail(w, http.StatusConflict, "该出库单无明细,无法回滚库存")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 盘点冻结守卫:作废要把库存加回/恢复,盘点冻结目标一律拦截
|
|||
|
|
snList := make([]string, 0, len(details))
|
|||
|
|
batchNo := ""
|
|||
|
|
for _, d := range details {
|
|||
|
|
if d.SnCode != "" {
|
|||
|
|
snList = append(snList, d.SnCode)
|
|||
|
|
} else if d.BatchNo != "" {
|
|||
|
|
batchNo = d.BatchNo
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if !assertNotFrozenTargets(ctx, w, batchNo, snList) {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
tx, err := ctx.EntClient.Tx(ctx0())
|
|||
|
|
if err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
committed := false
|
|||
|
|
defer func() {
|
|||
|
|
if !committed {
|
|||
|
|
_ = tx.Rollback()
|
|||
|
|
}
|
|||
|
|
}()
|
|||
|
|
|
|||
|
|
restoredSN := 0
|
|||
|
|
restoredQty := 0
|
|||
|
|
if ob.SnMode == snModeSN {
|
|||
|
|
// 序列号=是:逐个 SN 恢复(只恢复本单 SN 的库存行)
|
|||
|
|
for _, d := range details {
|
|||
|
|
if d.SnCode == "" {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
inv, e := tx.Inventory.Query().Where(inventory.SnCodeEQ(d.SnCode)).Only(ctx0())
|
|||
|
|
if e != nil {
|
|||
|
|
fail(w, http.StatusConflict, "SN 库存行不存在,无法作废:"+d.SnCode)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if inv.Status != "出库" {
|
|||
|
|
fail(w, http.StatusConflict, "SN "+d.SnCode+" 当前状态为「"+inv.Status+"」(已被退库或后续处理),无法作废,请核对走退库流程")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// 出库前若是锁定状态(lockedQty>0),恢复为「锁定」;否则恢复「在库」
|
|||
|
|
upd := tx.Inventory.UpdateOneID(inv.ID).SetStatus("在库")
|
|||
|
|
if inv.LockedQty > 0 {
|
|||
|
|
upd = tx.Inventory.UpdateOneID(inv.ID).SetStatus("锁定")
|
|||
|
|
}
|
|||
|
|
if err := upd.Exec(ctx0()); err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, "恢复 SN 库存失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
restoredSN++
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
// 序列号=否:按本单明细数量加回批次库存行
|
|||
|
|
for _, d := range details {
|
|||
|
|
if d.BatchNo == "" || d.Quantity <= 0 {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
inv, e := tx.Inventory.Query().
|
|||
|
|
Where(inventory.SnModeEQ(snModeBatch), inventory.BatchNoEQ(d.BatchNo)).Only(ctx0())
|
|||
|
|
if e != nil {
|
|||
|
|
fail(w, http.StatusConflict, "批次库存行不存在(该批次已被消耗清零),无法作废:"+d.BatchNo+",请走「生产退不良」/「退库确认」冲正")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if _, err := tx.Inventory.UpdateOneID(inv.ID).AddQuantity(d.Quantity).Save(ctx0()); err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, "回加批次库存失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
restoredQty += d.Quantity
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if _, err := tx.OutboundOrder.UpdateOneID(ob.ID).
|
|||
|
|
SetVoided(true).SetVoidReason(req.Reason).
|
|||
|
|
SetNillableVoidedBy(strPtr(r.Header.Get("X-Username"))).
|
|||
|
|
SetVoidedAt(timeNow().Unix()).
|
|||
|
|
Save(ctx0()); err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, "标记作废失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if err := tx.Commit(); err != nil {
|
|||
|
|
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
committed = true
|
|||
|
|
|
|||
|
|
typeLabel := outboundTypeLabel(ob.OutboundType)
|
|||
|
|
ctx.EventLog.Write(ctx0(), "outbound.void", r.Header.Get("X-Username"), "outbound", req.OutboundNo,
|
|||
|
|
"作废"+typeLabel+"单 "+req.OutboundNo+"("+ob.MaterialCode+" ×"+itoa(ob.Quantity)+"),原因:"+req.Reason, map[string]any{
|
|||
|
|
"restoredSN": restoredSN, "restoredQty": restoredQty})
|
|||
|
|
ok(w, map[string]any{
|
|||
|
|
"outboundNo": req.OutboundNo,
|
|||
|
|
"restoredSN": restoredSN,
|
|||
|
|
"restoredQty": restoredQty,
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// markOutboundDeliveredHandler AGV 到位回写(2026-11-03 断头路补全):
|
|||
|
|
// 出库单状态机定义「已出库 → 已送达(AGV 配送到位)」,但此前全库无写者(帮助文档与筛选下拉均引用该状态)。
|
|||
|
|
// MES AGV 回调(任务结束=工件/料已到目标接驳台)凭出库单号调本内部接口置「已送达」。
|
|||
|
|
// 幂等:已是「已送达」或已作废的单直接跳过,重复回调无害;best-effort,失败不阻断 MES 回调。
|
|||
|
|
func markOutboundDeliveredHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
|||
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|||
|
|
var req struct {
|
|||
|
|
OutboundNos []string `json:"outboundNos"`
|
|||
|
|
}
|
|||
|
|
if err := parseJSON(r, &req); err != nil {
|
|||
|
|
fail(w, http.StatusBadRequest, "参数错误")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
delivered := 0
|
|||
|
|
for _, no := range req.OutboundNos {
|
|||
|
|
no = strings.TrimSpace(no)
|
|||
|
|
if no == "" {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
aff, err := ctx.EntClient.OutboundOrder.Update().
|
|||
|
|
Where(outboundorder.OutboundNoEQ(no), outboundorder.StatusEQ("已出库"), outboundorder.VoidedEQ(false)).
|
|||
|
|
SetStatus("已送达").Save(ctx0())
|
|||
|
|
if err == nil {
|
|||
|
|
delivered += aff
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if delivered > 0 {
|
|||
|
|
ctx.EventLog.Write(ctx0(), "outbound.delivered", "AGV", "outbound", strings.Join(req.OutboundNos, ","),
|
|||
|
|
"AGV 到位回写出库单已送达,共 "+itoa(delivered)+" 张", nil)
|
|||
|
|
}
|
|||
|
|
ok(w, map[string]any{"delivered": delivered})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// consumeLocksForBatch 出库核销批次锁记录(2026-11-03 断头路补全):
|
|||
|
|
// 出库扣减 locked_qty 的同时,把该批次对应 ACTIVE 锁记录按 FIFO 递减,扣到 0 置 CONSUMED。
|
|||
|
|
// 消除"锁创建后永不核销"的残留:此前 CONSUMED 状态全库 0 处使用,MES 锁库后货物出库,
|
|||
|
|
// ACTIVE 锁记录永久残留(仅能靠 MES 显式 unlock 收口)。
|
|||
|
|
func consumeLocksForBatch(tx *ent.Tx, batchNo, materialCode string, qty int) {
|
|||
|
|
if qty <= 0 {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
locks, err := tx.InventoryLock.Query().
|
|||
|
|
Where(inventorylock.TargetTypeEQ("BATCH"), inventorylock.TargetIDEQ(batchNo),
|
|||
|
|
inventorylock.MaterialCodeEQ(materialCode), inventorylock.StatusEQ("ACTIVE")).
|
|||
|
|
Order(ent.Asc(inventorylock.FieldID)).All(ctx0())
|
|||
|
|
if err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
remain := qty
|
|||
|
|
for _, l := range locks {
|
|||
|
|
if remain <= 0 {
|
|||
|
|
break
|
|||
|
|
}
|
|||
|
|
take := l.LockedQty
|
|||
|
|
if take > remain {
|
|||
|
|
take = remain
|
|||
|
|
}
|
|||
|
|
left := l.LockedQty - take
|
|||
|
|
upd := tx.InventoryLock.UpdateOneID(l.ID)
|
|||
|
|
if left <= 0 {
|
|||
|
|
upd = upd.SetLockedQty(0).SetStatus("CONSUMED")
|
|||
|
|
} else {
|
|||
|
|
upd = upd.SetLockedQty(left)
|
|||
|
|
}
|
|||
|
|
_, _ = upd.Save(ctx0())
|
|||
|
|
remain -= take
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// consumeLocksForSN 出库核销 SN 锁记录:SN 锁一码一量,出库即整条 CONSUMED。
|
|||
|
|
func consumeLocksForSN(tx *ent.Tx, sns []string) {
|
|||
|
|
if len(sns) == 0 {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
_, _ = tx.InventoryLock.Update().
|
|||
|
|
Where(inventorylock.TargetTypeEQ("SN"), inventorylock.TargetIDIn(sns...),
|
|||
|
|
inventorylock.StatusEQ("ACTIVE")).
|
|||
|
|
SetLockedQty(0).SetStatus("CONSUMED").Save(ctx0())
|
|||
|
|
}
|