<section class="invoice-tabs content">
<div class="panel">
    <div class="panel-heading text-center">BUILDING REPORT</div>
<div class="panel-body">
<?php
    $date_to            = date("Y-m-d");
        $lease_details = Yii::app()->db->createCommand()
                        ->select('COUNT(DISTINCT(flat_id)) AS occupied')
                        ->from("{{lease}}")
                        ->where('building_id='.$model->id)
                        ->andWhere('from_date<="'.$date_to.'"')
                        ->andWhere('to_date>="'.$date_to.'"')
                        ->queryRow();
        $flat_details = Yii::app()->db->createCommand()
                        ->select('COUNT(id) AS total')
                        ->from("{{flat}}")
                        ->where('building_id='.$model->id)
                        ->queryRow();
        $vaccant             = $flat_details['total']-$lease_details['occupied'];
       
?>
<table id="tbl_summary" class="table table-striped table-primary">
    <?php
    if($model->purchase_status==1){
        $buying_value=$model->buying_value;
    }else{
        $model_cost = Flat::model()->findBySql('select sum(`buying_value`) as `buying_value` from re_flat where building_id="'.$model->id.'"', array());
        $buying_value=$model_cost->buying_value;
    }
    ?>
    <tr>
        <th>Buying Date</th>
        <td><?php echo date_format(date_create($model->buying_date),'d/m/Y');?></td>
        <th>Building Name</th>
        <td><?php echo $model->building_name;?></td>
        <th>Buying Value</th>
        <td class="text-right"><?php echo Yii::app()->numberFormatter->formatCurrency($buying_value,"");?></td>
    </tr>
    <tr>
        <th>Location</th>
        <td><?php echo $model->location;?></td>
        <th>State</th>
        <td><?php echo $model->state;?></td>
        <th>Country</th>
        <td class="text-right"><?php echo $model->country_name->country_name;?></td>
    </tr>
    <tr>
        <th>Total Flats</th>
        <td><?php echo $flat_details['total'];?></td>
        <th>Occupied</th>
        <td><?php echo $lease_details['occupied'];?></td>
        <th>Vaccant</th>
        <td><?php echo $vaccant;?></td>
    </tr>
   
    <?php
    if($model->purchase_status==1){
    $model_details  = BuildingOwnerDetails::model()->findAll(array('condition' => 'building='.$model->id));
    if(!empty($model_details)){?>
     <tr><td colspan="6" class="text-center"><b>OWNER DETAILS</b></td></tr>
    <tr>
        <th colspan="4">Owners</th>
        <th>Percentage</th>
        <th>Buying Value</th>
    </tr>
    <?php
        foreach($model_details as $i=>$model_detail){?>
        <tr>
            <td colspan="4"><?php echo Landlord::getLandlordName($model_detail->landlord);?></td>
            <td class="text-right"><?php echo $model_detail->percentage.'%';?></td>
            <td class="text-right"><?php $percent_owner=$model_detail->percentage;
                $buy_percent=($model->buying_value*$percent_owner)/100;
                echo Yii::app()->numberFormatter->formatCurrency($buy_percent,"");
            ?></td>
        </tr>
        <?php } ?>
     <tr><td colspan="6" class="text-center"><b>FLAT DETAILS</b></td></tr>  
      <tr>
        <th>Flat No</th>
        <th>Block</th>
        <th>Floor</th>
        <th colspan="2">Flat Type</th>
        <th>Building Class</th>
    </tr>
     <?php
     $model_flat  = Flat::model()->findAll(array('condition' => 'building_id='.$model->id));
      if(!empty($model_flat)){
          foreach($model_flat as $i=>$flat_detail){?>
        <tr>
            <td><?php echo $flat_detail->flat_no;?></td>
            <td><?php echo $flat_detail->block;?></td>
            <td><?php echo $flat_detail->floor;?></td>
            <td colspan="2"><?php  if($flat_detail->flat_type==1){
                echo "Residential";
            }else{
                echo "Commercial";
            }?></td>         
           <td><?php echo $flat_detail->building_class->class_name;?></td>
        </tr>
     <?php
        }
      }
     ?>
    <?php    
    }
    }else{ ?>
        <tr><td colspan="6" class="text-center"><b>FLAT DETAILS</b></td></tr>  
      <tr>
        <th>Flat No</th>
        <th>Block</th>
        <th>Floor</th>
        <th>Flat Type</th>
        <th>Building Class</th>
        <th>Buying Value</th>

    </tr>
     <?php
     $model_flat  = Flat::model()->findAll(array('condition' => 'building_id='.$model->id));
      if(!empty($model_flat)){
          foreach($model_flat as $i=>$flat_detail){?>
        <tr>
            <td><?php echo $flat_detail->flat_no;?></td>
            <td><?php echo $flat_detail->block;?></td>
            <td><?php echo $flat_detail->floor;?></td>
            <td><?php  if($flat_detail->flat_type==1){
                echo "Residential";
            }else{
                echo "Commercial";
            }?></td>         
            <td><?php echo $flat_detail->building_class->class_name;?></td>
            <td class="text-right"><?php  echo Yii::app()->numberFormatter->formatCurrency($flat_detail->buying_value,"");
            ?></td>
        </tr>
     <?php
        }
      }
     ?>
   <?php }
    ?>
</table>
</div></div></section>