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; }