<?php 
  $cc=0;
  $rc=0;
  $tPrice=0;     
  $ttprice=0;
  $ttInv=0;
  $user=$_POST["user"];
  require_once "labClass.php";
  
 $con = new labClass();
  ?>
  <div class="container-fluid" id="co"> 
  <small><h6>PURCHASE ORDER REPORTS:</h6></small>
  <table class="table">
    <tr>
      <td colspan="5">
        <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->getporderListBcN();
     if ($Q->rowCount()>0) {
     $lb=
     $Q->fetchAll();
     foreach ($lb as $key => $l) {
      
      ?>
      <option value="<?php echo  $l["cname"];?>">  
             <?php
            
           }
           }
 
      ?>
      </datalist>

    </td>
     <td style="width:15%;">
          <label for="unrecv" class="form-label">Sort by un-received:</label>
         
     <select  class="form-control" id="unrecv" name="unrecv" onchange="processD();"> 
     <option value=""></option>
     <option value="Un-confirmed">Un-confirmed</option> 
     </select>
          
  </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 sdate=$('#sdate').val(); var dat1=$('#dat1').val(); var dat2=$('#dat2').val();  $.post('pharmPorderRBdtLPO.php',{sdate:sdate,dat1:dat1,dat2:dat2},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td> 
  
 
 </tr>
 </table>

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

  $chp=$chkp->fetchAll();
  ?>
<table class="table table-bordered" border="1"> 
   <thead class="bg-secondary" >
       
      <th>  
        <small><h6>ORDER DATE</h6></small>
      </th>
      <th>  
        <small><h6>ORDER NO</h6></small>
      </th>
       
      <th>  
        <small><h6>PATICULAR</h6></small>
      </th>
      
      <th>  
        <small><h6>QTY</h6></small>
      </th>
      <<th>  
        <small><h6>CONFIRM RECV QTY</h6></small>
      </th>
       
       <th>  
        <small><h6>RECEIVED DATE</h6></small>
      </th>
      <th>  
        <small><h6>RECEIVE FROM</h6></small>
      </th>
      <th>  
        <small><h6>VERIFIED BY</h6></small>
      </th>
    </thead>
   <?php
    foreach ($chp as $key => $p) {
    $orderID=$p['orderID'];

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


        ?>
 <tbody>
 <tr>
    <td rowspan="<?php echo $nr+2; ?>">
    <?php echo $p['orderdate'];?> 
    </td>
 
     <td rowspan="<?php echo $nr+2; ?>" >  
     <sup id="orno<?php echo $cc; ?>"><?php echo $p['orderID'];?></sup><p/><sup><small><?php echo $p['dname'];?></small></sup><br/>
     </td>

    <td colspan="3" >
    </td>
   <td rowspan="<?php echo $nr+2; ?>">
    <?php if ($p['recvby']!=NULL) 
    {
       echo $p['invdate'];
    }else{
    ?>
    <input type="date" style="width:100%;" class="form-control" id="dt<?php echo $cc; ?>" name="dt<?php echo $cc; ?>"  value="<?php echo Date("Y-m-d");?>" >

    
    <?php
    } 
    ?>
    <sup id="rdn<?php echo $cc; ?>"></sup>
    </td>
    
    <td rowspan="<?php echo $nr+2; ?>">
    From:<?php echo $p['receivedF'];?><br/>
    Vehicle:<?php echo $p['cpno'];?><br/>
    Owner:<?php echo $p['owner'];?><br/>
    Driver:<?php echo $p['dname'];?><br/>
    Phone no:<?php echo $p['pno'];?>
    <?php if ($p['recvby']=="") 
        {?>
        <a href="#" onclick="$('#hid<?php echo $cc; ?>').show();">Edit</a>
        <div style="display:none;" id="hid<?php echo $cc; ?>">
         <input type="text" style="width:100%;" onkeypress="$('#cbcp<?php echo $cc; ?>').show();" class="form-control"  id="ccpno<?php echo $cc; ?>" name="ccpno<?php echo $cc; ?>"  value="<?php echo $p['cpno']; ?>" >
          <input type="text" style="width:100%;" onkeypress="$('#cbcp<?php echo $cc; ?>').show();" class="form-control"  id="cowner<?php echo $cc; ?>" name="cowner<?php echo $cc; ?>"  value="<?php echo $p['owner']; ?>" >
           <input type="text" style="width:100%;" onkeypress="$('#cbcp<?php echo $cc; ?>').show();" class="form-control"  id="cdname<?php echo $cc; ?>" name="cdname<?php echo $cc; ?>"  value="<?php echo $p['dname']; ?>" >
           <input type="text" style="width:100%;" onkeypress="$('#cbcp<?php echo $cc; ?>').show();" class="form-control"  id="pcno<?php echo $cc; ?>" name="pcno<?php echo $cc; ?>"  value="<?php echo $p['pno']; ?>" >
           <input type="Submit" onclick="var orderID =document.getElementById('orno<?php echo $cc;?>').innerHTML;var ccpno=$('#ccpno<?php echo $cc;?>').val();
           var pno=$('#pcno<?php echo $cc;?>').val();var cowner=$('#cowner<?php echo $cc;?>').val();var cdname=$('#cdname<?php echo $cc;?>').val();
            if(orderID!='' && ccpno!='' && cowner!='' && cdname!=''&& pno!='') {$.post('changeTransDetail.php',{orderID:orderID,ccpno:ccpno,cdname:cdname,pno:pno,cowner:cowner},function(data){$('#rdn<?php echo $cc;?>').html(data).show();});$('#cbcp<?php echo $cc; ?>').show();} else{document.getElementById('rdn<?php echo $cc;?>').innerHTML='All field required!..';}" style="width:50px;"  id="cbcp<?php echo $cc; ?>" name="cbcp<?php echo $cc; ?>"  value="Save" >
           </div>
        <?php
         } 
        ?>
    <br>
    <?php if ($p['ttransfee']==$p['paidB']) 
        {
       echo "Full paid:" .$p['paidB'];
         }else{
             echo "Total T. fee: ".$p['ttransfee']."<br> Paid adv:".$p['paidB']."<br> Remained balance:".$p['ttransfee']-$p['paidB'];
        ?>
    <br> Complete paid:
         <input type="number" style="width:100%;" onkeypress="$('#sbcp<?php echo $cc; ?>').show();" class="form-control"  id="cpay<?php echo $cc; ?>" name="cpay<?php echo $cc; ?>"  value="<?php echo $p['ttransfee']; ?>" >

          <input type="Submit" onclick="var orderID =document.getElementById('orno<?php echo $cc;?>').innerHTML;var cpay=$('#cpay<?php echo $cc;?>').val();     
            if(orderID!='' && cpay!='') {$.post('completeTransfeeP.php',{orderID:orderID,cpay:cpay},function(data){$('#rdn<?php echo $cc;?>').html(data).show();});document.getElementById('cpay<?php echo $cc;?>').disabled=true;$('#sbcp<?php echo $cc; ?>').show();} else{document.getElementById('rdn<?php echo $cc;?>').innerHTML='All field required!..';}" style="width:50px;"  id="sbcp<?php echo $cc; ?>" name="sbcp<?php echo $cc; ?>"  value="Save" >
    <?php
    } 
    ?>
    </td>
    
    
    
    
    
    
    
    <div id="ds"></div>
    <td rowspan="<?php echo $nr+2; ?>"> 
   
    <?php if ($p['recvby']!="") 
        {
       echo $p['recvby'];
         }else{
        ?>
    
         <input type="text" style="width:100%;" onkeypress="$('#sb<?php echo $cc; ?>').show();" class="form-control" disabled=true; id="confby<?php echo $cc; ?>" name="confby<?php echo $cc; ?>"  value="<?php echo $user; ?>" >

          <input type="Submit" onclick="var orderID =document.getElementById('orno<?php echo $cc;?>').innerHTML;var confby=$('#confby<?php echo $cc;?>').val(); var dat=$('#dt<?php echo $cc;?>').val();    
            if(orderID!='' && confby!='' && dat!='') {$.post('pharmUpdateRecvPInvPro.php',{orderID:orderID,confby:confby,dat:dat},function(data){$('#rdn<?php echo $cc;?>').html(data).show();});document.getElementById('confby<?php echo $cc;?>').value='';document.getElementById('dt<?php echo $cc;?>').disabled=true;$('#sb<?php echo $cc; ?>').hide();} else{document.getElementById('rdn<?php echo $cc;?>').innerHTML='All field required!..';}" style="width:50px;"  id="sb<?php echo $cc; ?>" name="sb<?php echo $cc; ?>"  value="Save" >
    <?php
    } 
    ?>
    </td>
    </tr>    
     <?php 
   
    if ($cp->rowCount()>0) {

    $ch=$cp->fetchAll();
     
      foreach ($ch as $key => $c) {
         $rc++;
      ?>
          <tr>
            <td>
            <sup id="in<?php echo $rc;?>"><?php echo $c['iname'];?></sup>
            </td> 
            
            <td> 
               <?php echo $c['eQty'];?><br/> 
                 <?php if ($c['confrecv']!=NULL) 
                { 
                
                $text=$c['confrecv'];
                if (preg_match_all('/\((\d+)\)/', $text, $matches)) {
                  $numbers = $matches[1]; // all numbers inside ()
                  $sum = array_sum($numbers); 
                  
                 $quantity = (int) $sum;
                 if ($c['eQty']>$quantity)
                 {
                     echo "rcv:".$quantity;
                 }
                }
                }
                ?>     
            </td>

            <td>
              <sup id="rd<?php echo $rc; ?>"></sup>
               <?php if ($c['confrecv']!=NULL) 
                { 
                
                $text=$c['confrecv'];
                 if (preg_match_all('/\((\d+)\)/', $text, $matches)) {
                  $numbers = $matches[1]; // all numbers inside ()
                  $sum = array_sum($numbers);
                 $quantity = (int) $sum; 
                 //$quantity = (int) $matches[1];
                 if ($c['eQty']>$quantity)
                 {
                 ?>
                 Add to:
                  <select class="form-control" name="" id="sOpt<?php echo $rc; ?>">
                   <option value="">Select</option>
                  <option value="SHOP">SHOP</option>
                   <option value="STORE">STORE</option> 
                 </select>
                 <input type="hidden"  id="confrv<?php echo $rc; ?>" name="confrv<?php echo $rc; ?>"  value="<?php echo $text; ?>" >
                 
                 <input type="hidden"  id="pQty<?php echo $rc;?>" name="pQty<?php echo $rc;?>"  value="<?php echo $quantity; ?>" >
                 
                 <input type="number" onkeypress="$('#sbq<?php echo $rc;?>').show();" style="width:100%;" class="form-control" id="recvq<?php echo $rc;?>" name="recvq<?php echo $rc;?>"  value="" >
                   <input type="Submit" onclick="var inam =document.getElementById('in<?php echo $rc;?>').innerHTML;var orderID =document.getElementById('orno<?php echo $cc;?>').innerHTML;var recvq=$('#recvq<?php echo $rc;?>').val();var sOpt=$('#sOpt<?php echo $rc;?>').val();var pQty=$('#pQty<?php echo $rc;?>').val(); var user=$('#user').val();var confrv=$('#confrv<?php echo $rc;?>').val(); var sdate=$('#sdate').val();     
                     if(sOpt!=''&&  orderID!=''&& inam!='' && recvq!=''&& sdate!='' && user!='') {$.post('pharmupdatePInvProLPO.php',{confrv:confrv,pQty:pQty,sOpt:sOpt,orderID:orderID,recvq:recvq,inam:inam,user:user,sdate:sdate},function(data){$('#rd<?php echo $rc; ?>').html(data).show();});document.getElementById('recvq<?php echo $rc;?>').value='';  $('#sbq<?php echo $rc; ?>').hide();} else{document.getElementById('rd<?php echo $rc;?>').innerHTML='All field required!..';}" style="width:50px; display:none;"  id="sbq<?php echo $rc; ?>" name="sbq<?php echo $rc; ?>"  value="Save" >
                 
                 
                  <?php    
                 }
                 else 
                  {
                       echo $c['confrecv'];
                  } 
                } 
                
                }
                else
                 {
                  ?>
                  Add to:
                  <select class="form-control" name="sOpt<?php echo $rc; ?>" id="sOpt<?php echo $rc; ?>">
                   <option value="">Select</option>
                  <option value="SHOP">SHOP</option>
                   <option value="STORE">STORE</option> 
                 </select>
                 <input type="number" onkeypress="$('#sbq<?php echo $rc; ?>').show();" style="width:100%;" class="form-control" id="recvq<?php echo $rc;?>" name="recvq<?php echo $rc; ?>"   value="1" >
                   <input type="Submit" onclick="var inam =document.getElementById('in<?php echo $rc;?>').innerHTML;var orderID =document.getElementById('orno<?php echo $cc;?>').innerHTML;
                     var recvq=$('#recvq<?php echo $rc;?>').val();var sOpt=$('#sOpt<?php echo $rc;?>').val();var pQty=0; var user=$('#user').val();var confrv=''; var sdate=$('#sdate').val();   alert(sOpt); alert(orderID); alert(inam); alert(recvq); alert(sdate);
                     if(sOpt!=''&& orderID!=''&& inam!='' && recvq!=''&& sdate!='' && user!='') {$.post('pharmupdatePInvProLPO.php',{confrv:confrv,pQty:pQty,sOpt:sOpt,orderID:orderID,recvq:recvq,inam:inam,user:user,sdate:sdate},function(data){$('#rd<?php echo $rc; ?>').html(data).show();});document.getElementById('recvq<?php echo $rc;?>').disabled=true;  $('#sbq<?php echo $rc; ?>').hide();} else{document.getElementById('rd<?php echo $rc;?>').innerHTML='Incomplete submission!..';}" style="width:50px; display:none;"  id="sbq<?php echo $rc; ?>" name="sbq<?php echo $rc; ?>"  value="Save" >
                   <?php
                } 
               ?>
             </td>
              
                   </tr>
                   <?php 
                       } 
                       
                    }       
}
$cc=0; 
?>
 
</tbody>
</table> 

<?php

}
?>
 </div>
 <script type="text/javascript">
   function processData(){ 
var ecc=0; 
var cname =document.getElementById('cname').value; 
$.trim(cname);
var dat1 =document.getElementById('dat1').value; 
$.trim(dat1);
var dat2 =document.getElementById('dat2').value; 
$.trim(dat2);
  //FIRST NAME VALIDATION
  
if(cname==""){ 

         ecc++;
         document.getElementById('tr').innerHTML="Item customer is required!..";

             }
else{

document.getElementById('tr').innerHTML="";
    }
  if(dat1==""){ 

         ecc++;
         document.getElementById('tr').innerHTML="Item date range is required!..";

             }
else{

document.getElementById('tr').innerHTML="";
    }
if(dat2==""){ 

         ecc++;
         document.getElementById('tr').innerHTML="Item date range is required!..";

             }
else{

document.getElementById('tr').innerHTML="";
    }
    if(ecc==0)
    { 
        $.post("pharmporderBcNLPO.php",
          {cname:cname,dat1:dat1,dat2:dat2},
          function(data){
      $('#dpl').html(data).show();

    });
    }
 
} 

  
function processD(){ 
var user =document.getElementById('user').value; 
$.trim(user);  
  $.post("sortpurchaseBsttus.php",
          {user:user},
          function(data){
      $('#dpl').html(data).show();

    });
}

 </script>