<?php
error_reporting(0);
set_time_limit (99999999);
include_once("modules/EcmProductB2Bs/productCardReallyNew.php");
include_once("include/html2fpdf/html2fpdf.php");
$p=new HTML2FPDF("L");
$p->SetAutoPageBreak(10);
$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select c.iso4217 as s from currencies as c inner join ecmpricebooks as q on q.exchange_rate_id=c.id where q.id='".$_REQUEST['record']."'"));
$symbol=$rr['s'];


$result = $GLOBALS['db']->query("select * from ecmpricebooks_customview where id='".$_GET['customview_id']."'");
$row=$GLOBALS['db']->fetchByAssoc($result);
$c=explode("||",$row['columns']);
$t=explode("||",$row['titles']);
$o=explode("||",$row['orders']);

if($_COOKIE['customview_id']!=$_GET['customview_id'] || $_SESSION['customview_id']=="" || !$_GET['order_by'])
{
	$pbo=array();
	for($i=0;$i<=8;$i++)
	{
		if($o[$i])
		{
			$exp=explode(" ",$o[$i]);
			$adesc=$exp[1];
			$ord=$exp[0];
			if($ord=="list_price")$pbo[]="ecmpricebooks_ecmproducts.price ".$adesc;
			else $pbo[]="ecmproducts.".$ord." ".$adesc;
		}
	}
	$_SESSION['pricebook_order']=implode(",",$pbo);
}
setcookie('customview_id',$_GET['customview_id'],time()+60*24*60*3600);

$order=$_SESSION['pricebook_order'];
$order=str_replace("ecmproducts.margin_rate","margin_rate",$order);
if($_REQUEST['order_by']){
	$order=str_replace($_REQUEST['order_by']." desc",$_REQUEST['order_by']." ".$_REQUEST['sorder'],$order);
	$order=str_replace($_REQUEST['order_by']." asc",$_REQUEST['order_by']." ".$_REQUEST['sorder'],$order);
}

$_SESSION['pricebook_order']=$order;

if($_REQUEST['order_by'] && $_REQUEST['sorder'])$_SESSION['pricebook_order']=$_REQUEST['order_by']." ".$_REQUEST['sorder'];

$where="";
if(count($_SESSION['pricebook_check'][$_SESSION['pricebook_id']])>0)
{
	foreach($_SESSION['pricebook_check'][$_SESSION['pricebook_id']] as $key=>$value){
		if($_SESSION['pricebook_check'][$_SESSION['pricebook_id']][$key]!="true" && $_SESSION['pricebook_check'][$_SESSION['pricebook_id']]['all']=="true"){
			$where_clauses[]="i.id!='".$key."'";
			$orand=' and ';
		}
		elseif($_SESSION['pricebook_check'][$_SESSION['pricebook_id']][$key]=="true" && $_SESSION['pricebook_check'][$_SESSION['pricebook_id']]['all']!="true"){
			$where_clauses[]="i.id='".$key."'";
			$orand=' or ';
		}
	}
	if(count($where_clauses)>0)$where="(".implode($orand,$where_clauses).") and ";
	elseif($_SESSION['pricebook_check'][$_SESSION['pricebook_id']]['all']!="true" && count($where_clauses)==0)$where="ecmpricebooks_ecmproducts.id='9999999999999999' and ";
}
else $where="i.id='9999999999999999' and ";
if($_SESSION['pricebook_order'])$order=" order by ".$_SESSION['pricebook_order'];
$z="select distinct i.ecmproduct_id,i.price,p.product_category_id,p.th from ecmpricebooks_ecmproducts as i inner join ecmproducts as p on p.id=i.ecmproduct_id inner join ecmpricebooks_ecmproducts_categories_sort as c on c.category_id=p.product_category_id where ".$where." i.ecmpricebook_id='".$_REQUEST['record']."' and i.deleted='0' order by c.position asc,i.position asc";
$num=mysql_num_rows($GLOBALS['db']->query($z));
$w=$GLOBALS['db']->query($z);
$cat="";
$i=0;
if($_REQUEST['header']==1){
	$rwt=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query($z));
	$p=catalogueHeader($p,$rwt['th']);
}
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$id=$r['ecmproduct_id'];
	if($_GET['price']=="srp_price")$r['price']="";
	/*
	if(($cat!=$r['product_category_id'] || $cat=="") && $_REQUEST['title']==1){
		if($_REQUEST['extra']==1 && $cat!=""){
			if(categoryExtra($p,$cat,$th)){
				$p->AddPage();
				$p->Image("modules/EcmProductB2Bs/".categoryExtra($p,$cat,$th),5,0,283);
			}
		}
		$p->AddPage();
		$p=categoryTitle($p,$r['product_category_id'],$r['th']);
	}
	$cat=$r['product_category_id'];
	$th=$r['th'];
	*/
	//add mz 05-12-201
	//exchange price if EUR
	if ($_REQUEST['price']=="srp_price") {
		$srp_price_result = $GLOBALS['db']->query("SELECT srp_price FROM ecmproducts WHERE id ='".$r['ecmproduct_id']."'");
		$srp_price_row = $GLOBALS['db']->fetchByAssoc($srp_price_result);
		$price = $srp_price_row['srp_price'];
		} else
		$price = $r['price'];
	
	$exchange_result = $GLOBALS['db']->query("SELECT exchange_rate_id, currency_value FROM ecmpricebooks WHERE id='".$_SESSION['pricebook_id']."'");
	
	$exchange_row=$GLOBALS['db']->fetchByAssoc($exchange_result);
	if ($exchange_row['exchange_rate_id']=="5e72f103-1989-8b1f-dc9a-493fe7c7449f")
		$price = $price * (1 / $exchange_row['currency_value']); 

//$i++;
//if ($i<202) continue;
$p=productCardNew($p,$id,$_GET['language'],true,true,$price,$symbol,$_REQUEST['show_price'], $_REQUEST['ean']);	
}
/*
if($_REQUEST['extra']==1){
	if(categoryExtra($p,$cat,$th)){
		$p->AddPage();
		$p->Image("modules/EcmProductB2Bs/".categoryExtra($p,$cat,$th),5,0,283);
	}
}
*/
if($_REQUEST['create_img']==1){
	$guid=create_guid();
	$type="F";
	$file="cache/upload/Catalogue".$guid.".pdf";
}
else{
	$type="D";
	$file="Catalogue".date("YmdHi").".pdf";
}
$p->Output($file,$type);

if($_REQUEST['create_img']==1){
	exec("convert -density 120 -quality 100 /var/www/html/e5crm/".$file." /var/www/html/e5crm/".str_replace(".pdf",".jpg",$file));
	chmod(str_replace(".pdf",".jpg",$file),0777);
	header("Location: index.php?module=EcmProductB2Bs&action=showProductsCardImages&to_pdf=1&guid=".$guid);
}
?>