public static int CancelInstructions(int[] instructionIds) { IDalSession session = NHSessionFactory.CreateSession(); int instructionsCancelled = 0; // Initialize the Engine //IFeeFactory fees = FeeFactory.GetInstance(session); InstructionEngine engine = new InstructionEngine(); IList<IInstruction> instructions = InstructionMapper.GetInstructions(session, instructionIds); if (instructions != null && instructions.Count > 0) { session.BeginTransaction(); foreach (IInstruction instruction in instructions) { if (engine.CancelInstruction(instruction)) { InstructionMapper.Update(session, instruction); instructionsCancelled++; if (instruction.UpdateableOrders != null && instruction.UpdateableOrders.Count > 0) OrderMapper.Update(session, instruction.UpdateableOrders); } } session.CommitTransaction(); } session.Close(); return instructionsCancelled; }
public static void SaveCustomerAccount(AccountDetails saveValue, bool forceClosedStatus, out bool askConfirmation, out string message) { if (!SecurityManager.IsCurrentUserInRole("Data Mtce: Account Edit")) throw new System.Security.SecurityException("You are not authorized to update account details."); IDalSession session = NHSessionFactory.CreateSession(); message = ""; try { ICustomerAccount acc = null; bool isNewAccount = false; IPortfolioModel model = null; IPandHouder pandhouder = null; IVerpandSoort verpandSoort = null; ICounterAccount counterAccount = null; IInternalEmployeeLogin relatedEmployee = null; IRemisierEmployee remisierEmployee = null; IAccountTypeCustomer exitFeePayer = null; Money firstPromisedDeposit = null; DateTime upDateTime = session.GetServerTime(); IInternalEmployeeLogin employee = (IInternalEmployeeLogin)LoginMapper.GetCurrentLogin(session); if (saveValue.AccountNrID == int.MinValue) isNewAccount = true; if (saveValue.PandHouderID != int.MinValue) pandhouder = PandHouderMapper.GetPandHouder(session, saveValue.PandHouderID); if (saveValue.VerpandSoortID != int.MinValue) verpandSoort = VerpandSoortMapper.GetVerpandSoort(session, saveValue.VerpandSoortID); if (saveValue.CounterAccountID != int.MinValue) counterAccount = CounterAccountMapper.GetCounterAccount(session, saveValue.CounterAccountID); if (saveValue.Modelid != int.MinValue) model = ModelMapper.GetModel(session, saveValue.Modelid); if (saveValue.ExitFeePayingAccountID != int.MinValue) exitFeePayer = (IAccountTypeCustomer)AccountMapper.GetAccount(session, saveValue.ExitFeePayingAccountID); if (saveValue.RemisierEmployeeID != int.MinValue) remisierEmployee = RemisierEmployeeMapper.GetRemisierEmployee(session, saveValue.RemisierEmployeeID); if (saveValue.RelatedEmployeeID != int.MinValue) relatedEmployee = (IInternalEmployeeLogin)LoginMapper.GetLogin(session, saveValue.RelatedEmployeeID); if (saveValue.FirstPromisedDeposit > 0M) { ICurrency baseCurrency = InstrumentMapper.GetBaseCurrency(session); firstPromisedDeposit = new Money(saveValue.FirstPromisedDeposit, baseCurrency); } IAssetManager am = ManagementCompanyMapper.GetAssetManager(session, saveValue.AssetManagerKey); if (isNewAccount) { //string newAccountNr = am.GenerateAccountNumber(); //acc = new CustomerAccount(newAccountNr, "", am, model); } else { acc = (ICustomerAccount)AccountMapper.GetAccount(session, saveValue.AccountNrID); acc.Family = AccountFamilyMapper.GetAccountFamily(session, saveValue.FamilyID); if (!AccountMapper.AccountStatusIsOpen(session, acc.Status)) throw new ApplicationException("Could not save account because its status is already closed."); acc.AccountOwner = am; } askConfirmation = false; if (!isNewAccount && !AccountMapper.AccountStatusIsOpen(session, saveValue.Status)) { //IList positions = AccountMapper.GetPositions(session, acc, PositionReturnClass.AllPositions, PositionsView.NotZero); //if (positions.Count > 0) // throw new ApplicationException( // string.Format("Could not close account because it has {0} open position{1} ({2} total portfolio value).", // positions.Count, (positions.Count > 1 ? "s" : ""), acc.TotalPositionAmount(PositionAmountReturnValue.All))); //if (!forceClosedStatus) //{ // askConfirmation = true; // return; //} } if (am.SupportLifecycles && ((acc.Lifecycle != null ? acc.Lifecycle.Key : int.MinValue) != saveValue.LifecycleId)) { if (saveValue.LifecycleId != int.MinValue) AccountOverviewAdapter.CheckLifecycleForAccount(acc, acc.PrimaryAccountHolder); acc.Lifecycle = saveValue.LifecycleId != int.MinValue ? LifecycleMapper.GetLifecycle(session, saveValue.LifecycleId) : null; } if ((acc.ModelPortfolio == null && saveValue.Modelid != int.MinValue ) || (acc.ModelPortfolio != null && acc.ModelPortfolio.Key != saveValue.Modelid) || acc.IsExecOnlyCustomer != saveValue.IsExecOnlyCustomer || acc.EmployerRelationship != saveValue.EmployerRelationship || isNewAccount) { if (acc.ModelPortfolio != null && model == null && acc.TradeableStatus == Tradeability.Tradeable) throw new ApplicationException("The Model is mandatory when the account is tradeable."); if (acc.ActiveRebalanceInstructions != null && acc.ActiveRebalanceInstructions.Count > 0) { foreach (IInstruction instruction in acc.ActiveRebalanceInstructions) { if (instruction.Status > 1) throw new ApplicationException(string.Format("It is currently not possible to change the Model since an active rebalance instruction exists for account {0}.", acc.DisplayNumberWithName)); } } acc.SetModelPortfolio(acc.Lifecycle, model, saveValue.IsExecOnlyCustomer, saveValue.EmployerRelationship, employee, DateTime.Now); } acc.IsJointAccount = saveValue.IsJointAccount; acc.ShortName = saveValue.AccountShortName; acc.VerpandSoort = verpandSoort; acc.PandHouder = pandhouder; acc.CounterAccount = counterAccount; acc.ExitFeePayingAccount = exitFeePayer; acc.EmployerRelationship = saveValue.EmployerRelationship; acc.RelatedEmployee = relatedEmployee; acc.FirstPromisedDeposit = firstPromisedDeposit; DateTime originalMgtEndDate = acc.ManagementEndDate; acc.FinalManagementEndDate = saveValue.FinalManagementEndDate; if (acc.TradeableStatus != saveValue.TradeableStatus) { // check if (saveValue.TradeableStatus == Tradeability.Tradeable && acc.ModelPortfolio == null) throw new ApplicationException("A Model is mandatory on the account to be tradeable."); acc.DateTradeabilityStatusChanged = upDateTime; acc.TradeableStatus = saveValue.TradeableStatus; } if (acc.Status != saveValue.Status) { acc.LastDateStatusChanged = upDateTime; acc.Status = saveValue.Status; if (!AccountMapper.AccountStatusIsOpen(session, acc.Status)) { // Set Closed Model if (acc.AccountOwner.CompanyType == ManagementCompanyType.AssetManager) { IPortfolioModel closedModel = ((IAssetManager)acc.AccountOwner).ClosedModelPortfolio; if (closedModel != null) acc.SetModelPortfolio(acc.Lifecycle, closedModel, acc.IsExecOnlyCustomer, acc.EmployerRelationship, employee, acc.LastDateStatusChanged.Date); } DateTime finalEndDate = Util.IsNotNullDate(acc.ManagementEndDate) ? acc.ManagementEndDate : acc.LastDateStatusChanged.Date; acc.ValuationsEndDate = finalEndDate; if (Util.IsNullDate(acc.FinalManagementEndDate)) acc.FinalManagementEndDate = finalEndDate; } } if (Util.IsNullDate(originalMgtEndDate) && Util.IsNotNullDate(saveValue.FinalManagementEndDate)) { if (acc.ActiveWithdrawalInstructions.Count > 0) { InstructionEngine engine = new InstructionEngine(); int instructionsCount = 0; foreach (ICashWithdrawalInstruction instruction in acc.ActiveWithdrawalInstructions) { if (instruction.WithdrawalDate >= saveValue.FinalManagementEndDate) { if (engine.CancelInstruction(instruction)) instructionsCount++; else throw new ApplicationException( string.Format("Could not set the Management End since withdrawal instruction {0} could not be cancelled, please cancel the instruction in the 'Withdrawal Management Console'.", instruction.Key)); } } message = string.Format("{0} withdrawal instruction(s) were cancelled", instructionsCount); } if (acc.WithdrawalRules.Count > 0) { int rulesCount = 0; foreach (IWithdrawalRule rule in acc.WithdrawalRules) { if (rule.IsActive && (Util.IsNullDate(rule.EndDateWithdrawal) || rule.EndDateWithdrawal > saveValue.FinalManagementEndDate)) { rule.EndDateWithdrawal = saveValue.FinalManagementEndDate; rulesCount++; } } message += Environment.NewLine + string.Format("{0} withdrawal rule(s) were cancelled", rulesCount); } } // if FinalManagementEndDate > ValuationsEndDate -> update ValuationsEndDate if (Util.IsNotNullDate(acc.ValuationsEndDate) && acc.FinalManagementEndDate > acc.ValuationsEndDate) acc.ValuationsEndDate = acc.FinalManagementEndDate; if (acc.UseManagementFee != saveValue.UseManagementFee) acc.UseManagementFee = saveValue.UseManagementFee; if (acc.UseKickback != saveValue.UseKickBack) acc.UseKickback = saveValue.UseKickBack; IRemisierHistory rh = new RemisierHistory(remisierEmployee, saveValue.KickBack, saveValue.IntroductionFee, saveValue.IntroductionFeeReduction, saveValue.SubsequentDepositFee, saveValue.SubsequentDepositFeeReduction, employee, DateTime.Now.Date); if (acc.CurrentRemisierDetails == null || !acc.CurrentRemisierDetails.Equals(rh)) acc.RemisierDetailChanges.Add(rh); if (isNewAccount) AccountMapper.Insert(session, acc); else AccountMapper.Update(session, acc); } finally { session.Close(); } }
public static int ProcessInstructions(int[] instructionIds) { if (instructionIds == null || instructionIds.Count() == 0) throw new B4F.TotalGiro.ApplicationLayer.Common.GridviewNoSelectionException(); IDalSession session = NHSessionFactory.CreateSession(); // Initialize the Engine InstructionEngine engine = new InstructionEngine(); int instructionsUpdated = 0; IList<IInstruction> instructions = InstructionMapper.GetInstructions(session, instructionIds); if (instructions != null && instructions.Count > 0) { session.BeginTransaction(); foreach (IInstruction instruction in instructions) { if (engine.ProcessInstruction(instruction)) { InstructionMapper.Update(session, instruction); instructionsUpdated++; if (instruction.UpdateableOrders != null && instruction.UpdateableOrders.Count > 0) OrderMapper.Insert(session, instruction.UpdateableOrders); } } session.CommitTransaction(); } session.Close(); return instructionsUpdated; }
internal static bool rebalanceAccount(int accountId, InstructionTypes instructionType, OrderActionTypes actionType, bool noCharges, decimal depositCashPositionDiff, bool includePrevCash) { bool success = false; Money diff = null; IDalSession session = NHSessionFactory.CreateSession(); IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session, accountId); if (account.ActiveRebalanceInstructions != null && account.ActiveRebalanceInstructions.Count > 0) throw new ApplicationException(string.Format("The account {0} already has an active rebalance instruction.", account.Number)); IList<IJournalEntryLine> cashTransfers = JournalEntryMapper.GetUnProcessedCashTransfers(session, account); if (cashTransfers == null || cashTransfers.Count == 0) throw new ApplicationException("It is not possible to do this rebalance without cash transfers"); else diff = new Money(depositCashPositionDiff, cashTransfers[0].Currency); if (account.ModelPortfolio == null) throw new ApplicationException(string.Format("The account {0} does not have a model attached.", account.Number)); IInstruction instruction = account.CreateInstruction(instructionType, actionType, DateTime.Now.Date, noCharges, cashTransfers); if (instruction != null) { // check total value of the transfers if (instructionType == InstructionTypes.BuyModel) { IBuyModelInstruction bmi = (IBuyModelInstruction)instruction; if (!(bmi.CashTransfers.TotalTransferAmount.IsGreaterThanZero && account.TotalCash.IsGreaterThanZero)) throw new ApplicationException("It is not possible to do this rebalance with a zero/negative cash transfer amount"); if (!(account.ActiveWithdrawalInstructions.Count == 0 && account.ActiveMoneyTransferOrders.Count == 0)) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there are active money transfers/withdrawals.", account.Number)); if (account.OpenOrdersForAccount.NewCollection(x => x.Side == Side.Sell).Count > 0) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since ther are sell orders.", account.Number)); if (account.OpenOrdersForAccount.Count > 0) { if (diff.IsLessThanZero) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash difference is negative.", account.Number)); if (account.TotalCash - account.OpenOrderAmount() < bmi.CashTransfers.TotalTransferAmount) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash is already spent.", account.Number)); } else { if (diff != null && diff.IsNotZero) { // When there is a cash difference (max 15% of the deposit) -> clear it away if (includePrevCash) { if ((bmi.CashTransfers.TotalTransferAmount + diff).IsLessThanZero) throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there is not enough cash.", account.Number)); bmi.DepositCashPositionDifference = diff; } } } } ICurrency underlying = account.AccountOwner.StichtingDetails.BaseCurrency; InstructionEngineParameters engineParams = InstructionEngineParametersMapper.GetParameters(session); InstructionEngine engine = new InstructionEngine(engineParams); if (engine.ProcessInstruction(instruction)) { session.BeginTransaction(); AccountMapper.Update(session, account); if (instruction.UpdateableOrders != null && instruction.UpdateableOrders.Count > 0) OrderMapper.Insert(session, instruction.UpdateableOrders); success = session.CommitTransaction(); } } session.Close(); return success; }