Artikel abgleichen

public void addProducts(string ItemID, UpdateTypes UpdateType)
{
string sSQL = „“;

if (ItemID.Length == 0)
if (UpdateType == UpdateTypes.Full)
sSQL = „Select ID from Artikel where Rakuten = 1 and Mandant = ‚“ + exSettings.eXODAClientname + „‚“;
else
sSQL = „Select ID from Artikel where Rakuten = 1 and RakutenProduktUpdate = 1 and Mandant = ‚“ + exSettings.eXODAClientname + „‚“;
else
sSQL = „Select ID from Artikel where ID = “ + ItemID + “ and Mandant = ‚“ + exSettings.eXODAClientname + „‚“;

exSettings.HS4Dataset.Open(HS4Software.DatabaseType.ExistingConnection, „“, sSQL, „classrakuten_product“, „ID“, true, false);

eXODALib.ItemLib.Items oItems = new eXODALib.ItemLib.Items();
oItems.Initialize(exSettings);
eXODALib.ItemLib.Item oItem = new eXODALib.ItemLib.Item();
eXODALib.HTML.HtmlToText oHTML = new eXODALib.HTML.HtmlToText();

//sSQL = „Update Artikel Set RakutenProduktUpdate = 0 where RakutenProduktUpdate = 1 and Mandant = ‚“ + exSettings.eXODAClientname + „‚“;
//exSettings.HS4Dataset.Open(HS4Software.DatabaseType.ExistingConnection, „“, sSQL, „classrakuten_update“, „ID“, true, false);

foreach (DataRow row in exSettings.HS4Dataset.HSDataSet.Tables[„classrakuten_product“].Rows)
{
oItem = oItems.LoadItem(Convert.ToInt64(row[„ID“].ToString()));
oItem.RakutenProductUpdate = false;
oItems.SaveItem(oItem);
string sDescription = „“;
sDescription = oHTML.ConvertHtml(oItem.eBayDescription + “ “ + oItem.ConditionDescription);

Boolean bProductExists = ProductExists(oItem.ItemID.ToString());

WebClient oClient = new WebClient();
NameValueCollection oNVC = new NameValueCollection();
oNVC[„name“] = oItem.Caption.Replace(„Gabel-Stapler“, „Gabelstapler“);
oNVC[„key“] = this.RakutenAPIKey;
oNVC[„product_art_no“] = oItem.ItemID.ToString();
oNVC[„producer“] = oItem.Brand;
oNVC[„stock_policy“] = „1“;
oNVC[„stock“] = oItems.Stock(oItem.ItemID).ToString();
oNVC[„description“] = (sDescription.Replace(„\r\n“, “ „));
oNVC[„delivery“] = „0“; //0     =     Sofort lieferbar (Lieferzeit 1-4 Werktage) 3     =     versandfertig in 3 Werktagen (Lieferzeit 4-6 Werktage)
double dPrice;
dPrice = oItem.RakutenPrice;
dPrice = oItem.RakutenPrice – oItem.OriginShippingCosts;

if ((oItem.eBayShipping.ToLower().IndexOf(„paket“) > 0) || (oItem.eBayShipping.ToLower().IndexOf(„ups“) > 0))
oNVC[„shipping_group“] = „2“;
else
oNVC[„shipping_group“] = „1“;

oNVC[„price“] = dPrice.ToString(„0.00“).Replace(„,“, „.“);
oNVC[„tax“] = „1“;

if (oTools.IsNumeric(oItem.EAN))
{
sSQL = „Select * from ArtikelEANPool where EAN = ‚“ + oItem.EAN + „‚ and Mandant = ‚“ + exSettings.eXODAClientname + „‚“;
exSettings.HS4Dataset.Open(HS4Software.DatabaseType.ExistingConnection, „“, sSQL, „classrakuten_ean“, „EAN“, true, false);
if (exSettings.HS4Dataset.HSDataSet.Tables[„classrakuten_ean“].Rows.Count == 0)
oNVC[„ean“] = oItem.EAN;
}

string sUrl;
if (bProductExists)
{
sUrl = „https://webservice.rakuten.de/merchants/products/editProduct“;
oNVC[„product_art_no“] = oItem.ItemID.ToString();
}
else
sUrl = „https://webservice.rakuten.de/merchants/products/addProduct“;
var response = oClient.UploadValues(sUrl, oNVC);

System.Xml.XmlDocument oXMLDoc = new XmlDocument();
oXMLDoc.LoadXml(Encoding.ASCII.GetString(response));
System.Data.DataSet ds = new System.Data.DataSet();

string sNextPrintDocNumber = oTools.GetNextPrintDocNumber();

oXMLDoc.Save(oTools.AppPath() + „/XML/“ + „RakutenAddProduct_“ + oItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);
ds.ReadXml(oTools.AppPath() + „/XML/“ + „RakutenAddProduct_“ + oItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);

if (ds.Tables.IndexOf(„error“) > -1)
{
string sErrortext = „Rakutenfehler:\r\n“;
foreach (DataRow rowErrors in ds.Tables[„error“].Rows)
{
sErrortext = sErrortext + rowErrors[„message“].ToString() + “ (“ + rowErrors[„code“].ToString() + „)\r\n“;
}
oItem.Comment = sErrortext;
oItem.RakutenItem = false;
oItems.SaveItem(oItem);
}
//Wenn Produkt schon angelegt, kommt keine ID zurück
if ((ds.Tables[„result“].Rows[0][„success“].ToString() == „1“) && (!bProductExists))
{
oItem.RakutenProductID = ds.Tables[„result“].Rows[0][„product_id“].ToString();
oItem.Comment = „Produkt bei Rakuten angelegt mit der ID “ + oItem.RakutenProductID;
oItems.SaveItem(oItem);
}
if (ds.Tables[„result“].Rows[0][„success“].ToString() == „1“)
{
if ((bProductExists) && (oItem.RakutenImageID.Length > 0))
deleteProductImage(oItems, oItem);
addProductImage(oItems, oItem);
}
}
}
/// <summary>
/// Fügt das Produktbild hinzu
/// </summary>
/// <param name=“eXODAItems“></param>
/// <param name=“eXODAItem“></param>
public void addProductImage(eXODALib.ItemLib.Items eXODAItems, eXODALib.ItemLib.Item eXODAItem)
{
WebClient oClient = new WebClient();
NameValueCollection oNVC = new NameValueCollection();
oNVC[„key“] = this.RakutenAPIKey;
oNVC[„product_id“] = eXODAItem.RakutenProductID;
oNVC[„url“] = oTools.ImgFirstPictureURL(eXODAItem.ItemID.ToString());
//oNVC[„replace_image_id“] = „1“;
string sUrl = „https://webservice.rakuten.de/merchants/products/addProductImage“;
var response = oClient.UploadValues(sUrl, oNVC);

System.Xml.XmlDocument oXMLDoc = new XmlDocument();
oXMLDoc.LoadXml(Encoding.ASCII.GetString(response));
System.Data.DataSet ds = new System.Data.DataSet();

string sNextPrintDocNumber = oTools.GetNextPrintDocNumber();

oXMLDoc.Save(oTools.AppPath() + „/XML/“ + „RakutenAddImage_“ + eXODAItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);
ds.ReadXml(oTools.AppPath() + „/XML/“ + „RakutenAddImage_“ + eXODAItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);

if (ds.Tables.IndexOf(„error“) > -1)
{
string sErrortext = „Rakutenfehler:\r\n“;
foreach (DataRow rowErrors in ds.Tables[„error“].Rows)
{
sErrortext = sErrortext + rowErrors[„message“].ToString() + “ (“ + rowErrors[„code“].ToString() + „)\r\n“;
}
eXODAItem.Comment = sErrortext;

}
else
eXODAItem.RakutenImageID = ds.Tables[„result“].Rows[0][„image_id“].ToString();

eXODAItems.SaveItem(eXODAItem);
}

/// <summary>
/// Löscht das erste Produktbild
/// </summary>
/// <param name=“eXODAItems“></param>
/// <param name=“eXODAItem“></param>
public void deleteProductImage(eXODALib.ItemLib.Items eXODAItems, eXODALib.ItemLib.Item eXODAItem)
{
WebClient oClient = new WebClient();
NameValueCollection oNVC = new NameValueCollection();
oNVC[„key“] = this.RakutenAPIKey;
oNVC[„product_id“] = eXODAItem.RakutenProductID;
oNVC[„image_id“] = eXODAItem.RakutenImageID;
string sUrl = „https://webservice.rakuten.de/merchants/products/deleteProductImage“;
var response = oClient.UploadValues(sUrl, oNVC);

System.Xml.XmlDocument oXMLDoc = new XmlDocument();
oXMLDoc.LoadXml(Encoding.ASCII.GetString(response));
System.Data.DataSet ds = new System.Data.DataSet();

string sNextPrintDocNumber = oTools.GetNextPrintDocNumber();

oXMLDoc.Save(oTools.AppPath() + „/XML/“ + „RakutenDeleteImage_“ + eXODAItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);
ds.ReadXml(oTools.AppPath() + „/XML/“ + „RakutenDeleteImage_“ + eXODAItem.ItemID.ToString() + „_“ + sNextPrintDocNumber + „.XML“);

if (ds.Tables.IndexOf(„error“) > -1)
{
string sErrortext = „Rakutenfehler:\r\n“;
foreach (DataRow rowErrors in ds.Tables[„error“].Rows)
{
sErrortext = sErrortext + rowErrors[„message“].ToString() + “ (“ + rowErrors[„code“].ToString() + „)\r\n“;
}
eXODAItem.Comment = sErrortext;
eXODAItems.SaveItem(eXODAItem);
}

}

/// <summary>
/// Prüft ob ein Artikel bereits bei Rakuten gelistet ist
/// </summary>
/// <param name=“ItemID“></param>
/// <returns></returns>
public Boolean ProductExists(string ItemID)
{
Boolean bfound = false;
// Creates an HttpWebRequest for the specified URL.
string sUrl = „https://webservice.rakuten.de/merchants/products/getProducts?search=“ + ItemID + „&search_field=product_art_no&key=“ + this.RakutenAPIKey;
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
// Sends the HttpWebRequest and waits for a response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine(„\r\nResponse Status Code is OK and StatusDescription is: {0}“,
myHttpWebResponse.StatusDescription);
// Releases the resources of the response.

System.Xml.XmlDocument oXMLDoc = new XmlDocument();
oXMLDoc.Load(myHttpWebResponse.GetResponseStream());
System.Data.DataSet ds = new System.Data.DataSet();

string sNextPrintDocNumber = oTools.GetNextPrintDocNumber();

oXMLDoc.Save(oTools.AppPath() + „/XML/“ + „RakutenGetProduct_“ + ItemID + „_“ + sNextPrintDocNumber + „.XML“);
ds.ReadXml(oTools.AppPath() + „/XML/“ + „RakutenGetProduct_“ + ItemID + „_“ + sNextPrintDocNumber + „.XML“);
myHttpWebResponse.Close();

if (Convert.ToInt16(ds.Tables[„paging“].Rows[0][„total“]) > 0)
bfound = true;
return bfound;
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.