<?php
header("Content-type: text/html; charset=windows-1256");

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

include("Functions_Sales.php");

print <<< HERE
<HTML>
<HEAD>
<TITLE> Cash PayOuts Report </TITLE>
<style type="text/css">

table.master h1 { font-size: 26px; 
                  font-weight: bold;
				  font-style: italic;
                  font-family:Times-New-Roman;
                }				 
				
table.master th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
table.master td { font-size: 13px; 
                  font-family:Times-New-Roman;
                 }
table.detail th { font-size: 13px; 
                  font-weight: bold;
                  font-family:Times-New-Roman;
                }				 
				 
table.detail td { font-size: 12px; 
                  font-family:Times-New-Roman;
                 }				 
				 
				 
table.totals td.labels { font-size: 13px; 
                         font-weight: bold;
                         font-family:Times-New-Roman;
					  }					  
table.totals td.amounts { font-size: 13px; 
                          font-family:Times-New-Roman;
					    }
table.totals p { border-width: 1px;
 				 border-style: none none dotted none;
				 border-color: #666666;
				}	
h4 { border-top-style: double; 
	 border-top-color: black; 
	 border-top-width: 1px;
   }				
				 
table.notes th { font-size: 12px; 
                 font-weight: bold;
                 font-family:Times-New-Roman;
                }				 
table.notes td { font-size: 12px; 
                 font-family:Times-New-Roman;
                }
</style>


</HEAD>

<BODY>
HERE;

include("config.inc.php");


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


$queryusers=" Select User_Name As User_FullName ".
			" From ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."' ";
$resultusers=mysql_query($queryusers,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultusers)){
	extract($rows);
}


//Read From Setup Table							
$querysetup= " Select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, grid_color1, grid_color2, ".
             " Company_Name As CompanyName, Company_Memo As CompanyMemo, Company_City As CompanyCity, ".
			 " Company_Telephone As CompanyTelephone, Company_Fax As CompanyFax, ".
			 " Setup_VatNo, Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


//Read From CashRegister Table
$queryCashRegister=" Select ifNull(CashRegisterTTC,0) As CashRegisterTTC ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

$PCashReg = $_GET['PCashReg'];
if (empty($PCashReg)) $PCashReg = $_SESSION['cashregister'];


$sub_query = " Select ctrans_id, ctrans_timein, ctrans_timeout, currency_code as ctrans_currency, ctrans_additionalcash, ".
			 " ifnull(ctrans_totalcashstart,0) as ctrans_totalcashstart, ifnull(ctrans_totalcashclose,0) as ctrans_totalcashclose ".
			 " From cashregistertrans ".
			 " Where upper(cashregister_name) = upper('".$PCashReg."') ".
			 " And cashregistertrans.ctrans_timeout is null ";
$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_row=mysql_fetch_array($sub_result)){
	extract($sub_row);
}
if($printtoday == 1) {
	$todaydate = date('Y-m-d');
} else {		
$PDateFrom = $ctrans_timein;
$PDateTo   = Date('Y-m-d h:i:s');	
}
echo "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td align='left'>";
echo "<table border=0 width='250' cellspacing=0 cellpadding=0 class='master' style='table-layout:fixed;' >
	  <col width=100>
	  <col width=150>";
if($printtoday == 1) 
echo"	<tr><th align='left' valign='top'>Date:</th><td align='left'>". $todaydate ."</td></tr>";
else	
echo"	<tr><th align='left' valign='top'>Date From/To:</th><td align='left'>". $PDateFrom ." <br> ". $PDateTo ."</td></tr>";
echo"	<tr><th align='left' style='padding-bottom:5px; border-bottom:1px dashed black;'>CashRegister:</th><td align='left' style='padding-bottom:5px; border-bottom:1px dashed black;'>". $PCashReg ."</td></tr>";	  
echo "</table></td></tr>";
echo "<tr><td align='left'>";
echo "<table border=0 width='250' cellspacing=0 cellpadding=0 class='detail' style='table-layout:fixed;' >
	  <col width=150>
	  <col width=100>";
echo"	<tr><th align='left' style='padding-bottom:5px; border-bottom:1px dashed black;'>Description</th><th align='center' style='padding-bottom:5px; border-bottom:1px dashed black;'>Amount $ctrans_currency</th></tr>";	  	  
	  
$TotalCashPayOut = 0;

$sub_query1 = " Select ctransdt_description, ifNull(ctransdt_amountb1,0) As ctransdt_amountb1, ifNull(ctransdt_amountb2,0) As ctransdt_amountb2 ".
			  " From cashregistertransdt ".
			  " Where ctrans_id = '".$ctrans_id."' ".
			  " And upper(ctransdt_type) = 'OUT' ";
if($printtoday == 1) 
	$sub_query1 = $sub_query1." and date_format(ctransdt_date,'%Y-%m-%d') = '$todaydate'"; 

$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
while($sub_row1=mysql_fetch_array($sub_result1)){
	extract($sub_row1);
	
	if ($ctrans_currency == $Base1) {
		$CashPayOut    = $ctransdt_amountb1;
	}	
	else {
		$CashPayOut    = $ctransdt_amountb2;
	}
	$TotalCashPayOut = $TotalCashPayOut + $CashPayOut;

	echo"<tr>
			<td align='left' valign='top'>".$ctransdt_description."</td>
			<td align='right' valign='top'>". number_format($CashPayOut, $DecimalBase) ."</td>
		</tr>";

}
		
echo"	<tr><th align='left' style='padding-top:5px; border-top:1px dashed black;'>Total</th><th align='right' style='padding-top:5px; border-top:1px dashed black;'>". number_format($TotalCashPayOut, $DecimalBase) ."</th></tr>";	  	  
echo "</table></td></tr>";
echo "</table>";



function current_server_time($connection){

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

  return $now[0];
}

function formatDateClosing($data){
	return substr($data, 0, 4) ."-". substr($data, 5, 2) . "-". substr($data, 8, 2) . substr($data, 10, 9);
}

function format_currency($amount, $DecimalBase) { 

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

function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result=mysql_query($query,$connection);
	$row=mysql_fetch_row($result);
	//echo $query;
	Return	$row[0];

}




print <<< HERE
<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

<script type="text/javascript" language="javascript">
if (typeof(window.print) != 'undefined')
{
    window.print();
	
	window.setTimeout(function() {
		window.close();
	}, 500 );
}

</script>


</BODY>
</HTML>
HERE;

?>

