public static void InitResidual(dynamic budgetItem) { var rate = FXRate.GetPlanRate((Guid)budgetItem.CurrencyId); if (rate.HasValue) { budgetItem.Residual = (decimal)budgetItem.AmountWithoutVAT * (decimal)rate.Value * (1 + (decimal)(budgetItem.VAT / 100)); budgetItem.ResidualWithoutVat = (decimal)budgetItem.AmountWithoutVAT * (decimal)rate.Value; } budgetItem.Fact = 0; budgetItem.FactWithoutVAT = 0; budgetItem.Expense = 0; budgetItem.ExpenseWithoutVAT = 0; }
private static void OnBudgetItemUpdate(EntityMetadata metadata, List <ChangeOperation> changes) { //Полный пересчет если поменялись VAT или CurrencyId var toFullRecalc = changes.Where( i => i.Data.Any( d => d.PropertyName.Equals("VAT", StringComparison.InvariantCultureIgnoreCase) || d.PropertyName.Equals("CurrencyId", StringComparison.InvariantCultureIgnoreCase) )) .Select(i => (Guid)i.Entity.GetId()).ToList(); if (toFullRecalc.Any()) { Calc.RecalculateAccount(toFullRecalc, toFullRecalc.First()); } var changedAmounts = changes.Where( i => i.Data.Any( d => d.PropertyName.Equals("AmountWithoutVAT", StringComparison.InvariantCultureIgnoreCase)) && !toFullRecalc.Contains((Guid)i.Entity.GetId())) .ToList(); if (!changedAmounts.Any()) { return; } var budgetItemIds = changedAmounts.Select(i => (i.Entity as dynamic).Id).Distinct().ToList(); foreach (var budgetItemId in budgetItemIds) { Guid id = budgetItemId; var budgetItem = changes.First(bi => bi.Entity.GetId().Equals(budgetItemId)).Entity as dynamic; var amountToRecalc = changedAmounts.FirstOrDefault(c => (c.Entity as dynamic).Id == id); if (amountToRecalc == null) { continue; } var amountChange = amountToRecalc.Data.First( c => c.PropertyName.Equals("AmountWithoutVAT", StringComparison.InvariantCultureIgnoreCase)); var amountChangeWithVATinUSD = -((decimal)amountChange.NewValue - (decimal)amountChange.InitialValue); var rate = FXRate.GetPlanRate((Guid)budgetItem.CurrencyId); if (rate.HasValue) { amountChangeWithVATinUSD = amountChangeWithVATinUSD * (decimal)rate.Value; } amountChangeWithVATinUSD = amountChangeWithVATinUSD * (1 + (decimal)(budgetItem.VAT / 100)); var recalculateRequestDelta = new RecalculateRequestDelta() { AccountId = budgetItemId, Delta = amountChangeWithVATinUSD }; Calc.RecalculateAccountByDelta(new List <RecalculateRequestDelta>() { recalculateRequestDelta }, budgetItemId); } }
protected override List <Tuple <string, List <dynamic> > > Recalc(Tuple <List <dynamic>, List <dynamic> > accounts, Tuple <List <dynamic>, List <dynamic> > externalTransactions, Tuple <List <dynamic>, List <dynamic> > internalTransactions) { var rates = new Dictionary <Guid, double>(); var result = new List <Tuple <string, List <dynamic> > >(); var budgetItems = accounts.Item1; var invoiceBudgetItems = externalTransactions.Item2; var invoices = externalTransactions.Item1; var biSave = new Tuple <string, List <dynamic> >("BudgetItem", new List <dynamic>()); result.Add(biSave); var budgets = DynamicRepository.GetByEntity("Budget"); foreach (var budgetItem in budgetItems) { dynamic item = budgetItem; var currencyId = (Guid)item.CurrencyId; double rate = 0; if (rates.ContainsKey(currencyId)) { rate = rates[currencyId]; } else { var res = FXRate.GetPlanRate(currencyId); if (!res.HasValue) { throw new Exception(string.Format("Rate with Id = '{0}' not found", currencyId)); } rate = res.Value; rates.Add(currencyId, rate); } var amountInUsdWithVat = (decimal)item.AmountWithoutVAT * (decimal)rate * (1 + (decimal)(item.VAT / 100)); //С НДС var factSum = invoiceBudgetItems.Where( id => id.BudgetItemId.Equals(item.Id) && (id.NextPeriodYear == null || budgets.Any(c => c.Name == id.NextPeriodYear)) && invoices.Any(i => i.Id.Equals(id.InvoiceId) && i.State == (byte)InvoiceStatus.Paid)).Select(id => (decimal)id.Amount).Sum(); var residualWithVat = amountInUsdWithVat - factSum; var residualWithoutVat = residualWithVat / (1 + (decimal)(item.VAT / 100)); var factWithVat = factSum; var factWithoutVat = factWithVat / (1 + (decimal)(item.VAT / 100)); //if (item.Residual != residualWithVat || item.ResidualWithoutVAT != residualWithoutVat) //{ item.Residual = residualWithVat; item.ResidualWithoutVAT = residualWithoutVat; item.Fact = factWithVat; item.FactWithoutVAT = factWithoutVat; item.Expense = 0m; item.ExpenseWithoutVAT = 0m; biSave.Item2.Add(item); //} } return(result); }