diff --git a/app/admin/models/decision.go b/app/admin/models/decision.go index 442910f..306bcba 100644 --- a/app/admin/models/decision.go +++ b/app/admin/models/decision.go @@ -39,23 +39,36 @@ type ErpDecisionReportResp struct { // 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"` // 产品入库 - TotalSystemIn uint32 `json:"total_system_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"` // 期末金额 + TotalBeginStock uint32 `json:"total_begin_stock"` // 期初库存 + TotalBeginAmount float64 `json:"total_begin_amount"` // 期初金额 + TotalPurchaseStock uint32 `json:"total_purchase_stock"` // 采购进货 + TotalPurchaseAmount float64 `json:"total_purchase_amount"` // 采购进货金额 + TotalPurchaseReturn uint32 `json:"total_purchase_return"` // 采购退货 + TotalReturnAmount float64 `json:"total_return_amount"` // 采购退货金额 + TotalOrderSale uint32 `json:"total_order_sale"` // 零售销售 + TotalSaleAmount float64 `json:"total_sale_amount"` // 零售销售金额 + TotalOrderReject uint32 `json:"total_order_reject"` // 零售退货 + TotalRejectAmount float64 `json:"total_reject_amount"` // 零售退货金额 + TotalAllotIn uint32 `json:"total_allot_in"` // 调拨入库 + TotalAllotInAmount float64 `json:"total_allot_in_amount"` // 调拨入库金额 + TotalAllotWaitIn uint32 `json:"total_allot_wait_in"` // 在途库存(入库) + TotalWaitInAmount float64 `json:"total_wait_in_amount"` // 在途库存(入库)金额 + TotalAllotOut uint32 `json:"total_allot_out"` // 调拨出库 + TotalAllotOutAmount float64 `json:"total_allot_out_amount"` // 调拨出库金额 + TotalAllotWaitOut uint32 `json:"total_allot_wait_out"` // 在途库存(出库) + TotalWaitOutAmount float64 `json:"total_wait_out_amount"` // 在途库存(出库)金额 + TotalProductIn uint32 `json:"total_product_in"` // 产品入库 + TotalProductInAmount float64 `json:"total_product_in_amount"` // 产品入库金额 + TotalSystemIn uint32 `json:"total_system_in"` // 系统入库 + TotalSystemInAmount float64 `json:"total_system_in_amount"` // 系统入库金额 + TotalSystemOut uint32 `json:"total_system_out"` // 系统出库 + TotalSystemOutAmount float64 `json:"total_system_out_amount"` // 系统出库金额 + TotalCheckIn uint32 `json:"total_check_in"` // 盘点入库 + TotalCheckInAmount float64 `json:"total_check_in_amount"` // 盘点入库金额 + TotalCheckOut uint32 `json:"total_check_out"` // 盘点出库 + TotalCheckOutAmount float64 `json:"total_check_out_amount"` // 盘点出库金额 + TotalEndStock uint32 `json:"total_end_stock"` // 期末数量 + TotalEndAmount float64 `json:"total_end_amount"` // 期末金额 } // DecisionReportData 进销存报表数据 @@ -68,18 +81,31 @@ type DecisionReportData struct { BeginStock uint32 `json:"begin_stock"` // 期初库存 BeginAmount float64 `json:"begin_amount"` // 期初金额 PurchaseStock uint32 `json:"purchase_stock"` // 采购进货 + PurchaseStockAmount float64 `json:"purchase_stock_amount"` // 采购进货金额 PurchaseReturn uint32 `json:"purchase_return"` // 采购退货 + PurchaseReturnAmount float64 `json:"purchase_return_amount"` // 采购退货金额 OrderSale uint32 `json:"order_sale"` // 零售销售 + OrderSaleAmount float64 `json:"order_sale_amount"` // 零售销售金额 OrderReject uint32 `json:"order_reject"` // 零售退货 + OrderRejectAmount float64 `json:"order_reject_amount"` // 零售退货金额 AllotIn uint32 `json:"allot_in"` // 调拨入库 + AllotInAmount float64 `json:"allot_in_amount"` // 调拨入库金额 AllotWaitIn uint32 `json:"allot_wait_in"` // 在途库存(入库) + AllotWaitInAmount float64 `json:"allot_wait_in_amount"` // 在途库存(入库)金额 AllotOut uint32 `json:"allot_out"` // 调拨出库 + AllotOutAmount float64 `json:"allot_out_amount"` // 调拨出库金额 AllotWaitOut uint32 `json:"allot_wait_out"` // 在途库存(出库) + AllotWaitOutAmount float64 `json:"allot_wait_out_amount"` // 在途库存(出库)金额 ProductIn uint32 `json:"product_in"` // 产品入库 + ProductInAmount float64 `json:"product_in_amount"` // 产品入库金额 SystemIn uint32 `json:"system_in"` // 系统入库 + SystemInAmount float64 `json:"system_in_amount"` // 系统入库金额 SystemOut uint32 `json:"system_out"` // 系统出库 + SystemOutAmount float64 `json:"system_out_amount"` // 系统出库金额 CheckIn uint32 `json:"check_in"` // 盘点入库 + CheckInAmount float64 `json:"check_in_amount"` // 盘点入库金额 CheckOut uint32 `json:"check_out"` // 盘点出库 + CheckOutAmount float64 `json:"check_out_amount"` // 盘点出库金额 EndStock uint32 `json:"end_stock"` // 期末数量 EndAmount float64 `json:"end_amount"` // 期末金额 } @@ -224,6 +250,9 @@ func (m *ErpDecisionReportReq) DecisionReportList(c *gin.Context) (*ErpDecisionR go func(item ErpStock) { defer wg.Done() + if len(item.DecisionStoreId) == 0 { + item.DecisionStoreId = append(item.DecisionStoreId, item.StoreId) + } reportData, _ := calculateInventoryReport(m, item) reportData.CommodityId = item.ErpCommodityId reportData.CommodityName = item.ErpCommodityName @@ -311,7 +340,7 @@ func calculateInventoryReport(m *ErpDecisionReportReq, item ErpStock) (DecisionR var reportData DecisionReportData // Step 1: 查询期初库存和金额 - systemStartData, err := getSystemStartCount(m, item) + systemStartData, err := getSystemStartData(m, item) if err != nil { return DecisionReportData{}, err } @@ -422,6 +451,81 @@ func calculateInventoryReport(m *ErpDecisionReportReq, item ErpStock) (DecisionR return reportData, nil } +// 查询期初库存 +func getSystemStartData(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) { + if req.StartTime == "" || req.EndTime == "" { + return DecisionReportData{}, nil + } + + tempReq := req + tempReq.EndTime = time.Now().Format(QueryTimeFormat) + + var respData DecisionReportData + // 期末库存 + sumEndData, err := getSystemEndCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + + // 查询采购进货数量 + purchaseStockData, err := getPurchaseStockCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询采购退货数量 + purchaseReturnData, err := getPurchaseReturnCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询零售销售数量 + saleOutData, err := getSaleOutCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询零售退货数量 + saleReturnData, err := getSaleReturnCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询调拨入库数量 + allotInData, err := getAllotInCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询调拨出库数量 + allotOutData, err := getAllotOutCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询产品入库数量:产品入库 + productData, err := getProductCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询系统出库数据:系统出库 + systemOutData, err := getSystemOutCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询盘点入库数量 + changeAddData, err := getChangeAddCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + // 查询盘点出库数量 + changeReduceData, err := getChangeReduceCount(tempReq, stock) + if err != nil { + return DecisionReportData{}, err + } + + respData.BeginStock = sumEndData.EndStock + (saleOutData.OrderSale - saleReturnData.OrderReject) + + allotOutData.AllotOut + systemOutData.SystemOut + changeReduceData.CheckOut - + (purchaseStockData.PurchaseStock - purchaseReturnData.PurchaseReturn) - allotInData.AllotIn - + productData.ProductIn - changeAddData.CheckIn + + return respData, nil +} + // 查询期初库存和金额 func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReportData, error) { var reportData DecisionReportData @@ -443,13 +547,13 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep qs := orm.Eloquent.Debug().Table("erp_stock_commodity"). Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount"). Where("storage_type = ? and erp_commodity_id = ?", SystemInventory, stock.ErpCommodityId). - Where("created_at <= ?", previousDay) + Where("first_stock_time <= ?", previousDay) if stock.StoreId != 0 { if len(stock.DecisionStoreId) > 1 { qs = qs.Where("store_id in (?)", stock.DecisionStoreId) } else { - qs = qs.Where("store_id = ?", stock.DecisionStoreId) + qs = qs.Where("store_id = ?", stock.DecisionStoreId[0]) } } @@ -458,7 +562,7 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep return DecisionReportData{}, err } - // 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库) + // 获取开始时间前一天的所有出库数量(包括销售出库、系统出库、盘点出库、采购退货) var outStockData struct { SoldOut uint32 `json:"sold_out"` SoldOutAmount float64 `json:"sold_out_amount"` @@ -483,7 +587,7 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep if len(stock.DecisionStoreId) > 1 { es = es.Where("store_id in (?)", stock.DecisionStoreId) } else { - es = es.Where("store_id = ?", stock.DecisionStoreId) + es = es.Where("store_id = ?", stock.DecisionStoreId[0]) } } @@ -492,7 +596,7 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep return DecisionReportData{}, err } - // 获取开始时间前一天的所有入库数量(包括采购入库、产品入库和盘点入库) + // 获取开始时间前一天的所有入库数量(包括采购入库、产品入库、盘点入库) var inStockData struct { PurchaseIn uint32 `json:"purchase_in"` PurchaseInAmount float64 `json:"purchase_in_amount"` @@ -516,7 +620,7 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep if len(stock.DecisionStoreId) > 1 { ts = ts.Where("store_id in (?)", stock.DecisionStoreId) } else { - ts = ts.Where("store_id = ?", stock.DecisionStoreId) + ts = ts.Where("store_id = ?", stock.DecisionStoreId[0]) } } @@ -525,8 +629,62 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep return DecisionReportData{}, err } + // 查询调拨入库、调拨出库的数量和金额 + // 调拨入库数量和金额 + var allotInData struct { + AllotIn uint32 `json:"allot_in"` + AllotInAmount float64 `json:"allot_in_amount"` + } + allotInQuery := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity"). + Select("SUM(erp_inventory_allot_commodity.count) AS allot_in, "+ + "SUM(erp_inventory_allot_commodity.amount) AS allot_in_amount"). + 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). + Where("erp_inventory_allot_order.receive_time <= ?", previousDay) + + // 处理 `store_id` 条件 + if len(stock.DecisionStoreId) > 1 { + allotInQuery = allotInQuery.Where("erp_inventory_allot_order.receive_store_id IN (?)", stock.DecisionStoreId) + } else { + allotInQuery = allotInQuery.Where("erp_inventory_allot_order.receive_store_id = ?", stock.DecisionStoreId[0]) + } + + err = allotInQuery.Find(&allotInData).Error + if err != nil { + return DecisionReportData{}, err + } + + // 调拨出库数量和金额 + var allotOutData struct { + AllotOut uint32 `json:"allot_out"` + AllotOutAmount float64 `json:"allot_out_amount"` + } + allotOutQuery := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity"). + Select("SUM(erp_inventory_allot_commodity.count) AS allot_out, "+ + "SUM(erp_inventory_allot_commodity.amount) AS allot_out_amount"). + 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). + Where("erp_inventory_allot_order.receive_time <= ?", previousDay) + + // 处理 `store_id` 条件 + if len(stock.DecisionStoreId) > 1 { + allotOutQuery = allotOutQuery.Where("erp_inventory_allot_order.deliver_store_id IN (?)", stock.DecisionStoreId) + } else { + allotOutQuery = allotOutQuery.Where("erp_inventory_allot_order.deliver_store_id = ?", stock.DecisionStoreId[0]) + } + + err = allotOutQuery.Find(&allotOutData).Error + if err != nil { + return DecisionReportData{}, err + } + // 计算期初库存 - 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(allotOutData.AllotOut) + int64(allotInData.AllotIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn) if beginStock < 0 { beginStock = 0 // 确保库存不会为负 @@ -534,8 +692,9 @@ func getSystemStartCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRep // 计算期初金额 beginAmount := systemStartData.BeginAmount - outStockData.SoldOutAmount - - outStockData.SystemOutAmount - outStockData.CheckOutAmount + - inStockData.PurchaseInAmount + inStockData.ProductInAmount + inStockData.CheckInAmount + outStockData.SystemOutAmount - outStockData.CheckOutAmount - allotOutData.AllotOutAmount + + allotInData.AllotInAmount + inStockData.PurchaseInAmount + inStockData.ProductInAmount + + inStockData.CheckInAmount if beginAmount < 0 { beginAmount = 0 // 确保库存不会为负 } @@ -569,121 +728,138 @@ func getSystemEndCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionRepor var reportData DecisionReportData qs := orm.Eloquent.Debug().Table("erp_stock_commodity") - es := orm.Eloquent.Debug().Table("erp_stock_commodity") + //es := orm.Eloquent.Debug().Table("erp_stock_commodity") - if req.EndTime != "" { // 有结束时间 - endTime, err := time.Parse(QueryTimeFormat, req.EndTime) - if err != nil { - logger.Errorf("getSystemEndCount err:", err) - return DecisionReportData{}, err - } + //if req.EndTime != "" { // 有结束时间 + // endTime, err := time.Parse(QueryTimeFormat, req.EndTime) + // if err != nil { + // logger.Errorf("getSystemEndCount err:", err) + // return DecisionReportData{}, err + // } + // + // // 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库) + // var inStockData struct { + // SystemIn uint32 `json:"system_in"` + // SystemInAmount float64 `json:"system_in_amount"` + // PurchaseIn uint32 `json:"purchase_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, "+ + // "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 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 wholesale_price ELSE 0 END) AS product_in_amount, "+ + // "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("erp_commodity_id = ?", stock.ErpCommodityId) + // + // if stock.StoreId != 0 { + // if len(stock.DecisionStoreId) > 1 { + // qs = qs.Where("store_id in (?)", stock.DecisionStoreId) + // } else { + // qs = qs.Where("store_id = ?", stock.DecisionStoreId[0]) + // } + // } + // + // err = qs.Find(&inStockData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + // + // // 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库) + // var outStockData struct { + // SoldOut uint32 `json:"sold_out"` + // SoldOutAmount float64 `json:"sold_out_amount"` + // 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, "+ + // "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 wholesale_price ELSE 0 END) AS system_out_amount, "+ + // "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("state IN (?)", []int{SoldOut, SystemOut, CheckOut}). + // Where("erp_commodity_id = ?", stock.ErpCommodityId) + // + // if stock.StoreId != 0 { + // if len(stock.DecisionStoreId) > 1 { + // es = es.Where("store_id in (?)", stock.DecisionStoreId) + // } else { + // es = es.Where("store_id = ?", stock.DecisionStoreId[0]) + // } + // } + // err = es.Find(&outStockData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + // + // // 计算期末库存 + // endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + + // int64(inStockData.CheckIn) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - + // int64(outStockData.CheckOut) + // + // if endStock < 0 { + // endStock = 0 // 确保库存不会为负 + // } + // + // // 计算期末金额 + // 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 { + // // 如果没有结束时间,直接获取系统在库数量作为期末库存 + // qs = 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) + // + // if stock.StoreId != 0 { + // if len(stock.DecisionStoreId) > 1 { + // qs = qs.Where("store_id in (?)", stock.DecisionStoreId) + // } else { + // qs = qs.Where("store_id = ?", stock.DecisionStoreId[0]) + // } + // } + // err := qs.Find(&reportData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + //} - // 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库) - var inStockData struct { - SystemIn uint32 `json:"system_in"` - SystemInAmount float64 `json:"system_in_amount"` - PurchaseIn uint32 `json:"purchase_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, "+ - "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 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 wholesale_price ELSE 0 END) AS product_in_amount, "+ - "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("erp_commodity_id = ?", stock.ErpCommodityId) + // 直接获取系统在库数量作为期末库存 + qs = 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) - if stock.StoreId != 0 { - if len(stock.DecisionStoreId) > 1 { - qs = qs.Where("store_id in (?)", stock.DecisionStoreId) - } else { - qs = qs.Where("store_id = ?", stock.DecisionStoreId) - } - } - - err = qs.Find(&inStockData).Error - if err != nil { - return DecisionReportData{}, err - } - - // 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库) - var outStockData struct { - SoldOut uint32 `json:"sold_out"` - SoldOutAmount float64 `json:"sold_out_amount"` - 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, "+ - "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 wholesale_price ELSE 0 END) AS system_out_amount, "+ - "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("state IN (?)", []int{SoldOut, SystemOut, CheckOut}). - Where("erp_commodity_id = ?", stock.ErpCommodityId) - - if stock.StoreId != 0 { - if len(stock.DecisionStoreId) > 1 { - es = es.Where("store_id in (?)", stock.DecisionStoreId) - } else { - es = es.Where("store_id = ?", stock.DecisionStoreId) - } - } - err = es.Find(&outStockData).Error - if err != nil { - return DecisionReportData{}, err - } - - // 计算期末库存 - endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + - int64(inStockData.CheckIn) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - - int64(outStockData.CheckOut) - - if endStock < 0 { - endStock = 0 // 确保库存不会为负 - } - - // 计算期末金额 - 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 { - // 如果没有结束时间,直接获取系统在库数量作为期末库存 - qs = 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) - - if stock.StoreId != 0 { - if len(stock.DecisionStoreId) > 1 { - qs = qs.Where("store_id in (?)", stock.DecisionStoreId) - } else { - qs = qs.Where("store_id = ?", stock.DecisionStoreId) - } - } - err := qs.Find(&reportData).Error - if err != nil { - return DecisionReportData{}, err + if stock.StoreId != 0 { + if len(stock.DecisionStoreId) > 1 { + qs = qs.Where("store_id in (?)", stock.DecisionStoreId) + } else { + qs = qs.Where("store_id = ?", stock.DecisionStoreId[0]) } } + err := qs.Find(&reportData).Error + if err != nil { + return DecisionReportData{}, err + } return reportData, nil } @@ -1188,6 +1364,7 @@ func getSystemInCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReport return DecisionReportData{}, err } qs = qs.Where("created_at > ?", parse) + qs = qs.Where("first_stock_time > ?", parse) } if req.EndTime != "" { // 出入库结束时间 @@ -1197,6 +1374,7 @@ func getSystemInCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReport return DecisionReportData{}, err } qs = qs.Where("created_at <= ?", parse) + qs = qs.Where("first_stock_time <= ?", parse) } var err error @@ -1489,7 +1667,67 @@ func getAllotOutCount(req *ErpDecisionReportReq, stock ErpStock) (DecisionReport return reportData, nil } +// 查询期初库存 +func getSumStartData(req *ErpDecisionReportReq) (DecisionReportData, error) { + if req.StartTime == "" || req.EndTime == "" { + return DecisionReportData{}, nil + } + + tempReq := req + tempReq.EndTime = time.Now().Format(QueryTimeFormat) + + var respData DecisionReportData + // 期末库存 + sumEndData, err := getSumEndCount(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + // 查询库存汇总数据:采购退货数量、产品入库、系统入库、系统出库、在途库存(入库)数量 + sumStockData, err := getSumStockData(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + // 查询采购进货数量 + sumPurchaseData, err := getSumPurchaseData(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + // 查询零售汇总数据:零售销售数量、零售退货数量 + sumSalesData, err := getSumSalesData(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + // 查询盘点汇总数据:盘点入库数量、盘点出库数量 + sumInventoryData, err := getSumInventoryData(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + // 查询调拨汇总数据:在途库存(出库)数量、调拨入库数量、调拨出库数量 + sumAllotData, err := getSumAllotData(tempReq) + if err != nil { + return DecisionReportData{}, err + } + + respData.BeginStock = sumEndData.EndStock + (sumSalesData.OrderSale - sumSalesData.OrderReject) + + sumAllotData.AllotOut + sumStockData.SystemOut + sumInventoryData.CheckOut - + (sumPurchaseData.PurchaseStock - sumStockData.PurchaseReturn) - sumAllotData.AllotIn - + sumStockData.ProductIn - sumInventoryData.CheckIn + + respData.BeginAmount = sumEndData.EndAmount + ((sumSalesData.OrderSaleAmount - sumSalesData.OrderRejectAmount) + + sumAllotData.AllotOutAmount + sumStockData.SystemOutAmount + sumInventoryData.CheckOutAmount) - + ((sumPurchaseData.PurchaseStockAmount - sumStockData.PurchaseReturnAmount) + sumAllotData.AllotInAmount + + sumStockData.ProductInAmount + sumInventoryData.CheckInAmount) + + return respData, nil +} + // 查询汇总数据:期初库存,期初金额 +// 期初库存 = 目前在库的数据 + 期初之后的出库(销售、调拨出库、系统出库、盘点出库)-期初之后的入库(采购、调拨入库、产品入库、盘点入库) func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { var reportData DecisionReportData @@ -1510,7 +1748,7 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { qs := orm.Eloquent.Debug().Table("erp_stock_commodity"). Select("SUM(count) as begin_stock, SUM(wholesale_price) as begin_amount"). Where("storage_type = ?", SystemInventory). - Where("created_at <= ?", previousDay) + Where("first_stock_time <= ?", previousDay) // 获取开始时间前一天的所有出库数量(包括销售出库、系统出库和盘点出库) var outStockData struct { @@ -1551,6 +1789,32 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { PurchaseInventory, PurchaseInventory, ProductInventory, ProductInventory, CheckInventory, CheckInventory). Where("created_at <= ?", previousDay) + // 调拨入库数量和金额 + var allotInData struct { + AllotIn uint32 `json:"allot_in"` + AllotInAmount float64 `json:"allot_in_amount"` + } + allotInQuery := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity"). + Select("SUM(erp_inventory_allot_commodity.count) AS allot_in, "+ + "SUM(erp_inventory_allot_commodity.amount) AS allot_in_amount"). + 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 = ?", ErpInventoryAllotOrderFinished). + Where("erp_inventory_allot_order.receive_time <= ?", previousDay) + + // 调拨出库数量和金额 + var allotOutData struct { + AllotOut uint32 `json:"allot_out"` + AllotOutAmount float64 `json:"allot_out_amount"` + } + allotOutQuery := orm.Eloquent.Debug().Table("erp_inventory_allot_commodity"). + Select("SUM(erp_inventory_allot_commodity.count) AS allot_out, "+ + "SUM(erp_inventory_allot_commodity.amount) AS allot_out_amount"). + 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 = ?", ErpInventoryAllotOrderFinished). + Where("erp_inventory_allot_order.receive_time <= ?", previousDay) + if len(req.StoreId) > 0 { // 门店复选 var storeIDs []uint32 for _, store := range req.StoreId { @@ -1560,10 +1824,14 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { qs = qs.Where("store_id = ?", storeIDs[0]) es = es.Where("store_id = ?", storeIDs[0]) ts = ts.Where("store_id = ?", storeIDs[0]) + allotInQuery = allotInQuery.Where("erp_inventory_allot_order.receive_store_id = ?", storeIDs[0]) + allotOutQuery = allotOutQuery.Where("erp_inventory_allot_order.deliver_store_id = ?", storeIDs[0]) } else { qs = qs.Where("store_id IN (?)", storeIDs) es = es.Where("store_id IN (?)", storeIDs) ts = ts.Where("store_id IN (?)", storeIDs) + allotInQuery = allotInQuery.Where("erp_inventory_allot_order.receive_store_id IN (?)", storeIDs) + allotOutQuery = allotOutQuery.Where("erp_inventory_allot_order.deliver_store_id IN (?)", storeIDs) } } @@ -1576,10 +1844,14 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { qs = qs.Where("erp_commodity_name = ?", commodityNames[0]) es = es.Where("erp_commodity_name = ?", commodityNames[0]) ts = ts.Where("erp_commodity_name = ?", commodityNames[0]) + allotOutQuery = allotOutQuery.Where("erp_commodity_name = ?", commodityNames[0]) + allotInQuery = allotInQuery.Where("erp_commodity_name = ?", commodityNames[0]) } else { qs = qs.Where("erp_commodity_name IN (?)", commodityNames) es = es.Where("erp_commodity_name IN (?)", commodityNames) ts = ts.Where("erp_commodity_name IN (?)", commodityNames) + allotOutQuery = allotOutQuery.Where("erp_commodity_name IN (?)", commodityNames) + allotInQuery = allotInQuery.Where("erp_commodity_name IN (?)", commodityNames) } } @@ -1592,10 +1864,14 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { qs = qs.Where("erp_category_id = ?", categoryIDs[0]) es = es.Where("erp_category_id = ?", categoryIDs[0]) ts = ts.Where("erp_category_id = ?", categoryIDs[0]) + allotOutQuery = allotOutQuery.Where("erp_category_id = ?", categoryIDs[0]) + allotInQuery = allotInQuery.Where("erp_category_id = ?", categoryIDs[0]) } else { qs = qs.Where("erp_category_id IN (?)", categoryIDs) es = es.Where("erp_category_id IN (?)", categoryIDs) ts = ts.Where("erp_category_id IN (?)", categoryIDs) + allotOutQuery = allotOutQuery.Where("erp_category_id IN (?)", categoryIDs) + allotInQuery = allotInQuery.Where("erp_category_id IN (?)", categoryIDs) } } @@ -1614,18 +1890,28 @@ func getSumStartCount(req *ErpDecisionReportReq) (DecisionReportData, error) { return DecisionReportData{}, err } + err = allotInQuery.Find(&allotInData).Error // 查询调拨入库 + if err != nil { + return DecisionReportData{}, err + } + + err = allotOutQuery.Find(&allotOutData).Error // 查询调拨出库 + if err != nil { + return DecisionReportData{}, err + } + // 计算期初库存 beginStock := int64(systemStartData.BeginStock) - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - - int64(outStockData.CheckOut) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + - int64(inStockData.CheckIn) + int64(outStockData.CheckOut) - int64(allotOutData.AllotOut) + int64(allotInData.AllotIn) + + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn) if beginStock < 0 { beginStock = 0 // 确保库存不会为负 } // 计算期初金额 - beginAmount := systemStartData.BeginAmount - outStockData.SoldOutAmount - - outStockData.SystemOutAmount - outStockData.CheckOutAmount + + beginAmount := systemStartData.BeginAmount - outStockData.SoldOutAmount - outStockData.SystemOutAmount - + outStockData.CheckOutAmount - allotOutData.AllotOutAmount + allotInData.AllotInAmount + inStockData.PurchaseInAmount + inStockData.ProductInAmount + inStockData.CheckInAmount if beginAmount < 0 { beginAmount = 0 // 确保库存不会为负 @@ -1734,92 +2020,101 @@ func getSumEndCount(req *ErpDecisionReportReq) (DecisionReportData, error) { } } - if req.EndTime != "" { // 有结束时间 - endTime, err := time.Parse(QueryTimeFormat, req.EndTime) - if err != nil { - logger.Errorf("getSystemEndCount err:", err) - return DecisionReportData{}, err - } + //if req.EndTime != "" { // 有结束时间 + // endTime, err := time.Parse(QueryTimeFormat, req.EndTime) + // if err != nil { + // logger.Errorf("getSystemEndCount err:", err) + // return DecisionReportData{}, err + // } + // + // // 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库) + // var inStockData struct { + // SystemIn uint32 `json:"system_in"` + // SystemInAmount float64 `json:"system_in_amount"` + // PurchaseIn uint32 `json:"purchase_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, "+ + // "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 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 wholesale_price ELSE 0 END) AS product_in_amount, "+ + // "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) + // + // err = qs.Find(&inStockData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + // + // // 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库) + // var outStockData struct { + // SoldOut uint32 `json:"sold_out"` + // SoldOutAmount float64 `json:"sold_out_amount"` + // 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, "+ + // "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 wholesale_price ELSE 0 END) AS system_out_amount, "+ + // "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("state IN (?)", []int{SoldOut, SystemOut, CheckOut}) + // + // err = es.Find(&outStockData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + // + // // 计算期末库存 + // endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn) - + // int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut) + // + // if endStock < 0 { + // endStock = 0 // 确保库存不会为负 + // } + // + // // 计算期末金额 + // 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 { + // // 如果没有结束时间,直接获取系统在库数量作为期末库存 + // qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount"). + // Where("state in (?)", []uint32{InStock, InAllot}).Find(&reportData) + // + // err := qs.Find(&reportData).Error + // if err != nil { + // return DecisionReportData{}, err + // } + //} - // 获取结束时间之前的所有入库数量(包括系统入库、采购入库、产品入库和盘点入库) - var inStockData struct { - SystemIn uint32 `json:"system_in"` - SystemInAmount float64 `json:"system_in_amount"` - PurchaseIn uint32 `json:"purchase_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, "+ - "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 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 wholesale_price ELSE 0 END) AS product_in_amount, "+ - "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) + // 直接获取系统在库数量作为期末库存 + qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount"). + Where("state in (?)", []uint32{InStock, InAllot}).Find(&reportData) - err = qs.Find(&inStockData).Error - if err != nil { - return DecisionReportData{}, err - } - - // 获取结束时间之前的所有出库数量(包括销售出库、系统出库和盘点出库) - var outStockData struct { - SoldOut uint32 `json:"sold_out"` - SoldOutAmount float64 `json:"sold_out_amount"` - 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, "+ - "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 wholesale_price ELSE 0 END) AS system_out_amount, "+ - "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("state IN (?)", []int{SoldOut, SystemOut, CheckOut}) - - err = es.Find(&outStockData).Error - if err != nil { - return DecisionReportData{}, err - } - - // 计算期末库存 - endStock := int64(inStockData.SystemIn) + int64(inStockData.PurchaseIn) + int64(inStockData.ProductIn) + int64(inStockData.CheckIn) - - int64(outStockData.SoldOut) - int64(outStockData.SystemOut) - int64(outStockData.CheckOut) - - if endStock < 0 { - endStock = 0 // 确保库存不会为负 - } - - // 计算期末金额 - 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 { - // 如果没有结束时间,直接获取系统在库数量作为期末库存 - qs = qs.Select("SUM(count) as end_stock, SUM(wholesale_price) as end_amount"). - Where("state in (?)", []uint32{InStock, InAllot}).Find(&reportData) - - err := qs.Find(&reportData).Error - if err != nil { - return DecisionReportData{}, err - } + err := qs.Find(&reportData).Error + if err != nil { + return DecisionReportData{}, err } return reportData, nil @@ -1882,11 +2177,17 @@ func getSumStockData(req *ErpDecisionReportReq) (DecisionReportData, error) { subQuery := qs.Select(` SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS purchase_return, + SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS purchase_return_amount, SUM(CASE WHEN storage_type = ? THEN count ELSE 0 END) AS product_in, + SUM(CASE WHEN storage_type = ? THEN wholesale_price ELSE 0 END) AS product_in_amount, 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 state = ? THEN count ELSE 0 END) AS system_out, - SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS allot_wait_in - `, PurchaseReturn, ProductInventory, SystemInventory, SystemOut, InAllot) + SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS system_out_amount, + SUM(CASE WHEN state = ? THEN count ELSE 0 END) AS allot_wait_in, + SUM(CASE WHEN state = ? THEN wholesale_price ELSE 0 END) AS allot_wait_in_amount + `, PurchaseReturn, PurchaseReturn, ProductInventory, ProductInventory, SystemInventory, SystemInventory, + SystemOut, SystemOut, InAllot, InAllot) err := subQuery.Scan(&reportData).Error if err != nil { @@ -1955,7 +2256,8 @@ func getSumPurchaseData(req *ErpDecisionReportReq) (DecisionReportData, error) { } var err error - err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock"). + err = qs.Select("SUM(erp_purchase_inventory.count) AS purchase_stock,"+ + "SUM(erp_purchase_inventory.amount) AS purchase_stock_amount"). 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 @@ -2025,8 +2327,13 @@ func getSumSalesData(req *ErpDecisionReportReq) (DecisionReportData, error) { 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) + SUM(CASE WHEN erp_order.retail_type = ? AND erp_order.state = ? AND erp_order.pay_status = ? THEN erp_order_commodity.wholesale_price ELSE 0 END) AS order_sale_amount, + SUM(CASE WHEN erp_order.retail_type = ? AND erp_order.state = ? THEN erp_order_commodity.count ELSE 0 END) AS order_reject, + SUM(CASE WHEN erp_order.retail_type = ? AND erp_order.state = ? THEN erp_order_commodity.wholesale_price ELSE 0 END) AS order_reject_amount + `, RetailTypeSale, ErpOrderStateAudited, HavePaid, + RetailTypeSale, ErpOrderStateAudited, HavePaid, + RetailTypeRejected, ErpOrderStateAudited, + RetailTypeRejected, ErpOrderStateAudited) err := subQuery.Scan(&reportData).Error if err != nil { @@ -2085,9 +2392,16 @@ func getSumInventoryData(req *ErpDecisionReportReq) (DecisionReportData, error) 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.price ELSE 0 END) AS check_in_amount, 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) +THEN erp_inventory_change_commodity.count ELSE 0 END) AS check_out, + SUM(CASE WHEN erp_inventory_change_order.change_type = ? AND erp_inventory_change_order.state = ? +THEN erp_inventory_change_commodity.price ELSE 0 END) AS check_out_amount + `, AddChangeOrder, ErpInventoryChangeOrderFinished, + AddChangeOrder, ErpInventoryChangeOrderFinished, + ReduceChangeOrder, ErpInventoryChangeOrderFinished, + ReduceChangeOrder, ErpInventoryChangeOrderFinished) err := subQuery.Scan(&reportData).Error if err != nil { @@ -2151,9 +2465,15 @@ func getSumAllotData(req *ErpDecisionReportReq) (DecisionReportData, error) { 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 IN (?, ?) AND erp_inventory_allot_order.deliver_store_id IN (?) THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_wait_out_amount, 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 + SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.receive_store_id IN (?) THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_in_amount, + 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, + SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.deliver_store_id IN (?) THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_out_amount `, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId, + ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId, + ErpInventoryAllotOrderFinished, req.StoreId, + ErpInventoryAllotOrderFinished, req.StoreId, ErpInventoryAllotOrderFinished, req.StoreId, ErpInventoryAllotOrderFinished, req.StoreId) @@ -2165,9 +2485,15 @@ func getSumAllotData(req *ErpDecisionReportReq) (DecisionReportData, error) { } 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 IN (?, ?) AND erp_inventory_allot_order.deliver_store_id = ? THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_wait_out_amount, 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 + SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.receive_store_id = ? THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_in_amount, + 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, + SUM(CASE WHEN erp_inventory_allot_order.state = ? AND erp_inventory_allot_order.deliver_store_id = ? THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_out_amount `, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId[0], + ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, req.StoreId[0], + ErpInventoryAllotOrderFinished, req.StoreId[0], + ErpInventoryAllotOrderFinished, req.StoreId[0], ErpInventoryAllotOrderFinished, req.StoreId[0], ErpInventoryAllotOrderFinished, req.StoreId[0]) @@ -2180,9 +2506,14 @@ func getSumAllotData(req *ErpDecisionReportReq) (DecisionReportData, error) { // 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 IN (?, ?) THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_wait_out_amount, 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 + SUM(CASE WHEN erp_inventory_allot_order.state = ? THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_in_amount, + SUM(CASE WHEN erp_inventory_allot_order.state = ? THEN erp_inventory_allot_commodity.count ELSE 0 END) AS allot_out, + SUM(CASE WHEN erp_inventory_allot_order.state = ? THEN erp_inventory_allot_commodity.amount ELSE 0 END) AS allot_out_amount `, ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, + ErpInventoryAllotOrderWaitSend, ErpInventoryAllotOrderWaitReceive, + ErpInventoryAllotOrderFinished, ErpInventoryAllotOrderFinished, ErpInventoryAllotOrderFinished, ErpInventoryAllotOrderFinished) err := subQuery.Scan(&reportData).Error @@ -2199,13 +2530,15 @@ func getSumAllotData(req *ErpDecisionReportReq) (DecisionReportData, error) { func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error) { var sumData DecisionSumData - sumStartData, err := getSumStartCount(req) + // 期初库存 + sumStartData, err := getSumStartData(req) if err != nil { return DecisionSumData{}, err } sumData.TotalBeginStock = sumStartData.BeginStock sumData.TotalBeginAmount = sumStartData.BeginAmount + // 期末库存 sumEndData, err := getSumEndCount(req) if err != nil { return DecisionSumData{}, err @@ -2213,6 +2546,7 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error sumData.TotalEndStock = sumEndData.EndStock sumData.TotalEndAmount = sumEndData.EndAmount + // 查询库存汇总数据:采购退货数量、产品入库、系统入库、系统出库、在途库存(入库)数量 sumStockData, err := getSumStockData(req) if err != nil { return DecisionSumData{}, err @@ -2223,12 +2557,14 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error sumData.TotalSystemIn = sumStockData.SystemIn sumData.TotalSystemOut = sumStockData.SystemOut + // 查询采购进货数量 sumPurchaseData, err := getSumPurchaseData(req) if err != nil { return DecisionSumData{}, err } sumData.TotalPurchaseStock = sumPurchaseData.PurchaseStock + // 查询零售汇总数据:零售销售数量、零售退货数量 sumSalesData, err := getSumSalesData(req) if err != nil { return DecisionSumData{}, err @@ -2236,6 +2572,7 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error sumData.TotalOrderSale = sumSalesData.OrderSale sumData.TotalOrderReject = sumSalesData.OrderReject + // 查询盘点汇总数据:盘点入库数量、盘点出库数量 sumInventoryData, err := getSumInventoryData(req) if err != nil { return DecisionSumData{}, err @@ -2243,6 +2580,7 @@ func getSumDecisionReportData(req *ErpDecisionReportReq) (DecisionSumData, error sumData.TotalCheckIn = sumInventoryData.CheckIn sumData.TotalCheckOut = sumInventoryData.CheckOut + // 查询调拨汇总数据:在途库存(出库)数量、调拨入库数量、调拨出库数量 sumAllotData, err := getSumAllotData(req) if err != nil { return DecisionSumData{}, err diff --git a/app/admin/models/inventory_report.go b/app/admin/models/inventory_report.go index 92f980c..a048eb4 100644 --- a/app/admin/models/inventory_report.go +++ b/app/admin/models/inventory_report.go @@ -1510,6 +1510,7 @@ func reportOtherExport(req *InventoryReportByOtherResp) (string, error) { file.SetColWidth("Sheet1", "B", "B", 28) file.SetColWidth("Sheet1", "C", "C", 20) file.SetColWidth("Sheet1", "D", "D", 20) + file.SetColWidth("Sheet1", "F", "F", 18) endRow := fmt.Sprintf("F"+"%d", nExcelStartRow+2) // 应用样式到整个表格 @@ -1879,6 +1880,8 @@ func reportOtherDetailExport(req *InventoryReportOtherDetailResp) (string, error file.SetColWidth("Sheet1", "E", "E", 28) file.SetColWidth("Sheet1", "G", "G", 18) file.SetColWidth("Sheet1", "I", "I", 18) + file.SetColWidth("Sheet1", "J", "J", 18) + file.SetColWidth("Sheet1", "K", "K", 18) endRow := fmt.Sprintf("K"+"%d", nExcelStartRow+2) // 应用样式到整个表格