public XElement GenerateModRq(bool?ClearExpenseLines = null) { XElement xElement = new XElement(nameof(Bill) + "Mod"); xElement.Add(TxnID?.ToQBXML(nameof(TxnID))); xElement.Add(TimeCreated?.ToQBXML(nameof(TimeCreated))); xElement.Add(TimeModified?.ToQBXML(nameof(TimeModified))); xElement.Add(EditSequence?.ToQBXML(nameof(EditSequence))); xElement.Add(TxnNumber?.ToQBXML(nameof(TxnNumber))); xElement.Add(VendorRef?.ToQBXML(nameof(VendorRef))); xElement.Add(VendorAddress?.ToQBXML(nameof(VendorAddress))); xElement.Add(APAccountRef?.ToQBXML(nameof(APAccountRef))); xElement.Add(TxnDate?.ToQBXML(nameof(TxnDate))); xElement.Add(DueDate?.ToQBXML(nameof(DueDate))); xElement.Add(AmountDue?.ToQBXML(nameof(AmountDue))); xElement.Add(CurrencyRef?.ToQBXML(nameof(CurrencyRef))); xElement.Add(ExchangeRate?.ToQBXML(nameof(ExchangeRate))); xElement.Add(AmountDueInHomeCurrency?.ToQBXML(nameof(AmountDueInHomeCurrency))); xElement.Add(RefNumber?.ToQBXML(nameof(RefNumber))); xElement.Add(TermsRef?.ToQBXML(nameof(TermsRef))); xElement.Add(Memo?.ToQBXML(nameof(Memo))); xElement.Add(IsTaxIncluded?.ToQBXML(nameof(IsTaxIncluded))); xElement.Add(SalesTaxCodeRef?.ToQBXML(nameof(SalesTaxCodeRef))); xElement.Add(IsPaid?.ToQBXML(nameof(IsPaid))); xElement.Add(ExternalGUID?.ToQBXML(nameof(ExternalGUID))); xElement.Add(LinkedTxnList?.ToQBXML(nameof(LinkedTxnList))); xElement.Add(ExpenseLineList?.ToQBXML(nameof(ExpenseLineList))); xElement.Add(ItemLineList?.ToQBXML(nameof(ItemLineList))); xElement.Add(OpenAmount?.ToQBXML(nameof(OpenAmount))); xElement.Add(DataExtList?.ToQBXML(nameof(DataExtList))); return(xElement); }
private static void GenerateRequisition(RQRequestSelection filter, List <RQRequestLineOwned> lines) { RQRequisitionEntry graph = PXGraph.CreateInstance <RQRequisitionEntry>(); RQRequisition requisition = (RQRequisition)graph.Document.Cache.CreateInstance(); graph.Document.Insert(requisition); requisition.ShipDestType = null; bool isCustomerSet = true; bool isVendorSet = true; bool isShipToSet = true; int? shipContactID = null; int? shipAddressID = null; var vendors = new HashSet <VendorRef>(); foreach (RQRequestLine line in lines) { PXResult <RQRequest, RQRequestClass> e = (PXResult <RQRequest, RQRequestClass>) PXSelectJoin <RQRequest, InnerJoin <RQRequestClass, On <RQRequestClass.reqClassID, Equal <RQRequest.reqClassID> > >, Where <RQRequest.orderNbr, Equal <Required <RQRequest.orderNbr> > > > .Select(graph, line.OrderNbr); RQRequest req = e; RQRequestClass reqclass = e; requisition = PXCache <RQRequisition> .CreateCopy(graph.Document.Current); if (reqclass.CustomerRequest == true && isCustomerSet) { if (requisition.CustomerID == null) { requisition.CustomerID = req.EmployeeID; requisition.CustomerLocationID = req.LocationID; } else if (requisition.CustomerID != req.EmployeeID || requisition.CustomerLocationID != req.LocationID) { isCustomerSet = false; } } else { isCustomerSet = false; } if (isShipToSet) { if (shipContactID == null && shipAddressID == null) { requisition.ShipDestType = req.ShipDestType; requisition.ShipToBAccountID = req.ShipToBAccountID; requisition.ShipToLocationID = req.ShipToLocationID; shipContactID = req.ShipContactID; shipAddressID = req.ShipAddressID; } else if (requisition.ShipDestType != req.ShipDestType || requisition.ShipToBAccountID != req.ShipToBAccountID || requisition.ShipToLocationID != req.ShipToLocationID) { isShipToSet = false; } } if (line.VendorID != null && line.VendorLocationID != null) { VendorRef vendor = new VendorRef() { VendorID = line.VendorID.Value, LocationID = line.VendorLocationID.Value }; vendors.Add(vendor); if (isVendorSet) { if (requisition.VendorID == null) { requisition.VendorID = line.VendorID; requisition.VendorLocationID = line.VendorLocationID; } else if (requisition.VendorID != line.VendorID || requisition.VendorLocationID != line.VendorLocationID) { isVendorSet = false; } } } else { isVendorSet = false; } if (!isCustomerSet) { requisition.CustomerID = null; requisition.CustomerLocationID = null; } if (!isVendorSet) { requisition.VendorID = null; requisition.VendorLocationID = null; requisition.RemitAddressID = null; requisition.RemitContactID = null; } else if (requisition.VendorID == req.VendorID && requisition.VendorLocationID == req.VendorLocationID) { requisition.RemitAddressID = req.RemitAddressID; requisition.RemitContactID = req.RemitContactID; } if (!isShipToSet) { requisition.ShipDestType = PX.Objects.PO.POShippingDestination.CompanyLocation; graph.Document.Cache.SetDefaultExt <RQRequisition.shipToBAccountID>(requisition); } graph.Document.Update(requisition); if (line.OpenQty > 0) { if (!graph.Lines.Cache.IsDirty && req.CuryID != requisition.CuryID) { requisition = PXCache <RQRequisition> .CreateCopy(graph.Document.Current); requisition.CuryID = req.CuryID; graph.Document.Update(requisition); } graph.InsertRequestLine(line, line.OpenQty.GetValueOrDefault(), filter.AddExists == true); } } if (isShipToSet) { foreach (PXResult <POAddress, POContact> res in PXSelectJoin <POAddress, CrossJoin <POContact>, Where <POAddress.addressID, Equal <Required <RQRequisition.shipAddressID> >, And <POContact.contactID, Equal <Required <RQRequisition.shipContactID> > > > > .Select(graph, shipAddressID, shipContactID)) { AddressAttribute.CopyRecord <RQRequisition.shipAddressID>(graph.Document.Cache, graph.Document.Current, (POAddress)res, true); AddressAttribute.CopyRecord <RQRequisition.shipContactID>(graph.Document.Cache, graph.Document.Current, (POContact)res, true); } } if (requisition.VendorID == null && vendors.Count > 0) { foreach (var vendor in vendors) { RQBiddingVendor bid = PXCache <RQBiddingVendor> .CreateCopy(graph.Vendors.Insert()); bid.VendorID = vendor.VendorID; bid.VendorLocationID = vendor.LocationID; graph.Vendors.Update(bid); } } if (graph.Lines.Cache.IsDirty) { graph.Save.Press(); throw new PXRedirectRequiredException(graph, string.Format(Messages.RequisitionCreated, graph.Document.Current.ReqNbr)); } for (int i = 0; i < lines.Count; i++) { PXProcessing <RQRequestLine> .SetInfo(i, PXMessages.LocalizeFormatNoPrefixNLA(Messages.RequisitionCreated, graph.Document.Current.ReqNbr)); } }
public string toXml() { StringBuilder toXML = new StringBuilder(); toXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); toXML.Append("<?qbxml version=\"13.0\" ?>"); toXML.Append("<QBXML>"); toXML.Append("<QBXMLMsgsRq onError=\"stopOnError\">"); toXML.Append("<BillAddRq>"); toXML.Append("<BillAdd>"); if (VendorRef != null) { toXML.Append(VendorRef.toXMLVendorRef()); } if (VendorAddress != null) { toXML.Append(VendorAddress.toXmlRef()); } if (APAccountRef != null) { toXML.Append(APAccountRef.toXmlRefBill()); } if (txnDate != null) { string DateString = txnDate.ToString(); DateTime dt = Convert.ToDateTime(DateString); toXML.Append("<TxnDate>" + dt.ToString("yyyy-MM-dd") + "</TxnDate>"); } if (DueDate != null) { string DateString = DueDate.ToString(); DateTime dt = Convert.ToDateTime(DateString); toXML.Append("<DueDate>" + dt.ToString("yyyy-MM-dd") + "</DueDate>"); } toXML.Append("<RefNumber>" + RefNumber + "</RefNumber>"); if (TermsRef != null) { toXML.Append(TermsRef.toXmlRef()); } if (Memo != string.Empty) { toXML.Append("<Memo>" + Memo + "</Memo>"); } //if (IsTaxIncluded != null) //{ // xml += " <IsTaxIncluded >" + IsTaxIncluded.ToString() + "</IsTaxIncluded>"; //} //if (SalesTaxCodeRef != null) //{ // xml += SalesTaxCodeRef.toXmlRef(); //} if (ExchangeRate != null) { System.Globalization.CultureInfo myInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"); string val = ExchangeRate.ToString(); double value = Double.Parse(val, myInfo); toXML.Append("<ExchangeRate>" + value.ToString("0.00", myInfo) + "</ExchangeRate>"); } if (ExternalGUID != string.Empty) { toXML.Append(" <ExternalGUID >" + ExternalGUID + "</ExternalGUID>"); } foreach (BillExpenseLine line in expenseLines) { toXML.Append(line.toXml()); } foreach (BillItemLine line in itemLines) { toXML.Append(line.toXml()); } toXML.Append("</BillAdd>"); toXML.Append("</BillAddRq>"); toXML.Append("</QBXMLMsgsRq>"); toXML.Append("</QBXML>"); return(toXML.ToString()); }
public string toXmlMod() { string xml = string.Empty; xml += "<?xml version=\"1.0\" ?>"; xml += "<?qbxml version=\"13.0\" ?>"; xml += "<QBXML>"; xml += Environment.NewLine + "<QBXMLMsgsRq onError=\"stopOnError\">"; xml += Environment.NewLine + "<BillModRq requestID=\"10002\">"; xml += Environment.NewLine + "<BillMod>"; xml += Environment.NewLine + "<TxnID>" + TxnID + "</TxnID>"; xml += Environment.NewLine + "<EditSequence>" + EditSequence + "</EditSequence>"; if (VendorRef != null) { xml += VendorRef.toXMLVendorRef(); } if (APAccountRef != null) { xml += APAccountRef.toXmlRef(); } if (txnDate != null) { string DateString = txnDate.ToString(); DateTime dt = Convert.ToDateTime(DateString); xml += Environment.NewLine + "<TxnDate>" + dt.ToString("yyyy-MM-dd") + "</TxnDate>"; } if (DueDate != null) { string DateString = DueDate.ToString(); DateTime dt = Convert.ToDateTime(DateString); xml += Environment.NewLine + "<DueDate>" + dt.ToString("yyyy-MM-dd") + "</DueDate>"; } xml += Environment.NewLine + "<RefNumber>" + RefNumber + "</RefNumber>"; if (Memo != string.Empty) { xml += Environment.NewLine + "<Memo>" + Memo + "</Memo>"; } if (ExchangeRate != null) { System.Globalization.CultureInfo myInfo = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"); string val = ExchangeRate.ToString(); Double value = Double.Parse(val, myInfo); xml += Environment.NewLine + "<ExchangeRate>" + value + "</ExchangeRate>"; } foreach (BillExpenseLine line in expenseLines) { xml += Environment.NewLine + line.toXmlMod(); } foreach (BillItemLine line in itemLines) { xml += Environment.NewLine + line.toXmlMod(); } xml += Environment.NewLine + "</BillMod>"; xml += Environment.NewLine + "</BillModRq>"; xml += Environment.NewLine + "</QBXMLMsgsRq>"; xml += Environment.NewLine + "</QBXML>"; return(xml); }
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(); } }