private string GetWeight(string strOrderNo) { try { if (!string.IsNullOrEmpty(mage_session)) { salesOrderEntity soEntity = mservice.salesOrderInfo(mage_session, strOrderNo); return(soEntity.weight); } } catch { } return("1.00"); }
protected override string GetSalesMonth(DateTime exportDate, string filename) { //access Magento store API //Get the sales records for the requested month DateTime fromDate = new DateTime(exportDate.Year, exportDate.Month, 1); DateTime toDate = fromDate.AddMonths(1); string result = "\n" + filename + ": "; string tempDisplay = ProgressText; ProgressText += result + "Exporting..."; StringBuilder data = new StringBuilder(); string h2 = "Product ID\tCustomer ID\tQuantity\tDate\r\n"; //sales file second header line StringBuilder salesData = new StringBuilder(CommonHeader + h2); Mage_Api_Model_Server_V2_HandlerPortTypeClient Mclient = new Mage_Api_Model_Server_V2_HandlerPortTypeClient(); string MsessionId = ""; //---------------------SALES MONTH EXPORT---------------------------- //errors caught by calling function //login MsessionId = Mclient.login(m_apiUserName, m_apiKey); //create filter to get sales for this month only filters monthFilter = new filters(); monthFilter.complex_filter = new complexFilter[2]; monthFilter.complex_filter[0] = new complexFilter(); monthFilter.complex_filter[0].key = "created_at"; monthFilter.complex_filter[0].value = new associativeEntity(); monthFilter.complex_filter[0].value.key = "from"; monthFilter.complex_filter[0].value.value = fromDate.ToString("yyyy-MM-dd HH:mm:ss"); monthFilter.complex_filter[1] = new complexFilter(); monthFilter.complex_filter[1].key = "created_at"; monthFilter.complex_filter[1].value = new associativeEntity(); monthFilter.complex_filter[1].value.key = "to"; monthFilter.complex_filter[1].value.value = toDate.ToString("yyyy-MM-dd HH:mm:ss"); //get list of sales orders salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, monthFilter); if (sorders.Length > 0) { foreach (salesOrderEntity s in sorders) { string customerid = s.customer_id; if (s.customer_is_guest.Equals("1")) { customerid = s.customer_email; if (customerid == null || customerid.Length < 1) customerid = s.increment_id; } string date = s.created_at; //get list of items purchased on each sales order salesOrderEntity sinfo = Mclient.salesOrderInfo(MsessionId, s.increment_id); foreach (salesOrderItemEntity item in sinfo.items) { string productid = item.product_id; //TODO: Need to get parent_item_id here if it exists string quantity = item.qty_ordered; int len = quantity.IndexOf("."); if (len > 0) quantity = quantity.Substring(0, len); //remove fractional part //add line to sales data salesData.Append(productid + "\t" + customerid + "\t" + quantity + "\t" + date + "\r\n"); } } } //upload sales data ProgressText = tempDisplay + result + "Uploading..."; result += m_boostService.WriteTable(m_alias, filename, data); ProgressText = tempDisplay + result; return result; }
protected string ApiTest() { string result = "\n" + CatalogFilename + ": "; string tempDisplay = ProgressText; ProgressText += result + "Rows to export..."; StopWatch exportWatch = new StopWatch(true); #if MAGENTO_API_AVAILABLE #region Static Trevor if (static_proxy && m_alias.Equals("Trevor")) { MagentoService Mclient = new MagentoService(); string MsessionId = ""; //---------------------CATALOG EXPORT---------------------------- try { MsessionId = Mclient.login(m_apiUserName, m_apiKey); catalogProductEntity[] plist = Mclient.catalogProductList(MsessionId, null, ""); if (plist.Length < 1) throw new Exception("No products available"); //TODO: create catalog file header string type = ""; foreach (catalogProductEntity p in plist) { string pid = p.product_id; if (p.type.Equals("simple")) //only export combined items or else simple items with no parents { bool isChild = false; catalogProductLinkEntity[] plinks = Mclient.catalogProductLinkList(MsessionId, "grouped", pid, "id"); foreach (catalogProductLinkEntity pl in plinks) if (pl.type.Equals("configurable")) { isChild = true; break; } if (isChild) continue; } else type += p.type + " "; string pname = p.name; string patt1 = ""; bool first = true; foreach (string cid in p.category_ids) { if (first) first = false; else patt1 += ","; patt1 += cid; } catalogProductReturnEntity pinfo = Mclient.catalogProductInfo(MsessionId, pid, "", null, "id"); catalogProductReturnEntity pPriceInfo = Mclient.catalogProductGetSpecialPrice(MsessionId, pid, "", "id"); string patt2 = ""; string pprice = pPriceInfo.price; ; if ((pPriceInfo.special_from_date != null) && (pinfo.special_to_date != null)) { DateTime saleStart = DateTime.Parse(pPriceInfo.special_from_date); DateTime saleEnd = DateTime.Parse(pPriceInfo.special_to_date); DateTime now = DateTime.Now; if (now >= saleStart && now <= saleEnd) pprice = pPriceInfo.special_price; } string pfilter = ""; string plink = pinfo.url_key; string pimage = ""; string psku = pinfo.sku; catalogProductImageEntity pimageinfo = null; try { pimageinfo = Mclient.catalogProductAttributeMediaInfo(MsessionId, pid, "", "", "id"); } catch { } if (pimageinfo != null) { pimage = pimageinfo.url; } } } catch { } //---------------------SALES EXPORT---------------------------- try { //salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, null); salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, null); if (sorders.Length > 0) { //TODO: create header line for sales export foreach (salesOrderEntity s in sorders) { string customerid = s.customer_id; if (s.customer_is_guest.Equals("1")) { customerid = s.customer_email; if (customerid == null || customerid.Length < 1) customerid = s.increment_id; } string date = s.created_at; salesOrderEntity sinfo = Mclient.salesOrderInfo(MsessionId, s.increment_id); foreach (salesOrderItemEntity item in sinfo.items) { string productid = item.product_id; string quantity = item.qty_ordered; int len = quantity.IndexOf("."); if (len > 0) quantity = quantity.Substring(0, len); //remove fractional part //TODO: add line to sales data here } } //TODO: upload sales data } } catch { } } #endregion #endif #region Static GoStore Mage_Api_Model_Server_V2_HandlerPortTypeClient Mclient = new Mage_Api_Model_Server_V2_HandlerPortTypeClient(); string MsessionId = ""; //---------------------CATALOG EXPORT---------------------------- try { //login //MsessionId = Mclient.login(m_apiUserName, m_apiKey); MsessionId = Mclient.login("4Tell", "4tellsoftware"); //condomania //Get API calls available apiEntity[] resources = Mclient.resources(MsessionId); //resultObj = proxy.CallMethod("resources", sessionID); //Type t = resultObj.GetType(); //XmlSerializer xs = new XmlSerializer(t); //XElement resources = xs.SerializeAsXElement(resultObj); //TODO: check each CallMethod to make sure it is in this list... //Set product attributes to fetch catalogProductRequestAttributes prodAttributes = new catalogProductRequestAttributes(); string[] attributes = { "sku", "url_key", "price", "special_price", "special_from_date", "special_to_date", "parent_item_id" }; prodAttributes.attributes = attributes; //filters prodFilters = new filters(); //associativeEntity[] filterList = new associativeEntity[1]; //filterList[0].key = ""; //filterList[0].value = ""; //loop through all products StringBuilder data = new StringBuilder(CommonHeader + ProductRecord.Header()); catalogProductEntity[] plist; Mclient.catalogProductList(out plist, MsessionId, null, ""); string type = ""; int maxCid = 0; foreach (catalogProductEntity p in plist) { string pid = p.product_id; if (p.type.Equals("simple")) //only export combined items or else simple items with no parents { //bool isChild = false; //catalogProductLinkEntity[] plinks = Mclient.catalogProductLinkList(MsessionId, "grouped", pid, "id"); //foreach (catalogProductLinkEntity pl in plinks) // if (pl.type.Equals("configurable")) // { // isChild = true; // break; // } //if (isChild) continue; } else type += p.type + " "; string pname = p.name; string patt1 = ""; bool first = true; foreach (string cid in p.category_ids) { if (first) first = false; else patt1 += ","; patt1 += cid; int id = Convert.ToInt32(cid); if (id > maxCid) maxCid = id; } string patt2 = ""; catalogProductReturnEntity pinfo = Mclient.catalogProductInfo(MsessionId, pid, "", prodAttributes, "id"); string pprice = pinfo.price; ; if ((pinfo.special_from_date != null) && (pinfo.special_to_date != null)) { DateTime saleStart = DateTime.Parse(pinfo.special_from_date); DateTime saleEnd = DateTime.Parse(pinfo.special_to_date); DateTime now = DateTime.Now; if (now >= saleStart && now <= saleEnd) pprice = pinfo.special_price; } string pfilter = ""; string plink = pinfo.url_key; string pimage = ""; string psku = pinfo.sku; catalogProductImageEntity pimageinfo = null; try { pimageinfo = Mclient.catalogProductAttributeMediaInfo(MsessionId, pid, "", "", "id"); } catch { try { pimageinfo = Mclient.catalogProductAttributeMediaInfo(MsessionId, psku, "", "", "sku"); } catch { } } if (pimageinfo != null) { pimage = pimageinfo.url; } data.Append(pid + "\t" + pname + "\t" + patt1 + "\t" + patt2 + "\t" + pprice + "\t" + pfilter + "\t" + plink + "\t" + pimage + "\t" + psku + "\r\n"); } result += m_boostService.WriteTable(m_alias, CatalogFilename, data); //get cat info result += "\n" + Att1Filename + ": "; ProgressText = tempDisplay + result + "Exporting..."; catalogCategoryInfo cinfo; StringBuilder csb = new StringBuilder(CommonHeader + AttributeRecord.Header()); for (int cid = 1; cid <= maxCid; cid++) { try { cinfo = Mclient.catalogCategoryInfo(MsessionId, cid, "", null); csb.Append(cid.ToString() + "\t" + cinfo.name + "\r\n"); } catch { csb.Append(cid.ToString() + "\t" + cid.ToString() + "\r\n"); } } result += m_boostService.WriteTable(m_alias, Att1Filename, csb); ProgressText = tempDisplay + result; } catch { } //try //{ // //catalogCategoryTree ctree = Mclient.catalogCategoryTree(MsessionId, "", ""); // catalogCategoryTree ctree = Mclient.catalogCategoryTree(MsessionId, "0", ""); //} //catch { } //try //{ // //catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); // catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); //} //catch { } //try //{ // //catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); // catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); //} //catch { } //try //{ // //catalogCategoryInfo cinfo = Mclient.catalogCategoryInfo(MsessionId, 0, "CurrentView", null); // catalogCategoryInfo cinfo = Mclient.catalogCategoryInfo(MsessionId, 4, "", null); //} //catch { } //try //{ // //catalogProductAttributeSetEntity[] pasList = Mclient.catalogProductAttributeSetList(MsessionId); // //...this one works! // catalogProductAttributeSetEntity[] pasList = Mclient.catalogProductAttributeSetList(MsessionId); //} //catch { } //---------------------SALES EXPORT---------------------------- try { DateTime exportDate = DateTime.Now; //pass this date in string salesFileName = string.Format(SalesFilenameFormat, exportDate.ToString("yyyy-MM")); result += "\n" + salesFileName + ": "; StringBuilder salesData = new StringBuilder(CommonHeader + SalesRecord.Header()); //create filter to get sales for this month only string fromDate = string.Format("{0:0000}-{1:00}-01 00:00:00", exportDate.Year, exportDate.Month); string toDate = string.Format("{0:0000}-{1:00}-01 00:00:00", exportDate.Year, exportDate.Month + 1); filters monthFilter = new filters(); monthFilter.complex_filter = new complexFilter[2]; monthFilter.complex_filter[0] = new complexFilter(); monthFilter.complex_filter[0].key = "created_at"; monthFilter.complex_filter[0].value = new associativeEntity(); monthFilter.complex_filter[0].value.key = "from"; monthFilter.complex_filter[0].value.value = fromDate; monthFilter.complex_filter[1] = new complexFilter(); monthFilter.complex_filter[1].key = "created_at"; monthFilter.complex_filter[1].value = new associativeEntity(); monthFilter.complex_filter[1].value.key = "to"; monthFilter.complex_filter[1].value.value = toDate; //get list of sales orders salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, monthFilter); if (sorders.Length > 0) { //TODO: create header line for sales export foreach (salesOrderEntity s in sorders) { string customerid = s.customer_id; if (s.customer_is_guest.Equals("1")) { customerid = s.customer_email; if (customerid == null || customerid.Length < 1) customerid = s.increment_id; } string date = s.created_at; //get list of items purchased on each sales order salesOrderEntity sinfo = Mclient.salesOrderInfo(MsessionId, s.increment_id); foreach (salesOrderItemEntity item in sinfo.items) { string productid = item.product_id; string quantity = item.qty_ordered; int len = quantity.IndexOf("."); if (len > 0) quantity = quantity.Substring(0, len); //remove fractional part //add line to sales data salesData.Append(customerid + "\t" + productid + "\t" + quantity + "\t" + date + "\r\n"); } } //upload sales data result += m_boostService.WriteTable(m_alias, salesFileName, salesData); } } catch { } #endregion return result; }
protected void APITest() { //static test Mage_Api_Model_Server_V2_HandlerPortTypeClient Mclient = new Mage_Api_Model_Server_V2_HandlerPortTypeClient(); string MsessionId = ""; try { //login MsessionId = Mclient.login(m_apiUserName, m_apiKey); } catch { } try { catalogCategoryTree ctree = Mclient.catalogCategoryTree(MsessionId, "0", ""); } catch { } try { catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); } catch { } try { catalogCategoryEntityNoChildren[] clist = Mclient.catalogCategoryLevel(MsessionId, "", "", ""); } catch { } try { catalogCategoryInfo cinfo = Mclient.catalogCategoryInfo(MsessionId, 4, "", null); } catch { } try { catalogProductAttributeSetEntity[] pasList = Mclient.catalogProductAttributeSetList(MsessionId); } catch { } //---------------------SALES EXPORT---------------------------- try { //salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, null); salesOrderEntity[] sorders = Mclient.salesOrderList(MsessionId, null); if (sorders.Length > 0) { //TODO: create header line for sales export foreach (salesOrderEntity s in sorders) { string customerid = s.customer_id; if (s.customer_is_guest.Equals("1")) { customerid = s.customer_email; if (customerid == null || customerid.Length < 1) customerid = s.increment_id; } string date = s.created_at; salesOrderEntity sinfo = Mclient.salesOrderInfo(MsessionId, s.increment_id); foreach (salesOrderItemEntity item in sinfo.items) { string productid = item.product_id; string quantity = item.qty_ordered; int len = quantity.IndexOf("."); if (len > 0) quantity = quantity.Substring(0, len); //remove fractional part //TODO: add line to sales data here } } //TODO: upload sales data } } catch { } //dynamic test DynamicProxy proxy = GetSoapProxy(m_wsdlUrl, "Mage_Api_Model_Server_V2_HandlerPortType"); if (proxy == null) throw new Exception("Unable to create SOAP proxy for Magento"); //login to get a session id object resultObj = proxy.CallMethod("login", m_apiUserName, m_apiKey); string sessionID = resultObj.ToString(); Type t; XmlSerializer xs; try { //catalog_category.info catalogCategoryInfo resultObj = proxy.CallMethod("catalog_category.info", sessionID); XElement categories = XmlSerializerExtension.SerializeAsXElement(resultObj); //t = resultObj.GetType(); //xs = new XmlSerializer(t); //XElement categories = xs.SerializeAsXElement(resultObj); foreach (XElement category in categories.Elements("catalogCategoryEntity")) { string test = category.Element("").Value; } } catch (Exception ex) { string errMsg = "Error extracting catalog: " + ex.Message; if (ex.InnerException != null) errMsg += "\nInner Exception" + ex.InnerException.Message; } try { //catalog_category.tree catalogCategoryTree resultObj = proxy.CallMethod("catalog_category.tree", sessionID); XElement categoryTree = XmlSerializerExtension.SerializeAsXElement(resultObj); //t = resultObj.GetType(); //xs = new XmlSerializer(t); //XElement categoryTree = xs.SerializeAsXElement(resultObj); foreach (XElement category in categoryTree.Elements("catalogCategoryEntity")) { string test = category.Element("").Value; } } catch (Exception ex) { string errMsg = "Error extracting catalog: " + ex.Message; if (ex.InnerException != null) errMsg += "\nInner Exception" + ex.InnerException.Message; } try { //product_attribute_set.list catalogProductAttributeSetList resultObj = proxy.CallMethod("product_attribute_set.list", sessionID); XElement attributes = XmlSerializerExtension.SerializeAsXElement(resultObj); //t = resultObj.GetType(); //xs = new XmlSerializer(t); //XElement attributes = xs.SerializeAsXElement(resultObj); foreach (XElement attribute in attributes.Elements("catalogProductAttributeSetEntity")) { string test = attribute.Element("").Value; } } catch (Exception ex) { string errMsg = "Error extracting catalog: " + ex.Message; if (ex.InnerException != null) errMsg += "\nInner Exception" + ex.InnerException.Message; } //---------------------SALES EXPORT---------------------------- try { //sales_order.list salesOrderList resultObj = proxy.CallMethod("sales_order.list", sessionID); XElement orders = XmlSerializerExtension.SerializeAsXElement(resultObj); //t = resultObj.GetType(); //xs = new XmlSerializer(t); //XElement orders = xs.SerializeAsXElement(resultObj); foreach (XElement order in orders.Elements("salesOrderEntity")) { string orderID = Client.GetValue(order, "order_id"); string date = Client.GetValue(order, "created_at"); //Get order details sales_order.info salesOrderInfo resultObj = proxy.CallMethod("sales_order.info", sessionID, orderID); XElement orderInfo = XmlSerializerExtension.SerializeAsXElement(resultObj, "salesOrderInfo"); //t = resultObj.GetType(); //if (!t.Name.Equals("salesOrderInfo")) // throw new Exception("Illegal response from magento service"); //xs = new XmlSerializer(t); //XElement orderInfo = xs.SerializeAsXElement(resultObj); //salesOrderEntity orderInfo = (salesOrderEntity)resultObj; string customerID = Client.GetValue(orderInfo, "customer_id"); //orderInfo.customer_id; bool isGuest = Client.GetValue(orderInfo, "customer_is_guest").Equals("1");//orderInfo.customer_is_guest.Equals("1"); if (isGuest) customerID = Client.GetValue(orderInfo, "customer_email"); //orderInfo.customer_email; //foreach (salesOrderItemEntity item in orderInfo.items) foreach (XElement items in orderInfo.Elements("items")) foreach (XElement x in items.Elements("salesOrderItemEntity")) { string productID = Client.GetValue(x, "product_id"); //item.product_id; string q = Client.GetValue(x, "qty_ordered"); int len = q.IndexOf("."); if (len > 0) q = q.Substring(0, len); int quantity = Convert.ToInt32(q); //item.qty_ordered); //TODO: add line to sales data here } } //TODO: upload sales data (check that string has some data in it) } catch (Exception ex) { string errMsg = "Error extracting catalog: " + ex.Message; if (ex.InnerException != null) errMsg += "\nInner Exception" + ex.InnerException.Message; } }