<!-- Start forms -->
<div class="page">  
    <div class="content">    
        <section class="panel">     
           <div class="panel-body">   
                <?php          
                    if($date_from=="" || $date_from=='0000-00-00')         
                        $date_from=date("Y-m-d");         
                    ?>
                <?php               $ts2 = strtotime($date_from);       
                foreach ($building_model as $i=>$row) {        
                    $each_buying_value = $row->buying_value;        
                    $each_buying_date  = $row->buying_date;        
                    $each_depreciation_months=$row->depreciation_months;        
                    $ts1 = strtotime($each_buying_date);        
                    $mdate =$ts1;                       
                    $year1 = date('Y', $ts1);        
                    $year2 = date('Y', $ts2);        
                    $month1 = date('m', $ts1);        
                    $month2 = date('m', $ts2);        
                    $diff = (($year2 - $year1) * 12) + ($month2 - $month1);        
                    $cur_amnt = $each_buying_value;        
                    $calculation_table = '';        
                    if($diff ==0){          
                        $each_depreciation = 0;           
                        $calculation_table .= '<tr><td colspan="4" class="text-center">No Details Found</td> </tr>'; 
                    }else{
                        if($each_depreciation_months != 0)              
                            $each_depreciation = ($each_buying_value * $diff)/$each_depreciation_months;            
                        else {
                            $each_depreciation =0;            
                            
                        }            
                        $current_value =$each_buying_value;            
                        $depr_month    = 0;            
                        $depr_per_month = $each_buying_value/$each_depreciation_months;            
                        for($i=1;$i<=$diff;$i++){
                            $buying_value=$current_value-$depr_month;              
                            $depr_month += $depr_per_month;                
                            if($cur_amnt > 0){
                                $cur_amnt= number_format((float)$buying_value, 2, '.', '')-number_format((float)$depr_per_month, 2, '.', '');
                                $dateObj   = DateTime::createFromFormat('!m', $month1);                    
                                $monthName = $dateObj->format('F');                    
                                $monthyear = $monthName.' '.$year1;                    
                                $mdate = strtotime(date("Y-m-d", $mdate) . " +1 month");                    
                                $year1 = date('Y', $mdate);                    
                                $month1 = date('m', $mdate);
                                $calculation_table.='<tr>
                                                        <td>'.$monthyear.'</td>
                                                        <td class="text-right">'.Yii::app()->numberFormatter->formatCurrency($buying_value,"").'</td>                            <td class="text-right">'.Yii::app()->numberFormatter->formatCurrency($depr_per_month,"").'</td>'
                                        . '             <td class="text-right">'.Yii::app()->numberFormatter->formatCurrency($cur_amnt,"").'</td>'
                                        . '       </tr>';
                                $final_deprn=$each_buying_value-$cur_amnt;
                            }             

                        }
                    }        ?>         
                <input type="hidden" name="date" id="date" value="<?php echo $date_from;?>" >         
                <input type="hidden" name="building" id="building" value="<?php echo $building;?>" >
                <table class="table table-fixed table-primary">
                    <tr>
                        <th class="col-xs-1 nw">Building</th>
                        <td class="col-xs-2"> <b> <?php echo $row->building_name; ?></b></td>
                        <th class="col-xs-1 nw">No.of Months</th>
                        <td class="col-xs-1"> <b> <?php echo $diff; ?></b></td>
                        <th class="col-xs-1 nw">Value</th>
                        <td class="col-xs-1"> <b> <?php echo Yii::app()->numberFormatter->formatCurrency($each_buying_value,""); ?></b></td>
                        <th class="col-xs-1 nw">Current Value</th>
                        <td class="col-xs-1"> <b> <?php echo Yii::app()->numberFormatter->formatCurrency($cur_amnt,""); ?></b></td>
                        <th class="col-xs-1 nw">Depreciation</th>
                        <td class="col-xs-2"> <b> <?php echo Yii::app()->numberFormatter->formatCurrency($final_deprn,""); ?></b></td>
                    </tr>        
                </table>        
                <table id="tbl_magazine_table" class="table table-fixed table-primary">
                    <thead>
                        <tr>                  
                            <th class="col-xs-3 text-center">Month</th>
                            <th class="col-xs-3 text-center">Value</th>
                            <th class="col-xs-3 text-center">Depreciation</th>
                            <th class="col-xs-3 text-center">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                  <?php echo $calculation_table;?>
                    </tbody>
                </table>
        <?php            }            ?>
<!--                <div align="center"> 
                    <input type="button" onclick="depreciationprint();" value="Print" class="btn btn-primary">
                    <button onclick="excelDepreciation();" class="btn btn-primary" type="button">Export Excel</button>
                </div>      -->
            </div>
        </section>
    </div>

    </div>
<!-- End forms -->
    <?php $cs = Yii::app()->getClientScript(); 
    $cs->registerScript("depreciationprint", "function depreciationprint(){
            var url= '" . Yii::app()->createUrl('purchasedbuilding/PrintDepreciationPDF') . "';    
            var date_from=$('#date').val();
            var building=$('#building').val();
            window.open($('#liveSite').val()+'administrator.php?r=purchasedbuilding/PrintDepreciationPDF&date_from='+date_from+'&building='+building, '_blank');
    }", CClientScript::POS_HEAD);
    $cs->registerScript("excelDepreciation", "function excelDepreciation(){
                var url= '" . Yii::app()->createUrl('purchasedbuilding/DepreciationExcel') . "';
                var date_from=$('#date').val();
                var building=$('#building').val();
                window.open($('#liveSite').val()+'administrator.php?r=purchasedbuilding/DepreciationExcel&date_from='+date_from+'&building='+building, '_blank');
    }", CClientScript::POS_HEAD);