private static bool RunMigrations(AMSDbContext db) { var result = AskWhetherToUpgrade(); if (!result) { return(false); } try { //http://stackoverflow.com/questions/34699283/ef6-1-3-expects-createdon-field-in-migrationhistory-table-when-database-setiniti //MessageBox.Show( // "You might need to the following SQL First /n/p " + // "ALTER TABLE dbo.__MigrationHistory ADD CreatedOn DateTime Default GETDATE() /n/p" + "GO /n/p" + // "UPDATE dbo.__MigrationHistory SET CreatedOn = GETDATE()"); var num = db.RunMigrations(); MessageBox.Show($"{num} migrations have been run"); return(true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(false); } }
//private void RebuildMirrorLinks(AMSDbContext connect) //{ // // DisconnectMismatches(connect); keep links we set up, for example for cash sale and for head office // var ar = items.OrderBy(x => x.CompanyName).ThenBy(x => x.LastName).ThenBy(x => x.FirstName).ToArray(); // var sb = new StringBuilder(); // // Connect Matches Add // foreach (var item in ar) // { // var jtItem = connect.MYOBCustomers.FirstOrDefault(x => x.Uid == item.UID); //unique idex should ensure only 1 // if (jtItem == null) // { // sb.AppendLine(item.CompanyName); // //throw new Exception($"Unable to find {item.CompanyName}"); // continue; // } // if (item.LastName == "Aikens") // { // Trace.WriteLine("hi"); // } // // locate contect using Name or FirstName, LastName depending on whether IsIndividual // var key = MakeKey(item); // var rec = connect.Contacts.SingleOrDefault(x => x.Name == key); // if (rec == null) // { // var alternateKey = MakeAlternateKey(item); // rec = connect.Contacts.SingleOrDefault(x => x.Name == alternateKey); // if (rec != null) // { // throw new ExceptionMYOBContactMissMatch( // $"Customer: CompanyName:{item.CompanyName}, LastName{item.LastName}, IsIndividual{item.IsIndividual} conflicts with Organisation {rec.Name}"); // } // } // // if not found then add it. // if (rec == null) // { // var newRec = new Contact // { // MYOBCustomer = jtItem, // IsIndividual = jtItem.IsIndividual, // Name = // jtItem.IsIndividual // ? HandyContactFunctions.FormattedLastAndFirstName( jtItem.LastName, jtItem.FirstName) // : jtItem.CompanyName, // Person = { FirstName = jtItem.FirstName, LastName = jtItem.LastName } // }; // if (item.Addresses != null) // { // var billAddress = item.Addresses.SingleOrDefault(x => x.Location == 1); // if (billAddress != null) // { // newRec.BillingAddress = HandyFunctions.MakeAddress(billAddress); // } // var shipAddress = item.Addresses.SingleOrDefault(x => x.Location == 2); // if (shipAddress != null) // { // newRec.ShippingAddress = HandyFunctions.MakeAddress(shipAddress); // } // } // HandyFunctions.Log("added {0}", newRec.Name); // connect.Contacts.Add(newRec); // } // else // { // // if found then link it // if (rec.MYOBCustomer == null || rec.MYOBCustomer.Id != jtItem.Id) // { // rec.MYOBCustomer = jtItem; // } // } // } // connect.SaveChanges(); // if (sb.Length > 0) // { // MessageBox.Show("Unable to locate " + sb); // } //} //private void DisconnectMismatches(b.JobTalkDbContext connect) //{ // foreach (var rec in // connect.Contacts.Where( // x => // x.MYOBCustomer != null && // !((x.MYOBCustomer.IsIndividual && // (x.MYOBCustomer.LastName + ", " + x.MYOBCustomer.FirstName) == x.Name) || // ((!x.MYOBCustomer.IsIndividual) && x.MYOBCustomer.CompanyName == x.Name)))) // { // rec.MYOBCustomer = null; // } // connect.SaveChanges(); //} private void RebuildMirrorTable(AMSDbContext connect, IProgress <int> progressCallback) { var ar = items.OrderBy(x => x.CompanyName).ThenBy(x => x.LastName).ThenBy(x => x.FirstName).ToArray(); AddNewUpdateExistingRecords(ar, connect, progressCallback); DeleteMissingMirrorRecords(connect, ar); }
private static void CopyItemFromMYOB(AMSDbContext connect, MYOBTaxCode jtItem, TaxCode item) { jtItem.Uid = item.UID; jtItem.URI = item.URI.ToString(); jtItem.Code = item.Code; jtItem.Description = item.Description; jtItem.Rate = item.Rate; jtItem.TypeCode = item.Type.ToString(); if (item.TaxCollectedAccount == null) { jtItem.TaxCollectedAccount = null; } else { jtItem.TaxCollectedAccount = connect.MYOBAccounts.SingleOrDefault(x => x.Uid == item.TaxCollectedAccount.UID); } if (item.TaxPaidAccount == null) { jtItem.TaxPaidAccount = null; } else { jtItem.TaxPaidAccount = connect.MYOBAccounts.SingleOrDefault(x => x.Uid == item.TaxPaidAccount.UID); } jtItem.RowVersion = item.RowVersion; }
public void SynchroniseItems(IProgress <int> progress) { using (var connect = new AMSDbContext(HandyFunctions.ConnectionString())) { RebuildMirrorTable(connect, progress); // RebuildMirrorLinks(connect); } }
//private void RebuildMirrorLinks(AMSDbContext connect, Supplier[] ar) //{ // DisconnectMismatches(connect); // // Connect Matches Add // foreach (var item in ar) // { // var jtItem = connect.MYOBSuppliers.Single(x => x.Uid == item.UID); //unique idex should ensure only 1 // var key = HandyContactFunctions.MakeKey(item.IsIndividual, item.LastName, item.FirstName,item.CompanyName); // var rec = connect.Contacts.SingleOrDefault(x => x.Name == key); // if (rec == null) // { // var alternateKey = HandyContactFunctions.MakeAlternateKey(item.IsIndividual, item.LastName, item.FirstName, item.CompanyName); // rec = connect.Contacts.SingleOrDefault(x => x.Name == alternateKey); // if (rec != null) // { // throw new ExceptionMYOBContactMissMatch( // $"Supplier: CompanyName:{item.CompanyName}, LastName{item.LastName}, IsIndividual{item.IsIndividual} conflicts with Organisation {rec.Name}"); // } // } // if (rec == null) // { // var newRec = new Contact // { // MYOBSupplier = jtItem, // Name = jtItem.IsIndividual ? HandyContactFunctions.FormattedLastAndFirstName( jtItem.LastName, jtItem.FirstName) : jtItem.CompanyName // }; // if (item.Addresses != null) // { // var billAddress = item.Addresses.SingleOrDefault(x => x.Location == 1); // if (billAddress != null) // { // newRec.BillingAddress = HandyFunctions.MakeAddress(billAddress); // } // var shipAddress = item.Addresses.SingleOrDefault(x => x.Location == 2); // if (shipAddress != null) // { // newRec.ShippingAddress = HandyFunctions.MakeAddress(shipAddress); // } // } // HandyFunctions.Log("added {0}", newRec.Name); // connect.Contacts.Add(newRec); // } // else // { // if (rec.MYOBSupplier == null || rec.MYOBSupplier.Id != rec.Id) // { // rec.MYOBSupplier = jtItem; // } // } // } // connect.SaveChanges(); //} //private static void DisconnectMismatches(AMSDbContext connect) //{ // foreach (var rec in // connect.Contacts.Where( // x => // x.MYOBSupplier != null && // !((x.MYOBSupplier.IsIndividual && (x.MYOBSupplier.LastName + ", " + x.MYOBSupplier.FirstName) == x.Name) || // ((!x.MYOBSupplier.IsIndividual) && x.MYOBSupplier.CompanyName == x.Name)))) // { // rec.MYOBSupplier = null; // } // connect.SaveChanges(); //} private static void RebuildMirrorTable( Supplier[] ar, AMSDbContext connect, IProgress <int> progressCallback) { AddNewUpdateExistingRecords(ar, connect, progressCallback); DeleteMissingMirrorRecords(connect, ar); }
public void SynchroniseItems(IProgress <int> progressCallback) { using (var connect = new AMSDbContext(HandyFunctions.ConnectionString())) { var ar = items.OrderBy(x => x.CompanyName).ThenBy(x => x.LastName).ThenBy(x => x.FirstName).ToArray(); RebuildMirrorTable(ar, connect, progressCallback); // RebuildMirrorLinks(connect, ar); } }
//private void RebuildMirrorLinks(AMSDbContext connect, TaxCode[] ar) //{ // // disconnect mismatches // foreach (var taxrate in connect.TaxRates.Where(x => x.MYOBTaxCode != null && x.Code != x.MYOBTaxCode.Code)) // { // taxrate.MYOBTaxCode = null; // } // connect.SaveChanges(); // // add missing TaxRates // foreach (var item in ar) // { // HandyFunctions.Log("checking {0}", item.Code); // var taxRate = connect.TaxRates.SingleOrDefault(x => x.Code == item.Code); // if (taxRate == null) // { // var jtItem = GetMatchingJTItemByUid(connect, item); // taxRate = new TaxRate { Code = jtItem.Code, Percentage = jtItem.Rate, MYOBTaxCode = jtItem }; // connect.TaxRates.Add(taxRate); // taxRate.MYOBTaxCode = jtItem; // } // else // { // if (taxRate.MYOBTaxCode == null) // { // var jtItem = GetMatchingJTItemByUid(connect, item); // taxRate.MYOBTaxCode = jtItem; // } // } // } // connect.SaveChanges(); //} private void RebuildMirrorTable(TaxCode[] ar, AMSDbContext connect, IProgress <int> progressCallback) { var info = ""; var stepNum = 1; var numSteps = ar.Count(); try { foreach (var item in ar) { if (item.Code == info) { throw new ExceptionDuplicateKey($"Taxcode {info}"); } info = item.Code; HandyFunctions.Log(info); var jtItem = GetMatchingJTItemByUid(connect, item); if (jtItem == null) { jtItem = new MYOBTaxCode(); connect.MYOBTaxCodes.Add(jtItem); } CopyItemFromMYOB(connect, jtItem, item); progressCallback.Report((100 * stepNum) / numSteps); stepNum++; } connect.SaveChanges(); // delete missing foreach (var taxcode in connect.MYOBTaxCodes) { var item = ar.SingleOrDefault(x => x.UID == taxcode.Uid); if (item == null) { //var codeString = item.Code; //foreach (var rate in connect.TaxRates.Where(x => x.MYOBTaxCode != null && x.MYOBTaxCode.Code == codeString)) //{ // rate.MYOBTaxCode = null; //} connect.MYOBTaxCodes.Remove(taxcode); } } connect.SaveChanges(); } catch (Exception ex) { ex.Data.Add("TaxCode", info); throw; } }
public void SynchroniseItems(IProgress <int> progressCallback) { var info = ""; using (var connect = new AMSDbContext(HandyFunctions.ConnectionString())) { var ar = items.OrderBy(x => x.Number).ToArray(); // RebuildMirrorTable(ar, info, connect,progressCallback); // RebuildMirrorLinks(connect, ar); } }
private static bool CheckMigrationVersionAndUpgradeIfNeeded() { try { //using (var db = new AMSDbContext()) //{ // var config = db.ConfigurationSettings.FirstOrDefault(); // if (config == null) // { Debug.Print("There is not config record");} // else // { Debug.Print(config.Name); } //} using (var db = new AMSDbContext()) { if (!db.Database.Exists()) { db.Database.Initialize(true); //db.Database.Create(); // new DatabaseInitializer().Seed(this); } var hasMetaData = true; try { var compatible = db.Database.CompatibleWithModel(true); } catch (Exception) { hasMetaData = false; } if (!hasMetaData) { return(RunMigrations(db)); } if (!db.Database.CompatibleWithModel(false)) { return(RunMigrations(db)); } return(true); } } catch (Exception ex) { var s = "Problem in MigrateIfNeeded /n/p" + ex; MessageBox.Show(s); return(false); } }
private static void DeleteMissingMirrorRecords(AMSDbContext connect, Customer[] ar) { foreach (var rec in connect.MYOBCustomers) { var oItem = ar.SingleOrDefault(x => x.UID == rec.Uid); if (oItem != null) { continue; } { //var key = rec.IsIndividual ? HandyContactFunctions.FormattedLastAndFirstName(rec.LastName, rec.FirstName) : rec.CompanyName; //foreach (var contact in connect.Contacts.Where(x => x.MYOBCustomer != null && x.Name == key)) //{ // contact.MYOBCustomer = null; // unlink //} connect.MYOBCustomers.Remove(rec); } } connect.SaveChanges(); }
private static void ModifyIfNeeded( AMSDbContext connect, MYOBCustomerAddress existingAddress, string propertyName, string oldValue, string newValue) { if (oldValue == null && newValue == null) { return; } if (oldValue == newValue) { return; } var entry = connect.Entry(existingAddress); entry.Property(propertyName).CurrentValue = newValue; entry.Property(propertyName).IsModified = true; }
private static void CopyRecordFromMyob(AMSDbContext connect, MYOBCustomer jtItem, Customer item) { jtItem.CompanyName = item.IsIndividual ? HandyContactFunctions.FormattedLastAndFirstName(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(); if (item.SellingDetails?.InvoiceDelivery != null) { jtItem.DocumentAction = (DocumentAction)item.SellingDetails.InvoiceDelivery; } SyncAddresses(connect, jtItem, item); }
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, MYOBCustomer jtMyobCustomer, CompanyFile myCompanyFile, CompanyFileCredentials myCredentials, CancellationToken ct) { var item = new Customer { IsActive = jtMyobCustomer.IsActive, CompanyName = jtMyobCustomer.CompanyName, FirstName = jtMyobCustomer.FirstName, LastName = jtMyobCustomer.LastName }; HandyFunctions.Log(item.ToJson2()); var task = _myService.InsertAsync(myCompanyFile, item, myCredentials, ct, ErrorLevel.WarningsAsErrors); await task; jtMyobCustomer.Uid = item.UID; connect.SaveChanges(); }
private static void AddNewUpdateExistingRecords( Customer[] ar, AMSDbContext connect, IProgress <int> progressCallback) { var sb = new StringBuilder(); var info = ""; var stepNum = 1; var numSteps = ar.Count(); foreach (var item in ar) { if (MakeKey(item) == info) { // skip this one //throw new ExceptionDuplicateKey($"Unable to import. Duplicate key exists in MYOB for: {info}"); sb.AppendLine(info); continue; } info = MakeKey(item); HandyFunctions.Log(info); var jtCustomer = connect.MYOBCustomers.SingleOrDefault(x => x.Uid == item.UID); //unique idex should ensure only 1 if (jtCustomer == null) { jtCustomer = new MYOBCustomer(); connect.MYOBCustomers.Add(jtCustomer); } CopyRecordFromMyob(connect, jtCustomer, item); progressCallback.Report((100 * stepNum) / numSteps); stepNum++; } connect.SaveChanges(); if (sb.Length > 0) { MessageBox.Show("Duplicate customers skipped /n" + sb); } }
private string RebuildMirrorTable(Account[] ar, string info, AMSDbContext connect, IProgress <int> progressCallback) { var stepNum = 0; var numSteps = ar.Count(); foreach (var item in ar) { if (item.DisplayID == info) { throw new ExceptionDuplicateKey($"DisplayID {info}"); } info = item.DisplayID; HandyFunctions.Log(info); var jtItem = GetMatchingJTItemByUid(connect, item); if (jtItem == null) { jtItem = new MYOBAccount(); connect.MYOBAccounts.Add(jtItem); } CopyItemFromMYOB(jtItem, item); progressCallback.Report((100 * stepNum) / numSteps); stepNum++; } connect.SaveChanges(); foreach (var acc in connect.MYOBAccounts) { var item = ar.SingleOrDefault(x => x.UID == acc.Uid); if (item == null) { connect.MYOBAccounts.Remove(acc); } } connect.SaveChanges(); return(info); }
private static void DeleteMissingMirrorRecords(AMSDbContext connect, Supplier[] ar) { foreach (var rec in connect.MYOBSuppliers) { var oItem = ar.SingleOrDefault(x => x.UID == rec.Uid); if (oItem == null) { var key = rec.IsIndividual ? rec.LastName : rec.CompanyName; //foreach ( // var sup in // connect.Contacts.Where( // x => // x.MYOBSupplier != null && x.MYOBSupplier.CompanyName == key || // x.MYOBSupplier.LastName == key)) //{ // sup.MYOBSupplier = null; // unlink //} connect.MYOBSuppliers.Remove(rec); } } connect.SaveChanges(); }
public void SynchroniseItems(IProgress <int> progressCallback) { var info = ""; try { // psuedo code // add item if it is missing. // update item if it needs updating // delete items that no longer match MYOB's UID var ar = items.OrderBy(x => x.DisplayID).ToArray(); using (var connect = new AMSDbContext(HandyFunctions.ConnectionString())) { info = RebuildMirrorTable(ar, info, connect, progressCallback); } } catch (Exception ex) { ex.Data.Add("ItemName", info); throw; } }
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(); }
public AMSDA() { dbContext = new AMSDbContext(); }
public MenuRepository(AMSDbContext dbcontext) : base(dbcontext) { }
private static void SyncAddresses(AMSDbContext connect, MYOBCustomer jtItem, Customer 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.Fax, tempAddress.Fax); ModifyIfNeeded(connect, existingAddress, "WebSite", existingAddress.WebSite, 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)) { continue; } var newAddress = new MYOBCustomerAddress(); HandyFunctions.PopulateMyobAddress(newAddress, address); jtItem.Addresses.Add(newAddress); newAddress.MyobCustomer = jtItem; } } } var deleteQueue = jtItem.Addresses.Where(x => x.TaggedToDelete).ToArray(); foreach (var a in deleteQueue) { var entry = connect.Entry(a); entry.State = EntityState.Deleted; } }
//private static void RebuildMirrorLinks(AMSDbContext connect, Item[] ar) //{ // DisconnectMismatches(connect); // // Connect Matches Add // foreach (var item in ar) // { // var rec = connect.Products.SingleOrDefault(x => x.Code == item.Number); // var jtItem = GetMatchingJTItemByUid(connect, item); // if (rec == null) // { // var newRec = new Product // { // Code = jtItem.Number, MYOBItem = jtItem, Description = jtItem.Description // }; // connect.Products.Add(newRec); // } // else // { // if (rec.MYOBItem == null) // { // rec.MYOBItem = jtItem; // } // } // } // connect.SaveChanges(); //} //private static void AddNewUpdateExistingMirrorRecords(Item[] ar, string info, AMSDbContext connect, IProgress<int> progressCallback) //{ // var stepNum = 1; // var numSteps = ar.Count(); // foreach (var item in ar) // { // if (item.Number == info) // { // throw new ExceptionDuplicateKey($"code {info}"); // } // info = item.Number; // HandyFunctions.Log(info); // var jtItem = GetMatchingJTItemByUid(connect, item); // if (jtItem == null) // { // jtItem = new MYOBItem(); // connect.MYOBItems.Add(jtItem); // } // CopyItemFromMYOB(jtItem, item); // progressCallback.Report((100 * stepNum) / numSteps); // stepNum++; // } // connect.SaveChanges(); //} private static MYOBItem GetMatchingJTItemByUid(AMSDbContext connect, Item item) { var jtItem = connect.MYOBItems.SingleOrDefault(x => x.Uid == item.UID); //unique idex should ensure only 1 return(jtItem); }
private MYOBTaxCode GetMatchingJTItemByUid(AMSDbContext connect, TaxCode taxcode) { return(connect.MYOBTaxCodes.SingleOrDefault(x => x.Uid == taxcode.UID)); }
public UserRepository(AMSDbContext dbcontext) : base(dbcontext) { }
private MYOBAccount GetMatchingJTItemByUid <T1>(AMSDbContext connect, T1 account) where T1 : BaseEntity { return(connect.MYOBAccounts.SingleOrDefault(x => x.Uid == account.UID)); }
public RoleRepository(AMSDbContext dbcontext) : base(dbcontext) { }
/**********************************************************************************************//** * @fn public RepositoryBase(FCDbContext dbContext) * * @brief 通过构造函数注入得到数据上下文对象实例 * * @author rxf * @date 2017/12/26 * * @param dbContext Context for the database. **************************************************************************************************/ public RepositoryBase(AMSDbContext dbContext) { _dbContext = dbContext; }