Pārlūkot izejas kodu

PostUtils class

Kenric Nugteren 2 gadi atpakaļ
vecāks
revīzija
c9fe4f0d54

+ 1 - 1
prs.desktop/Panels/Invoices/InvoiceListGrid.cs

@@ -23,7 +23,7 @@ namespace PRSDesktop
         public InvoiceListGrid()
         {
             Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.AddRows, DynamicGridOption.DeleteRows, DynamicGridOption.EditRows,
-                DynamicGridOption.SelectColumns);
+                DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
             AddButton("Print", PRSDesktop.Resources.printer.AsBitmapImage(), PrintInvoice2);
             AddButton("Email", PRSDesktop.Resources.email.AsBitmapImage(), EmailInvoice2);
             HiddenColumns.Add(x => x.CustomerLink.ID);

+ 5 - 96
prs.desktop/Panels/Invoices/InvoicePanel.xaml.cs

@@ -48,104 +48,13 @@ namespace PRSDesktop
             return new Dictionary<string, object[]> { { typeof(Invoice).EntityName(), Invoices.SelectedRows } };
         }
 
-        private static void ConfigureInvoicePost()
-        {
-            var invoicePostSettings = PosterUtils.LoadPostableSettings<Invoice>();
-
-            var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
-            if (grid.EditItems(new PostableSettings[] { invoicePostSettings }))
-            {
-                PosterUtils.SavePostableSettings<Invoice>(invoicePostSettings);
-            }
-        }
-
         public void CreateToolbarButtons(IPanelHost host)
         {
-            var invoicePostSettings = PosterUtils.LoadPostableSettings<Invoice>();
-            if (Security.CanPost<Invoice>())
-            {
-                Bitmap? image = null;
-                if(invoicePostSettings.Thumbnail.ID != Guid.Empty)
-                {
-                    var icon = new Client<Document>()
-                        .Load(new Filter<Document>(x => x.ID).IsEqualTo(invoicePostSettings.Thumbnail.ID)).FirstOrDefault();
-                    if(icon is not null)
-                    {
-                        image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
-                    }
-                }
-                host.CreatePanelAction(new PanelAction
-                {
-                    Caption = invoicePostSettings.ButtonName.NotWhiteSpaceOr("Process Invoices"),
-                    Image = image ?? PRSDesktop.Resources.edit,
-                    OnExecute = action =>
-                    {
-                        var rows = Invoices.SelectedRows.Select(x => x.ToObject<Invoice>()).ToList();
-                        if (!rows.Any())
-                        {
-                            MessageBox.Show("Please select at least one invoice.");
-                            return;
-                        }
-                        try
-                        {
-                            if (PosterUtils.Process(rows))
-                            {
-                                MessageBox.Show("Processing successful!");
-                                Invoices.Refresh(false, true);
-                            }
-                            else
-                            {
-                                MessageBox.Show("Processing failed.");
-                                Invoices.Refresh(false, true);
-                            }
-                        }
-                        catch (RepostedException)
-                        {
-                            MessageBox.Show("At least one of the items you selected has already been processed. Processing cancelled.");
-                        }
-                        catch (PostCancelledException)
-                        {
-                            MessageBox.Show("Processing cancelled.");
-                        }
-                        catch (MissingSettingsException)
-                        {
-                            if (Security.CanConfigurePost<Invoice>())
-                            {
-                                if(MessageBox.Show("Processing has not been configured for Invoices. Would you like to configure this now?",
-                                    "Configure Processing?", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
-                                {
-                                    ConfigureInvoicePost();
-                                }
-                                else
-                                {
-                                    MessageBox.Show("Processing cancelled.");
-                                }
-                            }
-                            else
-                            {
-                                MessageBox.Show("Processing has not been configured for Invoices!");
-                            }
-                        }
-                        catch(Exception e)
-                        {
-                            MessageBox.Show($"Processing failed: {e.Message}");
-                            Invoices.Refresh(false, true);
-                        }
-                    }
-                });
-            }
-
-            if (Security.CanConfigurePost<Invoice>())
-            {
-                host.CreateSetupAction(new PanelAction
-                {
-                    Caption = "Configure Invoice Processing",
-                    OnExecute = action =>
-                    {
-                        ConfigureInvoicePost();
-                    }
-                });
-            }
+            PostUtils.CreateToolbarButtons(
+                host,
+                () => Invoices.SelectedRows.Select(x => x.ToObject<Invoice>()),
+                () => Invoices.Refresh(false, true),
+                true);
         }
 
         public void Setup()

+ 138 - 0
prs.desktop/Utils/PostUtils.cs

@@ -0,0 +1,138 @@
+using Comal.Classes;
+using InABox.Clients;
+using InABox.Core;
+using InABox.Core.Postable;
+using InABox.DynamicGrid;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace PRSDesktop
+{
+    public static class PostUtils
+    {
+        private static readonly Inflector.Inflector inflector = new(new CultureInfo("en"));
+
+        public static void PostEntities<T>(IEnumerable<T> entities, Action refresh, Action? configurePost = null)
+            where T : Entity, IPostable, IRemotable, IPersistent, new()
+        {
+            var items = entities.AsList();
+            if (!items.Any())
+            {
+                MessageBox.Show($"Please select at least one {typeof(T).Name}.");
+                return;
+            }
+            try
+            {
+                if (PosterUtils.Process(items))
+                {
+                    MessageBox.Show("Processing successful!");
+                    refresh();
+                }
+                else
+                {
+                    MessageBox.Show("Processing failed.");
+                    refresh();
+                }
+            }
+            catch (RepostedException)
+            {
+                MessageBox.Show("At least one of the items you selected has already been processed. Processing cancelled.");
+            }
+            catch (PostCancelledException)
+            {
+                MessageBox.Show("Processing cancelled.");
+            }
+            catch (MissingSettingsException)
+            {
+                if (configurePost is not null && Security.CanConfigurePost<T>())
+                {
+                    if (MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}. Would you like to configure this now?",
+                        "Configure Processing?", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
+                    {
+                        configurePost();
+                    }
+                    else
+                    {
+                        MessageBox.Show("Processing cancelled.");
+                    }
+                }
+                else
+                {
+                    MessageBox.Show($"Processing has not been configured for {inflector.Pluralize(typeof(T).Name)}!");
+                }
+            }
+            catch (Exception e)
+            {
+                MessageBox.Show($"Processing failed: {e.Message}");
+                refresh();
+            }
+        }
+
+        public static void CreateToolbarButtons<T>(IPanelHost host, Func<IEnumerable<T>> entities, Action refresh, Action? configurePost = null)
+            where T : Entity, IPostable, IRemotable, IPersistent, new()
+        {
+            var postSettings = PosterUtils.LoadPostableSettings<T>();
+            if (Security.CanPost<T>())
+            {
+                Bitmap? image = null;
+                if (postSettings.Thumbnail.ID != Guid.Empty)
+                {
+                    var icon = new Client<Document>()
+                        .Load(new Filter<Document>(x => x.ID).IsEqualTo(postSettings.Thumbnail.ID)).FirstOrDefault();
+                    if (icon is not null)
+                    {
+                        image = new ImageConverter().ConvertFrom(icon.Data) as Bitmap;
+                    }
+                }
+                host.CreatePanelAction(new PanelAction
+                {
+                    Caption = postSettings.ButtonName.NotWhiteSpaceOr($"Process {inflector.Pluralize(typeof(T).Name)}"),
+                    Image = image ?? PRSDesktop.Resources.edit,
+                    OnExecute = action =>
+                    {
+                        PostEntities(
+                            entities(),
+                            refresh,
+                            configurePost);
+                    }
+                });
+            }
+
+            if (configurePost is not null && Security.CanConfigurePost<T>())
+            {
+                host.CreateSetupAction(new PanelAction
+                {
+                    Caption = $"Configure {typeof(T).Name} Processing",
+                    OnExecute = action =>
+                    {
+                        configurePost();
+                    }
+                });
+            }
+        }
+
+        public static void ConfigurePost<T>()
+            where T : Entity, IPostable, IRemotable, IPersistent, new()
+        {
+            var postSettings = PosterUtils.LoadPostableSettings<T>();
+
+            var grid = (DynamicGridUtils.CreateDynamicGrid(typeof(DynamicGrid<>), typeof(PostableSettings)) as DynamicGrid<PostableSettings>)!;
+            if (grid.EditItems(new PostableSettings[] { postSettings }))
+            {
+                PosterUtils.SavePostableSettings<T>(postSettings);
+            }
+        }
+
+        public static void CreateToolbarButtons<T>(IPanelHost host, Func<IEnumerable<T>> entities, Action refresh, bool allowConfig)
+            where T : Entity, IPostable, IRemotable, IPersistent, new()
+        {
+            CreateToolbarButtons(host, entities, refresh, allowConfig ? ConfigurePost<T> : null);
+        }
+    }
+}