<link rel="stylesheet" href="css/new_mxerp.css">
<?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);
	prnMsg( _('Field Deleted Successfully!'),'success');
	echo "<script>location.href='index.php?route=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);
	}

prnMsg( _('Field Entered Successfully!'),'success');
echo "<script>location.href='index.php?route=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>

<div class="body_contain">
<div class="tab_div">


<h1>Item Master Setup</h1>
<form action="" method="post">
<h3 align="center"><strong>Select the Item Master to which the field has to be added</strong></h3>
<br />

<table width="40%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <th class="tabledata1">Item Master Category </th>
    <td>
	<select name="item_master" id="item_master" style="width:50%">
	<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"><h3>The Field Name has to be UNIQUE </h3></td>
  </tr>
  <tr>
    <th class="tabledata1">Field Name </th>
    <td><input name="field" type="text" id="field" value="<?php $_REQUEST['field'];?>" style="width:50%" /></td>
  </tr>
  <tr>
    <th class="tabledata1">Field Type </th>
    <td>
	<select name="field_type" id="field_type" onchange="get_size(this.value)" style="width:50%">
	<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>

<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata">Item Category </td>
    <td class="tabledata">Field Name </td>
	<td class="tabledata">Field Label </td>
    <td class="tabledata">Field Type </td>
	<td class="tabledata">Action</td>
  </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="#c1c1c1">
    <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=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>

</div></div>

<?php 
//include('includes/footer.inc');
?>