public List <AllocationAmountData> GetAllocationsData(int[] allocationIds, StatusType minOrderStatus = StatusType.ApprovedPendingInvoice, int?excludeOrder = null, int startMonth = 1, int endMonth = 12, params string[] includes) { List <AllocationAmountData> data = new List <AllocationAmountData>(); string[] baseIncludes = { "Budget", "Budgets_AllocationToMonth" }; includes = baseIncludes.Union(includes).ToArray(); List <Budgets_Allocations> allocations; allocations = this.GetWithCanceled(includes).Where(x => allocationIds.Contains(x.Id)).ToList(); foreach (var allocation in allocations) { bool budgetYearPassed = allocation.Budget.Year < DateTime.Now.Year; IEnumerable <Orders_OrderToAllocation> approvedAllocationsQuery = allocation .Orders_OrderToAllocation .Where(x => x.Order.StatusId >= (int)minOrderStatus && x.Order.StatusId != (int)StatusType.Declined && x.Order.StatusId != (int)StatusType.OrderCancelled); if (excludeOrder.HasValue) { approvedAllocationsQuery = approvedAllocationsQuery.Where(x => x.OrderId != excludeOrder.Value); } List <Orders_OrderToAllocation> approvedAllocations = approvedAllocationsQuery.ToList(); List <AllocationMonthAmountData> allocationMonthData = new List <AllocationMonthAmountData>(); for (int monthId = startMonth; monthId <= endMonth; monthId++) { var allocationMonth = allocation.Budgets_AllocationToMonth.SingleOrDefault(x => x.MonthId == monthId); AllocationMonthAmountData newMonthData = new AllocationMonthAmountData() { OriginalAllocationMonth = allocationMonth, MonthId = allocationMonth.MonthId, TotalAmount = allocationMonth.Amount, UsedAmount = approvedAllocations.Where(m => m.MonthId == monthId).Select(d => (decimal?)d.Amount).Sum() ?? 0, BudgetYearPassed = budgetYearPassed }; allocationMonthData.Add(newMonthData); } AllocationAmountData newData = new AllocationAmountData() { OriginalAllocation = allocation, AllocationId = allocation.Id, TotalAmount = allocation.Budgets_AllocationToMonth.Select(x => (decimal?)x.Amount).Sum() ?? 0, UsedAmount = approvedAllocations.Select(x => (decimal?)x.Amount).Sum() ?? 0, Months = allocationMonthData, BudgetYearPassed = budgetYearPassed }; data.Add(newData); } return(data); }
public List <Orders_OrderToAllocation> GenerateOrderAllocations(AllocationAmountData data, decimal amount, bool allowExeeding = true, int maxMonth = 12, int?orderId = null) { List <Orders_OrderToAllocation> generatedAllocations = new List <Orders_OrderToAllocation>(); decimal remainingAmountToAllocate = amount; for (int monthId = 1; monthId <= maxMonth && remainingAmountToAllocate > 0; monthId++) { var monthData = data.Months.Single(x => x.MonthId == monthId); decimal newAmount; if (!allowExeeding || monthId != maxMonth) { if (monthData.RemainingAmount <= 0) { continue; } newAmount = monthData.RemainingAmount < remainingAmountToAllocate ? monthData.RemainingAmount : remainingAmountToAllocate; } else { newAmount = remainingAmountToAllocate; } Orders_OrderToAllocation newAllocation = new Orders_OrderToAllocation() { AllocationId = data.AllocationId, MonthId = monthData.MonthId, Amount = newAmount, OrderId = orderId ?? 0 }; remainingAmountToAllocate -= newAllocation.Amount; generatedAllocations.Add(newAllocation); } if (remainingAmountToAllocate > 0) { return(null); } return(generatedAllocations); }