<?php
session_start();

include("config.inc.php");
include("Functions_Sales.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");


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


$frm	   	   = $_GET['frm']; if ($frm == ""){$frm = $_POST["frm"];}
$item 	   	   = $_GET['item'];if ($item == ""){$item = $_POST["item"];}
$message       = $_GET['message'];if ($message == ""){$message = $_POST["message"];}
$startrange    = $_GET['startrange'];if ($startrange == ""){$startrange = $_POST["startrange"];}
$PActionMode   = $_GET['PActionMode'];if ($PActionMode == ""){$PActionMode = $_POST["PActionMode"];}

$perpage    = $_POST["perpage"]; if ($perpage == ""){$perpage = $_GET["perpage"];}

$p_name    = $_POST["p_name"]; if ($p_name == ""){$p_name = $_GET["p_name"];}
$p_cashregister    = $_POST["p_cashregister"]; if ($p_cashregister == ""){$p_cashregister = $_GET["p_cashregister"];}
$p_subgroup  = $_POST["p_subgroup"]; if ($p_subgroup == ""){$p_subgroup = $_GET["p_subgroup"];}

   
print <<<HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}

print <<<HERE
<title>Setup</title>
<script type="text/javascript" language="javascript">

function SelectRecord(value1,value2,value3){

	vFrm  = document.form1.frm.value;
	vItem = document.form1.item.value;

	if(vFrm=='1'){
		opener.document.retailpay.txtRetCustomerCode.value = value1;
		opener.document.retailpay.txtRetCustomerName.value = value2;
		opener.document.retailpay.txtRetRemark.value = value3;
		opener.document.retailpay.txtRetCustomerName.focus();
		opener.document.retailpay.submit();
	}
	else if(vFrm=='2'){
		if(vItem==1){
			opener.document.form1.fromcustomer.value = value1;
			opener.document.form1.fromcustomerdesc.value = value2;
		} else {
			opener.document.form1.tocustomer.value = value1;
			opener.document.form1.tocustomerdesc.value = value2;
		}	
	}
	

	self.close();
}

function ClearForm()
{
	document.form1.PLedgerNo.value='';
	document.form1.startrange.value = 0;
	document.form1.submit();
}

function AddRecord() {
	var x = document.form1;
	
	x.PLedgerNo.value='';
	x.PActionMode.value='Add';
	x.submit();
}

function CancelRecord() {
	var x = document.form1;
	
	x.PActionMode.value='';
	
	
	x.p_name.value='';
	x.p_id.value='';
	x.p_cashregister.value='';
	x.p_subgroup.value='';
	
	
	x.submit();
}
function SubmitForm()
{	

  //document.search.keyword.value='';
  document.form1.action='Form_CashRegisterSetup.php?startrange=0';
  document.form1.submit();

}

function clearform()
{
  document.form1.keyword.value='';

  document.form1.action='Form_CashRegisterSetup.php';
  document.form1.submit();

}


function SubmitRecord() {
	var x = document.form1;
	var result = CheckFields();
	
	if (result == 1) {
		x.PActionMode.value='Submit';
		x.submit();
	}
}

function UpdateRecord(pid) {
	var x = document.form1;
	
	x.PActionMode.value='Edit';
	x.p_id.value=pid;
	x.submit();

}

function CheckFields(){
  
    var x = document.form1;
	
	var vCode = x.p_name.value;
	var vName = x.p_subgroup.value;
	
	if (vCode=='' )
	{
		alert("Name is mandatory!");
		x.p_name.focus();
		return 0;
	}
	else if (vName=='')
	{
		alert("Subgroup is mandatory!");
		x.p_subgroup.focus();
		return 0;
	}
	
	
	else
	return 1 ;	
}


function SetNextPage(startrange, perpage){
	x=document.form1;
	
	x.startrange.value = startrange;
	x.action='List_Customers.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function SetPrvPage(startrange, perpage){
	x=document.form1;
	
	x.startrange.value = startrange;
	x.action='List_Customers.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function OpenKeyboard(field){
	VirtualKeyboard.attachInput(field);
	VirtualKeyboard.open(field,'td');
}
</script>



</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input type='hidden' name='PActionMode' id='PActionMode'  value='$PActionMode' >
<input type='hidden' name='p_id' id='p_id'  value='$p_id' >
<input type='hidden' name='item' id='item' value='$item' />
<input type='hidden' name='frm' id='frm' value='$frm' />
<input type='hidden' name='message' value='$message' size='200'>
<input type='hidden' name='startrange' value='$startrange'>
			
			

	
HERE;
//echo "<table><tr><td>":
echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:650px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Setup
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr><tr height = 400 valign = top><td id='search-content1'></td><td>
";

echo '  <table width="680" border="0" cellspacing="1" align="center">';

if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 10;

if ($PActionMode == 'Submit') {
	$ErrorExists = 0;
	
	if ($p_id == '') {
		if ($p_name != '') {
			$sub_query = " Select Count(p_id) From ".$_SESSION['dbLabel'].".printersubgroup ".
						 " Where p_id = '".$p_id."'";
			$sub_result = selectData($connection, $sub_query);
			if ($sub_result[0] != 0) $ErrorExists = 1;
		}
		
		if ($ErrorExists == 1) {
			$PActionMode = 'Add';
			$message = 'Insert not allowed. Duplicate Printer: '.$p_name;
		}	
			
		
		
		if ($p_name != "" && $ErrorExists == 0){
		
			fbegin();
			$query = " INSERT INTO ".$_SESSION['dbLabel'].".printersubgroup (p_name, p_subgroup, p_cashregister ) ".
					 " VALUES ( '".$p_name."', '".ereg_replace("'", "\'", trim($p_subgroup))."', '".$p_cashregister."'  )";
						 
				 
			$result=mysql_query($query,$connection);
			//echo $query."<br>";exit;	
			
			if ($result){
				$PActionMode = '';
				$PLedgerNo   = $p_name;
				fcommit();
				
				header ("Location: ./Form_CashRegisterSetup.php?frm=1&p_name=".$p_name."&p_subgroup=".$p_subgroup."&p_cashregister=".$p_cashregister."&message=".$message);	
			}else{
				$PActionMode = 'Add';
				$message  = mysql_error(); 
				frollback();
			}
	
		}
	} else {
		fbegin();
		$query = " UPDATE ".$_SESSION['dbLabel'].".printersubgroup SET ".
		         " p_name = '".ereg_replace("'", "\'", trim($p_name))."', p_subgroup = '".$p_subgroup."', p_cashregister = '".$p_cashregister."' 
							WHERE p_id = '".$p_id."'";
						 
				 
		$result=mysql_query($query,$connection);
		//echo $query."<br>";exit;	
			
		if ($result){
			$PActionMode = '';
			
			$PLedgerNo   = $p_name;
			fcommit();
				
			header ("Location: ./Form_CashRegisterSetup.php?frm=1&p_name=".$p_name."&p_subgroup=".$p_subgroup."&p_cashregister=".$p_cashregister."&message=".$message);
		}else{
			$PActionMode = 'Edit';
			$message  = mysql_error(); 
			frollback();
		}
	}	
	
}



if ($PActionMode == 'Add'  || $PActionMode == 'Edit') {

$sub_query= " Select p_id, p_name,p_subgroup,p_cashregister ".
			" From ".$_SESSION['dbLabel'].".printersubgroup ".
			" Where (p_name like '%".$PLedgerNo."%' or p_subgroup like '%".$PLedgerNo."%' or p_cashregister like '%".$PLedgerNo."%' ) ".
			" Order By Lpad(p_name,10,'0') ";
			
	if ($p_id != '') {
		$sub_query = " Select  p_name,p_subgroup,p_cashregister   ".
					 " From  ".$_SESSION['dbLabel'].".printersubgroup ".
					 " Where p_id = '".$p_id."'";
					 
		$sub_result=selectData($connection, $sub_query);
		
		$p_name    = $sub_result[0];
		$p_subgroup    = $sub_result[1];
		$p_cashregister  = $sub_result[2];
		
		$CodeMode = ' readonly ';
	}
	else {
		/*$sub_query = " Select ifNull(Max(Lpad(Customer_Code,10,'0')),0) + 1 ".
					 " From Customer ";
		$sub_result = selectData($connection, $sub_query);
		$p_name    = $sub_result[0];
		*/
	}
	
	
	
	echo"<tr><td colspan='4'>";
	echo"  <table width='680' border='0' cellspacing='1' align='left'>";
	echo"	<tr bgcolor='#F7F7F7'>
			  <td height='20' colspan='2' align='center'><font size='1' color='#FF0000' face='Verdana'><b>$message</b></font></td>
			</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>Printer</span></td>";
	echo"	  <td width='500'><input type='text' name='p_name' id='p_name' value='$p_name' style='width:350px;' maxlength='200'    /></td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>Subgroup</span></td>";
	echo"	  <td width='500'><input type='text' name='p_subgroup' id='p_subgroup' value='".htmlspecialchars($p_subgroup, ENT_QUOTES)."'  style='width:350px;' maxlength='200'  /></td>";
	echo"	</tr>";
	echo"	<tr bgcolor='#F7F7F7'>";
	echo"	  <td width='100'><span class='style5'>CashRegister</span></td>";
	echo"	  <td width='500'>
					<input type='text' name='p_cashregister' id='p_cashregister' value='".htmlspecialchars($p_cashregister, ENT_QUOTES)."'  style='width:163px;' maxlength='200'  />
			  </td>";
	echo"	</tr>";
	
	echo"	<tr bgcolor='#DDDDDD'>";
	echo"	<td colspan='2' align='center'>";
	
	echo "<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitRecord(); return false;' title='Submit'><span><span>Submit</span></span></button>";
	echo "<button type='button' id='btnClear' name='btnClear' onclick='CancelRecord(); return false;' title='Cancel'><span><span>Cancel</span></span></button>";

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

}

if ($PActionMode == '' or  $PActionMode=='Submit') {
echo"<tr><td colspan=5><table width=100%>";
	echo"<tr>
		  <td bgcolor='#DDDDDD' colspan='2'><span class='style4'>Search by <br> Code / Name</span></td>
		  <td bgcolor='#DDDDDD' colspan='2'><input type='text' name='PLedgerNo' id = 'keyword' value='$PLedgerNo'  size='30'  />
			<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm();return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='clearform();return false;' title='Cancel'><span><span>Cancel</span></span></button>
			<button type='button' id='btnAdd' name='btnAdd' onclick='AddRecord();return false;' title='Add'><span><span>Add</span></span></button>

			<span class='style4'>Per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='ClearForm();'>";
              
			
			if ($perpage==10)
				$s10 = 'selected';
			else if ($perpage==20)
				$s20 = 'selected';	
			else if ($perpage==30)
				$s30 = 'selected';	
			else if ($perpage==40)
				$s40 = 'selected';
			else if ($perpage==50)
				$s50 = 'selected';

			
				
        echo'<option '.$s10.' value="10">10</option>
			 <option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span>';	
			 
	echo"  </td>
		</tr>";
		echo"</table></td></tr>";
}


echo"<tr bgcolor='#E5E5E5'>
      <td width='20'>&nbsp;</td>
      <td width='250'><span class='style4'>Printer </span></td>
      <td width='200'><span class='style4'>Subgroup </span></td>
	  <td width='150'><span class='style4'>CashRegister</span></td>
    </tr>";


			
$sub_query= " Select p_id, p_name,p_subgroup,p_cashregister ".
			" From ".$_SESSION['dbLabel'].".printersubgroup ".
			" Where (p_name like '%".$PLedgerNo."%' or p_subgroup like '%".$PLedgerNo."%' or p_cashregister like '%".$PLedgerNo."%' ) ".
			" Order By Lpad(p_name,10,'0') ";
		

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($sub_result);

		
$sub_query = $sub_query." limit ".$startrange.",".$perpage;

$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
while($sub_rows=mysql_fetch_array($sub_result)){
	
	extract($sub_rows);

	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";		

    $p_name=ereg_replace("'","%27",$p_name);

	echo"<tr $Color>";
	echo " <td align='center' valign='center'><span class='style15'>
			<img src='img/button_edit.png'  onMouseOver=\"style.cursor='pointer'\" onclick=UpdateRecord('".$p_id."') title='Edit Record'  ></span></td>";
	echo"  <td align='left'><span class='style5'>$p_name</span></a></td>";
	echo"  <td align='left'><span class='style5'>$p_subgroup</span></td>";
	echo"  <td align='left'><span class='style5'>$p_cashregister</span></td>";
    echo"</tr>";
	
}	

echo"<tr><td colspan='4'>&nbsp;</td></tr>
	 <tr height='30'><td colspan=4><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
$diff= $startrange - $perpage;
		
if (($diff) >= 0 ){
	$prvstartrange = $startrange - $perpage;
	echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='pointer'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
}
else
	echo "&nbsp;";
		
echo"</td><td width='150' align='center'>";
		
if ($startrange <= $totalrows){
	$startrange = $startrange + $perpage;
	if  ($startrange < $totalrows)
		echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='pointer'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
} else
	echo "&nbsp;";
			
echo"</td><td width='170'>&nbsp;</td></tr>";
echo"</table>";
echo"</td></tr>";

function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}

echo"<tr height='50px'><td align=left colspan='4'>";

echo"</td></tr>";

print <<<HERE
  </table></td><td id='search-content3' ></td></tr>
  <tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:600px;' valign='top'>
HERE;
		//include("toolbar.php");
		echo"</td>
		<td id='search-bottom3' style='height:34px; width:31px;'>";


	
print <<<HERE
&nbsp;</td></tr>
		
   </table>
</form>
</body>
</html>

HERE;
?>