public ReportIncomeViewModel ReviewIncomeMonthlyDetail(int month, int year) { ReportIncomeViewModel result = new ReportIncomeViewModel { Month = month, Year = year }; #region Revenue #region Get completed order list DateTime startDate = new DateTime(year, month, 1); DateTime endDate = startDate.AddMonths(1); List<Order> completeOrderList = db.Orders.Where(m => m.OrderStatus == 5 && m.FinishTime >= startDate && m.FinishTime < endDate) .ToList(); int completeValue = 0; foreach (Order order in completeOrderList) { completeValue += (order.Amount - order.DiscountAmount); } result.CompletedOrderValue = completeValue; result.CompletedOrderList = completeOrderList; #endregion #region Get cancel order list List<Order> canceledOrderList = db.Orders.Where(m => m.OrderStatus == 6 && m.CancelTime >= startDate && m.CancelTime < endDate) .ToList(); int canceledValue = 0; foreach (Order order in canceledOrderList) { canceledValue += (order.DepositAmount - order.ReturnDeposit); } result.CanceledOrderValue = canceledValue; result.CanceledOrderList = canceledOrderList; result.RevenueAmount = completeValue + canceledValue; #endregion #endregion #region MaterialExpense int materialExpenseValue = 0; #region CompletedOrderList List<ReportProductMaterial> productMaterialList = new List<ReportProductMaterial>(); foreach (Order order in completeOrderList) { foreach (OrderItem orderItem in order.OrderItems) { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { if (productMaterialList.Count == 0) { ReportProductMaterial productMaterial = new ReportProductMaterial(); productMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; productMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; productMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialValue = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { double price = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == outputMaterial.ProductMaterialId && m.InputBillId == exportFrom.InputbillId).InputMaterialPrice; productMaterialValue += (int)(price * exportFrom.ExportFromQuantity); } productMaterial.ProductMaterialAmount = productMaterialValue; materialExpenseValue += productMaterialValue; productMaterialList.Add(productMaterial); } else { bool check = true; foreach (ReportProductMaterial reportProductMaterial in productMaterialList) { if ( reportProductMaterial.ProductMaterialName.Equals( outputMaterial.ProductMaterial.ProductMaterialName)) { check = false; reportProductMaterial.ProductMaterialQuantity += outputMaterial.ExportQuantity; int productMaterialValue = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { double price = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == outputMaterial.ProductMaterialId && m.InputBillId == exportFrom.InputbillId).InputMaterialPrice; productMaterialValue += (int)(price * exportFrom.ExportFromQuantity); } reportProductMaterial.ProductMaterialAmount += productMaterialValue; materialExpenseValue += productMaterialValue; } } if (check) { ReportProductMaterial productMaterial = new ReportProductMaterial(); productMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; productMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; productMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialValue = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { double price = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == outputMaterial.ProductMaterialId && m.InputBillId == exportFrom.InputbillId).InputMaterialPrice; productMaterialValue += (int)(price * exportFrom.ExportFromQuantity); } productMaterial.ProductMaterialAmount = productMaterialValue; materialExpenseValue += productMaterialValue; productMaterialList.Add(productMaterial); } } } } } #endregion #region CanceledOrderList foreach (Order order in canceledOrderList) { foreach (OrderItem orderItem in order.OrderItems) { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { bool check = true; foreach (ReportProductMaterial reportProductMaterial in productMaterialList) { if ( reportProductMaterial.ProductMaterialName.Equals( outputMaterial.ProductMaterial.ProductMaterialName)) { check = false; reportProductMaterial.ProductMaterialQuantity += outputMaterial.ExportQuantity; int productMaterialValue = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { double price = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == outputMaterial.ProductMaterialId && m.InputBillId == exportFrom.InputbillId).InputMaterialPrice; productMaterialValue += (int)(price * exportFrom.ExportFromQuantity); } reportProductMaterial.ProductMaterialAmount += productMaterialValue; materialExpenseValue += productMaterialValue; } } if (check) { ReportProductMaterial productMaterial = new ReportProductMaterial(); productMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; productMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; productMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialValue = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { double price = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == outputMaterial.ProductMaterialId && m.InputBillId == exportFrom.InputbillId).InputMaterialPrice; productMaterialValue += (int)(price * exportFrom.ExportFromQuantity); } productMaterial.ProductMaterialAmount = productMaterialValue; materialExpenseValue += productMaterialValue; productMaterialList.Add(productMaterial); } } } } #endregion result.ProductMaterialList = productMaterialList; result.MaterialExpense = materialExpenseValue; #endregion #region OtherExpense int otherExpenseValue = 0; List<OtherExpense> otherExpenses = db.OtherExpenses.Where(m => m.OtherExpenseMonthTime == month && m.OtherExpenseYearTime == year).ToList(); foreach (OtherExpense otherExpense in otherExpenses) { otherExpenseValue += otherExpense.OtherExpenseAmount; } result.OtherExpense = otherExpenseValue; result.OtherExpenseList = otherExpenses; #endregion #region Income result.Income = result.RevenueAmount - result.MaterialExpense - result.OtherExpense; #endregion return result; }
public ReportProductIncomeViewModel GetReportProductIncomeViewModel(int productId, DateTime? startDate, DateTime? endDate, int? startMonth, int? startYear, int? endMonth, int? endYear) { ReportProductIncomeViewModel result = new ReportProductIncomeViewModel(); #region Get Product info Product product = db.Products.FirstOrDefault(m => m.ProductId == productId); if (product != null) { result.ProductCode = product.ProductCode; result.ProductName = product.ProductName; } #endregion if (startDate != null && endDate != null) { #region Get report product weekly result.StartDate = startDate.Value; result.EndDate = endDate.Value; int totalIncome = 0; #region Create List income report period DateTime firstWeekEndDate = startDate.Value.LastDayOfWeek(); DateTime lastWeekStartDate = endDate.Value.FirstDayOfWeek(); List<ReportProductIncomePeriod> incomeReportPeriods = new List<ReportProductIncomePeriod>(); ReportProductIncomePeriod firstPeriod = new ReportProductIncomePeriod { StartDate = startDate.Value, EndDate = firstWeekEndDate }; incomeReportPeriods.Add(firstPeriod); for (DateTime temp = firstWeekEndDate.AddDays(1); temp < lastWeekStartDate; temp = temp.AddDays(7)) { ReportProductIncomePeriod period = new ReportProductIncomePeriod { StartDate = temp, EndDate = temp.LastDayOfWeek() }; incomeReportPeriods.Add(period); } ReportProductIncomePeriod lastPeriod = new ReportProductIncomePeriod { StartDate = lastWeekStartDate, EndDate = endDate.Value }; incomeReportPeriods.Add(lastPeriod); #endregion #region Get each income report foreach (ReportProductIncomePeriod incomePeriod in incomeReportPeriods) { DateTime tempEndDate = incomePeriod.EndDate.AddDays(1); int revenue = 0; int expense = 0; List<OrderItem> orderItems = db.OrderItems.Where( m => m.ProductId == productId && m.Order.OrderStatus == 5 && m.Order.FinishTime >= incomePeriod.StartDate && m.Order.FinishTime < tempEndDate).ToList(); List<ReportProductOrder> reportProductOrderList = new List<ReportProductOrder>(); List<ReportProductMaterial> reportProductMaterialList = new List<ReportProductMaterial>(); foreach (OrderItem orderItem in orderItems) { #region Create ReportProductOrder ReportProductOrder reportProductOrder = new ReportProductOrder(); reportProductOrder.OrderCode = orderItem.Order.OrderCode; reportProductOrder.Quantity = orderItem.Quantity; reportProductOrder.Amount = orderItem.Amount; reportProductOrder.DiscountAmount = (orderItem.Amount * (orderItem.Order.DiscountAmount * 100 / orderItem.Order.Amount)) / 100; reportProductOrder.Revenue = reportProductOrder.Amount - reportProductOrder.DiscountAmount; reportProductOrderList.Add(reportProductOrder); revenue += reportProductOrder.Revenue; #endregion #region Create ReportProductMaterial if (reportProductMaterialList.Count == 0) { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { ReportProductMaterial reportProductMaterial = new ReportProductMaterial(); reportProductMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; reportProductMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; reportProductMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount = amount; reportProductMaterialList.Add(reportProductMaterial); expense += amount; } } else { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { foreach (ReportProductMaterial reportProductMaterial in reportProductMaterialList) { if ( reportProductMaterial.ProductMaterialName.Equals( outputMaterial.ProductMaterial.ProductMaterialName)) { reportProductMaterial.ProductMaterialQuantity += outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount += amount; expense += amount; } } } } #endregion } incomePeriod.RevenueAmount = revenue; incomePeriod.OrderList = reportProductOrderList; incomePeriod.MaterialExpense = expense; incomePeriod.ProductMaterialList = reportProductMaterialList; incomePeriod.Income = revenue - expense; totalIncome += incomePeriod.Income; } #endregion #region Finish result result.ReportProductIncomePeriodList = incomeReportPeriods; result.TotalIncome = totalIncome; #endregion #endregion } else if (startMonth != null && endMonth != null && startYear != null && endYear != null) { #region Get report product monthly result.StartMonth = startMonth.Value; result.StartYear = startYear.Value; result.EndMonth = endMonth.Value; result.EndYear = endYear.Value; #region Create List income report period DateTime firstMonthStartDate = new DateTime(startYear.Value, startMonth.Value, 1); DateTime lastMonthStartDate = new DateTime(endYear.Value, endMonth.Value, 1); List<ReportProductIncomePeriod> incomeReportPeriods = new List<ReportProductIncomePeriod>(); int totalIncome = 0; for (DateTime tempDate = firstMonthStartDate; tempDate <= lastMonthStartDate; tempDate = tempDate.AddMonths(1)) { ReportProductIncomePeriod reportPeriod = new ReportProductIncomePeriod { Month = tempDate.Month, Year = tempDate.Year }; int revenue = 0; int expense = 0; DateTime tempEndDate = tempDate.AddMonths(1); List<OrderItem> orderItems = db.OrderItems.Where( m => m.ProductId == productId && m.Order.OrderStatus == 5 && m.Order.FinishTime >= tempDate && m.Order.FinishTime < tempEndDate).ToList(); List<ReportProductOrder> reportProductOrderList = new List<ReportProductOrder>(); List<ReportProductMaterial> reportProductMaterialList = new List<ReportProductMaterial>(); foreach (OrderItem orderItem in orderItems) { #region Create ReportProductOrder ReportProductOrder reportProductOrder = new ReportProductOrder(); reportProductOrder.OrderCode = orderItem.Order.OrderCode; reportProductOrder.Quantity = orderItem.Quantity; reportProductOrder.Amount = orderItem.Amount; reportProductOrder.DiscountAmount = (orderItem.Amount * (orderItem.Order.DiscountAmount * 100 / orderItem.Order.Amount)) / 100; reportProductOrder.Revenue = reportProductOrder.Amount - reportProductOrder.DiscountAmount; reportProductOrderList.Add(reportProductOrder); revenue += reportProductOrder.Revenue; #endregion #region Create ReportProductMaterial if (reportProductMaterialList.Count == 0) { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { ReportProductMaterial reportProductMaterial = new ReportProductMaterial(); reportProductMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; reportProductMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; reportProductMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount = amount; reportProductMaterialList.Add(reportProductMaterial); expense += amount; } } else { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { foreach (ReportProductMaterial reportProductMaterial in reportProductMaterialList) { if ( reportProductMaterial.ProductMaterialName.Equals( outputMaterial.ProductMaterial.ProductMaterialName)) { reportProductMaterial.ProductMaterialQuantity += outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount += amount; expense += amount; } } } } #endregion } reportPeriod.RevenueAmount = revenue; reportPeriod.OrderList = reportProductOrderList; reportPeriod.MaterialExpense = expense; reportPeriod.ProductMaterialList = reportProductMaterialList; reportPeriod.Income = revenue - expense; incomeReportPeriods.Add(reportPeriod); totalIncome += reportPeriod.Income; } #endregion #region Finish result result.ReportProductIncomePeriodList = incomeReportPeriods; result.TotalIncome = totalIncome; #endregion #endregion } else if (startMonth == null && endMonth == null && startYear != null && endYear != null) { #region Get report product monthly result.StartYear = startYear.Value; result.EndYear = endYear.Value; #region Create List income report period DateTime firstMonthStartDate = new DateTime(startYear.Value, 1, 1); DateTime lastMonthStartDate = new DateTime(endYear.Value, 1, 1); List<ReportProductIncomePeriod> incomeReportPeriods = new List<ReportProductIncomePeriod>(); int totalIncome = 0; for (DateTime tempDate = firstMonthStartDate; tempDate <= lastMonthStartDate; tempDate = tempDate.AddYears(1)) { ReportProductIncomePeriod reportPeriod = new ReportProductIncomePeriod { Year = tempDate.Year }; int revenue = 0; int expense = 0; DateTime tempEndDate = tempDate.AddYears(1); List<OrderItem> orderItems = db.OrderItems.Where( m => m.ProductId == productId && m.Order.OrderStatus == 5 && m.Order.FinishTime >= tempDate && m.Order.FinishTime < tempEndDate).ToList(); List<ReportProductOrder> reportProductOrderList = new List<ReportProductOrder>(); List<ReportProductMaterial> reportProductMaterialList = new List<ReportProductMaterial>(); foreach (OrderItem orderItem in orderItems) { #region Create ReportProductOrder ReportProductOrder reportProductOrder = new ReportProductOrder(); reportProductOrder.OrderCode = orderItem.Order.OrderCode; reportProductOrder.Quantity = orderItem.Quantity; reportProductOrder.Amount = orderItem.Amount; reportProductOrder.DiscountAmount = (orderItem.Amount * (orderItem.Order.DiscountAmount * 100 / orderItem.Order.Amount)) / 100; reportProductOrder.Revenue = reportProductOrder.Amount - reportProductOrder.DiscountAmount; reportProductOrderList.Add(reportProductOrder); revenue += reportProductOrder.Revenue; #endregion #region Create ReportProductMaterial if (reportProductMaterialList.Count == 0) { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { ReportProductMaterial reportProductMaterial = new ReportProductMaterial(); reportProductMaterial.ProductMaterialName = outputMaterial.ProductMaterial.ProductMaterialName; reportProductMaterial.ProductMaterialUnit = outputMaterial.ProductMaterial.ProductMaterialUnit; reportProductMaterial.ProductMaterialQuantity = outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount = amount; reportProductMaterialList.Add(reportProductMaterial); expense += amount; } } else { foreach (OutputMaterial outputMaterial in orderItem.OutputMaterials) { foreach (ReportProductMaterial reportProductMaterial in reportProductMaterialList) { if ( reportProductMaterial.ProductMaterialName.Equals( outputMaterial.ProductMaterial.ProductMaterialName)) { reportProductMaterial.ProductMaterialQuantity += outputMaterial.ExportQuantity; int productMaterialId = outputMaterial.ProductMaterialId; int amount = 0; foreach (ExportFrom exportFrom in outputMaterial.ExportFroms) { InputMaterial inputMaterial = db.InputMaterials.FirstOrDefault( m => m.ProductMaterialId == productMaterialId && m.InputBillId == exportFrom.InputbillId); amount += exportFrom.ExportFromQuantity * (int)inputMaterial.InputMaterialPrice; } reportProductMaterial.ProductMaterialAmount += amount; expense += amount; } } } } #endregion } reportPeriod.RevenueAmount = revenue; reportPeriod.OrderList = reportProductOrderList; reportPeriod.MaterialExpense = expense; reportPeriod.ProductMaterialList = reportProductMaterialList; reportPeriod.Income = revenue - expense; incomeReportPeriods.Add(reportPeriod); totalIncome += reportPeriod.Income; } #endregion #region Finish result result.ReportProductIncomePeriodList = incomeReportPeriods; result.TotalIncome = totalIncome; #endregion #endregion } return result; }