From 973b77d5e9147bae82a2eb8e127dde80bc07dc47 Mon Sep 17 00:00:00 2001 From: chenlin Date: Wed, 17 Jul 2024 12:12:12 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E9=87=87=E8=B4=AD=E6=8A=A5?= =?UTF-8?q?=E8=A1=A8=EF=BC=88=E6=8C=89=E5=8D=95=EF=BC=89=E6=B1=87=E6=80=BB?= =?UTF-8?q?=E9=87=91=E9=A2=9D=E5=92=8C=E6=95=B0=E9=87=8F=E5=8F=AA=E6=98=AF?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E9=A1=B5=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/models/purchase.go | 88 ++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/app/admin/models/purchase.go b/app/admin/models/purchase.go index 666d281..dad4814 100644 --- a/app/admin/models/purchase.go +++ b/app/admin/models/purchase.go @@ -3833,10 +3833,6 @@ func getReportByOrderFromCommon(req *ErpPurchaseReportByOrderReq, c *gin.Context if err != nil { return nil, err } - if reportByOrderData.CommodityData == nil { - continue - } - //if reportByOrderData.CommodityData == nil { // continue //} @@ -3861,15 +3857,20 @@ func getReportByOrderFromCommon(req *ErpPurchaseReportByOrderReq, c *gin.Context } resp.List = reportByOrderDataList[startIndex:endIndex] + nTotalAmount = math.Round(nTotalAmount*100) / 100 + resp.Amount = nTotalAmount + resp.Count = nTotalCount } else { resp.List = reportByOrderDataList + // 查询订单总的执行金额和数量 + totalAmount, totalCount, err := getOrderInventorySumData(req) + if err != nil { + return nil, err + } + resp.Amount = totalAmount + resp.Count = totalCount } - nTotalAmount = math.Round(nTotalAmount*100) / 100 - resp.Amount = nTotalAmount - resp.Count = nTotalCount - //resp.Total = int(count) - if req.IsExport == 1 { filePath, err := reportByOrderExport(resp) if err != nil { @@ -3882,6 +3883,75 @@ func getReportByOrderFromCommon(req *ErpPurchaseReportByOrderReq, c *gin.Context return resp, nil } +type OrderInventorySum struct { + TotalAmount float64 `gorm:"column:totalAmount"` + TotalCount int32 `gorm:"column:totalCount"` +} + +// 查询采购订单的汇总信息:总金额,总执行数量 +func getOrderInventorySumData(req *ErpPurchaseReportByOrderReq) (float64, int32, error) { + qs := orm.Eloquent.Table("erp_purchase_order").Where("state != ?", ErpPurchaseOrderUnAudit) // 未审核订单不展示 + if req.SerialNumber != "" { // 单据编号 + qs = qs.Where("serial_number=?", req.SerialNumber) + } + if req.PurchaseType != "" { // 采购类型 + qs = qs.Where("purchase_type=?", req.PurchaseType) + } + if len(req.ErpSupplierId) > 0 { // 供应商复选 + var supplierIDs []uint32 + for _, supplier := range req.ErpSupplierId { + supplierIDs = append(supplierIDs, supplier) + } + qs = qs.Where("erp_supplier_id IN (?)", supplierIDs) + } + if len(req.StoreId) > 0 { // 门店复选 + var storeIDs []uint32 + for _, store := range req.StoreId { + storeIDs = append(storeIDs, store) + } + qs = qs.Where("store_id IN (?)", storeIDs) + } + if req.HandlerId != 0 { // 经手人id + qs = qs.Where("handler_id=?", req.HandlerId) + } + if req.State != 0 { // 订单状态 + qs = qs.Where("state=?", req.State) + } + if req.AuditTimeStart != "" { // 审核开始时间 + parse, err := time.Parse(QueryTimeFormat, req.AuditTimeStart) + if err != nil { + logger.Errorf("erpPurchaseOrderList err:", err) + return 0, 0, err + } + qs = qs.Where("audit_time > ?", parse) + } + if req.AuditTimeEnd != "" { // 审核结束时间 + parse, err := time.Parse(QueryTimeFormat, req.AuditTimeEnd) + if err != nil { + logger.Errorf("erpPurchaseOrderList err:", err) + return 0, 0, err + } + qs = qs.Where("audit_time < ?", parse) + } + + var result OrderInventorySum + + // 使用联表查询获取总金额和总执行数量 + err := qs.Joins("LEFT JOIN erp_purchase_inventory ON erp_purchase_inventory.erp_purchase_order_id = erp_purchase_order.id"). + Select("SUM(CASE WHEN erp_purchase_order.purchase_type = 'reject' THEN -erp_purchase_inventory.amount ELSE erp_purchase_inventory.amount END) as totalAmount, " + + "SUM(CASE WHEN erp_purchase_order.purchase_type = 'reject' THEN -erp_purchase_inventory.count ELSE erp_purchase_inventory.count END) as totalCount"). + Scan(&result).Error + if err != nil { + logger.Errorf("getOrderInventorySumData err:", err) + return 0, 0, err + } + + // 四舍五入保留2位小数 + result.TotalAmount = math.Round(result.TotalAmount*100) / 100 + + return result.TotalAmount, result.TotalCount, nil +} + // 查询采购订单的入库信息 func getOrderInventoryInfo(req *ErpPurchaseReportByOrderReq, erpPurchaseOrderId uint32) ([]ErpPurchaseCommodityData, float64, int32, error) { var inventoryList []ErpPurchaseInventory