<?php 
 $cc=0;
 $tPrice=0;     
 $ttprice=0;
 $tptde=0;     
 $tbal=0;
  require_once "labClass.php";
 $con = new labClass();
  ?>
  <div id="co">
<table class="table">
    <tr><td colspan="6" align="center"><small>REPORTS OF SALES BY INVOICES:</small></td></tr>
<tr><td colspan="2">
      
      <label for="stock" class="form-label">Sort sales by:</label>
      <select class="form-control"  name="stock" id="stock" onchange="processDat();"> 
      <option value="">Select</option>
      <option value="PAID">PAID</option>  
       <option value="NOT PAID">NOT PAID</option>
       <option value="BOTH">BOTH</option>     
      ?>
      </select>
</td>
      <td colspan="3">
        <sup id="tr"></sup>
      <label for="cname" class="form-label">Choose customer from the list:</label>
<input class="form-control" list="cnames" name="cname" id="cname" onchange="processData();">
<datalist id="cnames">
    <?php
     $Q= $con->getOrderListBcN();
     if ($Q->rowCount()>0) {
     $lb=
     $Q->fetchAll();
     foreach ($lb as $key => $l) {
      
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
            
           }
           }
 
      ?>
      </datalist>

    </td>
  </tr>
  <tr>
  <td style="width:15%;">Sort by date range:</td><td style="width:10%;" ><input type="date" style="width:200px;" class="form-control" id="dat1" name="dat1"  value="<?php echo Date("Y-m-d");?>" >
  </td>
  <td>and:</td><td style="width:10%;">
          <input type="date" style="width:200px;" class="form-control" id="dat2" name="dat2" value="<?php echo Date("Y-m-d");?>">
  </td><td style="width:10%;">  
  <button type="submit" class="btn btn-primary" style="width:100%; height:40px;" onclick="var stock=$('#stock').val();var sdate=$('#sdate').val(); var dat1=$('#dat1').val(); var dat2=$('#dat2').val();  $.post('pharmSinvPrevBdtLPO.php',{sdate:sdate,dat1:dat1,dat2:dat2,stock:stock},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td>      
 </tr>
 </table>

 <div class="container-fluid " id="dpl">
  <?php
 $chkp= $con->getInvRec(); 
     
if ($chkp->rowCount()>0) {

  $chp=$chkp->fetchAll();
  ?>
<table class="table" border="1"> 
   <thead class="bg-secondary" >
       
      <th>  
        <small><h6>DATE</h6></small>
      </th>
      <th>  
        <small><h6>INV. NO/C'S NAME</h6></small>
      </th>
       
      <th>  
        <small><h6>TOTAL PAYABLE</h6></small>
      </th>
      <th>  
        <small><h6>PAID TO DATE</h6></small>
      </th>
      <th>  
        <small><h6>BALANCE TO DATE</h6></small>
      </th>
       <th>  
        <small><h6>PAY STATUS</h6></small>
      </th>
      
    </thead>
   <?php
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];

    $cp= $con->getThisInvItemList($orderID);
    $cc++; 
    $nr=$cp->rowCount();


        ?>
 <tbody>
 <tr>
   
 <td>
    <?php echo $p['orderdate'];?> 
    </td>
     <td>  
     <?php echo $p['orderID']."."; $parts = explode('(', $p['cname']); echo $parts[0]."<br/>";$parts = explode('(', $p['cname']);echo  str_replace(")","", $parts[1]);?> 
     </td>
     <td align="right">
    <?php echo number_format($p['payableA'],2);?> 
    </td>
    <td align="right">
    <?php echo number_format($p['paidtodate'],2);
     $tptde=$tptde+$p['paidtodate'];     
     $tbal= $tbal+$p['balance'];
    ?> 
    </td>
    <td align="right">
    <?php echo number_format($p['balance'],2);?> 
    </td>
      <td>
    <?php echo $p['pstatus'];?> 
    </td>
    </tr> 
     <tr>
    <td colspan="2">  
    </td>
     <td colspan="3">
       <table class="table">    
        <th>  
           <small><h6>Item name</h6></small>
         </th>
         <th>  
           <small><h6>Price</h6></small>
          </th>
          <th>  
            <small><h6>Qty</h6></small>
           </th>
           <th>  
             <small><h6>Total</h6></small>
          </th>


   <?php 
   
    if ($cp->rowCount()>0) {

    $ch=$cp->fetchAll();
     
    foreach ($ch as $key => $c) {
      ?>
         <tr>
            <td>  
            <?php echo $c['iname'];?>
            </td> 
            <td align="right">
            <?php echo number_format($c['isprice'],2);?>
            </td> 
            <td align="right"> 
               <?php echo $c['eQty'];?>    
            </td>
            <td align="right">
              <?php echo number_format($c['tprice'],2);?>  
            </td>
              
         </tr>
         
 
       <?php
       $tPrice=$tPrice+$c['isprice']*$c['eQty'];
        } 
        ?>

         <tr>
            <td colspan="3" align="right">
              Sub-total:  
            </td>
            <td align="right">
               <?php echo number_format($tPrice,2);?>  
            </td>
        </tr>
 
     <?php 
      } 
     ?>




</table>

</td>
<td>  
</td>
</tr>
 
          
     
    

<?php
$ttprice=$ttprice+$tPrice;
$tPrice=0;      
}
$cc=0; 
?>
<tr><td colspan="2" align="right">Total:</td><td align="right"><?php echo number_format($ttprice,2);?></td><td align="right"><?php echo number_format($tptde,2);?></td><td align="right"><?php echo number_format($tbal,2);?></td></tr>
</tbody>
</table> 

<?php
}
?>
</div>
</div>
<div>
   <button type="submit" class="btn btn-primary" value="Print" style="width:10%; height:30px;" onclick="printPageArea();">Print </button>
</div>
<script type="text/javascript"> 
  function printPageArea(){
    var printContent = document.getElementById('co').innerHTML;
    var originalContent = document.body.innerHTML;
    document.body.innerHTML = printContent;
    window.print();
    document.body.innerHTML = originalContent;
}
 
function processDat(){ 
document.getElementById('dpl').innerHTML='';
var ecc=0;  
var stock =document.getElementById('stock').value;
$.trim(stock); 
var dat1 =document.getElementById('dat1').value; 
//$.trim(dat1);
var dat2 =document.getElementById('dat2').value; 
//$.trim(dat2);  
  //FIRST NAME VALIDATION
  
if(stock==""){ 

         ecc++;
         alert('Stock required!..');
             }
  

    if(ecc==0)
    { 
        $.post("pharmSinvRBcNLPO.php",
          {stock:stock,dat1:dat1,dat2:dat2},
          function(data){
      $('#dpl').html(data).show();

    });
    }
  } 

function processData(){ 
var ecc=0; 
 
var cname= document.getElementById('cname').value;
$.trim(cname); 
var stock =document.getElementById('stock').value;
$.trim(stock); 
var dat1 =document.getElementById('dat1').value; 
var dat2 =document.getElementById('dat2').value;  
  //FIRST NAME VALIDATION
  if(stock==""){ 

         ecc++;
         alert('Specify sales type!..');

             }
 if(cname==""){ 

         ecc++;
         alert('Customer name required!..');

             }
 
  if(dat1==""){ 

         ecc++;
         alert('Date range is required!..');

             }
 
if(dat2==""){ 

         ecc++;
         alert('Date range is required!..');

             }
 
    if(ecc==0)
    { 
        $.post("pharmSinvRBcN.php",
          {cname:cname,stock:stock,dat1:dat1,dat2:dat2},
          function(data){
      $('#dpl').html(data).show();

    });
    }
 
} 

 </script>