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

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=Account/AccountGroups_Master".$url;
if($_REQUEST['Save'])
{
	if(empty($_REQUEST['GroupName']))
	{
	   $msgErr[]='Please Enter Group Name';
	   $status=false;
	}
	 
	 
	 	$condition='GroupName="'.$_REQUEST['GroupName'].'"';
		$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){
	echo "<div class='success'>Can't Delete This Record because AccountName Attatch with LedgerDayBook table.</div>";
	$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='success'>Can't Delete This Record. First Delete it's Parent Record.</div>";
			$s->pageLocation($targetpage.'&daction=2');
 		}
		else{
		 $condition="GroupId='".$_REQUEST['GroupId']."'";
	     $s->DeleteRecord($table,$condition);
		 echo "<div class='success'>Record Delete Sucessfully</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'];
  $NatureOfGroup=$RData['NatureOfGroup'];
   $Type=$RData['Type'];
			 
}
 

if($_REQUEST['Save'] &&  $status && $action=='Edit')
{
 $data['GroupName']=$_REQUEST['GroupName'];
  $data['MainGroupId']=$_REQUEST['MainGroupId'];
   $data['NatureOfGroup']=$_REQUEST['NatureOfGroup'];
  $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 Sucessfully</div>";
  $s->pageLocation($targetpage.'&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{    $data=array();
 	$data['GroupName']=$_REQUEST['GroupName'];
   	$data['MainGroupId']=$_REQUEST['MainGroupId'];
  	$data['NatureOfGroup']=$_REQUEST['NatureOfGroup'];
   	$data['Type']='G';	

	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
echo "<div class='success'>Record Inserted Sucessfully</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>
<!--<p align="right" style="float:right;"><A HREF="javascript:history.go(-1)" class="search">Go Back</A></p>-->
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <?php if(isset($daction))
  {?>
<tr class="row1">
    <td colspan="6" ><!--<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 Parent Record   ";
		 ?>
	</div>--></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
<tr class="row1">
    <td colspan="6" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>
   
<tr class="row1">
	<td width="13%" align="right">Group</td>
    <td width="20%"><input name="GroupName" id="GroupName" type="text"  value="<?php echo $GroupName;?>"  onkeyup="return removewhitespace(this.value,this.id)"></td>
	<td width="13%" align="right">Under Group</td>
    <td width="20%"><?php //  $AccountMaster->AccountGroup($MainGroupId,'MainGroupId'); 
 	$pcode= $MainGroupId;
 	function CategoryShow($pcode, $PcateID, $dashVal)
	{
	    global $db;
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."' and Type!='N'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from accountgroupmaster where GroupId  =".$rowCate['GroupId']."  and Type!='N'";
				$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" onchange="activeDeactive(this.value,'NatureOfGroup')" >     <!--ShowHide(this.value,'natureofgroup')-->
  <option value="0">Primary</option>
  <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  accountgroupmaster where MainGroupId = 0 and Type!='N'";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from accountgroupmaster where MainGroupId   =".$rowCate['GroupId']." and Type!='N'";
				$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>
  <?php  
  if($_REQUEST['action']=='Edit')
  {
  	if($NatureOfGroup=='0')
	{$disabled='disabled';}else{$disabled='';}
  }
  else
  {
  	$disabled='';
  }
  
  ?>
	<td width="13%" align="right">Nature of Group</td>
	<td><select name="NatureOfGroup" id="NatureOfGroup" <?php echo $disabled;?> >
<option value="">Select</option>
<?php 
$nature_sql=DB_query("Select * from accountgroupmaster where Type='N'",$db);
while($nature_row=DB_fetch_array($nature_sql)){
?>
	<option value="<?php echo $nature_row['GroupId'];?>" <?php if($NatureOfGroup==$nature_row['GroupId']) echo "selected";?>><?php echo $nature_row['GroupName'];?></option>
<?php  }?>
</select></td>
  </tr>
  
<tr class="row1">
	<td style="text-align:center;" colspan="6"><input type="submit" name="Save" class="search" value="<?php if($_REQUEST['action']=='Edit') {echo 'Update';}else{echo 'Save';}?>" onclick="return checkNature()" /></td>
</tr>
</table>
</form>
 <script language="javascript" type="text/javascript">
 function checkNature()
 {
	if(document.getElementById("MainGroupId").value=='0')
	{
		if(document.getElementById("NatureOfGroup").value=='')
		{alert('Please select nature for your primary group'); return false;}
	}
 }
 function activeDeactive(str,target)
 {
	if(str=='0'){document.getElementById(target).disabled=false;}
	else{document.getElementById(target).disabled=true;}
 }
 </script>

<br>
<?php include('ViewGroupMaster.php') ?> 

</div>
</div>
