<?php   
$PageSecurity = 1;
include('includes/AccountGroupMaster_Class.php');
include('includes/PaginationClass.php');
include('includes/fun-HRClass.php');
include('includes/fun-php.php');
$table='bil_chamber';

function yearsDifference($endDate, $beginDate)
{
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   return $date_parts2[0] - $date_parts1[0];
}

if($_REQUEST['Save'] && $_REQUEST['action']!='Edit')
{
$data['chamber']=$_REQUEST['Chamber'];
$data['chamber_code']=$_REQUEST['chamber_code'];
$data['CompanyId']=$_SESSION['ACGCompanyId'];
$data['Created_date']=date('Y-m-d');
$data['Created_by']=$_SESSION['UserID'];
$s->RecordInsert($table,$data);
//$s->pageLocation('index.php?route=Voucher_Master&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
	
if(!$_REQUEST['Save'] &&   $_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['VoucherType']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$chamber_code=$RData['chamber_code'];
	$Chamber=$RData['chamber'];
	
}	
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
 $data['chamber']=$_REQUEST['Chamber'];
 $data['chamber_code']=$_REQUEST['chamber_code'];
 $data['Modified_date']=date('Y-m-d');
 $data['Modified_by']=$_SESSION['UserID'];
  $cond="id='".$_REQUEST['VoucherType']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  prnMsg( _('Record Updated successfully'),'success');
}

if($_REQUEST['action']=='del')
{
	$condition="id='".$_REQUEST['VoucherType']."'";
	$s->DeleteRecord($table,$condition);
	prnMsg( _('Record deleted successfully'),'success');
	$s->pageLocation('index.php?route=Billing/Masters/chamber_master&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
if($_REQUEST['submit']!='')
{
     if($_REQUEST['type']=='') { $_REQUEST['type']='CA'; }
        if($_REQUEST['type']!='')
       {
        $filter[]="a.act = '".$_REQUEST['type']."'";
       }
       if($_REQUEST['category']!='')
       {
        $filter[]="a.method = '".$_REQUEST['category']."'";
       }
    
       $filter[]="a.CompanyId='".$_SESSION['ACGCompanyId']."'";
       if($filter!='')
	{
           $filter=implode(" and ",$filter);
            $query="select * from depretiation_calc a  where $filter";
           // $maxSql="select MIN(date) as  maxInstallationdate from machine_center where $filter and dep_as_per_com!='0'";
         
        //echo $query;
        }
}
?>
<link rel="stylesheet" href="css/new_mxerp1.css">
<!--<script src="js/jquery10.js" type="text/javascript"></script>-->
<script src="js/settings.js" type="text/javascript"></script>
<div class="new_tab">
<div class="inside_tab">
	<ul>
		<li><a href="index.php?route=ToolPlants/depcIndex&modid=<?php echo  $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid']?>&tab=1&type=CA">Company Act</a></li>
		<li><a href="index.php?route=ToolPlants/depcIndex&modid=<?php  echo $_REQUEST['modid']?>&frmid=<?php echo $_REQUEST['frmid']?>&tab=2&type=IA">Income Act</a></li>
		<div style="clear:both;"></div>
	</ul>
</div>
</div>
<div style="height:3px;"></div>
<div class="body_contain">
    <div class="tab_div">
       <form action="" method="post" name="myform">
<table width="100%" cellspacing="0" cellpadding="3">
<tr class="row1">
        <td class="tabledata1"><strong> Method of Depreciation <span style="color:red;">*</span></strong></td>
        <td>
            <input <?php if($_REQUEST['category']=='SLM' || $_REQUEST['category']=='') { echo 'checked'; } else { echo ''; }?> type="radio" name="category" id="category" value="SLM" style="width:150px;"> SLM
            <?php if($_REQUEST['type']=='CA' || $_REQUEST['type']=='') { ?>
            <input type="radio" <?php if($_REQUEST['category']=='WDV') { echo 'checked'; } else { echo ''; }?> name="category" id="category" value="WDV" style="width:150px;"> WDV
            <input <?php if($_REQUEST['category']=='Both') { echo 'checked'; } else { echo ''; }?> type="radio" name="category" id="category" value="Both" style="width:150px;"> Both
                <?php } ?>
        </td>
	
  </tr>
  
  
  <tr class="row1">
      <td colspan="8" style="text-align: center;"><input type="submit" name="submit" class="search" value="Search" /></td>
  </tr>
  
</table>
           <br/>
<?php
if($_REQUEST['submit']!='')
{ 
     $maxSql="select MIN(b.inst_date) as  maxInstallationdate from depretiation_calc a inner join depretiation_calc_details b on a.id=b.parent_id  where $filter";
          
         $maxSqlExec = DB_query($maxSql,$db);
         $maxdata = DB_fetch_array($maxSqlExec);
        echo $maxinstallationDate=$maxdata['maxInstallationdate'];
         $insArray= explode('-', $maxinstallationDate);
        $insYear=$insArray[0];
    
        //echo $query;
        $sqlExec = DB_query($query,$db);
       while($RowData = DB_fetch_array($sqlExec))
       {          
         $dataArray[]=$RowData;
        
         $depPeriod=yearsDifference($RowData['Created_date'],$maxinstallationDate)-1; 
       }
       //echo $depPeriod;
       
 ?>
           <fieldset>
    <legend><?php if($_REQUEST['type']=='CA' || $_REQUEST['type']=='') { echo 'Company Act'; } else { 'Income Act'; } ?></legend>
            <table width="100%" border="0" cellpadding="3" cellspacing="0" >
    <tr>
         <td width="25">
            <div style="width:25px;height: 25px;background-color:#FFA500; border-radius:2px; font-family: serif;" ></div>
        </td>
        <td width="100">Company Act</td>
        <td width="25">
            <div style="width:25px;height: 25px;background-color:#47D1D1;margin-left: 30px;border-radius:2px; font-family: serif;" ></div>
        </td>
        <td width="100">Income Act</td>
        <td width="25">
        </td>
        <td></td>
  
        
    </tr> 
</table>
    <div style="width:100%; overflow: auto;">
<table width="130%" cellspacing="0" cellpadding="3" border="1">
    <tr class="row1">
        <td><strong>Sr No.</strong></td>
        <td><strong>Act</strong></td>
        <td><strong>Asset Code</strong></td>
        <td><strong>Asset Name</strong></td>
        <td><strong>Method of Depreciation</strong></td>
        <td style="width:50px;"><strong>From Date</strong></td>
        <td style="width:50px;"><strong>To Date</strong></td>
        <td><strong>Capitalisation Date</strong></td>
        <td><strong>Period of Depreciation</strong></td>
       <td ><strong>Depreciation</strong></td>
        <?php if($_REQUEST['type']=='CA'|| $_REQUEST['type']=='') {
            
             if($_REQUEST['category']=='SLM' || $_REQUEST['category']=='Both')
            { // slm
            ?>
        <td><strong>Additional Year</strong></td>
        
        <?php } if($_REQUEST['category']=='WDV' || $_REQUEST['category']=='Both') { 
            $a=$insYear;// WDV 
            $b=0;
        for($i=0; $i<$depPeriod; $i++)
        { ?>
        <td>
        <?php 
            $b=$a+1;            
            echo '<strong>'.$a.'-'.$b.'</strong>';
            ?>
        
      </td>
            <?php
           $a=$b;  } ?>
        <?php }
        }
 else {
     if($_REQUEST['category']=='SLM' || $_REQUEST['category']=='Both') 
     {
     ?>
     <td><strong>Additional Year</strong></td>
        
     <?php }
     if($_REQUEST['category']=='WDV' || $_REQUEST['category']=='Both') { 
            $a=$insYear;// WDV 
            $b=0;
        for($i=0; $i<$depPeriod; $i++)
        { ?>
        <td>
        <?php 
            $b=$a+1;            
            echo '<strong>'.$a.'-'.$b.'</strong>';
            ?>
        
      </td>
            <?php
           $a=$b;  } ?>
        <?php }
     
     
     
     } ?>
        </td>
    </tr>
    <?php 
   //echo $query;
     $i=0;
       if($dataArray!='')
       {  
       foreach($dataArray as $RowData){
           $detailsQuery="select * from depretiation_calc_details where parent_id='".$RowData['id']."'";
           $detailsQueryExec = DB_query($detailsQuery,$db);
           while($detData = DB_fetch_array($detailsQueryExec))
       {
           $instYrArray = explode('-', $detData['inst_date']);
           $instYr=$instYrArray[0];
            if($RowData['act']=='CA'){ $color='#FFA500';} elseif($RowData['act']=='IA'){ $color='#47D1D1';}
           //echo $_SESSION['financialYrStart'].'--------'.$RowData['date'];
            
    ?>
    <tr class="row" bgcolor="<?php echo $color; ?>">
        <td><?php echo ($i+1).'.'; ?></td>
        <td><?php if($RowData['act']=='CA') { echo 'Company Act'; } else { echo 'Income Act'; } ?></td>
        <td><?php echo $s->GetFieldValue('machine_center',"id='".$detData['asset_id']."'",'code'); ?></td>
        <td><?php echo $s->GetFieldValue('machine_center',"id='".$detData['asset_id']."'",'name'); ?></td>        
        <td><?php if($_REQUEST['category']=='Both'){ echo 'WDV & SLM'; } else { echo $_REQUEST['category'];} ?></td>
        <td><?php echo ConvertSQLDate($RowData['fromdate']); ?></td>
        <td><?php echo ConvertSQLDate($RowData['todate']); ?></td>
        <td><?php echo ConvertSQLDate($detData['inst_date']); ?></td>
        <td><?php echo $detData['period']; ?></td>
        <td>Dep. Amt.<br/>Net Amt.</td>
        
        <?php if($_REQUEST['type']=='CA' || $_REQUEST['type']=='') {
            
             if($_REQUEST['category']=='SLM'  || $_REQUEST['category']=='Both')
            { // slm
            ?>
       <td><?php echo $detData['dept_amt']; ?>           
           <br/>
       <?php echo $detData['net_amt']; ?>
       </td>
        <?php } if($_REQUEST['category']=='WDV'  || $_REQUEST['category']=='Both') { 
            $a=$insYear;// WDV 
            $b=0;
            $deptAmtArray=explode(',', $detData['dept_amt']);
            print_r($deptAmtArray);
        for($j=0; $j<$depPeriod; $j++)
        { ?>
        <td>
        <?php 
            $b=$a+1;            
            
             //echo $instYr.'---'.$a;
             if($a>=$instYr)
             { 
                 
                echo $j; 
//             /echo $deptAmt=$deptAmtArray[$j];
             //$origAqAmt=$depAmount-$origAqAmt;
             ?>
            
                 <?php
             }
             
                 else { $j=-1;
                 echo $instYr.'---'.$a;
             if($a>=$instYr)
                 if($a>=$instYr)
             {
                     echo $j; 
             }
             }
           $a=$b;?>
        <br/>
        <?php
        if($a>=$instYr)
             { //echo $RowData['accumlative_depriciation_dep'].'-----'.$origAqAmt.'<br/>';
            //echo $deptAmt=round($detData['accum_amt']-$deptAmt, 2);?>
   <?php }
        ?></td>
            <?php
            
            $detData['accum_amt']=$deptAmt;
              $a=$b; } ?>
        <?php }
        } else {
            if($_REQUEST['category']=='SLM' || $_REQUEST['category']=='Both') 
     {
            ?>
        <td><?php if($depPeriodReal==0) {
        $depPeriodReal=1;}
            echo $depAmt=round(($origAqAmt*$depRateslm)/(12*$depPeriodReal*100), 2); ?>
            <input type="hidden" name="dept_amts[<?php echo $i; ?>]" value="<?php echo $depAmt; ?>" />
        <br/><?php echo round(($origAqAmt-$depAmt), 2); ?>
        <input type="hidden" name="net_amts[<?php echo $i; ?>]" value="<?php echo round(($origAqAmt-$depAmt), 2); ?>" />
        </td>
        <?php }
        if($_REQUEST['category']=='WDV'  || $_REQUEST['category']=='Both') { 
            $a=$insYear;// WDV 
            $b=0;
        for($j=0; $j<$depPeriod; $j++)
        {?>
        <td>
        <?php 
            $b=$a+1;            
             '<strong>Depreciation for '.$a.'--'.$b.'</strong>';
             //echo $instYr.'---'.$a;
             if($a>=$instYr)
             {
             echo $origAqAmt=round(($RowData['accumlative_depriciation_dep']*$depRatewdv)/100, 2);
             //$origAqAmt=$depAmount-$origAqAmt;
             ?>
            <input type="hidden" name="dept_amt[<?php echo $i; ?>][<?php echo $j; ?>]" value="<?php echo $origAqAmt; ?>" />
            <?php
             }
           ?>
        <br/>
        <?php
        if($a>=$instYr)
             { //echo $RowData['accumlative_depriciation_dep'].'-----'.$origAqAmt.'<br/>';
            echo $origAqAmt=round($RowData['accumlative_depriciation_dep']-$origAqAmt, 2);?>
        <input type="hidden" name="net_amt[<?php echo $i; ?>][<?php echo $j; ?>]" value="<?php echo $origAqAmt; ?>" />
       <?php }
        ?></td>
            <?php
            
            $RowData['accumlative_depriciation_dep']=$origAqAmt;
              $a=$b; } ?>
        <?php }
        
        
     } ?>
    </tr>
    
       <?php $i++; }}?>
    <input type="hidden" name="counter" id="counter" value="<?php echo $i; ?>" />
    <?php } else { ?>
    <tr>
        <td style="color:red;" colspan="50">No Record Found !!</td>
    </tr>
<?php } ?>	   
</table>
           </div>
     </fieldset>
<?php } ?>
</form> 
    </div>
</div>

