diff --git a/AyaNovaQBI/Map.cs b/AyaNovaQBI/Map.cs index 36609b0..499a132 100644 --- a/AyaNovaQBI/Map.cs +++ b/AyaNovaQBI/Map.cs @@ -67,7 +67,7 @@ namespace AyaNovaQBI } - private void linkSelectedItemsToolStripMenuItem_Click(object sender, EventArgs e) + private async void linkSelectedItemsToolStripMenuItem_Click(object sender, EventArgs e) { bool IsAyaGrid = false; if (gridAya.SelectedRows.Count == 0 && gridQB.SelectedRows.Count == 0) return; @@ -89,76 +89,71 @@ namespace AyaNovaQBI s.Dispose(); var selectedAyaNovaIds = new List(); StringBuilder selectedAyaNovaNames = new StringBuilder(); - + foreach (DataGridViewRow r in gridAya.SelectedRows) { selectedAyaNovaNames.AppendLine(r.Cells[0].Value.ToString()); selectedAyaNovaIds.Add((long)r.Cells[1].Value); } - + //################################# //LINKING LinkAyaObjectToQBConfirm d = new LinkAyaObjectToQBConfirm(); - d.QBItem = QBItemName; + d.QBItem = QBItemName; d.AyaItems = selectedAyaNovaNames.ToString(); - if (d.ShowDialog() != DialogResult.OK) + if (d.ShowDialog() != DialogResult.OK) return; - + ////ok, link away... - //foreach (object o in moveData.Rows) - //{ - // //qb listid - // string dropID = rowdrop.Cells["ID"].Value.ToString(); - // string dropName = rowdrop.Cells["Name"].Value.ToString(); - // IntegrationMap m = null; - // //Is AyaNova object already mapped? - // if (Util.QBI.Maps.Contains(((AyaNameID)o).ID)) - // { - // //Get the mapping - // m = Util.QBI.Maps[((AyaNameID)o).ID]; - - // //Is it already linked to the selected qb object? - - // //Yes so do nothing and continue on to the next object - // if (m.ForeignID == dropID) - // continue; - // else - // { - - // //No, AyaNova object was mapped elsewhere, prompt user if this is ok - // if (MessageBox.Show( - // "AyaNova object: " + ((AyaNameID)o).Name + "\r\n" + - // "Is already linked to QuickBooks object: " + m.Name + "\r\n" + - // "Do you really want to change the link to the QuickBooks object: " + dropName + "\r\n", - // "Change link?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) - // == DialogResult.No) continue; - - // } - - // //If we're here it's because the object is already mapped - // //but the users has signified they want to change the map to another object so... - // m.ForeignID = dropID; - // m.Name = rowdrop.Cells["Name"].Value.ToString(); - // m.LastSync = System.DateTime.Now; - - // } - // else - // { - // //not already present, so add it, easy peasy... - // m = Util.QBI.Maps.Add(Util.QBI); - // m.RootObjectID = ((AyaNameID)o).ID; - // m.RootObjectType = _Type; - // m.ForeignID = dropID; - // m.Name = dropName; - // m.LastSync = System.DateTime.Now; - // } - //} + foreach (DataGridViewRow r in gridAya.SelectedRows) + { + string AyaName = r.Cells[0].Value.ToString(); + long AyaId = (long)r.Cells[1].Value; - //Util.QBI = (Integration)Util.QBI.Save(); + + //Is AyaNova object already mapped? + IntegrationItem m = util.QBIntegration.Items.FirstOrDefault(z => z.Id == AyaId && z.AType == _Type); + if (m != null) + { + + + //Is it already linked to the selected qb object? + + //Yes so do nothing and continue on to the next object + if (m.IntegrationItemId == QBItemId) + continue; + else + { + + //No, AyaNova object was mapped elsewhere, prompt user if this is ok + if (MessageBox.Show( + $"AyaNova object: {AyaName}\r\nIs already linked to QuickBooks object: {m.IntegrationItemName}\r\nDo you really want to change the link to the QuickBooks object: {QBItemName}\r\n", + "Change link?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) + == DialogResult.No) continue; + + } + + //If we're here it's because the object is already mapped + //but the users has signified they want to change the map to another object so... + m.IntegrationItemId = QBItemId; + m.IntegrationItemName = QBItemName; + m.LastSync = System.DateTime.Now; + + } + else + { + //not already present, so add it, easy peasy... + m = new IntegrationItem { AType = _Type, IntegrationItemName = QBItemName, IntegrationItemId = QBItemId, LastSync = System.DateTime.Now, ObjectId = AyaId }; + util.QBIntegration.Items.Add(m); + } + } + + + await util.SaveIntegrationObject(); Initialize(); //################################# diff --git a/AyaNovaQBI/util.cs b/AyaNovaQBI/util.cs index 887243a..bdaf1a0 100644 --- a/AyaNovaQBI/util.cs +++ b/AyaNovaQBI/util.cs @@ -498,7 +498,7 @@ namespace AyaNovaQBI Cancel = 2 } - + /* @@ -1119,7 +1119,7 @@ namespace AyaNovaQBI "for invoices. If you do not use Invoice templates or are not sure what they are\r\n" + "select < Use default > from the list below.\r\n\r\n" + "This setting is required."; - s3b.QBInvoiceTemplates = QBInvoiceTemplates; + s3b.QBInvoiceTemplates = QBInvoiceTemplates; s3b.SelectedQBInvoiceTemplate = QDat.QBInvoiceTemplate; if (s3b.ShowDialog() == DialogResult.Cancel) @@ -1317,7 +1317,19 @@ namespace AyaNovaQBI } #endregion validate user settings - + #region Integration object persistance + public static async Task SaveIntegrationObject() + { + if (QDat.IsDirty) + { + await IntegrationLog("ValidateSettings: QBI settings modified by user, saving now"); + QBIntegration.IntegrationData = Newtonsoft.Json.JsonConvert.SerializeObject(QDat); + } + ApiResponse r = await PutAsync("integration", Newtonsoft.Json.JsonConvert.SerializeObject(QBIntegration)); + QBIntegration.Concurrency = r.ObjectResponse["data"]["concurrency"].Value(); + QDat.IsDirty = false; + } + #endregion integration object persistance #region PFC QB side ///