public QBXMLMsgsRs Transmit <T>(object txx, out T resp) { QBXML req = new QBXML(); req.ItemsElementName = new ItemsChoiceType103[] { ItemsChoiceType103.QBXMLMsgsRq }; req.Items = new object[] { new QBXMLMsgsRq() }; QBXMLMsgsRq r = req.Items[0] as QBXMLMsgsRq; r.onError = QBXMLMsgsRqOnError.stopOnError; r.Items = new object[] { txx }; MemoryStream ms = new MemoryStream(); XmlTextWriter w = new XmlTextWriter(ms, null); w.WriteProcessingInstruction("xml", "version=\"1.0\""); w.WriteProcessingInstruction("qbxml", "version=\"11.0\""); ser.Serialize(w, req, ns); ms.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(ms); string t = sr.ReadToEnd(); t = this.process.Transmit(t); QBXML ret = null; StringReader tr = new StringReader(t); ret = (QBXML)ser.Deserialize(tr); QBXMLMsgsRs rs = (ret.Items[0] as QBXMLMsgsRs); resp = (T)rs.Items[0]; return(rs); }
protected override QBXML OnSerialize() { QBXMLMsgsRq msgRqSet = new QBXMLMsgsRq(); msgRqSet.onError = OnError; List <IRqMsg> rqMsgs = new List <IRqMsg>(); //request order matters! foreach (var rq in _rqMsgs) { rqMsgs.Add(rq); } msgRqSet.Items = rqMsgs.ToArray(); QBXML qbxml = new QBXML() { Items = new[] { msgRqSet }, ItemsElementName = new[] { ItemsChoiceType99.QBXMLMsgsRq } }; return(qbxml); }
private void TransferJobs(XmlSerializer serializer, XmlSerializerNamespaces ns, QBXML qbxml, QBXMLMsgsRq qbMsgsRq) { MemoryStream ms; StreamReader sr; string xmlRequest; var custrq = new CustomerQueryRqType(); custrq.requestID = "1"; qbMsgsRq.Items = new object[1] { custrq }; qbxml.Items[0] = qbMsgsRq; ms = new MemoryStream(); serializer.Serialize(ms, qbxml, ns); ms.Seek(0, SeekOrigin.Begin); sr = new StreamReader(ms); xmlRequest = sr.ReadToEnd(); xmlRequest = xmlRequest.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\"?><?qbxml version=\"4.0\"?>"); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\CustQBXML.xml", xmlRequest); } _Response = _Rp.ProcessRequest(_Ticket, xmlRequest); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\Customers.xml", _Response); } QBXML rsXML = GetQbxml(serializer); if (rsXML?.Items?[0] is QBXMLMsgsRs) { QBXMLMsgsRs msgsrs = (QBXMLMsgsRs)rsXML.Items[0]; if (msgsrs?.Items?[0] is CustomerQueryRsType) { CustomerQueryRsType rs = (CustomerQueryRsType)msgsrs.Items[0]; if (rs.statusCode != "0") { MessageBox.Show(rs.statusMessage); } else { for (int i = 0; i < rs.CustomerRet.Length; i++) { CustomerRet cr = rs.CustomerRet[i]; if (cr.Sublevel == "0") { continue; //don't process the top level customers; we only want the job records } Match m = _JobRegEx.Match(cr?.FullName); if (!m.Success) { continue; //this isn't a job number we can use } string qbJobID = m.Groups[1].Value; string qbJobName = m.Groups[2].Value; using (ApexDataDataContext dc = new ApexDataDataContext(_SqlConnBuilder.ConnectionString)) { Job job = null; QBJob qbjob; qbjob = dc.QBJobs.Where(s => s.ApexCompany == _ApexTargetCompany && s.ApexJobID == qbJobID && s.QBListID != cr.ListID) .FirstOrDefault(); if (qbjob != null) { MessageBox.Show($"Job {qbJobID} from {qbJobName} has already been assigned to {qbjob.QBJobName} and cannot be transferred.", "Duplicate job"); continue; } qbjob = dc.QBJobs.Where(s => s.ApexCompany == _ApexTargetCompany && s.QBListID == cr.ListID).SingleOrDefault(); if (qbjob != null) { if (qbjob.ApexJobID != qbJobID.PadLeft(12)) //we need to delete and treat as new { dc.QBJobs.DeleteOnSubmit(qbjob); QBJob qbjob2 = new QBJob(); qbjob2.QBListID = cr?.ListID; qbjob2.QBJobName = cr?.FullName; qbjob2.ApexJobID = qbJobID.PadLeft(12); qbjob2.ApexCompany = _ApexTargetCompany; dc.QBJobs.InsertOnSubmit(qbjob2); } else { qbjob.QBJobName = cr?.FullName; } } else { qbjob = new QBJob(); qbjob.QBListID = cr?.ListID; qbjob.QBJobName = cr?.FullName; qbjob.ApexJobID = qbJobID.PadLeft(12); qbjob.ApexCompany = _ApexTargetCompany; dc.QBJobs.InsertOnSubmit(qbjob); } job = dc.Jobs.Where(s => s.Job1 == qbjob.ApexJobID.PadLeft(12)).SingleOrDefault(); if (job != null) { SetJobFields(cr, qbJobName, job); } else { job = new Job(); //Start the new Apex job job.Job1 = qbJobID.PadLeft(12); job.Act = "A"; job.Schedule = "STD"; job.TaxDefault = "Y"; job.TaxRate = 0; job.POMsg = String.Empty; SetJobFields(cr, qbJobName, job); dc.Jobs.InsertOnSubmit(job); } dc.SubmitChanges(); } } } } } }
private void TransferVendors(XmlSerializer serializer, XmlSerializerNamespaces ns, QBXML qbxml, QBXMLMsgsRq qbMsgsRq) { MemoryStream ms; StreamReader sr; string xmlRequest; var vendrq = new VendorQueryRqType(); vendrq.requestID = "1"; qbMsgsRq.Items[0] = vendrq; qbxml.Items[0] = qbMsgsRq; ms = new MemoryStream(); serializer.Serialize(ms, qbxml, ns); ms.Seek(0, SeekOrigin.Begin); sr = new StreamReader(ms); xmlRequest = sr.ReadToEnd(); xmlRequest = xmlRequest.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\"?><?qbxml version=\"4.0\"?>"); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\VendQBXML.xml", xmlRequest); } _Response = _Rp.ProcessRequest(_Ticket, xmlRequest); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\Vendors.xml", _Response); } QBXML rsXML = GetQbxml(serializer); if (rsXML?.Items?[0] is QBXMLMsgsRs) { QBXMLMsgsRs msgsrs = (QBXMLMsgsRs)rsXML.Items[0]; if (msgsrs?.Items?[0] is VendorQueryRsType) { VendorQueryRsType rs = (VendorQueryRsType)msgsrs.Items[0]; if (rs.statusCode != "0") { MessageBox.Show(rs.statusMessage); } else { for (int i = 0; i < rs.VendorRet.Length; i++) { VendorRet vr = rs.VendorRet[i]; using (ApexDataDataContext dc = new ApexDataDataContext(_SqlConnBuilder.ConnectionString)) { Vendor vendor = null; QBVendor qbvendor = dc.QBVendors .Where(s => s.ApexCompany == _ApexTargetCompany && s.QBListID == vr.ListID) .SingleOrDefault(); bool newRecord; if (qbvendor == null) //new vendor { newRecord = true; qbvendor = new QBVendor(); vendor = new Vendor(); string newVendorID = GetNextApexVendor(dc); //Set up the translation table qbvendor.QBListID = vr?.ListID; qbvendor.QBVendorName = vr?.Name; qbvendor.ApexVendorID = newVendorID.PadLeft(6); //pad it just in case we change the way we're numbering qbvendor.ApexCompany = _ApexTargetCompany; qbvendor.Terms = vr?.TermsRef?.FullName; //Start the new Apex vendor vendor.Vendor1 = newVendorID; vendor.AcctID = newVendorID; vendor.Name = LoadField(vr?.Name.ToUpper(), 25); } else { newRecord = false; qbvendor.Terms = vr?.TermsRef?.FullName; vendor = dc.Vendors.Where(s => s.Vendor1 == qbvendor.ApexVendorID).Single(); } vendor.Add1 = LoadField(vr?.VendorAddress?.Addr1, 25); vendor.Add2 = LoadField(vr?.VendorAddress?.Addr2, 25); vendor.City = LoadField(vr?.VendorAddress?.City, 15); vendor.State = LoadField(vr?.VendorAddress?.State, 4); vendor.Zip = LoadField(vr?.VendorAddress?.PostalCode, 15); vendor.EMail = LoadField(vr?.Email, 40); vendor.Attn = LoadField(vr?.Contact, 20); vendor.Phone = LoadField(vr?.Phone, 15); vendor.CompLevel = 0; vendor.Terms = LoadField(vr?.TermsRef?.FullName, 15); vendor.Company = _ApexTargetCompany; if (newRecord) { dc.QBVendors.InsertOnSubmit(qbvendor); dc.Vendors.InsertOnSubmit(vendor); } dc.SubmitChanges(); } } } } } }
private void TransferTerms(XmlSerializer serializer, XmlSerializerNamespaces ns, QBXML qbxml, QBXMLMsgsRq qbMsgsRq) { MemoryStream ms; StreamReader sr; string xmlRequest; var termsrq = new TermsQueryRqType(); termsrq.requestID = "1"; qbMsgsRq.Items[0] = termsrq; qbxml.Items[0] = qbMsgsRq; ms = new MemoryStream(); serializer.Serialize(ms, qbxml, ns); ms.Seek(0, SeekOrigin.Begin); sr = new StreamReader(ms); xmlRequest = sr.ReadToEnd(); xmlRequest = xmlRequest.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\"?><?qbxml version=\"4.0\"?>"); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\TermsQBXML.xml", xmlRequest); } _Response = _Rp.ProcessRequest(_Ticket, xmlRequest); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\Terms.xml", _Response); } QBXML rsXML = GetQbxml(serializer); if (rsXML?.Items?[0] is QBXMLMsgsRs) { QBXMLMsgsRs msgsrs = (QBXMLMsgsRs)rsXML.Items[0]; if (msgsrs?.Items?[0] is TermsQueryRsType) { TermsQueryRsType rs = (TermsQueryRsType)msgsrs.Items[0]; using (ApexDataDataContext dc = new ApexDataDataContext(_SqlConnBuilder.ConnectionString)) { foreach (var term in rs.Items) { if (term is StandardTermsRet) { StandardTermsRet qbTerm = (StandardTermsRet)term; if (!dc.VENDTERMs.Where(s => s.VendTerm1 == qbTerm.Name).Any()) { VENDTERM newTerm = new VENDTERM(); newTerm.VendTerm1 = qbTerm.Name; dc.VENDTERMs.InsertOnSubmit(newTerm); } } dc.SubmitChanges(); } } } } }
private void TransferAccounts(XmlSerializer serializer, XmlSerializerNamespaces ns, QBXML qbxml, QBXMLMsgsRq qbMsgsRq) { //This is only useful in DEBUGMODE to look at the accounts returned by QuickBooks in an XML File MemoryStream ms; StreamReader sr; string xmlRequest; var accountrq = new AccountQueryRqType(); accountrq.requestID = "1"; qbMsgsRq.Items[0] = accountrq; qbxml.Items[0] = qbMsgsRq; ms = new MemoryStream(); serializer.Serialize(ms, qbxml, ns); ms.Seek(0, SeekOrigin.Begin); sr = new StreamReader(ms); xmlRequest = sr.ReadToEnd(); xmlRequest = xmlRequest.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\"?><?qbxml version=\"4.0\"?>"); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\AccountQBXML.xml", xmlRequest); } _Response = _Rp.ProcessRequest(_Ticket, xmlRequest); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\Accounts.xml", _Response); } }
private void btnImport_Click(object sender, EventArgs e) { try { _Rp = new RequestProcessor2(); _Rp.OpenConnection("", "Apex Interface"); _Ticket = _Rp.BeginSession("", Interop.QBXMLRP2.QBFileMode.qbFileOpenDoNotCare); _QBCompanyName = Path.GetFileName(_Rp.GetCurrentCompanyFileName(_Ticket)); _ApexTargetCompany = GetApexTargetCompany(_QBCompanyName); if (_ApexTargetCompany == "*") //invalid company open { Application.Exit(); return; } lblApexCompany.Text = "Quickbooks Company: " + Path.GetFileName(_QBCompanyName); lblApexCompany.Visible = true; lblApexCompany.Refresh(); //For efficiency we are creating these items once and reusing them for each QuickBooks transfer XmlSerializer serializer = new XmlSerializer(typeof(QBXML)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); var qbxml = new QBXML(); qbxml.ItemsElementName = new ItemsChoiceType99[1] { ItemsChoiceType99.QBXMLMsgsRq }; qbxml.Items = new object[1]; var qbMsgsRq = new QBXMLMsgsRq(); qbMsgsRq.Items = new object[1]; qbMsgsRq.onError = QBXMLMsgsRqOnError.stopOnError; Cursor.Current = Cursors.WaitCursor; try { StatusLabel.Text = "Receiving jobs..."; TransferJobs(serializer, ns, qbxml, qbMsgsRq); StatusLabel.Text = "Receiving vendors..."; TransferVendors(serializer, ns, qbxml, qbMsgsRq); StatusLabel.Text = "Receiving terms..."; TransferTerms(serializer, ns, qbxml, qbMsgsRq); if (DEBUGMODE) { StatusLabel.Text = "Receiving accounts..."; TransferAccounts(serializer, ns, qbxml, qbMsgsRq); StatusLabel.Text = "Receiving classes..."; TransferClasses(serializer, ns, qbxml, qbMsgsRq); } } finally { StatusLabel.Text = ""; Cursor.Current = Cursors.Default; } MessageBox.Show("QuickBooks data has been imported", "Importing complete"); } catch (System.Runtime.InteropServices.COMException ex) { MessageBox.Show("COM Error Description = " + ex.Message, "COM error"); return; } finally { StatusLabel.Text = ""; if (_Ticket != null) { _Rp.EndSession(_Ticket); } if (_Rp != null) { _Rp.CloseConnection(); } }; }
private void ProcessInvoice(VendIvc invoice, ApexDataDataContext apexData) { PO po = apexData.POs.Where(s => s.Po1 == invoice.PO).SingleOrDefault(); //get the corresponding P/O if (po == null) { _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = "The invoice points to an invalid P/O!?" }); return; } if (po.Vendor == null) { _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = "There is no vendor on this P/O" }); return; } Job job = apexData.Jobs.Where(s => s.Job1 == po.Job).SingleOrDefault(); //get the job if (job == null) { _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = "There is no job on this P/O" }); return; } QBJob qbjob = apexData.QBJobs.Where(s => s.ApexCompany == _ApexTargetCompany && s.ApexJobID == po.Job).SingleOrDefault(); if (qbjob == null) { _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = "This P/O has an invalid QuickBooks job reference" }); return; } QBVendor qbvendor = apexData.QBVendors.Where(s => s.ApexCompany == _ApexTargetCompany && s.ApexVendorID == po.Vendor).SingleOrDefault(); if (qbvendor == null) { _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = "This P/O has an invalid QuickBooks vendor reference" }); return; } var qbxml = new QBXML(); qbxml.ItemsElementName = new ItemsChoiceType99[1] { ItemsChoiceType99.QBXMLMsgsRq }; var qbMsgsRq = new QBXMLMsgsRq(); qbMsgsRq.onError = QBXMLMsgsRqOnError.continueOnError; var billaddrq = new BillAddRqType(); billaddrq.requestID = "1"; TermsRef termsref = new TermsRef { FullName = po.VendorTerms }; string ApexGLRef = apexData.Costcodes .Where(s => s.Schedule == "STD" && s.CostCode1 == po.POLines.Select(l => l.CostCode).FirstOrDefault()) .Select(s => s.GL).FirstOrDefault(); if (String.IsNullOrEmpty(ApexGLRef)) { ApexGLRef = "M"; } string QBGLAcctFullName = GLAcctUtility.GLAcctList .Where(s => s.ApexCompany == _ApexTargetCompany && s.ApexGLRef == ApexGLRef) .Select(s => s.QBGLAcctFullName).SingleOrDefault(); AccountRef accountref = new AccountRef { FullName = QBGLAcctFullName }; AccountRef creditaccountref = new AccountRef { FullName = "Cash Discount on Payables" }; //Classes in QuickBooks equate to Divisions in Apex for this client ClassRef classref = new ClassRef { FullName = apexData.Divisions.Where(s => s.Company == po.Company && s.Division1 == po.Division).Select(s => s.Name).SingleOrDefault() }; CustomerRef customerref = new CustomerRef { ListID = qbjob.QBListID }; ExpenseLineAdd expenseline = new ExpenseLineAdd { AccountRef = accountref, Amount = invoice.IvcAmt?.ToString("F2"), CustomerRef = customerref, Memo = job.Job1 + " " + qbjob.QBJobName.Substring(0, qbjob.QBJobName.IndexOf(':')) }; if (classref.FullName != null) { expenseline.ClassRef = classref; } ExpenseLineAdd[] expenseLines; if ((invoice.Discount ?? 0) != 0) //Add an expense line for the discount amount if the discount is not zero { ExpenseLineAdd creditexpenseline = new ExpenseLineAdd { AccountRef = creditaccountref, Amount = (0 - invoice.Discount ?? 0).ToString("F2"), ClassRef = classref, Memo = job.Job1 + " " + qbjob.QBJobName.Substring(0, qbjob.QBJobName.IndexOf(':')) }; expenseLines = new ExpenseLineAdd[2]; expenseLines[0] = expenseline; expenseLines[1] = creditexpenseline; } else { expenseLines = new ExpenseLineAdd[1]; expenseLines[0] = expenseline; } VendorRef vendorref = new VendorRef { ListID = qbvendor.QBListID }; var billadd = new BillAdd { DueDate = invoice.PayDate?.ToString("yyyy-MM-dd"), Memo = "From Apex", RefNumber = invoice.Invoice, TermsRef = termsref, TxnDate = invoice.IvcDate?.ToString("yyyy-MM-dd"), ExpenseLineAdd = expenseLines, VendorRef = vendorref }; qbMsgsRq.Items = new object[1] { billaddrq }; qbxml.Items = new object[1] { qbMsgsRq }; billaddrq.BillAdd = billadd; XmlSerializer serializer = new XmlSerializer(typeof(QBXML)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); //Don't use a namespace in the XML for QuickBooks MemoryStream ms = new MemoryStream(); serializer.Serialize(ms, qbxml, ns); ms.Seek(0, SeekOrigin.Begin); var sr = new StreamReader(ms); string xmlRequest = sr.ReadToEnd(); xmlRequest = xmlRequest.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\"?><?qbxml version=\"4.0\"?>"); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\BillAddQBXML.xml", xmlRequest); } _Response = _Rp.ProcessRequest(_Ticket, xmlRequest); if (DEBUGMODE) { File.WriteAllText("c:\\QB\\BillAddResponse.xml", _Response); } QBXML rsXML = GetQbxml(serializer); string message = ((BillAddRsType)((QBXMLMsgsRs)rsXML?.Items?[0])?.Items?[0]).statusMessage; string statuscode = ((BillAddRsType)((QBXMLMsgsRs)rsXML?.Items?[0])?.Items?[0]).statusCode; _StatusLines.Add(new StatusLine { Invoice = invoice.Invoice, PO = invoice.PO.Trim(), Message = message, StatusCode = statuscode }); if (statuscode == "0") //Apex's part is done now that the invoice has been successfully sent to QuickBooks to be paid { QBInvoice qbIvc = new QBInvoice { Invoice = invoice.Invoice, PO = invoice.PO, SentDate = DateTime.Now }; apexData.QBInvoices.InsertOnSubmit(qbIvc); invoice.IvcStatus = "P"; apexData.SubmitChanges(); } }