1.优化进销存报表,新增系统入库字段;
This commit is contained in:
parent
4537477eff
commit
2033896d43
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
.idea
|
.idea
|
||||||
./logs/*
|
./logs/*
|
||||||
|
./temp/*
|
||||||
dev-go-admin
|
dev-go-admin
|
||||||
main.exe
|
main.exe
|
|
@ -2794,7 +2794,9 @@ func SetStockCommodityState(c *gin.Context, id uint32) error { //更新库存状
|
||||||
|
|
||||||
// 更新商品库存详情表状态为:系统出库
|
// 更新商品库存详情表状态为:系统出库
|
||||||
if err := begin.Model(&ErpStockCommodity{}).Where("id=?", id).Updates(map[string]interface{}{
|
if err := begin.Model(&ErpStockCommodity{}).Where("id=?", id).Updates(map[string]interface{}{
|
||||||
"state": SystemOut}).Error; err != nil {
|
"state": SystemOut,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
}).Error; err != nil {
|
||||||
return fmt.Errorf("[update err]:%v", err)
|
return fmt.Errorf("[update err]:%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ type DecisionSumData struct {
|
||||||
TotalAllotOut uint32 `json:"total_allot_out"` // 调拨出库
|
TotalAllotOut uint32 `json:"total_allot_out"` // 调拨出库
|
||||||
TotalAllotWaitOut uint32 `json:"total_allot_wait_out"` // 在途库存(出库)
|
TotalAllotWaitOut uint32 `json:"total_allot_wait_out"` // 在途库存(出库)
|
||||||
TotalProductIn uint32 `json:"total_product_in"` // 产品入库
|
TotalProductIn uint32 `json:"total_product_in"` // 产品入库
|
||||||
|
TotalSystemIn uint32 `json:"total_system_in"` // 系统入库
|
||||||
TotalSystemOut uint32 `json:"total_system_out"` // 系统出库
|
TotalSystemOut uint32 `json:"total_system_out"` // 系统出库
|
||||||
TotalCheckIn uint32 `json:"total_check_in"` // 盘点入库
|
TotalCheckIn uint32 `json:"total_check_in"` // 盘点入库
|
||||||
TotalCheckOut uint32 `json:"total_check_out"` // 盘点出库
|
TotalCheckOut uint32 `json:"total_check_out"` // 盘点出库
|
||||||
|
@ -75,6 +76,7 @@ type DecisionReportData struct {
|
||||||
AllotOut uint32 `json:"allot_out"` // 调拨出库
|
AllotOut uint32 `json:"allot_out"` // 调拨出库
|
||||||
AllotWaitOut uint32 `json:"allot_wait_out"` // 在途库存(出库)
|
AllotWaitOut uint32 `json:"allot_wait_out"` // 在途库存(出库)
|
||||||
ProductIn uint32 `json:"product_in"` // 产品入库
|
ProductIn uint32 `json:"product_in"` // 产品入库
|
||||||
|
SystemIn uint32 `json:"system_in"` // 系统入库
|
||||||
SystemOut uint32 `json:"system_out"` // 系统出库
|
SystemOut uint32 `json:"system_out"` // 系统出库
|
||||||
CheckIn uint32 `json:"check_in"` // 盘点入库
|
CheckIn uint32 `json:"check_in"` // 盘点入库
|
||||||
CheckOut uint32 `json:"check_out"` // 盘点出库
|
CheckOut uint32 `json:"check_out"` // 盘点出库
|
||||||
|
@ -222,71 +224,6 @@ func (m *ErpDecisionReportReq) DecisionReportList(c *gin.Context) (*ErpDecisionR
|
||||||
go func(item ErpStock) {
|
go func(item ErpStock) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
//var reportData DecisionReportData
|
|
||||||
//reportData.CommodityId = item.ErpCommodityId
|
|
||||||
//reportData.CommodityName = item.ErpCommodityName
|
|
||||||
//reportData.CommoditySerialNumber = item.CommoditySerialNumber
|
|
||||||
//reportData.CategoryID = item.ErpCategoryId
|
|
||||||
//reportData.CategoryName = item.ErpCategoryName
|
|
||||||
//
|
|
||||||
//// 查询期初库存、期初金额
|
|
||||||
//systemStartData, _ := getSystemStartCount(m, item)
|
|
||||||
//reportData.BeginStock = systemStartData.BeginStock
|
|
||||||
//reportData.BeginAmount = systemStartData.BeginAmount
|
|
||||||
//
|
|
||||||
//// 查询采购进货数量
|
|
||||||
//purchaseStockData, _ := getPurchaseStockCount(m, item)
|
|
||||||
//reportData.PurchaseStock = purchaseStockData.PurchaseStock
|
|
||||||
//
|
|
||||||
//// 查询采购退货数量
|
|
||||||
//purchaseReturnData, _ := getPurchaseReturnCount(m, item)
|
|
||||||
//reportData.PurchaseReturn = purchaseReturnData.PurchaseReturn
|
|
||||||
//
|
|
||||||
//// 查询零售销售数量
|
|
||||||
//saleOutData, _ := getSaleOutCount(m, item)
|
|
||||||
//reportData.OrderSale = saleOutData.OrderSale
|
|
||||||
//
|
|
||||||
//// 查询零售退货数量
|
|
||||||
//saleReturnData, _ := getSaleReturnCount(m, item)
|
|
||||||
//reportData.OrderReject = saleReturnData.OrderReject
|
|
||||||
//
|
|
||||||
//// 查询调拨入库数量
|
|
||||||
//allotInData, _ := getAllotInCount(m, item)
|
|
||||||
//reportData.AllotIn = allotInData.AllotIn
|
|
||||||
//
|
|
||||||
//// 查询在途库存(入库)数量
|
|
||||||
//allotWaitInData, _ := getAllotWaitInCount(m, item)
|
|
||||||
//reportData.AllotWaitIn = allotWaitInData.AllotWaitIn
|
|
||||||
//
|
|
||||||
//// 查询调拨出库数量
|
|
||||||
//allotOutData, _ := getAllotOutCount(m, item)
|
|
||||||
//reportData.AllotOut = allotOutData.AllotOut
|
|
||||||
//
|
|
||||||
//// 查询在途库存(出库)数量
|
|
||||||
//allotWaitOutData, _ := getAllotWaitOutCount(m, item)
|
|
||||||
//reportData.AllotWaitOut = allotWaitOutData.AllotWaitOut
|
|
||||||
//
|
|
||||||
//// 查询产品入库数量:产品入库
|
|
||||||
//productData, _ := getProductCount(m, item)
|
|
||||||
//reportData.ProductIn = productData.ProductIn
|
|
||||||
//
|
|
||||||
//// 查询系统出库数据:系统出库
|
|
||||||
//systemOutData, _ := getSystemOutCount(m, item)
|
|
||||||
//reportData.SystemOut = systemOutData.SystemOut
|
|
||||||
//
|
|
||||||
//// 查询盘点入库数量
|
|
||||||
//changeAddData, _ := getChangeAddCount(m, item)
|
|
||||||
//reportData.CheckIn = changeAddData.CheckIn
|
|
||||||
//
|
|
||||||
//// 查询盘点出库数量
|
|
||||||
//changeReduceData, _ := getChangeReduceCount(m, item)
|
|
||||||
//reportData.CheckOut = changeReduceData.CheckOut
|
|
||||||
//
|
|
||||||
//// 查询期末库存、期末金额
|
|
||||||
//systemEndData, _ := getSystemEndCount(m, item)
|
|
||||||
//reportData.EndStock = systemEndData.EndStock
|
|
||||||
//reportData.EndAmount = systemEndData.EndAmount
|
|
||||||
|
|
||||||
reportData, _ := calculateInventoryReport(m, item)
|
reportData, _ := calculateInventoryReport(m, item)
|
||||||
reportData.CommodityId = item.ErpCommodityId
|
reportData.CommodityId = item.ErpCommodityId
|
||||||
reportData.CommodityName = item.ErpCommodityName
|
reportData.CommodityName = item.ErpCommodityName
|
||||||
|
@ -333,28 +270,9 @@ func (m *ErpDecisionReportReq) DecisionReportList(c *gin.Context) (*ErpDecisionR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//var sumData DecisionSumData
|
|
||||||
for _, data := range commodityMap {
|
for _, data := range commodityMap {
|
||||||
//sumData.TotalBeginStock += data.BeginStock
|
|
||||||
//sumData.TotalBeginAmount += data.BeginAmount
|
|
||||||
//sumData.TotalPurchaseStock += data.PurchaseStock
|
|
||||||
//sumData.TotalPurchaseReturn += data.PurchaseReturn
|
|
||||||
//sumData.TotalOrderSale += data.OrderSale
|
|
||||||
//sumData.TotalOrderReject += data.OrderReject
|
|
||||||
//sumData.TotalAllotIn += data.AllotIn
|
|
||||||
//sumData.TotalAllotWaitIn += data.AllotWaitIn
|
|
||||||
//sumData.TotalAllotOut += data.AllotOut
|
|
||||||
//sumData.TotalAllotWaitOut += data.AllotWaitOut
|
|
||||||
//sumData.TotalProductIn += data.ProductIn
|
|
||||||
//sumData.TotalSystemOut += data.SystemOut
|
|
||||||
//sumData.TotalCheckIn += data.CheckIn
|
|
||||||
//sumData.TotalCheckOut += data.CheckOut
|
|
||||||
//sumData.TotalEndStock += data.EndStock
|
|
||||||
//sumData.TotalEndAmount += data.EndAmount
|
|
||||||
|
|
||||||
reportList = append(reportList, data)
|
reportList = append(reportList, data)
|
||||||
}
|
}
|
||||||
//resp.SumData = sumData
|
|
||||||
|
|
||||||
// 排序规则:商品编号小
|
// 排序规则:商品编号小
|
||||||
SortReportByDecisionCommodities(reportList)
|
SortReportByDecisionCommodities(reportList)
|
||||||
|
@ -463,6 +381,12 @@ func calculateInventoryReport(m *ErpDecisionReportReq, item ErpStock) (DecisionR
|
||||||
}
|
}
|
||||||
reportData.ProductIn = productData.ProductIn
|
reportData.ProductIn = productData.ProductIn
|
||||||
|
|
||||||
|
systemInData, err := getSystemInCount(m, item)
|
||||||
|
if err != nil {
|
||||||
|
return DecisionReportData{}, err
|
||||||
|
}
|
||||||
|
reportData.SystemIn = systemInData.SystemIn
|
||||||
|
|
||||||
systemOutData, err := getSystemOutCount(m, item)
|
systemOutData, err := getSystemOutCount(m, item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DecisionReportData{}, err
|
return DecisionReportData{}, err
|
||||||
|
@ -481,13 +405,18 @@ func calculateInventoryReport(m *ErpDecisionReportReq, item ErpStock) (DecisionR
|
||||||
}
|
}
|
||||||
reportData.CheckOut = changeReduceData.CheckOut
|
reportData.CheckOut = changeReduceData.CheckOut
|
||||||
|
|
||||||
// Step 4: 计算调整库存逻辑(期初+入库-出库)
|
if m.EndTime != "" {
|
||||||
reportData.EndStock = reportData.BeginStock + reportData.PurchaseStock + reportData.AllotIn + reportData.ProductIn + reportData.CheckIn
|
// Step 4: 计算调整库存逻辑(期初+入库-出库)
|
||||||
if reportData.EndStock < (reportData.PurchaseReturn + reportData.OrderSale + reportData.AllotOut + reportData.SystemOut + reportData.CheckOut) {
|
reportData.EndStock = reportData.BeginStock + reportData.PurchaseStock + reportData.AllotIn +
|
||||||
reportData.EndStock = 0
|
reportData.ProductIn + reportData.CheckIn + reportData.OrderReject + reportData.SystemIn
|
||||||
reportData.EndAmount = 0
|
if reportData.EndStock < (reportData.PurchaseReturn + reportData.OrderSale + reportData.AllotOut +
|
||||||
} else {
|
reportData.SystemOut + reportData.CheckOut) {
|
||||||
reportData.EndStock -= reportData.PurchaseReturn + reportData.OrderSale + reportData.AllotOut + reportData.SystemOut + reportData.CheckOut
|
reportData.EndStock = 0
|
||||||
|
reportData.EndAmount = 0
|
||||||
|
} else {
|
||||||
|
reportData.EndStock -= reportData.PurchaseReturn + reportData.OrderSale + reportData.AllotOut +
|
||||||
|
reportData.SystemOut + reportData.CheckOut
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reportData, nil
|
return reportData, nil
|
||||||
|
@ -504,7 +433,9 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算开始时间的前一天
|
// 计算开始时间的前一天
|
||||||
previousDay := startTime.AddDate(0, 0, -1)
|
//previousDay := startTime.AddDate(0, 0, -1)
|
||||||
|
previousDay := startTime.AddDate(0, 0, -1).Truncate(24 * time.Hour).
|
||||||
|
Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||||||
|
|
||||||
// 获取系统入库数量
|
// 获取系统入库数量
|
||||||
var systemStartData DecisionReportData
|
var systemStartData DecisionReportData
|
||||||
|
@ -528,15 +459,21 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep
|
||||||
|
|
||||||
// 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库)
|
// 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库)
|
||||||
var outStockData struct {
|
var outStockData struct {
|
||||||
SoldOut uint32 `json:"sold_out"`
|
SoldOut uint32 `json:"sold_out"`
|
||||||
SystemOut uint32 `json:"system_out"`
|
SoldOutAmount float64 `json:"sold_out_amount"`
|
||||||
CheckOut uint32 `json:"check_out"`
|
SystemOut uint32 `json:"system_out"`
|
||||||
|
SystemOutAmount float64 `json:"system_out_amount"`
|
||||||
|
CheckOut uint32 `json:"check_out"`
|
||||||
|
CheckOutAmount float64 `json:"check_out_amount"`
|
||||||
}
|
}
|
||||||
es := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
es := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS sold_out_amount, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out",
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS system_out_amount, "+
|
||||||
SoldOut, SystemOut, CheckOut).
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS check_out_amount",
|
||||||
|
SoldOut, SoldOut, SystemOut, SystemOut, CheckOut, CheckOut).
|
||||||
Where("updated_at <= ?", previousDay).
|
Where("updated_at <= ?", previousDay).
|
||||||
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut}).
|
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut}).
|
||||||
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
||||||
|
@ -556,15 +493,21 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep
|
||||||
|
|
||||||
// 获取开始时间前一天的所有入库数量(包括采购入库、产品入库和盘点入库)
|
// 获取开始时间前一天的所有入库数量(包括采购入库、产品入库和盘点入库)
|
||||||
var inStockData struct {
|
var inStockData struct {
|
||||||
PurchaseIn uint32 `json:"purchase_in"`
|
PurchaseIn uint32 `json:"purchase_in"`
|
||||||
ProductIn uint32 `json:"product_in"`
|
PurchaseInAmount float64 `json:"purchase_in_amount"`
|
||||||
CheckIn uint32 `json:"check_in"`
|
ProductIn uint32 `json:"product_in"`
|
||||||
|
ProductInAmount float64 `json:"product_in_amount"`
|
||||||
|
CheckIn uint32 `json:"check_in"`
|
||||||
|
CheckInAmount float64 `json:"check_in_amount"`
|
||||||
}
|
}
|
||||||
ts := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
ts := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS purchase_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in",
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS product_in_amount, "+
|
||||||
PurchaseInventory, ProductInventory, CheckInventory).
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS check_in_amount",
|
||||||
|
PurchaseInventory, PurchaseInventory, ProductInventory, ProductInventory, CheckInventory, CheckInventory).
|
||||||
Where("created_at <= ?", previousDay).
|
Where("created_at <= ?", previousDay).
|
||||||
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
||||||
|
|
||||||
|
@ -584,30 +527,38 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep
|
||||||
// 计算期初库存
|
// 计算期初库存
|
||||||
beginStock := int64(systemStartData.BeginStock) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut) +
|
beginStock := int64(systemStartData.BeginStock) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut) +
|
||||||
int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn)
|
int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn)
|
||||||
|
|
||||||
if beginStock < 0 {
|
if beginStock < 0 {
|
||||||
beginStock = 0 // 确保库存不会为负
|
beginStock = 0 // 确保库存不会为负
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 计算期初金额
|
||||||
|
beginAmount := systemStartData.BeginAmount - outStockData.SoldOutAmount -
|
||||||
|
outStockData.SystemOutAmount - outStockData.CheckOutAmount +
|
||||||
|
inStockData.PurchaseInAmount + inStockData.ProductInAmount + inStockData.CheckInAmount
|
||||||
|
if beginAmount < 0 {
|
||||||
|
beginAmount = 0 // 确保库存不会为负
|
||||||
|
}
|
||||||
|
|
||||||
reportData.BeginStock = uint32(beginStock)
|
reportData.BeginStock = uint32(beginStock)
|
||||||
reportData.BeginAmount = systemStartData.BeginAmount
|
reportData.BeginAmount = beginAmount
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 如果没有开始时间,直接获取系统入库数量作为期初库存
|
//// 如果没有开始时间,直接获取系统入库数量作为期初库存
|
||||||
qs := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
//qs := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount").
|
// Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount").
|
||||||
Where("storage_type = ? and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId)
|
// Where("storage_type = ? and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId)
|
||||||
|
//
|
||||||
if stock.StoreId != 0 {
|
//if stock.StoreId != 0 {
|
||||||
if len(stock.DecisionStoreId) > 1 {
|
// if len(stock.DecisionStoreId) > 1 {
|
||||||
qs = qs.Where("store_id in (?)", stock.DecisionStoreId)
|
// qs = qs.Where("store_id in (?)", stock.DecisionStoreId)
|
||||||
} else {
|
// } else {
|
||||||
qs = qs.Where("store_id = ?", stock.DecisionStoreId)
|
// qs = qs.Where("store_id = ?", stock.DecisionStoreId)
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
err := qs.Find(&reportData).Error
|
//err := qs.Find(&reportData).Error
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return DecisionReportData{}, err
|
// return DecisionReportData{}, err
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reportData, nil
|
return reportData, nil
|
||||||
|
@ -628,17 +579,27 @@ func getSystemEndCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRepor
|
||||||
|
|
||||||
// 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库)
|
// 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库)
|
||||||
var inStockData struct {
|
var inStockData struct {
|
||||||
SystemIn uint32 `json:"system_in"`
|
SystemIn uint32 `json:"system_in"`
|
||||||
PurchaseIn uint32 `json:"purchase_in"`
|
SystemInAmount float64 `json:"system_in_amount"`
|
||||||
ProductIn uint32 `json:"product_in"`
|
PurchaseIn uint32 `json:"purchase_in"`
|
||||||
CheckIn uint32 `json:"check_in"`
|
PurchaseInAmount float64 `json:"purchase_in_amount"`
|
||||||
|
ProductIn uint32 `json:"product_in"`
|
||||||
|
ProductInAmount float64 `json:"product_in_amount"`
|
||||||
|
CheckIn uint32 `json:"check_in"`
|
||||||
|
CheckInAmount float64 `json:"check_in_amount"`
|
||||||
}
|
}
|
||||||
qs = qs.Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS system_in, "+
|
qs = qs.Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS system_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS system_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS purchase_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in",
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS product_in_amount, "+
|
||||||
SystemInventory, PurchaseInventory, ProductInventory, CheckInventory).
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in, "+
|
||||||
Where("updated_at <= ?", endTime)
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS check_in_amount",
|
||||||
|
SystemInventory, SystemInventory, PurchaseInventory, PurchaseInventory, ProductInventory, ProductInventory,
|
||||||
|
CheckInventory, CheckInventory).
|
||||||
|
Where("updated_at <= ?", endTime).
|
||||||
|
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
||||||
|
|
||||||
if stock.StoreId != 0 {
|
if stock.StoreId != 0 {
|
||||||
if len(stock.DecisionStoreId) > 1 {
|
if len(stock.DecisionStoreId) > 1 {
|
||||||
|
@ -655,16 +616,23 @@ func getSystemEndCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRepor
|
||||||
|
|
||||||
// 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库)
|
// 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库)
|
||||||
var outStockData struct {
|
var outStockData struct {
|
||||||
SoldOut uint32 `json:"sold_out"`
|
SoldOut uint32 `json:"sold_out"`
|
||||||
SystemOut uint32 `json:"system_out"`
|
SoldOutAmount float64 `json:"sold_out_amount"`
|
||||||
CheckOut uint32 `json:"check_out"`
|
SystemOut uint32 `json:"system_out"`
|
||||||
|
SystemOutAmount float64 `json:"system_out_amount"`
|
||||||
|
CheckOut uint32 `json:"check_out"`
|
||||||
|
CheckOutAmount float64 `json:"check_out_amount"`
|
||||||
}
|
}
|
||||||
es = es.Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
es = es.Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS sold_out_amount, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out",
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS system_out_amount, "+
|
||||||
SoldOut, SystemOut, CheckOut).
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS check_out_amount",
|
||||||
|
SoldOut, SoldOut, SystemOut, SystemOut, CheckOut, CheckOut).
|
||||||
Where("updated_at <= ?", endTime).
|
Where("updated_at <= ?", endTime).
|
||||||
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut})
|
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut}).
|
||||||
|
Where("erp_commodity_id = ?", stock.ErpCommodityId)
|
||||||
|
|
||||||
if stock.StoreId != 0 {
|
if stock.StoreId != 0 {
|
||||||
if len(stock.DecisionStoreId) > 1 {
|
if len(stock.DecisionStoreId) > 1 {
|
||||||
|
@ -679,18 +647,29 @@ func getSystemEndCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRepor
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算期末库存
|
// 计算期末库存
|
||||||
endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn) -
|
endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) +
|
||||||
int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut)
|
int64(inStockData.CheckIn) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) -
|
||||||
|
int64(outStockData.CheckOut)
|
||||||
|
|
||||||
if endStock < 0 {
|
if endStock < 0 {
|
||||||
endStock = 0 // 确保库存不会为负
|
endStock = 0 // 确保库存不会为负
|
||||||
}
|
}
|
||||||
reportData.EndStock = uint32(endStock)
|
|
||||||
|
|
||||||
|
// 计算期末金额
|
||||||
|
endAmount := inStockData.SystemInAmount + inStockData.PurchaseInAmount + inStockData.ProductInAmount +
|
||||||
|
inStockData.CheckInAmount - outStockData.SoldOutAmount - outStockData.SystemOutAmount -
|
||||||
|
outStockData.CheckOutAmount
|
||||||
|
if endAmount < 0 {
|
||||||
|
endAmount = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
reportData.EndStock = uint32(endStock)
|
||||||
|
reportData.EndAmount = endAmount
|
||||||
} else {
|
} else {
|
||||||
// 如果没有结束时间,直接获取系统入库数量作为期末库存
|
// 如果没有结束时间,直接获取系统在库数量作为期末库存
|
||||||
qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
||||||
Where("storage_type = ? and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId)
|
Where("state in (?) and erp_commodity_id = ?", []uint32{InStock, InAllot}, stock.ErpCommodityId).
|
||||||
|
Find(&reportData)
|
||||||
|
|
||||||
if stock.StoreId != 0 {
|
if stock.StoreId != 0 {
|
||||||
if len(stock.DecisionStoreId) > 1 {
|
if len(stock.DecisionStoreId) > 1 {
|
||||||
|
@ -1194,6 +1173,51 @@ func getChangeReduceCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRe
|
||||||
return reportData, nil
|
return reportData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询系统入库数据:系统入库
|
||||||
|
func getSystemInCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
||||||
|
var reportData DecisionReportData
|
||||||
|
|
||||||
|
qs := orm.Eloquent.Debug().Table("erp_stock_commodity")
|
||||||
|
|
||||||
|
// 进行条件查询
|
||||||
|
if req.StartTime != "" { // 出入库开始时间
|
||||||
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("getSystemStartCount err:", err)
|
||||||
|
return DecisionReportData{}, err
|
||||||
|
}
|
||||||
|
qs = qs.Where("created_at > ?", parse)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.EndTime != "" { // 出入库结束时间
|
||||||
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("getSystemStartCount err:", err)
|
||||||
|
return DecisionReportData{}, err
|
||||||
|
}
|
||||||
|
qs = qs.Where("created_at <= ?", parse)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if stock.StoreId != 0 {
|
||||||
|
if len(stock.DecisionStoreId) > 1 {
|
||||||
|
err = qs.Select("SUM(count) as system_in").Where("storage_type = ? and store_id in (?) "+
|
||||||
|
"and erp_commodity_id = ?", SystemInventory, stock.DecisionStoreId, stock.ErpCommodityId).Find(&reportData).Error
|
||||||
|
} else {
|
||||||
|
err = qs.Select("SUM(count) as system_in").Where("storage_type = ? and store_id = ? "+
|
||||||
|
"and erp_commodity_id = ?", SystemInventory, stock.StoreId, stock.ErpCommodityId).Find(&reportData).Error
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = qs.Select("SUM(count) as system_in").Where("storage_type = ? "+
|
||||||
|
"and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId).Find(&reportData).Error
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return DecisionReportData{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return reportData, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 查询系统出库数据:系统出库
|
// 查询系统出库数据:系统出库
|
||||||
func getSystemOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
func getSystemOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
||||||
var reportData DecisionReportData
|
var reportData DecisionReportData
|
||||||
|
@ -1476,7 +1500,9 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算开始时间的前一天
|
// 计算开始时间的前一天
|
||||||
previousDay := startTime.AddDate(0, 0, -1)
|
//previousDay := startTime.AddDate(0, 0, -1)
|
||||||
|
previousDay := startTime.AddDate(0, 0, -1).Truncate(24 * time.Hour).
|
||||||
|
Add(23*time.Hour + 59*time.Minute + 59*time.Second)
|
||||||
|
|
||||||
// 获取系统入库数量
|
// 获取系统入库数量
|
||||||
var systemStartData DecisionReportData
|
var systemStartData DecisionReportData
|
||||||
|
@ -1487,29 +1513,41 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
|
|
||||||
// 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库)
|
// 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库)
|
||||||
var outStockData struct {
|
var outStockData struct {
|
||||||
SoldOut uint32 `json:"sold_out"`
|
SoldOut uint32 `json:"sold_out"`
|
||||||
SystemOut uint32 `json:"system_out"`
|
SoldOutAmount float64 `json:"sold_out_amount"`
|
||||||
CheckOut uint32 `json:"check_out"`
|
SystemOut uint32 `json:"system_out"`
|
||||||
|
SystemOutAmount float64 `json:"system_out_amount"`
|
||||||
|
CheckOut uint32 `json:"check_out"`
|
||||||
|
CheckOutAmount float64 `json:"check_out_amount"`
|
||||||
}
|
}
|
||||||
es := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
es := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS sold_out_amount, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out",
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS system_out_amount, "+
|
||||||
SoldOut, SystemOut, CheckOut).
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS check_out_amount",
|
||||||
|
SoldOut, SoldOut, SystemOut, SystemOut, CheckOut, CheckOut).
|
||||||
Where("updated_at <= ?", previousDay).
|
Where("updated_at <= ?", previousDay).
|
||||||
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut})
|
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut})
|
||||||
|
|
||||||
// 获取开始时间前一天的所有入库数量(包括采购入库、产品入库和盘点入库)
|
// 获取开始时间前一天的所有入库数量(包括采购入库、产品入库和盘点入库)
|
||||||
var inStockData struct {
|
var inStockData struct {
|
||||||
PurchaseIn uint32 `json:"purchase_in"`
|
PurchaseIn uint32 `json:"purchase_in"`
|
||||||
ProductIn uint32 `json:"product_in"`
|
PurchaseInAmount float64 `json:"purchase_in_amount"`
|
||||||
CheckIn uint32 `json:"check_in"`
|
ProductIn uint32 `json:"product_in"`
|
||||||
|
ProductInAmount float64 `json:"product_in_amount"`
|
||||||
|
CheckIn uint32 `json:"check_in"`
|
||||||
|
CheckInAmount float64 `json:"check_in_amount"`
|
||||||
}
|
}
|
||||||
ts := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
ts := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS purchase_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in",
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS product_in_amount, "+
|
||||||
PurchaseInventory, ProductInventory, CheckInventory).
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS check_in_amount",
|
||||||
|
PurchaseInventory, PurchaseInventory, ProductInventory, ProductInventory, CheckInventory, CheckInventory).
|
||||||
Where("created_at <= ?", previousDay)
|
Where("created_at <= ?", previousDay)
|
||||||
|
|
||||||
if len(req.StoreId) > 0 { // 门店复选
|
if len(req.StoreId) > 0 { // 门店复选
|
||||||
|
@ -1576,61 +1614,71 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算期初库存
|
// 计算期初库存
|
||||||
beginStock := int64(systemStartData.BeginStock) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut) +
|
beginStock := int64(systemStartData.BeginStock) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) -
|
||||||
int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn)
|
int64(outStockData.CheckOut) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) +
|
||||||
|
int64(inStockData.CheckIn)
|
||||||
|
|
||||||
if beginStock < 0 {
|
if beginStock < 0 {
|
||||||
beginStock = 0 // 确保库存不会为负
|
beginStock = 0 // 确保库存不会为负
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 计算期初金额
|
||||||
|
beginAmount := systemStartData.BeginAmount - outStockData.SoldOutAmount -
|
||||||
|
outStockData.SystemOutAmount - outStockData.CheckOutAmount +
|
||||||
|
inStockData.PurchaseInAmount + inStockData.ProductInAmount + inStockData.CheckInAmount
|
||||||
|
if beginAmount < 0 {
|
||||||
|
beginAmount = 0 // 确保库存不会为负
|
||||||
|
}
|
||||||
|
|
||||||
reportData.BeginStock = uint32(beginStock)
|
reportData.BeginStock = uint32(beginStock)
|
||||||
reportData.BeginAmount = systemStartData.BeginAmount
|
reportData.BeginAmount = beginAmount
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// 如果没有开始时间,直接获取系统入库数量作为期初库存
|
//// 如果没有开始时间,直接获取系统入库数量作为期初库存
|
||||||
qs := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
//qs := orm.Eloquent.Debug().Table("erp_stock_commodity").
|
||||||
Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount").
|
// Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount").
|
||||||
Where("storage_type = ?", SystemInventory)
|
// Where("storage_type = ?", SystemInventory)
|
||||||
|
//
|
||||||
if len(req.StoreId) > 0 { // 门店复选
|
//if len(req.StoreId) > 0 { // 门店复选
|
||||||
var storeIDs []uint32
|
// var storeIDs []uint32
|
||||||
for _, store := range req.StoreId {
|
// for _, store := range req.StoreId {
|
||||||
storeIDs = append(storeIDs, store)
|
// storeIDs = append(storeIDs, store)
|
||||||
}
|
// }
|
||||||
if len(storeIDs) == 1 {
|
// if len(storeIDs) == 1 {
|
||||||
qs = qs.Where("store_id IN = ?", storeIDs[0])
|
// qs = qs.Where("store_id IN = ?", storeIDs[0])
|
||||||
} else {
|
// } else {
|
||||||
qs = qs.Where("store_id IN (?)", storeIDs)
|
// qs = qs.Where("store_id IN (?)", storeIDs)
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
if len(req.CommodityName) > 0 { // 商品名称
|
//if len(req.CommodityName) > 0 { // 商品名称
|
||||||
var commodityNames []string
|
// var commodityNames []string
|
||||||
for _, commodityName := range req.CommodityName {
|
// for _, commodityName := range req.CommodityName {
|
||||||
commodityNames = append(commodityNames, commodityName)
|
// commodityNames = append(commodityNames, commodityName)
|
||||||
}
|
// }
|
||||||
if len(commodityNames) == 1 {
|
// if len(commodityNames) == 1 {
|
||||||
qs = qs.Where("erp_commodity_name = ?", commodityNames[0])
|
// qs = qs.Where("erp_commodity_name = ?", commodityNames[0])
|
||||||
} else {
|
// } else {
|
||||||
qs = qs.Where("erp_commodity_name IN (?)", commodityNames)
|
// qs = qs.Where("erp_commodity_name IN (?)", commodityNames)
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
if len(req.CategoryID) > 0 { // 商品分类id
|
//if len(req.CategoryID) > 0 { // 商品分类id
|
||||||
var categoryIDs []uint32
|
// var categoryIDs []uint32
|
||||||
for _, category := range req.CategoryID {
|
// for _, category := range req.CategoryID {
|
||||||
categoryIDs = append(categoryIDs, category)
|
// categoryIDs = append(categoryIDs, category)
|
||||||
}
|
// }
|
||||||
if len(categoryIDs) == 1 {
|
// if len(categoryIDs) == 1 {
|
||||||
qs = qs.Where("erp_category_id = ?", categoryIDs[0])
|
// qs = qs.Where("erp_category_id = ?", categoryIDs[0])
|
||||||
} else {
|
// } else {
|
||||||
qs = qs.Where("erp_category_id IN (?)", categoryIDs)
|
// qs = qs.Where("erp_category_id IN (?)", categoryIDs)
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
err := qs.Find(&reportData).Error
|
//err := qs.Find(&reportData).Error
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
return DecisionReportData{}, err
|
// return DecisionReportData{}, err
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
return reportData, nil
|
return reportData, nil
|
||||||
|
@ -1694,16 +1742,25 @@ func getSumEndCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
|
|
||||||
// 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库)
|
// 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库)
|
||||||
var inStockData struct {
|
var inStockData struct {
|
||||||
SystemIn uint32 `json:"system_in"`
|
SystemIn uint32 `json:"system_in"`
|
||||||
PurchaseIn uint32 `json:"purchase_in"`
|
SystemInAmount float64 `json:"system_in_amount"`
|
||||||
ProductIn uint32 `json:"product_in"`
|
PurchaseIn uint32 `json:"purchase_in"`
|
||||||
CheckIn uint32 `json:"check_in"`
|
PurchaseInAmount float64 `json:"purchase_in_amount"`
|
||||||
|
ProductIn uint32 `json:"product_in"`
|
||||||
|
ProductInAmount float64 `json:"product_in_amount"`
|
||||||
|
CheckIn uint32 `json:"check_in"`
|
||||||
|
CheckInAmount float64 `json:"check_in_amount"`
|
||||||
}
|
}
|
||||||
qs = qs.Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS system_in, "+
|
qs = qs.Select("SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS system_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS system_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS purchase_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS purchase_in_amount, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, "+
|
||||||
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in",
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS product_in_amount, "+
|
||||||
SystemInventory, PurchaseInventory, ProductInventory, CheckInventory).
|
"SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS check_in, "+
|
||||||
|
"SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS check_in_amount",
|
||||||
|
SystemInventory, SystemInventory, PurchaseInventory, PurchaseInventory, ProductInventory, ProductInventory,
|
||||||
|
CheckInventory, CheckInventory).
|
||||||
Where("updated_at <= ?", endTime)
|
Where("updated_at <= ?", endTime)
|
||||||
|
|
||||||
err = qs.Find(&inStockData).Error
|
err = qs.Find(&inStockData).Error
|
||||||
|
@ -1713,14 +1770,20 @@ func getSumEndCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
|
|
||||||
// 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库)
|
// 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库)
|
||||||
var outStockData struct {
|
var outStockData struct {
|
||||||
SoldOut uint32 `json:"sold_out"`
|
SoldOut uint32 `json:"sold_out"`
|
||||||
SystemOut uint32 `json:"system_out"`
|
SoldOutAmount float64 `json:"sold_out_amount"`
|
||||||
CheckOut uint32 `json:"check_out"`
|
SystemOut uint32 `json:"system_out"`
|
||||||
|
SystemOutAmount float64 `json:"system_out_amount"`
|
||||||
|
CheckOut uint32 `json:"check_out"`
|
||||||
|
CheckOutAmount float64 `json:"check_out_amount"`
|
||||||
}
|
}
|
||||||
es = es.Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
es = es.Select("SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS sold_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS sold_out_amount, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out, "+
|
||||||
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out",
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS system_out_amount, "+
|
||||||
SoldOut, SystemOut, CheckOut).
|
"SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS check_out, "+
|
||||||
|
"SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS check_out_amount",
|
||||||
|
SoldOut, SoldOut, SystemOut, SystemOut, CheckOut, CheckOut).
|
||||||
Where("updated_at <= ?", endTime).
|
Where("updated_at <= ?", endTime).
|
||||||
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut})
|
Where("state IN (?)", []int{SoldOut, SystemOut, CheckOut})
|
||||||
|
|
||||||
|
@ -1736,12 +1799,21 @@ func getSumEndCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
if endStock < 0 {
|
if endStock < 0 {
|
||||||
endStock = 0 // 确保库存不会为负
|
endStock = 0 // 确保库存不会为负
|
||||||
}
|
}
|
||||||
reportData.EndStock = uint32(endStock)
|
|
||||||
|
|
||||||
|
// 计算期末金额
|
||||||
|
endAmount := inStockData.SystemInAmount + inStockData.PurchaseInAmount + inStockData.ProductInAmount +
|
||||||
|
inStockData.CheckInAmount - outStockData.SoldOutAmount - outStockData.SystemOutAmount -
|
||||||
|
outStockData.CheckOutAmount
|
||||||
|
if endAmount < 0 {
|
||||||
|
endAmount = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
reportData.EndStock = uint32(endStock)
|
||||||
|
reportData.EndAmount = endAmount
|
||||||
} else {
|
} else {
|
||||||
// 如果没有结束时间,直接获取系统入库数量作为期末库存
|
// 如果没有结束时间,直接获取系统在库数量作为期末库存
|
||||||
qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
||||||
Where("storage_type = ?", SystemInventory)
|
Where("state in (?)", []uint32{InStock, InAllot}).Find(&reportData)
|
||||||
|
|
||||||
err := qs.Find(&reportData).Error
|
err := qs.Find(&reportData).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1752,7 +1824,7 @@ func getSumEndCount(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
return reportData, nil
|
return reportData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询库存汇总数据:采购退货数量、产品入库、系统出库、在途库存(入库)数量
|
// 查询库存汇总数据:采购退货数量、产品入库、系统入库、系统出库、在途库存(入库)数量
|
||||||
func getSumStockData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
func getSumStockData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
var reportData DecisionReportData
|
var reportData DecisionReportData
|
||||||
qs := orm.Eloquent.Debug().Table("erp_stock_commodity")
|
qs := orm.Eloquent.Debug().Table("erp_stock_commodity")
|
||||||
|
@ -1810,9 +1882,10 @@ func getSumStockData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
||||||
subQuery := qs.Select(`
|
subQuery := qs.Select(`
|
||||||
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS purchase_return,
|
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS purchase_return,
|
||||||
SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in,
|
SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in,
|
||||||
|
SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS system_in,
|
||||||
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out,
|
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS system_out,
|
||||||
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS allot_wait_in
|
SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS allot_wait_in
|
||||||
`, PurchaseReturn, ProductInventory, SystemOut, InAllot)
|
`, PurchaseReturn, ProductInventory, SystemInventory, SystemOut, InAllot)
|
||||||
|
|
||||||
err := subQuery.Scan(&reportData).Error
|
err := subQuery.Scan(&reportData).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2146,6 +2219,7 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error
|
||||||
sumData.TotalPurchaseReturn = sumStockData.PurchaseReturn
|
sumData.TotalPurchaseReturn = sumStockData.PurchaseReturn
|
||||||
sumData.TotalAllotWaitIn = sumStockData.AllotWaitIn
|
sumData.TotalAllotWaitIn = sumStockData.AllotWaitIn
|
||||||
sumData.TotalProductIn = sumStockData.ProductIn
|
sumData.TotalProductIn = sumStockData.ProductIn
|
||||||
|
sumData.TotalSystemIn = sumStockData.SystemIn
|
||||||
sumData.TotalSystemOut = sumStockData.SystemOut
|
sumData.TotalSystemOut = sumStockData.SystemOut
|
||||||
|
|
||||||
sumPurchaseData, err := getSumPurchaseData(req)
|
sumPurchaseData, err := getSumPurchaseData(req)
|
||||||
|
@ -2176,13 +2250,16 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error
|
||||||
sumData.TotalAllotIn = sumAllotData.AllotIn
|
sumData.TotalAllotIn = sumAllotData.AllotIn
|
||||||
sumData.TotalAllotOut = sumAllotData.AllotOut
|
sumData.TotalAllotOut = sumAllotData.AllotOut
|
||||||
|
|
||||||
// Step 4: 计算调整库存逻辑(期初+入库-出库)
|
if req.EndTime != "" {
|
||||||
sumData.TotalEndStock = sumData.TotalBeginStock + sumData.TotalPurchaseStock + sumData.TotalAllotIn + sumData.TotalProductIn + sumData.TotalCheckIn
|
// Step 4: 计算调整库存逻辑(期初+入库-出库)
|
||||||
if sumData.TotalEndStock < (sumData.TotalPurchaseReturn + sumData.TotalOrderSale + sumData.TotalAllotOut + sumData.TotalSystemOut + sumData.TotalCheckOut) {
|
sumData.TotalEndStock = sumData.TotalBeginStock + sumData.TotalPurchaseStock + sumData.TotalAllotIn +
|
||||||
sumData.TotalEndStock = 0
|
sumData.TotalProductIn + sumData.TotalCheckIn + sumData.TotalOrderReject
|
||||||
sumData.TotalEndAmount = 0
|
if sumData.TotalEndStock < (sumData.TotalPurchaseReturn + sumData.TotalOrderSale + sumData.TotalAllotOut + sumData.TotalSystemOut + sumData.TotalCheckOut) {
|
||||||
} else {
|
sumData.TotalEndStock = 0
|
||||||
sumData.TotalEndStock -= sumData.TotalPurchaseReturn + sumData.TotalOrderSale + sumData.TotalAllotOut + sumData.TotalSystemOut + sumData.TotalCheckOut
|
sumData.TotalEndAmount = 0
|
||||||
|
} else {
|
||||||
|
sumData.TotalEndStock -= sumData.TotalPurchaseReturn + sumData.TotalOrderSale + sumData.TotalAllotOut + sumData.TotalSystemOut + sumData.TotalCheckOut
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
roundDecisionSumData(&sumData)
|
roundDecisionSumData(&sumData)
|
||||||
|
|
|
@ -783,7 +783,10 @@ func handleInventoryReduce(gdb *gorm.DB, changeOrder ErpInventoryChangeOrder) er
|
||||||
|
|
||||||
// 更新库存商品状态为:盘点出库
|
// 更新库存商品状态为:盘点出库
|
||||||
err = gdb.Table("erp_stock_commodity").Where("imei = ?", trimCommodities[i].IMEI).
|
err = gdb.Table("erp_stock_commodity").Where("imei = ?", trimCommodities[i].IMEI).
|
||||||
Updates(map[string]interface{}{"state": CheckOut}).Error
|
Updates(map[string]interface{}{
|
||||||
|
"state": CheckOut,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
}).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("handleInventoryReduce update erp_stock_commodity err:",
|
logger.Error("handleInventoryReduce update erp_stock_commodity err:",
|
||||||
logger.Field("err", err))
|
logger.Field("err", err))
|
||||||
|
@ -836,7 +839,10 @@ func handleInventoryReduce(gdb *gorm.DB, changeOrder ErpInventoryChangeOrder) er
|
||||||
}
|
}
|
||||||
|
|
||||||
err = gdb.Table("erp_stock_commodity").Where("id = ?", rightId).
|
err = gdb.Table("erp_stock_commodity").Where("id = ?", rightId).
|
||||||
Updates(map[string]interface{}{"state": CheckOut}).Error // 状态更新为:盘点出库
|
Updates(map[string]interface{}{
|
||||||
|
"state": CheckOut,
|
||||||
|
"updated_at": time.Now(),
|
||||||
|
}).Error // 状态更新为:盘点出库
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("handleInventoryReduce update erp_stock_commodity err:",
|
logger.Error("handleInventoryReduce update erp_stock_commodity err:",
|
||||||
logger.Field("err", err))
|
logger.Field("err", err))
|
||||||
|
|
|
@ -1400,8 +1400,9 @@ func InventoryErpPurchaseUpdateRejectStock(gdb *gorm.DB, list []ErpPurchaseInven
|
||||||
|
|
||||||
err = gdb.Table("erp_stock_commodity").Where("imei = ?", list[i].IMEI).
|
err = gdb.Table("erp_stock_commodity").Where("imei = ?", list[i].IMEI).
|
||||||
Updates(&map[string]interface{}{
|
Updates(&map[string]interface{}{
|
||||||
"state": PurchaseReturn,
|
"state": PurchaseReturn,
|
||||||
"stock_sn": list[i].SerialNumber,
|
"stock_sn": list[i].SerialNumber,
|
||||||
|
"updated_at": time.Now(),
|
||||||
}).Error // 状态更新为采购退货
|
}).Error // 状态更新为采购退货
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("RetailTypeRejected commodities err:", logger.Field("err", err))
|
logger.Error("RetailTypeRejected commodities err:", logger.Field("err", err))
|
||||||
|
@ -1462,8 +1463,9 @@ func InventoryErpPurchaseUpdateRejectStock(gdb *gorm.DB, list []ErpPurchaseInven
|
||||||
return fmt.Errorf("商品[%s]采购退货数量超出实际库存数量,请先零售退货", list[i].ErpCommodityName)
|
return fmt.Errorf("商品[%s]采购退货数量超出实际库存数量,请先零售退货", list[i].ErpCommodityName)
|
||||||
}
|
}
|
||||||
err = gdb.Table("erp_stock_commodity").Where("id = ?", currentID).Updates(&map[string]interface{}{
|
err = gdb.Table("erp_stock_commodity").Where("id = ?", currentID).Updates(&map[string]interface{}{
|
||||||
"state": PurchaseReturn,
|
"state": PurchaseReturn,
|
||||||
"stock_sn": list[i].SerialNumber,
|
"stock_sn": list[i].SerialNumber,
|
||||||
|
"updated_at": time.Now(),
|
||||||
}).Error // 状态更新为采购退货
|
}).Error // 状态更新为采购退货
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("RetailTypeRejected commodities err:", logger.Field("err", err))
|
logger.Error("RetailTypeRejected commodities err:", logger.Field("err", err))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user