public void AuthorizeOrderRequest(Model.OrderRequest or) { try { decimal?amount; decimal?currAmount; //Amount in ProjectBudget currency using (var context = new SCMSEntities()) { var orItems = context.OrderRequestItems.Where(i => i.OrderRequestId == or.Id).ToList(); foreach (var orItem in orItems) { amount = orItem.EstimatedPrice; currAmount = exchangeRateService.GetForeignCurrencyValue(orItem.ProjectBudget.BudgetCategory.ProjectDonor.Currency, orItem.OrderRequest.Currency, amount, (Guid)or.CountryProgrammeId); orItem.ProjectBudget.TotalCommitted += currAmount; //Add to BudgetCommitment var budgetCommitment = new BudgetCommitment(); budgetCommitment.Id = Guid.NewGuid(); budgetCommitment.OrderRequestItemId = orItem.Id; budgetCommitment.AmountCommitted = (decimal)currAmount; budgetCommitment.DateCommitted = DateTime.Now; budgetCommitment.BudgetLineId = orItem.ProjectBudget.Id; context.BudgetCommitments.Add(budgetCommitment); } context.SaveChanges(); } } catch (Exception ex) { } }
public bool AuthorizePurchaseOrder(Model.PurchaseOrder po) { decimal? amount; decimal? currAmount;//Amount in ProjectBudget currency BudgetCommitment orCommit; using (var context = new SCMSEntities()) { var poItems = context.PurchaseOrderItems.Where(p => p.PurchaseOrderId == po.Id).ToList(); foreach (var poItem in poItems) { amount = poItem.TotalPrice; //Get PO Item amount in Budget Currency currAmount = exchangeRateService.GetForeignCurrencyValue(poItem.ProjectBudget.BudgetCategory.ProjectDonor.Currency, poItem.PurchaseOrder.Currency, amount, (Guid)po.CountryProgrammeId); orCommit = new BudgetCommitment(); orCommit.AmountCommitted = 0; //Get amount committed by OrderRequestItem if (poItem.OrderRequestItem != null) { var commitmentList = poItem.OrderRequestItem.BudgetCommitments.ToList <BudgetCommitment>(); if (commitmentList.Count > 0) { //List should have exactly one item foreach (var commitment in commitmentList) { orCommit = commitment; break; } //Remove initial commitment by OR Item poItem.OrderRequestItem.ProjectBudget.TotalCommitted -= orCommit.AmountCommitted; context.BudgetCommitments.Remove(orCommit); } } //Add commitment by PO Item poItem.ProjectBudget.TotalCommitted += currAmount; //Add to BudgetCommitment table var budgetCommitment = new BudgetCommitment(); budgetCommitment.Id = Guid.NewGuid(); budgetCommitment.PurchaseOrderItemId = poItem.Id; budgetCommitment.AmountCommitted = (decimal)currAmount; budgetCommitment.DateCommitted = DateTime.Now; budgetCommitment.BudgetLineId = poItem.ProjectBudget.Id; context.BudgetCommitments.Add(budgetCommitment); } return((context.SaveChanges() > 0) ? true : false); } }