<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<?php 
   $gbid   = $_GET['bid'];
   $gbrid  = $_GET['brid'];
   $edate  = $_GET['edate'];
   
$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$inac= mysqli_fetch_assoc($inacc);
   
   $co_name        = $inac['co_name'];
   $owner_name     = $inac['owner_name'];
   $co_address     = $inac['co_address'];
   $co_phone       = $inac['co_phone'];
   $co_email       = $inac['co_email'];
   $co_logo        = $inac['co_logo']; 
   ?>
<section class="content">
   <div class="row">
      <div class="col-sm-12">
         <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
            <div class="card-header" style="padding: 3px 8px;">
               <h3 class="card-title"><?php echo language(184);?></h3>
               <div class="card-tools">
                  <span id="sdf"></span>
                  <button type="button" onclick="pdfMe('#blsheet');" class="btn btn-tool" title="Download Pdf"><i class="fas fa-download"></i></button>
                  <button type="button" class="btn btn-tool" onclick="printMe('#blsheet')" title="Print Now"><i class="fas fa-print"></i></button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove">
                  <i class="fas fa-times"></i>
                  </button>
               </div>
            </div>
            <div class="card-body p-1 table-responsive cscroll" id="blsheet">
                                       <input type="hidden" id="vounameset" value="Balance Sheet Report <?php echo date('d/M/y', strtotime($edate));?> ">

               <div class="row d-flex">
                  <div class="col-sm-12">
                     <table class="ptable" cellpadding="0" cellspacing="0">
                        <thead>
                           <tr>
                              <th rowspan="3" class="thborder" style="width:10%;">
                                <?php if($co_logo != ""): ?>
                                      <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo;?>" style="width:80px;height:77px;">
                                <?php else : ?>
                                      <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:77px;"></div>
                                <?php endif; ?>
                                 
                              </th>
                              <th rowspan="3" class="thborder" style="width:70%;">
                                 <dt style="font-size:18px;font-weight: 600;"><?php echo $co_name;?></dt>
                                 <dt><?php echo $owner_name;?></dt>
                                 <dt><?php echo $co_address;?></dt>
                                 <dt><?php echo $co_phone;?>#<?php echo $co_email;?></dt>
                              </th>
                              <th class="thborder text-center" style="width:20%;"><?php echo language(307);?></th>
                           </tr>
                           <tr>
                              <th class="thborder text-center"><?php echo language(184);?></th>
                           </tr>
                           <tr>
                              <th class="thborder text-center"><?php echo language(131);?> - <?php echo date('d/m/y', strtotime($edate));?>
                              </th>
                           </tr>
                        </thead>
                     </table>
                  </div>
                  <div class="col-sm-6 pr-0">
                     <div class="card shadow-none" style="border:1px solid #969c9d;border-radius:0;">
                        <div style="padding: 4px 8px;border-bottom:1px solid #969c9d;background:#00bfff29;">
                           <span><b><?php echo language(226);?></b></span>
                           <span style="float:right;"><b><?php echo language(163);?></b></span>
                        </div>
                        <div style="height:400px;" class="table-responsive cscroll">
                           <table cellpadding="0" cellspacing="0" style="width:100%;">
                              <?php
                                 $acass= mysqli_query($conn,"SELECT ac_s_catid,ac_s_cat_name FROM ac_sub_category WHERE ac_catid=1 AND (SELECT ac_g_catid FROM ac_sub_g_category g LEFT JOIN transaction t ON (t.dr_gcatid=g.ac_g_catid || t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND g.ac_s_catid=ac_sub_category.ac_s_catid LIMIT 1) IS NOT NULL") or die(mysqli_error($conn));
                                 $asstotal = 0;
                                 while($acas= mysqli_fetch_assoc($acass)){
                                  $ac_s_catid    = $acas['ac_s_catid'];
                                  $ac_s_cat_name = $acas['ac_s_cat_name'];
                                 ?> 
                              <thead>
                                 <tr>
                                    <th style="width:85%;background:#8080803b;padding:3px 10px;"><?php echo $ac_s_cat_name;?></th>
                                    <th style="width:15%;background:#8080803b;padding:3px 10px;"></th>
                                 </tr>
                              </thead>
                              <tbody>
                                 <?php 
                                    $trbal = mysqli_query($conn,"SELECT g.ac_g_catid,g.ac_g_cat_name,SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0)) balance FROM transaction t LEFT JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) <='$edate' AND g.ac_s_catid='$ac_s_catid' GROUP BY g.ac_g_cat_name HAVING balance <> 0  ORDER BY g.ac_g_catid ASC") or die(mysqli_error($conn));
                                     while($tbl= mysqli_fetch_assoc($trbal)){
                                      $gcatid = $tbl['ac_g_catid'];
                                      $asstotal += $tbl['balance'];
                                    ?>                         
                                 <tr class="btn-outline-dark" onclick="dtls(this);" data-toggle="collapse" href="#id<?php echo $gcatid;?>" gcatid="<?php echo $gcatid;?>">
                                    <td style="padding:3px 10px;"><?php echo $tbl['ac_g_cat_name']?> <i class="fas fa-caret-left float-right"></i></td>
                                    <td class="text-right" style="padding:3px 8px 3px 0px;">
                                       <dt><?php echo money($tbl['balance'],true);?></dt>
                                    </td>
                                 </tr>
                                 <tr>
                              <tbody id="id<?php echo $gcatid;?>" class="collapse"></tbody>
                              </tr>
                              <?php } ?>
                              </tbody>
                              <?php } ?>                        
                           </table>
                        </div>
                        <div style="padding: 3px 8px;border-top:1px solid #969c9d;background:#00bfff29;">
                           <span><b><?php echo language(140);?>-<?php echo language(226);?></b></span>
                           <span style="float:right;"><b><?php echo money($asstotal,true);?></b></span>
                        </div>
                     </div>
                  </div>
                  <div class="col-sm-6 pl-0">
                     <div class="card shadow-none" style="border:1px solid #969c9d;border-left:0;border-radius:0;">
                        <div style="padding: 4px 8px;border-bottom:1px solid #969c9d;background:#00bfff29;">
                           <span><b><?php echo language(227);?></b></span>
                           <span style="float:right;"><b><?php echo language(163);?></b></span>
                        </div>
                        <div style="height:400px;" class="table-responsive cscroll">
                           <table cellpadding="0" cellspacing="0" style="width:100%;">
                              <?php
                                 $aclb= mysqli_query($conn,"SELECT ac_s_catid,ac_s_cat_name FROM ac_sub_category WHERE (ac_catid=2||ac_catid=3) AND (SELECT ac_g_catid FROM ac_sub_g_category g LEFT JOIN transaction t ON (t.dr_gcatid=g.ac_g_catid || t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$bid' AND t.brid='$brid' AND t.active=1 AND g.ac_s_catid=ac_sub_category.ac_s_catid LIMIT 1) IS NOT NULL") or die(mysqli_error($conn));
                                 $lbtotal = 0;
                                 while($acatlb= mysqli_fetch_assoc($aclb)){
                                  $ac_s_catid1    = $acatlb['ac_s_catid'];
                                  $ac_s_cat_name1 = $acatlb['ac_s_cat_name'];
                                 ?> 
                              <thead>
                                 <tr>
                                    <th style="width:85%;background:#8080803b;padding:3px 10px;"><?php echo $ac_s_cat_name1;?></th>
                                    <th style="width:15%;background:#8080803b;padding:3px 10px;"></th>
                                 </tr>
                              </thead>
                              <tbody>
                                 <?php 
                                    $trbal1 = mysqli_query($conn,"SELECT g.ac_g_catid,g.ac_g_cat_name,SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0)) balance FROM transaction t LEFT JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) <='$edate' AND g.ac_s_catid='$ac_s_catid1' GROUP BY g.ac_g_cat_name HAVING balance <> 0  ORDER BY g.ac_g_catid ASC") or die(mysqli_error($conn));
                                     while($tbl1= mysqli_fetch_assoc($trbal1)){
                                      $gcatid1 = $tbl1['ac_g_catid'];
                                      $lbtotal += $tbl1['balance'];
                                    ?>                         
                                 <tr class="btn-outline-dark" onclick="dtls(this);" data-toggle="collapse" data-target="#id<?php echo $gcatid1;?>" gcatid="<?php echo $gcatid1;?>">
                                    <td style="padding:3px 10px;"><?php echo $tbl1['ac_g_cat_name']?> <i class="fas fa-caret-left float-right"></i></td>
                                    <td class="text-right" style="padding:3px 8px 3px 0px;">
                                       <dt><?php echo money($tbl1['balance'],true);?></dt>
                                    </td>
                                 </tr>
                                 <tr>
                              <tbody id="id<?php echo $gcatid1;?>" class="collapse"></tbody>
                              </tr>
                              <?php } ?>
                              </tbody>                             
                              <?php }

                                    $tin = mysqli_fetch_assoc(mysqli_query($conn,"SELECT s.ac_catid,SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0)) balance FROM transaction t INNER JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) INNER JOIN ac_sub_category s ON(s.ac_s_catid=g.ac_s_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) <='$edate' AND s.ac_catid=4 GROUP BY s.ac_catid HAVING balance <> 0"));
                                    $tincome = ($tin)?$tin['balance']:0;

                                    $tex = mysqli_fetch_assoc(mysqli_query($conn,"SELECT s.ac_catid,SUM(COALESCE(CASE WHEN t.dr_gcatid = g.ac_g_catid THEN amount END,0))-SUM(COALESCE(CASE WHEN t.cr_gcatid = g.ac_g_catid THEN amount END,0)) balance FROM transaction t INNER JOIN ac_sub_g_category g ON (t.dr_gcatid=g.ac_g_catid ||t.cr_gcatid=g.ac_g_catid) INNER JOIN ac_sub_category s ON(s.ac_s_catid=g.ac_s_catid) WHERE t.coid='$coid' AND t.bid='$gbid' AND t.brid='$gbrid' AND t.active=1 AND date(date) <='$edate' AND s.ac_catid=5 GROUP BY s.ac_catid HAVING balance <> 0")); 
                                    $texpense = ($tex)?$tex['balance']:0; 
                                    $netincome = ($tincome-$texpense);
                               ?>    
                              <tbody>                        
                                 <tr>
                                    <td style="padding:3px 10px;">Net Profit</td>
                                    <td class="text-right" style="padding:3px 8px 3px 0px;">
                                       <dt><?php echo money($netincome,true);?></dt>
                                    </td>
                                 </tr>
                              </tbody>

                           </table>
                        </div>
                        <div style="padding: 3px 8px;border-top:1px solid #969c9d;background:#00bfff29;">
                           <span><b><?php echo language(140);?>-<?php echo language(227);?></b></span>
                           <span style="float:right;"><b><?php echo money(($lbtotal+$netincome),true);?></b></span>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</section>
<script>
   function dtls(sid) {
    var gcatid = $(sid).attr('gcatid');
       $(".preloader").css('display','flex');
       $('#id'+gcatid).load("../display_report/sub_view.php",
       ({
          gcatid: gcatid,
          gbid:'<?php echo $gbid;?>',
          gbrid:'<?php echo $gbrid;?>',
          edate:'<?php echo $edate;?>'
      }),
      function() {
       $('.preloader').hide(); 
       $(sid).removeAttr("onclick");
       $('#id'+gcatid).collapse('toggle')
     });
   }
</script>