<?php 
/**
 * Balance Sheet Created on 31-1-2018
 * Author jo~
 */
?>
<style type="text/css">
@media print {
    @page {
    margin-top: 1.3cm;
    margin-bottom: 3cm;
  }
       *{

        box-sizing: border-box;

    }
  .heading{

    width: 100%;

    float: left;

    text-align: left;

  }

  .heading-img{

    float: right;

    max-width: 200px;

  }

  .heading h4{

    margin: 8px 0;

    float: left;

    font-size: 16px;

  }

  .heading small{

    float: left;

    clear: both;

    font-size: 12px;

    font-weight: 300;

  }

      .logo img{
    height: 75px;
  }
}
.left-col {
    float: left;
    width:50%;
}
.center-col {
    float: left;
    width: 20%;
}
.right-col {
    float: left;
    width: 28%;
    padding: 6px;
    float: right;
}


    .table-tree{
        width:100%;
        text-align:left;
        padding-left:10px !important;
        font-size: 12px;
        border-collapse: collapse;
        border-spacing: 0;
    }
    .table-tree th{
        padding-top:5px;
        padding-bottom:5px;
        border-top:1px solid #000;
        border-bottom:1px solid #000;
    }
    .table-tree th, td{
        border-left: 0;
        border-right: 0;
        margin: 0;
        padding: 3px 5px;
    }
    .ze{background: #CCC}
    .box-pad,.box-pad *{
        -moz-box-sizing: border-box;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
    }
    .tre td{
        border:none;
    }
    .level-1{
        padding-left:5px !important;
    }
    .level-2{
        padding-left: 20px !important;

    }
    .level-3{
        padding-left: 35px !important;
    }
    .level-4{
        padding-left: 55px !important;
    }
    .l1{
        font-size: 12px !important;
    }
    .l2{
        font-size: 11px !important;
        //font-style: italic !important;
    }
    .l3{
        font-size: 12px !important;
    }

    .l4{
        font-size: 11px !important;
        font-style: italic;
    }
    .txt-right{
        text-align: right !important;
    }

    .tittle{

        width: 100%;

        margin:5px 0;

        float: left;

        text-align: center;



    }

    .tittle h6{

        border-bottom:2px solid #000;

        width: 150px;

        max-width: 400px;

        font-size: 16px;

        margin: 5px auto;

    }

    .date{
        width: 100%;
        float: left;
        font-size: 11px;
        padding-left: 5px;
        font-weight: 600;
        text-align: left;
        padding-bottom: 3px;
    }

    .date span{

        width: 200px;

        display: inline-block;

    }
    .clear{
        clear: both;
    }
    .cell-left{
      text-align: left !important;
    }
    .cell-right{
      text-align: right !important;
    }
    .br-top{
      border-top: 1px solid #000 !important;
      border-bottom: 3px double #000 !important;
    }
    .br-u{
      border-bottom: 1px solid #000 !important;
    }
    .space{
      width: 100%;
      float: left;
      clear: both;
      height: 5px;
    }
    .col-6{
      width: 50%;
    }
    .col-9{
      width: 75%
    }
    .col-3{
      width:25%;
    }
    .f-l{
      float: left;
    }
    .f-r{
      float: right;
    }
    .txt-right{
      text-align: right;
    }
    .txt-left{
      text-align: left;
    }
    .x-space{
      box-sizing: border-box;
    }
    .bg-w{
      background: #FFF !important;
    }
    .footer {
      width:100%;
      float:left;
      font-size:11px;
      position:absolute;
      bottom:0px;
      left:0px;
      text-align:center !important;
      line-height:18px;
      border-top: 1px dotted #000;
      padding: 5px;
    }
    .span1{
        width: 80px !important;
        display: inline-block;
        float: left;
    }
    .span2{
        width: 80px !important;
        display: inline-block;
        float: left;
    }
    .span3{
        width: auto !important;
        display: inline-block;
        float: left;
    }
    .pro{
        background: #ccffdf;
    }
    .loss{
        background: #ffb4b4;
    }
    .cy{
        background: #cdcdcd;
    }
    .py{
        background: #ececec;
    }
    .to{
        background: #f1f1f1;
    }
    .tax{
        background: #c1d1f0;
    }
}   
</style>
<?php
    $year=date('Y', strtotime($date_to));
    $date_from=$year.'-01-01';
    $year=2018; 
    $from=$year.'-01-01';
?>
  <div class="container">
    <div class="heading">
        <?php echo CHtml::image(Yii::app()->baseUrl . '/images/logo/'.Yii::app()->user->pdf_company_logo,'',array('class'=>'heading-img')); ?>
        <div style="width:70%; float:left"><h2><?php echo Yii::app()->user->master_company_name; ?><small></br><br></small></h2></div>
    </div>
    <br>
<div class="tittle"><h6>Balance Sheet</h6></div><br>
    <div id="finance-account-trialbal">
        <table class="table-tree">
            <thead>
                <tr>
                    <th class="cell-left">Account No</th>
                    <th class="cell-left">Description</th>
                    <th>Amount</th>
                </tr>
        </thead>
        <tbody>
        <?php
        /**
         * Assets
         */
        $query = "SELECT * from crm_balancesheet_sort_order where account_type='A'";
        $allAssets 	= Yii::app()->db->createCommand($query)->queryAll();
        
        $previous  = 'H'; 
        $total  =  0;
        $assets_total = 0;
        $assets = 0;
        $depreciation = 0;
        foreach ($allAssets as $row)
        {
            $amount =  "0.00";
            $name   =  $row['description'];
            $opening_balance = 0.00;
            if( $row['type'] == 'A' )
                $opening_balance = FinAccounts::model()->find(array('condition' => 'account_no ='.$row['account_no']))->opening_bal;
           
            $total += $opening_balance;
            $assets_total += $opening_balance;
            if( $row['type'] == 'T' ){
                echo '<tr><td></td><td></td></tr><tr class="tax"><td colspan="2"><b>'.$row['description'].'<b></td><td class="txt-right"><b>'.Yii::app()->numberFormatter->formatCurrency( ($assets_total - ( $assets - $depreciation  ) ),"").'<b></td></tr><tr><td></td><td></td></tr>';
            }if( $previous == 'A' && $row['type'] == 'E' ){
                if(  $row['type'] == 'E' &&  $row['id'] == 4 ){
                    $total = ( $assets - $depreciation  );
                    echo '<tr><td></td><td></td></tr><tr class="pro"><td colspan="2"><b>'.$row['description'].'<b></td><td class="txt-right"><b>'.Yii::app()->numberFormatter->formatCurrency($total,"").'<b></td></tr><tr><td></td><td></td></tr>';
                }else
                    echo '<tr><td></td><td></td></tr><tr class="pro"><td colspan="2"><b>'.$row['description'].'<b></td><td class="txt-right"><b>'.Yii::app()->numberFormatter->formatCurrency($total,"").'<b></td></tr><tr><td></td><td></td></tr>';
                $total  =  0;
            }
            else if( $row['type'] == 'H' )
                echo '<tr class="l1"><td><b>'.$row['account_no'].'</b></td><td class="level-1 l1"><b>'.$name.'</b></td><td class="txt-right"></td></tr>';
            else if( $row['type'] == 'A' ){
                echo '<tr class="l2"><td><b>'.$row['account_no'].'</b></td><td class="level-2 l2"><b>'.$name.'</b></td><td class="txt-right">opening balance '.$opening_balance.'</td></tr>';
                $allSubledgers = FinGeneralLedger::model()->findAll(array( 'select' => 'subledger,SUM(debit) as debit,SUM(credit) as credit','condition' => 'isAllocated = "Y" AND account_id ='.$row['account_ids'] ,'group' =>'subledger' ));
                if(!empty($allSubledgers)){
                    foreach ( $allSubledgers as $eachsubledger ) {
                        echo '<tr class="l3"><td>'.$eachsubledger['sub']['account_no'].'</td><td class="level-3 l3"> '.$eachsubledger['sub']['description'].'</td><td class="txt-right">'.($eachsubledger['debit'] -$eachsubledger['credit']).'</td></tr>';
                        $total += ($eachsubledger['debit'] -$eachsubledger['credit']);
                        $assets_total += ($eachsubledger['debit'] -$eachsubledger['credit']);
                        if( $row['id'] == 2 )
                            $assets += ($eachsubledger['debit'] - $eachsubledger['credit']);
                        if( $row['id'] == 3 )   //Accumulated Depreciation
                            $depreciation += ($eachsubledger['debit'] -$eachsubledger['credit']);
                    }
                }
            }
            $previous = $row['type'];    ///Previous Type
        }
        
        /**
         * Liabilities
         */
        
        $query = "SELECT * from crm_balancesheet_sort_order where account_type='L'";
        $allLiabilities = Yii::app()->db->createCommand($query)->queryAll();

        $previous  = 'H'; 
        $total  =  0;
        $liability_total = 0;
        foreach ($allLiabilities as $row)
        {
            $amount =  "0.00";
            $name   =  $row['description'];
            $opening_balance = 0.00;
            if( $row['type'] == 'A' )
                $opening_balance = FinAccounts::model()->find(array('condition' => 'account_no ='.$row['account_no']))->opening_bal;
           
            $total += $opening_balance;
            $liability_total += $opening_balance;
            if( $row['type'] == 'T' )
                echo '<tr><td></td><td></td></tr><tr class="tax"><td colspan="2"><b>'.$row['description'].'<b></td><td class="txt-right"><b>'.Yii::app()->numberFormatter->formatCurrency($liability_total,"").'<b></td></tr><tr><td></td><td></td></tr>';
            if( $previous == 'A' && $row['type'] == 'E' ){
                echo '<tr><td></td><td></td></tr><tr class="pro"><td colspan="2"><b>'.$row['description'].'<b></td><td class="txt-right"><b>'.Yii::app()->numberFormatter->formatCurrency($total,"").'<b></td></tr><tr><td></td><td></td></tr>';
                $total  =  0;
            }
            else if( $row['type'] == 'H' )
                echo '<tr class="l1"><td><b>'.$row['account_no'].'</b></td><td class="level-1 l1"><b>'.$name.'</b></td><td class="txt-right"></td></tr>';
            else if( $row['type'] == 'A' ){
                echo '<tr class="l2"><td><b>'.$row['account_no'].'</b></td><td class="level-2 l2"><b>'.$name.'</b></td><td class="txt-right">opening balance '.$opening_balance.'</td></tr>';
                $allSubledgers = FinGeneralLedger::model()->findAll(array( 'select' => 'subledger,SUM(debit) as debit,SUM(credit) as credit','condition' => 'isAllocated = "Y" AND account_id ='.$row['account_ids'] ,'group' =>'subledger' ));
                if(!empty($allSubledgers)){
                    foreach ( $allSubledgers as $eachsubledger ) {
                        echo '<tr class="l3"><td>'.$eachsubledger['sub']['account_no'].'</td><td class="level-3 l3"> '.$eachsubledger['sub']['description'].'</td><td class="txt-right">'.($eachsubledger['credit']-$eachsubledger['debit']).'</td></tr>';
                        $total += ($eachsubledger['credit']-$eachsubledger['debit']);
                        $liability_total += ($eachsubledger['credit']-$eachsubledger['debit']);
                    }
                }
            }
            $previous = $row['type'];    ///Previous Type
        }
        ?>
</tbody>
    </table>

                    </div>

        <!--mpdf
    <htmlpagefooter name="myfooter" class="newfoot">

            <!-- <div style="text-align:center;width:100%;font-size:10px;"><?= Yii::app()->user->footer_autogenerated; ?>
        <?php
                  date_default_timezone_set("Asia/Dubai");
                 echo date('d/m/Y')." ".date("h:i:sa"); ?>
    </div> -->

    
        <div style="border-top: 1px dotted #000000; font-size: 12px; text-align: center; padding-top: 10px; background-color: #d7faeb;">
            <div style="text-align:center;width:100%">
                <b><?= Yii::app()->user->footer_line_1 ?></b><br>
                <?= Yii::app()->user->footer_line_2 ?>
            </div>
            <div style="text-align:center;width:100%">Page {PAGENO} of {nb} </div>
        </div>
    </htmlpagefooter>
        <sethtmlpagefooter name="myfooter" page="all" value="on" />
        mpdf-->