<?php //include('./includes/OverheadMaster_Class.php');
 $table='overheadgroupmaster';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=Production/overheadgroup_master".$url;
if($_REQUEST['Save'])
{
	if(empty($_REQUEST['GroupName']))
	{
	   $msgErr[]='Please Enter Group Name';
	   $status=false;
	}
	 
	 
	 	$condition='GroupName="'.$_REQUEST['GroupName'].'" and CompanyId='.$_SESSION['ACGCompanyId'].'';
		$allready=$s->GetRecord($table,  $condition);
		$row=DB_query($allready,$db);
		if(DB_num_rows($row)>0 && $action!='Edit')
		{
			 $msgErr[]='All Ready Exists';
			 $status=false;
		}
		
} 

//************************Delete*********************************************** 
if( $action=='del')
{
	
	//before delete check wether this entry existing in cashbankvoucher_master Table
	//$condition="UnderGroup='".$_REQUEST['GroupId']."'";
  	//$r=$s->GetRecordById('accountledger',$condition);
	//$numrows = DB_num_rows($r);
	//if($numrows>0){
	//$s->pageLocation($targetpage.'&daction=1');
	//}
 	//else{
 		$condition="MainGroupId='".$_REQUEST['GroupId']."'";
  		$r=$s->GetRecordById($table,$condition);
		$numrows = DB_num_rows($r);
		if($numrows>0)
		{
			echo "<div class='error'>Can't Delete This Record. First Delete it's Child Record.</div>";
			$s->pageLocation($targetpage.'&daction=2');
 		}
		else{
		 $condition="GroupId='".$_REQUEST['GroupId']."'";
	     $s->DeleteRecord($table,$condition);
		 echo "<div class='success'>Record Deleted Successfully.</div>";
	     $s->pageLocation($targetpage.'daction=Deleted');
		}
	//}
}
 
  
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="GroupId='".$_REQUEST['GroupId']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
  	 
	 $GroupName=$RData['GroupName'];
  	 $MainGroupId=$RData['MainGroupId'];
	$sequence=$RData['sequence'];
	$status1=$RData['status'];
    		 
}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
 	$data['GroupName']=$_REQUEST['GroupName'];
  	$data['MainGroupId']=$_REQUEST['MainGroupId'];
	$data['sequence']=$_REQUEST['sequence'];
	$data['status']=$_REQUEST['status'];
     $cond="GroupId='".$_REQUEST['GroupId']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
	echo "<div class='success'>Record Updated Successfully.</div>";
  $s->pageLocation($targetpage.'&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
 	$data['GroupName']=$_REQUEST['GroupName'];
   	$data['MainGroupId']=$_REQUEST['MainGroupId'];
  	$data['sequence']=$_REQUEST['sequence'];
	$data['status']=$_REQUEST['status'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];

	

	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
echo "<div class='success'>Record Inserted Successfully.</div>";
 $s->pageLocation($targetpage.'&daction=Inserted');
}
?>


<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<h1><?php echo $s->GetFieldValue('modules_form','id="'.$_REQUEST['frmid'].'"','description');?></h1>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td class="tabledata1"><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
</tr>
</table>
<form action="" method="post">
<table width="50%" border="0" cellspacing="3" cellpadding="3">
  <?php if(isset($daction11))
  {?>
<tr class="row1">
  
    <td colspan="2" ><div class="success">
	<?php 
	 if($_REQUEST['daction']!=1 && $_REQUEST['daction']!=2)
	 echo "Record ".$_REQUEST['daction']." Successfully";
	 elseif($_REQUEST['daction']==1)
	 	 echo "Can't Delete This Record because AccountName Attatch with LedgerDayBook table ";
	 elseif($_REQUEST['daction']==2)		 
		 echo "Can't Delete This Record. First Delete it's Child Record   ";
		 ?>
	</div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
<tr class="row1">
  
    <td colspan="2" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
<tr class="row1">
	<td class="tabledata1">Group</td>
    <td><input name="GroupName" type="text" style="width:150px" value="<?php echo $GroupName;?>"></td>
  </tr>
<tr class="row1">
	<td class="tabledata1">Under Group</td>
    <td><?php //  $AccountMaster->AccountGroup($MainGroupId,'MainGroupId'); 
 	$pcode= $MainGroupId;
 	function CategoryShow($pcode, $PcateID, $dashVal)
	{
	    global $db;
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from overheadgroupmaster  where  MainGroupId = '".$PcateID."' and CompanyId=".$_SESSION['ACGCompanyId']."";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from overheadgroupmaster where GroupId  =".$rowCate['GroupId']." and CompanyId=".$_SESSION['ACGCompanyId']."";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$pcode)
				 $px="selected";
 		?>
 				<option value="<?php echo $rowCate['GroupId'];?>" <?php echo $px ?>><?php echo $dashVal;?>
				 <?php echo $rowCate['GroupName'];?> </option>
	   <?php			
				   CategoryShow($pcode,$rowCate['GroupId'], $dashVal);
			}
		}
	}   
   ?> 
   <select name="MainGroupId" id="MainGroupId" style="width:150px;height:20px">     <!--ShowHide(this.value,'natureofgroup')-->
  <option value="0">Primary</option>
  <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  overheadgroupmaster where MainGroupId = 0 and CompanyId=".$_SESSION['ACGCompanyId']."";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from overheadgroupmaster where MainGroupId   =".$rowCate['GroupId']." and CompanyId=".$_SESSION['ACGCompanyId']."";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$pcode)
				 $px="selected";
			echo $dashVal;	
		  ?>
		<option value="<?php echo $rowCate['GroupId'];?>"<?php echo $px ?>> 
		<?php echo  $rowCate['GroupName'] ;?></option>
 		<?php			
			 CategoryShow($pcode, $rowCate['GroupId'], $dashVal);
		}
	}
?></select>
	
	
	
	
	</td>
	
	
  </tr>
<tr class="row1">
	<td class="tabledata1">Sequence</td>
    <td><input name="sequence" type="text" style="width:150px" value="<?php echo $sequence;?>"></td>
  </tr>
<tr class="row1">
	<td class="tabledata1">Status</td>
    <td><select name="status" type="text" style="width:150px">
<option value="" <?php if($status1==''){echo "selected";}?>>--select--</option>
<option value="1" <?php if($status1=='1'){echo "selected";}?>>Active</option>
<option value="0" <?php if($status1=='0'){echo "selected";}?>>Deactive</option>
</select>
</td>
  </tr>
<tr class="row1">
	<td class="tabledata1" style="text-align:center;" colspan="2">
      <input type="submit" name="Save" class="search" value="<?php if($_REQUEST['action']=='Edit'){ echo 'Update';}else{echo 'Save';}?>">
		<input type="reset" name="Cancel" class="search" value="Cancel"/>
    </td>
</tr>
</table>
</form>


<table width="80%" border="0" cellspacing="2" cellpadding="2">
   <tr>
   <th>GroupId</th>
   <th>GroupName</th>
   <th>Under</th>
	<th>Sequence</th>
	<th>Status</th>
	<th>Edit</th>
   <th>Delete</th>
   </tr>
   <?php 
      // LIMIT $startpoint,$perpage
   $GetRecord = "Select * from $table where CompanyId=".$_SESSION['ACGCompanyId']."  order by MainGroupId asc, sequence asc";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {
  
   		//$check_ledger=DB_query("select * from accountledger where UnderGroup='".$RowData['GroupId']."'",$db);
		//$data1=DB_num_rows($check_ledger);
		//$check_under_group=DB_query("select * from accountgroupmaster where MainGroupId='".$RowData['GroupId']."'",$db);
		//$data2=DB_num_rows($check_under_group);
		
   ?>
 <tr bgcolor="#FFFFFF">
 <td width="10%"><div align="center"><?php echo $i; ?></div></td>
    <td width="20%"><?php echo $RowData['GroupName'];?>	  </td>
	  <td width="20%"><?php 
	   
	    $parent= $s->getField($table,'GroupId',$RowData['MainGroupId'],'GroupName');	 
	  if($parent!='')
	  echo $parent;
	  else{
	  echo "Primary";
	  }
 	   ?></td>
   <td width="10%"><?php echo $RowData['sequence'];?>	  </td>
<td width="10%"><?php if($RowData['status']==1){echo "Active";}else{echo "Deactive";}?>	  </td>
     
   <td width="10%"><div align="center"><a href="index.php?route=Production/overheadgroup_master&action=Edit&GroupId=<?php echo $RowData['GroupId'].$url; ?>"><img src="images/icon-edit.png"  width="10" height="10"/></a></div></td>
	<td width="20%"><div align="center"><?php //if($data1=='0' && $data2=='0'){?><a href="index.php?route=Production/overheadgroup_master&action=del&GroupId=<?php echo $RowData['GroupId'].$url;; ?>" onclick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="10" height="10" /></a><?php //}else{ echo "Cannot be deleted"; } ?></div></td>	
	 </tr>
 
	<?php $i++;}   
   ?> 
   
    
    <!-- <td colspan="4">
	<?php  
	//$cond="1=1";
	//Pages($table,20,"index.php?route=AccountGroups_Master&action=Edit".$url,$cond,'GroupId');
	 //echo Pages("jos_users",$perpage,"index.php?option=com_empdictory&dept=$dept&",$condition);?>
	</td> -->
   
  </tr>
 </table>
