This commit is contained in:
2022-06-30 19:40:39 +00:00
parent d4c429490a
commit 9a2d8994c2

View File

@@ -643,46 +643,100 @@ namespace AyaNovaQBI
/// <returns></returns>
public static async Task<bool> 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<bool>() == false)
bool present = true;
foreach (IntegrationItem m in QBIntegration.Items)
{
//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");
present = true;
switch (m.AType)
{
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<Integration>();
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<Integration>();
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;
}
}
public static async Task IntegrationLog(string logLine) => await PostAsync("integration/log", Newtonsoft.Json.JsonConvert.SerializeObject(new NameIdItem { Id = QBIntegration.Id, Name = logLine }));