<style type="text/css">

@media print {
  @page {
    margin-top: 6.4cm;
    margin-bottom: 3cm;
  }
  *{

    box-sizing: border-box;

  }
  .table-tree{
    width:100%;
    text-align:left;
    padding-left:10px !important;
    font-size: 11px;
    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: 50px !important;
  }
  .txt-right{
    text-align: right !important;
  }

  .tittle{

    width: 100%;

    margin:0px 0 10px;

    float: left;

    text-align: center;



  }

  .tittle h6{

    border-bottom:2px solid #000;

    width: 185px;

    max-width: 330px;

    font-size: 16px;

    margin: 10px auto;

  }

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

  .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;
  }
  .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;
  }
  .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;
  }
  .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;
  }
  .heading{
    height: 75px;
  }
  .logo img{
    height: 75px;
  }
}

</style>
<!--mpdf
  <htmlpageheader name="myHeader">
  <div class="heading">
    <div style="width:70%; float:left"><h3><?php echo Yii::app()->user->master_company_name;?><small></br><br><?php // echo MasCompany::getBillingAddress();?></small></h3></div>
    <div style="width:30%; float:left" class="logo"><?php echo CHtml::image(Yii::app()->baseUrl . '/images/logo/'.Yii::app()->user->pdf_company_logo); ?></div>
  </div>


<div id="finance-account-trialbal">
  <div class="tittle"><h6>GL Transaction Report</h6></div>
  <div class="col-3 f-r">
    <div class="date"><div class="span1"><b>Date From </b></div> <div class="span1"><b>:</b> <?php echo date_format(date_create($date_from), 'd-m-Y');?><br> </div></div>
    <div class="date"><div class="span1"><b>Date To </b></div> <div class="span1"><b>:</b> <?php echo date_format(date_create($date_to),'d-m-Y');?><br></div> </div>
  </div>

  <div class="col-9 f-l">
    <?php
    $opening_bal=0;
 if($account_no!=null) {
        $acc=FinAccounts::model()->findByPk($account_no);
      ?>

      <div class="date"><div class="span2"><b>Account </b></div>
      <div class="span3"><b>:  <?php
      echo  $acc->account_no.' '.$acc->description; ?>
      </b><br>
    </div></div>
    <?php } ?>

    <?php
    if($company_id!=null) {
      ?>
      <div class="date"><div class="span2"><b>Subledger A/C </b></div><div class="span3"><b>: <?php
      $sub=Customers::model()->findByPk($company_id);
      echo $sub->id.' '.$sub->customer_name; ?>
      </b><br>
    </div>
    <?php } ?>
  </div></div>



  <div class="space"></div>
  <div class="clear"></div>
  <div class="col-6 f-l"></div>
  <div class="col-6 f-r">
    <?php
        $type="";
    $year_opn=explode('-',$date_to);
    $start_date=$year_opn[0].'-01-01';
//    $from=date('Y-m-d', strtotime($date_from.' -1 day'));
    $from=$date_from;
    $from1=date('Y-m-d', strtotime($date_from));


    if($company_id==null && $account_no!=null)
    {
     $chart=FinAccounts::model()->findByPk($account_no); 
     $current_year=date("Y",strtotime($from1));
     
     $current_year=$current_year.'-01-01';
     if($chart->account_type=="E" || $chart->account_type=="I")
     {
     $SQL="SELECT company.account_type as acc_type, sum( debit ) AS debit, sum( credit ) AS credit
      FROM crm_fin_general_ledger AS ledger
      LEFT JOIN crm_fin_accounts AS company ON ledger.subledger = company.id
      WHERE account_id =$account_no AND isAllocated='Y' AND (ledger.date between '$current_year' AND '$from')";  
     }
     else {
      $SQL="SELECT company.account_type as acc_type, sum( debit ) AS debit, sum( credit ) AS credit
      FROM crm_fin_general_ledger AS ledger
      LEFT JOIN crm_fin_accounts AS company ON ledger.subledger = company.id
      WHERE account_id =$account_no AND isAllocated='Y' AND (ledger.date <'$from')";

     }
      $arr_sum_amount   = Yii::app()->db->createCommand($SQL)->queryAll();
      if(empty($arr_sum_amount))

      {

        $debit = "0.00";
        $credit = "0.00";
        
        if($chart->opening_bal_type==1)
        {
          $opening_bal_debit= $chart->opening_bal;
        }
        else if($chart->opening_bal_type==2){
          $opening_bal_credit=$chart->opening_bal;
        }
        if($chart->account_type=="A" || $chart->account_type=="E")
        {
          $opening_bal1=$opening_bal_debit-$opening_bal_credit;
        }
        else {
          $opening_bal1=$opening_bal_credit-$opening_bal_debit;
        } 
      }

      else {
        $coa_cr=0;
        $coa_db=0;
        foreach($arr_sum_amount as $sum_amount)

        {
          $account=FinAccounts::model()->findByPk($account_no);
          $type=$account->account_type;
          $debit=$sum_amount["debit"];
          $credit=$sum_amount["credit"];
          if($type=='A' || $type=='E')
          {
            $opening_bal2=$debit-$credit;
            $chart=FinAccounts::model()->findByPk($account_no);
            $opening_bal3=$chart->opening_bal;
            if($chart->opening_bal_type==1)
            {
              $coa_db=$chart->opening_bal;
            }
            else if($chart->opening_bal_type==2){
              $coa_cr=$chart->opening_bal;
            }
            $op1=$coa_db-$coa_cr;
          }
          else if($type=='I' || $type=='L')
          {
            $opening_bal2=$credit-$debit;
            $chart=FinAccounts::model()->findByPk($account_no);
            $opening_bal3=$chart->opening_bal;
            if($chart->opening_bal_type==1)
            {
              $coa_db= $chart->opening_bal;
            }
            else if($chart->opening_bal_type==2){
              $coa_cr=$chart->opening_bal;
            }
            $op1=$coa_cr-$coa_db;
          }
        }
      }
    }
    else if($company_id!=null && $account_no!=null)
    {
     $chart=FinAccounts::model()->findByPk($account_no);   
     $current_year=date("Y",strtotime($from));
     $current_year=$current_year.'-01-01';
     if($chart->account_type=="E" || $chart->account_type=="I")
     {
     $SQL="SELECT company.account_type as acc_type, sum( debit ) AS debit, sum( credit ) AS credit
     FROM crm_fin_general_ledger AS ledger
     LEFT JOIN crm_fin_accounts AS company ON ledger.subledger = company.id
     WHERE account_id =".$account_no." AND isAllocated='Y'  AND (ledger.date between '$current_year' AND '$from') AND subledger=".$company_id."
     GROUP BY subledger";  
     }
     else {
     $SQL="SELECT company.account_type as acc_type, sum( debit ) AS debit, sum( credit ) AS credit
     FROM crm_fin_general_ledger AS ledger
     LEFT JOIN crm_fin_accounts AS company ON ledger.subledger = company.id
     WHERE account_id =".$account_no." AND isAllocated='Y'  AND (ledger.date < '" . $from ."') AND subledger=".$company_id."
     GROUP BY subledger";
     }
     $arr_sum_amount  = Yii::app()->db->createCommand($SQL)->queryAll();

     if(empty($arr_sum_amount))

     {

      $debit = "0.00";
      $credit = "0.00";
      $subledger_opening=FinOpeningBalance::model()->find(array('condition'=>'account_id='.$company_id.' AND mainacc='.$chart->id));
      if($subledger_opening->type==1)
      {
        $opening_bal_debit= $subledger_opening->opening_balace;
      }
      else if($subledger_opening->type==2){
        $opening_bal_credit=$subledger_opening->opening_balace;
      }
      if($chart->account_type=="A" || $chart->account_type=="E")
      {
        $opening_bal1=$opening_bal_debit-$opening_bal_credit;
      }
      else {
        $opening_bal1=$opening_bal_credit-$opening_bal_debit;
      }
    }

    else {

      foreach($arr_sum_amount as $sum_amount)

      {
        $account=FinAccounts::model()->findByPk($account_no);
      $subledger_opening=FinOpeningBalance::model()->find(array('condition'=>'account_id='.$company_id.' AND mainacc='.$account->id));
      if($subledger_opening->type==1)
      {
        $opening_bal_debit= $subledger_opening->opening_balace;
      }
      else if($subledger_opening->type==2){
        $opening_bal_credit=$subledger_opening->opening_balace;
      }
        $type=$account->account_type;
        $debit=$sum_amount["debit"];
        $credit=$sum_amount["credit"];
        if($type=='A' || $type=='E')
        {
          $opening_bal1=$opening_bal_debit-$opening_bal_credit;
          $opening_bal2=$debit-$credit;
          $chart=FinAccounts::model()->findByPk($account_no);
          $opening_bal3=$chart->opening_bal;
//          if($chart->opening_bal_type==1)
//          {
//            $coa_db=$chart->opening_bal;
//          }
//          else if($chart->opening_bal_type==2){
//            $coa_cr=$chart->opening_bal;
//          }
//          $op1=$coa_db-$coa_cr;
        }
        else if($type=='I' || $type=='L')
        {
          $opening_bal1=$opening_bal_credit-$opening_bal_debit;
          $opening_bal2=$credit-$debit;
          $chart=FinAccounts::model()->findByPk($account_no);
          $opening_bal3=$chart->opening_bal;
//          if($chart->opening_bal_type==1)
//          {
//            $opening_bal_debit= $chart->opening_bal;
//          }
//          else if($chart->opening_bal_type==2){
//            $opening_bal_credit=$chart->opening_bal;
//          }
//          $op1=$coa_cr-$coa_db;
        }
      }
    }
  }
  else {
   $accounts = FinGeneralLedger::model()->findAll(array('condition' =>'date < "'.$date_from .'"','order'=>'date ASC'));
 }
//                   $year_opn=explode('-',$date_from);
//                     $condition ='account_id ='.$account_no.' AND year='.$year_opn[0].'';
//                    $account_opnbal = FinanceAuditClose::model()->find(array('condition' => $condition));
//
//                    $opening_bal+=$account_opnbal->opening_balance;
//nodate
 $opening_bal=$opening_bal1+$op1+$opening_bal2;
 ?>
 <div class="date txt-right"><span><b>Opening Balance </b></span> <span><?php 
  $account=FinAccounts::model()->findByPk($account_no);
  $type=$account->account_type;
 if($type=='A' || $type=='E')
  {
 if($opening_bal<0)
 {
 echo Yii::app()->numberFormatter->formatCurrency(abs($opening_bal),"").' Cr';     
 }
 else 
 {
 echo Yii::app()->numberFormatter->formatCurrency($opening_bal,"").' Dr'; 
 }    
 }
 else {
 if($opening_bal<0)
 {
 echo Yii::app()->numberFormatter->formatCurrency(abs($opening_bal),"").' Dr';     
 }
 else 
 {
 echo Yii::app()->numberFormatter->formatCurrency($opening_bal,"").' Cr'; 
 }
 }
 ?></span></div>
</div>
<div class="clear"></div>
</div> 
</htmlpageheader>
<sethtmlpageheader name="myHeader" value="on" />        mpdf-->
<table class="table-tree">
  <thead>
    <tr>

    <th class="cell-left">Date</div></th>
    <th class="cell-left">Voucher No</div></th>

    <th class="cell-left">Narration</div></th>
    <th class="cell-right">Debit</div></th>
    <th class="cell-right">Credit</div></th>
    <th class="cell-right">Balance</div></th>
  </tr>
</thead>
  <?php
$condtion=" AND 1=1";
if(!empty($item_type))
{
$condition.=' AND item_type="'.$item_type.'"';    
}
if(!empty($item_id))
{
$condition.=' AND item_id="'.$item_id.'"';    
}
  if($company_id!=null && $account_no==null)
  {
   $accounts = FinGeneralLedger::model()->findAll(array('condition' =>'isAllocated="Y" AND subledger='.$company_id.' AND (date >="'.$date_from .'" AND date <="'.$date_to.'") '.$condition,'order'=>'date,voucher_no ASC'));
 }
 else if($company_id==null && $account_no!=null)
 {
   $accounts = FinGeneralLedger::model()->findAll(array('condition' =>'isAllocated="Y" AND account_id='.$account_no.' AND (date >="'.$date_from .'" AND date <="'.$date_to.'") '.$condition,'order'=>'date,voucher_no ASC'));
 }
 else if($company_id!=null && $account_no!=null)
 {
   $accounts = FinGeneralLedger::model()->findAll(array('condition' =>'isAllocated="Y" AND account_id='.$account_no.' AND subledger="'.$company_id.'" AND (date >="'.$date_from .'" AND date <="'.$date_to.'") '.$condition,'order'=>'date,voucher_no ASC'));
                   //$accounts = FinGeneralLedger::model()->findAll(array('condition' =>'subledger='.$company_id.' AND account_id='.$account_no.' AND date BETWEEN "'.$date_from .'" AND "'.$date_to.'" ','order'=>'date ASC'));
 }
 else {
   $accounts = FinGeneralLedger::model()->findAll(array('condition' =>'isAllocated="Y" AND (date >="'.$date_from .'" AND date <="'.$date_to.'") '.$condition,'order'=>'date,voucher_no ASC'));
 }
 $totaldebit=0;
 $totalcredit=0;
 $bal=$opening_bal;
//             echo '<tr><td>';
//             print_r($accounts);echo $company_id,$account_no,$date_from,$date_to;
//             echo '</td></tr>';
 foreach ($accounts as $i=>$eachaccounts) {
  $acc_detail=FinAccounts::model()->findByPk($eachaccounts->account_id);
  $type=$acc_detail->account_type;
//                if($i%2==0){
//                    $class="ze";
//                }
//                else {
//                    $class="";
//                }

  ?>

  <tr class="<?php echo $class; ?>">
    <td class="cell-left"><?php echo date_format(date_create($eachaccounts->date), 'd-m-Y');?></div></td>
    <td class="cell-left"><?php echo $eachaccounts->voucher_no; ?></div></td>
<!--    <td class="cell-left"><?php
//    if($eachaccounts->debit==0)
//    {
//     $new_acc=FinGeneralLedger::model()->find(array('condition'=>'voucher_no="'.$eachaccounts->voucher_no.'" AND debit='.$eachaccounts->credit));
//   }
//   else {
//    $new_acc=FinGeneralLedger::model()->find(array('condition'=>'voucher_no="'.$eachaccounts->voucher_no.'" AND credit='.$eachaccounts->debit));
//  }
//  $acc=FinAccounts::model()->findByPk($new_acc->account_id);
//
//  echo  $acc->account_no;
  ?>
</td>-->
<td class="cell-left"><?php if($eachaccounts->item_description=='') { echo $eachaccounts->narration; } else { echo $eachaccounts->item_description; } ?></div></td>
<td class="cell-right"><?php


//                            if($type=='A' || $type=='E')
//                            {
//                                $debitvalue=$eachaccounts->debit;
//                                 $totaldebit+=$debitvalue;
//                            }else{
$debitvalue=$eachaccounts->debit;
$totaldebit+=$debitvalue;
//                            }

echo Yii::app()->numberFormatter->formatCurrency($debitvalue,"");


?></div></td>
<td  class="cell-right"><?php

//                               if($type=='I' || $type=='L')
//                            {
$creditvalue=$eachaccounts->credit;
$totalcredit+=$creditvalue;
//                            }else{
//                                $creditvalue=$eachaccounts->credit;
//                                $totalcredit+=$creditvalue;
//                            }
echo Yii::app()->numberFormatter->formatCurrency($creditvalue,""); ?></div></td>

<td  class="cell-right"><?php
if($i==0) {
  if($type=='A' || $type=='E')
  {
    $bal+=($debitvalue-$creditvalue);
  }
  else if($type=='I' || $type=='L')
  {
    $bal+=($creditvalue-$debitvalue);
  }
}
else {
  if($type=='A' || $type=='E')
  {
    $bal+=$debitvalue-$creditvalue;
  }
  else if($type=='I' || $type=='L')
  {
    $bal+=$creditvalue-$debitvalue;
  }
}
if($type=='A' || $type=='E')
  {
if($bal<0)
{
echo Yii::app()->numberFormatter->formatCurrency(abs($bal),"").'Cr';    
}
else {
echo Yii::app()->numberFormatter->formatCurrency($bal,"").'Dr';    
 }
  }
  else {
if($bal<0)
{
echo Yii::app()->numberFormatter->formatCurrency(abs($bal),"").'Dr';    
}
else {
echo Yii::app()->numberFormatter->formatCurrency($bal,"").'Cr';    
 }      
  }
?></div></td>
</tr>
<?php	}

?>
<tr class="bg-w">
  <td class="cell-right" colspan="6">&nbsp;</td>
</tr>
<tr class="bg-w">
  <td class="cell-right" colspan="2"></td>
  <td class="cell-right"><b>Total</b></td>
  <td class="cell-right br-top"><b><?php echo Yii::app()->numberFormatter->formatCurrency($totaldebit,''); ?></b></td>
  <td class="cell-right br-top"><b><?php echo Yii::app()->numberFormatter->formatCurrency($totalcredit,''); ?></b></td>
  <td class="cell-right br-top"><b></b></td>
</tr>
<tr class="ze">
  <td class="cell-right" colspan="2"></td>
  <td class="cell-right"><b>Closing Balance</b></td>
<?php
  if($totaldebit==0 && $totalcredit==0)
  {
   if($type=='A' || $type=='E')
  {
 if($opening_bal<0)
 {
 ?>
  <td class="cell-right" colspan="2"><b><?php echo Yii::app()->numberFormatter->formatCurrency($bal,''); ?></b></td>
  <td></td>
  <?php
 }
 else 
 {
 ?>
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td colspan="2"></td>
  <?php
 }    
 }
 else {
 if($opening_bal<0)
 {
?>
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td colspan="2"></td>
  <?php    
 }
  else 
 {
 ?>
  <td class="cell-right" colspan="2"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  <?php
 } 
 }   
  }
   else if($bal<0)
      {
       if($type=='A' || $type=='E')
  {
      ?>
   <td></td>
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  <?php
  }
  else {
   ?>
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  <td></td>
  <?php   
  }
  }
  else if($bal>0){
      if($type=='A' || $type=='E')
  {
      ?>
   <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  <td></td>
  <?php
  }
  else {
   ?>
  <td></td>
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  
  <?php   
  }
  }
  else {
   ?>
  
  <td class="cell-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency(abs($bal),''); ?></b></td>
  <td></td>
  <td></td>
  
  <?php   
  }
?>
 
</tr>
</table>
<div class="space"></div><div class="space"></div><div class="space"></div>

</div>
        <!--mpdf
        <htmlpagefooter name="myfooter">
            <div style="border-top: 1px dotted #000000; font-size: 12px; text-align: center; padding-top: 10px; ">
                <div style="text-align:left;width:50%;float:left">Printed on <?php
                  date_default_timezone_set("Asia/Dubai");
                 echo date('d/m/Y')." ".date("h:i:sa"); ?> </div>
                <div style="text-align:right;width:50%;float:left">Page {PAGENO} of {nb} </div>
            </div>
        </htmlpagefooter>
        <sethtmlpagefooter name="myfooter" page="all" value="on" />
        mpdf-->
