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 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(); } }