public XDocument GetInvoice(int shopperID,int orderID) { shopper = rep.GetShopper(shopperID); order = rep.GetShopperOrder(shopper.id,orderID); requestID = rep.GetQuickBooksOrder(order.id).RequestID; var lineItems = rep.GetInventoryItems(order.cartID); foreach(var item in lineItems) { var shopInventory = rep.GetShopInventoryItems(shopper.clientID).Where(i => i.Name == item.InventoryName); var exists = shopInventory.Any(); if (!exists) { itemsExist = false; missingInventoryItem = item.InventoryName; } item.InventoryName = shopInventory.SingleOrDefault().QuickbooksInventoryID != null ? shopInventory.SingleOrDefault().QuickbooksInventory.FullName : item.InventoryName; } if (itemsExist) { doc = new XDocument( new XDeclaration("1.0", "utf-8", null), new XProcessingInstruction("qbxml", "version=\"6.0\""), new XElement("QBXML", new XElement("QBXMLMsgsRq", new XAttribute("onError", "stopOnError"), new XElement("InvoiceAddRq", new XAttribute("requestID", requestID), new XElement("InvoiceAdd", new XElement("CustomerRef", new XElement("FullName",Name)), new XElement("TxnDate",String.Format("{0:yyyy-MM-dd}",DateTime.Now)), new XElement("BillAddress", new XElement("Addr1", FirstName + " " + LastName), new XElement("Addr2", BillAddress1), new XElement("Addr3", BillAddress2), new XElement("City", BillCity), new XElement("State", BillState), new XElement("PostalCode", BillPostalCode), new XElement("Country", BillCountry)), new XElement("ShipAddress", new XElement("Addr1", S_firstname + " " + S_lastname ), new XElement("Addr2", ShipAddress1), new XElement("Addr3", ShipAddress2), new XElement("City", ShipCity), new XElement("State", ShipState), new XElement("PostalCode", ShipPostalCode), new XElement("Country", ShipCountry)), new XElement("ItemSalesTaxRef") ) ) ) ) ); var customerNode = doc.LastNode.Document.Descendants().Where(n => n.Name == "CustomerRef").SingleOrDefault(); var salesTaxNode = doc.LastNode.Document.Descendants().Where(n => n.Name == "ItemSalesTaxRef").SingleOrDefault(); if(order.orderTax != null && order.orderTax > 0) { salesTaxNode.Add(new XElement("FullName",TaxFullName)); } foreach (var item in lineItems) { (lastLineItemNode == null ? salesTaxNode : lastLineItemNode).AddAfterSelf( new XElement("InvoiceLineAdd", new XElement("ItemRef", new XElement("FullName",item.InventoryName)), new XElement("Desc",item.Desc), new XElement("Quantity",item.Quantity == null ? 1 : item.Quantity), new XElement("Rate", ((decimal)item.Rate).ToString("N2")), new XElement("Amount", ((decimal)item.Amount).ToString("N2")) ) ); } if(ClassRef != null && ClassRef != String.Empty) { customerNode.AddAfterSelf( new XElement("ClassRef", new XElement("FullName", ClassRef) ) ); } if (TemplateRef != null && TemplateRef != String.Empty) { var element = doc.LastNode.Document.Descendants().Where(n => n.Name == "ClassRef").SingleOrDefault() == null ? customerNode : doc.LastNode.Document.Descendants().Where(n => n.Name == "ClassRef").SingleOrDefault(); element.AddAfterSelf( new XElement("TemplateRef", new XElement("FullName", TemplateRef) ) ); } lastLineItemNode.AddAfterSelf( new XElement("InvoiceLineAdd", new XElement("ItemRef", new XElement("FullName",ShippingFullName)), new XElement("Desc","Shipping"), new XElement("Amount", Shipping) ) ); } else //Query Quickbooks to see if the inventory Item Exists { doc = Inventory.ItemQuery(requestID,missingInventoryItem); } return doc; }
public XDocument GetInvoice(int shopperID, int orderID) { shopper = rep.GetShopper(shopperID); order = rep.GetShopperOrder(shopper.id, orderID); requestID = rep.GetQuickBooksOrder(order.id).RequestID; var lineItems = rep.GetInventoryItems(order.cartID); foreach (var item in lineItems) { var shopInventory = rep.GetShopInventoryItems(shopper.clientID).Where(i => i.Name == item.InventoryName); var exists = shopInventory.Any(); if (!exists) { itemsExist = false; missingInventoryItem = item.InventoryName; } item.InventoryName = shopInventory.SingleOrDefault().QuickbooksInventoryID != null?shopInventory.SingleOrDefault().QuickbooksInventory.FullName : item.InventoryName; } if (itemsExist) { doc = new XDocument( new XDeclaration("1.0", "utf-8", null), new XProcessingInstruction("qbxml", "version=\"6.0\""), new XElement("QBXML", new XElement("QBXMLMsgsRq", new XAttribute("onError", "stopOnError"), new XElement("InvoiceAddRq", new XAttribute("requestID", requestID), new XElement("InvoiceAdd", new XElement("CustomerRef", new XElement("FullName", Name)), new XElement("TxnDate", String.Format("{0:yyyy-MM-dd}", DateTime.Now)), new XElement("BillAddress", new XElement("Addr1", FirstName + " " + LastName), new XElement("Addr2", BillAddress1), new XElement("Addr3", BillAddress2), new XElement("City", BillCity), new XElement("State", BillState), new XElement("PostalCode", BillPostalCode), new XElement("Country", BillCountry)), new XElement("ShipAddress", new XElement("Addr1", S_firstname + " " + S_lastname), new XElement("Addr2", ShipAddress1), new XElement("Addr3", ShipAddress2), new XElement("City", ShipCity), new XElement("State", ShipState), new XElement("PostalCode", ShipPostalCode), new XElement("Country", ShipCountry)), new XElement("ItemSalesTaxRef") ) ) ) ) ); var customerNode = doc.LastNode.Document.Descendants().Where(n => n.Name == "CustomerRef").SingleOrDefault(); var salesTaxNode = doc.LastNode.Document.Descendants().Where(n => n.Name == "ItemSalesTaxRef").SingleOrDefault(); if (order.orderTax != null && order.orderTax > 0) { salesTaxNode.Add(new XElement("FullName", TaxFullName)); } foreach (var item in lineItems) { (lastLineItemNode == null ? salesTaxNode : lastLineItemNode).AddAfterSelf( new XElement("InvoiceLineAdd", new XElement("ItemRef", new XElement("FullName", item.InventoryName)), new XElement("Desc", item.Desc), new XElement("Quantity", item.Quantity == null ? 1 : item.Quantity), new XElement("Rate", ((decimal)item.Rate).ToString("N2")), new XElement("Amount", ((decimal)item.Amount).ToString("N2")) ) ); } if (ClassRef != null && ClassRef != String.Empty) { customerNode.AddAfterSelf( new XElement("ClassRef", new XElement("FullName", ClassRef) ) ); } if (TemplateRef != null && TemplateRef != String.Empty) { var element = doc.LastNode.Document.Descendants().Where(n => n.Name == "ClassRef").SingleOrDefault() == null ? customerNode : doc.LastNode.Document.Descendants().Where(n => n.Name == "ClassRef").SingleOrDefault(); element.AddAfterSelf( new XElement("TemplateRef", new XElement("FullName", TemplateRef) ) ); } lastLineItemNode.AddAfterSelf( new XElement("InvoiceLineAdd", new XElement("ItemRef", new XElement("FullName", ShippingFullName)), new XElement("Desc", "Shipping"), new XElement("Amount", Shipping) ) ); } else //Query Quickbooks to see if the inventory Item Exists { doc = Inventory.ItemQuery(requestID, missingInventoryItem); } return(doc); }