<?php

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

include("config.inc.php");
include("Functions_Trans.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");
//amin nabil spanish
/*
mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET CHARACTER SET utf8'); 		
mysql_set_charset('utf8', $connection);
*/
if(!isset($actionmode))
$actionmode='';
 
$orderby = $_GET['orderby']; if ($orderby == '') $orderby = $_POST['orderby']; 
$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,maxperpage from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Getting ALl currencies from clients
$queryCurr ="select distinct clients.currency_code , currency_name from clients left join currency on clients.currency_code = currency.currency_code";
$resultCurrs =mysql_query($queryCurr, $connection) or die(mysql_error());



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


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


if($clientloaded=='') $clientloaded=0;
print <<<HERE

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="css/tabs.css">

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>
<script type="text/javascript" language="javascript">



function showlistclient(str){
	
    window.open('list_client.php?str='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function loadClient(){
	document.clientex.clientloaded.value='1';
    if(document.clientex.fromdate.value==''||document.clientex.todate.value==''){
        alert("Please fill From date/to date !");
        return 0;
    }
	document.clientex.action = 'Form_Clientscontract.php';
	document.clientex.submit();
}

function ClearPage(){ 
	window.location.replace('Form_Clientscontract.php');
}

function checkPer(indx){
	var txt = 'txtchecked'+indx;
	if(txt != 'txtcheckedExceededCredit'){
		if(document.getElementById(txt).checked)
			document.getElementById(txt).value=1;
		else document.getElementById(txt).value=0;
	}
}

function sendEmail(){
	
	//var fromclient = document.clientex.fromclient.value ;
	//var toclient = document.clientex.toclient.value;
	let clientloaded = document.clientex.clientloaded.value;
	document.clientex.actionmode='sendEmail'; 
	//fromclient = encodeURIComponent(fromclient);
	//toclient   = encodeURIComponent(toclient);
	if (clientloaded=='0')
	alert("Please load the clients !");
	else{
		document.clientex.action = "form_ClientsContract_action.php?actionmode=sendEmail";
		document.clientex.submit();		
	}

	
}

function ToggleAll(){
	var inputs = document.getElementsByTagName("input");
    for(var i = 0; i < inputs.length; i++) {
        if(inputs[i].type == "checkbox" &&  inputs[i].name != 'ExceededCredit') { 
             if(inputs[i].checked == false) {
				inputs[i].checked = true; 
			}
			else if(inputs[i].name != 'ExceededCredit'){
				if(inputs[i].checked == true) {
					inputs[i].checked = false; 
				 }   
			} 
			var indx = inputs[i].name.replace('txtchecked','');
			checkPer(indx);
        }  
    } 
}


function showlistsalesman(){
  var vdate, vcode; 
  if(vcode=='')
	vcode=vname;
  window.open('List_Salesmen.php?str=21','mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Clients Contract</title>

<style type="text/css">
<!--

.style1 {
	color: #FF0000;
	font-family: "Times New Roman", Times, serif;
	font-size: 20px;
}
.style8 {font-size: 11px; color: #999999; font-weight: bold; }
.style11 {font-size: 11px}
body {
	margin-top: 0px;
}
-->
</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body >
<form name='clientex' method='post'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type=hidden name='message' value='$message'>
<input type=hidden id='clientcode' name='clientcode' value='$clientcode'>
<input type=hidden id='clientloaded' name='clientloaded' value='$clientloaded'>
HERE;


echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title' style='text-transform: uppercase'>Clients Contract</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width' width='1100px'>
				
				<tr>
					<td valign='middle' height='34' style='padding-left:40px'>";
                    echo"		<button type='button' id='btnClear' name='btnClear'  onclick=\"ClearPage(); return false;\"  title='Clear Page'><span><span>Clear </span></span></button>";
                   
                    echo"		<button type='button' id='btnLoad' name='btnLoad' onclick=\"loadClient(); return false;\" title='Load Clients'><span><span>Load Clients</span></span></button>";
                    echo"		<button type='button' id='btntoggle' name='btntoggle' onclick='ToggleAll(); return false;' title='Toggle check' ><span><span>Toggle Check</span></span></button>";					
					
					$comp = explode('_',$_SESSION['dbName']);
				
                    					
							
echo"					<td align='center'>";
echo"					</td> <td  align='right'> ";
						
					if($clientloaded == "1") echo"		<button type='button' id='btnSend' name='btnSend' onclick=\"sendEmail(); return false;\" title='Send Email'><span><span>Send Email</span></span></button>";
					
echo"				</td>
					
					<td align='left' width='263px'> ";

echo"				</td>
					
					<td colspan=2 align='left'>";
echo"  				 </td>
				</tr>
			</table>
		</td>
	</tr>"; 
// var_dump($_POST);

echo"	<tr>
		<td id='content-body'>";		
echo"			<table border='0'  align='center' valign='top' width=1100px class='tablecontent'>";
echo"   			<tr bgcolor='#F7F7F7'>";
echo"   				<td valign='top'>
							<table style='table-layout:fixed' align='left' valign='top' width='1100px' border=0>
                            <col width=40>
                            <col width=130>
                            <col width=40>
                            <col width=180>
                            
                            <tr height='25px'>
                            <td colspan='2' bgcolor='#F5F5F5'><span class='style8'>Date</span></td>						
                         </tr>						
								
							
                         <tr height='15px'>
                         <td   align='left' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>
                         <td    valign='middle' bgcolor='#F5F5F5' ><span class='style8'>
                             <input type='text' name='fromdate' id='fromdate' value='$fromdate' style='width:130px;' maxlength='20'  />
                             <A HREF='#' onClick=\"displayCalendar(document.clientex.fromdate,'dd/mm/yyyy',this);  return false;\" NAME='anchor1' ID='anchor1'><img src='./img/cal.gif' border=0 align=center></A></td>

                             </td>
                             <td   align='left' bgcolor='#F5F5F5' ></td>
                          <td    valign='middle' bgcolor='#F5F5F5' ></td>
                     </tr> 	
                     <tr height='15px'>
                         <td   align='left' bgcolor='#F5F5F5' ><span class='style8'>To</span></td>
                         <td    valign='middle' bgcolor='#F5F5F5' ><span class='style8'>
                             <input type='text' name='todate' id='todate' value='$todate' style='width:130px;' maxlength='20'  />
                             <A HREF='#' onClick=\"displayCalendar(document.clientex.todate,'dd/mm/yyyy',this);  return false;\" NAME='anchor1' ID='anchor1'><img src='./img/cal.gif' border=0 align=center></A></td>

                             </td>
                             <td   align='left' bgcolor='#F5F5F5' ></td>
                          <td    valign='middle' bgcolor='#F5F5F5' ></td>
                             
                         
                     </tr>	";     
						
echo "<tr height='20px'><td></td></tr>";

if($clientloaded == "1"){
	
	echo"<tr><table  style='font-size:10px' width='1092px' cellspacing=0 width='1100px' cellpadding=0>";
echo "<col width='30px'>
<col width='100px'>
<col width='200px'>
<col width='150px'>
<col width='100px'>
<col width='100px'>
<col width='100px'>";
echo"<tr height='25px' bgcolor=#E8E8E8>";
echo"<td style='$top;$left;$right;'></td>";
echo"<td style='$top;$right;' align='center'><b>Client Code</b></td>";
echo"<td style='$top;$right;' align='center'><b>Client Name</b></td>";
echo"<td style='$top;$right;' align='center'><b>Project Code</b></td>";
echo"<td style='$top;$right;' align='center'><b>Contract Amount</b></td>";
echo"<td style='$top;$right;' align='center'><b>Start Date</b></td>";
echo"<td style='$top;$right;' align='center'><b>Contract Term </b></td>";

echo"</tr>";
$ndates = explode('/', trim($fromdate));
$fromdate = $ndates[2]."-".$ndates[1]."-".$ndates[0];

$ndates = explode('/', trim($todate));
$todate = $ndates[2]."-".$ndates[1]."-".$ndates[0];


		$query=" select clientscontract.ledger_number,ledger_name,clientscontract.project_code, amount,contract_startdate,contract_terms".
        " from clientscontract left join clients on clients.ledger_number=clientscontract.ledger_number ".
        " where contract_startdate between '$fromdate' and '$todate'";

// echo $query;	
	 
        $ct=0;
$result=mysql_query($query, $connection) or die(mysql_error());
while($row=mysql_fetch_array($result)){
	$txtchecked = 'txtchecked'.$row[0];	
    if($ct%2) $bgcolor = "#e0e0e0";
	else $bgcolor = "#f7f7f7";
	
	
    
		echo"<tr style='background-color:$bgcolor;'>";
		echo"<td align='center' style='$top;$bottom;$left;$right;'><input type='checkbox' name='$txtchecked' id='$txtchecked' value='1' onclick=\"checkPer('$row[0]');\" ></td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[0]</td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[1]</td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[2]</td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[3]</td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[4]</td>";
        echo"<td align='center' style='$top;$bottom;$right;'>$row[5]</td>";
        

	$ct++;
}

 


}

echo "</table></td></tr>


</table></td></tr>";		  
 echo" <tr height='30'><td align='center' id='content-bottom' ><div id='dispclients'></div></td></tr>";
include("footer.php");	
print <<< HERE
			</td>
		</tr>
	</table>
HERE;



print <<<HERE
</form>
</body>

HERE;



function format_currency($amount, $DecimalBase) { 

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

?>
