|
@@ -293,6 +293,11 @@ public class CustomerMYOBPoster :
|
|
}).Flatten();
|
|
}).Flatten();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static bool IsBlankCode(string code)
|
|
|
|
+ {
|
|
|
|
+ return code.IsNullOrWhiteSpace() || code.Equals("*None");
|
|
|
|
+ }
|
|
|
|
+
|
|
public IPullResult<Customer> Pull()
|
|
public IPullResult<Customer> Pull()
|
|
{
|
|
{
|
|
var result = new PullResult<Customer>();
|
|
var result = new PullResult<Customer>();
|
|
@@ -316,8 +321,8 @@ public class CustomerMYOBPoster :
|
|
}
|
|
}
|
|
|
|
|
|
var myobIDs = myobCustomers.Items.ToArray(x => x.UID.ToString());
|
|
var myobIDs = myobCustomers.Items.ToArray(x => x.UID.ToString());
|
|
- var myobCodes = myobCustomers.Items.Select(x => x.DisplayID).Where(x => !x.IsNullOrWhiteSpace()).ToArray();
|
|
|
|
- var myobNames = myobCustomers.Items.Where(x => x.DisplayID.IsNullOrWhiteSpace() && !x.CompanyName.IsNullOrWhiteSpace())
|
|
|
|
|
|
+ var myobCodes = myobCustomers.Items.Select(x => x.DisplayID).Where(x => !IsBlankCode(x)).ToArray();
|
|
|
|
+ var myobNames = myobCustomers.Items.Where(x => IsBlankCode(x.DisplayID) && !x.CompanyName.IsNullOrWhiteSpace())
|
|
.Select(x => x.CompanyName).ToArray();
|
|
.Select(x => x.CompanyName).ToArray();
|
|
|
|
|
|
var customers = Client.Query(
|
|
var customers = Client.Query(
|
|
@@ -328,6 +333,7 @@ public class CustomerMYOBPoster :
|
|
.ToArray<Customer>();
|
|
.ToArray<Customer>();
|
|
var customerDict = customers.Where(x => !x.PostedReference.IsNullOrWhiteSpace())
|
|
var customerDict = customers.Where(x => !x.PostedReference.IsNullOrWhiteSpace())
|
|
.ToDictionary(x => x.PostedReference);
|
|
.ToDictionary(x => x.PostedReference);
|
|
|
|
+ var blankCustomers = customers.Where(x => x.PostedReference.IsNullOrWhiteSpace()).ToArray();
|
|
|
|
|
|
var needCodes = new Dictionary<string, (string prefix, int i, Customer customer)>();
|
|
var needCodes = new Dictionary<string, (string prefix, int i, Customer customer)>();
|
|
|
|
|
|
@@ -338,9 +344,9 @@ public class CustomerMYOBPoster :
|
|
// Skipping existing customers at this point.
|
|
// Skipping existing customers at this point.
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- customer = !myobCustomer.DisplayID.IsNullOrWhiteSpace()
|
|
|
|
- ? customers.FirstOrDefault(x => string.Equals(x.Code, myobCustomer.DisplayID))
|
|
|
|
- : customers.FirstOrDefault(x => string.Equals(x.Name, myobCustomer.CompanyName));
|
|
|
|
|
|
+ customer = !IsBlankCode(myobCustomer.DisplayID)
|
|
|
|
+ ? blankCustomers.FirstOrDefault(x => string.Equals(x.Code, myobCustomer.DisplayID))
|
|
|
|
+ : blankCustomers.FirstOrDefault(x => string.Equals(x.Name, myobCustomer.CompanyName));
|
|
if(customer is not null)
|
|
if(customer is not null)
|
|
{
|
|
{
|
|
customer.PostedReference = myobCustomer.UID.ToString();
|
|
customer.PostedReference = myobCustomer.UID.ToString();
|
|
@@ -351,7 +357,7 @@ public class CustomerMYOBPoster :
|
|
customer = new Customer();
|
|
customer = new Customer();
|
|
|
|
|
|
string code;
|
|
string code;
|
|
- if (!myobCustomer.DisplayID.IsNullOrWhiteSpace())
|
|
|
|
|
|
+ if (!IsBlankCode(myobCustomer.DisplayID))
|
|
{
|
|
{
|
|
code = myobCustomer.DisplayID.ToString();
|
|
code = myobCustomer.DisplayID.ToString();
|
|
}
|
|
}
|
|
@@ -375,17 +381,20 @@ public class CustomerMYOBPoster :
|
|
customer.Name = myobCustomer.CompanyName;
|
|
customer.Name = myobCustomer.CompanyName;
|
|
customer.ABN = myobCustomer.SellingDetails.ABN;
|
|
customer.ABN = myobCustomer.SellingDetails.ABN;
|
|
|
|
|
|
- var delivery = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 2);
|
|
|
|
- if(delivery is not null)
|
|
|
|
|
|
+ if(myobCustomer.Addresses is not null)
|
|
{
|
|
{
|
|
- customer.Delivery.CopyFrom(ContactMYOBUtils.ConvertAddress(delivery));
|
|
|
|
- }
|
|
|
|
- var postal = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 1);
|
|
|
|
- if(postal is not null)
|
|
|
|
- {
|
|
|
|
- customer.Postal.CopyFrom(ContactMYOBUtils.ConvertAddress(postal));
|
|
|
|
|
|
+ var delivery = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 2);
|
|
|
|
+ if(delivery is not null)
|
|
|
|
+ {
|
|
|
|
+ customer.Delivery.CopyFrom(ContactMYOBUtils.ConvertAddress(delivery));
|
|
|
|
+ }
|
|
|
|
+ var postal = myobCustomer.Addresses.FirstOrDefault(x => x.Location == 1);
|
|
|
|
+ if(postal is not null)
|
|
|
|
+ {
|
|
|
|
+ customer.Postal.CopyFrom(ContactMYOBUtils.ConvertAddress(postal));
|
|
|
|
+ }
|
|
|
|
+ customer.Email = delivery?.Email ?? postal?.Email ?? "";
|
|
}
|
|
}
|
|
- customer.Email = delivery?.Email ?? postal?.Email ?? "";
|
|
|
|
|
|
|
|
customer.PostedReference = myobCustomer.UID.ToString();
|
|
customer.PostedReference = myobCustomer.UID.ToString();
|
|
result.AddEntity(PullResultType.New, customer);
|
|
result.AddEntity(PullResultType.New, customer);
|
|
@@ -407,7 +416,7 @@ public class CustomerMYOBPoster :
|
|
int i = needed.i;
|
|
int i = needed.i;
|
|
do
|
|
do
|
|
{
|
|
{
|
|
- needed.customer.Code = $"{needed.prefix}{needed.i:d3}";
|
|
|
|
|
|
+ needed.customer.Code = $"{needed.prefix}{i:d3}";
|
|
++i;
|
|
++i;
|
|
} while (customerCodes.Contains(needed.customer.Code));
|
|
} while (customerCodes.Contains(needed.customer.Code));
|
|
customerCodes.Add(needed.customer.Code);
|
|
customerCodes.Add(needed.customer.Code);
|
|
@@ -465,10 +474,31 @@ public class CustomerMYOBPoster :
|
|
myobCustomer = myobCustomers.Items[0];
|
|
myobCustomer = myobCustomers.Items[0];
|
|
isNew = false;
|
|
isNew = false;
|
|
}
|
|
}
|
|
|
|
+ else if(service.Query(
|
|
|
|
+ ConnectionData,
|
|
|
|
+ new Filter<MYOBCustomer>(x => x.CompanyName).IsEqualTo(customer.Name)
|
|
|
|
+ .And(new Filter<MYOBCustomer>(x => x.DisplayID).IsEqualTo(null)
|
|
|
|
+ .Or(x => x.DisplayID).IsEqualTo("")
|
|
|
|
+ .Or(x => x.DisplayID).IsEqualTo("*None")),
|
|
|
|
+ top: 1).Get(out myobCustomers, out error))
|
|
|
|
+ {
|
|
|
|
+ if(myobCustomers.Items.Length > 0)
|
|
|
|
+ {
|
|
|
|
+ myobCustomer = myobCustomers.Items[0];
|
|
|
|
+ myobCustomer.DisplayID = customer.Code;
|
|
|
|
+ isNew = false;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ myobCustomer = new MYOBCustomer();
|
|
|
|
+ isNew = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- myobCustomer = new MYOBCustomer();
|
|
|
|
- isNew = true;
|
|
|
|
|
|
+ CoreUtils.LogException("", error);
|
|
|
|
+ results.AddFailed(customer, error.Message);
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
else
|