436 lines
14 KiB
Go
436 lines
14 KiB
Go
package purchasemanage
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/codinl/go-logger"
|
|
"github.com/gin-gonic/gin"
|
|
model "go-admin/app/admin/models"
|
|
orm "go-admin/common/global"
|
|
"go-admin/tools/app"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func ErpPurchaseOrderCreate(c *gin.Context) {
|
|
req := struct {
|
|
//StoreId uint32 `json:"store_id"`
|
|
ErpCashierId uint32 `json:"erp_cashier_id" binding:"required"`
|
|
StoreId uint32 `json:"store_id" binding:"required"`
|
|
ErpSupplierId uint32 `json:"erp_supplier_id" binding:"required"`
|
|
DeliveryTime string `json:"delivery_time" binding:"required"`
|
|
Remark string `json:"remark"`
|
|
DeliveryAddress string `json:"delivery_address" binding:"required"`
|
|
PurchaseType string `json:"purchase_type" binding:"required"`
|
|
ErpPurchaseCommodities []model.ErpPurchaseCommodity `json:"erp_purchase_commodities" binding:"required"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
sysUser, err := model.GetSysUserByCtx(c)
|
|
if err != nil {
|
|
logger.Error("sys user 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"), "参数错误")
|
|
return
|
|
}
|
|
|
|
if len(req.ErpPurchaseCommodities) == 0 {
|
|
logger.Error("erp purchase commodities is nil")
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
nowTime := time.Now()
|
|
purchaseOrder := &model.ErpPurchaseOrder{
|
|
SerialNumber: "cgr" + model.NewErpPurchaseSn(),
|
|
PurchaseType: req.PurchaseType,
|
|
StoreId: req.StoreId,
|
|
ErpSupplierId: req.ErpSupplierId,
|
|
MakerTime: nowTime,
|
|
MakerId: uint32(sysUser.UserId),
|
|
MakerName: sysUser.NickName,
|
|
State: 1,
|
|
}
|
|
err = purchaseOrder.IdInit()
|
|
if err != nil {
|
|
logger.Error("info err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
err = model.ErpPurchaseCommodityListPerfectInfo(req.ErpPurchaseCommodities)
|
|
if err != nil {
|
|
logger.Error("info err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
begin := orm.Eloquent.Begin()
|
|
err = begin.Create(purchaseOrder).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create purchase order err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
for i, _ := range req.ErpPurchaseCommodities {
|
|
req.ErpPurchaseCommodities[i].ErpPurchaseOrderId = purchaseOrder.ID
|
|
req.ErpPurchaseCommodities[i].InventoryCount = 0
|
|
err = begin.Create(&req.ErpPurchaseCommodities[i]).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create purchase commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
}
|
|
err = begin.Commit().Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("commit purchase commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
purchaseOrder.Commodities = req.ErpPurchaseCommodities
|
|
|
|
app.OK(c, purchaseOrder, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseOrderList(c *gin.Context) {
|
|
req := &model.ErpPurchaseOrderListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
|
|
resp, err := req.List()
|
|
if err != nil {
|
|
logger.Error("erp commodity list err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, resp, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseOrderRejectedCreate(c *gin.Context) {
|
|
req := struct {
|
|
//StoreId uint32 `json:"store_id"`
|
|
ErpCashierId uint32 `json:"erp_cashier_id" binding:"required"`
|
|
MakerId uint32 `json:"maker_id" binding:"required"`
|
|
PurchaseOrderSn string `json:"purchase_order_sn" binding:"required"`
|
|
PurchaseType string `json:"purchase_type" binding:"required"`
|
|
Remark string `json:"remark"`
|
|
ErpPurchaseCommodities []model.ErpPurchaseCommodity `json:"erp_purchase_commodities" binding:"required"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
//sysUser, err := model.GetSysUserByCtx(c)
|
|
//if err != nil {
|
|
// logger.Error("sys user 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"), "参数错误")
|
|
// return
|
|
//}
|
|
|
|
if len(req.ErpPurchaseCommodities) == 0 {
|
|
logger.Error("erp purchase commodities is nil")
|
|
app.Error(c, http.StatusInternalServerError, errors.New("commodities is nil"), "操作失败")
|
|
return
|
|
}
|
|
var erpPurchaseOrder model.ErpPurchaseOrder
|
|
err := orm.Eloquent.Table("erp_purchase_order").Where("serial_number=?", req.PurchaseOrderSn).Find(&erpPurchaseOrder).Error
|
|
if err != nil {
|
|
logger.Error("purchase order err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
nowTime := time.Now()
|
|
purchaseOrder := &model.ErpPurchaseOrder{
|
|
SerialNumber: "cgt" + model.NewErpPurchaseSn(),
|
|
PurchaseType: req.PurchaseType,
|
|
StoreId: erpPurchaseOrder.StoreId,
|
|
ErpSupplierId: erpPurchaseOrder.ErpSupplierId,
|
|
MakerTime: nowTime,
|
|
MakerId: uint32(req.MakerId),
|
|
//MakerName: sysUser.NickName,
|
|
State: 1,
|
|
}
|
|
err = purchaseOrder.IdInit()
|
|
if err != nil {
|
|
logger.Error("info err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
err = model.ErpPurchaseCommodityListPerfectInfo(req.ErpPurchaseCommodities)
|
|
if err != nil {
|
|
logger.Error("info err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
begin := orm.Eloquent.Begin()
|
|
err = begin.Create(purchaseOrder).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create purchase order err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
for i, _ := range req.ErpPurchaseCommodities {
|
|
req.ErpPurchaseCommodities[i].ErpPurchaseOrderId = purchaseOrder.ID
|
|
err = begin.Create(&req.ErpPurchaseCommodities[i]).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create purchase commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
}
|
|
err = begin.Commit().Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("commit purchase commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
purchaseOrder.Commodities = req.ErpPurchaseCommodities
|
|
|
|
app.OK(c, purchaseOrder, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseInventoryCreate(c *gin.Context) {
|
|
req := struct {
|
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" binding:"required"` //
|
|
InventoryType string `json:"inventory_type" binding:"required"`
|
|
Inventories []model.ErpPurchaseInventory `json:"inventories" binding:"required"`
|
|
Commodities []model.ErpInventoryCommodity `json:"commodities" binding:"required"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
//sysUser, err := model.GetSysUserByCtx(c)
|
|
//if err != nil {
|
|
// logger.Error("sys user 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"), "参数错误")
|
|
// return
|
|
//}
|
|
|
|
err := model.ErpPurchaseInventoryListIdInit(req.Inventories, req.Commodities, req.ErpPurchaseOrderId, req.InventoryType)
|
|
if err != nil {
|
|
logger.Error("inventory list id init err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
|
|
begin := orm.Eloquent.Begin()
|
|
for _, v := range req.Inventories {
|
|
err = begin.Exec(fmt.Sprintf(
|
|
"UPDATE erp_purchase_commodity SET inventory_count=%d WHERE erp_purchase_order_id=%d AND erp_commodity_id=%d",
|
|
v.InventoryCount, v.ErpPurchaseOrderId, v.ErpCommodityId)).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("update inventory count err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
err = begin.Create(&v).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create erp inventory commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
}
|
|
|
|
for i, _ := range req.Commodities {
|
|
err = begin.Create(&req.Commodities[i]).Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("create erp inventory commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
}
|
|
err = begin.Commit().Error
|
|
if err != nil {
|
|
begin.Rollback()
|
|
logger.Error("commit err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
|
return
|
|
}
|
|
app.OK(c, nil, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseOrderDetail(c *gin.Context) {
|
|
req := struct {
|
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" binding:"required"` //
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
//sysUser, err := model.GetSysUserByCtx(c)
|
|
//if err != nil {
|
|
// logger.Error("sys user 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"), "参数错误")
|
|
// return
|
|
//}
|
|
var purchaseOrder model.ErpPurchaseOrder
|
|
err := orm.Eloquent.Table("").Where("id=?", req.ErpPurchaseOrderId).Find(&purchaseOrder).Error
|
|
if err != nil {
|
|
logger.Error("purchase order 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:", err)
|
|
app.Error(c, http.StatusBadRequest, err, "获取失败")
|
|
return
|
|
}
|
|
purchaseOrder.Commodities = purchaseCommodities
|
|
app.OK(c, purchaseOrder, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseQuotationList(c *gin.Context) {
|
|
req := &model.ErpPurchaseQuotationListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
|
|
resp, err := req.List()
|
|
if err != nil {
|
|
logger.Error("erp commodity list err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
|
|
app.OK(c, resp, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseCommodityManipulateList(c *gin.Context) {
|
|
req := &struct {
|
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" binding:"required"` //
|
|
Inventories []model.ErpPurchaseInventory `json:"inventories" binding:"required"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
if len(req.Inventories) == 0 {
|
|
logger.Error("inventories err")
|
|
app.Error(c, http.StatusInternalServerError, errors.New("inventories err"), "获取失败")
|
|
return
|
|
}
|
|
commodityIds := make([]uint32, 0, len(req.Inventories))
|
|
for i, _ := range req.Inventories {
|
|
commodityIds = append(commodityIds, req.Inventories[i].ErpCommodityId)
|
|
}
|
|
commodityMap, err := model.GetErpPurchaseCommodityMap(commodityIds, req.ErpPurchaseOrderId)
|
|
if err != nil {
|
|
logger.Error("erp purchase commodity map err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
commodities := make([]model.ErpInventoryCommodity, 0)
|
|
count := uint32(0)
|
|
amount := uint32(0)
|
|
for _, inventory := range req.Inventories {
|
|
v, ok := commodityMap[inventory.ErpCommodityId]
|
|
if !ok || inventory.Count == 0 {
|
|
continue
|
|
}
|
|
commodity := model.ErpInventoryCommodity{
|
|
ErpPurchaseOrderId: req.ErpPurchaseOrderId,
|
|
ErpCommodityId: v.ErpCommodityId,
|
|
ErpCommodityName: v.ErpCommodityName,
|
|
CommoditySerialNumber: v.CommoditySerialNumber,
|
|
IMEIType: v.IMEIType,
|
|
Price: v.Price,
|
|
EmployeePrice: inventory.EmployeePrice,
|
|
ImplementationPrice: inventory.ImplementationPrice,
|
|
}
|
|
if inventory.IMEIType == 1 {
|
|
commodity.Count = inventory.Count
|
|
commodities = append(commodities, commodity)
|
|
} else {
|
|
if inventory.Count == 0 {
|
|
continue
|
|
}
|
|
for i := 0; i < int(inventory.Count); i++ {
|
|
commodity.Count = 1
|
|
commodities = append(commodities, commodity)
|
|
}
|
|
}
|
|
count += inventory.Count
|
|
amount += inventory.Count * inventory.EmployeePrice
|
|
}
|
|
rsp := &map[string]interface{}{
|
|
"list": commodities,
|
|
"count": count,
|
|
"amount": amount,
|
|
}
|
|
app.OK(c, rsp, "")
|
|
return
|
|
}
|
|
|
|
func ErpPurchaseOrderCommodityList(c *gin.Context) {
|
|
req := &struct {
|
|
ErpPurchaseOrderId uint32 `json:"erp_purchase_order_id" binding:"required"` //
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
|
return
|
|
}
|
|
var commodities []model.ErpPurchaseCommodity
|
|
err := orm.Eloquent.Table("erp_purchase_commodity").Where("erp_purchase_order_id=?", req.ErpPurchaseOrderId).Find(&commodities).Error
|
|
if err != nil {
|
|
logger.Error("erp purchase commodity err:", err)
|
|
app.Error(c, http.StatusInternalServerError, err, "获取失败")
|
|
return
|
|
}
|
|
app.OK(c, commodities, "")
|
|
return
|
|
}
|