<?php
session_start();
// ============
include("../../asset/theme/css/myfunc.php");
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
$branchid = $_SESSION['LOGIN_BRANCH'];
extract($_POST);
$date=date('Y-m-d');
$date_t=date('d-m-Y');
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$v5=$obj->cash_in_hand_balance($branchid);
$v6=$obj->default_bank_balance($branchid);
?>


<?php

if($branchid!=''){
  $brans_obj = $obj->View_branch_details_By_Cond($branchid,'B');
  $inst_name = $brans_obj->insName;
  $ins_c_name = $brans_obj->insCodeName;
  if($ins_c_name!='-'){$ins_code_name = '('.$ins_c_name.')';}else{$ins_code_name = '';} 
  $extablishd_year = $brans_obj->_establish_year ;
  $ins_logo = $brans_obj->_logo_location;
  $ins_address = $brans_obj->_address;
  $inst_thanta = $brans_obj->thana_name ;
  $ins_district = $brans_obj->distrinc_name  ;
  $ins_division = $brans_obj->division_name  ;
  $ins_web = $brans_obj->_web_address !='' ? $brans_obj->_web_address : '' ;
  $ins_email = $brans_obj->_email_add !='' ? '|| '.$brans_obj->_email_add : '' ;
  
}
else{
  
  $institute_id = isset($_SESSION['LOGIN_INSTITUTE_ID']) ? $_SESSION['LOGIN_INSTITUTE_ID']: '';
  
  if($institute_id != ''){
    $ins_obj = $obj->View_Institute_details_By_ID($institute_id);
    $inst_name = $_SESSION['LOGIN_INSTITUTE_NAME'];;
    $ins_c_name = $ins_obj->_code_name;
    if($ins_c_name!='-'){$ins_code_name = '('.$ins_c_name.')';}else{$ins_code_name = '';}
    $extablishd_year = $ins_obj->_establish_year ;
    $ins_logo = $ins_obj->_logo_location;
    $ins_address = $ins_obj->_address;
    $inst_thanta = $ins_obj->thana_name ;
    $ins_district = $ins_obj->distrinc_name  ;
    $ins_division = $ins_obj->division_name  ;
    $ins_web = $ins_obj->_web_address !='' ? $ins_obj->_web_address : '' ;
    $ins_email = $ins_obj->_email_add !='' ? '|| '.$ins_obj->_email_add : '' ;
    }
  }

?>

<style type="text/css">
.table th, .table td{
    padding: 1px !important;
    text-align: right;
    padding-right: 3px !important;
  }
  .ins_info{
    text-align: center;
  }
  .prbtn{
    margin-left: 7%;
    margin-top: 8%;
  }
</style>


<div class="row-fluid" >
 <div class="span2">
    <?php 
     if($ins_logo !=''){
        echo '<img src="'.$ins_logo.'" style="height:120px;" title="logo" alt="" id="image" />';
       }else{
         
      // alternate iamge   
         }
    ?> 
  </div>                      
                    
  <div class="span8 ins_info">
    <h4><b> <?php echo $inst_name ; ?> </b><?php echo $ins_code_name; ?> </h4>
    <h5><?php echo $ins_address ; ?>,&nbsp<?php echo $inst_thanta ; ?>&nbsp<?php echo $ins_district ?>,&nbsp<?php echo $ins_division ; ?> </h5>
    <h5><?php echo $ins_web ?>  <?php echo  $ins_email; ?> </h5>                     
     <h4>Accounts Day Book(<?php echo $date_t; ?>) </h4>
  </div> 

   <a href="module/finance/pdf_day_book.php" target="blank"><button class="btn btn-mini btn-warning prbtn">
    Save As PDF
    <i class="icon-print  bigger-125 icon-on-right"></i>
    </button></a>
 
</div>

<table id="sample-table-22" class="table table-bordered">
  <tr>
    <td rowspan="2" colspan="3" style="text-align:center; font-weight: bold; vertical-align: middle;">Opening Blance</td>
    <td style=" font-weight: bold;">Cash</td>
    <td colspan="2"><?php echo $v5['cash_balance_default']; ?></td>
  </tr>
  <tr>
    <td style=" font-weight: bold;">Bank</td>
    
    <td colspan="2"><?php echo $v6['bank_balance_default']; ?></td>
  </tr>
  
  <tr>
    <td  style="text-align:center; font-weight: bold;">SL</td>
    <td  style="text-align:center; font-weight: bold;">Voucher No</td>
    <td  style="text-align:center; font-weight: bold;">Voucher Type</td>
    <td  style="text-align:center; font-weight: bold;">GL</td>
    <td  style="text-align:center; font-weight: bold;">Dr.</td>
    <td  style="text-align:center; font-weight: bold;">Cr.</td>
  </tr>
 
<?php
$i=0;
foreach($obj->View_All_By_Cond("acc_voucher","_br_id='$branchid' AND DATE(_entry_date)='$date'") as $v1){
$i++;
$total_gl=$obj->Total_Count_By_Cond("acc_voucher_gl","_voucher_id='".$v1['_id']."'");


foreach($obj->voucher_gl_info("_voucher_id='".$v1['_id']."' limit 1") as $v2){
?>

  <tr>
    <td rowspan="<?php echo $total_gl; ?>" style="vertical-align: middle;"><?php echo $i; ?></td>
    <td rowspan="<?php echo $total_gl; ?>" style="vertical-align: middle;"><?php echo $v1['_voutcher_no']; ?></td>
    <td rowspan="<?php echo $total_gl; ?>" style="vertical-align: middle;"><?php if($v1['_voutcher_type']=='C'){echo "Cash (";}else if($v1['_voutcher_type']=='B'){echo "Bank (";} if($v1['_trxn_type']=='C'){echo "Credit)";}else if($v1['_trxn_type']=='D'){echo "Debit)";}?></td>
    <td><?php echo $v2['gl_head_name'].' ('. $v2['gl_head_code'].')'; ?></td>
   	<td><?php if($v2['_trxn_type']=='D'){ echo $v2['_amount'];}else{echo '-';} ?></td>
    <td><?php if($v2['_trxn_type']=='C'){ echo $v2['_amount'];}else{echo '-';} ?></td>
  </tr>
<?php
}
foreach($obj->voucher_gl_info("_voucher_id='".$v1['_id']."' limit 1, $total_gl") as $v3){
?>
  <tr>
    <td><?php echo $v3['gl_head_name'].' ('. $v3['gl_head_code'].')'; ?></td>
   	<td><?php if($v3['_trxn_type']=='D'){ echo $v3['_amount'];}else{echo '-';} ?></td>
    <td><?php if($v3['_trxn_type']=='C'){ echo $v3['_amount'];}else{echo '-';} ?></td>
  </tr>
<?php
}
?>

  <tr>
    <td colspan="4" style="font-weight: bold;">Total</td>
    <td><?php if($v1['_trxn_type']=='D'){ echo $v1['_amount'];}else{echo '-';} ?></td>
    <td><?php if($v1['_trxn_type']=='C'){ echo $v1['_amount'];}else{echo '-';} ?></td>  
  </tr>
<?php
}

$v4=$obj->day_book_balance($branchid,$date);
?>

  <tr>
    <td colspan="4" style="font-weight: bold;">Net total</td>
    <td><?php echo $v4['debit_amount']; ?></td>
    <td><?php echo $v4['credit_amount']; ?></td>
  </tr>
  <tr>
    <td colspan="4" style="font-weight: bold;">Balance</td>
    <td colspan="2" ><?php echo ($v4['credit_amount']-$v4['debit_amount']); ?></td>   
  </tr>
</table>