Beispiel #1
0
        public Control GetScreen(ScreenContainerContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.IncomingInvoiceFilterScreen)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.IncomingInvoiceExtension,
                CommandBarTagName = ExtensionCatalog.Tags.IncomingInvoiceFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().IncomingInvoiceSettings
            };

            var template = FilterDisplayHelper.LoadFilterScreenTemplate(origin);

            // Отключаем кнопки.
            foreach (var buttonTemplate in template.CommandButtons)
            {
                var formProvider =
                    context.ExtensionManager.TryGetTopFormProvider(buttonTemplate.Command);

                if (!(formProvider?.CheckCompatibility(context) ?? false))
                {
                    buttonTemplate.Enabled = false;
                }
            }

            var screen = new FilterScreen();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, screen);

            var now = DateTime.Now;

            var incomeValuesWrapper = new IncomingInvoiceFilterFormValuesWrapper
            {
                Control1FromTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local),
                Control2ToTime   = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, DateTimeKind.Local)
            };

            screen.ApplyTemplate(template);
            screen.ApplyShapeValues(incomeValuesWrapper.CollectIncomeValues());

            // Меню
            screen.MenuItemResolver = (entity, command) =>
            {
                var invoice = screen.CurrentEntity as IIncomingInvoice;

                if (null == invoice)
                {
                    return(false);
                }

                var invoiceContext = new IncomingInvoiceContext(context, invoice);

                var formProvider = context.ExtensionManager.TryGetIncomingInvoiceFormProvider(command);
                return(formProvider?.CheckCompatibility(invoiceContext) ?? false);
            };

            // Обработка событий.
            screen.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (FilterScreen.BeginUpdateServiceCommand.Equals(args.Command))
                {
                    context.ScreenContainer.OnStartProgress();
                    return;
                }

                if (FilterScreen.EndUpdateServiceCommand.Equals(args.Command))
                {
                    context.ScreenContainer.OnStopProgress();
                    return;
                }

                if (FilterScreen.DisplayContentServiceCommand.Equals(args.Command))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.Details;
                }

                var form = screen.FindForm();

                var invoice = args.Argument as IIncomingInvoice;

                // Кнопки
                if (null == invoice)
                {
                    var formProvider = context.ExtensionManager.TryGetTopFormProvider(command);
                    formProvider?.GetForm(context).Show(form);
                }
                else
                {
                    // Системная
                    var formProvider = context.ExtensionManager.TryGetIncomingInvoiceFormProvider(command);
                    formProvider?.GetForm(new IncomingInvoiceContext(context, invoice))
                    .Show(form);
                }
            };

            // Команда Refresh
            screen.WorkCallback = list =>
            {
                var valuesWrapper = new IncomingInvoiceFilterFormValuesWrapper(list);

                var invoiceService = context.UnityContainer.Resolve <IInvoiceService>();

                var invoices = invoiceService.SelectIncomingInvoices(valuesWrapper.Control1FromTime.ToUniversalTime(),
                                                                     valuesWrapper.Control2ToTime.ToUniversalTime(), true);

                var gridRowContentList = new List <GridRowContent>();

                var operationSettings = context.Session.SettingsService.GetSettings().OperationSettings;

                decimal paid    = 0;
                decimal notPaid = 0;
                decimal refused = 0;

                foreach (var invoice in invoices)
                {
                    var gridRowContent = new GridRowContent(invoice.PrimaryId.ToString(), invoice);

                    switch (invoice.State)
                    {
                    case InvoiceState.Paid:
Paid:
                        gridRowContent.ForeColor          = operationSettings.OutcomeForeColor;
                        gridRowContent.SelectionForeColor = operationSettings.OutcomeForeColor;
                        break;

                    case InvoiceState.PaidWithProtection:
                        gridRowContent.BackColor          = operationSettings.ProtectedColor;
                        gridRowContent.SelectionBackColor = ColorUtility.CalculateSelectionColor(operationSettings.ProtectedColor);
                        goto Paid;

                    case InvoiceState.Refusal:
                        gridRowContent.Strikeout = true;
                        break;
                    }

                    switch (invoice.State)
                    {
                    case InvoiceState.NotPaid:
                        notPaid += invoice.Amount;
                        break;

                    case InvoiceState.PaidWithProtection:
                    case InvoiceState.Paid:
                        paid += invoice.Amount;
                        break;

                    case InvoiceState.Refusal:
                        refused += invoice.Amount;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    gridRowContentList.Add(gridRowContent);
                }

                var chartPoint1 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.IncomingInvoiceFilterScreen, "Paid"), (double)paid);
                var chartPoint2 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.IncomingInvoiceFilterScreen, "Not paid"),
                    (double)notPaid);
                var chartPoint3 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.IncomingInvoiceFilterScreen, "Refused"), (double)refused);

                var filterScreenContent = new FilterScreenContent();

                filterScreenContent.RowContentList.AddRange(gridRowContentList);

                if (paid > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint1);
                }

                if (notPaid > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint2);
                }

                if (refused > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint3);
                }

                return(filterScreenContent);
            };

            return(screen);
        }
Beispiel #2
0
        public Form GetForm(PurseContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.TransferFilter)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.TransferExtension,
                CommandBarTagName = ExtensionCatalog.Tags.TransferFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().TransferSettings
            };

            var template = FilterDisplayHelper.LoadFilterFormTemplate(origin);

            template.Title = string.Format(CultureInfo.InvariantCulture, "{0} {1}",
                                           Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "History by purse"),
                                           context.Account.Number);

            // Отключаем кнопки.
            foreach (var buttonTemplate in template.FilterScreen.CommandButtons)
            {
                var formProvider =
                    context.ExtensionManager.TryGetPurseFormProvider(buttonTemplate.Command);

                if (!(formProvider?.CheckCompatibility(context) ?? false))
                {
                    buttonTemplate.Enabled = false;
                }
            }

            var form = new FilterForm();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, form);

            form.ApplyTemplate(template);

            var now = DateTime.Now;

            var incomeValuesWrapper = new TransferFilterFormValuesWrapper
            {
                Control1FromTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local),
                Control2ToTime   = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, DateTimeKind.Local)
            };

            form.ApplyShapeValues(incomeValuesWrapper.CollectIncomeValues());

            // Сохранение документа
            form.SaveItemsCallback = (s, list) =>
            {
                var importExportService = context.UnityContainer.Resolve <IImportExportService>();
                importExportService.Save(list, s);
            };

            // Сохранение настроек
            form.FormClosing += (sender, args) =>
            {
                var settings         = context.Session.SettingsService.GetSettings();
                var transferSettings = settings.TransferSettings;

                FilterDisplayHelper.UpdateColumnsSettings(transferSettings, form.SelectGridSettings());

                context.Session.SettingsService.SetSettings(settings);
                context.Session.SettingsService.Save();
            };

            // Меню
            form.MenuItemResolver = (entity, command) =>
            {
                var transfer = form.CurrentEntity as ITransfer;

                if (null == transfer)
                {
                    return(false);
                }

                var transferContext = new TransferContext(context, transfer);

                var formProvider = context.ExtensionManager.TryGetTransferFormProvider(command);
                return(formProvider?.CheckCompatibility(transferContext) ?? false);
            };

            // Обработка событий.
            form.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.Details;
                }

                var transfer = args.Argument as ITransfer;

                // Кнопки
                if (null == transfer)
                {
                    var formProvider = context.ExtensionManager.TryGetPurseFormProvider(command);
                    formProvider?.GetForm(context).Show(form);
                }
                else
                {
                    // Системная
                    var formProvider = context.ExtensionManager.TryGetTransferFormProvider(command);
                    formProvider?.GetForm(new TransferContext(context, transfer))
                    .Show(form);
                }
            };

            // Команда Refresh
            form.WorkCallback = values =>
            {
                var valuesWrapper = new TransferFilterFormValuesWrapper(values);


                var tansferService = context.UnityContainer.Resolve <ITransferService>();

                var transfers = tansferService.SelectTransfers(context.Account.Number,
                                                               valuesWrapper.Control1FromTime.ToUniversalTime(), valuesWrapper.Control2ToTime.ToUniversalTime(),
                                                               !valuesWrapper.Control3OfflineSearch);

                var gridRowContentList = new List <GridRowContent>();

                decimal income  = 0;
                decimal outcome = 0;

                var operationSettings = context.Session.SettingsService.GetSettings().OperationSettings;

                foreach (var transfer in transfers)
                {
                    var gridRowContent = new GridRowContent(transfer.PrimaryId.ToString(), transfer);

                    switch (transfer.Type)
                    {
                    case TransferType.Regular:
                        gridRowContent.SelectionBackColor = operationSettings.SelectionColor;
                        break;

                    case TransferType.Protected:
Protected:
                        gridRowContent.BackColor          = operationSettings.ProtectedColor;
                        gridRowContent.SelectionBackColor = ColorUtility.CalculateSelectionColor(operationSettings.ProtectedColor);
                        break;

                    case TransferType.Canceled:
                        gridRowContent.Strikeout = true;
                        goto Protected;
                    }

                    var foreColor = transfer.SourcePurse.Equals(context.Account.Number)
                        ? operationSettings.OutcomeForeColor
                        : operationSettings.IncomeForeColor;

                    gridRowContent.ForeColor          = foreColor;
                    gridRowContent.SelectionForeColor = foreColor;

                    if (TransferType.Canceled != transfer.Type &&
                        TransferType.Protected != transfer.Type)
                    {
                        if (transfer.SourcePurse.Equals(context.Account.Number))
                        {
                            outcome += transfer.Amount;
                        }
                        else
                        {
                            income += transfer.Amount;
                        }
                    }

                    gridRowContentList.Add(gridRowContent);
                }

                var chartPoint1 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "Income"), (double)income)
                {
                    Color     = operationSettings.IncomeChartColor,
                    FontColor = ChartFontColor
                };
                var chartPoint2 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "Outcome"), (double)outcome)
                {
                    Color     = operationSettings.OutcomeChartColor,
                    FontColor = ChartFontColor
                };

                var filterScreenContent = new FilterScreenContent();
                filterScreenContent.RowContentList.AddRange(gridRowContentList);

                if (income > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint1);
                }

                if (outcome > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint2);
                }

                var formattingService = context.UnityContainer.Resolve <IFormattingService>();

                var filterFormContent = new FilterFormContent(filterScreenContent);
                filterFormContent.LabelValues.Add(
                    $"{Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "IN")}: {formattingService.FormatAmount(income)}");
                filterFormContent.LabelValues.Add(
                    $"{Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "OUT")}: {formattingService.FormatAmount(outcome)}");

                return(filterFormContent);
            };

            return(form);
        }
Beispiel #3
0
        public Form GetForm(TransferBundleContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.PreparedTransferFilter)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.PreparedTransferExtension,
                CommandBarTagName = ExtensionCatalog.Tags.PreparedTransferFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().PreparedTransferSettings
            };

            var template = FilterDisplayHelper.LoadFilterFormTemplate(origin);

            template.Title = string.Format(CultureInfo.InvariantCulture, "{0} <{1}>",
                                           Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "History by bundle"),
                                           context.TransferBundle.Name);

            // Отключаем кнопки.
            foreach (var buttonTemplate in template.FilterScreen.CommandButtons)
            {
                var formProvider =
                    context.ExtensionManager.TryGetTopFormProvider(buttonTemplate.Command);

                if (!(formProvider?.CheckCompatibility(context) ?? false))
                {
                    buttonTemplate.Enabled = false;
                }
            }

            var form = new FilterForm();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, form);

            form.Load += (sender, args) =>
            {
                form.ShowData();
            };

            form.ApplyTemplate(template);

            var eventBroker = context.UnityContainer.Resolve <IEventBroker>();

            var subscriber = new Subscriber(context, form);

            eventBroker.Subscribe(subscriber, nameof(IPreparedTransferNotification));

            // Сохранение документа
            form.SaveItemsCallback = (s, list) =>
            {
                var importExportService = context.UnityContainer.Resolve <IImportExportService>();
                importExportService.Save(list, s);
            };

            // Сохранение настроек
            form.FormClosing += (sender, args) =>
            {
                var settings         = context.Session.SettingsService.GetSettings();
                var transferSettings = settings.PreparedTransferSettings;

                FilterDisplayHelper.UpdateColumnsSettings(transferSettings, form.SelectGridSettings());

                context.Session.SettingsService.SetSettings(settings);
                context.Session.SettingsService.Save();
            };

            // Меню
            form.MenuItemResolver = (entity, command) =>
            {
                var transfer = form.CurrentEntity as IPreparedTransfer;

                if (null == transfer)
                {
                    return(false);
                }

                var transferContext = new PreparedTransferContext(context, transfer);

                var formProvider = context.ExtensionManager.TryGetPreparedTransferFormProvider(command);
                return(formProvider?.CheckCompatibility(transferContext) ?? false);
            };

            // Обработка событий.
            form.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.Details;
                }

                var transfer = args.Argument as IPreparedTransfer;

                // Кнопки
                if (null == transfer)
                {
                    var formProvider = context.ExtensionManager.TryGetTransferBundleFormProvider(command);
                    formProvider?.GetForm(context).Show(form);
                }
                else
                {
                    var transferContext = new PreparedTransferContext(context, transfer);

                    var formProvider = context.ExtensionManager.TryGetPreparedTransferFormProvider(command);
                    formProvider?.GetForm(transferContext).Show(form);
                }
            };

            // Команда Refresh
            form.WorkCallback = values =>
            {
                var bundleService = context.UnityContainer.Resolve <ITransferBundleService>();

                var transfers = bundleService.ObtainBundle(context.TransferBundle.Id, true).Transfers;

                var gridRowContentList = new List <GridRowContent>();

                decimal completedAmount   = 0;
                decimal uncompletedAmount = 0;

                var transferSettings = context.Session.SettingsService.GetSettings().PreparedTransferSettings;

                foreach (var transfer in transfers)
                {
                    var gridRowContent = BuildGridRowContent(transfer, transferSettings);
                    gridRowContentList.Add(gridRowContent);

                    switch (transfer.State)
                    {
                    case PreparedTransferState.Failed:
                    case PreparedTransferState.Registered:
                    case PreparedTransferState.Pended:
                    case PreparedTransferState.Processed:
                    case PreparedTransferState.Interrupted:
                        uncompletedAmount += transfer.Amount;
                        break;

                    case PreparedTransferState.Completed:
                        completedAmount += transfer.Amount;
                        break;

                    default:
                        throw new InvalidOperationException("transfer.State == " + transfer.State);
                    }
                }

                var filterScreenContent = new FilterScreenContent();
                filterScreenContent.RowContentList.AddRange(gridRowContentList);

                var formattingService = context.UnityContainer.Resolve <IFormattingService>();

                var filterFormContent = new FilterFormContent(filterScreenContent);
                filterFormContent.LabelValues.Add(formattingService.FormatAmount(completedAmount));
                filterFormContent.LabelValues.Add(formattingService.FormatAmount(uncompletedAmount));

                return(filterFormContent);
            };

            form.Disposed += (sender, args) =>
            {
                subscriber.Dispose();
            };

            return(form);
        }
Beispiel #4
0
        public Form GetForm(PurseContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.OutgoingInvoiceFilter)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.OutgoingInvoiceExtension,
                CommandBarTagName = ExtensionCatalog.Tags.OutgoingInvoiceFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().OutgoingInvoiceSettings
            };

            var template = FilterDisplayHelper.LoadFilterFormTemplate(origin);

            template.Title = string.Format(CultureInfo.InvariantCulture, "{0} {1}",
                                           Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "Outgoing invoices by purse"),
                                           context.Account.Number);

            // Отключаем кнопки.
            foreach (var templateCommandButton in template.FilterScreen.CommandButtons)
            {
                var purseFormProvider =
                    context.ExtensionManager.TryGetPurseFormProvider(templateCommandButton.Command);

                if (!(purseFormProvider?.CheckCompatibility(context) ?? false))
                {
                    templateCommandButton.Enabled = false;
                }
            }

            var form = new FilterForm();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, form);

            var now = DateTime.Now;

            var incomeValuesWrapper = new OutgoingInvoiceFilterFormValuesWrapper
            {
                Control1FromTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local),
                Control2ToTime   = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, DateTimeKind.Local)
            };

            form.ApplyTemplate(template);
            form.ApplyShapeValues(incomeValuesWrapper.CollectIncomeValues());

            // Сохранение документа
            form.SaveItemsCallback = (s, list) =>
            {
                var importExportService = context.UnityContainer.Resolve <IImportExportService>();
                importExportService.Save(list, s);
            };

            // Сохранение настроек
            form.FormClosing += (sender, args) =>
            {
                var settings = context.Session.SettingsService.GetSettings();
                var outgoingInvoiceSettings = settings.OutgoingInvoiceSettings;

                FilterDisplayHelper.UpdateColumnsSettings(outgoingInvoiceSettings, form.SelectGridSettings());

                context.Session.SettingsService.SetSettings(settings);
                context.Session.SettingsService.Save();
            };

            // Меню
            form.MenuItemResolver = (entity, command) =>
            {
                var invoice = form.CurrentEntity as IOutgoingInvoice;

                if (null == invoice)
                {
                    return(false);
                }

                var invoiceContext = new OutgoingInvoiceContext(context, invoice);

                var formProvider = context.ExtensionManager.TryGetOutgoingInvoiceFormProvider(command);
                return(formProvider?.CheckCompatibility(invoiceContext) ?? false);
            };

            // Обработка событий.
            form.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.Details;
                }

                var invoice = args.Argument as IOutgoingInvoice;

                // Кнопки
                if (null == invoice)
                {
                    var formProvider = context.ExtensionManager.TryGetPurseFormProvider(command);
                    formProvider?.GetForm(context).Show(form);
                }
                else
                {
                    // Системная
                    var formProvider = context.ExtensionManager.TryGetOutgoingInvoiceFormProvider(command);
                    formProvider?.GetForm(new OutgoingInvoiceContext(context, invoice))
                    .Show(form);
                }
            };

            // Команда Refresh
            form.WorkCallback = list =>
            {
                var valuesWrapper = new OutgoingInvoiceFilterFormValuesWrapper(list);

                var invoiceService = context.UnityContainer.Resolve <IInvoiceService>();

                var invoices = invoiceService.SelectOutgoingInvoices(context.Account.Number,
                                                                     valuesWrapper.Control1FromTime.ToUniversalTime(), valuesWrapper.Control2ToTime.ToUniversalTime(), true);

                var gridRowContentList = new List <GridRowContent>();
                var operationSettings  = context.Session.SettingsService.GetSettings().OperationSettings;

                decimal paid    = 0;
                decimal notPaid = 0;
                decimal refused = 0;

                foreach (var invoice in invoices)
                {
                    var gridRowContent = new GridRowContent(invoice.PrimaryId.ToString(), invoice);

                    switch (invoice.State)
                    {
                    case InvoiceState.Paid:
Paid:
                        gridRowContent.ForeColor          = operationSettings.OutcomeForeColor;
                        gridRowContent.SelectionForeColor = operationSettings.OutcomeForeColor;
                        break;

                    case InvoiceState.PaidWithProtection:
                        gridRowContent.BackColor          = operationSettings.ProtectedColor;
                        gridRowContent.SelectionBackColor =
                            ColorUtility.CalculateSelectionColor(operationSettings.ProtectedColor);
                        goto Paid;

                    case InvoiceState.Refusal:
                        gridRowContent.Strikeout = true;
                        break;
                    }

                    switch (invoice.State)
                    {
                    case InvoiceState.NotPaid:
                        notPaid += invoice.Amount;
                        break;

                    case InvoiceState.PaidWithProtection:
                    case InvoiceState.Paid:
                        paid += invoice.Amount;
                        break;

                    case InvoiceState.Refusal:
                        refused += invoice.Amount;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    gridRowContentList.Add(gridRowContent);
                }

                var chartPoint1 = new ChartPoint(Translator.Instance.Translate(ExtensionCatalog.OutgoingInvoiceFilter, "Paid"),
                                                 (double)paid);
                var chartPoint2 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.OutgoingInvoiceFilter, "Not paid"), (double)notPaid);
                var chartPoint3 = new ChartPoint(
                    Translator.Instance.Translate(ExtensionCatalog.OutgoingInvoiceFilter, "Refused"), (double)refused);

                var filterScreenContent = new FilterScreenContent();
                filterScreenContent.RowContentList.AddRange(gridRowContentList);

                if (paid > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint1);
                }

                if (notPaid > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint2);
                }

                if (refused > 0)
                {
                    filterScreenContent.ChartPoints.Add(chartPoint3);
                }

                var formattingService = context.UnityContainer.Resolve <IFormattingService>();

                var filterFormContent = new FilterFormContent(filterScreenContent);
                filterFormContent.LabelValues.Add(
                    $"{Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "PAID")}: {formattingService.FormatAmount(paid)}");
                filterFormContent.LabelValues.Add(
                    $"{Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "UNP")}: {formattingService.FormatAmount(notPaid)}");
                filterFormContent.LabelValues.Add(
                    $"{Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "REF")}: {formattingService.FormatAmount(refused)}");

                return(filterFormContent);
            };

            return(form);
        }
        public Form GetForm(SessionContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.TransferBundleFilter)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.TransferBundleExtension,
                CommandBarTagName = ExtensionCatalog.Tags.TransferBundleFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().TransferBundleSettings
            };

            var template = FilterDisplayHelper.LoadFilterFormTemplate(origin);

            template.Title = Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "Transfer bundles");

            // Отключаем кнопки.
            foreach (var buttonTemplate in template.FilterScreen.CommandButtons)
            {
                var formProvider =
                    context.ExtensionManager.TryGetTopFormProvider(buttonTemplate.Command);

                if (!(formProvider?.CheckCompatibility(context) ?? false))
                {
                    buttonTemplate.Enabled = false;
                }
            }

            _form = new FilterForm();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, _form);
            _form.ApplyTemplate(template);

            var now = DateTime.Now;

            var incomeValuesWrapper = new TransferBundleFilterFormValuesWrapper
            {
                Control1FromTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local),
                Control2ToTime   = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, DateTimeKind.Local)
            };

            _form.ApplyShapeValues(incomeValuesWrapper.CollectIncomeValues());

            EventBroker.TransferBundleCreated += EventBrokerOnTransferBundleCreated;

            var eventBroker = context.UnityContainer.Resolve <IEventBroker>();

            var subscriber = new Subscriber(context, _form);

            eventBroker.Subscribe(subscriber, nameof(ITransferBundleNotification));

            // Сохранение документа
            _form.SaveItemsCallback = (s, list) =>
            {
                var importExportService = context.UnityContainer.Resolve <IImportExportService>();
                importExportService.Save(list, s);
            };

            // Сохранение настроек
            _form.FormClosing += (sender, args) =>
            {
                var settings       = context.Session.SettingsService.GetSettings();
                var bundleSettings = settings.TransferBundleSettings;

                FilterDisplayHelper.UpdateColumnsSettings(bundleSettings, _form.SelectGridSettings());

                context.Session.SettingsService.SetSettings(settings);
                context.Session.SettingsService.Save();
            };

            // Меню
            _form.MenuItemResolver = (entity, command) =>
            {
                var transferBundle = _form.CurrentEntity as ITransferBundle;

                if (null == transferBundle)
                {
                    return(false);
                }

                var transferContext = new TransferBundleContext(context, transferBundle);

                if (command.Equals(ExtensionCatalog.StartTransferBundle) ||
                    command.Equals(ExtensionCatalog.StopTransferBundle))
                {
                    var actionProvider = context.ExtensionManager.TryGetTransferBundleActionProvider(command);
                    return(actionProvider?.CheckCompatibility(transferContext) ?? false);
                }

                var formProvider = context.ExtensionManager.TryGetTransferBundleFormProvider(command);
                return(formProvider?.CheckCompatibility(transferContext) ?? false);
            };

            // Обработка событий.
            _form.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.PreparedTransferFilter;
                }

                var transferBundle = args.Argument as ITransferBundle;

                // Кнопки
                if (null == transferBundle)
                {
                    var formProvider = context.ExtensionManager.TryGetTopFormProvider(command);
                    formProvider?.GetForm(context).Show(_form);
                }
                else
                {
                    var transferBundleContext = new TransferBundleContext(context, transferBundle);

                    if (command.Equals(ExtensionCatalog.StartTransferBundle) ||
                        command.Equals(ExtensionCatalog.StopTransferBundle))
                    {
                        var actionProvider = context.ExtensionManager.TryGetTransferBundleActionProvider(command);
                        actionProvider?.RunAction(transferBundleContext);

                        return;
                    }

                    var formProvider = context.ExtensionManager.TryGetTransferBundleFormProvider(command);
                    formProvider?.GetForm(transferBundleContext).Show(_form);
                }
            };

            // Команда Refresh
            _form.WorkCallback = values =>
            {
                var valuesWrapper = new TransferBundleFilterFormValuesWrapper(values);

                var transferBundleService = context.UnityContainer.Resolve <ITransferBundleService>();
                var bundles = transferBundleService.SelectBundles(valuesWrapper.Control1FromTime.ToUniversalTime(),
                                                                  valuesWrapper.Control2ToTime.ToUniversalTime());

                var gridRowContentList = new List <GridRowContent>();

                var bundleSettings = context.Session.SettingsService.GetSettings().TransferBundleSettings;

                foreach (var bundle in bundles)
                {
                    var gridRowContent = BuildGridRowContent(bundle, bundleSettings);
                    gridRowContentList.Add(gridRowContent);
                }

                var filterScreenContent = new FilterScreenContent();
                filterScreenContent.RowContentList.AddRange(gridRowContentList);
                var filterFormContent = new FilterFormContent(filterScreenContent);

                return(filterFormContent);
            };

            _form.Disposed += (sender, args) =>
            {
                EventBroker.TransferBundleCreated -= EventBrokerOnTransferBundleCreated;
                subscriber.Dispose();
            };

            return(_form);
        }
        public Form GetForm(SessionContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var origin = new FilterOrigin(context.ExtensionManager,
                                          ExtensionCatalog.ContractFilter)
            {
                MenuItemsTagName  = ExtensionCatalog.Tags.ContractExtension,
                CommandBarTagName = ExtensionCatalog.Tags.ContractFilterExtension,
                ColumnsSettings   = context.Session.SettingsService.GetSettings().ContractSettings
            };

            var template = FilterDisplayHelper.LoadFilterFormTemplate(origin);

            template.Title = Translator.Instance.Translate(ExtensionCatalog.TransferFilter, "Contracts");

            // Отключаем кнопки.
            foreach (var buttonTemplate in template.FilterScreen.CommandButtons)
            {
                var formProvider =
                    context.ExtensionManager.TryGetTopFormProvider(buttonTemplate.Command);

                if (!(formProvider?.CheckCompatibility(context) ?? false))
                {
                    buttonTemplate.Enabled = false;
                }
            }

            _form = new FilterForm();

            ErrorFormDisplayHelper.ApplyErrorAction(context.ExtensionManager, _form);

            var now = DateTime.Now;

            var incomeValuesWrapper = new TransferFilterFormValuesWrapper
            {
                Control1FromTime = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, DateTimeKind.Local),
                Control2ToTime   = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, DateTimeKind.Local)
            };

            _form.ApplyTemplate(template);
            _form.ApplyShapeValues(incomeValuesWrapper.CollectIncomeValues());

            // Подписка на создание/изменение контракта
            EventBroker.ContractChanged += EventBrokerOnContractChanged;

            // Сохранение документа
            _form.SaveItemsCallback = (s, list) =>
            {
                var importExportService = context.UnityContainer.Resolve <IImportExportService>();
                importExportService.Save(list, s);
            };

            // Сохранение настроек
            _form.FormClosing += (sender, args) =>
            {
                var settings         = context.Session.SettingsService.GetSettings();
                var contractSettings = settings.ContractSettings;

                FilterDisplayHelper.UpdateColumnsSettings(contractSettings, _form.SelectGridSettings());

                context.Session.SettingsService.SetSettings(settings);
                context.Session.SettingsService.Save();
            };

            // Меню
            _form.MenuItemResolver = (entity, command) =>
            {
                var contract = _form.CurrentEntity as IContract;

                if (null == contract)
                {
                    return(false);
                }

                var contractContext = new ContractContext(context, contract);

                if (command.Equals(ExtensionCatalog.RefreshContract))
                {
                    var actionProvider = context.ExtensionManager.TryGetContractActionProvider(command);
                    return(actionProvider?.CheckCompatibility(contractContext) ?? false);
                }

                var formProvider = context.ExtensionManager.TryGetContractFormProvider(command);
                return(formProvider?.CheckCompatibility(contractContext) ?? false);
            };

            // Обработка событий.
            _form.ServiceCommand += (sender, args) =>
            {
                string command = args.Command;

                if (null == command)
                {
                    throw new InvalidOperationException("null == command");
                }

                if (command.StartsWith("CellContentClick:", StringComparison.Ordinal))
                {
                    return;
                }

                if (command.StartsWith("CellMouseDoubleClick:", StringComparison.Ordinal))
                {
                    command = ExtensionCatalog.ContractDetails;
                }

                var contract = args.Argument as IContract;

                // Кнопки
                if (null == contract)
                {
                    var formProvider = context.ExtensionManager.TryGetTopFormProvider(command);

                    formProvider?.GetForm(context).Show(_form);
                }
                else
                {
                    var contractContext = new ContractContext(context, contract);

                    if (command.Equals(ExtensionCatalog.RefreshContract))
                    {
                        var actionProvider = context.ExtensionManager.TryGetContractActionProvider(command);
                        actionProvider?.RunAction(contractContext);
                    }
                    else
                    {
                        var formProvider = context.ExtensionManager.TryGetContractFormProvider(command);
                        formProvider?.GetForm(contractContext).Show(_form);
                    }
                }
            };

            // Команда Refresh
            _form.WorkCallback = values =>
            {
                var valuesWrapper = new ContractFilterFormValuesWrapper(values);

                var contractService = context.UnityContainer.Resolve <IContractService>();

                var contracts =
                    contractService.SelectContracts(valuesWrapper.Control1FromTime, valuesWrapper.Control2ToTime);

                var gridRowContentList = new List <GridRowContent>();

                int signed   = 0;
                int unsigned = 0;

                var contractSettings = context.Session.SettingsService.GetSettings().ContractSettings;

                foreach (var contract in contracts)
                {
                    var gridRowContent = new GridRowContent(contract.Id.ToString(), contract);

                    switch (contract.State)
                    {
                    case ContractState.Created:
                        gridRowContent.BackColor          = Color.White;
                        gridRowContent.SelectionBackColor = contractSettings.SelectionColor;
                        unsigned++;
                        break;

                    case ContractState.Signed:
                    case ContractState.Completed:
                        gridRowContent.BackColor          = contractSettings.SignedColor;
                        gridRowContent.SelectionBackColor =
                            ColorUtility.CalculateSelectionColor(contractSettings.SignedColor);
                        signed++;
                        break;

                    default:
                        throw new InvalidOperationException("contract.State == " + contract.State);
                    }

                    if (0 == contract.AccessCount)
                    {
                        gridRowContent.ForeColor          = contractSettings.PublicForeColor;
                        gridRowContent.SelectionForeColor = contractSettings.PublicForeColor;
                    }

                    gridRowContentList.Add(gridRowContent);
                }

                var filterScreenContent = new FilterScreenContent();
                filterScreenContent.RowContentList.AddRange(gridRowContentList);

                var filterFormContent = new FilterFormContent(filterScreenContent);

                filterFormContent.LabelValues.Add(string.Format(CultureInfo.InvariantCulture, "SIG: {0}", signed));
                filterFormContent.LabelValues.Add(string.Format(CultureInfo.InvariantCulture, "UNSIG: {0}", unsigned));

                return(filterFormContent);
            };

            _form.Disposed += (sender, args) =>
            {
                EventBroker.ContractChanged -= EventBrokerOnContractChanged;
            };

            return(_form);
        }