2024-04-10 06:17:19 +00:00
|
|
|
package models
|
|
|
|
|
2024-04-25 09:33:36 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
|
orm "go-admin/common/global"
|
|
|
|
"go-admin/logger"
|
|
|
|
"go-admin/tools"
|
|
|
|
"go-admin/tools/config"
|
2024-05-23 07:30:20 +00:00
|
|
|
"math"
|
2024-04-25 09:33:36 +00:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-04-10 06:17:19 +00:00
|
|
|
// ErpDecisionReportReq 进销存报表入参
|
|
|
|
type ErpDecisionReportReq struct {
|
|
|
|
StoreId []uint32 `json:"store_id"` // 门店id
|
|
|
|
CommodityName []string `json:"commodity_name"` // 商品名称
|
|
|
|
CategoryID []uint32 `json:"category_id"` // 商品分类id
|
|
|
|
StartTime string `json:"start_time"` // 开始时间
|
|
|
|
EndTime string `json:"end_time"` // 结束时间
|
|
|
|
IsExport uint32 `json:"is_export"` // 1-导出
|
|
|
|
PageIndex int `json:"pageIndex"` // 页码
|
|
|
|
PageSize int `json:"pageSize"` // 页面条数
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErpDecisionReportResp 进销存报表出参
|
|
|
|
type ErpDecisionReportResp struct {
|
2024-04-25 09:33:36 +00:00
|
|
|
Total int `json:"total"` // 总条数/记录数
|
|
|
|
PageIndex int `json:"pageIndex"` // 页码
|
|
|
|
PageSize int `json:"pageSize"` // 页面条数
|
2024-04-10 06:17:19 +00:00
|
|
|
ExportUrl string `json:"export_url"` // 导出excel路径
|
2024-04-25 09:33:36 +00:00
|
|
|
SumData DecisionSumData `json:"sum_data"` // 汇总数据
|
|
|
|
List []DecisionReportData `json:"list"` // 明细数据
|
2024-04-10 06:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecisionSumData 进销存汇总数据
|
|
|
|
type DecisionSumData struct {
|
|
|
|
TotalBeginStock uint32 `json:"total_begin_stock"` // 期初库存
|
|
|
|
TotalBeginAmount float64 `json:"total_begin_amount"` // 期初金额
|
|
|
|
TotalPurchaseStock uint32 `json:"total_purchase_stock"` // 采购进货
|
|
|
|
TotalPurchaseReturn uint32 `json:"total_purchase_return"` // 采购退货
|
|
|
|
TotalOrderSale uint32 `json:"total_order_sale"` // 零售销售
|
|
|
|
TotalOrderReject uint32 `json:"total_order_reject"` // 零售退货
|
|
|
|
TotalAllotIn uint32 `json:"total_allot_in"` // 调拨入库
|
|
|
|
TotalAllotWaitIn uint32 `json:"total_allot_wait_in"` // 在途库存(入库)
|
|
|
|
TotalAllotOut uint32 `json:"total_allot_out"` // 调拨出库
|
|
|
|
TotalAllotWaitOut uint32 `json:"total_allot_wait_out"` // 在途库存(出库)
|
|
|
|
TotalProductIn uint32 `json:"total_product_in"` // 产品入库
|
|
|
|
TotalSystemOut uint32 `json:"total_system_out"` // 系统出库
|
|
|
|
TotalCheckIn uint32 `json:"total_check_in"` // 盘点入库
|
|
|
|
TotalCheckOut uint32 `json:"total_check_out"` // 盘点出库
|
|
|
|
TotalEndStock uint32 `json:"total_end_stock"` // 期末数量
|
|
|
|
TotalEndAmount float64 `json:"total_end_amount"` // 期末金额
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecisionReportData 进销存报表数据
|
|
|
|
type DecisionReportData struct {
|
2024-06-20 06:32:03 +00:00
|
|
|
CommodityId uint32 `json:"commodity_id"` // 商品id
|
|
|
|
CommodityName string `json:"commodity_name"` // 商品名称
|
|
|
|
CommoditySerialNumber string `json:"commodity_serial_number" gorm:"-"` // 商品编码
|
|
|
|
CategoryID uint32 `json:"category_id"` // 商品分类id
|
|
|
|
CategoryName string `json:"category_name"` // 商品分类名称
|
|
|
|
BeginStock uint32 `json:"begin_stock"` // 期初库存
|
|
|
|
BeginAmount float64 `json:"begin_amount"` // 期初金额
|
|
|
|
PurchaseStock uint32 `json:"purchase_stock"` // 采购进货
|
|
|
|
PurchaseReturn uint32 `json:"purchase_return"` // 采购退货
|
|
|
|
OrderSale uint32 `json:"order_sale"` // 零售销售
|
|
|
|
OrderReject uint32 `json:"order_reject"` // 零售退货
|
|
|
|
AllotIn uint32 `json:"allot_in"` // 调拨入库
|
|
|
|
AllotWaitIn uint32 `json:"allot_wait_in"` // 在途库存(入库)
|
|
|
|
AllotOut uint32 `json:"allot_out"` // 调拨出库
|
|
|
|
AllotWaitOut uint32 `json:"allot_wait_out"` // 在途库存(出库)
|
|
|
|
ProductIn uint32 `json:"product_in"` // 产品入库
|
|
|
|
SystemOut uint32 `json:"system_out"` // 系统出库
|
|
|
|
CheckIn uint32 `json:"check_in"` // 盘点入库
|
|
|
|
CheckOut uint32 `json:"check_out"` // 盘点出库
|
|
|
|
EndStock uint32 `json:"end_stock"` // 期末数量
|
|
|
|
EndAmount float64 `json:"end_amount"` // 期末金额
|
2024-04-10 06:17:19 +00:00
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
|
|
|
|
// DecisionReportList 进销存报表
|
|
|
|
func (m *ErpDecisionReportReq) DecisionReportList(c *gin.Context) (*ErpDecisionReportResp, error) {
|
|
|
|
// 非管理员才判断所属门店
|
|
|
|
if !(tools.GetRoleName(c) == "admin" || tools.GetRoleName(c) == "系统管理员") {
|
|
|
|
sysUser, err := GetSysUserByCtx(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("操作失败:" + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回sysUser未过期的门店id列表
|
|
|
|
storeList := GetValidStoreIDs(sysUser.StoreData)
|
|
|
|
if len(storeList) > 0 {
|
|
|
|
m.StoreId = CompareLists(storeList, m.StoreId)
|
|
|
|
if len(m.StoreId) == 0 { // 没有匹配的数据,表示入参门店不是用户有权限的门店
|
|
|
|
return &ErpDecisionReportResp{}, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("用户未绑定门店")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &ErpDecisionReportResp{
|
|
|
|
PageIndex: m.PageIndex,
|
|
|
|
PageSize: m.PageSize,
|
|
|
|
}
|
|
|
|
page := m.PageIndex - 1
|
|
|
|
if page < 0 {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
if m.PageSize == 0 {
|
|
|
|
m.PageSize = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_stock")
|
|
|
|
|
|
|
|
if len(m.StoreId) > 0 { // 门店复选
|
|
|
|
var storeIDs []uint32
|
|
|
|
for _, store := range m.StoreId {
|
|
|
|
storeIDs = append(storeIDs, store)
|
|
|
|
}
|
|
|
|
qs = qs.Where("store_id IN (?)", storeIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.CommodityName) > 0 { // 商品名称
|
|
|
|
var commodityNames []string
|
|
|
|
for _, commodityName := range m.CommodityName {
|
|
|
|
commodityNames = append(commodityNames, commodityName)
|
|
|
|
}
|
2024-05-23 07:30:20 +00:00
|
|
|
qs = qs.Where("erp_commodity_name IN (?)", commodityNames)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.CategoryID) > 0 { // 商品分类id
|
|
|
|
var categoryIDs []uint32
|
|
|
|
for _, category := range m.CategoryID {
|
|
|
|
categoryIDs = append(categoryIDs, category)
|
|
|
|
}
|
2024-05-23 07:30:20 +00:00
|
|
|
qs = qs.Where("erp_category_id IN (?)", categoryIDs)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
err := qs.Count(&count).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("查询无库存列表数量失败", logger.Field("err", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var commodities []ErpStock
|
2024-07-19 02:06:02 +00:00
|
|
|
if len(m.StoreId) == 1 && m.IsExport != 1 { // 只查询1个门店数据
|
2024-07-16 10:30:46 +00:00
|
|
|
err = qs.Order("erp_commodity_id, store_id desc").Offset(page * m.PageSize).Limit(m.PageSize).Find(&commodities).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Order("erp_commodity_id, store_id desc").Find(&commodities).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil && err != RecordNotFound {
|
|
|
|
logger.Error("查询无库存列表失败", logger.Field("err", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-19 02:06:02 +00:00
|
|
|
if len(m.StoreId) != 1 || (len(m.StoreId) == 1 && m.IsExport == 1) { // 查询所有门店数据
|
2024-07-16 10:30:46 +00:00
|
|
|
// 剔除商品id重复的数据
|
|
|
|
var trimCommodities []ErpStock
|
|
|
|
tempCommodityMap := make(map[uint32]ErpStock)
|
|
|
|
for i, item := range commodities {
|
|
|
|
if _, found := tempCommodityMap[item.ErpCommodityId]; found {
|
|
|
|
commodities[i].DecisionStoreId = append(commodities[i].DecisionStoreId, item.StoreId)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if len(m.StoreId) != 0 {
|
|
|
|
commodities[i].StoreId = 0
|
|
|
|
}
|
|
|
|
commodities[i].DecisionStoreId = append(commodities[i].DecisionStoreId, item.StoreId)
|
|
|
|
tempCommodityMap[item.ErpCommodityId] = commodities[i]
|
|
|
|
trimCommodities = append(trimCommodities, commodities[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SortStockCommodities(trimCommodities)
|
|
|
|
|
|
|
|
// 分页
|
|
|
|
resp.Total = len(trimCommodities)
|
|
|
|
commodities = nil
|
|
|
|
startIdx := (resp.PageIndex - 1) * resp.PageSize
|
|
|
|
endIdx := resp.PageIndex * resp.PageSize
|
|
|
|
|
|
|
|
// 确保不超出索引范围
|
|
|
|
if startIdx >= len(trimCommodities) {
|
|
|
|
commodities = []ErpStock{}
|
|
|
|
} else if endIdx > len(trimCommodities) {
|
|
|
|
commodities = trimCommodities[startIdx:]
|
|
|
|
} else {
|
|
|
|
commodities = trimCommodities[startIdx:endIdx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:33:36 +00:00
|
|
|
var reportList []DecisionReportData
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ch := make(chan DecisionReportData, len(commodities))
|
|
|
|
|
|
|
|
for _, item := range commodities {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(item ErpStock) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var reportData DecisionReportData
|
|
|
|
reportData.CommodityId = item.ErpCommodityId
|
|
|
|
reportData.CommodityName = item.ErpCommodityName
|
2024-06-20 06:32:03 +00:00
|
|
|
reportData.CommoditySerialNumber = item.CommoditySerialNumber
|
2024-04-25 09:33:36 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
// 发送结果到通道
|
|
|
|
ch <- reportData
|
|
|
|
}(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 从通道中接收结果
|
2024-05-31 09:51:41 +00:00
|
|
|
commodityMap := make(map[uint32]DecisionReportData)
|
2024-04-25 09:33:36 +00:00
|
|
|
for data := range ch {
|
2024-05-31 09:51:41 +00:00
|
|
|
if existing, found := commodityMap[data.CommodityId]; found {
|
|
|
|
// Merge data if the commodity already exists in the map
|
|
|
|
existing.BeginStock += data.BeginStock
|
|
|
|
existing.BeginAmount += data.BeginAmount
|
|
|
|
existing.PurchaseStock += data.PurchaseStock
|
|
|
|
existing.PurchaseReturn += data.PurchaseReturn
|
|
|
|
existing.OrderSale += data.OrderSale
|
|
|
|
existing.OrderReject += data.OrderReject
|
|
|
|
existing.AllotIn += data.AllotIn
|
|
|
|
existing.AllotWaitIn += data.AllotWaitIn
|
|
|
|
existing.AllotOut += data.AllotOut
|
|
|
|
existing.AllotWaitOut += data.AllotWaitOut
|
|
|
|
existing.ProductIn += data.ProductIn
|
|
|
|
existing.SystemOut += data.SystemOut
|
|
|
|
existing.CheckIn += data.CheckIn
|
|
|
|
existing.CheckOut += data.CheckOut
|
|
|
|
existing.EndStock += data.EndStock
|
|
|
|
existing.EndAmount += data.EndAmount
|
|
|
|
|
|
|
|
commodityMap[data.CommodityId] = existing
|
|
|
|
} else {
|
|
|
|
// If not found, add new entry to the map
|
|
|
|
commodityMap[data.CommodityId] = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, data := range commodityMap {
|
2024-04-25 09:33:36 +00:00
|
|
|
reportList = append(reportList, data)
|
|
|
|
}
|
|
|
|
|
2024-05-31 09:51:41 +00:00
|
|
|
// 排序规则:商品编号小
|
2024-06-20 06:32:03 +00:00
|
|
|
SortReportByDecisionCommodities(reportList)
|
2024-05-31 09:51:41 +00:00
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var sumData DecisionSumData
|
|
|
|
sumData, err = getSumDecisionReportData(m)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("查询进销存汇总数据报错", logger.Field("err", err))
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
resp.SumData = sumData
|
|
|
|
|
|
|
|
if m.IsExport == 1 {
|
2024-07-16 10:30:46 +00:00
|
|
|
resp.Total = len(reportList)
|
2024-05-23 07:30:20 +00:00
|
|
|
resp.List = reportList
|
2024-04-25 09:33:36 +00:00
|
|
|
resp.ExportUrl, err = reportDecisionExport(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp.Total = 0
|
|
|
|
resp.List = nil
|
|
|
|
resp.SumData = DecisionSumData{}
|
2024-05-23 07:30:20 +00:00
|
|
|
} else {
|
2024-07-16 10:30:46 +00:00
|
|
|
if len(m.StoreId) == 1 {
|
|
|
|
resp.Total = int(count)
|
|
|
|
resp.List = reportList
|
2024-05-23 07:30:20 +00:00
|
|
|
} else {
|
2024-07-16 10:30:46 +00:00
|
|
|
resp.List = reportList
|
2024-05-23 07:30:20 +00:00
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询期初库存、期初金额
|
|
|
|
func getSystemStartCount(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("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount").
|
|
|
|
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 begin_stock, SUM(wholesale_price) as begin_amount").
|
|
|
|
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 begin_stock, SUM(wholesale_price) as begin_amount").
|
|
|
|
Where("storage_type = ? and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询采购进货数量
|
|
|
|
func getPurchaseStockCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_purchase_inventory")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_purchase_inventory.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_purchase_inventory.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock").
|
|
|
|
Joins("JOIN erp_purchase_order ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id").
|
|
|
|
Where("erp_purchase_order.store_id in (?) and erp_purchase_inventory.erp_commodity_id = ? and "+
|
|
|
|
"erp_purchase_inventory.purchase_type = ?", stock.DecisionStoreId, stock.ErpCommodityId, ErpProcureOrder).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock").
|
|
|
|
Joins("JOIN erp_purchase_order ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id").
|
|
|
|
Where("erp_purchase_order.store_id = ? and erp_purchase_inventory.erp_commodity_id = ? and "+
|
|
|
|
"erp_purchase_inventory.purchase_type = ?", stock.StoreId, stock.ErpCommodityId, ErpProcureOrder).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock").
|
|
|
|
Joins("JOIN erp_purchase_order ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id").
|
|
|
|
Where("erp_purchase_inventory.erp_commodity_id = ? and "+
|
|
|
|
"erp_purchase_inventory.purchase_type = ?", stock.ErpCommodityId, ErpProcureOrder).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询采购退货数量
|
|
|
|
func getPurchaseReturnCount(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("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as purchase_return").Where("state = ? and store_id in (?) and "+
|
|
|
|
"erp_commodity_id = ?", PurchaseReturn, stock.DecisionStoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as purchase_return").Where("state = ? and store_id = ? and "+
|
|
|
|
"erp_commodity_id = ?", PurchaseReturn, stock.StoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as purchase_return").Where("state = ? and "+
|
|
|
|
"erp_commodity_id = ?", PurchaseReturn, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询零售销售数量
|
|
|
|
func getSaleOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
2024-06-05 02:50:15 +00:00
|
|
|
qs := orm.Eloquent.Debug().Table("erp_order_commodity").
|
|
|
|
Joins("JOIN erp_order ON erp_order_commodity.erp_order_id = erp_order.id")
|
2024-04-25 09:33:36 +00:00
|
|
|
|
2024-06-05 02:50:15 +00:00
|
|
|
if req.StartTime != "" { // 审核开始时间
|
2024-04-25 09:33:36 +00:00
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
2024-06-05 02:50:15 +00:00
|
|
|
logger.Errorf("err:", err)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
2024-06-05 02:50:15 +00:00
|
|
|
qs = qs.Where("erp_order.audit_time > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
2024-06-05 02:50:15 +00:00
|
|
|
if req.EndTime != "" { // 审核结束时间
|
2024-04-25 09:33:36 +00:00
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
2024-06-05 02:50:15 +00:00
|
|
|
logger.Errorf("err:", err)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_order.audit_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("sum(erp_order_commodity.count) as order_sale").Where("erp_order.retail_type = ? and erp_order.state = ? "+
|
|
|
|
"and erp_order.store_id in (?) and erp_order.pay_status = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?",
|
|
|
|
RetailTypeSale, ErpOrderStateAudited, stock.DecisionStoreId, HavePaid, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("sum(erp_order_commodity.count) as order_sale").Where("erp_order.retail_type = ? and erp_order.state = ? "+
|
|
|
|
"and erp_order.store_id = ? and erp_order.pay_status = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?",
|
|
|
|
RetailTypeSale, ErpOrderStateAudited, stock.StoreId, HavePaid, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("sum(erp_order_commodity.count) as order_sale").Where("erp_order.retail_type = ? and erp_order.state = ? "+
|
|
|
|
"and erp_order.pay_status = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?",
|
|
|
|
RetailTypeSale, ErpOrderStateAudited, HavePaid, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询零售退货数量
|
|
|
|
func getSaleReturnCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_order_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
qs = qs.Where("erp_order.audit_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_order.audit_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_order_commodity.count) AS order_reject").
|
|
|
|
Joins("JOIN erp_order ON erp_order.id = erp_order_commodity.erp_order_id").
|
|
|
|
Where("erp_order.retail_type = ? and erp_order.store_id in (?) and erp_order.state = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?", RetailTypeRejected, stock.DecisionStoreId, ErpOrderStateAudited,
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_order_commodity.count) AS order_reject").
|
|
|
|
Joins("JOIN erp_order ON erp_order.id = erp_order_commodity.erp_order_id").
|
|
|
|
Where("erp_order.retail_type = ? and erp_order.store_id = ? and erp_order.state = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?", RetailTypeRejected, stock.StoreId, ErpOrderStateAudited,
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_order_commodity.count) AS order_reject").
|
|
|
|
Joins("JOIN erp_order ON erp_order.id = erp_order_commodity.erp_order_id").
|
|
|
|
Where("erp_order.retail_type = ? and erp_order.state = ? "+
|
|
|
|
"and erp_order_commodity.erp_commodity_id = ?", RetailTypeRejected, ErpOrderStateAudited,
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询产品入库数量:产品入库
|
|
|
|
func getProductCount(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("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as product_in").Where("storage_type = ? and store_id in (?) "+
|
|
|
|
"and erp_commodity_id = ?", ProductInventory, stock.DecisionStoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as product_in").Where("storage_type = ? and store_id = ? "+
|
|
|
|
"and erp_commodity_id = ?", ProductInventory, stock.StoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as product_in").Where("storage_type = ? "+
|
|
|
|
"and erp_commodity_id = ?", ProductInventory, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询盘点入库数量
|
|
|
|
func getChangeAddCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_change_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_change_order.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_change_order.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_in").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? and erp_inventory_change_order.store_id in (?) "+
|
|
|
|
"and erp_inventory_change_order.state = ? "+"and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
AddChangeOrder, stock.DecisionStoreId, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_in").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? and erp_inventory_change_order.store_id = ? "+
|
|
|
|
"and erp_inventory_change_order.state = ? "+"and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
AddChangeOrder, stock.StoreId, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_in").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? "+
|
|
|
|
"and erp_inventory_change_order.state = ? "+"and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
AddChangeOrder, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询盘点出库数量
|
|
|
|
func getChangeReduceCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_change_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_change_order.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_change_order.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_out").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? and erp_inventory_change_order.store_id in (?) "+
|
|
|
|
"and erp_inventory_change_order.state = ? and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
ReduceChangeOrder, stock.DecisionStoreId, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_out").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? and erp_inventory_change_order.store_id = ? "+
|
|
|
|
"and erp_inventory_change_order.state = ? and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
ReduceChangeOrder, stock.StoreId, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_change_commodity.count) AS check_out").
|
|
|
|
Joins("JOIN erp_inventory_change_order "+
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id").
|
|
|
|
Where("erp_inventory_change_order.change_type = ? "+
|
|
|
|
"and erp_inventory_change_order.state = ? and erp_inventory_change_commodity.commodity_id = ?",
|
|
|
|
ReduceChangeOrder, ErpInventoryChangeOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询系统出库数据:系统出库
|
|
|
|
func getSystemOutCount(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("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as system_out").Where("state = ? and store_id in (?) "+
|
|
|
|
"and erp_commodity_id = ?", SystemOut, stock.DecisionStoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as system_out").Where("state = ? and store_id = ? "+
|
|
|
|
"and erp_commodity_id = ?", SystemOut, stock.StoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as system_out").Where("state = ? "+
|
|
|
|
"and erp_commodity_id = ?", SystemOut, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询在途库存(入库)数量
|
|
|
|
func getAllotWaitInCount(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("updated_at > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("updated_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as allot_wait_in").Where("state = ? and store_id in (?) "+
|
|
|
|
"and erp_commodity_id = ?", InAllot, stock.DecisionStoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as allot_wait_in").Where("state = ? and store_id = ? "+
|
|
|
|
"and erp_commodity_id = ?", InAllot, stock.StoreId, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as allot_wait_in").Where("state = ? "+
|
|
|
|
"and erp_commodity_id = ?", InAllot, stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询在途库存(出库)数量
|
|
|
|
func getAllotWaitOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotOutCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotOutCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_wait_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.deliver_store_id in (?) and erp_inventory_allot_order.state in (?) "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.DecisionStoreId, []uint32{ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive}, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_wait_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.deliver_store_id = ? and erp_inventory_allot_order.state in (?) "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.StoreId, []uint32{ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive}, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_wait_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.state in (?) "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
[]uint32{ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive}, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询调拨入库数量
|
|
|
|
func getAllotInCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getChangeAddCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_in").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.receive_store_id in (?) and erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.DecisionStoreId, ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_in").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.receive_store_id = ? and erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.StoreId, ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_in").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询调拨出库数量
|
|
|
|
func getAllotOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotOutCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at > ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotOutCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.deliver_store_id in (?) and erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.DecisionStoreId, ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.deliver_store_id = ? and erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
stock.StoreId, ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(erp_inventory_allot_commodity.count) AS allot_out").
|
|
|
|
Joins("JOIN erp_inventory_allot_order "+
|
|
|
|
"ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id").
|
|
|
|
Where("erp_inventory_allot_order.state = ? "+
|
|
|
|
"and erp_inventory_allot_commodity.commodity_id = ?",
|
|
|
|
ErpInventoryAllotOrderFinished, stock.ErpCommodityId).
|
|
|
|
Find(&reportData).Error
|
|
|
|
}
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询期末库存、期末金额
|
|
|
|
func getSystemEndCount(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("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSystemStartCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
2024-04-25 09:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
var err error
|
|
|
|
if stock.StoreId != 0 {
|
|
|
|
if len(stock.DecisionStoreId) > 1 {
|
|
|
|
err = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
|
|
|
Where("state in (?) and store_id in (?) and erp_commodity_id = ?", []uint32{InStock, InAllot}, stock.DecisionStoreId,
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
|
|
|
Where("state in (?) and store_id = ? and erp_commodity_id = ?", []uint32{InStock, InAllot}, stock.StoreId,
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount").
|
|
|
|
Where("state in (?) and erp_commodity_id = ?", []uint32{InStock, InAllot},
|
|
|
|
stock.ErpCommodityId).Find(&reportData).Error
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询库存汇总数据:期初库存、期初金额、采购退货数量、产品入库、系统出库、在途库存(入库)数量、期末库存、期末金额
|
|
|
|
func getSumStockData(req *ErpDecisionReportReq) (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("getStockData err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
qs = qs.Where("first_stock_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getStockData err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
qs = qs.Where("first_stock_time <= ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.StoreId) > 0 {
|
|
|
|
if len(req.StoreId) == 1 {
|
|
|
|
qs = qs.Where("store_id = ?", req.StoreId[0])
|
|
|
|
} else {
|
|
|
|
qs = qs.Where("store_id IN (?)", req.StoreId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS begin_stock,
|
|
|
|
SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS begin_amount,
|
|
|
|
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 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 IN (?) THEN count ELSE 0 END) AS end_stock,
|
|
|
|
SUM(CASE WHEN state IN (?) THEN wholesale_price ELSE 0 END) AS end_amount
|
|
|
|
`, SystemInventory, SystemInventory, PurchaseReturn,
|
|
|
|
ProductInventory, SystemOut, InAllot,
|
|
|
|
[]uint32{InStock, InAllot}, []uint32{InStock, InAllot})
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getStockData err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询采购进货数量
|
|
|
|
func getSumPurchaseData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_purchase_inventory")
|
|
|
|
|
|
|
|
// 进行条件查询
|
|
|
|
if req.StartTime != "" { // 出入库开始时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_purchase_inventory.created_at > ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" { // 出入库结束时间
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getPurchaseCount err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_purchase_inventory.created_at <= ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.StoreId) > 0 {
|
|
|
|
if len(req.StoreId) == 1 {
|
|
|
|
qs = qs.Where("store_id = ?", req.StoreId[0])
|
|
|
|
} else {
|
|
|
|
qs = qs.Where("store_id IN (?)", req.StoreId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock").
|
|
|
|
Joins("JOIN erp_purchase_order ON erp_purchase_order.id = erp_purchase_inventory.erp_purchase_order_id").
|
|
|
|
Where("erp_purchase_inventory.purchase_type = ?", ErpProcureOrder).
|
|
|
|
Find(&reportData).Error
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询零售汇总数据:零售销售数量、零售退货数量
|
|
|
|
func getSumSalesData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_order_commodity").
|
|
|
|
Joins("JOIN erp_order ON erp_order_commodity.erp_order_id = erp_order.id")
|
|
|
|
|
|
|
|
if req.StartTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSalesAndReturns err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
qs = qs.Where("erp_order.audit_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getSalesAndReturns err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_order.audit_time <= ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.StoreId) > 0 {
|
|
|
|
if len(req.StoreId) == 1 {
|
|
|
|
qs = qs.Where("store_id = ?", req.StoreId[0])
|
|
|
|
} else {
|
|
|
|
qs = qs.Where("store_id IN (?)", req.StoreId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN erp_order.retail_type = ? AND erp_order.state = ? AND erp_order.pay_status = ? THEN erp_order_commodity.count ELSE 0 END) AS order_sale,
|
|
|
|
SUM(CASE WHEN erp_order.retail_type = ? AND erp_order.state = ? THEN erp_order_commodity.count ELSE 0 END) AS order_reject
|
|
|
|
`, RetailTypeSale, ErpOrderStateAudited, HavePaid, RetailTypeRejected, ErpOrderStateAudited)
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
2024-04-25 09:33:36 +00:00
|
|
|
if err != nil {
|
2024-07-16 10:30:46 +00:00
|
|
|
logger.Errorf("getSalesAndReturns err:", err)
|
2024-04-25 09:33:36 +00:00
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
2024-07-16 10:30:46 +00:00
|
|
|
// 查询盘点汇总数据:盘点入库数量、盘点出库数量
|
|
|
|
func getSumInventoryData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_change_commodity").
|
|
|
|
Joins("JOIN erp_inventory_change_order " +
|
|
|
|
"ON erp_inventory_change_order.id = erp_inventory_change_commodity.change_order_id")
|
|
|
|
|
|
|
|
if req.StartTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getInventoryChanges err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
qs = qs.Where("erp_inventory_change_order.audit_time > ?", parse)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getInventoryChanges err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_change_order.audit_time <= ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.StoreId) > 0 {
|
|
|
|
if len(req.StoreId) == 1 {
|
|
|
|
qs = qs.Where("store_id = ?", req.StoreId[0])
|
|
|
|
} else {
|
|
|
|
qs = qs.Where("store_id IN (?)", req.StoreId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN erp_inventory_change_order.change_type = ? AND erp_inventory_change_order.state = ?
|
|
|
|
THEN erp_inventory_change_commodity.count ELSE 0 END) AS check_in,
|
|
|
|
SUM(CASE WHEN erp_inventory_change_order.change_type = ? AND erp_inventory_change_order.state = ?
|
|
|
|
THEN erp_inventory_change_commodity.count ELSE 0 END) AS check_out
|
|
|
|
`, AddChangeOrder, ErpInventoryChangeOrderFinished, ReduceChangeOrder, ErpInventoryChangeOrderFinished)
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getInventoryChanges err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询调拨汇总数据:在途库存(出库)数量、调拨入库数量、调拨出库数量
|
|
|
|
func getSumAllotData(req *ErpDecisionReportReq) (DecisionReportData, error) {
|
|
|
|
var reportData DecisionReportData
|
|
|
|
qs := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity").
|
|
|
|
Joins("JOIN erp_inventory_allot_order ON erp_inventory_allot_order.id = erp_inventory_allot_commodity.allot_order_id")
|
|
|
|
|
|
|
|
// Apply time filters if provided
|
|
|
|
if req.StartTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.StartTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotCounts err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at > ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.EndTime != "" {
|
|
|
|
parse, err := time.Parse(QueryTimeFormat, req.EndTime)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotCounts err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
2024-07-19 02:06:02 +00:00
|
|
|
qs = qs.Where("erp_inventory_allot_commodity.created_at <= ?", parse)
|
2024-07-16 10:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply store_id filter if provided
|
|
|
|
if len(req.StoreId) > 1 {
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state IN (?, ?) AND erp_inventory_allot_order.deliver_store_id IN (?) THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_wait_out,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.receive_store_id IN (?) THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_in,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.deliver_store_id IN (?) THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_out
|
|
|
|
`, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId,
|
|
|
|
ErpInventoryAllotOrderFinished, req.StoreId,
|
|
|
|
ErpInventoryAllotOrderFinished, req.StoreId)
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotCounts err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
} else if len(req.StoreId) == 1 {
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state IN (?, ?) AND erp_inventory_allot_order.deliver_store_id = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_wait_out,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.receive_store_id = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_in,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.deliver_store_id = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_out
|
|
|
|
`, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId[0],
|
|
|
|
ErpInventoryAllotOrderFinished, req.StoreId[0],
|
|
|
|
ErpInventoryAllotOrderFinished, req.StoreId[0])
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotCounts err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If StoreId is empty, query without deliver_store_id and receive_store_id conditions
|
|
|
|
subQuery := qs.Select(`
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state IN (?, ?) THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_wait_out,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_in,
|
|
|
|
SUM(CASE WHEN erp_inventory_allot_order.state = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_out
|
|
|
|
`, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive,
|
|
|
|
ErpInventoryAllotOrderFinished, ErpInventoryAllotOrderFinished)
|
|
|
|
|
|
|
|
err := subQuery.Scan(&reportData).Error
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("getAllotCounts err:", err)
|
|
|
|
return DecisionReportData{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reportData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询进销存报表汇总数据
|
|
|
|
func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error) {
|
|
|
|
var sumData DecisionSumData
|
|
|
|
|
|
|
|
sumStockData, err := getSumStockData(req)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionSumData{}, err
|
|
|
|
}
|
|
|
|
sumData.TotalBeginStock = sumStockData.BeginStock
|
|
|
|
sumData.TotalBeginAmount = sumStockData.BeginAmount
|
|
|
|
sumData.TotalPurchaseReturn = sumStockData.PurchaseReturn
|
|
|
|
sumData.TotalAllotWaitIn = sumStockData.AllotWaitIn
|
|
|
|
sumData.TotalProductIn = sumStockData.ProductIn
|
|
|
|
sumData.TotalSystemOut = sumStockData.SystemOut
|
|
|
|
sumData.TotalEndStock = sumStockData.EndStock
|
|
|
|
sumData.TotalEndAmount = sumStockData.EndAmount
|
|
|
|
|
|
|
|
sumPurchaseData, err := getSumPurchaseData(req)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionSumData{}, err
|
|
|
|
}
|
|
|
|
sumData.TotalPurchaseStock = sumPurchaseData.PurchaseStock
|
|
|
|
|
|
|
|
sumSalesData, err := getSumSalesData(req)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionSumData{}, err
|
|
|
|
}
|
|
|
|
sumData.TotalOrderSale = sumSalesData.OrderSale
|
|
|
|
sumData.TotalOrderReject = sumSalesData.OrderReject
|
|
|
|
|
|
|
|
sumInventoryData, err := getSumInventoryData(req)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionSumData{}, err
|
|
|
|
}
|
|
|
|
sumData.TotalCheckIn = sumInventoryData.CheckIn
|
|
|
|
sumData.TotalCheckOut = sumInventoryData.CheckOut
|
|
|
|
|
|
|
|
sumAllotData, err := getSumAllotData(req)
|
|
|
|
if err != nil {
|
|
|
|
return DecisionSumData{}, err
|
|
|
|
}
|
|
|
|
sumData.TotalAllotWaitOut = sumAllotData.AllotWaitOut
|
|
|
|
sumData.TotalAllotIn = sumAllotData.AllotIn
|
|
|
|
sumData.TotalAllotOut = sumAllotData.AllotOut
|
|
|
|
|
|
|
|
roundDecisionSumData(&sumData)
|
|
|
|
|
|
|
|
return sumData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// roundFloat64 rounds a float64 number to the specified decimal places.
|
|
|
|
func roundFloat64(num float64, decimalPlaces int) float64 {
|
|
|
|
precision := math.Pow(10, float64(decimalPlaces))
|
|
|
|
return math.Round(num*precision) / precision
|
|
|
|
}
|
|
|
|
|
|
|
|
// roundDecisionSumData rounds all float64 fields in DecisionSumData to 2 decimal places.
|
|
|
|
func roundDecisionSumData(data *DecisionSumData) {
|
|
|
|
data.TotalBeginAmount = roundFloat64(data.TotalBeginAmount, 2)
|
|
|
|
data.TotalEndAmount = roundFloat64(data.TotalEndAmount, 2)
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:33:36 +00:00
|
|
|
// reportDecisionExport 进销存报表导出excel
|
|
|
|
func reportDecisionExport(req *ErpDecisionReportResp) (string, error) {
|
|
|
|
file := excelize.NewFile()
|
|
|
|
fSheet := "Sheet1"
|
|
|
|
|
|
|
|
url := ExportUrl
|
|
|
|
fileName := time.Now().Format(TimeFormat) + "进销存报表" + ".xlsx"
|
|
|
|
fmt.Println("url fileName:", url+fileName)
|
|
|
|
|
|
|
|
// 组合标题栏数据
|
|
|
|
title := []interface{}{"商品名称", "商品分类", "期初库存", "期初金额", "采购进货", "采购退货", "零售销售", "零售退货", "调拨入库",
|
|
|
|
"在途库存(入库)", "调拨出库", "在途库存(出库)", "产品入库", "系统出库", "盘点入库", "盘点出库", "期末数量", "期末金额"}
|
|
|
|
for i, _ := range title {
|
|
|
|
cell, _ := excelize.CoordinatesToCellName(1+i, 1)
|
|
|
|
err := file.SetCellValue(fSheet, cell, title[i])
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("file set value err:", logger.Field("err", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var row1 []interface{}
|
|
|
|
nExcelStartRow := 0
|
|
|
|
for _, reportData := range req.List {
|
|
|
|
row1 = []interface{}{
|
|
|
|
reportData.CommodityName, // 商品名称
|
|
|
|
reportData.CategoryName, // 商品分类
|
|
|
|
reportData.BeginStock,
|
|
|
|
reportData.BeginAmount,
|
|
|
|
reportData.PurchaseStock,
|
|
|
|
reportData.PurchaseReturn,
|
|
|
|
reportData.OrderSale,
|
|
|
|
reportData.OrderReject,
|
|
|
|
reportData.AllotIn,
|
|
|
|
reportData.AllotWaitIn,
|
|
|
|
reportData.AllotOut,
|
|
|
|
reportData.AllotWaitOut,
|
|
|
|
reportData.ProductIn,
|
|
|
|
reportData.SystemOut,
|
|
|
|
reportData.CheckIn,
|
|
|
|
reportData.CheckOut,
|
|
|
|
reportData.EndStock,
|
|
|
|
reportData.EndAmount,
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, _ := range row1 {
|
|
|
|
cell, _ := excelize.CoordinatesToCellName(1+j, nExcelStartRow+2)
|
|
|
|
err := file.SetCellValue(fSheet, cell, row1[j])
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("file set value err:", logger.Field("err", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nExcelStartRow++
|
|
|
|
}
|
|
|
|
|
|
|
|
totalData := "汇总 记录数:" + strconv.FormatInt(int64(req.Total), 10)
|
|
|
|
end := []interface{}{totalData, "",
|
|
|
|
req.SumData.TotalBeginStock,
|
|
|
|
req.SumData.TotalBeginAmount,
|
|
|
|
req.SumData.TotalPurchaseStock,
|
|
|
|
req.SumData.TotalPurchaseReturn,
|
|
|
|
req.SumData.TotalOrderSale,
|
|
|
|
req.SumData.TotalOrderReject,
|
|
|
|
req.SumData.TotalAllotIn,
|
|
|
|
req.SumData.TotalAllotWaitIn,
|
|
|
|
req.SumData.TotalAllotOut,
|
|
|
|
req.SumData.TotalAllotWaitOut,
|
|
|
|
req.SumData.TotalProductIn,
|
|
|
|
req.SumData.TotalSystemOut,
|
|
|
|
req.SumData.TotalCheckIn,
|
|
|
|
req.SumData.TotalCheckOut,
|
|
|
|
req.SumData.TotalEndStock,
|
|
|
|
req.SumData.TotalEndAmount}
|
|
|
|
for i, _ := range end {
|
|
|
|
cell, _ := excelize.CoordinatesToCellName(1+i, nExcelStartRow+2)
|
|
|
|
err := file.SetCellValue(fSheet, cell, end[i])
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("file set value err:", logger.Field("err", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置所有单元格的样式: 居中、加边框
|
|
|
|
style, _ := file.NewStyle(`{"alignment":{"horizontal":"center","vertical":"center"},
|
|
|
|
"border":[{"type":"left","color":"000000","style":1},
|
|
|
|
{"type":"top","color":"000000","style":1},
|
|
|
|
{"type":"right","color":"000000","style":1},
|
|
|
|
{"type":"bottom","color":"000000","style":1}]}`)
|
|
|
|
|
|
|
|
//设置单元格高度
|
|
|
|
file.SetRowHeight("Sheet1", 1, 20)
|
|
|
|
|
|
|
|
// 设置单元格大小
|
|
|
|
file.SetColWidth("Sheet1", "A", "A", 15)
|
|
|
|
|
|
|
|
endRow := fmt.Sprintf("R"+"%d", nExcelStartRow+2)
|
|
|
|
// 应用样式到整个表格
|
|
|
|
_ = file.SetCellStyle("Sheet1", "A1", endRow, style)
|
|
|
|
|
|
|
|
fmt.Println("save fileName:", config.ExportConfig.Path+fileName)
|
|
|
|
if err := file.SaveAs(config.ExportConfig.Path + fileName); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return url + fileName, nil
|
|
|
|
}
|