<html>
    <head>
        <title><?php echo $b_header['title']; ?></title>
       
  <style>
            @media print
            {
                * {-webkit-print-color-adjust:exact;}
                @page {margin:0in 0.10in 0in 0in; }
                .super_container
                {
                    page-break-after: always;
                    margin-top:0.25in;
                    position:relative;
                    width:100vw;
                    height:710px;
                }
            }
            body {margin: 0.25in 0.25in 0.63in 0.25in; box-sizing:border-box;width:100vw; height:100vh;padding-bottom:0.63in;}
            *{margin:0;padding:0;font-family:calibri;}
            .clear:after{content:"";clear:both;display:block;}
            .left{float:left;}
            .right{float:right;}
            .wd-5{width:5%;}
            .wd-10{width:10%;}
            .wd-20{width:20%;}
            .wd-30{width:30%;}
            .wd-40{width:40%;}
            .wd-50{width:50%;}
            .wd-60{width:60%;}
            .wd-70{width:70%;}
            .wd-80{width:80%;}
            .wd-90{width:90%;}
            .wd-100{width:100%;}
            .wd-15{width:15%;}
            .wd-25{width:25%;}
            .wd-35{width:35%;}
            .wd-45{width:45%;}
            .wd-55{width:55%;}
            .wd-65{width:65%;}
            .wd-75{width:75%;}
            .wd-85{width:85%;}
            .wd-95{width:95%;}
            .wd-100{width:100%;}
            .title{font-size:35px;}
            .super_container
            {
                margin-bottom:200px;
                position:relative;
                width:100vw;
                height:710px;
            }
            .wrapper{
                border:4px transparent #18a05e;
                
                outline-offset: 5px;
                box-sizing: border-box;
                padding:5px;
            }
            .super_wrapper
            {
                position:absolute;
                top:0;
                left:0;
                width:100%;
                z-index:120;
                margin:0;
                padding:0;
            }
            /*.watermark*/
            /*{*/
            /*    height:200px;*/
            /*    width:300px;*/
            /*    margin:auto;*/
            /*    padding-top:50px;*/
            /*    padding-left:100px;*/
            /*    opacity:0.1;*/
            /*    background:url("<?php echo base_url().$b_header['logo1']; ?>");*/
            /*    background-size:100% 100%;*/
            /*}*/
            /*.watermark_wrapper*/
            /*{*/
            /*    height:470px;*/
            /*    width:100%;*/
            /*    margin-top:50px;*/
            /*    display:inline-flex;*/
            /*    display:-webkit-inline-flex;*/
            /* }*/
            .wrapper.left2{
                width: calc(90vw);
                margin-right: 20px;
                margin-left: 20px;
            }
            h1,h2,h3,h4,p{
                color:#191970;
                font-family:calibri;
                text-align:center;
            }
            .boxed{color:#000000;width:80%;margin:0 auto;}
            .header{
                width:100%;
                border-bottom:4px solid #191970;            
            }
            .body{
                padding:10px;
            }
            .row{
                width:100%;
                margin: 0px auto;  
            }
            .text-center{text-align:center;}
            .bold{font-weight:bold;}
            .b-u-l{
                border-bottom: 1px solid #000;
                height:inherit;
            }
            .x-small{
                font-size: 12px;
            }
            .x-x-small{
                font-size: 9px;
            }
            .page_break{
                width: 100vw;
                height: 100vh;
                padding: 10px 0px;
                box-sizing: border-box;
            }
            .tbl1
            {
                border: 1px solid #000;
                text-align:center;
                width:100%;
                min-height:20px;
            }
            th{
                font-size: 12px;
                border: 1px solid #000;
                  border-right: 0px;
                border-bottom: 0px; 
                background-color:#ccccb3; 
                padding: 3px !important;
            }
            td{
                font-size:12px;
                border: 1px solid #000;
                text-align: left;
                  border-right: 0px;
                border-bottom: 0px; 
                padding: 2px !important;
            }
            .normal{
                font-weight: normal !important;
            }
            .text-right{
                text-align: right !important;
            }
            .text-left{
                text-align: left !important;
            }
            .margin-a{margin: 30px auto 0px auto;}
        </style>
    </head>
    <body>

        <div class="super_container">
        <div class="container-fluid">
          <div class="row header">
              <div class="col-md-12 col-sm-12 col-xs-12">
                  <div class="row">
                       <div class="col-md-1 col-sm-2 col-xs-2 text-left" ><img class="logo" src="<?php echo base_url().$b_header['logo1']; ?>" width="100px" height="100px"></div>
                      <div class="col-md-1 col-sm-2 col-xs-2 relative">
                         
                      <div class="col-md-11 col-sm-10 col-xs-10 text-center" style="margin-top:-50px;">
                            <h2><?php echo $b_header['title']; ?></h2>
                            <h3><?php echo $b_header['tagline']; ?></h3>
                      </div>

                      <div class="col-md-12 col-sm-12 col-xs-12">
                             <div class="col-md-4 col-sm-4 col-xs-4 text-left">
                                <h3> <?php echo ""; ?></h3>
                             </div>
                             <div class="col-md-4 col-sm-4 col-xs-4 text-center">
                                <h2> <u> <?php echo "Trial Balance"; ?></u></h2>
                             </div>
                               <div class="col-md-4 col-sm-4 col-xs-4 text-center">
                                <h5>
                                  <u> <?php echo "<b>Till Date: </b> ".$newDate = date("d-m-Y", strtotime($to)); ?></u></h5>
                      </div>
                             
                      </div>
                  </div>  
              </div>
          </div>
<!--<hr/>-->
<!--<hr/>-->

 
    <div>
    <table class="tbl1">
                        <thead>
                        <tr>
                         <th>Code</th>
                        <th>Head of Account</th>
                        <th>Dr</th>
                        <th >Cr</th>
                      </tr>
                      </thead>
                      <tbody>
<?php
$smdr=0;
$smcr=0;

$datee = str_replace('/', '-', $to);
$dateto= date('Y-m-d', strtotime($datee));

 //var_dump('<pre>',$heads);
// die();
if(!empty($heads))
{
     $branch=$this->user_model->getbranch();
    foreach($heads as $key=>$values)
    {
   $totdr=0;
   $totcr=0;
         $tot_dr_amt=$this->db->select('sum(amount) as amount')
                              ->from('transction')
                              ->where('head',$values['id'])
                               ->where('date <=',$dateto)
                              ->where('branch',$branch)
                              ->where('has_cr',1)
                              ->get()->row_array()['amount']; 
                              
        $tot_cr_amt=$this->db->select('sum(amount) as amount')
                              ->from('transction')
                              ->where('head',$values['id'])
                               ->where('date <=',$dateto)
                              ->where('branch',$branch)
                              ->where('has_cr',0)
                              ->get()->row_array()['amount']; 
          if(!empty($tot_dr_amt))
             {
                 $totdr=$tot_dr_amt;
             }else
             {
                 $totdr=0;
             }
                    
           if(!empty($tot_cr_amt))
              {
                   $totcr=$tot_cr_amt;
              }else
             {
                 $totcr=0;
             }
        $op_blnc=$this->db->select('*')
                                ->from('opening_balance')
                                ->where('head',$values['id'])
                                ->where('branch',$branch)
                                ->get()->row();
            if(!empty($op_blnc))
            {
                   if($op_blnc->has_cr == 0)
                    {
                        $totcr=$totcr+$op_blnc->amount;
                    }else
                    {   $totdr=$totdr+$op_blnc->amount;
                    } 
            }
                            
            $diff=$totdr-$totcr;
            if(!empty($diff))
                 {
                  
                        
                      if($diff >=0)  
                            {
                                $smdr=$smdr+$diff;
                              echo " <tr>
                              <td>";
                              $cod=$this->db->select('code')
                                                 ->from('account_heads')
                                                 ->where('id',$values['id'])
                                                 ->get()->row_array()['code'];
                                                 echo $cod;
                              echo "</td>";
                             echo"<td>";
                       if($values['parent'] !=0) 
                       {        
                         $fetch_parents_lev0=0;
                         $fetch_parents_lev1=0;
                         $fetch_parents_lev2=0;
                         $fetch_parents_lev3=0;
                         $fetch_parents_lev4=0;
                         $fetch_parents_lev5=0;
                         $fetch_parents_lev6=0;
                         $fetch_parents_lev7=0;
                       
                          $fetch_parents_lev0=$this->db->select('*')
                                                 ->from('account_heads')
                                                 ->where('id',$values['id'])
                                                 ->get()->row();
                              $names[]=$fetch_parents_lev0->name;
                              $fetch_parents_lev0=$fetch_parents_lev0->parent;
                          if($fetch_parents_lev0!= 0)
                              {                       
                        $fetch_parents_lev1=$this->db->select('*')
                                                 ->from('account_heads')
                                                 ->where('id',$fetch_parents_lev0)
                                                 ->get()->row();
                              $names[]=$fetch_parents_lev1->name;
                              $fetch_parents_lev1=$fetch_parents_lev1->parent;
                              }
                         if($fetch_parents_lev1!= 0)
                         
                              {
                           $fetch_parents_lev2=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev1)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev2->name;
                                 $fetch_parents_lev2=$fetch_parents_lev2->parent;
                              }
                              if($fetch_parents_lev2!= 0)
                              {
                           $fetch_parents_lev3=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev2)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev3->name;     
                                 $fetch_parents_lev3=$fetch_parents_lev3->parent;     
                              }
                           if($fetch_parents_lev3 != 0)
                              {
                           $fetch_parents_lev4=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev3)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev4->name;            
                                 $fetch_parents_lev4=$fetch_parents_lev4->parent;     
                              }
                              
                              if($fetch_parents_lev4 != 0)
                              {
                           $fetch_parents_lev5=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev4)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev5->name;           
                                 $fetch_parents_lev5=$fetch_parents_lev5->parent;     
                              }
                              
                              if($fetch_parents_lev5 != 0)
                              {
                           $fetch_parents_lev6=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev5)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev6->name;            
                                 $fetch_parents_lev6=$fetch_parents_lev6->parent;     
                              }
                              
                               if($fetch_parents_lev6 != 0)
                              {
                           $fetch_parents_lev7=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev6)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev7->name;                 
                              }
                            
                             $names= array_reverse($names);
                              if(!empty($names)){
                               
                                 $i=1;
                              $len=count($names);
                                 foreach($names as $key=>$valtm)
                                 {
                                     
                                     echo  " ".$valtm;
                                     if($i != $len)
                                     {
                                      echo  "-";
                                     }
                                     $i++;
                                 }
                                 unset($names); 
                             }
                             
                              }else
                              {  
                                echo $values['name'];
                               } 
                               
                                echo "</td> 
                            <td style= 'text-align: right;'>";
                              echo  number_format($diff);

                            echo "</td>
                                <td></td>
                                </tr>";
                             }else
                            {
                                $smcr=$smcr+$diff;
                              echo " <tr>
                              <td>";
                              $cod=$this->db->select('code')
                                                 ->from('account_heads')
                                                 ->where('id',$values['id'])
                                                 ->get()->row_array()['code'];
                                   echo $cod;
                              echo "</td>";
                             echo"<td>";
                               
                       if($values['parent'] !=0) 
                       {        
                         $fetch_parents_lev0=0;
                         $fetch_parents_lev1=0;
                         $fetch_parents_lev2=0;
                         $fetch_parents_lev3=0;
                         $fetch_parents_lev4=0;
                         $fetch_parents_lev5=0;
                         $fetch_parents_lev6=0;
                         $fetch_parents_lev7=0;
                       
                          $fetch_parents_lev0=$this->db->select('*')
                                                 ->from('account_heads')
                                                 ->where('id',$values['id'])
                                                 ->get()->row();
                              $names[]=$fetch_parents_lev0->name;
                              $fetch_parents_lev0=$fetch_parents_lev0->parent;
                          if($fetch_parents_lev0!= 0)
                              {                       
                        $fetch_parents_lev1=$this->db->select('*')
                                                 ->from('account_heads')
                                                 ->where('id',$fetch_parents_lev0)
                                                 ->get()->row();
                              $names[]=$fetch_parents_lev1->name;
                              $fetch_parents_lev1=$fetch_parents_lev1->parent;
                              }
                         if($fetch_parents_lev1!= 0)
                         
                              {
                           $fetch_parents_lev2=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev1)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev2->name;
                                 $fetch_parents_lev2=$fetch_parents_lev2->parent;
                              }
                              if($fetch_parents_lev2!= 0)
                              {
                           $fetch_parents_lev3=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev2)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev3->name;       
                                 $fetch_parents_lev3=$fetch_parents_lev3->parent;     
                              }
                           if($fetch_parents_lev3 != 0)
                              {
                           $fetch_parents_lev4=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev3)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev4->name;           
                                 $fetch_parents_lev4=$fetch_parents_lev4->parent;     
                              }
                              
                              if($fetch_parents_lev4 != 0)
                              {
                           $fetch_parents_lev5=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev4)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev5->name;           
                                 $fetch_parents_lev5=$fetch_parents_lev5->parent;     
                              }
                              
                              if($fetch_parents_lev5 != 0)
                              {
                           $fetch_parents_lev6=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev5)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev6->name;            
                                 $fetch_parents_lev6=$fetch_parents_lev6->parent;     
                              }
                              
                               if($fetch_parents_lev6 != 0)
                              {
                           $fetch_parents_lev7=$this->db->select('*')
                                                 ->from('account_heads')
                                              ->where('id',$fetch_parents_lev6)
                                                 ->get()->row();
                                 $names[]=$fetch_parents_lev7->name;                 
                              }
                            
                             $names= array_reverse($names);
                              if(!empty($names)){
                               
                                 $i=1;
                               $len=count($names);
                                 foreach($names as $key=>$valtm)
                                 {
                                     
                                     echo  " ".$valtm;
                                  if($i != $len)
                                     {
                                      echo  "-";
                                     }
                                    
                                     $i++;
                                 }
                                 unset($names); 
                             }
                             
                              }else
                              {  
                                echo $values['name'];
                               } 
                               
                        echo "</td> 
                                <td></td>
                <td style ='text-align: right;'>";
                echo  number_format(abs($diff));
                echo "</td></tr>";
                        }
                }   //end if
                
           
    }
}
  echo "<tr>";
  echo "<td>";
 echo "-";
  echo "</td>";
      echo "<td>";
      echo "<h4>"."Total"."</h4>";
      echo "</td>";
         echo "<td style = 'text-align: right;'>";
         echo number_format($smdr);
         echo "</td>";
         echo "<td style = 'text-align: right;'>";
          echo number_format(abs($smcr));
          echo "</td>";
     echo "</tr>";
     ?>
     <tbody>
     </table>   
          

 <script>window.print();</script>
    </body>
</html>
