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); } }
public bool CommitFunds(PaymentRequest rfp) { decimal rfpCommited = 0; decimal poCommitAmt = 0;//this amount is in the currency of the rfp; using (var context = SCMSEntities.Define()) { using (TransactionScope scope = new TransactionScope()) { try { var po = context.PurchaseOrders.FirstOrDefault(p => p.Id == rfp.PurchaseOrderId); var poItemCommits = context.BudgetCommitments.Where(c => c.PurchaseOrderItem.PaymentRequestBudgetLines.Where(p => p.PaymentRequestId == rfp.Id).Count() > 0).ToList(); //REMOVE FUNDS FROM PO COMMITTED foreach (var poItemCommit in poItemCommits) { var rfpBL = poItemCommit.PurchaseOrderItem.PaymentRequestBudgetLines.FirstOrDefault(p => p.PurchaseOrderItemId == poItemCommit.PurchaseOrderItemId); if (rfpBL == null) { continue; } //Get committed amount in rfp currency poCommitAmt = (decimal)exchangeRateService.GetForeignCurrencyValue(po.Currency, poItemCommit.ProjectBudget.BudgetCategory.ProjectDonor.Currency, poItemCommit.AmountCommitted, rfp.CountryProgrammeId); rfpCommited += poCommitAmt; var pb = context.ProjectBudgets.FirstOrDefault(p => p.Id == poItemCommit.BudgetLineId); if (rfpBL.Amount >= poCommitAmt) { //Delete commitment context.BudgetCommitments.Remove(poItemCommit); //Deduct value from budget commitment pb.TotalCommitted -= poItemCommit.AmountCommitted; } else { //convert rfpBL.Amount to project budget currency and deduct it poCommitAmt = (decimal)exchangeRateService.GetForeignCurrencyValue(poItemCommit.ProjectBudget.BudgetCategory.ProjectDonor.Currency, po.Currency, rfpBL.Amount, rfp.CountryProgrammeId); //Deduct this amount from budget commitments and also from project budget commitments poItemCommit.AmountCommitted -= poCommitAmt; pb.TotalCommitted -= poCommitAmt; } } //MOVE FUNDS TO COMMITTED var rfpBudgetLines = context.PaymentRequestBudgetLines.Where(p => p.PaymentRequestId == rfp.Id).ToList(); foreach (var rfpBL in rfpBudgetLines) { var budgetLine = context.ProjectBudgets.FirstOrDefault(pb => pb.Id == rfpBL.BudgetLineId); var newCommit = new BudgetCommitment(); newCommit.Id = Guid.NewGuid(); newCommit.AmountCommitted = (decimal)exchangeRateService.GetForeignCurrencyValue(budgetLine.BudgetCategory.ProjectDonor.Currency, po.Currency, rfpBL.Amount, rfp.CountryProgrammeId); newCommit.DateCommitted = DateTime.Now; newCommit.RFPBudgetLineId = rfpBL.Id; newCommit.BudgetLineId = budgetLine.Id; context.BudgetCommitments.Add(newCommit); budgetLine.TotalCommitted += newCommit.AmountCommitted; } //SAVE ALL CHANGES if (context.SaveChanges() > 0) { scope.Complete(); return(true); } else { scope.Dispose(); return(false); } } catch (Exception ex) { scope.Dispose(); throw ex; } } } }