<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Sales </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style type="text/css">
table.params td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }

table.master th { font-size: 14px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                      }				 
table.master td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }
table.group td { font-size: 12px; 
                 font-weight: bold;
                 font-family:Times-New-Roman;
               }				 
				 
table.detail td { font-size: 10px; 
                  font-family:Times-New-Roman;
                 }				 
table.headerdetail td { font-size: 10px; 
                        font-weight: bold;
                        font-family:Times-New-Roman;
                      }				 
table.totals td { font-size: 12px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                 }					  
				 

</style>


</HEAD>


HERE;

include("config.inc.php");

//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");
 
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");



//maroun 30.03.2011
$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}


if ($printaccess==0 or $printaccess=='')
echo'<BODY oncontextmenu="return false;">';
else
echo'<BODY>';



	
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, report_color1, report_color2, Company_Name As CompanyName from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
}


//User Access
$querylog=" Select ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice ".
          " From ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$_SESSION['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist   = mysql_num_rows($projectresult);




$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);

print <<< HERE
<table width="100%" border="0" align="center">
<tr>
  <td width='30%'><b>$CompanyName</b></td>
  <td width='50%' align='center'><font size=4><b>$title</b></font></td>
  <td width='10%'><font size=1><b> Printed Date: </b> <br><b>Time: </b>
  <td width='10%'><font size=1>
HERE;

echo $printdate[2]."/".$printdate[1]."/".$printdate[0];
//echo DATE("d"."/"."m"."/"."Y");
echo "<br>".$dt[1]." ".$dt[2];

$dispfromref  = $fromref;
$disptoref = $toref;
$dispfromclient = $fromclient;
$disptoclient = $toclient;
$dispfromdate = $fromdate;
$disptodate = $todate;

if (empty($fromref))
  $fromref = 'null'; 
  
if (empty($toref))
  $toref = 'null';   
  
if ($cur == 'currency1'){
   $cur='null';  
   $displaycur = '';
}   
else {
   $displaycur = $cur;
   $cur="'".$cur."'";
}   

if (empty($fromclient))   
   $fromclient = 'null';
else
   $fromclient = "'".$fromclient."'";   
   
if (empty($toclient))   
   $toclient = 'null';
else
   $toclient = "'".$toclient."'";      

if (empty($fromproject))   
   $fromproject = 'null';
else
   $fromproject = "'".$fromproject."%'";    

if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  

if (empty($fromdate))   
   $fromdate = 'null';
else
   $fromdate = "'".$fromdate."'"; 
   
if (empty($todate))   
   $todate = 'null';
else
   $todate = "'".$todate."'";

$query_warehouse = " select Warehouse_code, Warehouse_name from warehouse ";
				// " where ledger_number between ifNull(".$fromclient.",ledger_number) and ifNull(". $toclient.",ledger_number) ";
   
print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>

HERE;


print <<<HERE
<table class="params" width="100%" border="0" align="center" >
<tr>
  <td width="10%" valign='top'><b>Client</b></td>
  <td width="5%"><b>From <br>To </b></td>
  <td width="35%">$dispfromclient <br> $disptoclient</td>
  <td width="10%" valign='top'><b>Reference</b></td>
  <td width="5%"><b>From <br>To </b></td>
  <td width="35%">$dispfromref <br> $disptoref </td>
</tr>
<tr>
	<td width="10%" valign='top'><b>Sales Man</b></td>
	<td width="40%">$salesmanname</td>
</tr>
<tr>
  <td width="10%"><b>Date From/To</b></td>
  <td width="40%" colspan='2'>$dispfromdate - $disptodate </td>
  <td width="10%"><b>Currency</b></td>
  <td width="40%" colspan='2'>$displaycur </td>
</tr>
</table>


<table width="100%" border="0" cellpadding='0' cellspacing='0' align="center" class='master' >
<tr>
	
HERE;

print <<<HERE
    <th width="8%">Items</th>
	<th width="15%">Description</th>
HERE;
$warehousear = array();
$i=0;
$resultwarehouse = mysql_query($query_warehouse,$connection);
while($rwwarehouse = mysql_fetch_row($resultwarehouse)){
	$warehousear[$i] = $rwwarehouse[0];
	echo "<th width='8%'>$rwwarehouse[0]</th>";
	$i++;
}
	
echo"</tr>";


print <<<HERE
<tr>
    <td colspan='13'><hr width='100%' color='black' size='2'></td>
</tr>
 
HERE;
 
   
$querydelivery = "select deliverydt.Warehouse_code,date_format(deliveries.delivery_date,'%d/%m/%Y') delivery_date, deliverydt.item_code, item_description, deliverydt.currency_code,
				if(upper($cur)='".$Base1."',round(sum(deliverydt_quantity*SaleDt_PriceBase1),2),if(upper($cur)='".$Base2."',round(sum(deliverydt_quantity*SaleDt_PriceBase2),2),0)) as totalprice
				from deliverydt
				left join deliveries on deliveries.delivery_id = deliverydt.delivery_id
				left join warehouse on warehouse.Warehouse_code = deliverydt.Warehouse_Code
				left join items on items.item_code = deliverydt.item_code
				left join project on project.project_code = deliveries.project_code
				left join costcent on costcent.costcent_code = deliveries.costcent_code";
			
if ($projectexist >	0)
  $querydelivery= $querydelivery." left join ( select distinct project_code from ".$_SESSION['dbLabel'].".loginstk ".
								 "			 where loginstk.user_account='".$_SESSION['useraccount']."'  ".
								 "           and (project_code is not null and project_code != '') ".
								 "			) Q1 on project.project_code like concat(Q1.project_code,'%') ";

				


$querydelivery= $querydelivery.  				
				" where deliveries.client_code between ifNull(".$fromclient.",deliveries.client_code) and ifNull(". $toclient.",deliveries.client_code) ".
				" and delivery_reference between ifNull(". $fromref .",delivery_reference) and ifNull(". $toref.",delivery_reference) ".
				" and upper(ifNull(deliveries.project_code,-1)) like ifNull(upper(".$fromproject."), upper(ifNull(deliveries.project_code,-1))) ".
				" and date_format(deliveries.delivery_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(deliveries.delivery_date,'%Y-%m-%d')) 
				and ifNull(". $todate.",date_format(deliveries.delivery_date,'%Y-%m-%d')) ";
if ($_GET['salesman'] !='') {
	$querydelivery= $querydelivery."and salesman_code='$salesman'";
}
$querydelivery= $querydelivery."group by deliverydt.item_code, deliverydt.Warehouse_code
								order by deliverydt.item_code, deliverydt.Warehouse_code ";
								//echo $querydelivery;
				

$masteritem  = '';


  $resultdelivery=mysql_query($querydelivery,$connection) or die(mysql_error());
  
$col=0; $i=0;
echo "<tr><td></td>";
while($rowdelivery=mysql_fetch_row($resultdelivery)){

	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}
		if($masteritem != $rowdelivery[2]){
	  echo "</tr>"; 	$i=0;		
	  echo "<tr>";
	  echo "<td width='10%'>$rowdelivery[2]</td>";
	  echo "<td width='15%'>$rowdelivery[3]</td>";
		}
	 while($warehousear[$i]){
	  if($warehousear[$i] == $rowdelivery[0]){
		echo "<td width='8%' align='center'>$rowdelivery[5]</td>";
		$i++;break;
	  }else {
		echo "<td width='8%'></td>";
		$i++;
		}
	 }
	  if($masteritem != $rowdelivery[2]){
	
		}
	 
	   $masteritem = $rowdelivery[2];
	   
	}



  
  

echo "</table>";

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


if ($printaccess==1)
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;

?>