|
@@ -245,6 +245,145 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
|
|
|
}).Flatten();
|
|
|
}
|
|
|
|
|
|
+ private static bool IsBlankCode(string code)
|
|
|
+ {
|
|
|
+ return code.IsNullOrWhiteSpace() || code.Equals("*None");
|
|
|
+ }
|
|
|
+
|
|
|
+ public IPullResult<Supplier> Pull()
|
|
|
+ {
|
|
|
+ var result = new PullResult<Supplier>();
|
|
|
+
|
|
|
+ var top = 400;
|
|
|
+ var skip = 0;
|
|
|
+
|
|
|
+ var supplierCodes = new HashSet<string>();
|
|
|
+
|
|
|
+ var service = new SupplierService(ConnectionData.Configuration, null, ConnectionData.AuthKey);
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ if(!service.Query(ConnectionData, null, top: top, skip: skip).Get(out var myobSuppliers, out var error))
|
|
|
+ {
|
|
|
+ CoreUtils.LogException("", error);
|
|
|
+ throw new PullFailedMessageException(error.Message);
|
|
|
+ }
|
|
|
+ if(myobSuppliers.Items.Length == 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ var myobIDs = myobSuppliers.Items.ToArray(x => x.UID.ToString());
|
|
|
+ var myobCodes = myobSuppliers.Items.Select(x => x.DisplayID).Where(x => !IsBlankCode(x)).ToArray();
|
|
|
+ var myobNames = myobSuppliers.Items.Where(x => IsBlankCode(x.DisplayID) && !x.CompanyName.IsNullOrWhiteSpace())
|
|
|
+ .Select(x => x.CompanyName).ToArray();
|
|
|
+
|
|
|
+ var suppliers = Client.Query(
|
|
|
+ new Filter<Supplier>(x => x.PostedReference).InList(myobIDs)
|
|
|
+ .Or(x => x.Code).InList(myobCodes)
|
|
|
+ .Or(x => x.Name).InList(myobNames),
|
|
|
+ Columns.None<Supplier>().Add(x => x.ID).Add(x => x.PostedReference).Add(x => x.Code).Add(x => x.Name))
|
|
|
+ .ToArray<Supplier>();
|
|
|
+ var supplierDict = suppliers.Where(x => !x.PostedReference.IsNullOrWhiteSpace())
|
|
|
+ .ToDictionary(x => x.PostedReference);
|
|
|
+ var blankSuppliers = suppliers.Where(x => x.PostedReference.IsNullOrWhiteSpace()).ToArray();
|
|
|
+
|
|
|
+ var needCodes = new Dictionary<string, (string prefix, int i, Supplier supplier)>();
|
|
|
+
|
|
|
+ foreach(var myobSupplier in myobSuppliers.Items)
|
|
|
+ {
|
|
|
+ if (supplierDict.TryGetValue(myobSupplier.UID.ToString(), out var supplier))
|
|
|
+ {
|
|
|
+ // Skipping existing suppliers at this point.
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ supplier = !IsBlankCode(myobSupplier.DisplayID)
|
|
|
+ ? blankSuppliers.FirstOrDefault(x => string.Equals(x.Code, myobSupplier.DisplayID))
|
|
|
+ : blankSuppliers.FirstOrDefault(x => string.Equals(x.Name, myobSupplier.CompanyName));
|
|
|
+ if(supplier is not null)
|
|
|
+ {
|
|
|
+ supplier.PostedReference = myobSupplier.UID.ToString();
|
|
|
+ result.AddEntity(PullResultType.Linked, supplier);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ supplier = new Supplier();
|
|
|
+
|
|
|
+ string code;
|
|
|
+ if (!IsBlankCode(myobSupplier.DisplayID))
|
|
|
+ {
|
|
|
+ code = myobSupplier.DisplayID.ToString();
|
|
|
+ }
|
|
|
+ else if (!myobSupplier.CompanyName.IsNullOrWhiteSpace())
|
|
|
+ {
|
|
|
+ code = myobSupplier.CompanyName[..Math.Min(3, myobSupplier.CompanyName.Length)].ToUpper();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ code = "SUP";
|
|
|
+ }
|
|
|
+ int i = 1;
|
|
|
+ supplier.Code = code;
|
|
|
+ while (supplierCodes.Contains(supplier.Code))
|
|
|
+ {
|
|
|
+ supplier.Code = $"{code}{i:d3}";
|
|
|
+ ++i;
|
|
|
+ }
|
|
|
+ supplierCodes.Add(supplier.Code);
|
|
|
+
|
|
|
+ supplier.Name = myobSupplier.CompanyName;
|
|
|
+ supplier.ABN = myobSupplier.BuyingDetails.ABN;
|
|
|
+
|
|
|
+ if(myobSupplier.Addresses is not null)
|
|
|
+ {
|
|
|
+ var delivery = myobSupplier.Addresses.FirstOrDefault(x => x.Location == 2);
|
|
|
+ if(delivery is not null)
|
|
|
+ {
|
|
|
+ supplier.Delivery.CopyFrom(ContactMYOBUtils.ConvertAddress(delivery));
|
|
|
+ }
|
|
|
+ var postal = myobSupplier.Addresses.FirstOrDefault(x => x.Location == 1);
|
|
|
+ if(postal is not null)
|
|
|
+ {
|
|
|
+ supplier.Postal.CopyFrom(ContactMYOBUtils.ConvertAddress(postal));
|
|
|
+ }
|
|
|
+ supplier.Email = delivery?.Email ?? postal?.Email ?? "";
|
|
|
+ }
|
|
|
+
|
|
|
+ supplier.PostedReference = myobSupplier.UID.ToString();
|
|
|
+ result.AddEntity(PullResultType.New, supplier);
|
|
|
+ needCodes.Add(supplier.Code, (code, i, supplier));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Do code clash checking
|
|
|
+ while(needCodes.Count > 0)
|
|
|
+ {
|
|
|
+ var codes = Client.Query(
|
|
|
+ new Filter<Supplier>(x => x.Code).InList(needCodes.Values.Select(x => x.supplier.Code).ToArray()),
|
|
|
+ Columns.None<Supplier>().Add(x => x.Code));
|
|
|
+ var newNeedCodes = new Dictionary<string, (string prefix, int i, Supplier supplier)>();
|
|
|
+ foreach(var row in codes.Rows)
|
|
|
+ {
|
|
|
+ var code = row.Get<Supplier, string>(x => x.Code);
|
|
|
+ if(needCodes.Remove(code, out var needed))
|
|
|
+ {
|
|
|
+ int i = needed.i;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ needed.supplier.Code = $"{needed.prefix}{i:d3}";
|
|
|
+ ++i;
|
|
|
+ } while (supplierCodes.Contains(needed.supplier.Code));
|
|
|
+ supplierCodes.Add(needed.supplier.Code);
|
|
|
+ newNeedCodes.Add(needed.supplier.Code, (needed.prefix, i, needed.supplier));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ needCodes = newNeedCodes;
|
|
|
+ }
|
|
|
+
|
|
|
+ skip += top;
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
public IPostResult<Supplier> Process(IDataModel<Supplier> model)
|
|
|
{
|
|
|
var results = new PostResult<Supplier>();
|
|
@@ -340,4 +479,11 @@ public class SupplierMYOBPoster : IMYOBPoster<Supplier, SupplierMYOBPosterSettin
|
|
|
return results;
|
|
|
}
|
|
|
}
|
|
|
-public class SupplierMYOBPosterEngine<T> : MYOBPosterEngine<Supplier, SupplierMYOBPoster, SupplierMYOBPosterSettings> { }
|
|
|
+public class SupplierMYOBPosterEngine<T> : MYOBPosterEngine<Supplier, SupplierMYOBPoster, SupplierMYOBPosterSettings>, IPullerEngine<Supplier, SupplierMYOBPoster>
|
|
|
+{
|
|
|
+ public IPullResult<Supplier> DoPull()
|
|
|
+ {
|
|
|
+ LoadConnectionData();
|
|
|
+ return Poster.Pull();
|
|
|
+ }
|
|
|
+}
|