<?php
$cc=0;
$csh=0;
$mps=0;
$tgp=0;
$atlM=0;
$hlp=0;
$nmb=0;
$crdb=0;
$csh1=0;
$mps1=0;
$tgp1=0;
$atlM1=0;
$hlp1=0;
$nmb1=0;
$crdb1=0;
$tpayb=0;     
$tptd=0;
$tbal=0;
$tPrice=0;
$status="NOT PAID";
 
if(isset($_POST["sdate"]) &&  isset($_POST["dat1"]) && isset($_POST["dat2"]))
{
 $sdate=$_POST["sdate"];
 $dat1=$_POST['dat1'];   
 $dat2=$_POST['dat2'];
  require_once"labClass.php";
  $con=new labClass();
 
  ?>  
  
 
 <?php
  $rQ= $con->getSellRBydate($dat1,$dat2);
  if ($rQ->rowCount()>0) {
  $chp=$rQ->fetchAll();
 ?>
  <table class="table table-bordered table-striped">
  <thead class="bg-secondary">
      <th>  
        <small><h6>S/N</h6></small>
      </th>
      <th>  
        <small><h6>ITEM NAME</h6></small>
      </th>
      <th>  
        <small><h6>QUANTITY</h6></small>
      </th>
      <th>  
        <small><h6>RATE</h6></small>
      </th>
      <th>  
        <small><h6>S. PRICE</h6></small>
      </th>
      <!--<th>  
        <small><h6>PROFIT</h6></small>
      </th>-->
      <th>
        <small><h6>DATE.</h6></small>
      </th>
       <th>
        <small><h6>P. OPTION.</h6></small>
      </th>
      <th>
        <small><h6>TIME</h6></small>
      </th>
      <th>
        <small><h6>DICISSION</h6></small>
      </th>
    </thead>
  <tbody>
   
  <?php
 $tpprice=0;
 $tsprice=0;
 $tprofit=0;
 foreach ($chp as $key => $p) {
        $cc++;
        ?>
        
         
       <input type="hidden" name="itemD<?php echo $cc;?>" id="itemD<?php echo $cc;?>" value="<?php echo $p['itemID'];?>"/>
      <input type="hidden" name="recID<?php echo $cc;?>" id="recID<?php echo $cc;?>" value="<?php echo $p['recID'];?>"/>
        <input type="hidden" name="inam<?php echo $cc;?>" id="inam<?php echo $cc;?>" value="<?php echo $p['iname'];?>"/>
        <input type="hidden" name="tim<?php echo $cc;?>" id="tim<?php echo $cc;?>" value="<?php echo $p['timeAt'];?>"/>
        <input type="hidden" name="pOpt<?php echo $cc;?>" id="pOpt<?php echo $cc;?>" value="<?php echo $p['pOpt'];?>"/>
        
     
  
        
        
        
        
        
        
        
        
        
        
    <tr>
    <td>
        <?php echo $cc;?>
    </td>
    <td>
        <sup id="rdn<?php echo $cc;?>" ></sup>
      <?php echo $p['iname'];?>
    </td> 
    <td> 
    <?php  $p['eQty'];?> 
     <input type="number" name="qtyt<?php echo $cc;?>" id="qty<?php echo $cc;?>" value="<?php echo $p['eQty'];?>"/>
    </td>
    <td align="right"> 
      <?php 
      $tpprice=$tpprice+$p['ipprice']*$p['eQty'];
      echo  number_format($p['isprice'],2);
      ?> 
    </td> 
    <td align="right"> 
    <?php
     $tsprice=$tsprice+$p['isprice']*$p['eQty'];
     echo number_format($p['isprice']*$p['eQty'],2);?> 
    </td>
    <!--<td align="right">-->
      <?php
    // $tprofit=$tprofit+($p['isprice']*$p['eQty'])-($p['ipprice']*$p['eQty']);
     //echo number_format(($p['isprice']*$p['eQty'])-($p['ipprice']*$p['eQty']),2);
 
if ($p['pOpt']=="CASH") {
                   $csh=$csh+$p['isprice']*$p['eQty'];
                   }
 if ($p['pOpt']=="M-PESA") {
                   $mps=$mps+$p['isprice']*$p['eQty'];
                   } 
 if ($p['pOpt']=="TIGO PESA") {
                   $tgp=$tgp+$p['isprice']*$p['eQty'];
                   }
 if ($p['pOpt']=="AIRTEL MONEY") {
                   $atlM=$atlM+$p['isprice']*$p['eQty'];
                   }
if ($p['pOpt']=="HALO PESA") {
                   $hlp=$hlp+$p['isprice']*$p['eQty'];
                   } 
if ($p['pOpt']=="NMB") {
                   $nmb=$nmb+$p['isprice']*$p['eQty'];
                   }                                             
if ($p['pOpt']=="CRDB") {
                   $crdb=$crdb+$p['isprice']*$p['eQty'];
                   }  
 
     ?> 
    <!--</td>-->
    <td>
        <input type="date" name="ds<?php echo $cc;?>" id="ds<?php echo $cc;?>" value="<?php echo $p['entryD'];?>"/> 
     
    </td>
    
     <td> 
    <?php echo $p['timeAt'];?> 
    </td>
    <td>
        <select class="form-control" name="npOpt<?php echo $cc;?>" id="npOpt<?php echo $cc;?>">
    <option value="<?php echo $p['pOpt'];?>"><?php echo $p['pOpt'];?></option>
    <option value="CASH">CASH</option>
    <option value="M-PESA">M-PESA</option>
    <option value="AIRTEL MONEY">AIRTEL MONEY</option>
    <option value="TIGO PESA">TIGO PESA</option>
    <option value="HALO PESA">HALO PESA</option>
    <option value="NMB">NMB</option>
    <option value="CRDB">CRDB</option>  
    </select>  
    </td>
     <td> 
        <input type="submit" name="add<?php echo $cc;?>" id="add<?php echo $cc;?>" value="Click to return" id="submit" onclick=" var user=$('#user').val(); var iname=$('#inam<?php echo $cc;?>').val(); var tim=$('#tim<?php echo $cc;?>').val();var sdate=$('#sdate').val();  var ds=$('#ds<?php echo $cc;?>').val();var pOpt =$('#pOpt<?php echo $cc;?>').val(); var qty =$('#qty<?php echo $cc;?>').val();var recID =$('#recID<?php echo $cc;?>').val(); var itemID =$('#itemD<?php echo $cc;?>').val();document.getElementById('add<?php echo $cc;?>').disabled=true; if(itemID!=''&& recID!=''&& ds!='' && qty!=''&& pOpt!=''&& iname!=''&& user!='' ) {alert('You return item!..'); $.post('returnSellPro.php',{user:user,sdate:sdate,itemID:itemID,recID:recID,pOpt:pOpt,qty:qty,ds:ds,tim:tim,iname:iname},function(data){$('#rdn<?php echo $cc;?>').html(data).show();});} else{document.getElementById('rdn<?php echo $cc;?>').innerHTML='Valide item detail required!..';}" />
  </td>
   
  </tr>
<?php
}
  $cc=0;
?>
 
  

   
</tbody>

</table>






<div style="width:40%;">

<h6>SUMMARY OF THE REPORT:</h6>
 
          <table class="table" border="1">
            <tr>
            <td>
              TOTAL SALES:
              </td>
              <td align="right">
               <?php echo number_format($tsprice,2);?>  
              </td> 
            </tr>
          </table>
         <?php
          $chkp= $con->getThisCusDebitsBsysD($status,$sdate); 
          if ($chkp->rowCount()>0){
          $chp=$chkp->fetchAll();
          ?>
       <table class="table" border="1">
            <tr class="bg-secondary" >
    
                <td colspan="2">  
                  <small>CUSTOMER DEBITS:</small>
                  </td> 
                 </tr> 
                 <tr class="bg-secondary" >
    
                   <td>  
                     <small> CUSTOMER NAME</small>
                   </td> 
                   <td>  
                    <small>BALANCE</small>
                   </td>
     
                 </tr>
                <?php
               foreach ($chp as $key => $p) {
                $orderID=$p['orderID'];
                 $cp= $con->getThisInvItemList($orderID);
                 $cc++; 
                  $nr=$cp->rowCount();
                   ?>
 
                 <tr>
                 <td>  
                   <a href="#" onclick="var orno=document.getElementById('orno<?php echo $cc;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orno<?php echo $cc;?>"><?php echo $p['orderID'];?></sup></a><?php $parts = explode('(', $p['cname']); echo $parts[0];?> 
                 </td>
 
                 <td align="right">
                  <?php echo number_format($p['balance'],2);
                   $tbal=$tbal+$p['balance'];
                   ?>  
                  </td>
                  </tr>   
                  <?php      
                   }
                  $cc=0; 
                  ?>
                 <tr><td>Total:
                  </td>  
                   <td align="right">

                 <?php echo number_format($tbal,2);?> 
              </td> 
             </tr> 
           </table> 

           <?php
            }
             ?>
  <table width="5%" class="table">
    <tr>
    
    <td colspan="2">  
     <small>DEBIT PAYMENT:</small>
    </td>
               
   </tr>
     <tr>

        <td>  
          <small>C'S NAME</small>
         </td>
        <td>  
           <small>AMOUNT</small>
         </td>          
    </tr>
     <?php  
      $chkp= $con->getDebitsPrecBsysD($sdate); 
     
       if ($chkp->rowCount()>0) {

       $chp=$chkp->fetchAll();
  
        foreach ($chp as $key => $p) {
         $orderID=$p['orderID'];  
         $ch=$con->getDebitsPayListsBsysD($orderID,$sdate);
          if ($ch->rowCount()>0) {

           $h=$ch->fetchAll();
     
          foreach ($h as $key => $r) 
         {
          $tPrice=$tPrice+$r['paidAmount'];
          if ($r['pOpt']=="CASH") {
                   $csh1=$csh1+$r['paidAmount'];
                   }
          if ($r['pOpt']=="M-PESA") {
                   $mps1=$mps1+$r['paidAmount'];
                   } 
          if ($r['pOpt']=="TIGO PESA") {
                   $tgp1=$tgp1+$r['paidAmount'];
                   }
          if ($r['pOpt']=="AIRTEL MONEY") {
                   $atlM1=$atlM1+$r['paidAmount'];
                   }
          if ($r['pOpt']=="HALO PESA") {
                   $hlp1=$hlp1+$r['paidAmount'];
                   } 
          if ($r['pOpt']=="NMB") {
                   $nmb1=$nmb1+$r['paidAmount'];
                   }                                             
           if ($r['pOpt']=="CRDB") {
                   $crdb1=$crdb1+$r['paidAmount'];
                   } 
            } 
           ?>

            <tr>
            <td>
               <a href="#" onclick="var orno=document.getElementById('orno<?php echo $cc;?>').innerHTML; $.post('returnSellPro.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orno<?php echo $cc;?>"><?php echo $p['orderID'];?></sup></a><?php $parts = explode('(', $r['cname']); echo $parts[0];?>
            </td>
            <td align="right">
               <?php echo number_format($tPrice,2);?>  
            </td> 
        </tr>
 
     <?php
     $tptd=$tptd+$tPrice;
     $tPrice=0; 
      }       
}  
?>
<tr><td align="right">Total:</td><td align="right"><?php echo number_format($tptd,2);?> </td></tr>
</table>
<?php
}
?>
   
    EXPENSES REPORT 
   <?php  
      $chkp= $con->getExpensesRBsysD($sdate);   
        if ($chkp->rowCount()>0) {
        $chp=$chkp->fetchAll();
         ?>    
        <table class="table">
         <tr> 
          <td>
           <small>PURPOSE</small>
          </td>
          <td> 
          AMOUNT
          </td> 
         </tr>
        <?php
        $tex=0;
        $expID=0;
        foreach ($chp as $key => $p) {
        $expID=$p['expID'];
       
        ?>
        <tr>
        <td> 
          <?php echo $p['exReason'];?>
         </td>
          <td> 
          <?php $tex=$tex+$p['exAmount']; 
           echo $p['exAmount'];?>
         </td>    
        </tr>
     <?php
        }
       $cc=0;
       ?>
      <tr>
       <td>
             Total:
  
       </td>
        <td> 
             <?php echo $tex;?>

        </td>   
        </tr>  
     </table> 
      <?php
    }
?>
<table class="table" border="1">
<tr class="bg-secondary" >
    
    <td colspan="2">  
      <small>MEANS OF PAYMENT:</small>
    </td> 
</tr> 
<tr class="bg-secondary" >
    
    <td>  
      <small>MEANS</small>
    </td> 
    <td>  
      <small>AMOUNT</small>
    </td>
     
</tr>

 <tr>
 <td>  
   CASH
</td>
 
<td align="right">
   
    <?php echo number_format($csh+$csh1,2);?>

</td>
 </tr>   
 
<tr>
<td>
    M-PESA
</td>  
<td align="right">

 <?php echo number_format($mps+$mps1,2);?> 
</td> 
</tr>
<tr>
<td>
    AIRTEL MONEY
</td>  
<td align="right">

 <?php echo number_format($atlM+$atlM1,2);?> 
</td> 
</tr>
<tr>
<td>
    TIGO PESA
</td>  
<td align="right">
 <?php echo number_format($tgp+$tgp1,2);?> 
</td> 
</tr>
<tr>
<td>
    HALO PESA
</td>  
<td align="right">

  
 <?php echo number_format($hlp+$hlp1,2);?> 
</td> 
</tr>
<tr>
<td>
    NMB
</td>  
<td align="right">

 <?php echo number_format($nmb+$nmb1,2);?> 
</td> 
</tr>
<tr>
<td>
    CRDB
</td>  
<td align="right">

 <?php echo number_format($crdb+$crdb1,2);?> 
</td> 
</tr>

</table> 

<table class="table table-bordered">
<tr>
  <td>Cash in hand:</td><td><?php echo number_format(($tsprice+$csh1-$tbal-$tex-$mps-$mps1-$atlM-$atlM1-$tgp-$tgp1-$hlp-$hlp1-$nmb-$nmb1-$crdb-$crdb1),2);?> 
</td>
</tr>
</table>
SHORT SUMMARY:
<table class="table table-bordered">
<tr>
  <td>Total sales:</td><td align="right">
    <?php echo number_format($tsprice,2);?> 
</td>
</tr>
<tr>
 <td>Debit paid:</td>
<td align="right"> 
  <?php echo number_format(($tdbtP),2);?> 
</td>
<tr>
<tr>
 <td>Cash:</td>
<td align="right"> 
  <?php echo number_format(($tdbtP+$tsprice),2);?> 
</td>
<tr>
<tr>
  <td>Expenses:</td>
  <td align="right">
    <?php echo number_format($tex,2);?> 
</td>
</tr>
<tr>
  <td>Debits:</td>
  <td align="right"> 
    <?php echo number_format($tbal,2);?>
</td>
</tr>
<tr>
<td>Cash in hand:</td>
<td align="right"> 
  <?php echo number_format(($tsprice+$csh1-$tbal-$tex),2);?> 
</td>
</tr>
<!--<tr>
<td>Profit from sales:</td>
<td align="right"> 
  <?php// echo number_format(($tprofit-$tex),2);?> 
</td>
</tr>-->

 <td>Total cash in hand:</td>
<td align="right"> 
  <?php echo number_format(($tdbtP+$tsprice+$csh1-$tbal-$tex),2);?> 
</td>
</tr>
<tr>
  <td>Excess:</td><td> 
</td>
</tr>
</table>
</div>

<div>
   <button type="submit" class="btn btn-primary" value="Print" style="width:10%; height:30px;" onclick="printPageArea();">Print </button>
</div>
<?php
}
else{
?>
<small><h6 class="text-danger">No matching record!..</h6></small>
<?php
}
}
else{
?>
<small><h6 class="text-danger">Valide date required!..</h6></small>
<?php
}
?>
 

<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;
}

</script>