private void GenerateEvents(ClosureOptions options)
        {
            if (options.DoOverdue || options.DoProvision || options.DoAccrued || options.DoLoanClosure)
            {
                UpdateStatus("PerformingClosure", "");
                _listLoan = ServicesProvider.GetInstance().GetContractServices().SelectContractsForClosure();
                UpdateStatus("", "Contracts -> " + _listLoan.Count.ToString());
            }
            //generate overdue events
            if (options.DoOverdue)
            {
                UpdateStatus("OverdueEventsProcessing", "");
                LateLoanProcessing();
            }
            //generate provision events
            if (options.DoProvision)
            {
                UpdateStatus("ProvisionEventsProcessing", "");
                ProvisionLoanProcessing();
            }
            //generate accrued interest events
            if (options.DoAccrued)
            {
                UpdateStatus("AccruedInterestEventsProcessing", "");
                AccruedInterestLoanProcessing();
            }

            if (options.DoSavingEvents)
            {
                UpdateStatus("SavingEventsGeneration", "");
                GenerateSavingsClosureEvents();
            }

            UpdateStatus("", "Loding list ...");
            olvEvents.SetObjects(_generatedEvents);
            UpdateStatus("", "Done");
        }
        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);
        }
        private void BtnRunClick(object sender, EventArgs e)
        {
            _beginDate = dateTimePickerBeginDate.Value.Date;
            _endDate = dateTimePickerEndDate.Value.Date;
            _branch = ((Branch)cmbBranches.SelectedItem);

            btnPreview.Enabled = false;
            dateTimePickerBeginDate.Enabled = false;
            dateTimePickerEndDate.Enabled = false;
            cmbBranches.Enabled = false;
            clbxFields.Enabled = false;
            btnPost.Enabled = false;
            btnView.Enabled = false;
            //DisableX();

            EnableCloseButton(this, false);

            if (_mode == 0)
            {
                _closureOptions = new ClosureOptions
                                      {
                                          DoAccrued = false,
                                          DoOverdue = false,
                                          DoProvision = false,
                                          DoSavingEvents = false,
                                          DoReversalTransactions =
                                              clbxFields.CheckedItems.Contains(_closureItems.Items[0]),
                                          DoLoanClosure = clbxFields.CheckedItems.Contains(_closureItems.Items[1]),
                                          DoSavingClosure = clbxFields.CheckedItems.Contains(_closureItems.Items[2]),
                                          DoManualEntries = clbxFields.CheckedItems.Contains(_closureItems.Items[3]),
                                          DoTellerManagementClosure = clbxFields.CheckedItems.Contains(_closureItems.Items[4])
                                      };
            }

            if (_mode == 1)
            {
                //generaion of events
                _closureOptions = new ClosureOptions
                                      {
                                          DoAccrued = clbxFields.CheckedItems.Contains(_closureItems.Items[0]),
                                          DoOverdue = clbxFields.CheckedItems.Contains(_closureItems.Items[1]),
                                          DoProvision = clbxFields.CheckedItems.Contains(_closureItems.Items[2]),
                                          DoSavingEvents = clbxFields.CheckedItems.Contains(_closureItems.Items[3]),
                                          DoReversalTransactions = false,
                                          DoLoanClosure = false,
                                          DoSavingClosure = false,
                                          DoManualEntries = false
                                      };
            }

            bwRun.RunWorkerAsync();
        }