<?php

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


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

$querylog = " select ifNull(FI_ProductionDel,0) As FI_ProductionDel, ifNull(FI_ProductionMod,0) As FI_ProductionMod, ifNull(FI_ProductionApp,0) As FI_ProductionApp, " .
	" 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, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultlog)) {
	extract($rows);
}

//Read From Module Table
$querymodule = " Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, " .
	" ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent, " .
	" ifNull(Module_PostToJournal,0) As Module_PostToJournal From Module";
$resultmodule = mysql_query($querymodule, $connection) or die(mysql_error());
while ($rowmodule = mysql_fetch_array($resultmodule)) {
	extract($rowmodule);
}


//Access Mode On Insert/Edit/Delete
$PrdDelMode = 1;
$PrdEditMode = 1;
$PrdPrevMode = 1;

$PrevHint = '';
$PrevPrice = '';
$EditHint = '';
$DelHint = '';

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

$MainCompanyName = explode('_', $_SESSION['dbName']);

$queryperiod = " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays, ifNull(AllowLockedPeriod,0) As AllowLockedPeriod, " .
	" LockedPeriod As LockedPeriodDate, LockedPeriodDays As LockedPeriodDays " .
	" From " . $_SESSION['dbLabel'] . ".ListUserDir  " .
	" Where Usercode='" . $_SESSION['useraccount'] . "' " .
	" And Upper(CompanyName) = Upper('" . $MainCompanyName[0] . "') " .
	" And Upper(Diryear) = Upper('" . $MainCompanyName[1] . "') ";

$resultperiod = mysql_query($queryperiod, $connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while ($rows = mysql_fetch_array($resultperiod)) {
	extract($rows);
}

//echo $queryperiod;	

$queryproject = " select distinct project.project_code, project_name " .
	" from project left join " . $_SESSION['dbLabel'] . ".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) " .
	" where user_account='" . $_SESSION['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 " . $_SESSION['dbLabel'] . ".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.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);

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


$querywarehouse = " select distinct warehouse.warehouse_code, warehouse_name " .
	" from warehouse where warehouse_code in (select warehouse_code from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "') " .
	" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse, $connection) or die(mysql_error());
$whexist  = mysql_num_rows($resultwh);


//Get Default Project
$querydflproject = "select project_code as defaultproject from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_project=1 ";
$resultdflproject = mysql_query($querydflproject, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflproject)) {
	extract($rows);
}

//Get Default Coscenter
$querydflcostcent = "select costcent_code as defaultcostcent from " . $_SESSION['dbLabel'] . ".loginstk where user_account='" . $_SESSION['useraccount'] . "' and default_costcenter=1 ";
$resultdflcostcent = mysql_query($querydflcostcent, $connection) or die(mysql_error());
while ($rows = mysql_fetch_array($resultdflcostcent)) {
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)
	$PCostCentCode = $defaultcostcent;

if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 2;

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


print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search Productions</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
	document.search.PRef.value='';
	document.search.SelectedID.value = '';
	document.search.PProjectCode.value='';
	document.search.PCostCentCode.value='';
	document.search.startrange.value = '';
	document.search.action='Form_ProductionSearch.php';
	document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.PRef.focus();
	
	ChangeLocation();
}

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

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

function SelectRef (sref) {

	document.search.action="Form_Production_Actions.php?mainmode=search&SPrdId="+sref;
	document.search.submit();
		
	opener.document.production.txtPrdId.value=sref;
	opener.document.production.mainaction.value='search';
	opener.document.production.actionmode.value='';
	opener.document.production.action='Form_Production.php';
	opener.document.production.submit();
	self.close();

}

function DeleteRecord(PrdId, PrdRef){

	if (PrdId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+PrdRef+" ?") )
		{
			document.search.action="Form_Production_Actions.php?mainmode=delete&SPrdId="+PrdId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(PrdId){
		
	document.search.action="Form_Production_Actions.php?mainmode=search&SPrdId="+PrdId;
	document.search.submit();
	
	document.search.PMode.value=1;
	
	opener.document.production.txtPrdId.value=PrdId;
	opener.document.production.mainaction.value='edit';
	opener.document.production.actionmode.value='';
	
	//opener.document.production.action='Form_Production.php';
	//opener.document.production.submit();
	
	
	//self.close();
	

}

function ShowHideDivisions(PrdId, Option) {
	if (Option == 'S') {
		document.search.SelectedID.value = PrdId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
	}	
	document.search.action='Form_ProductionSearch.php?SelectedID='+PrdId;
	document.search.submit();
}

function PrintPreview(PrdId)
{
	var str = '';
	var vPrtSer = 0;
	
	if (confirm ( "Do you want to Print With Price?","Yes","No") )
	{
		vPrtSer = 1;
	}
		
	str = 'Form_ProductionPreview.php?ProductionId='+PrdId+'&PrtSer='+vPrtSer;
	window.open(str);

}
function PrintPreviewPrice(PrdId)
{
	var str = '';
	var vPrtSer = 0;
	
	if (confirm ( "Do you want to Print Without Price?","Yes","No") )
	{
		vPrtSer = 1;
	}
		
	str = 'Form_ProductionPreviewPrice.php?ProductionId='+PrdId+'&PrtSer='+vPrtSer;
	window.open(str);

}

function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
		opener.document.production.action='Form_Production.php';
		opener.document.production.submit();
		self.close();
	}	
}



function SelectOrderBy()
{
  document.search.PRef.value='';
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  document.search.action='Form_ProductionSearch.php';
  document.search.submit();

}

function SelectSearchBy()
{	
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_ProductionSearch.php';
  document.search.submit();

}

function SubmitForm()
{	
  document.search.startrange.value = '';
  document.search.SelectedID.value = '';
  //document.search.PRef.value='';
  //document.search.action='Form_ProductionSearch.php';
  document.search.submit();

}

function PrintBarCode(){
	var pid =  document.search.SelectedID.value;
	if(pid == ''){
		alert('Select production first!');
		return;
	}
	win = window.open('Form_ProductionBarcode.php?pid='+pid, 'Form_ProductionBarcode', 'toolbar=no,titlebar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=260,height=180,top=100,left=200');
	win.creator = self;
	win.focus();
}

function kd(e)
{
	x=document.search;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}

function showlistDivision(DivisionType,Str) {
	x = document.production;
	
	window.open('List_FrmDivisions.php?frm=4&item='+Str+'&divtype='+DivisionType,'Divisions','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,width=700,height=450,left=100,top=20');
	
}

</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();'>
<form name='search' method='post' action=''>
<input type='hidden' name='SelectedID' id='SelectedID' value='$SelectedID'>
HERE;

echo "<table border='0' cellspacing='1'  style='table-layout:fixed'>
	  <col width=25>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=70>
	  <col width=90>
	  <col width=80>
	  <col width=70>
	  <col width=80>
	  <col width=80>
	  <col width=100>
	  <col width=320>
	       
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='12'><span class='style16'>List of Production</span></td></tr>
	  <tr bgcolor='#DDDDDD'>";

if ($Module_Project == 1) {
	echo " <td align='left' colspan='3'><span class='style11'>Project</span></td>
	      <td align='left' colspan='2'><span class='style11'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistDivision('PROJECT',1);\"  /></span></label></td>";
} else {
	echo " <td align='left' colspan='5'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";
}

if ($Module_CostCenter == 1) {
	echo " <td align='right' ><span class='style11'>Costcenter</span></td>		
		  <td align='left' colspan='3'><span class='style11'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='17' maxlength='15' /></span>
		    <label><span class='style11'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick=\"showlistDivision('COSTCENTER',2);\" /></span></label></td>";
} else {
	echo " <td align='left' colspan='4'><input type='hidden' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' /></td>";
}

echo " <td align='right' colspan='3'>&nbsp;</td>";
//echo" <td align='right' colspan='2'>&nbsp;</td>	";

echo " </tr>
	  <tr bgcolor='#DDDDDD'>";

echo "   <td align='left' colspan='3'><span class='style11'>Search By</span></td>
		<td align='left' colspan='5'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30' onkeydown='return kd(event)'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
			<input type='hidden' name='startrange' value='$startrange'>
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";

if ($searchby == 1)
	$s21 = 'selected';
else if ($searchby == 2)
	$s22 = 'selected';
else if ($searchby == 3)
	$s23 = 'selected';
else if ($searchby == 4)
	$s24 = 'selected';
else if ($searchby == 5)
	$s25 = 'selected';

echo "<option " . $s21 . " value='1'>Date (yyyy-mm-dd)</option>
				 <option " . $s22 . " value='2'>Reference</option>
				 <option " . $s23 . " value='3'>Batch No</option>";

if ($Module_PostToJournal == 1) {
	echo "<option " . $s25 . " value='4'>Folio</option>
						 <option " . $s26 . " value='5'>Jv Ref.</option>";
}

echo "</select>";

echo "	    <input type='submit' name='btnSubmit' value='Find' onclick='SubmitForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='ClearForm();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>";
echo " <td align='right' ><span class='style11'>Order By</span></td>		
	  <td align='left' ><span class='style11'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();'>";

if ($orderby == 1)
	$s11 = 'selected';
else if ($orderby == 2)
	$s12 = 'selected';

echo "<option " . $s11 . " value='1'>Date</option>
			 <option " . $s12 . " value='2'>Reference</option>
			 </select></span></td>";

echo "	<td align='right' valign='center'><span class='style11'>Per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='SubmitForm();'>";

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

echo "<td align='center'><button type='button' id='btnPrint' name='btnPrint' onclick='PrintBarCode(); return false;' title='Print Barcode'><span><span>Print Barcode</span></span></button>
</td>";
echo " </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='12'><font size='1' color='#FF0000' face='Verdana'><b>" . $message . "</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
	    <td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>Divisions</span></td>
		<td  height='25' align='center'><span class='style25'>Reference </span></td>
		<td  height='25' align='center'><span class='style25'>Date </span></td>";

if ($Module_PostToJournal == 1) {
	echo "<td  height='25' align='center'><span class='style25'>Folio </span></td>
		 <td  height='25' align='center'><span class='style25'>JV Ref. </span></td>";
} else {
	echo "<td  height='25' align='center' colspan='2'>&nbsp;</td>";
}

echo "	<td  height='25' align='center'><span class='style25'>ExpireDate</span></td>
		<td  height='25' align='center'><span class='style25'>Batch No.</span></td>
		<td  height='25' align='center'><span class='style25'>Remark</span></td>
	  </tr>";





if (empty($PRef))
	$PRef = 'Null';
else
	$PRef = "'" . $PRef . "'";

if (empty($ViewPeriodDate))
	$ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))
	$ViewNumberOfDays = 1;


$querysearch = " Select Distinct ProductionHD.ProductionHD_ID, ProductionHD.ProductionHD_Reference, Date_Format(ProductionHD.ProductionHD_Date,'%d-%m-%Y') As ProductionHD_Date, " .
	" Date_Format(ProductionHD.ProductionHD_ExpireDate,'%d-%m-%Y') As ExpireDate, ProductionHD.ProductionHD_BatchNo, " .
	" ProductionHD.ProductionHD_Remark, ProductionHD.Currency_Code, " .
	" Round(ProductionHD.ProductionHD_Total, " . $DecimalBase . ") As ProductionTotal, Round(ProductionHD.ProductionHD_IdealTotal, " . $DecimalBase . ") As ProductionIdealTotal, " .
	" ifNull(Temp_ProductionHD.ProductionHD_ID, -1) As TempProductionId, Journal.Journal_Folio, Journal.Journal_RefGovernment " .
	" From ProductionHD " .
	" Left Join Journal on ProductionHD.Journal_ID = Journal.Journal_ID " .
	" Left Join Temp_ProductionHD on Temp_ProductionHD.ProductionHD_ID = ProductionHD.ProductionHD_ID ";


if ($projectexist >	0) {
	$querysearch = $querysearch . " Inner Join ProductionHDDivisions As PrdDivPrj on PrdDivPrj.ProductionHD_ID = ProductionHD.ProductionHD_ID " .
		" Inner Join Divisions As DivPrj on DivPrj.Divisions_id = PrdDivPrj.Divisions_id " .
		" Inner Join DivisionTypes As DivTypePrj on DivTypePrj.DivisionTypes_Id = DivPrj.DivisionTypes_Id and upper(DivTypePrj.DivisionTypes_Name)='PROJECT' ";

	$querysearch = $querysearch . " Inner join ( select distinct project_code " .
		" 			   From " . $_SESSION['dbLabel'] . ".loginstk " .
		" 			   Where loginstk.user_account='" . $_SESSION['useraccount'] . "' " .
		" 			   And (project_code is not null and project_code != '') " .
		"			 ) QPrj on (($Module_ProjectParent=1 and DivPrj.Divisions_Code like concat(QPrj.project_code,'%')) or ($Module_ProjectParent=0 and DivPrj.Divisions_Code = QPrj.project_code )) ";
}
if ($costcentexist > 0) {
	$querysearch = $querysearch . " Inner Join ProductionHDDivisions As PrdDivCst on PrdDivCst.ProductionHD_ID = ProductionHD.ProductionHD_ID " .
		" Inner Join Divisions As DivCst on DivCst.Divisions_id = PrdDivCst.Divisions_id " .
		" Inner Join DivisionTypes As DivTypeCst on DivTypeCst.DivisionTypes_Id = DivCst.DivisionTypes_Id and upper(DivTypeCst.DivisionTypes_Name)='COSTCENTER' ";

	$querysearch = $querysearch . " 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 != '') " .
		"			 ) QCst on (($Module_ProjectParent=1 and DivCst.Divisions_Code like concat(QCst.costcent_code,'%')) or ($Module_ProjectParent=0 and DivCst.Divisions_Code = QCst.costcent_code )) ";
}

if ($PProjectCode != '') {
	$querysearch = $querysearch . " Inner Join ProductionHDDivisions As FPrdDivPrj on FPrdDivPrj.ProductionHD_ID = ProductionHD.ProductionHD_ID " .
		" Inner Join Divisions As FDivPrj on FDivPrj.Divisions_id = FPrdDivPrj.Divisions_id And FDivPrj.Divisions_Code like concat('" . $PProjectCode . "','%') " .
		" Inner Join DivisionTypes As FDivTypePrj on FDivTypePrj.DivisionTypes_Id = FDivPrj.DivisionTypes_Id and upper(FDivTypePrj.DivisionTypes_Name)='PROJECT' ";
}
if ($PCostCentCode != '') {
	$querysearch = $querysearch . " Inner Join ProductionHDDivisions As FPrdDivCst on FPrdDivCst.ProductionHD_ID = ProductionHD.ProductionHD_ID " .
		" Inner Join Divisions As FDivCst on FDivCst.Divisions_id = FPrdDivCst.Divisions_id And FDivCst.Divisions_Code like concat('" . $PCostCentCode . "','%') " .
		" Inner Join DivisionTypes As FDivTypeCst on FDivTypeCst.DivisionTypes_Id = FDivCst.DivisionTypes_Id and upper(FDivTypeCst.DivisionTypes_Name)='COSTCENTER' ";
}

$querysearch = $querysearch .
	" Where (($searchby = 2 and ProductionHD.ProductionHD_Reference Like Concat(ifNull($PRef, ProductionHD.ProductionHD_Reference),'%')) Or " .
	"        ($searchby = 1 and Date_Format(ProductionHD.ProductionHD_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(ProductionHD.ProductionHD_Date,'%Y-%m-%d'))) Or  " .
	"        ($searchby = 3 and ifNull(ProductionHD.ProductionHD_BatchNo, -1) Like Concat(ifNull($PRef, ifNull(ProductionHD.ProductionHD_BatchNo, -1)),'%') ) Or  " .
	"        ($searchby = 4 and Journal.Journal_Folio Like Concat(ifNull($PRef, Journal.Journal_Folio),'%')) Or " .
	"        ($searchby = 5 and Journal.Journal_RefGovernment Like Concat(ifNull($PRef, Journal.Journal_RefGovernment),'%')) ) " .
	" And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(ProductionHD.ProductionHD_Date,'%Y-%m-%d') >= Date_Format(ifNull('" . $ViewPeriodDate . "',ProductionHD.ProductionHD_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   " .
	"                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(ProductionHD.ProductionHD_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) " .
	" And ifNull(ProductionHD.ProductionHD_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";


if ($orderby == 1)
	$querysearch = $querysearch . " Order By ProductionHD.ProductionHD_Date Desc, ProductionHD.ProductionHD_Reference Desc ";
else if ($orderby == 2)
	$querysearch = $querysearch . " Order By ProductionHD.ProductionHD_Reference Desc ";


//echo $querysearch;			 

$resultsearch = mysql_query($querysearch, $connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);


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

$resultsearch = mysql_query($querysearch, $connection) or die(mysql_error());


while ($rows = mysql_fetch_array($resultsearch)) {
	$ExceedLockPeriod = 0;
	extract($rows);
	$ct += 1;

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


	$PrevHint = 'With Price';
	$PrevPrice = 'Without Price';
	$EditHint = 'Edit Record';
	$DelHint  = 'Delete Record';

	if ($TempProductionId != -1) {
		$PrevPrice = $PrevHint = $EditHint = $DelHint = 'Exists in Pending';
	}


	if ($FI_ProductionMod == 0)	$PrdEditMode = 0;
	else if ($FI_ProductionMod == 1 && $TempProductionId != -1) $PrdEditMode = 0;
	else if ($FI_ProductionMod == 1 && $TempProductionId == -1) $PrdEditMode = 1;

	if ($FI_ProductionDel == 0)	$PrdDelMode = 0;
	else if ($FI_ProductionDel == 1 && $TempProductionId != -1) $PrdDelMode = 0;
	else if ($FI_ProductionDel == 1 && $TempProductionId == -1) $PrdDelMode = 1;

	if ($TempProductionId != -1) $PrdPrevMode = 0;
	else $PrdPrevMode = 1;

	if ($rowsperiod > 0 && $AllowLockedPeriod == 1) {
		if (!(empty($LockedPeriodDate)) && $LockedPeriodDate != '' && date('Y-m-d', strtotime(insertMySQLDate($ProductionHD_Date))) <= date('Y-m-d', strtotime($LockedPeriodDate))) $ExceedLockPeriod = 1;
		if ($LockedPeriodDays != '' && dateDiff(insertMySQLDate(displayToday()), insertMySQLDate($ProductionHD_Date)) >= $LockedPeriodDays) $ExceedLockPeriod = 1;
	}

	if ($ExceedLockPeriod == 1) {
		$PrdEditMode = 0;
		$PrdDelMode = 0;
	}

	echo '<tr ' . $Color . '>';

	if ($PrdPrevMode == 1)
		echo ' <td align="center"><span class="style15"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview(' . $ProductionHD_ID . ');"  title="' . $PrevHint . '" " style="width: 15px; height: 15px; "></span></td>';
	else echo '<td align="center">&nbsp;</td>';

	if ($PrdPrevMode == 1)
		echo ' <td align="center"><span class="style15"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreviewPrice(' . $ProductionHD_ID . ');"  title="' . $PrevPrice . '" " style="width: 15px; height: 15px; " ></span></td>';
	else echo '<td align="center">&nbsp;</td>';


	if ($PrdEditMode == 1)
		echo '<td align="center"><span class="style15"><img src="img/button_edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord(' . $ProductionHD_ID . ');" title="' . $EditHint . '"  ></span></td>';
	else echo '<td align="center">&nbsp;</td>';

	if ($PrdDelMode == 1)
		echo '<td align="center"><span class="style15"><img src="img/c_delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord(' . $ProductionHD_ID . ', \'' . $Production_Reference . '\');" title="' . $DelHint . '"  ></span></td>';
	else echo '<td align="center">&nbsp;</td>';

	if ($SelectedID != "" && $ProductionHD_ID == $SelectedID)
		echo '<td align="center"><span class="style15"><img src="img/hide.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDivisions(' . $ProductionHD_ID . ', \'' . 'H' . '\');" title="Hide Divisions" height="15px" width="15px"></span></td>';
	else
		echo '<td align="center"><span class="style15"><img src="img/show.png" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDivisions(' . $ProductionHD_ID . ', \'' . 'S' . '\');" title="Show Divisions" height="15px" width="15px"></span></td>';

	echo '	<td align="left"><span class="style15">' . $ProductionHD_Reference . '</span></td>
			<td align="center"><span class="style15">' . $ProductionHD_Date . '</span></td>';

	if ($Module_PostToJournal == 1) {
		echo ' <td align="center"><span class="style15">' . $Journal_Folio . '</span></td>
			  <td align="center"><span class="style15">' . $Journal_RefGovernment . '</span></td>';
	} else {
		echo ' <td align="center" colspan="2">&nbsp;</td>';
	}

	echo '	<td align="left"><span class="style15">' . $ExpireDate . '</span></td>
			<td align="left"><span class="style15">' . $ProductionHD_BatchNo . '</span></td>
			<td align="left"><span class="style15">' . $ProductionHD_Remark . '</span></td>
		</tr>';

	if ($SelectedID != "" && $ProductionHD_ID == $SelectedID) {
		$sub_query = " Select DivisionTypes_Name, Divisions_Code, Divisions_Name  " .
			" From ProductionHDDivisions " .
			" Inner Join Divisions on ProductionHDDivisions.Divisions_Id = Divisions.Divisions_Id " .
			" Inner Join DivisionTypes on Divisions.DivisionTypes_Id = DivisionTypes.DivisionTypes_Id " .
			" Where ProductionHDDivisions.ProductionHD_Id = '" . $SelectedID . "' ";
		$sub_result = mysql_query($sub_query, $connection) or die(mysql_error());
		$sub_totalrows = mysql_num_rows($sub_result);

		if ($sub_totalrows > 0) {
			echo "<tr " . $Color . ">";
			echo "<td colspan='9' valign='top' align='left'><div style='border:1px solid #33CCFF; z-index:9999; background-color:#CCFFFF; padding-left:5px; padding-bottom:5px;'>";
			echo "<table width='90%' border='0' cellpading='0' cellspacing='1'><tr>";
			echo "<td width='20%'><span class='style15'>Type</span></td><td width='20%'><span class='style15'>Code</span></td> <td width='60%'> <span class='style15'>Name</span> </td>";
			while ($sub_rows = mysql_fetch_array($sub_result)) {
				echo "<tr><td>" . $sub_rows[0] . "</td><td>" . $sub_rows[1] . "</td><td>" . $sub_rows[2] . "</td></tr>";
			}

			echo "</tr></table></div></td>";
			echo "<td colspan='2' valign='top' align='left'>&nbsp;</td></tr>";
		}
	}
}




echo "   <tr><td colspan='12'>&nbsp;</td></tr>
		<tr><td colspan='12'><table width='100%' border=0><tr><td width='300'>&nbsp;</td><td width='177' align='center'>";

$diff = $startrange - $perpage;

if (($diff) >= 0) {
	$prvstartrange = $startrange - $perpage;
	echo "<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('" . $prvstartrange . "','" . $perpage . "')>";
} else
	echo "&nbsp;";

echo "</td><td width='177' align='center'>";


if ($startrange <= $totalrows) {
	$startrange = $startrange + $perpage;
	if ($startrange <= $totalrows)
		echo "<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('" . $startrange . "','" . $perpage . "')>";
} else
	echo "&nbsp;";

echo "</td><td width='300'>&nbsp;</td></tr>";
echo "<table>";

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







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

HERE;
