private bool runCalculateFeesForUnit(IDalSession session, FeeFactory feeFactory, IManagementPeriodUnit unit, out string message) { bool success = false; message = ""; try { switch (unit.ManagementPeriod.ManagementType) { case B4F.TotalGiro.Accounts.ManagementPeriods.ManagementTypes.ManagementFee: unit.Success = feeFactory.CalculateFeePerUnit(session, unit); break; case B4F.TotalGiro.Accounts.ManagementPeriods.ManagementTypes.KickBack: unit.Success = calculateKickBackOnUnit(session, unit, out message); break; } if (!string.IsNullOrEmpty(message)) unit.Message = message; success = session.Update(unit); } catch (Exception ex) { message = Util.GetMessageFromException(ex); string logMessage = string.Format("Error in runCalculateFeesForUnit -> unit: {0}; {1}", unit.Key, message); log.Error(logMessage); } return success; }
private static bool createMgtFeeTransactionForMP(IJournal journalMgmtFee, IGLLookupRecords lookups, int mgtPeriodId, int year, int quarter, ManagementTypes managementType, FeeFactory feeFactory, DateTime minDate, DateTime maxDate, decimal taxPercentage, bool createCashInitiatedOrders) { bool retVal = false; IDalSession session = NHSessionFactory.CreateSession(); Hashtable parameterLists = new Hashtable(1); Hashtable parameters = new Hashtable(2); Util.GetDatesFromQuarter(year, quarter, out minDate, out maxDate); parameterLists.Add("periods", Util.GetPeriodsFromQuarter(year, quarter)); parameters.Add("mgtPeriodId", mgtPeriodId); parameters.Add("managementType", managementType); IList<IManagementPeriodUnit> unitsUnsorted = session.GetTypedListByNamedQuery<IManagementPeriodUnit>( "B4F.TotalGiro.ApplicationLayer.Fee.ManagementPeriodUnitsForMgtFeeTransactionCreation", parameters, parameterLists); if (unitsUnsorted != null && unitsUnsorted.Count > 0) { var unitsByMP = from a in unitsUnsorted group a by a.ManagementPeriod into g select new { ManagementPeriod = g.Key, Units = g.ToList() }; if (unitsByMP.Count() != 1) throw new ApplicationException("The number of management periods is not what is expected."); IManagementPeriod managementPeriod = unitsByMP.First().ManagementPeriod; IList<IManagementPeriodUnit> units = unitsByMP.First().Units; DateTime feeStartDate = minDate; DateTime feeEndDate = maxDate; if (managementPeriod.StartDate > minDate) feeStartDate = managementPeriod.StartDate; if (Util.IsNotNullDate(managementPeriod.EndDate) && managementPeriod.EndDate < maxDate) feeEndDate = managementPeriod.EndDate.Value; if (feeStartDate.Year != feeEndDate.Year) throw new ApplicationException("The year of the start date and end date for the management fee should equal"); string mgtFeeDescription; decimal mgtFeeThreshold; getMgtFeeInfo(managementPeriod, feeEndDate, year, quarter, out mgtFeeDescription, out mgtFeeThreshold); if (units != null && units.Count > 0) { // check the number of units int expectedUnitCount = Util.DateDiff(DateInterval.Month, feeStartDate, feeEndDate) + 1; if (expectedUnitCount != units.Count) throw new ApplicationException(string.Format("The number of units {0} does not equal the number of expected units {1}.", units.Count, expectedUnitCount)); // check if all have the same management period var mps = from a in units group a by a.ManagementPeriod into g select g.Key; if (mps.Count() != 1) throw new ApplicationException("Not all units belong to the same management period."); if (mps.First().Key != managementPeriod.Key) throw new ApplicationException("The management period is not ok."); if (Util.GetPeriodFromDate(managementPeriod.StartDate) == Util.GetPeriodFromDate(feeStartDate) && managementPeriod.StartDate.Day != feeStartDate.Day) throw new ApplicationException(string.Format("The start date of the management period ({0}) does not equal the presented start date ({1}).", managementPeriod.StartDate.ToString("yyyy-MM-dd"), feeStartDate.ToString("yyyy-MM-dd"))); if (managementPeriod.EndDate.HasValue) { if (feeEndDate > managementPeriod.EndDate) throw new ApplicationException(string.Format("The end date of the management period ({0}) does not equal the presented end date ({1}).", managementPeriod.EndDate.Value.ToString("yyyy-MM-dd"), feeEndDate.ToString("yyyy-MM-dd"))); else if (Util.GetPeriodFromDate(managementPeriod.EndDate.Value) == Util.GetPeriodFromDate(feeEndDate) && managementPeriod.EndDate.Value.Day != feeEndDate.Day) throw new ApplicationException(string.Format("The end date of the management period ({0}) does not equal the presented end date ({1}).", managementPeriod.EndDate.Value.ToString("yyyy-MM-dd"), feeEndDate.ToString("yyyy-MM-dd"))); } IAccountTypeCustomer account = (IAccountTypeCustomer)managementPeriod.Account; if (!account.ExitFeePayingAccount.Equals(account)) mgtFeeDescription += string.Format(" voor {0}", account.Number); string nextJournalEntryNumber = JournalEntryMapper.GetNextJournalEntryNumber(session, journalMgmtFee); IMemorialBooking memorialBooking = new MemorialBooking(journalMgmtFee, nextJournalEntryNumber); memorialBooking.TransactionDate = feeEndDate.AddDays(1); memorialBooking.Description = mgtFeeDescription; IManagementFee mgtFee = new ManagementFee(account, feeStartDate, feeEndDate, units, memorialBooking, taxPercentage, lookups); if (mgtFee != null && mgtFee.NettAmount != null && mgtFee.NettAmount.IsNotZero) { if (mgtFee.NettAmount.Quantity < mgtFeeThreshold) { ITradeableInstrument instrument; account = account.ExitFeePayingAccount; if (createCashInitiatedOrders && mgtFee.NeedToCreateCashInitiatedOrder(out instrument)) { if (instrument != null) { OrderAmountBased order = new OrderAmountBased(account, mgtFee.Components.TotalAmount, instrument, true, feeFactory, true); order.OrderInfo = mgtFeeDescription; mgtFee.CashInitiatedOrder = order; } else { // Sell from the biggest position IFundPosition pos = account.Portfolio.PortfolioInstrument.Where(x => x.Size.IsGreaterThanZero).OrderByDescending(x => x.CurrentValue).FirstOrDefault(); if (pos != null && (pos.CurrentBaseValue + mgtFee.NettAmount).IsGreaterThanOrEqualToZero) { OrderAmountBased order = new OrderAmountBased(account, mgtFee.Components.TotalAmount, pos.Instrument, true, feeFactory, true); order.OrderInfo = mgtFeeDescription; mgtFee.CashInitiatedOrder = order; } } } if (mgtFee.CashInitiatedOrder == null && (account.Portfolio.TotalValue() + mgtFee.TotalAmount).IsLessThanZero) throw new ApplicationException(string.Format("Could not create a management fee booking for account {0} since the total portfolio value ({1}) is insufficient.", account.DisplayNumberWithName, account.Portfolio.TotalValue().DisplayString)); mgtFee.BookLines(); GeneralOperationsBookingMapper.Update(session, mgtFee); retVal = true; } } } } session.Close(); return retVal; }