From 9a2d8994c23312a5b7cc708a05203318c4d4eedc Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Thu, 30 Jun 2022 19:40:39 +0000 Subject: [PATCH] --- AyaNovaQBI/util.cs | 116 +++++++++++++++++++++++++++++++++------------ 1 file changed, 85 insertions(+), 31 deletions(-) diff --git a/AyaNovaQBI/util.cs b/AyaNovaQBI/util.cs index 7dd176d..676a5a8 100644 --- a/AyaNovaQBI/util.cs +++ b/AyaNovaQBI/util.cs @@ -643,45 +643,99 @@ namespace AyaNovaQBI /// public static async Task ValidateMap(StringBuilder initErrors) { - ApiResponse r = null; - try - { - r = await GetAsync($"integration/exists/{QBI_INTEGRATION_ID}"); + //Missing links table: + DataTable dtTemp = new DataTable(); + dtTemp.Columns.Add("MAPID", typeof(Guid)); + dtTemp.Columns.Add("Name", typeof(string)); - if (r.ObjectResponse["data"].Value() == false) + bool present = true; + foreach (IntegrationItem m in QBIntegration.Items) + { + present = true; + + switch (m.AType) { - //doesn't exist, need to create it now - QBIntegration = new Integration(); - QBIntegration.IntegrationAppId = QBI_INTEGRATION_ID; - QBIntegration.Active = true; - QBIntegration.Name = "QBI - QuickBooks Desktop integration"; - r = await PostAsync("integration", Newtonsoft.Json.JsonConvert.SerializeObject(QBIntegration)); - QBIntegration.Id = IdFromResponse(r); - await IntegrationLog("AyaNova QBI Integration installed to AyaNova"); + case AyaType.Customer: + present = QBClients.Rows.Contains(m.IntegrationItemId); + break; + case AyaType.Vendor: + present = QBVendors.Rows.Contains(m.IntegrationItemId); + break; + case AyaType.ServiceRate: + case AyaType.TravelRate: + case AyaType.Part: + present = QBItems.Rows.Contains(m.IntegrationItemId); + break; + } + + if (!present) + dtTemp.Rows.Add(new object[] { m.Id, m.AType.ToString() + ": " + m.IntegrationItemName }); + + } + + if (dtTemp.Rows.Count > 0) + { + if (dtTemp.Rows.Count == QBIntegration.Items.Count) + { + //None of the items mapped match offer to remove them all + #region Nothing matches + await IntegrationLog("PFC: No integration maps match qb database objects!"); + + DialogResult dr = MessageBox.Show("None of the mapped items in AyaNova were found in the \r\n" + + "Currently open QuickBooks database.\r\n" + + "It's possible you have the wrong database open.\r\n\r\n" + + "Do you want to remove all mappings from AyaNova?", "", MessageBoxButtons.YesNoCancel, + MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3); + + if (dr == DialogResult.Yes) + { + dr = MessageBox.Show("If you select YES all mappings will be removed from AyaNova.", "", MessageBoxButtons.YesNoCancel, + MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3); + if (dr == DialogResult.Yes) + { + await IntegrationLog("PFC: User opted to remove all mappings after double warning."); + QBIntegration.Items.Clear(); + //foreach (DataRow row in dtTemp.Rows) + //{ + // QBIntegration.Items.Remove(row["MAPID"].ToString()); + //} + //TODO: POST BACK TO SERVER + //Exists, fetch it check if active then we're done here + ApiResponse r = await PostAsync($"integration/{QBI_INTEGRATION_ID}", Newtonsoft.Json.JsonConvert.SerializeObject(QBIntegration)); + QBIntegration = r.ObjectResponse["data"].ToObject(); + + + return false; + } + + } + #endregion + + } else { - //Exists, fetch it check if active then we're done here - r = await GetAsync($"integration/{QBI_INTEGRATION_ID}"); + //some items match so iterate them and offer to delete one by one + + await IntegrationLog("PFC: Some integration maps do not match qb database objects"); + foreach (DataRow row in dtTemp.Rows) + { + + DialogResult dr = MessageBox.Show("Linked object: " + row["Name"].ToString() + "\r\n" + + "Is missing or set Inactive in QuickBooks.\r\n\r\nRemove it's link from AyaNova?", "", MessageBoxButtons.YesNoCancel, + MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button3); + if (dr == DialogResult.Cancel) return false; + if (dr == DialogResult.Yes) + QBIntegration.Items.Remove(QBIntegration.Items.Where(z => z.IntegrationItemId == row["MAPID"].ToString()).First()); + } + + ApiResponse r = await PostAsync($"integration/{QBI_INTEGRATION_ID}", Newtonsoft.Json.JsonConvert.SerializeObject(QBIntegration)); QBIntegration = r.ObjectResponse["data"].ToObject(); - if (!QBIntegration.Active) - { - initErrors.AppendLine("QBI Integration is currently deactivated and can not be used\r\nThis setting can be changed in AyaNova in the Administration section -> Integrations -> QuickBooks Desktop integration record\r\nSet to active and save to enable QBI"); - return false; - } } - - return true; } - catch (Exception ex) - { - initErrors.AppendLine("Error fetching QBI Integration object"); - initErrors.AppendLine(ex.Message); - initErrors.AppendLine(r.CompactResponse); - return false; + return true; - } } @@ -2170,7 +2224,7 @@ namespace AyaNovaQBI DataRow dr = _dtQBVendors.Rows.Add( new object[] -{ + { clitem.ListID.GetValue(), clitem.Name.GetValue(), ProcessAddress(clitem.VendorAddress), @@ -2183,7 +2237,7 @@ namespace AyaNovaQBI clitem.TimeCreated.GetValue(), clitem.TimeModified.GetValue(), ProcessQBString(clitem.AccountNumber) -}); + }); } } // for loop } // if