
<?php

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

if($prtexcel ==1){
	$filename = "Exp_SalesHistory" . date('Ymd') . ".xls";
	header("Content-Disposition: attachment; filename=\"$filename\"");
	header("Content-Type: application/vnd.ms-excel");
} 

if($export ==1){
}
else  

print <<< HERE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<TITLE> Client Sales Report </TITLE>
</HEAD>


HERE;

$top='border-top:1px solid #000000';
$bottom='border-bottom:1px solid #000000';
$left='border-left:1px solid #000000';
$right='border-right:1px solid #000000';

include("config.inc.php");
include('function.inc.php');

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

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


$querylog="SELECT print as printaccess from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['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, ".
			" ifnull(Setup_Tax1Active,0) as Setup_Tax1Active, ifnull(Setup_Tax2Active,0) as Setup_Tax2Active,  ".
			" ifnull(Setup_Tax3Active,0) as Setup_Tax3Active, ifnull(Setup_Tax4Active,0) as Setup_Tax4Active,  ".
			" ifnull(Setup_Tax5Active,0) as Setup_Tax5Active, Setup_Tax1Name, Setup_Tax2Name, Setup_Tax3Name, ".
			" Setup_Tax4Name, Setup_Tax5Name ".
			" from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
  extract($rows);
} 

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter, ".
		  " ifNull(FI_RepRestrictWarehouse,0) As FI_RepRestrictWarehouse, ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Grn,0) As Module_Grn, ifNull(Module_Delivery,0) As Module_Delivery, ".
			 " ifNull(Module_AdditionalTaxes,0) As Module_AdditionalTaxes
			  , ifNull(Module_Transportation,0) As Module_Transportation
			 From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

if($Module_Transportation==0)
	$PrtTransp = 0;

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%') ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " 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 ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%') ".
			   " where user_account='".$HTTP_SESSION_VARS['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);		

$querywh=" select distinct warehouse.warehouse_code, warehouse_name ".
         " from warehouse where warehouse_code in ( select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
		 "                                          where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
		 "                                          and (loginstk.warehouse_code is not null and loginstk.warehouse_code != '') ) ".
		 " order by warehouse.warehouse_code";
$resultwh = mysql_query($querywh,$connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist  = mysql_num_rows($resultgroup);

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


/*
echo"
<TABLE CELLPADDING=6>
<THEAD>";
*/

print <<< HERE
<table width="100%" style='margin-left: 0.2cm;' border="0" align="left" >
HERE;


echo "<thead><tr><td align='left'>";
	
$PageLayout='landscape';
ShowReportHeader('Sales History',$PageLayout, $connection);
	
echo"</td></tr></thead><tbody>";

echo"<tr><td><b>From Date:</b>	$fromdate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>To Date:</b>	$todate</td></tr>";

$queryoptions = "Select Ledger_Name From Suppliers Where Ledger_Number = '".$fromsupplier."'";
$resultoptions = mysql_query($queryoptions,$connection) or die(mysql_error());
$rowoptions=mysql_fetch_row($resultoptions);
$fromsuppliername = $rowoptions[0];

$queryoptions = "Select Ledger_Name From Suppliers Where Ledger_Number = '".$tosupplier."'";
$resultoptions = mysql_query($queryoptions,$connection) or die(mysql_error());
$rowoptions=mysql_fetch_row($resultoptions);
$tosuppliername = $rowoptions[0];

if ($curr == 'currency1'){
   $curr='null';  
   $displaycur = '';
}   
else {
   $displaycur = $curr;
   $curr="'".$curr."'";
}   

$subproj 	 = ereg_replace('-','%',$subproj);
$subcostcent = ereg_replace('-','%',$subcostcent);

$SelectedOptions = "Filter by: <br>";
if ($fromdate != '')      $SelectedOptions .= "-> Date From: ". $fromdate . "  To: ". $todate ."<br>";	
if ($displaycur != '')    $SelectedOptions .= "-> Currency: ". $displaycur ."<br>";	
if ($fromitem != '')      $SelectedOptions .= "-> Item From: ". $fromitem . "  To: ". $toitem ."<br>";
if ($fromgroup != '')     $SelectedOptions .= "-> Group From: ". $fromgroup . "  To: ". $togroup ."<br>";
if ($fromsubgroup != '')  $SelectedOptions .= "-> SubGroup From: ". $fromsubgroup . "  To: ". $tosubgroup ."<br>";
if ($fromcategory != '')  $SelectedOptions .= "-> Category From: ". $fromcategory . "  To: ". $tocategory ."<br>";
if ($fromproject != '')   $SelectedOptions .= "-> Project From: ". $fromproject . "<br>";
if ($fromcostcent != '')  $SelectedOptions .= "-> Costcent From: ". $fromcostcent . "<br>";
if ($fromclient != '')    $SelectedOptions .= "-> Client From: ". $fromclient . "  To: ". $toclient ."<br>";
if ($fromsupplier != '')  $SelectedOptions .= "-> Supplier From: ". $fromsupplier . " (". $fromsuppliername .") To: ". $tosupplier . " (". $tosuppliername .")<br>";
if ($fromsalesman != '')  $SelectedOptions .= "-> Salesman From: ". $fromsalesman . "  To: ". $tosalesman ."<br>";
if ($fromwarehouse != '') $SelectedOptions .= "-> Warehouse From: ". $fromwarehouse . "<br>";
if ($fromarea != '') 	  $SelectedOptions .= "-> Area From: ". $fromarea . "<br>";
if ($cltrating != '')	  $SelectedOptions .= "-> Client Rating: ". $cltrating . "<br>";
if ($subproj != '') 	  $SelectedOptions .= "-> Project Include: ". $subproj . "<br>";
if ($subcostcent != '')	  $SelectedOptions .= "-> Costcent Include: ". $subcostcent . "<br>";

$fromdate1=$fromdate;
$todate1=$todate;
  
if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  
   
$fromitem      = GetSQLValueString($fromitem, 'text');
$toitem        = GetSQLValueString($toitem, 'text');
$fromproject   = GetSQLValueString($fromproject, 'text');
$fromcostcent  = GetSQLValueString($fromcostcent, 'text');
$fromgroup     = GetSQLValueString($fromgroup, 'text');
$togroup       = GetSQLValueString($togroup, 'text');
$fromsubgroup  = GetSQLValueString($fromsubgroup, 'text');
$tosubgroup    = GetSQLValueString($tosubgroup, 'text');
$fromcategory  = GetSQLValueString($fromcategory, 'text');
$tocategory    = GetSQLValueString($tocategory, 'text');
$fromwarehouse = GetSQLValueString($fromwarehouse, 'text');
$fromarea      = GetSQLValueString($fromarea, 'text');
$fromclient    = GetSQLValueString($fromclient, 'text');
$toclient      = GetSQLValueString($toclient, 'text');
$fromsupplier  = GetSQLValueString($fromsupplier, 'text');
$tosupplier    = GetSQLValueString($tosupplier, 'text');
$fromsalesman  = GetSQLValueString($fromsalesman, 'text');
$tosalesman    = GetSQLValueString($tosalesman, 'text');
$fromdate  	   = GetSQLValueString($fromdate, 'date');
$todate        = GetSQLValueString($todate, 'date');     
$subproj 	   = GetSQLValueString($subproj, 'text');
$subcostcent   = GetSQLValueString($subcostcent, 'text');        
$cltrating     = GetSQLValueString($cltrating, 'text');

if(empty($showweight))   $showweight = 0;
if(empty($showsales))    $showsales = 0;
if(empty($showretsales)) $showretsales = 0;
if(empty($showunit2qty)) $showunit2qty = 0;

if(empty($prtamt)) 	  $prtamt = 0;
if(empty($prtvat)) 	  $prtvat = 0;
if(empty($prtamtvat)) $prtamtvat = 0;
if(empty($showsellprice)) $showsellprice = 0;
if(empty($sellprice)) $sellprice = 1;

if(empty($activeclt))  $activeclt = 1;
if(empty($activeitem)) $activeitem = 1;

	echo"<table border=0  cellspacing=0 cellpadding=0   style='font-size:10px;margin-left: 0.2cm;'>";
	
	$fromdate2 = explodeword($fromdate1);
	$todate2= explodeword($todate1);
 
 
	$fromdate1 = explodeword($fromdate1);
	$todate1= explodeword($todate1);
 
	
 
	
	
	
	$i=0;
	$dateisINTIME = strtotime($fromdate1);
	$dateistoINTIME = strtotime($todate1);
	while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
	
			if($day=='Monday')	$Monday[] = date('d', strtotime($fromdate1));
			if($day=='Tuesday')	$Tuesday[] = date('d', strtotime($fromdate1));
			if($day=='Wednesday')	$Wednesday[] = date('d', strtotime($fromdate1));
			if($day=='Thursday')	$Thursday[] = date('d', strtotime($fromdate1));
			if($day=='Friday')	$Friday[] = date('d', strtotime($fromdate1));
			if($day=='Saturday')	$Saturday[] = date('d', strtotime($fromdate1));
			if($day=='Sunday')	$Sunday[] = date('d', strtotime($fromdate1));
			
			
			
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	$i+=1;
	}
if($activeitem==3) $mult = 2; else $mult = 1;
$col1 = count($Monday) * $mult;
$col2 = count($Tuesday) * $mult;
$col3 = count($Wednesday) * $mult;
$col4 = count($Thursday) * $mult;
$col5 = count($Friday) * $mult;
$col6 = count($Saturday) * $mult;
$col7 = count($Sunday) * $mult;

echo"<tr><td width=75px align=center><b></b></td><td></td>";	
if(count($Monday)>0) echo"<td  style='$top;$left' colspan=".$col1."><b>Monday</b></td>";
if(count($Tuesday)>0) echo"<td  style='$top;$left' colspan=".$col2."><b>Tuesday</b></td>";
if(count($Wednesday)>0) echo"<td  style='$top;$left' colspan=".$col3."><b>Wednesday</b></td>";
if(count($Thursday)>0) echo"<td style='$top;$left'  colspan=".$col4."><b>Thursday</b></td>";
if(count($Friday)>0) echo"<td style='$top;$left'  colspan=".$col5."><b>Friday</b></td>";
if(count($Saturday)>0) echo"<td  style='$top;$left' colspan=".$col6."><b>Saturday</b></td>";
if(count($Sunday)>0) echo"<td  style='$top;$left' colspan=".$col7."><b>Sunday</b></td>";



echo"</tr>";
echo"<tr><td colspan=2 width=75px style='$top;$left'  align=center><b>Date</b></td>";		



 $length = count($Monday)  ;
 
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left' colspan=$mult  align=center><b>$Monday[$i]</b></td>";
}
$totallength+=$length;
 $length = count($Tuesday)  ;
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left'  colspan=$mult  align=center><b>$Tuesday[$i]</td>";
}
$totallength+=$length;		
 $length = count($Wednesday) ;
for ($i = 0; $i < $length; $i++) {
  echo "<td width=75px  style='$top;$left'   colspan=$mult align=center><b>$Wednesday[$i]</td>";
}
	
$totallength+=$length;	
 $length = count($Thursday);
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left'  colspan=$mult  align=center><b>$Thursday[$i]</td>";
}
$totallength+=$length;	
 $length = count($Friday);
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left'  colspan=$mult  align=center><b>$Friday[$i]</td>";
}
$totallength+=$length;	
 $length = count($Saturday);
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left'  colspan=$mult  align=center><b>$Saturday[$i]</td>";
}
 $totallength+=$length;	
 $length = count($Sunday);
for ($i = 0; $i < $length; $i++) {
  echo "<td  width=75px style='$top;$left' colspan=$mult  align=center><b>$Sunday[$i]</td>";
}
	
	
	echo"</tr>";
if($activeitem==3){
 echo"<tr><td></td><td></td>";
	for ($i = 0; $i < $totallength; $i++) {
	  echo "<td  width=75px style='$top;$left'    align=center><b>Tot.Qty.</td>";
	  echo "<td  width=75px style='$top;$left'    align=center><b>Tot.Price</td>";
	}
echo"</tr>";		
}
if($activeitem==1){
 echo"<tr><td></td><td></td>";
	for ($i = 0; $i < $totallength; $i++) {
	  echo "<td  width=75px style='$top;$left'    align=center><b>Tot.Qty.</td>";
	}
echo"</tr>";		
}
if($activeitem==2){
 echo"<tr><td></td><td></td>";
	for ($i = 0; $i < $totallength; $i++) {
	  echo "<td  width=75px style='$top;$left'    align=center><b>Tot.Price</td>";
	}
echo"</tr>";		
}

$querydelivery1 = "	 Select distinct Items.item_code,Items.Item_description
				 From DeliveryDt,Items 
				 where Delivery_Date between $fromdate and $todate
				 and Items.Item_code=Deliverydt.Item_Code
				 Group by Items.Item_Code,date_format(Delivery_Date,'%d/%m/%Y')
				 Order by Items.Item_Code,date_format(Delivery_Date,'%W'),Delivery_Date";

$querydelivery = "	 Select Items.item_code,Sum(DeliveryDt_Quantity)
				 ,Sum(Round(((DeliveryDt_Quantity*Deliverydt.SaleDt_PriceBase1)-((DeliveryDt_Quantity*Deliverydt.DeliveryDt_PriceBase1*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") )As Total
				 ,date_format(Delivery_Date,'%Y-%m-%d')
				 ,Item_description
				 From Deliverydt,Items 
				 where Delivery_Date between $fromdate and $todate
				 and Items.Item_code=Deliverydt.Item_Code
				 Group by Items.Item_Code,date_format(Delivery_Date,'%d/%m/%Y')
				 Order by Items.Item_Code,date_format(Delivery_Date,'%W'),Delivery_Date";

$querysales1 = "	 Select distinct Items.item_code,Items.Item_description
				 From SalesDt,Items 
				 where Sale_Date between $fromdate and $todate
				 and Items.Item_code=Salesdt.Item_Code
				 Group by Items.Item_Code,date_format(Sale_Date,'%d/%m/%Y')
				 Order by Items.Item_Code,date_format(sale_date,'%W'),sale_date";

$querysales = "	 Select Items.item_code,Sum(SaleDt_Quantity)
				 ,Sum(Round(((SaleDt_Quantity*SalesDt.SaleDt_PriceBase1)-((SaleDt_Quantity*SalesDt.SaleDt_PriceBase1*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") )As Total
				 ,date_format(Sale_Date,'%Y-%m-%d')
				 ,Item_description
				 From SalesDt,Items 
				 where Sale_Date between $fromdate and $todate
				 and Items.Item_code=Salesdt.Item_Code
				 Group by Items.Item_Code,date_format(Sale_Date,'%d/%m/%Y')
				 Order by Items.Item_Code,date_format(sale_date,'%W'),sale_date";


 if($Module_Delivery == 1 )
  $result=mysql_query($querysales,$connection) or die(mysql_error());
else
  $result=mysql_query($querydelivery,$connection) or die(mysql_error());

if($Module_Delivery == 1 )
  $result1=mysql_query($querysales1,$connection) or die(mysql_error());
else
  $result1=mysql_query($querydelivery1,$connection) or die(mysql_error());


	$i=1;
	while($rows=mysql_fetch_array($result)){
	  $ItemCode = $rows[0];
	  $Qty = $rows[1];
	  $TotalPrice = number_format($rows[2],$DecimalBase);
	  $Sale_Date = $rows[3];
	  $ItemDesc = $rows[4];
	  
	  $arr[$ItemCode][$Sale_Date]['QTY'] = $Qty;
	  $arr[$ItemCode][$Sale_Date]['TotalPrice'] = $TotalPrice;
 
	}		





$col=0;	
while($rows=mysql_fetch_array($result1)){

	if($col%2==0){
		$color = "style='background-color:".$report_color1."'";
	}else{
		$color = "style='background-color:".$report_color2."'";
	}

echo"<tr $color><td  style='$top;$left' width=130px>".$rows[0]."</td><td  style='$top;$left' width=300>".$rows[1]."</td>";


$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		
		
		
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Monday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td  bgcolor=#E8E8E8 style='$top;$left' width=50 align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		
		if($day=='Monday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td  bgcolor=#E8E8E8 style='$top;$left' width=50 align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Tuesday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		if($day=='Tuesday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
	
$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Wednesday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		
		if($day=='Wednesday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
	
	$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Thursday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		
		if($day=='Thursday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
	

	$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Friday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		
			if($day=='Friday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
	
	
	$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Saturday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td width=50 style='$top;$left'  align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		if($day=='Saturday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td width=50 style='$top;$left'  align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
		$fromdate1=$fromdate2;
$dateisINTIME = strtotime($fromdate1);
$dateistoINTIME = strtotime($todate1);
while ($dateisINTIME <= $dateistoINTIME) {
		$day = date('l', strtotime($fromdate1));
						
		if($day=='Sunday' and ($activeitem==1 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['QTY']=='') $arr[$rows[0]][$fromdate1]['QTY']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['QTY']."</td>";
		}	
		if($day=='Sunday' and ($activeitem==2 or $activeitem==3)){
			if($arr[$rows[0]][$fromdate1]['TotalPrice']=='') $arr[$rows[0]][$fromdate1]['TotalPrice']=0;
			echo  "<td  bgcolor=#E8E8E8 width=50  style='$top;$left' align=center>";
			echo $arr[$rows[0]][$fromdate1]['TotalPrice']."</td>";
		}	
		
		
	$fromdate1 = date('Y-m-d', strtotime($fromdate1. ' + 1 days'));
	$dateisINTIME = strtotime($fromdate1);
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	$col+=1;
	$i+=1;
echo"</tr>";

}

echo"<tr><td style='$top' colspan=50>&nbsp;</td></tr>";


function format_currency($amount, $DecimalBase) { 

    if($amount < 0) 
        return "(".number_format(abs($amount),$DecimalBase).")"; 
 
    else return number_format($amount, $DecimalBase); 
} 

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p'),dayname(now()) ";
  $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;


?>