public List <Booking> GetBookings(AccountingRuleCollection rules,
                                          EventStock eventStock,
                                          List <Teller> tellers,
                                          List <PaymentMethod> paymentMethods,
                                          List <ExchangeRate> rates,
                                          List <FiscalYear> fiscalYears)
        {
            if (eventStock == null)
            {
                return(new List <Booking>());
            }

            rules.SortByOrder();
            eventStock.SortEventsById();
            bool isExported = false;
            var  bookings   = new List <Booking>();

            foreach (Event eventItem in eventStock)
            {
                ClosureStatus     = "Closure";
                ClosureStatusInfo = " ->" + eventItem.Code + "-" + eventItem.Id;

                var attributes = new List <EventAttribute>();
                List <ContractAccountingRule> rulesToApply =
                    rules.GetContractAccountingRules().Where(item => item.EventType.EventCode == eventItem.Code).ToList();

                rules.SortByOrder();
                List <ContractAccountingRule> orders = rulesToApply.GroupBy(a => a.Order).Select(g => g.Last()).ToList();

                foreach (ContractAccountingRule orderRule in orders)
                {
                    foreach (ContractAccountingRule rule in rulesToApply.Where(r => r.Order == orderRule.Order).ToList())
                    {
                        List <EventAttribute> evtAttributes = (from eventAtt in attributes
                                                               where eventAtt.Name == rule.EventAttribute.Name
                                                               select eventAtt).ToList();

                        if (rule.EventType.EventCode == eventItem.Code &&
                            evtAttributes.Count <= rulesToApply.Count(r => r.Order == orderRule.Order) - 1)
                        {
                            ContractAccountingRule tempRule = rule.Copy();

                            if (paymentMethods != null && eventItem.PaymentMethod != null)
                            {
                                tempRule = GetParentPaymentAccount(eventItem.PaymentMethod.Id, tempRule, paymentMethods);
                            }

                            // teller must be last
                            if (tellers != null && eventItem.TellerId != null)
                            {
                                //that copy is very important because the rule might be over written by payment method
                                tempRule = rule.Copy();
                                tempRule = GetParentTellerAccount(eventItem.TellerId, tempRule, tellers);
                            }

                            Booking b = GetBooking(tempRule, eventItem);
                            if (b != null && b.Amount > 0)
                            {
                                //setting fiscal year
                                if (fiscalYears != null)
                                {
                                    b.FiscalYear =
                                        fiscalYears
                                        .First(
                                            f =>
                                            f.OpenDate <= b.Date.Date &&
                                            (f.CloseDate == null || f.CloseDate >= b.Date.Date)
                                            );
                                }
                                //setting xrate
                                ExchangeRate rate = null;
                                if (rates != null)
                                {
                                    rate = rates.FirstOrDefault(r => r.Date.Date == b.Date.Date);
                                }

                                b.ExchangeRate = b.Currency.IsPivot ? 1 : rate == null ? 0 : rate.Rate;

                                isExported = true;
                                attributes.Add(tempRule.EventAttribute);
                                bookings.Add(b);
                            }
                        }
                    }
                }

                if (eventItem is TellerCashInEvent || eventItem is TellerCashOutEvent)
                {
                    bookings.Add(GetTellerBooking((TellerEvent)eventItem, tellers, fiscalYears));
                }

                eventItem.IsFired = false;
                eventItem.IsFired = isExported;
            }
            return(bookings);
        }
Beispiel #2
0
        private void GetBookings(ClosureOptions options)
        {
            List <Booking> bookings   = new List <Booking>();
            EventStock     eventStock = new EventStock();

            AccountingRuleCollection rules = ServicesProvider.GetInstance().GetAccountingRuleServices().SelectAll();

            rules.SortByOrder();

            if (options.DoLoanClosure)
            {
                UpdateStatus("LoanClosureProcessing", "");
                eventStock =
                    ServicesProvider.GetInstance().GetEventProcessorServices().SelectEventsForClosure(_beginDate, _endDate, _branch);
                UpdateStatus("LoanClosureProcessing", eventStock.GetEvents().Count.ToString());
                //add generated events for processing
            }

            if (options.DoSavingClosure)
            {
                UpdateStatus("SavingsClosureProcessing", "");

                eventStock.AddRange(
                    ServicesProvider.GetInstance().GetSavingServices().SelectEventsForClosure(
                        _beginDate, _endDate, _branch));

                UpdateStatus("SavingsClosureProcessing", eventStock.GetEvents().Count.ToString());
            }

            if (options.DoTellerManagementClosure)
            {
                UpdateStatus("TellerManagementProcessing", "");
                eventStock.AddRange(ServicesProvider.GetInstance().GetEventProcessorServices().GetTellerEventsForClosure(
                                        _beginDate, _endDate));
                UpdateStatus("EventClosureProcessing", eventStock.GetEvents().Count.ToString());
            }

            //important to have sorted list
            eventStock.SortEventsById();
            //closure procesing
            timerClosure.Start();
            timerClosure.Enabled = true;

            //set ex rate
            List <ExchangeRate> rates =
                ServicesProvider.GetInstance().GetExchangeRateServices().SelectRatesByDate(_beginDate, _endDate);

            List <CoreDomain.Accounting.FiscalYear> fiscalYears =
                ServicesProvider.GetInstance().GetChartOfAccountsServices().SelectFiscalYears();

            bookings.AddRange(
                _accountingClosure.GetBookings(
                    rules,
                    eventStock,
                    ServicesProvider.GetInstance().GetTellerServices().FindAllTellers(),
                    ServicesProvider.GetInstance().GetPaymentMethodServices().GetAllPaymentMethodsForClosure(),
                    rates,
                    fiscalYears));

            timerClosure.Stop();
            timerClosure.Enabled = false;

            //manual transactions
            if (options.DoManualEntries)
            {
                bookings.AddRange(ServicesProvider.GetInstance().GetAccountingServices().SelectMovements(false, rates,
                                                                                                         fiscalYears));
            }

            #region Reversal
            if (options.DoReversalTransactions)
            {
                UpdateStatus("ReversalTransactionProcessing", "");
                bookings.AddRange(ServicesProvider.GetInstance().GetAccountingServices().SelectMovementsForReverse(
                                      rates,
                                      fiscalYears));
            }
            #endregion

            //add reversal provision booking
            if (options.DoLoanClosure)
            {
                bookings.AddRange(
                    _generatedEvents.GetProvisionEvents().Select(
                        provisionEvent =>
                        ServicesProvider.GetInstance().GetAccountingServices().SelectProvisionMovments(
                            provisionEvent.ContracId, rates,
                            fiscalYears)).Where(b => b != null));
            }

            SortBookingsByDate(bookings);

            FillListView(bookings);
        }