Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

HELP! Supplier_CSV_Cron.php not adding markup


Guest

Recommended Posts

Hi, heres my supplier CRON file - It should add my markup automatically but it doesnt, it updates the inventory but leaves the prices at cost and I have to use the admin "Update Prices" link - can anyone see whats wrong? I've put "supplier.com" instead of the suppliers true url, as google crawls these pages and I dont want to give my source away to the public.

 

<?
set_time_limit(0);

$percentage = 10; // make sure it is proper decimal value
$increase = true; // true or false . if you want to increase set it to true or if you want to decrease the price make it false;
$delete_old_products_categories = false;

getCSVFile();

$tblProducts = "products";
$tblBrand = "manufacturers";
$tblCat = "categories_description";
$tblCat2 = "categories";
$tblCategories = "products_to_categories";
$tblDescription = "products_description";

$cats=array();
$lastid=0;

$csvHeader = array("itemid","category","subcategory","brand","item","modelnumber","pic","price","instock","description","weight","retailprice","UPC", "Shipping Dimensions");

require('../includes/configure.php');
mysql_connect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD) or die ("mysql error: ".mysql_error());
mysql_select_db(DB_DATABASE) or die ("mysql error: ".mysql_error());
$current = 0;

$handle = fopen("csv/import.csv", "r");
$header = fgetcsv($handle, 15000, "^");
if($header!=$csvHeader)
{
$msg = "Invalid CSV file.";
}
else
{
if($delete_old_products_categories)
{
	$sql = "truncate table $tblCategories";
	$result = mysql_query($sql);

	$sql = "truncate table $tblDescription";
	$result = mysql_query($sql);

	$sql = "truncate table {$tblProducts}";
	$result = mysql_query($sql);

	$sql = "truncate table {$tblCat}";
	$result = mysql_query($sql);

	$sql = "truncate table {$tblCat2}";
	$result = mysql_query($sql);
}

$brands = getBrands($tblBrand);
$cats = getCats($tblCat,$tblCat2);
echo $lastid."<br>";
$sql = "select products_id from {$tblProducts} order by products_id desc limit 1";
$result = mysql_query($sql);
$lastproductid = 1;
if ($row = mysql_fetch_array($result))
{
	$lastproductid = $row["products_id"]+1;
}
echo "lastproductid: $lastproductid<br>";

$sql = "select manufacturers_id from {$tblBrand} order by manufacturers_id desc limit 1";
$result = mysql_query($sql);
$lastbrandid = 1;
if ($row = mysql_fetch_array($result))
{
	$lastbrandid = $row["manufacturers_id"]+1;
}
echo "lastbrandid: $lastbrandid<br>";
echo "Percentage Markup: $percentage<br>";
echo "Increase: $increase<br>";
while (($data = fgetcsv($handle, 15000, "^")) !== FALSE)
{
	$current++;
	$i = 1; 
	$catid = 0;
	while ($i < 3)
	{
		$sub = trim($data[$i]);
		if (strlen($sub)>0)
		{
			$catid = getCatID($cats,$catid,$sub);
		}
		else
		{
			break;
		}
		$i++;
	}
	if ($catid == 0)
	{
		continue;
	}
	$data[0] = (int)$data[0];
	$name = $data[4];
	$model = $data[5];

	$price = $data[7];
	$price = sprintf("%0.2f", $price);
	$percentage = sprintf("%0.2f", $percentage);
	$price_percentage = $price * ($percentage / 100);
	$price = ($increase == true) ? ($price + $price_percentage) : ($price - $price_percentage);

	$description = $data[9];
	$image = $data[6];
	$price = sprintf("%0.2f", $price);
	$dimension = $data[13];
	$dimension = $dimension != "" ? "<br><B>Dimensions of the shipping carton in inches : </B>$dimension" : "";
	$description .= $dimension;

	$sql = "SELECT products_id FROM {$tblProducts} WHERE products_id='$data[0]'";
	$rs = mysql_query($sql) or die("mysql error:". mysql_error());

	if($row = mysql_fetch_array($rs))
	{
		$productid=$row["products_id"];
		$sql = "UPDATE {$tblProducts} SET ".
		"products_model='".mysql_escape_string ($data[5])."',".
		"products_quantity='".mysql_escape_string ($data[8])."',".
		"products_weight='".mysql_escape_string ($data[10])."',".
		"products_price='{$data[7]}',".
		"products_image='".mysql_escape_string ($data[6])."',".
		"manufacturers_id='".getBrandID($tblBrand,$data[3])."'".
		" WHERE products_id='$data[0]'";
		$result = mysql_query($sql) or die("$sql:".mysql_error());
		$sql = "update {$tblDescription} set products_name='".mysql_escape_string($name)."',products_description='".mysql_escape_string($description)."' WHERE products_id='$productid'";
		$result = mysql_query($sql) or die("$sql:".mysql_error());
		$sql = "REPLACE {$tblCategories} (products_id,categories_id) values ('$productid','$catid')";
		$result = mysql_query($sql) or print "$model: $sql:".mysql_error()."\n<br>";
	}
	else
	{
		$sql = "delete from $tblDescription where products_id = '$data[0]'";
		mysql_query($sql);

		$sql = "delete from $tblCategories where products_id = '$data[0]'";
		mysql_query($sql);

		$sql = "INSERT INTO $tblDescription (products_id,products_name,products_description) values ('$data[0]','".mysql_escape_string ($name)."','".mysql_escape_string ($description)."')";
		$result = mysql_query($sql) or die("$sql:".mysql_error());
		$sql = "INSERT INTO {$tblCategories} (products_id,categories_id) values ('$data[0]','$catid')";
		$result = mysql_query($sql) or die("$sql:".mysql_error());
		$sql = "INSERT INTO {$tblProducts} (".
		"  products_id".
		", products_quantity".
		", products_weight".
		", products_model".
		", products_image".
		", products_price".
		", products_date_added".
		", products_status".
		", manufacturers_id".
		") VALUES (".
		"  '$data[0]'" .
		", '$data[8]'" .
		", '$data[10]'" .
		", '" . mysql_escape_string ($model) . "'" .
		", '" . mysql_escape_string ($image) . "'" .
		", '" . mysql_escape_string ($price)                       . "'" .
		", '" . date("Y-m-d H-i-s")             . "'
									, '1'
									, '".getBrandID($tblBrand,$data[3])."'
								)";
		$result = mysql_query($sql) or die("$sql:".mysql_error());
		$lastproductid++;
	}
}
fclose($handle);

$msg .= "</table>\r\n";

$sql = "SELECT DISTINCT products_image FROM {$tblProducts} WHERE products_image!=''";
$rs = mysql_query($sql);
while ($myRow = mysql_fetch_array($rs))
{
	$filename=$myRow["products_image"];
	$dstfile="../images/$filename";
	$srcfile="http://www.ppiimages.com/pics/$filename";
	if (!file_exists($dstfile))
	{
		echo "$srcfile => $dstfile<Br>";
		download($srcfile,$dstfile);
	}
}
}

function getBrands($tblBrand)
{
$sql = "SELECT manufacturers_id,manufacturers_name FROM {$tblBrand} ORDER BY manufacturers_id";
$rs = mysql_query($sql);
$i=0;
while ($row = mysql_fetch_array($rs))
{
	$brands[$i++] = $row;
}
return $brands;
}
function getCats($tblCat,$tblCat2)
{
$sql = "SELECT {$tblCat2}.categories_id,{$tblCat2}.parent_id,{$tblCat}.categories_name FROM {$tblCat},{$tblCat2} WHERE {$tblCat}.categories_id={$tblCat2}.categories_id ORDER BY categories_id";
$rs = mysql_query($sql);
$i=0;$j=0;
global $lastid;

while ($row = mysql_fetch_array($rs))
{
	if ($row["categories_id"]>$lastid)
	{
		$lastid=$row["categories_id"];
	}
	$cats[$i++] = $row;
}
return $cats;
}
function getBrandID($brands,$name)
{
global $lastbrandid,$tblBrand;
$sql = "SELECT $tblBrand.manufacturers_id FROM $tblBrand WHERE manufacturers_name='".mysql_escape_string($name)."'";
$rs = mysql_query($sql) or die("mysql error:".mysql_error());
if ($row = mysql_fetch_array($rs))
{
	return $row["manufacturers_id"];
}
else
{
	$sql = "insert into manufacturers (manufacturers_id,manufacturers_name) values( $lastbrandid,'".mysql_escape_string($name)."')";
	$rs = mysql_query($sql) or die("mysql error:".mysql_error());
	$lastbrandid++;
	return $lastbrandid-1;
}
}
function getCatID($cats,$parentid,$name)
{
global $tblCat,$tblCat2,$cats;
global $lastid;
$sql = "SELECT $tblCat.categories_id FROM $tblCat,$tblCat2 WHERE $tblCat.categories_id=$tblCat2.categories_id and categories_name='".mysql_escape_string($name)."' and parent_id='$parentid'";
$rs = mysql_query($sql) or die("mysql error:".mysql_error());
if ($row = mysql_fetch_array($rs))
return $row["categories_id"];

$lastid++;$curid=$lastid;
$qry="insert into $tblCat2 (categories_id,parent_id,sort_order) values('$curid','$parentid','0')";
mysql_query($qry);
$qry="insert into $tblCat (categories_id,categories_name) values('$curid','".mysql_escape_string($name)."')";
mysql_query($qry);

return $curid;
}
function getCatIDbyID($cats,$id)
{
for ($i=0; $i<count($cats); $i++)
{
	if (strtolower($cats[$i]['categories_id'])==$id) return $i;
}
return -1;
}
function getCSVFile()
{
$feed_url = "http://www.supplier.com/productindexdl.cfm";
$dir = "./csv";

$file = "import.csv";

if(file_exists($dir. "/". $file))
{
	@unlink($dir. "/". $file);
}

exec("wget -O $dir/$file $feed_url");
}
function download($file_source, $file_target)
{
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
if ($rh===false || $wh===false)
{
	return true;
}
while (!feof($rh))
{
	if (fwrite($wh, fread($rh, 1024)) === FALSE)
	{
		return true;
	}
}
fclose($rh);
fclose($wh);
return false;
}
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...