Explorar o código

Added MYOB Poster for receipts

Kenric Nugteren hai 1 ano
pai
achega
7a1af5e7db

+ 13 - 1
prs.classes/Entities/Invoice/InvoiceLink.cs

@@ -3,7 +3,7 @@ using InABox.Core;
 
 namespace Comal.Classes
 {
-    public class InvoiceLink : EntityLink<Invoice>
+    public class InvoiceLink : EntityLink<Invoice>, IPostable
     {
         [CodePopupEditor(typeof(Invoice))]
         public override Guid ID { get; set; }
@@ -25,5 +25,17 @@ namespace Comal.Classes
 
         [NullEditor]
         public CustomerLink CustomerLink { get; set; }
+
+        [NullEditor]
+        public DateTime Posted { get; set; }
+
+        [NullEditor]
+        public PostedStatus PostedStatus { get; set; }
+
+        [NullEditor]
+        public string PostedNote { get; set; }
+
+        [NullEditor]
+        public string PostedReference { get; set; }
     }
 }

+ 14 - 1
prs.classes/Entities/Receipt/Receipt.cs

@@ -19,7 +19,8 @@ namespace Comal.Classes
     }
 
     [UserTracking(typeof(Invoice))]
-    public class Receipt : Entity, IPersistent, IRemotable, ILicense<AccountsReceivableLicense>
+    public class Receipt : Entity, IPersistent, IRemotable, ILicense<AccountsReceivableLicense>,
+        IPostable
     {
         [DateEditor(TodayVisible = true)]
         [EditorSequence(0)]
@@ -43,6 +44,18 @@ namespace Comal.Classes
         [Aggregate(typeof(ReceiptTotal))]
         public double Total { get; set; }
 
+        [NullEditor]
+        public DateTime Posted { get; set; }
+
+        [NullEditor]
+        public PostedStatus PostedStatus { get; set; }
+
+        [NullEditor]
+        public string PostedNote { get; set; }
+
+        [NullEditor]
+        public string PostedReference { get; set; }
+
         public override string ToString()
         {
             return string.Format("{0:dd MMM yy}: {1} (${2:F2})", Date, ReceiptTypeLink.Description, Total);

+ 2 - 1
prs.shared/Posters/MYOB/CustomerMYOBPoster.cs

@@ -246,7 +246,8 @@ public class CustomerMYOBPoster : IMYOBPoster<Customer, CustomerMYOBPosterSettin
             {
                 try
                 {
-                    var result = service.Update(ConnectionData.CompanyFile, myobCustomer, ConnectionData.CompanyFileCredentials);
+                    var result = service.UpdateEx(ConnectionData.CompanyFile, myobCustomer, ConnectionData.CompanyFileCredentials);
+                    customer.PostedReference = result.UID.ToString();
                     results.AddSuccess(customer);
                 }
                 catch(Exception e)

+ 6 - 5
prs.shared/Posters/MYOB/InvoiceMYOBPoster.cs

@@ -39,10 +39,10 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
         model.SetColumns<Invoice>(RequiredInvoiceColumns());
 
         model.SetIsDefault<InvoiceLine>(true, alias: "Invoice_InvoiceLine");
-        model.SetColumns<InvoiceLine>(RequiredInvoiceLineColumns());
+        model.SetColumns<InvoiceLine>(RequiredInvoiceLineColumns(), alias: "Invoice_InvoiceLine");
 
         model.SetIsDefault<Customer>(true, alias: "Invoice_Customer");
-        model.SetColumns<Customer>(RequiredCustomerColumns());
+        model.SetColumns<Customer>(RequiredCustomerColumns(), alias: "Invoice_Customer");
 
         return true;
     }
@@ -135,7 +135,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
 
                     var line = new MYOBInvoiceLine();
                     line.Type = InvoiceLineType.Transaction;
-                    line.Description = item.Description;
+                    line.Description = item.Description.Truncate(1000);
                     // line.UnitOfMeasure = 
                     // line.UnitCount = 
                     // line.UnitPrice = 
@@ -216,7 +216,7 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
             // FreightTaxCode
             // Category
             // Salesperson
-            myobInvoice.Comment = invoice.Description;
+            myobInvoice.Comment = invoice.Description.Truncate(2000);
             // ShippingMethod
             // JournalMemo
             // ReferralSource
@@ -228,7 +228,8 @@ public class InvoiceMYOBPoster : IMYOBPoster<Invoice, InvoiceMYOBPosterSettings>
 
             try
             {
-                var result = service.Update(ConnectionData.CompanyFile, myobInvoice, ConnectionData.CompanyFileCredentials);
+                var result = service.UpdateEx(ConnectionData.CompanyFile, myobInvoice, ConnectionData.CompanyFileCredentials);
+                invoice.PostedReference = result.UID.ToString();
                 results.AddSuccess(invoice);
             }
             catch (Exception e)

+ 185 - 0
prs.shared/Posters/MYOB/ReceiptMYOBPoster.cs

@@ -0,0 +1,185 @@
+using Comal.Classes;
+using InABox.Core;
+using InABox.Poster.MYOB;
+using MYOB.AccountRight.SDK.Contracts.Version2.Sale;
+using MYOB.AccountRight.SDK.Services.Sale;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Markup.Localizer;
+using Invoice = Comal.Classes.Invoice;
+using MYOBReceipt = MYOB.AccountRight.SDK.Contracts.Version2.Sale.CustomerPayment;
+
+namespace PRS.Shared.Posters.MYOB;
+
+public class ReceiptMYOBPosterSettings : MYOBPosterSettings
+{
+}
+
+public class ReceiptMYOBPoster : IMYOBPoster<Receipt, ReceiptMYOBPosterSettings>
+{
+    public MYOBConnectionData ConnectionData { get; set; }
+    public ReceiptMYOBPosterSettings Settings { get; set; }
+    public MYOBGlobalPosterSettings GlobalSettings { get; set; }
+
+    public bool BeforePost(IDataModel<Receipt> model)
+    {
+        foreach(var (_, table) in model.ModelTables)
+        {
+            table.ShouldLoad = false;
+        }
+        model.SetShouldLoad<Receipt>(true);
+        model.SetColumns<Receipt>(RequiredReceiptColumns());
+
+        model.SetIsDefault<InvoiceReceipt>(true, alias: "Receipt_InvoiceReceipt");
+        model.SetColumns<InvoiceReceipt>(RequiredInvoiceReceiptColumns(), alias: "Receipt_InvoiceReceipt");
+
+        model.SetIsDefault<Customer>(true, alias: "Receipt_Customer");
+        model.SetColumns<Customer>(RequiredCustomerColumns(), alias: "Receipt_Customer");
+
+        return true;
+    }
+
+    private static Columns<Receipt> RequiredReceiptColumns()
+    {
+        return Columns.None<Receipt>()
+            .Add(x => x.ID)
+            .Add(x => x.PostedReference)
+            .Add(x => x.CustomerLink.ID)
+            .Add(x => x.Date)
+            .Add(x => x.Total)
+            .Add(x => x.Notes);
+    }
+
+    private static Columns<InvoiceReceipt> RequiredInvoiceReceiptColumns()
+    {
+        return Columns.None<InvoiceReceipt>()
+            .Add(x => x.ID)
+            .Add(x => x.Amount)
+            .Add(x => x.ReceiptLink.ID)
+            .Add(x => x.InvoiceLink.Number)
+            .Add(x => x.InvoiceLink.PostedReference);
+    }
+
+    private static Columns<Customer> RequiredCustomerColumns()
+    {
+        return CustomerMYOBPoster.RequiredColumns();
+    }
+
+    public IPostResult<Receipt> Process(IDataModel<Receipt> model)
+    {
+        var results = new PostResult<Receipt>();
+
+        var service = new CustomerPaymentService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
+
+        var receipts = model.GetTable<Receipt>().ToArray<Receipt>();
+
+        var customers = model.GetTable<Customer>("Receipt_Customer")
+            .ToObjects<Customer>().ToDictionary(x => x.ID);
+
+        var invoices = model.GetTable<InvoiceReceipt>("Receipt_InvoiceReceipt")
+            .ToObjects<InvoiceReceipt>().GroupBy(x => x.ReceiptLink.ID).ToDictionary(x => x.Key, x => x.ToArray());
+        
+        foreach(var receipt in receipts)
+        {
+            bool isNew;
+            MYOBReceipt myobReceipt;
+            Exception? error;
+            if(Guid.TryParse(receipt.PostedReference, out var myobID))
+            {
+                if(!service.Get(ConnectionData, myobID).Get(out var newReceipt, out error))
+                {
+                    CoreUtils.LogException("", error, $"Failed to find Receipt in MYOB with id {myobID}");
+                    results.AddFailed(receipt, $"Failed to find Receipt in MYOB with id {myobID}: {error.Message}");
+                    continue;
+                }
+                myobReceipt = newReceipt;
+                isNew = false;
+            }
+            else
+            {
+                myobReceipt = new MYOBReceipt();
+                isNew = true;
+            }
+
+            myobReceipt.DepositTo = DepositTo.UndepositedFunds;
+
+            // Setting this to null right now.
+            myobReceipt.Account.UID = Guid.Empty;
+            // Account =
+
+            if(customers.TryGetValue(receipt.CustomerLink.ID, out var customer))
+            {
+                if(!CustomerMYOBPoster.MapCustomer(ConnectionData, customer).Get(out var customerID, out error))
+                {
+                    CoreUtils.LogException("", error, $"Error while posting receipt {receipt.ID}");
+                    results.AddFailed(receipt, error.Message);
+                    continue;
+                }
+                myobReceipt.Customer.UID = customerID;
+            }
+
+            // myobReceipt.ReceiptNumber = 
+            myobReceipt.Date = receipt.Date;
+            myobReceipt.AmountReceived = (decimal)receipt.Total;
+            // myobReceipt.AmountReceivedForeign = 0;
+            // myobReceipt.PaymentMethod =
+            myobReceipt.Memo = receipt.Notes.Truncate(255);
+
+            if(invoices.TryGetValue(receipt.ID, out var receiptInvoices))
+            {
+                var myobInvoices = new CustomerPaymentLine[receiptInvoices.Length];
+                string? failed = null;
+                for(int i = 0; i < receiptInvoices.Length; ++i)
+                {
+                    var invoice = receiptInvoices[i];
+
+                    var line = new CustomerPaymentLine();
+                    line.RowID = i + 1;
+                    line.Number = invoice.InvoiceLink.Number.ToString().Truncate(8);
+                    line.AmountApplied = (decimal)invoice.Amount;
+                    line.Type = CustomerPaymentLineType.Invoice;
+
+                    if(Guid.TryParse(invoice.InvoiceLink.PostedReference, out var myobInvoiceID))
+                    {
+                        line.UID = myobInvoiceID;
+                    }
+                    else
+                    {
+                        failed = $"Invoice {invoice.InvoiceLink.Number} hasn't been posted yet; it must be posted before this receipt can be.";
+                        break;
+                    }
+
+                    myobInvoices[i] = line;
+                }
+
+                if(failed is not null)
+                {
+                    results.AddFailed(receipt, failed);
+                    continue;
+                }
+                myobReceipt.Invoices = [];
+            }
+            else
+            {
+                myobReceipt.Invoices = [];
+            }
+
+            try
+            {
+                var result = service.UpdateEx(ConnectionData.CompanyFile, myobReceipt, ConnectionData.CompanyFileCredentials);
+                receipt.PostedReference = result.UID.ToString();
+                results.AddSuccess(receipt);
+            }
+            catch(Exception e)
+            {
+                CoreUtils.LogException("", e, $"Error while posting receipt {receipt.ID}");
+                results.AddFailed(receipt, e.Message);
+            }
+        }
+
+        return results;
+    }
+}