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

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

	$ReportTitle = 'List of Detailed Sales Order';	
	
	
	
print <<< HERE
<HTML>
<HEAD>
<TITLE> List of Sales Order </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");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
//maroun 30.03.2011
$querylog="SELECT print as printaccess,fi_sellingpriceinitems as withprice, ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter
		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 grid_color1,grid_color2, 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);
}


$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);

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$_SESSION['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')".
			   " where user_account='".$_SESSION['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);	

$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>$ReportTitle</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."'";      

$fromproject = "".$fromproject;
if ($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."'";
   
$sordertypelen = 0;
if (!empty($sordertype))
   $sordertypelen = strlen($sordertype);
   
if (empty($sordertype))
   $sordertype = 'null';      

print <<<HERE
</font></td>
</tr>
<tr>
   <td colspan='4' width='100%'> <hr width='100%' color='black' size='1'> </td>
</tr>
</table>


<table class="params" width="100%" border="0" align="center" cellpadding='0' cellspacing='0' >
<tr>
  <td width="10%" valign='top'><b>Client</b></td>
  <td width="5%"><b>From <br>To </b></td>
  <td width="15%">$dispfromclient <br> $disptoclient</td>
  <td width="7%" valign='top'><b>Reference</b></td>
  <td width="5%"><b>From <br>To </b></td>
  <td width="50%" colspan='2'>$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="20%" colspan='2'>$dispfromdate - $disptodate </td>
  <td width="7%"><b>Currency</b></td>
  <td width="5%">$displaycur </td>
  <td width="10%" align='center'><b>Project:</b></td>
  <td width="40%" align='left'>$dispfromproject </td>
</tr>
</table>


<table width="100%" border="0" align="center" class='master' style='table-layout:fixed;'>
<col width=60>
<col width=60>
<col width=55>
<col width=80>
<col width=150>
<col width=150>
<col width=50>
<col width=150>
<col width=100>
<col width=120>

HERE;

if ($showProjCostName == 1)
echo "<col width=120>";
	
echo "<col width=120>";
if ($showProjCostName == 1)
echo "<col width=120>";

echo "<col width=50>
	<col width=80>
	<col width=60>
	<col width=60>
	<tr>
		<th align='left'>Date</th>
		<th align='left'>D Date</th>
		<th>Ref.</th>
		<th>Client No.</th>
		<th>Client</th>
		<th>Item</th>
		<th>Qt.</th>
		<th>Description</th>
		<th>Warehouse</th>
		<th>Project</th>";
if ($showProjCostName == 1)
echo "  <th>Project Name</th>";
echo "	<th>Costcenter</th>";
if ($showProjCostName == 1)
echo "  <th>Costcenter Name</th>";

echo "	<th>Cur.</th>
		<th>Value</th>
		<th>Status</th>
		<th>Progress</th>
	</tr>";
if ($showProjCostName == 1)	 
	$colspn = 17;
else $colspn = 15;
print <<<HERE
<tr>
    <td colspan='$colspn'><hr width='100%' color='black' size='2'></td>
</tr>
 
HERE;
 
    

   
$querysalesorder= " select sorderdt.Sorder_id, date_format(sorder.Sorder_date,'%d/%m/%Y') Sorder_date, Sorder_reference, Journal_Type, ".
                  " sorder.Client_code, clients.Ledger_name , sorderdt.item_code,item_description, sorderdt.project_code, sorderdt.costcent_code, ".
				  " sorder.currency_code, if('".$date."' = 'date', date_format(sorder.Sorder_date,'%Y-%m-%d'), Sorder_reference) ord, ".
				  " if(ifNull(Sorder_disapproved,0)=1, 'Cancelled', if(ifNull(Sorder_confirmed,0)=1, 'Confirmed', 'Unconfirmed')) As Sorder_Status, ".
				  " sorder.Sorder_Remark, date_format(sorder_requestdate,'%d/%m/%Y') sorder_requestdate, sorderdt.warehouse_code, sorderdt_quantity, ".
				  " if(ifNull(Sorder.Sorder_Status,0)=0, 'Pending', if(ifNull(Sorder.Sorder_Status,0)=1, 'Delivered', 'Transferred')) As Sorder_Progress, ".
				" ifNull(if(upper($cur)='".$Base1."',SorderDt_PriceBase1,if(upper($cur)='".$Base2."',SroderDt_PriceBase2, Sorderdt_price)),0) SorderDtPrice, ".
				" ifNull(Sorderdt_discount,0) SorderDtDiscountPer, ".
				" ((ifNull(SorderDt_Quantity,0) * ifNull(if(upper($cur)='".$Base1."',SorderDt_PriceBase1,if(upper($cur)='".$Base2."',SroderDt_PriceBase2,Sorderdt_price)),0)) - ".
				" ((ifNull(SorderDt_Quantity,0) * ifNull(if(upper($cur)='".$Base1."',SorderDt_PriceBase1,if(upper($cur)='".$Base2."',SroderDt_PriceBase2,Sorderdt_price)),0) * ifNull(Sorderdt_discount,0)) / 100) ) total, ".
				" project_name, costcent_name ".
 				  " from sorderdt ".
				  " inner join sorder on sorder.sorder_id = sorderdt.sorder_id ".
				  " left join clients on sorder.Client_code = clients.ledger_number ".
				  " inner join items on items.item_code = sorderdt.item_code ".
				  " left join project on project.project_code = sorderdt.project_code ".
				  " left join costcent on costcent.Costcent_code = sorderdt.Costcent_code ";
				  				
if ($projectexist >	0 && $FI_RepRestrictProject == 1)
  $querysalesorder= $querysalesorder." 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 sorderdt.project_code like concat(Q1.project_code,'%') ";
if ($costcentexist > 0 && $FI_RepRestrictCostcenter == 1) 
		$querysalesorder = $querysalesorder." Inner join ( select distinct costcent_code from ".$_SESSION['dbLabel'].".loginstk ".
									  "			     where loginstk.user_account='".$_SESSION['useraccount']."' ".
									  "              and (costcent_code is not null and costcent_code != '') ".
								  	  "			 ) Q2 on sorderdt.costcent_code like concat(Q2.Costcent_Code,'%')";
		
$querysalesorder= $querysalesorder.  
				" where Sorder_reference between ifNull(". $fromref .",Sorder_reference) and ifNull(". $toref.",Sorder_reference) ".
				" and (($grpclient=1 and sorder.Client_code between ifNull(".$fromclient.",sorder.Client_code) and ifNull(". $toclient.",sorder.Client_code)) or ($grpclient=0 and 1 = 1)) ".
				" and ( upper(ifNull(sorder.project_code,-1)) like ifNull(upper(".$fromproject."), upper(ifNull(sorder.project_code,-1))) or 
					upper(ifNull(sorderdt.project_code,-1)) like ifNull(upper(".$fromproject."), upper(ifNull(sorderdt.project_code,-1))) )".
				" and date_format(sorder.Sorder_date,'%Y-%m-%d') between ifNull(". $fromdate.",date_format(sorder.Sorder_date,'%Y-%m-%d')) and ifNull(". $todate.",date_format(sorder.Sorder_date,'%Y-%m-%d'))  ".
				" and sorder.journal_type='SO' ".
				" and (($sordertypelen = 0 and 1 = 1) or ($sordertypelen > 0 and sorder.sorder_type in ($sordertype)))  ".
				" and (($option = 1 and ifNull(Sorder_confirmed,0)=1 and ifNull(Sorder_disapproved,0)=0) or ($option = 2 and ifNull(Sorder_confirmed,0)=0 and ifNull(Sorder_disapproved,0)=0) or ".
				"      ($option = 3 and ifNull(Sorder_disapproved,0)=1) or ($option = 4 and 1=1) ) ";

if ($salesman != ''){
	$querysalesorder= $querysalesorder." and salesman_code = '$salesman' ";
}

$querysalesorder= $querysalesorder.  
				 " order by ord, Sorder_reference ";
//				 " order by if($grpclient=1, sorder.Client_code, if($grpproject=1, sorder.project_code, if($grpstatus=1, Sorder_Status, null)) ), ord, Sorder_reference ";
				
//echo $querysalesorder;

				
$SumNetTot      = 0;
$SumGroupNetTot = 0;
$masterproject  = '';
$masterclient = '*';
$masterstatus = '';
	
//echo $querysalesorder;

$resultsalesorder=mysql_query($querysalesorder,$connection) or die(mysql_error());

$col=0;
while($rowsalesorder=mysql_fetch_row($resultsalesorder)){

    if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}
	
	echo "<tr $color>";
	echo "<td align='center' valign='top'>". $rowsalesorder[1] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[14]."</td>";
	echo "<td align='left'   valign='top'>". $rowsalesorder[2] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[4] ."</td>";
	echo "<td align='left'   valign='top'>". $rowsalesorder[5] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[6] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[16] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[13] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[15] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[8] ."</td>";
if ($showProjCostName == 1)	
	echo "<td align='center' valign='top'>". $rowsalesorder[21] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[9] ."</td>";
if ($showProjCostName == 1)	
	echo "<td align='center' valign='top'>". $rowsalesorder[22] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[10] ."</td>";
    if ($withprice == 1){
	echo "<td align='center' valign='top'>".  number_format($rowsalesorder[18],$DecimalBase) ."</td>";
	}else { echo "<td align='right'  valign='top'></td>";}
	echo "<td align='center' valign='top'>". $rowsalesorder[12] ."</td>";
	echo "<td align='center' valign='top'>". $rowsalesorder[17] ."</td>";
	echo "</tr>";
	
	$col+=1;
	echo "<tr height='8' >";
	echo "  <td colspan='$colspn'><hr width='100%' color='black' size='1'></td>";
	echo "</tr>";

}


	
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;

?>