private DateTime GetDate(SqlDataReader reader, EntityEnum.ReportTypeEnum reportType) { if (reportType == EntityEnum.ReportTypeEnum.ByDay) { return(Convert.ToDateTime(reader["CreatedOf"])); } else if (reportType == EntityEnum.ReportTypeEnum.ByMonth) { int year = Convert.ToInt32(reader["Year"]); int month = Convert.ToInt32(reader["Month"]); return(new DateTime(year, month, 1, 0, 0, 0, 0)); } throw new ArgumentException(); }
private SalesReportModelItem GetReportByDate(DateTime date, EntityEnum.DocumentTypeEnum docType, EntityEnum.ReportTypeEnum reportType, int employeeId) { var result = new SalesReportModelItem(); string datePartFrom = "", datePartTo = ""; if (reportType == EntityEnum.ReportTypeEnum.ByMonth) { DateTime dateTo = date.AddMonths(1); datePartTo = SqlHelper.GetDateString(dateTo.Month, dateTo.Year); } else if (reportType == EntityEnum.ReportTypeEnum.ByDay) { DateTime dateTo = date.AddDays(1); datePartTo = SqlHelper.GetDateString(dateTo.Day, dateTo.Month, dateTo.Year); } datePartFrom = SqlHelper.GetDateString(date.Day, date.Month, date.Year); result = GetTotalReport(null, datePartFrom, datePartTo, docType, employeeId); return(result); }
public SalesReportDataModel GetSalesReport(int yearFromNumber, int yearToNumber, int monthFromNumber, int monthToNumber, EntityEnum.DocumentTypeEnum docType, EntityEnum.ReportTypeEnum reportType, int employeeId) { var result = new List <SalesReportModel>(); var data = new SalesReportDataModel(); List <SalesReportPeriodModel> periods = new List <SalesReportPeriodModel>(); if (monthFromNumber > 12 || monthToNumber > 12 || monthToNumber < 1 || monthFromNumber < 1 || yearFromNumber < 1901 || yearToNumber < 1901) { throw new ArgumentException(); } string yearMonthFrom = SqlHelper.GetDateString(monthFromNumber, yearFromNumber); string yearMonthTo = SqlHelper.GetDateString(monthToNumber, yearToNumber); using (SqlConnection connection = SqlHelper.GetConnection(ConnectionString)) { string datePartInSelect = " ", datePartInGroupBy = " ", datePartInOrderBy = " ", datePartCreatedOf = ", [CreatedOf] " /*, employeeStr = ""*/; if (reportType == EntityEnum.ReportTypeEnum.ByMonth) { datePartInSelect = " DATEPART(Year, CreatedOf) Year, DATEPART(Month, CreatedOf) Month, "; datePartInGroupBy = " DATEPART(Year, CreatedOf), DATEPART(Month, CreatedOf), "; datePartInOrderBy = ", Year, Month "; datePartCreatedOf = " "; } using (SqlCommand command = connection.GetCommand(" " + "SELECT " + datePartInSelect + " [Document].[ContractorId] AS DocumentContractorId, " + "[Contractor].Code AS ContractorCode " + datePartCreatedOf + ", SUM([Sum]) AS PurchaseSum, " + "SUM([SaleSum]) AS SaleSum " + "FROM [Document] " + "INNER JOIN Contractor on Document.ContractorId = Contractor.ContractorId " + "WHERE DocumentTypeId = " + (int)docType + " AND IsCommitted = 1 " + GetEmployeeIdWhereString(employeeId) + "AND CreatedOf >= '" + yearMonthFrom + "' AND CreatedOf < '" + yearMonthTo + "' " + "GROUP BY " + datePartInGroupBy + " [Contractor].Code " + datePartCreatedOf + ",[Document].[ContractorId] " + "ORDER BY [Document].[ContractorId] ASC " + datePartInOrderBy + " " + datePartCreatedOf , CommandType.Text)) { using (SqlDataReader reader = command.ExecuteReader()) { SalesReportModel item = null; while (reader.Read()) { DateTime createdOf = GetDate(reader, reportType); decimal purchaseSum = Convert.ToDecimal(reader["PurchaseSum"]); decimal saleSum = Convert.ToDecimal(reader["SaleSum"]); int contractorId = Int32.Parse(reader["DocumentContractorId"].ToString()); string contractorCode = reader["ContractorCode"].ToString(); if (periods.Where(x => x.Period == createdOf).FirstOrDefault() == null) { periods.Add( new SalesReportPeriodModel { Period = createdOf, ReportItem = GetReportByDate(createdOf, docType, reportType, employeeId), }); } if (item == null) { item = new SalesReportModel { ContractorModel = new ContractorCodeIdModel { ContractorCode = contractorCode, ContractorId = contractorId, }, Refunds = GetTotalReport(contractorId, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Refunds, employeeId), SubTotal = GetTotalReport(contractorId, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Shipment, employeeId), } } ; if (item.ContractorModel.ContractorId != contractorId) { result.Add(item); item = new SalesReportModel { ContractorModel = new ContractorCodeIdModel { ContractorCode = contractorCode, ContractorId = contractorId }, Refunds = GetTotalReport(contractorId, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Refunds, employeeId), SubTotal = GetTotalReport(contractorId, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Shipment, employeeId), ReportItems = new List <SalesReportModelItem>() }; } if (item.ReportItems == null) { item.ReportItems = new List <SalesReportModelItem>(); } item.ReportItems.Add(new SalesReportModelItem { CreatedOf = createdOf, PurchaseSum = purchaseSum, SaleSum = saleSum }); } // Check if contractor already exists result.Add(item); } } } /* * string yearMonthFrom = SqlHelper.GetDateString(monthFromNumber,yearFromNumber); * string yearMonthTo = SqlHelper.GetDateString(monthToNumber, yearToNumber); */ data.RefundsGrandTotal = GetTotalReport(null, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Refunds, employeeId); data.GrandTotal = GetTotalReport(null, yearMonthFrom, yearMonthTo, EntityEnum.DocumentTypeEnum.Shipment, employeeId); data.ReportModel = result; //List<DateTime> datesList = dates.ToList(); //datesList.Sort(); data.Periods = periods.OrderBy(x => x.Period).ToList(); return(data); } }