443 lines
15 KiB
Go
443 lines
15 KiB
Go
package purchasemanage
|
||
|
||
import (
|
||
"errors"
|
||
"github.com/gin-gonic/gin"
|
||
model "go-admin/app/admin/models"
|
||
orm "go-admin/common/global"
|
||
"go-admin/logger"
|
||
"go-admin/tools"
|
||
"go-admin/tools/app"
|
||
"net/http"
|
||
)
|
||
|
||
// ErpPurchaseCreate 新建采购订单
|
||
// @Summary 新建采购订单
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseCreateReq true "新建采购订单模型"
|
||
// @Success 200 {object} models.ErpPurchaseOrder
|
||
// @Router /api/v1/erp_purchase/create [post]
|
||
func ErpPurchaseCreate(c *gin.Context) {
|
||
req := new(model.ErpPurchaseCreateReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误"+err.Error())
|
||
return
|
||
}
|
||
sysUser, err := model.GetSysUserByCtx(c)
|
||
if err != nil {
|
||
logger.Error("sys user err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
||
return
|
||
}
|
||
//if sysUser.StoreId == 0 {
|
||
// logger.Error("sys user store id null")
|
||
// app.Error(c, http.StatusBadRequest, errors.New("para err"), "sys user store id null")
|
||
// return
|
||
//}
|
||
|
||
if len(req.ErpPurchaseCommodities) == 0 {
|
||
logger.Error("erp purchase commodities is nil")
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
||
return
|
||
}
|
||
|
||
purchaseOrder, err := model.CreateErpPurchaseOrder(req, sysUser)
|
||
if err != nil {
|
||
logger.Error("CreateErpPurchaseOrder err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "新增失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
purchaseOrder.Commodities = req.ErpPurchaseCommodities
|
||
|
||
app.OK(c, purchaseOrder, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseEdit 编辑采购订单
|
||
// @Summary 编辑采购订单
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseEditReq true "编辑采购订单模型"
|
||
// @Success 200 {object} models.ErpPurchaseOrder
|
||
// @Router /api/v1/erp_purchase/edit [post]
|
||
func ErpPurchaseEdit(c *gin.Context) {
|
||
req := new(model.ErpPurchaseEditReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误"+err.Error())
|
||
return
|
||
}
|
||
|
||
sysUser, err := model.GetSysUserByCtx(c)
|
||
if err != nil {
|
||
logger.Error("sys user err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
||
return
|
||
}
|
||
//if sysUser.StoreId == 0 {
|
||
// logger.Error("sys user store id null")
|
||
// app.Error(c, http.StatusBadRequest, errors.New("para err"), "sys user store id null")
|
||
// return
|
||
//}
|
||
|
||
if len(req.ErpPurchaseCommodities) == 0 {
|
||
logger.Error("erp purchase commodities is nil")
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
||
return
|
||
}
|
||
|
||
// 更新订单信息
|
||
purchaseOrder, err := model.EditErpPurchaseOrder(req, sysUser)
|
||
if err != nil {
|
||
logger.Error("EditErpPurchaseOrder err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "编辑失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
purchaseOrder.Commodities = req.ErpPurchaseCommodities
|
||
app.OK(c, purchaseOrder, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseList 查询采购订单列表
|
||
// @Summary 查询采购订单列表
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseOrderListReq true "查询采购订单列表模型"
|
||
// @Success 200 {object} models.ErpPurchaseOrderListResp
|
||
// @Router /api/v1/erp_purchase/list [post]
|
||
func ErpPurchaseList(c *gin.Context) {
|
||
req := &model.ErpPurchaseOrderListReq{}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||
return
|
||
}
|
||
|
||
resp, err := req.List()
|
||
if err != nil {
|
||
logger.Error("ErpPurchaseList err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "获取失败"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, resp, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseDetail 查询采购订单详情
|
||
// @Summary 查询采购订单详情
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseDetailReq true "查询采购订单详情模型"
|
||
// @Success 200 {object} models.ErpPurchaseOrder
|
||
// @Router /api/v1/erp_purchase/detail [post]
|
||
func ErpPurchaseDetail(c *gin.Context) {
|
||
req := new(model.ErpPurchaseDetailReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||
return
|
||
}
|
||
|
||
var purchaseOrder model.ErpPurchaseOrder
|
||
err := orm.Eloquent.Table("erp_purchase_order").Where("id=?", req.ErpPurchaseOrderId).Find(&purchaseOrder).Error
|
||
if err != nil {
|
||
logger.Error("purchase order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "获取失败")
|
||
return
|
||
}
|
||
var purchaseCommodities []model.ErpPurchaseCommodity
|
||
err = orm.Eloquent.Table("erp_purchase_commodity").Where("erp_purchase_order_id=?", req.ErpPurchaseOrderId).
|
||
Find(&purchaseCommodities).Error
|
||
if err != nil {
|
||
logger.Error("purchase commodities err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "获取失败")
|
||
return
|
||
}
|
||
purchaseOrder.Commodities = purchaseCommodities
|
||
|
||
// todo 需要添加实际采购入库信息
|
||
app.OK(c, purchaseOrder, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseAudit 审核采购订单
|
||
// @Summary 审核采购订单
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseAuditReq true "审核采购订单模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/erp_purchase/audit [post]
|
||
func ErpPurchaseAudit(c *gin.Context) {
|
||
var req = new(model.ErpPurchaseAuditReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
// todo 需要校验当前用户是否有权限
|
||
|
||
var erpPurchaseOrder model.ErpPurchaseOrder
|
||
err = orm.Eloquent.Table("erp_purchase_order").Where("serial_number = ?", req.SerialNumber).Find(&erpPurchaseOrder).Error
|
||
if err != nil {
|
||
logger.Error("order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
// 判断入参state:1-审核,2-取消审核
|
||
orderState := 0
|
||
switch req.State {
|
||
case 1: // 1-审核:待审核状态可以审核
|
||
if erpPurchaseOrder.State == model.ErpPurchaseOrderUnAudit {
|
||
if erpPurchaseOrder.PurchaseType == model.ErpProcureOrder { // 采购
|
||
orderState = model.ErpPurchaseOrderWaitInventory
|
||
} else if erpPurchaseOrder.PurchaseType == model.ErpRejectOrder { // 退货
|
||
orderState = model.ErpPurchaseOrderWaitReject
|
||
} else {
|
||
logger.Error("order err, PurchaseType is:", logger.Field("PurchaseType", erpPurchaseOrder.PurchaseType))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:采购类型有误")
|
||
return
|
||
}
|
||
} else {
|
||
app.OK(c, nil, "订单已审核")
|
||
return
|
||
}
|
||
case 2: // 2-取消审核:2/3可以取消审核
|
||
if erpPurchaseOrder.State == model.ErpPurchaseOrderWaitInventory || erpPurchaseOrder.State == model.ErpPurchaseOrderWaitReject {
|
||
orderState = model.ErpPurchaseOrderUnAudit
|
||
} else if erpPurchaseOrder.State == model.ErpPurchaseOrderUnAudit {
|
||
app.OK(c, nil, "订单已取消审核")
|
||
return
|
||
} else {
|
||
logger.Error("order err, erpPurchaseOrder.State is:", logger.Field("erpPurchaseOrder.State", erpPurchaseOrder.State))
|
||
app.Error(c, http.StatusInternalServerError, err, "取消审核失败:[入库/退货中、已终止、已完成]订单不能取消")
|
||
return
|
||
}
|
||
default:
|
||
logger.Error("order err, req.State is:", logger.Field("req.State", req.State))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:参数有误")
|
||
return
|
||
}
|
||
|
||
err = orm.Eloquent.Table("erp_purchase_order").Where("id = ?", erpPurchaseOrder.ID).Updates(map[string]interface{}{
|
||
"state": orderState,
|
||
}).Error
|
||
if err != nil {
|
||
logger.Error("update erp_purchase_order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "操作成功")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseDelete 删除采购订单
|
||
// @Summary 删除采购订单
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseTerminateReq true "删除采购订单模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/erp_purchase/delete [post]
|
||
func ErpPurchaseDelete(c *gin.Context) {
|
||
var req = new(model.ErpPurchaseTerminateReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
// todo 需要校验当前用户是否有权限
|
||
|
||
var erpPurchaseOrder model.ErpPurchaseOrder
|
||
err = orm.Eloquent.Table("erp_purchase_order").Where("serial_number = ?", req.SerialNumber).Find(&erpPurchaseOrder).Error
|
||
if err != nil {
|
||
logger.Error("order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
if erpPurchaseOrder.SerialNumber == "" {
|
||
logger.Error("order is null")
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:订单不存在")
|
||
return
|
||
}
|
||
|
||
// 仅待审核订单可删除
|
||
if erpPurchaseOrder.State != model.ErpPurchaseOrderUnAudit {
|
||
logger.Error("order err, erpPurchaseOrder.State is:", logger.Field("erpPurchaseOrder.State", erpPurchaseOrder.State))
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:仅待审核订单可删除")
|
||
return
|
||
}
|
||
|
||
begin := orm.Eloquent.Begin()
|
||
// 1-删除采购订单表
|
||
err = begin.Delete(erpPurchaseOrder).Error
|
||
if err != nil {
|
||
logger.Error("order delete1 err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
// 2-删除采购订单商品表
|
||
var commodities []model.ErpPurchaseCommodity
|
||
err = orm.Eloquent.Table("erp_purchase_commodity").Where("erp_purchase_order_id = ?", erpPurchaseOrder.ID).Find(&commodities).Error
|
||
if err != nil {
|
||
logger.Error("query erp_purchase_commodity err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
if len(commodities) != 0 {
|
||
err = begin.Delete(&commodities).Error
|
||
if err != nil {
|
||
logger.Error("更新商品订单信息-删除 error")
|
||
app.Error(c, http.StatusInternalServerError, err, "删除失败:"+err.Error())
|
||
return
|
||
}
|
||
}
|
||
|
||
err = begin.Commit().Error
|
||
if err != nil {
|
||
begin.Rollback()
|
||
logger.Error("commit err:", logger.Field("err", err))
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "删除成功")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseInventory 入库(退货)
|
||
// @Summary 入库(退货)
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseInventoryReq true "入库(退货)模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/erp_purchase/inventory [post]
|
||
func ErpPurchaseInventory(c *gin.Context) {
|
||
req := new(model.ErpPurchaseInventoryReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||
return
|
||
}
|
||
|
||
err := model.InventoryErpPurchase(req)
|
||
if err != nil {
|
||
logger.Error("InventoryErpPurchase err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseTerminate 终止采购
|
||
// @Summary 终止采购
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseTerminateReq true "终止采购模型"
|
||
// @Success 200 {object} app.Response
|
||
// @Router /api/v1/erp_purchase/terminate [post]
|
||
func ErpPurchaseTerminate(c *gin.Context) {
|
||
var req = new(model.ErpPurchaseTerminateReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, err, "参数错误:"+err.Error())
|
||
return
|
||
}
|
||
|
||
err := tools.Validate(req) //必填参数校验
|
||
if err != nil {
|
||
app.Error(c, http.StatusBadRequest, err, err.Error())
|
||
return
|
||
}
|
||
|
||
// todo 需要校验当前用户是否有权限
|
||
|
||
var erpPurchaseOrder model.ErpPurchaseOrder
|
||
err = orm.Eloquent.Table("erp_purchase_order").Where("serial_number = ?", req.SerialNumber).Find(&erpPurchaseOrder).Error
|
||
if err != nil {
|
||
logger.Error("order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
// 仅待入库、待退货订单可以终止
|
||
orderState := 0
|
||
if erpPurchaseOrder.State == model.ErpPurchaseOrderWaitInventory || erpPurchaseOrder.State == model.ErpPurchaseOrderWaitReject {
|
||
orderState = model.ErpPurchaseOrderEnd
|
||
} else {
|
||
logger.Error("order err, erpPurchaseOrder.State is:", logger.Field("erpPurchaseOrder.State", erpPurchaseOrder.State))
|
||
app.Error(c, http.StatusInternalServerError, err, "终止失败:仅待入库、待退货订单可以终止")
|
||
return
|
||
}
|
||
|
||
err = orm.Eloquent.Table("erp_purchase_order").Where("id = ?", erpPurchaseOrder.ID).Updates(map[string]interface{}{
|
||
"state": orderState,
|
||
}).Error
|
||
if err != nil {
|
||
logger.Error("update erp_purchase_order err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "审核失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, nil, "")
|
||
return
|
||
}
|
||
|
||
// ErpPurchaseExecute 执行(入库/退货)
|
||
// @Summary 执行(入库/退货)
|
||
// @Tags 采购管理, V1.3.0
|
||
// @Produce json
|
||
// @Accept json
|
||
// @Param request body models.ErpPurchaseInventoryReq true "执行(入库/退货)模型"
|
||
// @Success 200 {object} models.ErpPurchaseExecuteResp
|
||
// @Router /api/v1/erp_purchase/execute [post]
|
||
func ErpPurchaseExecute(c *gin.Context) {
|
||
req := new(model.ErpPurchaseInventoryReq)
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
logger.Error("ShouldBindJSON err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||
return
|
||
}
|
||
|
||
resp, err := model.ExecuteErpPurchase(req)
|
||
if err != nil {
|
||
logger.Error("InventoryErpPurchase err:", logger.Field("err", err))
|
||
app.Error(c, http.StatusInternalServerError, err, "操作失败:"+err.Error())
|
||
return
|
||
}
|
||
|
||
app.OK(c, resp, "")
|
||
return
|
||
}
|