private List <PaymentAmountResult> GetNextStagesPaymentAmounts( PaymentAmountResult fResult, PgPaymentRule paymentRule, IEnumerable <StageModel> nextStages) { var isAipeProjection = paymentRule.IsAIPE && fResult.StageName == CostStages.Aipe.ToString(); var result = new List <PaymentAmountResult>(); var currentResult = fResult; var currentInput = paymentRule; foreach (var nextStage in nextStages) { var input = new PgPaymentRule { // we only need these values for next stage projection calculation CostStages = nextStage.Key, // paymentRule already has current total - old payments. We only need to subtract // the freshly calculated values for this stage InsuranceCost = currentInput.InsuranceCost - currentResult.InsuranceCostPayment, OtherCost = currentInput.OtherCost - currentResult.OtherCostPayment, PostProductionCost = currentInput.PostProductionCost - currentResult.PostProductionCostPayment, ProductionCost = currentInput.ProductionCost - currentResult.ProductionCostPayment, TargetBudgetTotalCost = currentInput.TargetBudgetTotalCost - currentResult.TargetBudgetTotalPayment, TechnicalFeeCost = currentInput.TechnicalFeeCost - currentResult.TechnicalFeeCostPayment, TalentFeeCost = currentInput.TalentFeeCost - currentResult.TalentFeeCostPayment, IsAIPE = currentInput.IsAIPE, CostCarryOverAmount = currentResult.CostCarryOverAmount, TotalCost = currentInput.TotalCost - currentResult.TotalCostAmountPayment, TotalCostAmount = currentResult.TotalCostAmount ?? 0m }; var previousPayment = new CostStageRevisionTotalPayments { InsuranceCostPayments = currentResult.InsuranceCostPayment ?? 0, TalentFeeCostPayments = currentResult.TalentFeeCostPayment ?? 0, TechnicalFeeCostPayments = currentResult.TechnicalFeeCostPayment ?? 0, PostProductionCostPayments = currentResult.PostProductionCostPayment ?? 0, ProductionCostPayments = currentResult.ProductionCostPayment ?? 0, OtherCostPayments = currentResult.OtherCostPayment ?? 0, CarryOverAmount = currentResult.CostCarryOverAmount ?? 0, TargetBudgetTotalCostPayments = currentResult.TargetBudgetTotalPayment ?? 0, TotalCostPayments = currentResult.TotalCostAmountPayment ?? 0 }; currentResult = CalculateStagePaymentAmountsWithRule(fResult.MatchedPaymentRule, nextStage.Key, input, true, previousPayment, isAipeProjection, isNextStageCalc: true); currentInput = input; result.Add(currentResult); } return(result); }
private PaymentAmountResult TransformPaymentResult(List <CostStageRevisionPaymentTotal> dbResults) { var result = new PaymentAmountResult { IsDetailedSplit = dbResults.Count > 1, TotalCostAmount = dbResults.First(x => x.LineItemTotalType == Constants.CostSection.CostTotal).LineItemFullCost }; foreach (var p in dbResults) { switch (p.LineItemTotalType) { case Constants.CostSection.CostTotal: result.TotalCostAmountPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.InsuranceTotal: result.InsuranceCostPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.Other: result.OtherCostPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.PostProduction: result.PostProductionCostPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.Production: result.ProductionCostPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.TargetBudgetTotal: result.TargetBudgetTotalPayment = p.LineItemTotalCalculatedValue; break; case Constants.CostSection.TechnicalFee: result.TechnicalFeeCostPayment = p.LineItemTotalCalculatedValue; break; } } return(result); }
public async Task Create_Notification_For_Coupa_For_Non_NA_Non_Cyclone_Agency_When_Cost_Total_Amount_Decreased() { //Arrange decimal?previousPaymentAmount = 10.00M; decimal?latestPaymentAmount = 5.00M; //Decreased from 10 var timestamp = DateTime.UtcNow; var cost = new Cost(); var costOwner = new CostUser(); var latestRevision = new CostStageRevision(); var previousRevisionId = Guid.NewGuid(); var latestRevisionId = Guid.NewGuid(); SetupPurchaseOrderCost(cost, latestRevision, costOwner, previousRevisionId, latestRevisionId, PoNumber); var costUsers = new CostNotificationUsers { CostOwner = costOwner }; var previousPaymentResult = new PaymentAmountResult { TotalCostAmount = previousPaymentAmount }; var latestPaymentResult = new PaymentAmountResult { TotalCostAmount = latestPaymentAmount }; PgPaymentServiceMock.Setup(ppsm => ppsm.GetPaymentAmount(previousRevisionId, false)).Returns(Task.FromResult(previousPaymentResult)); PgPaymentServiceMock.Setup(ppsm => ppsm.GetPaymentAmount(latestRevisionId, false)).Returns(Task.FromResult(latestPaymentResult)); //Act IEnumerable <EmailNotificationMessage <CostNotificationObject> > result = await EmailNotificationBuilder.BuildPendingBrandApprovalNotification(costUsers, cost, latestRevision, timestamp); //Assert result.Should().NotBeNull(); var notifications = result.ToArray(); notifications.Should().HaveCount(1); }
private async Task SaveTotals(Guid costStageRevisionId, PaymentAmountResult paymentAmount, PgPaymentRule payment, bool isFirstFA = true) { // save the totals here var toSave = new List <CostStageRevisionPaymentTotal>(); if (paymentAmount.IsDetailedSplit) { toSave.AddRange(new List <CostStageRevisionPaymentTotal> { new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.InsuranceCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.InsuranceCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.InsuranceCostTotal, LineItemTotalType = Constants.CostSection.InsuranceTotal, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName, CalculatedAt = DateTime.UtcNow }, new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.OtherCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.OtherCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.OtherCostTotal, LineItemTotalType = Constants.CostSection.Other, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName }, new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.PostProductionCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.PostProductionCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.PostProductionCostTotal, LineItemTotalType = Constants.CostSection.PostProduction, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName }, new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.ProductionCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.ProductionCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.ProductionCostTotal, LineItemTotalType = Constants.CostSection.Production, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName }, new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.TargetBudgetTotalPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.TargetBudgetTotalCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.TargetBudgetTotal, LineItemTotalType = Constants.CostSection.TargetBudgetTotal, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName }, new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.TechnicalFeeCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.TechnicalFeeCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.TechnicalFeeCostTotal, LineItemTotalType = Constants.CostSection.TechnicalFee, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName } }); if (paymentAmount.TalentFeeCostPayment.HasValue) { toSave.Add(new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.TalentFeeCostPayment.GetValueOrDefault(0), LineItemRemainingCost = payment.TalentFeeCost.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.TalentFeeCostTotal, LineItemTotalType = Constants.CostSection.TalentFees, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName }); } } if (isFirstFA) { toSave.Add(new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = paymentAmount.TotalCostAmountPayment.GetValueOrDefault(0), LineItemRemainingCost = paymentAmount.CostCarryOverAmount.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.TotalCostAmountTotal, LineItemTotalType = Constants.CostSection.CostTotal, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName } ); } else { toSave.Add(new CostStageRevisionPaymentTotal { CostStageRevisionId = costStageRevisionId, LineItemTotalCalculatedValue = payment.TotalCost.GetValueOrDefault(0), LineItemRemainingCost = paymentAmount.CostCarryOverAmount.GetValueOrDefault(0), LineItemFullCost = payment.StageTotals.TotalCostAmountTotal, LineItemTotalType = Constants.CostSection.CostTotal, IsProjection = paymentAmount.IsProjection, StageName = paymentAmount.StageName } ); } foreach (var total in toSave) { total.CalculatedAt = DateTime.UtcNow; } await _costStageRevisionService.SaveCostStageRevisionPaymentTotals(toSave); }
private PaymentAmountResult CalculateStagePaymentAmountsWithRule( dataAccess.Entity.Rule paymentRule, string costStageKey, PgPaymentRule input, bool isProjection, CostStageRevisionTotalPayments previousPayments, bool isAipeProjection = false, CostSectionTotals rawTotals = null, bool isNextStageCalc = false ) { var res = new PaymentAmountResult(); var pgPaymentRuleData = JsonConvert.DeserializeObject <PgPaymentRuleDefinition>(paymentRule.Definition); res.IsDetailedSplit = pgPaymentRuleData.DetailedSplit; var costStage = (CostStages)Enum.Parse(typeof(CostStages), costStageKey); decimal?totalCalculatedValue; res.CostCarryOverAmount = 0; // do the specific calculations here: // - sum up all the individual costs // -- if it is an AIPE projection (projection of any stage triggered in AIPE stage) - we only have TargetBudgetTotal, so use that anyways if (pgPaymentRuleData.DetailedSplit && !isAipeProjection) { // ADC-2243: run through the payment allocation rule to ensure the 'input' is correctly allocated //ADC-2711: check null and get 0 res.InsuranceCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.InsuranceTotal, costStage) ?? 0; var talentFeeCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TalentFees, costStage); if (talentFeeCostPayment.HasValue) { res.TalentFeeCostPayment = talentFeeCostPayment; } res.PostProductionCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.PostProduction, costStage) ?? 0; res.ProductionCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Production, costStage) ?? 0; res.TechnicalFeeCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TechnicalFee, costStage) ?? 0; res.OtherCostPayment = GetAllocatedSplitPayment(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Other, costStage) ?? 0; res.TargetBudgetTotalPayment = 0m; // AIPE specific handling of target budget if (costStage == CostStages.Aipe) { res.TargetBudgetTotalPayment = input.TargetBudgetTotalCost * (pgPaymentRuleData.GetSplitByNameAndStage(Constants.CostSection.TargetBudgetTotal, costStage) ?? 0); } totalCalculatedValue = res.InsuranceCostPayment + res.OtherCostPayment + res.PostProductionCostPayment + res.ProductionCostPayment + res.TargetBudgetTotalPayment + res.TechnicalFeeCostPayment; //ADC-2711: if DPV then exclude postproduction cost due to it has already included into Othercost in GetAllocation method if (paymentRule.Type == RuleType.VendorPayment) { totalCalculatedValue = totalCalculatedValue - res.PostProductionCostPayment; } // we only need carry over amounts for detailed splits - for non detailed splits all payments are already contained within TotalCost if (costStage == CostStages.Aipe) { // we store it as a carry over for other stages to deduct from // only if the calculated value is > 0 - otherwise no payment is made in this stage -> no carry over amount res.CostCarryOverAmount = totalCalculatedValue > 0 ? input.TotalCostAmount - totalCalculatedValue : 0; } else if (input.IsAIPE) { if (input.CostCarryOverAmount > 0) { if (input.CostCarryOverAmount - totalCalculatedValue > 0) { // deduct current payment from the "pot" and store the remainder res.CostCarryOverAmount = input.CostCarryOverAmount - totalCalculatedValue; } else { res.CostCarryOverAmount = 0; } } // we need to subtract current payment from previous target budget total payments to never lose them totalCalculatedValue = totalCalculatedValue - input.CostCarryOverAmount; } else { if (totalCalculatedValue < 0) { // carry over amount will be negative in non-aipe overpayment cases res.CostCarryOverAmount = totalCalculatedValue; } } } else if (isAipeProjection && costStage == CostStages.FinalActual) { // we pretend to pay the rest on AIPE FA stage since detailed rules won't have a split for Cost Total totalCalculatedValue = input.TotalCost; } else { totalCalculatedValue = input.TotalCost * (pgPaymentRuleData.GetSplitByNameAndStage(Constants.CostSection.CostTotal, costStage) ?? 0); } // - if the stage = Final Actual - return whatever result (even if negative) // - otherwise - if <=0 return 0 if (totalCalculatedValue < 0 && costStage != CostStages.FinalActual && costStage != CostStages.FinalActualRevision) { res.TotalCostAmountPayment = 0; } else { if ((costStage == CostStages.FinalActual || costStage == CostStages.FinalActualRevision) && input.CostCarryOverAmount < 0) { // previous stage resulted in a negative calculation, despite us reporting 0, we still need to subtract that negative value totalCalculatedValue += input.CostCarryOverAmount; } res.TotalCostAmountPayment = totalCalculatedValue; } res.TotalCostAmount = totalCalculatedValue; res.MatchedPaymentRule = paymentRule; res.IsProjection = isProjection; res.StageName = input.CostStages; CostSectionTotals totals = null; if (rawTotals != null) { //ADC-2711 overwrite line_item_full_cost values to display on payment summary screen if (paymentRule.Type == RuleType.VendorPayment && isNextStageCalc == false) { var insuranceCostPaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.InsuranceTotal) ?? 0; var postProductionCostPaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.PostProduction) ?? 0; var productionCostPaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.Production) ?? 0; var technicalFeeCostPaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.TechnicalFee) ?? 0; var otherCostPaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.Other) ?? 0; var talentFeePaymentAllocation = GetAllocation(input, paymentRule.Type, Constants.CostSection.TalentFees) ?? 0; var insuranceCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.InsuranceTotal, costStage) ?? 0; var postProductionCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.PostProduction, costStage) ?? 0; var productionCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Production, costStage) ?? 0; var technicalFeeCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TechnicalFee, costStage) ?? 0; var otherCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Other, costStage) ?? 0; var talentFeeCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TalentFees, costStage) ?? 0; totals = new CostSectionTotals { // Recalculate allocated amount because it depends on the rule InsuranceCostTotal = GetDisplayedValueOnPaymentSummaryPage(insuranceCostPaymentAllocation, insuranceCostTotal), PostProductionCostTotal = GetDisplayedValueOnPaymentSummaryPage(postProductionCostPaymentAllocation, postProductionCostTotal), ProductionCostTotal = GetDisplayedValueOnPaymentSummaryPage(productionCostPaymentAllocation, productionCostTotal), TechnicalFeeCostTotal = GetDisplayedValueOnPaymentSummaryPage(technicalFeeCostPaymentAllocation, technicalFeeCostTotal), OtherCostTotal = GetDisplayedValueOnPaymentSummaryPage(otherCostPaymentAllocation - postProductionCostPaymentAllocation, otherCostTotal - postProductionCostTotal), TalentFeeCostTotal = GetDisplayedValueOnPaymentSummaryPage(talentFeePaymentAllocation, talentFeeCostTotal), TargetBudgetTotal = input.TargetBudgetTotalCost ?? 0 + previousPayments.TargetBudgetTotalCostPayments, TotalCostAmountTotal = input.TotalCostAmount }; } else { totals = new CostSectionTotals { // Recalculate allocated amount because it depends on the rule InsuranceCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.InsuranceTotal, costStage) ?? 0, PostProductionCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.PostProduction, costStage) ?? 0, ProductionCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Production, costStage) ?? 0, TechnicalFeeCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TechnicalFee, costStage) ?? 0, OtherCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Other, costStage) ?? 0, TalentFeeCostTotal = GetAllocatedAmount(rawTotals, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TalentFees, costStage) ?? 0, TargetBudgetTotal = input.TargetBudgetTotalCost ?? 0 + previousPayments.TargetBudgetTotalCostPayments, TotalCostAmountTotal = input.TotalCostAmount }; } } var paymentTotals = new CostSectionTotals { // Recalculate allocated amount because it depends on the rule TalentFeeCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TalentFees, costStage) ?? 0, InsuranceCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.InsuranceTotal, costStage) ?? 0, PostProductionCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.PostProduction, costStage) ?? 0, ProductionCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Production, costStage) ?? 0, TechnicalFeeCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.TechnicalFee, costStage) ?? 0, OtherCostTotal = GetAllocatedAmount(input, paymentRule.Type, pgPaymentRuleData, Constants.CostSection.Other, costStage) ?? 0, TargetBudgetTotal = input.TargetBudgetTotalCost ?? 0 + previousPayments.TargetBudgetTotalCostPayments, TotalCostAmountTotal = input.TotalCostAmount }; res.TotalRemainingPayment = new PgPaymentRule { BudgetRegion = input.BudgetRegion, ContentType = input.ContentType, CostType = input.CostType, CostStages = input.CostStages, ProductionType = input.ProductionType, DirectPaymentVendorId = input.DirectPaymentVendorId, IsAIPE = input.IsAIPE, TotalCostAmount = input.TotalCostAmount, TotalCost = input.TotalCost, TargetBudgetTotalCost = input.TargetBudgetTotalCost, CostCarryOverAmount = input.CostCarryOverAmount, // Recalculate remaining payment amount because allocated amount depends on macthed rule StageTotals = totals, InsuranceCost = paymentTotals.InsuranceCostTotal - previousPayments.InsuranceCostPayments, TalentFeeCost = paymentTotals.TalentFeeCostTotal - previousPayments.TalentFeeCostPayments, PostProductionCost = paymentTotals.PostProductionCostTotal - previousPayments.PostProductionCostPayments, ProductionCost = paymentTotals.ProductionCostTotal - previousPayments.ProductionCostPayments, TechnicalFeeCost = paymentTotals.TechnicalFeeCostTotal - previousPayments.TechnicalFeeCostPayments, OtherCost = paymentTotals.OtherCostTotal - previousPayments.OtherCostPayments }; return(res); }