|
@@ -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;
|
|
|
+ }
|
|
|
+}
|