<?php require_once('auth.php'); ?>
<?php
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];

   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);

    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND transaction.bid=$bbid";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
       $branch_name = 'All Branch';       
    }elseif ($bbrid >0){
        $bbridcond= " AND transaction.brid=$bbrid";
        $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
    }

    $report = mysqli_query($conn,"SELECT 
      SUM(COALESCE(CASE WHEN ttype = 'EPUR' THEN amount END,0)) t_purchase,
      SUM(COALESCE(CASE WHEN ttype= 'P' THEN amount END,0)) t_payment,
      SUM(COALESCE(CASE WHEN (ttype = 'ECSL' || ttype = 'ERSL' || ttype = 'INSL') THEN amount END,0)) t_sales,
      SUM(COALESCE(CASE WHEN (ttype = 'R' || ttype = 'INSR' || ttype = 'W_R' || ttype = 'R_R' || ttype = 'R_I') THEN amount END,0)) t_received
        FROM transaction WHERE coid='$coid' $bbidcond $bbridcond AND active=1 AND date(date) BETWEEN '$fdate' AND '$edate' GROUP BY coid") or die(mysqli_error($conn));
    $r_row = mysqli_fetch_assoc($report);
    $t_purchase = !empty($r_row)?$r_row['t_purchase']:0;
    $t_payment  = !empty($r_row)?$r_row['t_payment']:0;
    $t_sales    = !empty($r_row)?$r_row['t_sales']:0;
    $t_received = !empty($r_row)?$r_row['t_received']:0;
?>
<div class="row">
  <section class="col-md-12 mb-1">
    <table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
      <thead>
        <tr>
          <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
            <img class="profile-user-img img-fluid img-circle" src="../../dist/img/a.png" style="width:80px;height:77px;">
          </th>
          <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
            <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
            <dt><?php echo $cor['owner_name'];?></dt>
            <dt><?php echo $cor['co_address'];?></dt>
            <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
          </th>
          <th class="thborder text-center" style="width:20%;"><?php echo $branch_name;?></th>
        </tr>
        <tr>
          <th class="thborder text-center"><?php echo language(185);?></th>
        </tr>
        <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?>
          </th>
        </tr>
      </thead>
    </table>
  </section>
  <section class="col-md-6 connectedSortable">
    <div class="card direct-chat direct-chat-primary">
      <div class="card-header">
        <h3 class="card-title">Income & Expense</h3>
        <div class="card-tools">
          <button type="button" class="btn btn-tool" data-card-widget="collapse">
          <i class="fas fa-minus"></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 table-responsive cscroll" style="height: 250px;">
        <div class="col-md-12 mt-1">
          <table class="ttable table-hover" cellpadding="0" cellspacing="0">
            <thead>
              <tr class="sticky-top">
                <th style="text-align:center;width:5%;">S/L</th>
                <th style="width:80%;">Particular</th>
                <th style="text-align:center;width:15%;">Amount</th>
              </tr>
            </thead>
            <tbody>
            <?php if($t_purchase>0) { ?>
              <tr data-widget="expandable-table" aria-expanded="false" onclick="pagecall('../auth/d_sub_view.php?gtype=EPUR'+'&gbid=<?php echo $bbid;?>'+'&gbrid=<?php echo $bbrid;?>'+'&fdate=<?php echo $fdate;?>'+'&edate=<?php echo $edate;?>','#epur_s_view',0);$(this).removeAttr('onclick');">
            <?php }else{ ?>
              <tr>
            <?php } ?>
                <td style="text-align:center;">1</td>
                <td>Total Purchase</td>
                <td style="text-align:right;"><?php echo number_format($t_purchase,$decimel);?></td>
              </tr>
              <tr class="expandable-body d-none">
                <td colspan="3">
                  <div id="epur_s_view"></div>
                </td>
              </tr>
            <?php if($t_payment>0) { ?>
              <tr data-widget="expandable-table" aria-expanded="false" onclick="pagecall('../auth/d_sub_view.php?gtype=P'+'&gbid=<?php echo $bbid;?>'+'&gbrid=<?php echo $bbrid;?>'+'&fdate=<?php echo $fdate;?>'+'&edate=<?php echo $edate;?>','#pay_s_view',0);$(this).removeAttr('onclick');">
            <?php }else{ ?>
              <tr>
            <?php } ?>
                <td style="text-align:center;">2</td>
                <td>Total Payment</td>
                <td style="text-align:right;"><?php echo number_format($t_payment,$decimel);?></td>
              </tr>
              <tr class="expandable-body d-none">
                <td colspan="3">
                  <div id="pay_s_view"></div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
  <section class="col-md-6 connectedSortable">
    <div class="card direct-chat direct-chat-primary">
      <div class="card-header">
        <h3 class="card-title">Purchase & Sales</h3>
        <div class="card-tools">
          <button type="button" class="btn btn-tool" data-card-widget="collapse">
          <i class="fas fa-minus"></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 table-responsive cscroll" style="height: 250px;">
        <div class="col-md-12 mt-1">
          <table class="ttable table-hover" cellpadding="0" cellspacing="0">
            <thead>
              <tr class="sticky-top">
                <th style="text-align:center;width:5%;">S/L</th>
                <th style="width:80%;">Particular</th>
                <th style="text-align:center;width:15%;">Amount</th>
              </tr>
            </thead>
            <tbody>
            <?php if($t_sales>0) { ?>
               <tr data-widget="expandable-table" aria-expanded="false" onclick="pagecall('../auth/d_sub_view.php?gtype=Sales'+'&gbid=<?php echo $bbid;?>'+'&gbrid=<?php echo $bbrid;?>'+'&fdate=<?php echo $fdate;?>'+'&edate=<?php echo $edate;?>','#esale_s_view',0);$(this).removeAttr('onclick');">
            <?php }else{ ?>
              <tr>
            <?php } ?>
                <td style="text-align:center;">1</td>
                <td>Total Sales</td>
                <td style="text-align:right;"><?php echo number_format($t_sales,$decimel);?></td>
              </tr>
              <tr class="expandable-body d-none">
                <td colspan="3">
                  <div id="esale_s_view"></div>
                </td>
              </tr>
            <?php if($t_received>0) { ?>
              <tr data-widget="expandable-table" aria-expanded="false" onclick="pagecall('../auth/d_sub_view.php?gtype=R'+'&gbid=<?php echo $bbid;?>'+'&gbrid=<?php echo $bbrid;?>'+'&fdate=<?php echo $fdate;?>'+'&edate=<?php echo $edate;?>','#rec_s_view',0);$(this).removeAttr('onclick');">
            <?php }else{ ?>
              <tr>
            <?php } ?>
                <td style="text-align:center;">2</td>
                <td>Total Received</td>
                <td style="text-align:right;"><?php echo number_format($t_received,$decimel);?></td>
              </tr>
              <tr class="expandable-body d-none">
                <td colspan="3">
                  <div id="rec_s_view"></div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>