<?php
session_start();
include_once('../../settings/master-class.php');
$con =new MasterClass;
$action = '';
if(isset($_GET['action'])){
  $action = $_GET['action'];
}

if($action == 'all'){
?>
<div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
  <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
    <li class="nav-item">
      <button class="
              nav-link
            
              note-link
              d-flex
              align-items-center
              justify-content-center
              
              px-3 px-md-3
              me-0 me-md-2 text-body-color
            " id="all-category">
        <i class="ti ti-report fill-white me-0 me-md-1"></i>
        <span class="d-none d-md-block font-weight-medium">All Pending and Paid Invoices</span>
      </button>
    </li>
    <li class="nav-item ms-auto">
      <a href="../../generate-lpdf.php?type=invoice_request_all" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
       <i class="ti ti-download me-0 me-md-1 fs-4"></i>
        <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
      </a>
    </li>
  </ul>
</div>
<table class="table border table-striped table-bordered">
  <tr>
    <?php
      $amounts = 0;
      $balances = 0;
      $paid = 0;
      $amount_paid = 0;
      $all = $con->getRows('invoices', array('where'=>'invoice_status !=2'));
      if(!empty($all)){        
        foreach($all as $count){
          $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
          if(!empty($updates)){
            foreach ($updates as $key) {
              $amount_paid += $key['paid_amount']; 
            }              
          }  
          $amounts +=$count['amount'];
          $paid = $count['amount_paid'] + $amount_paid;
        }

        $balances = $amounts - $paid;
        
      }
    ?>
    <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
    <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
    <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
  </tr>  
</table>  

<table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
  <thead class="header-item">
    <th>
      Inv #
    </th>
    <th>Sacco Name</th>
    <th>Invoice Amount</th>
    <th>Amount Paid</th>
    <th>Balance</th>
    <th>Date</th>
    <th>Action</th>
  </thead>
  <tbody>
    <?php
      $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id', 'order_by'=>'date_posted desc'));
      if(!empty($invoices)){
        $balance = 0;
        $paid = 0;
        $amount_paid = 0;
        foreach($invoices as $invoice){ 
          $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
          if(!empty($updates)){
              $amount_paid = $updates['paid_amount']; 
            }          
          $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
          $paid = $invoice['amount_paid'] + $amount_paid;
    ?>
          <tr class="search-items">
            <td>
              <?=ucwords($invoice['invoice_number'])?>
            </td>
            <td>
              <div class="d-flex align-items-center">
                <div class="ms-3">
                  <div class="user-meta-info">
                    <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                  </div>
                </div>
              </div>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
            </td>
            <td>
              <div class="action-btn">
                <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                  <i class="ti ti-eye fs-5"></i>
                </a>
              </div>
            </td>
          </tr>
    <?php }
      }
    ?>
  </tbody>
</table>  
<?php }else if($action == "invoice_today"){ ?>
<div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
  <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
    <li class="nav-item">
      <button class="
              nav-link
            
              note-link
              d-flex
              align-items-center
              justify-content-center
              
              px-3 px-md-3
              me-0 me-md-2 text-body-color
            " id="all-category">
        <i class="ti ti-report fill-white me-0 me-md-1"></i>
        <span class="d-none d-md-block font-weight-medium"> Invoices Posted Today <?=date("d M, Y");?></span>
      </button>
    </li>
    <li class="nav-item ms-auto">
      <a href="../../generate-lpdf.php?type=invoice_request_today" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
       <i class="ti ti-download me-0 me-md-1 fs-4"></i>
        <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
      </a>
    </li>
  </ul>
</div>
<table class="table border table-striped table-bordered">
  <tr>
    <?php
      $amounts = 0;
      $balances = 0;
      $paid = 0;
      $amount_paid = 0;
      $date = date("Y-m-d");
      $all = $con->getRows('invoices', array('where'=>'invoice_status !=2 and date_posted="'.$date.'"'));
      if(!empty($all)){        
        foreach($all as $count){
          $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
          if(!empty($updates)){
            foreach ($updates as $key) {
              $amount_paid += $key['paid_amount']; 
            }              
          }  
          $amounts +=$count['amount'];
          $paid = $count['amount_paid'] + $amount_paid;
        }

        $balances = $amounts - $paid;
        
      }
    ?>
    <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
    <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
    <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
  </tr>  
</table>  
<table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
  <thead class="header-item">
    <th>
      Inv #
    </th>
    <th>Sacco Name</th>
    <th>Invoice Amount</th>
    <th>Amount Paid</th>
    <th>Balance</th>
    <th>Date</th>
    <th>Action</th>
  </thead>
  <tbody>
    <?php
      $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id and a.date_posted="'.$date.'"', 'order_by'=>'date_posted desc'));
      if(!empty($invoices)){
        $balance = 0;
        $paid = 0;
        $amount_paid = 0;
        foreach($invoices as $invoice){ 
          $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
          if(!empty($updates)){
              $amount_paid = $updates['paid_amount'];
              $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
              $paid = $invoice['amount_paid'] + $amount_paid; 
            }else{
              $amount_paid = 0;
              $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
              $paid = $invoice['amount_paid'] + $amount_paid;
            }          
          
    ?>
          <tr class="search-items">
            <td>
              <?=ucwords($invoice['invoice_number'])?>
            </td>
            <td>
              <div class="d-flex align-items-center">
                <div class="ms-3">
                  <div class="user-meta-info">
                    <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                  </div>
                </div>
              </div>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
            </td>
            <td>
              <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
            </td>
            <td>
              <div class="action-btn">
                <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                  <i class="ti ti-eye fs-5"></i>
                </a>
              </div>
            </td>
          </tr>
    <?php }
      }
    ?>
  </tbody>
</table> 
<?php 
  }else if($action == "invoice_week"){ 
    $monday = date('Y-m-d', strtotime('monday this week'));
    $saturday = date('Y-m-d', strtotime('sunday this week'));
    $mondays = strtotime("last monday");
    $mondays = date('w', $mondays)==date('w') ? $mondays+7*86400 : $mondays;

    $sunday = strtotime(date("Y-m-d",$mondays)." +6 days");

    $this_week_start = date("d M, Y",$mondays);
    $this_week_end = date("d M, Y",$sunday);

  ?>
  <div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
    <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
      <li class="nav-item">
        <button class="
                nav-link
              
                note-link
                d-flex
                align-items-center
                justify-content-center
                
                px-3 px-md-3
                me-0 me-md-2 text-body-color
              " id="all-category">
          <i class="ti ti-report fill-white me-0 me-md-1"></i>
          <span class="d-none d-md-block font-weight-medium"> Invoices Posted This Week <?=date("d M, Y");?></span>
        </button>
      </li>
      <li class="nav-item ms-auto">
        <a href="../../generate-lpdf.php?type=invoice_request_week" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
        <i class="ti ti-download me-0 me-md-1 fs-4"></i>
          <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
        </a>
      </li>
    </ul>
  </div>
  <table class="table border table-striped table-bordered">
    <tr>
      <?php
        $amounts = 0;
        $balances = 0;
        $paid = 0;
        $amount_paid = 0;
        $date = date("Y-m-d");
        $all = $con->getRows('invoices', array('where'=>'invoice_status !=2 and  date_posted >="'.$monday.'" and date_posted <= "'.$saturday.'"'));
        if(!empty($all)){        
          foreach($all as $count){
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
            if(!empty($updates)){
              foreach ($updates as $key) {
                $amount_paid += $key['paid_amount']; 
              }              
            }  
            $amounts +=$count['amount'];
            $paid = $count['amount_paid'] + $amount_paid;
          }

          $balances = $amounts - $paid;
          
        }
      ?>
      <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
      <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
      <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
    </tr>  
  </table>  
  <table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
    <thead class="header-item">
      <th>
        Inv #
      </th>
      <th>Sacco Name</th>
      <th>Invoice Amount</th>
      <th>Amount Paid</th>
      <th>Balance</th>
      <th>Date</th>
      <th>Action</th>
    </thead>
    <tbody>
      <?php
        $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id and   a.date_posted >="'.$monday.'" and a.date_posted <= "'.$saturday.'"', 'order_by'=>'date_posted desc'));
        if(!empty($invoices)){
          $balance = 0;
          $paid = 0;
          $amount_paid = 0;
          foreach($invoices as $invoice){ 
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
            if(!empty($updates)){
                $amount_paid = $updates['paid_amount'];
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid; 
              }else{
                $amount_paid = 0;
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid;
              }          
            
      ?>
            <tr class="search-items">
              <td>
                <?=ucwords($invoice['invoice_number'])?>
              </td>
              <td>
                <div class="d-flex align-items-center">
                  <div class="ms-3">
                    <div class="user-meta-info">
                      <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                    </div>
                  </div>
                </div>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
              </td>
              <td>
                <div class="action-btn">
                  <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                    <i class="ti ti-eye fs-5"></i>
                  </a>
                </div>
              </td>
            </tr>
      <?php }
        }
      ?>
    </tbody>
  </table>

<?php 
  }else if($action == "invoice_month"){ 
    $newDate = date('F');
        $first_date = date('Y-m-d',strtotime('first day of this month'));
        $last_date = date('Y-m-d',strtotime('last day of this month'));

  ?>

    <div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
      <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
        <li class="nav-item">
          <button class="
                  nav-link
                
                  note-link
                  d-flex
                  align-items-center
                  justify-content-center
                  
                  px-3 px-md-3
                  me-0 me-md-2 text-body-color
                " id="all-category">
            <i class="ti ti-report fill-white me-0 me-md-1"></i>
            <span class="d-none d-md-block font-weight-medium"> Invoices Posted This Month <?=$newDate;?>, <?=date('Y')?></span>
          </button>
        </li>
        <li class="nav-item ms-auto">
          <a href="../../generate-lpdf.php?type=invoice_request_month" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
          <i class="ti ti-download me-0 me-md-1 fs-4"></i>
            <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
          </a>
        </li>
      </ul>
    </div>
    <table class="table border table-striped table-bordered">
      <tr>
        <?php
          $amounts = 0;
          $balances = 0;
          $paid = 0;
          $amount_paid = 0;
          $date = date("Y-m-d");
          $all = $con->getRows('invoices', array('where'=>'invoice_status !=2 and  date_posted >="'.$first_date.'" and date_posted <= "'.$last_date.'"'));
          if(!empty($all)){        
            foreach($all as $count){
              $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
              if(!empty($updates)){
                foreach ($updates as $key) {
                  $amount_paid += $key['paid_amount']; 
                }              
              }  
              $amounts +=$count['amount'];
              $paid = $count['amount_paid'] + $amount_paid;
            }

            $balances = $amounts - $paid;
            
          }
        ?>
        <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
        <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
        <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
      </tr>  
    </table>  
    <table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
      <thead class="header-item">
        <th>
          Inv #
        </th>
        <th>Sacco Name</th>
        <th>Invoice Amount</th>
        <th>Amount Paid</th>
        <th>Balance</th>
        <th>Date</th>
        <th>Action</th>
      </thead>
      <tbody>
        <?php
          $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id and   a.date_posted >="'.$first_date.'" and a.date_posted <= "'.$last_date.'"', 'order_by'=>'date_posted desc'));
          if(!empty($invoices)){
            $balance = 0;
            $paid = 0;
            $amount_paid = 0;
            foreach($invoices as $invoice){ 
              $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
              if(!empty($updates)){
                  $amount_paid = $updates['paid_amount'];
                  $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                  $paid = $invoice['amount_paid'] + $amount_paid; 
                }else{
                  $amount_paid = 0;
                  $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                  $paid = $invoice['amount_paid'] + $amount_paid;
                }          
              
        ?>
              <tr class="search-items">
                <td>
                  <?=ucwords($invoice['invoice_number'])?>
                </td>
                <td>
                  <div class="d-flex align-items-center">
                    <div class="ms-3">
                      <div class="user-meta-info">
                        <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                      </div>
                    </div>
                  </div>
                </td>
                <td>
                  <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
                </td>
                <td>
                  <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
                </td>
                <td>
                  <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
                </td>
                <td>
                  <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
                </td>
                <td>
                  <div class="action-btn">
                    <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                      <i class="ti ti-eye fs-5"></i>
                    </a>
                  </div>
                </td>
              </tr>
        <?php }
          }
        ?>
      </tbody>
    </table>

<?php 
  }else if($action == "invoice_year"){ 
    $newDate = date('Y');
        $first_date =  date('Y-m-d', strtotime('first day of january this year'));
        $last_date =  date('Y-m-d', strtotime('last day of december this year'));

  ?>

  <div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
    <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
      <li class="nav-item">
        <button class="
                nav-link
              
                note-link
                d-flex
                align-items-center
                justify-content-center
                
                px-3 px-md-3
                me-0 me-md-2 text-body-color
              " id="all-category">
          <i class="ti ti-report fill-white me-0 me-md-1"></i>
          <span class="d-none d-md-block font-weight-medium"> Invoices Posted This Year of Of <?=date('Y')?></span>
        </button>
      </li>
      <li class="nav-item ms-auto">
        <a href="../../generate-lpdf.php?type=invoice_request_year" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
        <i class="ti ti-download me-0 me-md-1 fs-4"></i>
          <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
        </a>
      </li>
    </ul>
  </div>
  <table class="table border table-striped table-bordered">
    <tr>
      <?php
        $amounts = 0;
        $balances = 0;
        $paid = 0;
        $amount_paid = 0;
        $date = date("Y-m-d");
        $all = $con->getRows('invoices', array('where'=>'invoice_status !=2 and  date_posted >="'.$first_date.'" and date_posted <= "'.$last_date.'"'));
        if(!empty($all)){        
          foreach($all as $count){
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
            if(!empty($updates)){
              foreach ($updates as $key) {
                $amount_paid += $key['paid_amount']; 
              }              
            }  
            $amounts +=$count['amount'];
            $paid = $count['amount_paid'] + $amount_paid;
          }

          $balances = $amounts - $paid;
          
        }
      ?>
      <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
      <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
      <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
    </tr>  
  </table>  
  <table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
    <thead class="header-item">
      <th>
        Inv #
      </th>
      <th>Sacco Name</th>
      <th>Invoice Amount</th>
      <th>Amount Paid</th>
      <th>Balance</th>
      <th>Date</th>
      <th>Action</th>
    </thead>
    <tbody>
      <?php
        $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id and   a.date_posted >="'.$first_date.'" and a.date_posted <= "'.$last_date.'"', 'order_by'=>'date_posted desc'));
        if(!empty($invoices)){
          $balance = 0;
          $paid = 0;
          $amount_paid = 0;
          foreach($invoices as $invoice){ 
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
            if(!empty($updates)){
                $amount_paid = $updates['paid_amount'];
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid; 
              }else{
                $amount_paid = 0;
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid;
              }          
            
      ?>
            <tr class="search-items">
              <td>
                <?=ucwords($invoice['invoice_number'])?>
              </td>
              <td>
                <div class="d-flex align-items-center">
                  <div class="ms-3">
                    <div class="user-meta-info">
                      <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                    </div>
                  </div>
                </div>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
              </td>
              <td>
                <div class="action-btn">
                  <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                    <i class="ti ti-eye fs-5"></i>
                  </a>
                </div>
              </td>
            </tr>
      <?php }
        }
      ?>
    </tbody>
  </table>

<?php 
  }else if($action == "advance_custom"){ 
    
    $selection = '';
    $first_date = $_GET['date_from'];
    $last_date  = $_GET['date_to'];
    $sacco = $_GET['sacco'];
    if($sacco == 'All'){
      $selection = "All Saccos";

      $all = $con->getRows('advance_requests', array('where'=>'advance_status >=4 and date_approved >="'.$first_date.'" 
              and date_approved <= "'.$last_date.'"'));
      $requisitions = $con->getRows('advance_requests a, muscco_members b, departments c', 
               array('where'=>'a.requested_by=b.muscco_member_id and b.department_id=c.department_id and a.advance_status >=4 and a.date_approved >="'.$first_date.'" and a.date_approved <= "'.$last_date.'"','order_by'=>'date_posted desc'));
    }else{
      $sacco_name = $con->getRows('sacco', array('where'=>'sacco_id="'.$sacco.'"', 'return_type'=>'single'));
      $selection = $sacco_name['sacco_name'];
    }    
  ?>
  <div class="container-fluid note-has-grid" style="width:100%; padding:2px;">
    <ul class="nav nav-pills p-3 mb-3 rounded align-items-center card flex-row">
      <li class="nav-item">
        <button class="
                nav-link
              
                note-link
                d-flex
                align-items-center
                justify-content-center
                
                px-3 px-md-3
                me-0 me-md-2 text-body-color
              " id="all-category">
          <i class="ti ti-report fill-white me-0 me-md-1"></i>
          <span class="d-none d-md-block font-weight-medium"> Invoices Posted for <b><?=$selection?></b> From <?=$con->shortDate($first_date)?> To <?=$con->shortDate($last_date)?></span>
        </button>
      </li>
      <li class="nav-item ms-auto">
        <a href="../../generate-lpdf.php?type=invoice_custom" target="_blank" class="btn btn-primary d-flex align-items-center px-3" id="add-notes">
        <i class="ti ti-download me-0 me-md-1 fs-4"></i>
          <span class="d-none d-md-block font-weight-medium fs-3">Download</span>
        </a>
      </li>
    </ul>
  </div>
  <table class="table border table-striped table-bordered">
    <tr>
      <?php
        $amounts = 0;
        $balances = 0;
        $paid = 0;
        $amount_paid = 0;
        $options = ($sacco == "All") ? '' : ' and sacco_id="'.$sacco.'" '; 
        $all = $con->getRows('invoices', array('where'=>'invoice_status !=2 and  date_posted >="'.$first_date.'" and date_posted <= "'.$last_date.'" '.$options));
        if(!empty($all)){        
          foreach($all as $count){
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$count['invoice_id'].'"'));
            if(!empty($updates)){
              foreach ($updates as $key) {
                $amount_paid += $key['paid_amount']; 
              }              
            }  
            $amounts +=$count['amount'];
            $paid = $count['amount_paid'] + $amount_paid;
          }

          $balances = $amounts - $paid;
          
        }
      ?>
      <th>Total Invoice Amount<br> MK<?=number_format($amounts,2,'.',',')?></th>
      <th>Amount Paid<br> MK<?=number_format($paid,2,'.',',')?></th>
      <th>Balance<br> MK<?=number_format($balances,2,'.',',')?></th></th>
    </tr>  
  </table>  
  <table id="scroll_hor" class="table border table-striped table-bordered display dataTable no-footer" style="width: 100%;" aria-describedby="scroll_hor_info">
    <thead class="header-item">
      <th>
        Inv #
      </th>
      <th>Sacco Name</th>
      <th>Invoice Amount</th>
      <th>Amount Paid</th>
      <th>Balance</th>
      <th>Date</th>
      <th>Action</th>
    </thead>
    <tbody>
      <?php
        $options = ($sacco == "All") ? '' : ' and a.sacco_id="'.$sacco.'" '; 
        $invoices = $con->getRows('invoices a, sacco b', array('where'=>'a.invoice_status !=2 and a.sacco_id=b.sacco_id and  a.date_posted >="'.$first_date.'" and a.date_posted <= "'.$last_date.'" '.$options, 'order_by'=>'date_posted desc'));
        if(!empty($invoices)){
          $balance = 0;
          $paid = 0;
          $amount_paid = 0;
          foreach($invoices as $invoice){ 
            $updates = $con->getRows('invoice_status', array('where'=>'invoice_id="'.$invoice['invoice_id'].'"','return_type'=>'single'));
            if(!empty($updates)){
                $amount_paid = $updates['paid_amount'];
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid; 
              }else{
                $amount_paid = 0;
                $balance = $invoice['amount'] - ($invoice['amount_paid'] + $amount_paid);
                $paid = $invoice['amount_paid'] + $amount_paid;
              }          
            
      ?>
            <tr class="search-items">
              <td>
                <?=ucwords($invoice['invoice_number'])?>
              </td>
              <td>
                <div class="d-flex align-items-center">
                  <div class="ms-3">
                    <div class="user-meta-info">
                      <h6 class="user-name mb-0" data-name=""><?=ucwords($invoice['sacco_name'])?></h6>
                    </div>
                  </div>
                </div>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($invoice['amount'],2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($paid,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-email-addr">K<?=number_format($balance,2,'.',',')?></span>
              </td>
              <td>
                <span class="usr-ph-no" ><?=$con->shortDate($invoice['date_posted'])?></span>
              </td>
              <td>
                <div class="action-btn">
                  <a href="dashboard.php?page=invoice_details&invoice_id=<?=$invoice['invoice_id']?>" class="btn btn-primary btn-sm  ms-2"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Invoice Details">
                    <i class="ti ti-eye fs-5"></i>
                  </a>
                </div>
              </td>
            </tr>
      <?php }
        }
      ?>
    </tbody>
  </table>

<?php } ?>
<script src="../../dist/libs/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="../../dist/js/datatable/datatable-basic.init.js"></script> 