<?php
$PageSecurity = 11;
$title = _('Stock Category Maintenance');
if (isset($_GET['SelectedCategory']))
{
	$SelectedCategory = strtoupper($_GET['SelectedCategory']);
}
 else if (isset($_POST['SelectedCategory'])){
	$SelectedCategory = strtoupper($_POST['SelectedCategory']);
}

if (isset($_GET['DeleteProperty']))
{
	$ErrMsg = _('Could not delete the property') . ' ' . $_GET['DeleteProperty'] . ' ' . _('because');
	$sql = "DELETE FROM stockitemproperties WHERE stkcatpropid=" . $_GET['DeleteProperty'];
	$result = DB_query($sql,$db,$ErrMsg);
	$sql = "DELETE FROM stockcatproperties WHERE stkcatpropid=" . $_GET['DeleteProperty'];
	$result = DB_query($sql,$db,$ErrMsg);
	prnMsg(_('Deleted the property') . ' ' . $_GET['DeleteProperty'],'success');
}

if (isset($_POST['submit'])) 
{
$sqlPoApp=DB_query("SELECT * FROM stockcategory where stockmaincategory='".$_REQUEST['CategoryID1']."' and sequence='".$_REQUEST['sequence']."'",$db);
		if(DB_num_rows($sqlPoApp)==0){
 $sql = "INSERT INTO stockcategory SET 
categoryid='".$_REQUEST['CategoryID']."',
categorydescription='".$_REQUEST['CategoryDescription']."',
item_prefix='".$_REQUEST['item_prefix']."',
stockmaincategory='".$_REQUEST['CategoryID1']."',
CompanyId='".$_SESSION['ACGCompanyId']."',
sequence='".$_REQUEST['sequence']."' ";
 $ErrMsg = _('Could not insert the new stock category') . $_POST['CategoryDescription'] . _('because');
$result = DB_query($sql,$db,$ErrMsg);
prnMsg(_('A new stock category record has been added for') . ' ' . $_POST['CategoryDescription'],'success');
echo "<script>location.href='index.php?route=StockCategories&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";

	unset($_POST['StockType']);
	unset($_POST['CategoryDescription']);
	unset($_POST['item_prefix']);
	unset($_POST['CategoryID1']);
	unset($_POST['StockAct']);
	unset($_POST['AdjGLAct']);
	unset($_POST['PurchPriceVarAct']);
	unset($_POST['MaterialUseageVarAc']);
	unset($_POST['WIPAct']);
	unset($_POST['sequence']);
	}
	else
	{
		$msg = ('Duplicate Sequence No. is Avoided');
		prnMsg($msg,'success');
	}
}
if (isset($_GET['delete'])) 
{
	  $sql= "SELECT COUNT(*) FROM item_master WHERE item_master.SubCategoryID='".$_REQUEST['SelectedCategory']."'";
	$result = DB_query($sql,$db);
	$myrow = DB_fetch_row($result);
	if ($myrow[0]>0) {
		prnMsg(_('Cannot delete this stock category because stock items have been created using this stock category') .
			'<br> ' . _('There are') . ' ' . $myrow[0] . ' ' . _('items referring to this stock category code'),'warn');
	}
	 else {
		$sql = "SELECT COUNT(*) FROM salesglpostings WHERE stkcat='".$_REQUEST['SelectedCategory']."'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ($myrow[0]>0) {
			prnMsg(_('Cannot delete this stock category because it is used by the sales') . ' - ' . _('GL posting interface') . '. ' . _('Delete any records in the Sales GL Interface set up using this stock category first'),'warn');
		} else {
			$sql = "SELECT COUNT(*) FROM cogsglpostings WHERE stkcat='".$_REQUEST['SelectedCategory']."'";
			$result = DB_query($sql,$db);
			$myrow = DB_fetch_row($result);
			if ($myrow[0]>0) {
				prnMsg(_('Cannot delete this stock category because it is used by the cost of sales') . ' - ' . _('GL posting interface') . '. ' . _('Delete any records in the Cost of Sales GL Interface set up using this stock category first'),'warn');
			} else {
				$sql="DELETE FROM stockcategory WHERE categoryid='".$_REQUEST['SelectedCategory']."'";
				$result = DB_query($sql,$db);
				prnMsg(_('The stock category') . ' ' . $SelectedCategory . ' ' . _('has been deleted') . ' !','success');
				unset ($SelectedCategory);
				echo "<script>location.href='index.php?route=StockCategories&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
			}
		}
	} //end if stock category used in debtor transactions
}
if($_REQUEST['Update']!='')
{
if($_REQUEST['sequence']!="")
{
	$sqlPoApp=DB_query("SELECT * FROM stockcategory where stockmaincategory='".$_REQUEST['CategoryID1']."' and sequence='".$_REQUEST['sequence']."'",$db);
		
		if(DB_num_rows($sqlPoApp)>=1)
		{

   	 $up_sql="Update stockcategory SET 
									 categoryid='".$_REQUEST['CategoryID']."',
									 item_prefix='".$_REQUEST['item_prefix']."',
									 stockmaincategory='".$_REQUEST['CategoryID1']."'
 									 ,categorydescription='".$_REQUEST['CategoryDescription']."'
									  ,sequence='".$_REQUEST['sequence']."'
									 where id='".$_REQUEST['SID']."'";
								
								
		$up_res=DB_query($up_sql,$db);
						echo "<script>location.href='index.php?route=StockCategories&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
else
	{
		 echo "hello";
		$msg = ('Duplicate Sequence No. is Avoided');
		prnMsg($msg,'success');
	}
}
else
{
$up_sql="Update stockcategory SET 
									 categoryid='".$_REQUEST['CategoryID']."',
									 item_prefix='".$_REQUEST['item_prefix']."',
									 stockmaincategory='".$_REQUEST['CategoryID1']."'
 									 ,categorydescription='".$_REQUEST['CategoryDescription']."'
									  ,sequence='".$_REQUEST['sequence']."'
									 where id='".$_REQUEST['SID']."'";
								
								
		$up_res=DB_query($up_sql,$db);
						echo "<script>location.href='index.php?route=StockCategories&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $page_title; ?></h1>
<form name="CategoryForm" method="post" action="">
<?php 
	if($_REQUEST['action']=='edit' && $_REQUEST['SelectedCategory']!='') 
	{
		$get_sql="select * from stockcategory where categoryid='".$_REQUEST['SelectedCategory']."'";
		$get_res=DB_query($get_sql,$db);
		$get_row=DB_fetch_array($get_res);
		
    $categoryid=$get_row['categoryid'];
    $categorydescription=$get_row['categorydescription'];
    $item_prefix=$get_row['item_prefix'];
    $stockmaincategory=$get_row['stockmaincategory']; 
	$sequence=$get_row['sequence']; 
	} 
	else
	{
		$categoryid=$_REQUEST['CategoryID'];
    	$categorydescription=$_REQUEST['CategoryDescription'];
    	$item_prefix=$_REQUEST['item_prefix'];
    	$stockmaincategory=$_REQUEST['SID']; 
	}
	if (!isset($_POST['CategoryID'])) 
	{
		$_POST['CategoryID'] = '';
	}
?>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr class="row1">
    <td width="13%" align="right"><strong>Main Category:</strong></td> 
	<td width="20%"><select name="CategoryID1" id="CategoryID1" style="width:100%;">
        <?php 
       $sql = "SELECT id,stockmaincategoryid,stockmaincategoryname FROM stockmaincategory where CompanyId='".$_SESSION['ACGCompanyId']."'";
	   $result = DB_query($sql,$db,$ErrMsg,$DbgMsg);
		while ($myrow=DB_fetch_array($result))
		{?>
        <option value="<?php echo $myrow['id']; ?>"    <?php echo($stockmaincategory==$myrow['id']?'selected':'');?> ><?php echo $myrow['stockmaincategoryname'];?></option>
        <?php } ?>
    </select></td> 
    <td width="13%" align="right"><strong>Sub Category:</strong></td>
    <td width="20%"><?php if($_REQUEST['SelectedCategory']!='' && $_REQUEST['action']=='edit') {?>
	<input type="text" name="CategoryDescription" id="CategoryDescription" style="width:100%;" maxlength="30" value="<?php echo $categorydescription;?>" />
	<?php } else { ?>
	<input type="text" name="CategoryDescription" id="CategoryDescription" maxlength="30" style="width:100%;" value="<?php echo $categorydescription;?>" />
	<?php } ?></td>
    <td width="13%" align="right"><strong>Sub Category Prefix:</strong></td>
    <td><input type="text" name="item_prefix" id="item_prefix" style="width:100%;" value="<?php echo $item_prefix;?>" onblur="get_code(this.value)" maxlength="10"/></td>
  </tr>
<tr class="row1">
	<td align="right"><strong>Sub Category Code:</strong></td>
	<td><input type="Text" style="width:100%;" name="CategoryID" id="CategoryID" value="<?php echo $categoryid;?>" readonly="true"></td>
	<td align="right"><strong>Sequence:</strong></td>
	<td><input type="Text" style="width:100%;" name="sequence" id="sequence" value="<?php echo $sequence; ?>" maxlength="30"></td>
    <td colspan="2">&nbsp;</td>
	</tr>
<tr class="row1">
	  <td colspan="6" style="text-align:center;"><?php if($_REQUEST['action']=='edit')
	{?><input type="Submit" name="Update" class="search" value="Update" onclick="return frmvalidate();">
	<?php 
	}else{ ?>
	<input type="Submit" name="submit" class="search" value="Save" onclick="return frmvalidate();">
	<?php } ?></td>
</tr>
<?php 
	
	//SQL to poulate account selection boxes
	$sql = "SELECT accountcode,
                 accountname
                 FROM chartmaster,
                      accountgroups
                 WHERE chartmaster.group_=accountgroups.groupname
                 ORDER BY accountcode";

	$BSAccountsResult = DB_query($sql,$db);

	$sql = "SELECT accountcode,
                 accountname
                 FROM chartmaster,
                      accountgroups
                 WHERE chartmaster.group_=accountgroups.groupname
                 ORDER BY accountcode";

	$PnLAccountsResult = DB_query($sql,$db);

	if (!isset($_POST['CategoryDescription'])) 
	{
		$_POST['CategoryDescription'] = '';
	}
	?>	
	<input type="submit" name="UpdateTypes" style="visibility:hidden;width:1px" value="Not Seen"><?php 
	DB_data_seek($BSAccountsResult,0);
?>
</table>

</form>
<br />
<?php
	$sql = "SELECT * FROM stockcategory where CompanyId='".$_SESSION['ACGCompanyId']."'";
	$result = DB_query($sql,$db);
	?>

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid;">
<tr>
	<th rowspan="2" width="10%">Sub Category Code</th>
    <th rowspan="2">Sub Category Description</th>
	<th rowspan="2" width="15%">Main Category</th>
	<th rowspan="2" width="15%">Sub Category Prefix</th>
	<th rowspan="2" width="10%">Sequence</th>
	<th colspan="2" style="text-align:center;">Action</th>
</tr>
<tr>
	<th width="5%" style="text-align:center;">Edit</th>
	<th width="5%" style="text-align:center;">Delete</th>
</tr>
<?php 
	$k=1; //row colour counter
	$flag=0;
	while ($myrow = DB_fetch_array($result)) 
	{	
	$sqlitem=DB_query("select * from item_master where SubCategoryID='".$myrow['id']."'",$db);
	$numrow1=DB_num_rows($sqlitem);
	if($numrow1>0)
	{
	$flag=1;
	}
	else
	{
	$flag=0;
	}
	
	
	
	?>
		<tr bgcolor="#FFFFFF" >
		<td><?php echo $myrow['categoryid'];?></td>
		<td><?php echo $myrow['categorydescription'];?></td>
		<td><?php 
		$main_sql=DB_query("Select id,stockmaincategoryname from stockmaincategory where id='".$myrow['stockmaincategory']."'",$db);
		$main=DB_fetch_array($main_sql);
		echo $main['stockmaincategoryname'];?>		
		<input name="hid-stockmaincategory" type="hidden" value="<?php  echo $main['id']?>" /></td>
		<td><?php echo $myrow['item_prefix'];?></td>
		<td><?php echo $myrow['sequence'];?></td>
		<td style="text-align:center;"><a href="index.php?route=StockCategories&modid=<?php echo $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedCategory=<?php echo $myrow['categoryid'];?>&SID=<?php echo $myrow['id'];?>&action=edit"><img src="images/icon-edit.png" width="15" height="15" title="Edit" /></a></td>
		<td style="text-align:center;"><?php if($flag==0) { ?><a href="index.php?route=StockCategories&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedCategory=<?php echo $myrow['categoryid'];?>&SID=<?php echo $myrow['id'];?>&delete=yes" onclick="return confirm('Are you sure you wish to delete this stock category?')"><img src="images/Icon_Delete.png" width="15" height="15" title="Delete" /></a><?php } else { ?><img src="images/Icon_Delete.png" width="15" height="15" title="No Permission" style="opacity:0.5;" /><?php } ?></td>
		</tr>
		<?php
$k++;	} ?>
</table>
<?php
include('includes/footer.inc');
?>
<script language="javascript">
function get_code(str)
{
	var main_id=document.getElementById("CategoryID1").value;
	var final = main_id+str;
	document.getElementById("CategoryID").value=final;
}
var xmlHttp
function putnumber(str)
{ 
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		    alert ("Your browser does not support AJAX!");
		    return;
		  } 
	var url="randomnumber.php";
	url=url+"?Category="+str;
  //	alert(url);
	//url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=NumberChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function NumberChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
	//salert(xmlHttp.responseText);
	 document.getElementById("CategoryID").value=xmlHttp.responseText;
	 
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	return xmlHttp;
}




function frmvalidate()
{
	var chr=document.getElementById("CategoryDescription").value;
	if(chr=='')
	{
		alert("You Must Fill the SUB CATEGORY NAME/DESCRIPTION  First!");
		return false;
	}
	/*else
	{
		if(chr.length<5)
		{
			alert("SUB CATEGORY NAME/DESCRIPTION  Not Less than 5 Character");
			return false;
		}
		
	} */
	
	var chr1=document.getElementById("item_prefix").value;
	if(chr1=='')
	{
		alert("You Must Fill the SUB CATEGORY PREFIX First!");
		return false;
	}
	/*else
	{
		if(chr1.length<5)
		{
			alert("SUB CATEGORY PREFIX Not Less than 5 Character");
			return false;
		}
		
	} */
	
}
</script>
