<?php 
$PageSecurity = 11;
$title = _('Item Master Setup');
include('includes/SQL_CommonFunctions.inc');
if($_REQUEST['id']!='' && $_REQUEST['action']=='del' && $_REQUEST['master']!='')
{
	$delete_sql="Delete from item_master_setup where field='".$_REQUEST['id']."' and item_master='".$_REQUEST['master']."'";
	$delete_res=DB_query($delete_sql,$db);
	$update_master="alter table item_master DROP COLUMN ".$_REQUEST['id']."";
	$update_res=DB_query($update_master,$db);
	
	$_SESSION['success_msg']="Delete Successfully";
	echo "<script>location.href='index.php?route=Inventory/maintenance/item_master_setup&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";

}
if($_REQUEST['Save'])
{
			 $field_name=array();			
			if(strrpos($_REQUEST['field'],"/")!='')
			{
				$field_name=explode("/",$_REQUEST['field']);
				$final_fieldname=implode('_',$field_name);
			} 
			else
			if(strrpos($_REQUEST['field'],"-")!='')
			{
				$field_name=explode("-",$_REQUEST['field']);
				$final_fieldname=implode('_',$field_name);
			}
			else
			{
				$field_name=explode(" ",$_REQUEST['field']);
				$final_fieldname=implode('_',$field_name);
			}
			$sql="insert into item_master_setup set
							id='',  
							item_master='".$_REQUEST['item_master']."',  
							field='".$final_fieldname."', 
							field_label='".$_REQUEST['field']."',  
							field_type='".$_REQUEST['field_type']."', 
							field_size='".$_REQUEST['field_size']."' ";
			//echo "<pre>";
			//print_r($sql);
			$result=DB_query($sql,$db);
			
	$last_id=DB_Last_Insert_ID($db,'item_master_setup','id');
	if($result==true)
	{
		$alter_sql="alter table item_master add ".$final_fieldname." ".$_REQUEST['field_type']." (".$_REQUEST['field_size'].")";
		$alter_result=DB_query($alter_sql,$db);
	}

$_SESSION['success_msg']="Saved Successfully";

echo "<script>location.href='index.php?route=Inventory/maintenance/item_master_setup&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
//}
}
?>
<script language="javascript" type="text/javascript">
function get_size(str)
{
	var size;
	if(str=='varchar')
	{
	size='255';
	document.getElementById("field_size").value=size;
	}
	else if(str=='int')
	{
	size='10';
	document.getElementById("field_size").value=size;
	}
	else if(str=='double')
	{
	size='10,2';
	document.getElementById("field_size").value=size;
	}
	else
	{
	document.getElementById("field_size").value='';
	}
}
</script>
<h2 align="center"><strong>Item Master Setup</strong></h2>
<form action="" method="post">
<h3 align="center"><strong>Select the Item Master to which the field has to be added</strong></h3>
<table width="40%" border="0" cellspacing="1" cellpadding="2" align="center">
  <tr>
    <th><strong>ITEM MASTER CATEGORY </strong></th>
    <td>
	<select name="item_master" id="item_master">
	<option value="000">Select</option>
	<?php
	$query=DB_query("select * from stockmaincategory",$db);
	while($result=DB_fetch_array($query))
	{
	?>
	<option value="<?php echo $result['stockmaincategoryid'];?>"<?php echo ($_REQUEST['item_cat']==$result['stockmaincategoryid']?'selected':'')?>><?php echo $result['stockmaincategoryname'];?></option>
	<?php } ?>
	</select>	</td>
  </tr>
  <tr>
    <td colspan="2"><div align="center"><strong>The Field Name has to be UNIQUE </strong></div></td>
  </tr>
  <tr>
    <th><strong>FIELD NAME </strong></th>
    <td><input name="field" type="text" id="field" value="<?php $_REQUEST['field'];?>" size="25" /></td>
  </tr>
  <tr>
    <th><strong>FIELD TYPE </strong></th>
    <td>
	<select name="field_type" id="field_type" onchange="get_size(this.value)">
	<option value="000">Select</option>
	<option value="varchar">varchar</option>
	<option value="int">int</option>
	<option value="double">double</option>
	<option value="date">date</option>
	</select>
	<input type="hidden" name="field_size" id="field_size" value="" />	</td>
  </tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save"></p>
</form>
<br />
<br />
<table width="30%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th><strong>ITEM CATEGORY </strong></th>
    <th><strong>FIELD NAME </strong></th>
	<th><strong>FIELD LABEL </strong></th>
    <th><strong>FIELD TYPE </strong></th>
	<th><strong>ACTION</strong></th>
  </tr>
  <?php 
  $field_sql=DB_query("Select * from item_master_setup",$db);
  $i=1;
  while($field_row=DB_fetch_array($field_sql))
 {
  if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
  
  ?>
  <tr bgcolor="<?php echo $bg;?>">     
    <td><?php 
	$cat_sql=DB_query("Select stockmaincategoryname from stockmaincategory where stockmaincategoryid='".$field_row['item_master']."'",$db);
	$cat_row=DB_fetch_array($cat_sql);
	echo $cat_row['stockmaincategoryname'];?></td>
    <td><?php echo $field_row['field'];?></td>
	<td><?php echo $field_row['field_label'];?></td>
    <td><?php echo $field_row['field_type'];?></td>
	<td>
	<?php if($field_row['status']=='1') {
	echo "Cannot Be Deleted";}
	else
	{?>
	<a href="index.php?route=Inventory/maintenance/item_master_setup&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $field_row['field'];?>&master=<?php echo $field_row['item_master'];?>&action=del" onclick="return confirm('Are you sure you want to delete this field?')">Delete</a>
	<?php 	
	}
	?>
	</td>
  </tr>
  <?php $i++;} ?>
</table>
<?php 
include('includes/footer.inc');

 if(isset($_SESSION['success_msg']))
{ ?>
	<script> alert("<?php echo ($_SESSION['success_msg']);?>");</script>
<?php 
}	unset($_SESSION['success_msg']);
?>