/// <summary> /// Creates a ValuationCashMutation from a previous ValuationCashMutation (with older date) /// </summary> /// <param name="mutationDate">The date for the new valuation mutation</param> /// <param name="prevCashMutation">The previous mutation</param> internal ValuationCashMutation(DateTime mutationDate, IValuationCashMutation prevCashMutation) { if (prevCashMutation != null) { this.Date = mutationDate; this.PreviousCashMutation = prevCashMutation; this.ValuationCashType = prevCashMutation.ValuationCashType; this.Account = prevCashMutation.Account; this.Instrument = prevCashMutation.Instrument; this.AmountToDate = prevCashMutation.AmountToDate; this.BaseAmountToDate = prevCashMutation.BaseAmountToDate; } else throw new ApplicationException("Previous cash valuation can not be null"); }
private IValuationCashMutation getNearestMutation(ref IValuationCashMutation[] mutationsToSave, IJournalEntryLine line, int matchlevel) { IValuationCashMutation mut = null; TimeSpan days = TimeSpan.MaxValue; string lineKey; string mutKey; switch (matchlevel) { case 1: lineKey = string.Format("{0}", line.Currency.Key.ToString()); break; case 2: lineKey = string.Format("{0}-{1}", line.Currency.Key.ToString(), (line.BookingRelatedInstrument != null ? line.BookingRelatedInstrument.Key.ToString() : "0")); break; default: // 3 lineKey = string.Format("{0}-{1}-{2}", line.Currency.Key.ToString(), (line.BookingRelatedInstrument != null ? line.BookingRelatedInstrument.Key.ToString() : "0"), line.GLAccount.ValuationCashType.ToString()); break; } foreach (IValuationCashMutation mutation in mutationsToSave) { switch (matchlevel) { case 1: mutKey = string.Format("{0}", mutation.Amount.Underlying.Key.ToString()); break; case 2: mutKey = string.Format("{0}-{1}", mutation.Amount.Underlying.Key.ToString(), (mutation.Instrument != null ? mutation.Instrument.Key.ToString() : "0")); break; default: // 3 mutKey = string.Format("{0}-{1}-{2}", mutation.Amount.Underlying.Key.ToString(), (mutation.Instrument != null ? mutation.Instrument.Key.ToString() : "0"), mutation.ValuationCashType.ToString()); break; } if (lineKey.Equals(mutKey)) { //if (mut != null && ((TimeSpan)(mutation.Date - posTx.TransactionDate)).Days > 0) // return mut; if (mut == null || (days < (mutation.Date - line.BookDate))) { mut = mutation; days = mut.Date - line.BookDate; } if (days.Days >= 0) return mut; } } return mut; }
private void addNotRelevantLinesToCashMutations(ref IValuationCashMutation[] cashMutationsToSave, IList<IJournalEntryLine> notRelevantLines) { if (cashMutationsToSave != null && cashMutationsToSave.Length > 0 && notRelevantLines.Count > 0) { foreach (IJournalEntryLine line in notRelevantLines) { IValuationCashMutation mutation = getNearestMutation(ref cashMutationsToSave, line, 3); if (mutation == null) mutation = getNearestMutation(ref cashMutationsToSave, line, 2); if (mutation == null) mutation = getNearestMutation(ref cashMutationsToSave, line, 1); if (mutation == null) mutation = cashMutationsToSave[0]; if (mutation != null) mutation.AddNotRelevantLine(line); } } }
private void saveMutations(IDalSession session, ref List<IValuationMutation> mutationsToSave, ref IValuationCashMutation[] cashMutationsToSave) { if (mutationsToSave != null && mutationsToSave.Count > 0 && cashMutationsToSave != null && cashMutationsToSave.Length > 0) { object[] mutations = new object[mutationsToSave.Count + cashMutationsToSave.Length]; mutationsToSave.ToArray().CopyTo(mutations, 0); cashMutationsToSave.CopyTo(mutations, mutationsToSave.Count); session.Insert(mutations); } else if (mutationsToSave != null && mutationsToSave.Count > 0) { session.Insert(mutationsToSave); } else if (cashMutationsToSave != null && cashMutationsToSave.Length > 0) { session.Insert(cashMutationsToSave); } }
private void runCashValuationMutationJobForAccount(IDalSession session, IList<IJournalEntryLine> lines, IAccountTypeCustomer account, DateTime maxDate, out IValuationCashMutation[] cashMutationsToSave) { //raiseProgressEvent(string.Format("Create Cash valuations for {0}", account.DisplayNumberWithName)); cashMutationsToSave = null; IDictionary<string, IValuationCashMutation> mutations = new Dictionary<string, IValuationCashMutation>(); IValuationCashMutation mutation; IList<IJournalEntryLine> notRelevantLines = new List<IJournalEntryLine>(); if (lines != null && lines.Count > 0) { foreach (IJournalEntryLine line in lines.Where(u => u.GLAccount.ValuationCashType != ValuationCashTypes.None)) { if (line.BookDate <= maxDate && (Util.IsNullDate(account.ValuationsEndDate) || line.BookDate <= account.ValuationsEndDate)) { if (line.IsRelevant) { ValuationCashMutationKey key = new ValuationCashMutationKey((IAccountTypeCustomer)line.GiroAccount, line.BookingRelatedInstrument, line.GLAccount.ValuationCashType); // search key in account // It's possible because of storno's or earlier tx that there is no LastCashMutation // In this case create new CashMutation if (account.LastValuationCashMutations.Count > 0 && account.LastValuationCashMutations.ContainsKey(key) && account.LastValuationCashMutations[key].LastCashMutation != null) { ILastValuationCashMutationHolder holder = account.LastValuationCashMutations[key]; // If pos Date is earlier than last recorded date -> skip it if (holder.LastCashMutation.Date <= line.BookDate) { // Is the TxDate the same if (holder.LastCashMutation.Date.Equals(line.BookDate)) { // check whether line not already stored if (!holder.LastCashMutation.ContainsLine(line)) mutation = holder.LastCashMutation; else mutation = null; } else { mutation = new ValuationCashMutation(line.BookDate, holder.LastCashMutation); } } else mutation = null; } else { // Create Complete New Cash Valuation mutation = new ValuationCashMutation(line); } if (mutation != null) { mutation.AddLine(line); if (account.LastValuationCashMutations.ContainsKey(key)) account.LastValuationCashMutations[key].LastCashMutation = mutation; else account.LastValuationCashMutations.Add(key, mutation); if (!mutations.Keys.Contains(mutation.GetUniqueCode)) mutations.Add(mutation.GetUniqueCode, mutation); } } else notRelevantLines.Add(line); } } if (mutations != null && mutations.Count > 0) { cashMutationsToSave = new IValuationCashMutation[mutations.Count]; mutations.Values.CopyTo(cashMutationsToSave, 0); if (notRelevantLines.Count > 0) addNotRelevantLinesToCashMutations(ref cashMutationsToSave, notRelevantLines); // Validate mutations foreach (IValuationCashMutation mut in cashMutationsToSave) mut.Validate(); } } }
internal LastValuationCashMutationHolder(IAccountTypeCustomer account, ITradeableInstrument instrument, ValuationCashTypes valuationCashType, IValuationCashMutation lastCashMutation) { this.CashMutKey = new ValuationCashMutationKey(account, instrument, valuationCashType); this.LastCashMutation = lastCashMutation; }
internal LastValuationCashMutationHolder(ValuationCashMutationKey key, IValuationCashMutation lastCashMutation) { this.CashMutKey = key; this.LastCashMutation = lastCashMutation; }
internal JournalEntryLineValuationCashMapping(IJournalEntryLine line, IValuationCashMutation mutation) { this.key = line.Key; this.ValuationCashMutation = mutation; this.IsRelevant = line.IsRelevant; }