From 126bcedafb6f1f324ee1a426b208691d280a8bb0 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 6 Jul 2022 21:22:07 +0000 Subject: [PATCH] --- AyaNovaQBI/Map.cs | 8 +- AyaNovaQBI/util.cs | 351 +++++++++++++++++++++++++++++++++------------ 2 files changed, 265 insertions(+), 94 deletions(-) diff --git a/AyaNovaQBI/Map.cs b/AyaNovaQBI/Map.cs index ec48fd4..fb6f45a 100644 --- a/AyaNovaQBI/Map.cs +++ b/AyaNovaQBI/Map.cs @@ -1059,9 +1059,7 @@ namespace AyaNovaQBI break; } SaveIntegration = true; - - } - + } } switch (_Type) { @@ -1077,8 +1075,6 @@ namespace AyaNovaQBI break; } - - #endregion AyaGrid } else @@ -1098,7 +1094,7 @@ namespace AyaNovaQBI { case AyaType.Customer: - await util.RefreshQBCustomerFromAyaNova(im); + await util.RefreshQBCustomerFromAyaNovaAsync(im); break; case AyaType.Vendor: await util.RefreshQVendorFromAyaNova(im); diff --git a/AyaNovaQBI/util.cs b/AyaNovaQBI/util.cs index b9a5369..64aa9fc 100644 --- a/AyaNovaQBI/util.cs +++ b/AyaNovaQBI/util.cs @@ -2901,6 +2901,93 @@ namespace AyaNovaQBI } + private static async Task GetQBVendorEditSequenceAsync(string vendorid) + { + + string strEditSequence = ""; + + //Connect to QB and fill + // IY: Create the session manager object using QBFC + QBSessionManager sessionManager = new QBSessionManager(); + + // IY: We want to know if we begun a session so we can end it if an + // error happens + bool booSessionBegun = false; + + try + { + // IY: Get the RequestMsgSet based on the correct QB Version + IMsgSetRequest requestSet = getLatestMsgSetRequest(sessionManager); + + // IY: Initialize the message set request object + requestSet.Attributes.OnError = ENRqOnError.roeStop; + + // IY: Add the request to the message set request object + IVendorQuery cq = requestSet.AppendVendorQueryRq(); + cq.IncludeRetElementList.Add("EditSequence"); + cq.ORVendorListQuery.ListIDList.Add(vendorid); + + + sessionManager.OpenConnection2("", "AyaNova QBI", ENConnectionType.ctLocalQBDLaunchUI); + sessionManager.BeginSession("", ENOpenMode.omDontCare); + booSessionBegun = true; + + // IY: Do the request and get the response message set object + IMsgSetResponse responseSet = sessionManager.DoRequests(requestSet); + + // Uncomment the following to view and save the request and response XML + //string requestXML = requestSet.ToXMLString(); + //MessageBox.Show(requestXML); + // SaveXML(requestXML); + //string responseXML = responseSet.ToXMLString(); + //MessageBox.Show(responseXML); + // SaveXML(responseXML); + + IResponse response = responseSet.ResponseList.GetAt(0); + //nonzero indicates an error this is unrecoverable + //so throw an exception + + //Changed: 21-June-2006 nonzero status codes 500 and higher are serious errors, less than 500 could just mean no matching items in list + if (response.StatusCode > 499) + { + throw new ApplicationException(response.StatusMessage + " Code: " + response.StatusCode); + + } + + //Added: 21-June-2006 outer if to avoid crash on no match response (code 1) + if (response.StatusCode == 0) + { + IVendorRetList cl = response.Detail as IVendorRetList; + if (!(cl.Count == 0)) + { + IVendorRet clitem = cl.GetAt(0); + if (clitem != null) + strEditSequence = clitem.EditSequence.GetValue(); + } + + } + // IY: Close the session and connection with QuickBooks + sessionManager.EndSession(); + booSessionBegun = false; + sessionManager.CloseConnection(); + + + } + catch (Exception ex) + { + await IntegrationLog("GetVendorEditSequence: Failed with exception:" + ex.Message); + if (booSessionBegun) + { + sessionManager.EndSession(); + sessionManager.CloseConnection(); + } + throw; + + } + + return strEditSequence; + } + #endregion quickbooks Vendors @@ -4155,30 +4242,18 @@ namespace AyaNovaQBI #region Export / refresh to QuickBooks - public static async Task RefreshQBClientFromAyaNova(List objectIDList) - { - foreach (long id in objectIDList) - { - try - { - var r = await GetAsync($"customer/{id}"); - var c = r.ObjectResponse["data"].ToObject(); - await RefreshQBClientFromAyaNova(c); - } - catch { }; - } - - } /// /// Refresh the indicated AyaNova client /// to it's linked QuickBooks customer record /// - public static async Task RefreshQBClientFromAyaNova(Customer c) + public static async Task RefreshQBCustomerFromAyaNovaAsync(IntegrationItem im) { - IntegrationItem im = util.QBIntegration.Items.FirstOrDefault(z => z.ObjectId == c.Id && z.AType == AyaType.Customer); + if (im == null) return;//this object is not linked + var r = await GetAsync($"customer/{im.ObjectId}"); + var c = r.ObjectResponse["data"].ToObject(); + if (c == null) return; - if (im == null) return;//this client is not linked DataRow dr = _dtQBClients.Rows.Find(im.IntegrationItemId); if (dr == null) return; //QBListID not found in client list? @@ -4269,9 +4344,7 @@ namespace AyaNovaQBI ca.BillAddress.Addr2.SetValue(T(41, sad[0].TrimEnd())); if (sad.GetLength(0) > 1) ca.BillAddress.Addr3.SetValue(T(41, sad[1].TrimEnd())); - //if(sad.GetLength(0)>2) - // ca.BillAddress.Addr3.SetValue(T(21,sad[2].TrimEnd())); - + if (QVersion > 1.1 && sad.GetLength(0) > 3)//4th address line is 2 or newer ca.BillAddress.Addr4.SetValue(T(41, sad[2].TrimEnd())); @@ -4283,22 +4356,6 @@ namespace AyaNovaQBI //QBFC7 - unifies county and province to "State" ca.BillAddress.State.SetValue(T(21, c.PostRegion)); - //switch(QCountry) - //{ - // case "CA": - // ca.BillAddress.Province.SetValue(T(21,c.MailToAddress.StateProv)); - // break; - // case "UK": - // ca.BillAddress.County.SetValue(T(21,c.MailToAddress.StateProv)); - // break; - // default: - // ca.BillAddress.State.SetValue(T(21,c.MailToAddress.StateProv)); - // break; - - //} - - - bool bHasShipAddress = false; //Delivery address @@ -4317,9 +4374,6 @@ namespace AyaNovaQBI if (sad.GetLength(0) > 1) ca.ShipAddress.Addr3.SetValue(T(41, sad[1].TrimEnd())); - //if(sad.GetLength(0)>2) - // ca.ShipAddress.Addr3.SetValue(T(21,sad[2].TrimEnd())); - if (QVersion > 1.1 && sad.GetLength(0) > 3)//4th address line is 2 or newer ca.ShipAddress.Addr4.SetValue(T(41, sad[2].TrimEnd())); @@ -4332,20 +4386,6 @@ namespace AyaNovaQBI //QBFC7 - unifies county and province to "State" ca.ShipAddress.State.SetValue(T(21, c.Region)); - //switch(QCountry) - //{ - // case "CA": - // ca.ShipAddress.Province.SetValue(T(21,c.GoToAddress.StateProv)); - // break; - // case "UK": - // ca.ShipAddress.County.SetValue(T(21,c.GoToAddress.StateProv)); - // break; - // default: - // ca.ShipAddress.State.SetValue(T(21,c.GoToAddress.StateProv)); - // break; - - //} - //Added: 18-Nov-2006 CASE 95 @@ -4413,8 +4453,6 @@ namespace AyaNovaQBI //Link im.IntegrationItemName = sName; im.LastSync = DateTime.Now; - await SaveIntegrationObject(); - //BI = (Integration)QBI.Save(); } catch (Exception ex) { @@ -4437,39 +4475,6 @@ namespace AyaNovaQBI - //public static async Task ImportAyaCustomer(List objectIDList) - //{ - // ArrayList alErrors = new ArrayList(); - // foreach (long ayid in objectIDList) - // { - // try - // { - - // await ImportAyaCustomer(ayid, alErrors); - - // } - // catch { }; - // } - - // if (alErrors.Count != 0) - // { - - - // StringBuilder sb = new StringBuilder(); - // sb.Append("Export completed with some errors:\r\n\r\n"); - // foreach (object o in alErrors) - // { - // sb.Append((string)o); - // sb.Append("\r\n************\r\n"); - - // } - - // CopyableMessageBox cb = new CopyableMessageBox(sb.ToString()); - // cb.ShowDialog(); - // cb.Dispose(); - - // } - //} /// @@ -4741,6 +4746,176 @@ namespace AyaNovaQBI + + + //############ NEW VENDOR REFRESH, copied and altered from csutomer refresh so any issues may be related to that + /// + ///Update QuickBooks Vendor record from AyaNova + /// + public static async Task RefreshQBVendorFromAyaNovaAsync(IntegrationItem im) + { + if (im == null) return;//this object is not linked + var r = await GetAsync($"vendor/{im.ObjectId}"); + var c = r.ObjectResponse["data"].ToObject(); + if (c == null) return; + + + DataRow dr = _dtQBVendors.Rows.Find(im.IntegrationItemId); + if (dr == null) return; //QBListID not found in client list? + + string strEditSequence = await GetQBVendorEditSequenceAsync(im.IntegrationItemId); + if (string.IsNullOrEmpty(strEditSequence)) + { + MessageBox.Show("RefreshQBVendorFromAyaNova -> Error: unable to fetch edit sequence from QuickBooks. No changes made."); + return; + } + + string sName = c.Name; + if (sName.Length > 41) + sName = sName.Substring(0, 41); + + + //Connect to QB and fill + // IY: Create the session manager object using QBFC + QBSessionManager sessionManager = new QBSessionManager(); + + // IY: We want to know if we begun a session so we can end it if an + // error happens + bool booSessionBegun = false; + + try + { + + + //Import seems safe... + + // IY: Get the RequestMsgSet based on the correct QB Version + IMsgSetRequest requestSet = getLatestMsgSetRequest(sessionManager); + + // IY: Initialize the message set request object + requestSet.Attributes.OnError = ENRqOnError.roeStop; + + // IY: Add the request to the message set request object + + IVendorMod ca = requestSet.AppendVendorModRq(); + + //Set field value for ListID + ca.ListID.SetValue(im.IntegrationItemId); + // ca.ParentRef.ListID.SetValue(im.ForeignID); + ca.EditSequence.SetValue(strEditSequence); + + dr["FullName"] = sName; + dr["MailAddress"] = new Address(); + dr["StreetAddress"] = new Address(); + dr["Phone"] = ""; + dr["Fax"] = ""; + dr["AltPhone"] = ""; + dr["Email"] = ""; + dr["Contact"] = ""; + //dr["Created"] = DateTime.MinValue;//flag indicating fresh record incomplete + dr["Modified"] = DateTime.Now;//ditto + dr["Account"] = ""; + + ca.Name.SetValue(sName); + ca.Contact.SetValue(T(41, c.Contact)); + //ca.LastName.SetValue(T(25, c.Contact)); + + if (c.Phone1 != null) + ca.Phone.SetValue(T(21, c.Phone1)); + else + ca.Phone.SetValue(""); + ca.Fax.SetValue(T(21, c.Phone2)); + ca.AltPhone.SetValue(T(21, c.Phone3)); + ca.Email.SetValue(T(99, c.EmailAddress)); + + + + + if (!string.IsNullOrWhiteSpace(c.PostAddress)) + { + string[] sad = c.PostAddress.Replace("\r\n", "\n").Split('\n'); + if (sad.GetLength(0) > 0) + ca.VendorAddress.Addr1.SetValue(T(21, sad[0].TrimEnd())); + if (sad.GetLength(0) > 1) + ca.VendorAddress.Addr2.SetValue(T(21, sad[1].TrimEnd())); + if (sad.GetLength(0) > 2) + ca.VendorAddress.Addr3.SetValue(T(21, sad[2].TrimEnd())); + if (QVersion > 1.1 && sad.GetLength(0) > 3)//4th address line is 2 or newer + ca.VendorAddress.Addr4.SetValue(T(21, sad[3].TrimEnd())); + + } + ca.VendorAddress.City.SetValue(T(31, c.PostCity)); + ca.VendorAddress.Country.SetValue(T(31, c.PostCountry)); + ca.VendorAddress.PostalCode.SetValue(T(13, c.PostCode)); + ca.VendorAddress.State.SetValue(T(21, c.PostRegion)); + + ca.AccountNumber.SetValue(T(99, c.AccountNumber)); + + + //This is intended to be called after already sucessfully connected + //to get version info so no special safety checks here + sessionManager.OpenConnection2("", "AyaNova QBI", ENConnectionType.ctLocalQBDLaunchUI); + sessionManager.BeginSession("", ENOpenMode.omDontCare); + booSessionBegun = true; + + + IMsgSetResponse responseSet = sessionManager.DoRequests(requestSet); + + // //// Uncomment the following to view and save the request and response XML + // string requestXML = requestSet.ToXMLString(); + // MessageBox.Show(requestXML); + // ////SaveXML(requestXML); + // string responseXML = responseSet.ToXMLString(); + // MessageBox.Show(responseXML); + ////// SaveXML(responseXML); + + IResponse response = responseSet.ResponseList.GetAt(0); + //nonzero indicates an error this is unrecoverable + //so throw an exception + + if (response.StatusCode != 0) + { + throw new ApplicationException(response.StatusMessage + " Code: " + response.StatusCode); + + } + + IVendorRet cr = response.Detail as IVendorRet; + requestSet.ClearRequests(); + //---------------- + + + // Close the session and connection with QuickBooks + sessionManager.EndSession(); + booSessionBegun = false; + sessionManager.CloseConnection(); + + + + //Link + im.IntegrationItemName = sName; + im.LastSync = DateTime.Now; + } + catch (Exception ex) + { + if (booSessionBegun) + { + sessionManager.EndSession(); + sessionManager.CloseConnection(); + } + + + //crack the exception in case it's a generic dataportal one + //and it is if it's got an inner exception of any kind + if (ex.InnerException != null) ex = ex.InnerException; + MessageBox.Show("RefreshQBVendorFromAyaNova: QuickBooks won't allow refresh of " + sName + "\r\n" + + "Due to the following error:\r\n" + ex.Message); + + } + } + //###### new vendor refresh + + + /// /// Import the indicated Vendor /// to QuickBooks Vendor record