public override ITransaction Storno(IAccountTypeInternal stornoAccount, B4F.TotalGiro.Stichting.Login.IInternalEmployeeLogin employee, string reason, ITradingJournalEntry tradingJournalEntry)
 {
     CorporateActionStockDividend newStorno = new CorporateActionStockDividend();
     this.storno(stornoAccount, employee, reason, tradingJournalEntry, newStorno);
     return newStorno;
 }
Beispiel #2
0
        public static int InitialiseDividend(int dividendKey)
        {
            using (IDalSession session = NHSessionFactory.CreateSession())
            {
                int successCount = 0;
                IDividendHistory history = DividendHistoryMapper.GetDividendHistory(session, dividendKey);
                if (!history.IsInitialised)
                {
                    if (history.ExDividendDate > DateTime.Today)
                        throw new ApplicationException("The exdividend date is in the future.");

                    int journalId = int.Parse((string)(System.Configuration.ConfigurationManager.AppSettings.Get(@"DefaultDividendJournal")));
                    IJournal journal = JournalMapper.GetJournal(session, journalId);
                    IList<IHistoricalPosition> accountsWithPositionByDate = B4F.TotalGiro.MIS.StoredPositions.StoredPositionTransactionMapper.GetAccountsWithPositionByDate(session, history.ExDividendDate, history.Instrument.Key);

                    if (accountsWithPositionByDate != null && accountsWithPositionByDate.Count > 0)
                    {
                        if (history.NeedsStockDividend)
                        {
                            DateTime transactionDate = history.ExDividendDate;
                            IStockDividend instDiv = null;
                            if (!string.IsNullOrEmpty(history.StockDivIsin))
                                instDiv = history.Instrument.CorporateActionInstruments.GetStockDividendByIsin(history.StockDivIsin);
                            else
                                instDiv = history.Instrument.CorporateActionInstruments.GetLatestStockDividend();
                            if (instDiv == null)
                            {
                                instDiv = new StockDividend(history.Instrument, history.StockDivIsin);
                                if (!instDiv.Validate())
                                    throw new ApplicationException("Could not create stock dividend");
                                InstrumentMapper.Update(session, instDiv);
                            }
                            history.StockDividend = instDiv;
                            Price price = updateStockDividendPrices(session, instDiv, history);

                            foreach (IHistoricalPosition hp in accountsWithPositionByDate)
                            {
                                if (!history.StockDividends.Any(x => x.AccountA.Key == hp.Account.Key))
                                {
                                    IDalSession session2 = NHSessionFactory.CreateSession();
                                    IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session2, hp.Account.Key);
                                    InstrumentSize size = new InstrumentSize(hp.ValueSize.Quantity, instDiv);
                                    if (history.DividendType == DividendTypes.Scrip)
                                        size = size * (history.ScripRatio / 100M);

                                    if ((size * price).IsNotZero)
                                    {
                                        ITradingJournalEntry dividendBooking = getNewStockDividendBooking(session2, journal, transactionDate);
                                        ICorporateActionStockDividend stockDiv = new CorporateActionStockDividend(
                                            account, account.DefaultAccountforTransfer,
                                            size, price,
                                            instDiv.CurrencyNominal.ExchangeRate.Rate, transactionDate,
                                            history, hp.ValueSize, dividendBooking);
                                        if (session2.Insert(stockDiv))
                                            successCount++;
                                    }
                                    else
                                        successCount++;
                                }
                                else
                                    successCount++;
                            }
                        }
                        else
                        {
                            DateTime transactionDate = history.SettlementDate;
                            IGLLookupRecords lookups = GlLookupRecordMapper.GetGLLookupRecords(session, BookingComponentParentTypes.CashDividend);
                            foreach (IHistoricalPosition hp in accountsWithPositionByDate)
                            {
                                if (!history.CashDividends.Any(x => x.Account.Key == hp.Account.Key))
                                {
                                    IDalSession session2 = NHSessionFactory.CreateSession();
                                    IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session2, hp.Account.Key);
                                    IMemorialBooking dividendBooking = getNewCashDividendBooking(session2, journal, transactionDate);
                                    ICashDividend newBooking = new CashDividend(account, dividendBooking, history.Description, history.TaxPercentage, history, hp.ValueSize, lookups);
                                    if (newBooking.TotalAmount.IsNotZero)
                                    {
                                        if (session2.Insert(newBooking))
                                            successCount++;
                                    }
                                    else
                                        successCount++;
                                }
                                else
                                    successCount++;
                            }
                        }
                        if (accountsWithPositionByDate.Count == successCount)
                        {
                            history.IsInitialised = true;
                            session.InsertOrUpdate(history);
                        }
                    }
                }
                return dividendKey;
            }
        }