I am bulding a code for my site and have few problems
- When I run this few times, it updates table ps_stock_available but with wrong values and after some time is stops to update that table? The problem is in the last query, two queries above are working
`
mysql_query("UPDATE ps_product SET ".
"wholesale_price=".$priceWithDiscount.", ".
"price=".$price.", ".
"quantity=".$freeOnStock." ".
"WHERE supplier_reference='".$key."'");
mysql_query("UPDATE ps_product_shop SET ".
"wholesale_price=".$priceWithDiscount.", ".
"price=".$price." ".
//"WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
//" WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
mysql_query("UPDATE ps_stock_available SET ".
"quantity=".$freeOnStock." ".
// "WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
//" WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
mysql_query("UPDATE ps_product_supplier SET ".
"product_supplier_price_te=".$priceWithDiscount.", ".
//"WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
// " WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
//" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" .$key."'");
`
Also $selektiraniArtikliIDs[$artiklFoundIdx] has the wqorng id?
$shopArtiikalCount = 0;
while($row = mysql_fetch_array($result)) {
$selektiraniArtikli[$shopArtiikalCount] = $row['supplier_reference'];
$selektiraniArtikliIDs[$shopArtiikalCount] = $row['id_product'];
$shopArtiikalCount++;
}
if ( $onlyArtList )
{
//fwrite($fl, print_r($suplierItemList, true));
foreach ($suplierItemList->key as $key)
{
$row = array($key, $name, $tradeMark, $group, $subGroup, $currency, $price, $priceWithDiscount, $featured, $warranty, $freeOnStock, $availability, $description, $pictureURL, $pictureURLHighResolution, $length, $width, $height, $mass, $ean, $serialNumberOut, $manufacturersPartNumber, $similarItems, "Microline", $nasa_ref, $key, $name, $name);
fwrite($fp, implode($delimiter, $row)."\r\n");
}
exit;
}
In this part I only get $nasa_ref sometimes which is strange?
$manufacturersPartNumber = $xml->manufacturersPartNumber;
$similarItems = $xml->similarItems;
$nasa_ref = trim($manufacturersPartNumber);
//if(strlen($nasa_ref) == 0)
//{
//$nasa_ref = $key;
//}
if(is_null($nasa_ref) || empty($nasa_ref)) {
$nasa_ref = $key;
}
In this par I have problems with finding new products:
// artikl nije pronadjen?
if ( $findNew || !$artiklFound ) {
echo "novi artikl: $key <br/>\n";
fwrite($fl, "novi artikl: $key\n");
// dodaj ga u .csv
fwrite($fp, implode($delimiter, $row)."\r\n");
if ( $findNew ) {
$countUpdated++;
if ( $countUpdated >= 250 ) {
break;
}
}
}
This is whole code
<?php
ini_set('max_execution_time', '300');
// Izradio Silvio Baričević - silvio@silens.hr
// skype: silvio.baricevic
// Silens d.o.o.
// Trajanje uvoza mol u csv datoteku traje oko sat vremena kad se koristi cron job, što je i preporuka za korištenje ove skripte.
// Ukoliko budete koristili localhost, možete očekivati greške zbog prekida u streamu.
// Postavljamo vremenski limit za izvršavanje skripte
set_time_limit(10800);
// Postavljamo limit za memoriju za PHP (iako je bolje to učiniti u php.ini)
ini_set('memory_limit', '512M');
// Potreban library za izvršavanje soapa pošto većina hostinga ne podržava php soap zadani library
require_once('lib/nusoap.php');
// SELECT * FROM `ps_product` WHERE `supplier_reference` = "kon-tab-arn-4g" LIMIT 0 , 30
////////////////////////////////////////////
// config
////////////////////////////////////////////
// MOL Korisničke postavke za prijavu
////////////////////////////////////////////
// implementation
////////////////////////////////////////////
// Početak za timing skriptu za stranicu
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
$debug = 0; if(isset($_GET['debug'])) $debug = 1;
// bez parametara: svi proizvodi (ne upotrebljavati jer traje dugo (ca. 90 min)
// inace, ovo ...
$first = 1; if(isset($_GET['start'])) $first = $_GET['start'];
$nrRows = -1; if(isset($_GET['rows'])) $nrRows = $_GET['rows'];
// ... ili ovo ...
$findNew = 0; if(isset($_GET['onlynew'])) $findNew = 1;
// ... ili ovo
$onlyArtList = 0; if(isset($_GET['onlylist'])) $onlyArtList = 1;
echo 'Microline update';
echo "<br/>\n";
echo "<br/>\n";
// Delimiter koji ćemo koristiti u csv datoteci
$delimiter = '^';
// Standardna marža
$marza = 1.70;
// Postavke za wsdl
$wsdl='http://www.microline.hr/WebServices/MOL.asmx?wsdl';
$params = array();
$params["customerKey"] = $kupac;
$params["password"] = $lozinka;
$soap = new nusoap_client($wsdl, $params);
$soap->useHTTPPersistentConnection();
$soap->soap_defencoding = 'UTF-8';
$soap->decode_utf8 = false;
$suplierItemList = $soap->call("itemList", $params);
$suplierItemList = implode($suplierItemList);
$suplierItemList = simplexml_load_string($suplierItemList);
$resultTradeMarkList = $soap->call("tradeMarkList", $params);
$resultTradeMarkList = implode($resultTradeMarkList);
$resultTradeMarkList = simplexml_load_string($resultTradeMarkList);
$resultItemTypeList = $soap->call("itemTypeList", $params);
$resultItemTypeList = implode($resultItemTypeList);
$resultItemTypeList = simplexml_load_string($resultItemTypeList);
$paramsSubTypeList = $params;
$paramsSubTypeList["onlyForItemTypeKey"] = "" ;
$resultItemSubTypeList = $soap->call("itemSubTypeList", $paramsSubTypeList);
$resultItemSubTypeList = implode($resultItemSubTypeList);
$resultItemSubTypeList = simplexml_load_string($resultItemSubTypeList);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>microline update</title>
</head>
<body>
<?php
// Kreiramo polja za trademark, grupu i podgrupu. Naknadno ćemo pretraživati ova polja.
$countUpdated = 0;
foreach ($resultTradeMarkList->tradeMark as $marka) {
$brand[$countUpdated][0] = $marka->key;
$brand[$countUpdated][1] = $marka->description;
$countUpdated++;
}
$countUpdated = 0;
foreach ($resultItemTypeList->type as $grupa) {
$vrsta[$countUpdated][0] = $grupa->key;
$vrsta[$countUpdated][1] = $grupa->description;
$countUpdated++;
}
$countUpdated = 0;
foreach ($resultItemSubTypeList->subType as $podgrupa) {
$podvrsta[$countUpdated][0] = $podgrupa->id;
$podvrsta[$countUpdated][1] = $podgrupa->description;
$countUpdated++;
}
$fileNamPrefix = 'products_upd';
if ( $nrRows > 0 )
{
$fileNamPrefix = 'products_upd_'.$first.'_'.$nrRows;
}
else if ( $onlyArtList )
{
$fileNamPrefix = 'products_all_refs';
}
else if ( $findNew )
{
$fileNamPrefix = 'products_new';
}
$fl = NULL;
if ( $debug ) {
$fl = fopen($fileNamPrefix.'.log', 'w');
}
else {
$fl = fopen($fileNamPrefix.'.log', 'a+');
}
fwrite($fl, date("Y-m-d H:i:s")."\n");
// Otvaramo csv datoteku u koju ćemo zapisivati podatke
$fp = fopen($fileNamPrefix.'.csv', 'w');
//fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
if ( $nrRows > 0 )
{
echo 'osvjezavanje od '.$first.', sljedecih '.$nrRows.' proizvoda';
}
else if ( $findNew )
{
echo 'pronalazimo samo nove proizvode, file za import: products_new.csv';
}
else if ( $onlyArtList )
{
echo 'samo lista svih proizvoda, file: '.$fileNamPrefix.'.csv';
}
else
{
echo 'osvjezavanje svih proizvoda:';
}
echo "<br/>\n";
echo "<br/>\n";
// Zapisujemo header u csv, tj. prvi red
$row = array("key", "name", "tradeMark", "group", "subGroup", "currency", "basePrice", "priceWithDiscount", "featured", "warranty", "freeOnStock", "availability", "description", "pictureURL", "pictureURLHighResolution", "length", "width", "height", "mass", "ean", "serialNumberOut", "manufacturersPartNumber", "similarItems", "supplier", "nasa_ref", "key", "name", "name");
fputcsv($fp, $row, $delimiter);
$selektiraniArtikli = array();
$selektiraniArtikliIDs = array();
//$selektiraniArtikli = array("asus-g55vw-s1020h", "asus-g55vw-s1185h");
$con = mysql_connect("localhost", $mysql_user, $mysql_pwd);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($mysql_db, $con);
/////////////////////////////
// find microline's supplier id
$result = mysql_query("SELECT id_supplier FROM ps_supplier WHERE name = 'Microline'");
$id_supplier = -1;
while($row = mysql_fetch_array($result)) {
$id_supplier = $row['id_supplier'];
break;
}
if ( $id_supplier < 0 )
{
die( "Ne mogu pronaci Microline kao dobavljaca u bazi: ".$mysql_db );
}
/////////////////////////////
// select all suplier products from the shop
$result = mysql_query("SELECT id_product, supplier_reference FROM ps_product WHERE id_supplier = $id_supplier");
$shopArtiikalCount = 0;
while($row = mysql_fetch_array($result)) {
$selektiraniArtikli[$shopArtiikalCount] = $row['supplier_reference'];
$selektiraniArtikliIDs[$shopArtiikalCount] = $row['id_product'];
$shopArtiikalCount++;
}
if ( $debug ) {
fwrite($fl, "shopArtiikalCount: $shopArtiikalCount\n");
fwrite($fl, print_r($selektiraniArtikli, true));
fwrite($fl, "lista artikala u microline shop-u:\n");
fwrite($fl, print_r($suplierItemList, true));
}
if ( $onlyArtList )
{
//fwrite($fl, print_r($suplierItemList, true));
foreach ($suplierItemList->key as $key)
{
$row = array($key, $name, $tradeMark, $group, $subGroup, $currency, $price, $priceWithDiscount, $featured, $warranty, $freeOnStock, $availability, $description, $pictureURL, $pictureURLHighResolution, $length, $width, $height, $mass, $ean, $serialNumberOut, $manufacturersPartNumber, $similarItems, "Microline", $nasa_ref, $key, $name, $name);
fwrite($fp, implode($delimiter, $row)."\r\n");
}
exit;
}
// TODO: debug
//echo "first: $first <br/>\n";
// Ulazimo u petlju gdje idemo kroz sve šifre lagera
$artNr = 0;
$countUpdated = 0;
foreach ($selektiraniArtikli as $key) {
$artNr++;
$artiklFound = false;
$artiklFoundIdx = 0;
foreach ($suplierItemList->key as $artikl) {
if ($artikl == $key) {
$artiklFound = true;
break;
}
$artiklFoundIdx++;
}
// ako je postavljen raspon ...
if ( $nrRows > 0 )
{
// ... preskoci prve ...
if ( $artNr < $first )
{
continue;
}
// ... ili prekini nakon zadnjeg
else if ( $artNr > ($first + $nrRows - 1) )
{
break;
}
}
// ako pronalazimo samo nove artikle, a trenutni artikal je pronadjen, onda ga preskoci
else if ( $findNew && $artiklFound )
{
continue;
}
// e.g.: https://www.microline.hr/Microline.Screen.Report_Microline.Screen.Report.ItemData.ashx?customerKey=&password=itemKey=amd-a10-x4-5700&extraInfo=true
$url = "https://www.microline.hr/Microline.Screen.Report_Microline.Screen.Report.ItemData.ashx?customerKey=".$kupac."&password=".$lozinka."&itemKey=".$key."&extraInfo=true";
// Provjera da li postoji xml za odgovarajuću šifru da nam ne izbaci preranu pogrešku
if ($artiklFound)
{
if ($xml = simplexml_load_file(utf8_encode($url), 'SimpleXMLElement', LIBXML_NOCDATA))
{
if ( $debug ) {
//fwrite($fl, "xml for artnr->key: ".$artNr." -> ".$key." : ");
echo "shopArtNr->key->suplArtNr: $artNr -> $key -> $artiklFoundIdx <br>\n";
fwrite($fl, "xml for shopArtNr->key->suplArtNr: $artNr -> $key -> $artiklFoundIdx: ");
fwrite($fl, print_r($xml, true));
}
// Postavljamo varijable koje ćemo zapisati u csv datoteku
$name = $xml->name;
$tradeMark = $xml->tradeMark;
$array = $brand;
$searchTerm = (string)$tradeMark;
foreach ($array as $childKey => $childArray) {
if ($childArray['0'] == $searchTerm) {
$tradeMark = $brand[$childKey][1];
}
}
// translate group to group name
$group = $xml->group;
//echo "- group: $group <br/>\n";
// skip group: Razne stvari - ne koristiti
if ($group == "raz") {
//echo "- skipped <br/>\n";
continue;
}
$array = $vrsta;
$searchTerm = (string)$group;
foreach ($array as $childKey => $childArray) {
if ($childArray['0'] == $searchTerm) {
$group = $vrsta[$childKey][1];
}
}
// translate subGroup to subGroup name
$subGroup = $xml->subGroup;
$array = $podvrsta;
$searchTerm = (string)$subGroup;
foreach ($array as $childKey => $childArray) {
if ($childArray['0'] == $searchTerm) {
$subGroup = $podvrsta[$childKey][1];
}
}
$currency = $xml->currency;
$basePrice = $xml->basePrice;
$basePrice = str_replace(",", "." , $basePrice);
// Računamo cijenu sa uračunatom maržom bez pdv-a. Ukoliko želite dodati i PDV, samo pomnožite varijablu sa 1.25 u dodatnom redu.
$priceWithDiscount = $xml->priceWithDiscount;
$priceWithDiscount = str_replace(",", "." , $priceWithDiscount);
$priceWithDiscount = floatval($priceWithDiscount);
$price = $priceWithDiscount * $marza;
$featured_element = $xml->priceWithDiscount->attributes();
$featured = $featured_element['actionPrice'];
$warranty = $xml->warranty;
$freeOnStock = $xml->freeOnStock;
$availability = $xml->availability;
$length = $xml->length;
$width = $xml->width;
$height = $xml->height;
$mass = $xml->mass;
$length = str_replace(",", "." , $length);
$width = str_replace(",", "." , $width);
$mass = str_replace(",", "." , $mass);
$height = str_replace(",", "." , $mass);
$ean = $xml->ean;
$serialNumberOut = $xml->serialNumberOut;
$manufacturersPartNumber = $xml->manufacturersPartNumber;
$similarItems = $xml->similarItems;
$nasa_ref = trim($manufacturersPartNumber);
//if(strlen($nasa_ref) == 0)
//{
//$nasa_ref = $key;
//}
if(is_null($nasa_ref) || empty($nasa_ref)) {
$nasa_ref = $key;
}
//$description .= '<table width="560" border="0" cellspacing="0" cellpadding="0">';
$description = '<table width="560" border="0" cellspacing="0" cellpadding="0">';
$popuni1 = "<tr><td width='150' valign='top'><b>";
$popuni2 = "</b>: ";
foreach ($xml->description->children() as $opis) {
foreach ($opis->children() as $property) {
// Čistim string da ne bi bilo nereda prilikom zapisivanja u csv
$property = preg_replace("/\r\n|\r|\n/", ' ', $property);
$description .= $popuni1.$property.$popuni2;
if ($popuni1 == "<tr><td width='150' valign='top'><b>") {
$popuni1 = "</td><td valign='top'>";
$popuni2 = "</td></tr>";
}
else {
$popuni1 = "<tr><td width='150' valign='top'><b>";
$popuni2 = "</b>: ";
}
}
}
$description .= "<tr><td width='150' valign='top'><b></b></td><td> </td></tr>";
$description .= "<tr><td width='150' valign='top'><b>Dimenzije i težina:</b></td><td>širina: ".$width." cm - duljina: ".$length." cm - visina: ".$height." cm - težina: ".$mass." kg</td></tr>";
$description .= "<tr><td width='150' valign='top'><b></b></td><td> </td></tr>";
$description .= "<tr><td width='150' valign='top'><b>Jamstvo:</b></td><td>".$warranty." (godina)</td></tr>";
$description .= "</table>";
$pictureURL = $xml->pictureURL;
$pictureURLHighResolution = $xml->pictureURLHighResolution;
if ((string)$pictureURLHighResolution == "") {
$pictureURLHighResolution = $pictureURL;
}
// Ovdje preskačemo zapisivanje reda u csv. Postavljanje varijable se vrši u dodjeljivanju nadgrupa, grupa...
if ($nadgrupa == "PRESKOCI") continue;
// Pripremamo završno polje za zapisivanje u csv datoteku i zapisujemo red
$row = array($key, $name, $tradeMark, $group, $subGroup, $currency, $price, $priceWithDiscount, $featured, $warranty, $freeOnStock, $availability, $description, $pictureURL, $pictureURLHighResolution, $length, $width, $height, $mass, $ean, $serialNumberOut, $manufacturersPartNumber, $similarItems, "Microline", $nasa_ref, $key, $name, $name);
// artikl nije pronadjen?
if ( $findNew || !$artiklFound ) {
echo "novi artikl: $key <br/>\n";
fwrite($fl, "novi artikl: $key\n");
// dodaj ga u .csv
fwrite($fp, implode($delimiter, $row)."\r\n");
if ( $findNew ) {
$countUpdated++;
if ( $countUpdated >= 250 ) {
break;
}
}
}
else
{
//////////////////////////////////////////////////
// artikl je pronadjen, direktni update mysql baze
mysql_query("UPDATE ps_product SET ".
"wholesale_price=".$priceWithDiscount.", ".
"price=".$price.", ".
"quantity=".$freeOnStock." ".
"WHERE supplier_reference='".$key."'");
mysql_query("UPDATE ps_product_shop SET ".
"wholesale_price=".$priceWithDiscount.", ".
"price=".$price." ".
//"WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
//" WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
mysql_query("UPDATE ps_stock_available SET ".
"quantity=".$freeOnStock." ".
// "WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
//" WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
mysql_query("UPDATE ps_product_supplier SET ".
"product_supplier_price_te=".$priceWithDiscount.", ".
//"WHERE id_product=".$selektiraniArtikliIDs[$artiklFoundIdx]);
// " WHERE id_product = (SELECT id_product FROM ps_product_supplier WHERE product_supplier_reference = '" . $key . "' LIMIT 1)");
" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" . $key . "' LIMIT 1)");
//" WHERE id_product = (SELECT id_product FROM ps_product WHERE supplier_reference = '" .$key."'");
$countUpdated++;
}
}
else
{
echo "xml ne postoji: $key <br/>\n";
fwrite($fl, "xml ne postoji: $key\n");
}
$description = "";
}
else
{
echo "artikl vise ne postoji: $key <br/>\n";
fwrite($fl, "artikl vise ne postoji: $key\n");
}
if ($debug ) {
// Ovo je zgodno omogućiti da isprobamo promjene na skripti jer bi se inače moralo dugo čekati da se izvrši cjelokupni import
if ($countUpdated == 20) { echo "debug break<br/>\n"; break; }
}
}
fclose($fp);
mysql_close($con);
function csv2xml($file, $container = 'data', $rows = 'row')
{
$r = "<{$container}>\n";
$row = 0;
$cols = 0;
$titles = array();
$handle = @fopen($file, 'r');
if (!$handle) return $handle;
while (($data = fgetcsv($handle, 0, '^')) !== FALSE)
{
if ($row > 0) $r .= "\t<{$rows}>\n";
if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}
$r .= "\t\t<{$titles[$i]}>";
$r .= $data[$i];
$r .= "</{$titles[$i]}>\n";
}
if ($row > 0) $r .= "\t</{$rows}>\n";
$row++;
}
fclose($handle);
$r .= "</{$container}>";
return $r;
}
// for xml
$fh = fopen($fileNamPrefix.'.xml', 'w') or die("can't open file");
fwrite($fh, csv2xml($fileNamPrefix.'.csv'));
fclose($fh);
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo "<br/>\n";
if ( $findNew ) {
echo 'Novih artikala: '.$countUpdated;
fwrite($fl, 'Novih artikala: '.$countUpdated);
}
else
{
echo 'Osvjezeno artikala: '.$countUpdated;
fwrite($fl, 'Osvjezeno artikala: '.$countUpdated);
}
echo "<br/>\n";
echo 'Page generated in '.$total_time.' seconds.';
fwrite($fl, "\n");
fclose($fl);
?>
</body>
</html>
If anybody has ides on this?
Thanks