public static SupplierLink MakeSupplierCardLink(MYOBSupplier supplier) { var c = new SupplierLink { DisplayID = supplier.DisplayID, Name = supplier.CompanyName, UID = supplier.Uid, URI = new Uri(supplier.URI) }; if (c.UID == null) { throw new Exception($"UID not set for customer {supplier.DisplayID}"); } if (c.URI == null) { throw new Exception($"URI not set for {supplier.DisplayID} "); } return(c); }
private static void CopyRecordFromMYOB(AMSDbContext connect, MYOBSupplier jtItem, Supplier item) { jtItem.CompanyName = HandyContactFunctions.MakeKey( item.IsIndividual, item.LastName, item.FirstName, item.CompanyName); jtItem.DisplayID = item.DisplayID; jtItem.FirstName = item.FirstName; jtItem.LastName = item.LastName; jtItem.IsIndividual = item.IsIndividual; jtItem.IsActive = item.IsActive; jtItem.RowVersion = item.RowVersion; jtItem.Uid = item.UID; jtItem.LastModified = item.LastModified; jtItem.URI = item.URI.ToString(); jtItem.IsActive = item.IsActive; SyncAddresses(connect, jtItem, item); }
public async void AddItemToMYOB( AMSDbContext connect, MYOBSupplier jtMyobSupplier, CompanyFile myCompanyFile, CompanyFileCredentials myCredentials, CancellationToken ct) { var item = new Supplier { IsActive = jtMyobSupplier.IsActive, CompanyName = jtMyobSupplier.CompanyName, FirstName = jtMyobSupplier.FirstName, LastName = jtMyobSupplier.LastName }; HandyFunctions.Log(item.ToJson2()); var task = _myService.InsertAsync(myCompanyFile, item, myCredentials, ct, ErrorLevel.WarningsAsErrors); await task; jtMyobSupplier.Uid = item.UID; connect.SaveChanges(); }
private static void AddNewUpdateExistingRecords( Supplier[] ar, AMSDbContext connect, IProgress <int> progressCallback) { var info = ""; var stepNum = 1; var numSteps = ar.Count(); foreach (var item in ar) { var key = HandyContactFunctions.MakeKey( item.IsIndividual, item.LastName, item.FirstName, item.CompanyName); if (key == info) { throw new ExceptionDuplicateKey($"{info}"); } info = key; HandyFunctions.Log(info); var jtSupplier = connect.MYOBSuppliers.SingleOrDefault(x => x.Uid == item.UID); //unique idex should ensure only 1 if (jtSupplier == null) { jtSupplier = new MYOBSupplier(); connect.MYOBSuppliers.Add(jtSupplier); } CopyRecordFromMYOB(connect, jtSupplier, item); progressCallback.Report((100 * stepNum) / numSteps); stepNum++; } connect.SaveChanges(); }
private static void SyncAddresses(AMSDbContext connect, MYOBSupplier jtItem, Supplier item) { foreach (var a in jtItem.Addresses) { a.TaggedToDelete = true; } if (item.Addresses != null) { foreach (var address in item.Addresses) { var addresses = jtItem.Addresses.AsQueryable(); var tempAddress = address; var existingAddresses = addresses.Where(x => x.Location == tempAddress.Location).ToArray(); if (existingAddresses.Count() == 1) { var existingAddress = existingAddresses[0]; ModifyIfNeeded( connect, existingAddress, "ContactName", existingAddress.ContactName, tempAddress.ContactName); ModifyIfNeeded(connect, existingAddress, "Street", existingAddress.Street, tempAddress.Street); ModifyIfNeeded(connect, existingAddress, "City", existingAddress.City, tempAddress.City); ModifyIfNeeded(connect, existingAddress, "State", existingAddress.State, tempAddress.State); ModifyIfNeeded( connect, existingAddress, "Postcode", existingAddress.Postcode, tempAddress.PostCode); ModifyIfNeeded( connect, existingAddress, "Country", existingAddress.Country, tempAddress.Country); ModifyIfNeeded(connect, existingAddress, "Email", existingAddress.Email, tempAddress.Email); ModifyIfNeeded(connect, existingAddress, "Phone1", existingAddress.Phone1, tempAddress.Phone1); ModifyIfNeeded(connect, existingAddress, "Phone2", existingAddress.Phone2, tempAddress.Phone2); ModifyIfNeeded(connect, existingAddress, "Fax", existingAddress.Phone2, tempAddress.Fax); ModifyIfNeeded(connect, existingAddress, "WebSite", existingAddress.Phone2, tempAddress.Website); existingAddress.TaggedToDelete = false; if (existingAddress.ContactName != tempAddress.ContactName) { existingAddress.ContactName = tempAddress.ContactName; connect.Entry(existingAddress).State = EntityState.Modified; } } else { foreach (var duplAddress in existingAddresses) { duplAddress.TaggedToDelete = true; } if (!string.IsNullOrEmpty(address.Street)) { var newAddress = new MYOBSupplierAddress(); HandyFunctions.PopulateMyobAddress(newAddress, address); jtItem.Addresses.Add(newAddress); newAddress.MyobSupplier = jtItem; } } } } var deleteQueue = jtItem.Addresses.Where(x => x.TaggedToDelete).ToArray(); foreach (var a in deleteQueue) { var entry = connect.Entry(a); entry.State = EntityState.Deleted; } }