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;
        }
Пример #2
0
        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();
            }
        }