feat: 两库对等物料管理及产品工号贯通全流程
- 新增 MES 本地物料表及双向同步接口,WMS 物料变更自动同步 - 全流程新增产品工号字段,贯通入库/库存/出库/检验环节 - 工单管理支持多工单混产,移除单工单互斥限制 - 新增批次号/SN 字段支持质量检验联动 WMS 库存处置 - 修复权限规则,仅内置管理员账号不允许修改删除 - 工位终端新增 SN 扫码自动切换所属工单功能
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"bj_power_wms/ent"
|
||||
"bj_power_wms/internal/svc"
|
||||
)
|
||||
|
||||
// createSupplierReturnOutbound 为「在库处置·退货 / 复检不合格转退货」自动生成退供应商出库单并真实扣减库存,
|
||||
// 使退货一步闭环:质量置待退 + 实物出库 + 库存减少 + 留痕,全部落在同一事务内。
|
||||
// 调用方须已在 tx 中;inv 为待退货的库存行(整行退)。
|
||||
func createSupplierReturnOutbound(ctx *svc.ServiceContext, tx *ent.Tx, inv *ent.Inventory, remark, operator string) (string, error) {
|
||||
outboundNo, err := nextDailyNo(ctx, "OB")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成出库单号失败: %w", err)
|
||||
}
|
||||
if err = validateUniqueNo(ctx, "OB", outboundNo); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.OutboundOrder.Create().
|
||||
SetOutboundNo(outboundNo).
|
||||
SetOutboundType("general").
|
||||
SetStatus("已出库").
|
||||
SetNillableOutboundCategory(strPtr("supplier_return")).
|
||||
SetMaterialCode(inv.MaterialCode).
|
||||
SetManageMode(inv.ManageMode).
|
||||
SetNillableBatchNo(strPtr(inv.BatchNo)).
|
||||
SetNillableZoneCode(strPtr(inv.ZoneCode)).
|
||||
SetQuantity(inv.Quantity).
|
||||
SetNillableOperator(strPtr(operator)).
|
||||
SetNillableRemark(strPtr(remark)).
|
||||
Save(ctx0()); err != nil {
|
||||
return "", fmt.Errorf("创建退供应商出库单失败: %w", err)
|
||||
}
|
||||
if inv.ManageMode == 2 {
|
||||
// 电气件 SN:库存行置「出库」,出库明细记 SN
|
||||
if _, err = tx.Inventory.UpdateOneID(inv.ID).SetStatus("出库").Save(ctx0()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.OutboundDetail.Create().
|
||||
SetOutboundNo(outboundNo).SetSnCode(inv.SnCode).
|
||||
SetMaterialCode(inv.MaterialCode).SetQuantity(1).Save(ctx0()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
// 结构件批次 / 其他:扣减数量(含已锁定量),出库明细记批次
|
||||
if _, err = tx.Inventory.UpdateOneID(inv.ID).
|
||||
AddQuantity(-inv.Quantity).AddLockedQty(-min(inv.LockedQty, inv.Quantity)).Save(ctx0()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.OutboundDetail.Create().
|
||||
SetOutboundNo(outboundNo).SetNillableBatchNo(strPtr(inv.BatchNo)).
|
||||
SetMaterialCode(inv.MaterialCode).SetQuantity(inv.Quantity).Save(ctx0()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return outboundNo, nil
|
||||
}
|
||||
|
||||
// reinspectHandler 复检(换修闭环):针对「在库 + 不合格」的库存行重新判定,闭合换修 / 不合格处置的后续。
|
||||
// POST /api/inspection/reinspect
|
||||
// body: { inventoryId, result(pass/fail), disposalType(RETURN|SCRAP,仅 fail 时生效), returnTrackingNo, remark, operator }
|
||||
//
|
||||
// pass → 质量置「合格」,回可用(换修 / 隔离品修复后重新上线)
|
||||
// fail + RETURN → 质量置「待退」+ 自动退供应商出库(与 inventoryDisposeHandler 退货同闭环)
|
||||
// fail + SCRAP → 质量「不合格」+ 生命周期「报废」,退出可用
|
||||
//
|
||||
// 全程生成复检留痕检验记录(status 与结果一致),并写事件日志。
|
||||
func reinspectHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
InventoryID int `json:"inventoryId"`
|
||||
Result string `json:"result"`
|
||||
DisposalType string `json:"disposalType"`
|
||||
ReturnTrackingNo string `json:"returnTrackingNo"`
|
||||
Remark string `json:"remark"`
|
||||
Operator string `json:"operator"`
|
||||
}
|
||||
if err := parseJSON(r, &req); err != nil || req.InventoryID <= 0 {
|
||||
fail(w, http.StatusBadRequest, "参数错误:inventoryId 必填")
|
||||
return
|
||||
}
|
||||
if req.Result != "pass" && req.Result != "fail" {
|
||||
fail(w, http.StatusBadRequest, "复检结果必填(pass/fail)")
|
||||
return
|
||||
}
|
||||
inv, err := ctx.EntClient.Inventory.Get(ctx0(), req.InventoryID)
|
||||
if err != nil {
|
||||
fail(w, http.StatusNotFound, "库存行不存在")
|
||||
return
|
||||
}
|
||||
if inv.Status != "在库" {
|
||||
fail(w, http.StatusBadRequest, "仅「在库」库存行可复检,当前状态:"+inv.Status)
|
||||
return
|
||||
}
|
||||
if inv.QualityStatus != "不合格" {
|
||||
fail(w, http.StatusBadRequest, "仅「不合格」库存行可复检(合格/待退/未检无需复检),当前质量状态:"+inv.QualityStatus)
|
||||
return
|
||||
}
|
||||
if req.Result == "fail" {
|
||||
dt := disposalTypeOrNone(req.DisposalType)
|
||||
if dt != "RETURN" && dt != "SCRAP" {
|
||||
fail(w, http.StatusBadRequest, "复检不合格时必须选择处置方式(退货/报废)")
|
||||
return
|
||||
}
|
||||
if dt == "RETURN" && req.ReturnTrackingNo == "" {
|
||||
fail(w, http.StatusBadRequest, "退货处置必须填写退货运单号")
|
||||
return
|
||||
}
|
||||
}
|
||||
operator := r.Header.Get("X-Username")
|
||||
if operator == "" {
|
||||
operator = req.Operator
|
||||
}
|
||||
|
||||
tx, err := ctx.EntClient.Tx(ctx0())
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "开启事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
var recStatus, recDisposal, targetQuality string
|
||||
switch req.Result {
|
||||
case "pass":
|
||||
recStatus = "合格"
|
||||
recDisposal = ""
|
||||
targetQuality = "合格"
|
||||
case "fail":
|
||||
dt := disposalTypeOrNone(req.DisposalType)
|
||||
recStatus = "不合格"
|
||||
recDisposal = dt
|
||||
if dt == "RETURN" {
|
||||
targetQuality = "待退"
|
||||
} else {
|
||||
targetQuality = "不合格"
|
||||
}
|
||||
}
|
||||
if err = createReinspectTraceRecord(tx, inv, recStatus, recDisposal, req.Remark, operator); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "创建复检留痕记录失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
upd := tx.Inventory.UpdateOneID(inv.ID)
|
||||
switch targetQuality {
|
||||
case "合格":
|
||||
upd = upd.SetQualityStatus("合格")
|
||||
case "待退":
|
||||
upd = upd.SetQualityStatus("待退")
|
||||
default:
|
||||
upd = upd.SetQualityStatus("不合格")
|
||||
}
|
||||
if req.Result == "fail" && disposalTypeOrNone(req.DisposalType) == "SCRAP" {
|
||||
upd = upd.SetStatus("报废")
|
||||
}
|
||||
if _, err = upd.Save(ctx0()); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "更新库存状态失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
// 复检不合格转退货:自动退供应商出库(闭环)
|
||||
var outboundNo string
|
||||
if req.Result == "fail" && disposalTypeOrNone(req.DisposalType) == "RETURN" {
|
||||
outboundNo, err = createSupplierReturnOutbound(ctx, tx, inv, req.Remark, operator)
|
||||
if err != nil {
|
||||
fail(w, http.StatusInternalServerError, "自动退供应商出库失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "提交事务失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
|
||||
targetId := inv.BatchNo
|
||||
if inv.ManageMode == 2 {
|
||||
targetId = inv.SnCode
|
||||
}
|
||||
ctx.EventLog.Write(ctx0(), "inspection.reinspect", operator, "inventory", targetId,
|
||||
"复检 "+targetId+" → "+req.Result, map[string]any{"inventoryId": inv.ID, "result": req.Result, "outboundNo": outboundNo})
|
||||
ok(w, map[string]any{"inventoryId": inv.ID, "result": req.Result, "outboundNo": outboundNo})
|
||||
}
|
||||
}
|
||||
|
||||
// createReinspectTraceRecord 复检留痕:生成一条与复检结果一致的检验记录
|
||||
// (合格→合格记录;不合格→不合格记录并带处置方式),便于与来料检 / 在库处置记录区分。
|
||||
func createReinspectTraceRecord(tx *ent.Tx, inv *ent.Inventory, status, disposal, remark, operator string) error {
|
||||
targetType := "BATCH"
|
||||
targetId := inv.BatchNo
|
||||
qty := inv.Quantity
|
||||
if inv.ManageMode == 2 {
|
||||
targetType = "SN"
|
||||
targetId = inv.SnCode
|
||||
qty = 1
|
||||
}
|
||||
if qty <= 0 {
|
||||
qty = 1
|
||||
}
|
||||
passQty := 0
|
||||
rejectQty := qty
|
||||
if status == "合格" {
|
||||
passQty = qty
|
||||
rejectQty = 0
|
||||
}
|
||||
b := tx.InspectionRecord.Create().
|
||||
SetTargetType(targetType).
|
||||
SetTargetID(targetId).
|
||||
SetMaterialCode(inv.MaterialCode).
|
||||
SetNillableMaterialName(strPtr(inv.MaterialName)).
|
||||
SetStatus(status).
|
||||
SetInspectQty(qty).
|
||||
SetPassQty(passQty).
|
||||
SetRejectQty(rejectQty).
|
||||
SetNillableInspector(strPtr(operator)).
|
||||
SetNillableRemark(strPtr("复检留痕 " + remark))
|
||||
if disposal != "" {
|
||||
b = b.SetNillableDisposalType(strPtr(disposal))
|
||||
}
|
||||
_, err := b.Save(ctx0())
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user