<? include "top.php";

$stdt = $_REQUEST['stdt'];
$enddt = $_REQUEST['enddt'];
$customer = 1;
$name = mysqli_real_escape_string($con, $_POST['search']);
$custid = mysqli_real_escape_string($con, $_POST['search']);



header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=duereport.xls");
?>


<table style="width:100%;" border='1'>

   <tr>
      <th>Sl. No</th>
      <th>Customer Name</th>
      <th>A/C No</th>
      <th>Address</th>
      <th>Area</th>
      <? if ($customer == '1') { ?>
         <th>VC NO</th>
         <th>MSO no</th>
      <? } else { ?>
         <th>IP</th>
         <th>userid</th>
      <? } ?>
      <th>Bill Date</th>
      <th>Period</th>
      <th>Start Dt</th>
      <th>End Dt</th>
      <th>Bill Amt</th>
      <th>Status</th>
      <th>Paid(in Rs.)</th>
      <th>Payment Date</th>
      <th>Due(in Rs.)</th>
   </tr>


   <?

   $getc = mysqli_query($con, "SELECT * FROM adduser");
   while ($arrcust = mysqli_fetch_array($getc)) {

      $row = mysqli_query($con, "SELECT * FROM `cbill` WHERE bl_cid='$arrcust[id]' ORDER BY blid DESC LIMIT 1");


      while ($arrc = mysqli_fetch_array($row)) {
         $c++;

         $total = floatval($total) + floatval($arrc['bl_amt']);
         $paid = floatval($paid) + floatval($arrc['bl_payamt']);
         $dues = floatval($dues) + floatval($arrc['bl_due']);
         $getcust = f(q("SELECT * FROM `adduser` WHERE id='$arrc[bl_cid]'"));
         $billamt = floatval($arrc['bl_amt']) + floatval($arrc['bl_prevdue']);
         $bildue = floatval($billamt) - floatval($arrc['bl_payamt']);
         ?>
         <tr style="border-bottom: 1px solid gray;<? if ($arrc['bl_stat'] == 0) { ?>background-color: #f1d4c4;<? } ?>">
            <td class="table-plus" style="text-align: center;"><?= $c ?></td>
            <td><?= $arrc['bl_cname'] ?></td>
            <td><?= $getcust['custid'] ?></td>
            <td><?= $getcust['address'] ?></td>
            <td>
               <?= $getcust['area_name'] ?>
            </td>
            <td><?= $getcust['vc'] ?></td>
            <td><?= $getcust['msoacno'] ?></td>
            <td><?= $arrc['bl_dt'] ?></td>
            <td><?= $arrc['bl_month'] ?>,<?= $arrc['bl_year'] ?></td>

            <td><?= $arrc['bl_stdt'] ?></td>
            <td><?= $arrc['bl_expiry'] ?></td>
            <td><?= $billamt ?></td>
            <td><? if ($arrc['bl_stat'] == 1) { ?>Paid<? } else { ?>Unpaid<? } ?></td>
            <td><?= $arrc['bl_payamt'] ?></td>
            <td><?= $arrc['bl_paydt'] ?></td>
            <td><?= $bildue ?></td>
         </tr>
      <? }
   } ?>

</table><br>
<table>
   <tr>
      <td style="width: 65%;"></td>
      <td style="text-align:right;padding-right: 15px;"><b>Total Bill</b></td>
      <td>&#8377; <?= $total ?></td>
      <td></td>
   </tr>
   <tr>
      <td style="width: 65%;"></td>
      <td style="text-align:right;padding-right: 15px;"><b>Total Paid Amount</b></td>

      <td>&#8377; <?= $paid ?></td>
      <td></td>
   </tr>
   <tr>
      <td style="width: 65%;"></td>
      <td style="text-align:right;padding-right: 15px;"><b>Total Due Amount</b></td>

      <td>&#8377; <?= $dues ?></td>
      <td></td>
   </tr>

</table>