<?php
$cc=0;
if(isset($_POST["sdate"]) &&  isset($_POST["dat1"]) && isset($_POST["dat2"]))
{
 $sdate=$_POST["sdate"];
 $dat1=$_POST['dat1'];   
 $dat2=$_POST['dat2'];
  require_once"labClass.php";
$cc=0;
$csh=0;
$disc=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;
$tdbtP=0;
$c1=0;
$r2=0;
$status="NOT PAID";
  $con=new labClass();
 
  
  $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>P. PRICE</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>TIME</h6></small>
      </th>
    </thead>
  <tbody>
   
  <?php
 $tpprice=0;
 $tsprice=0;
 $tprofit=0;
 foreach ($chp as $key => $p) {
        $cc++;
        ?>
     <tr>
    <td>
        <?php echo $cc;?>
    </td>
    <td> 
      <?php echo $p['iname'];?>
    </td> 
    <td> 
    <?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> 
    <?php echo $p['entryD'];?> 
    </td>
    
     <td> 
    <?php echo $p['timeAt'];?> 
    </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->getInVRecBydt($dat1,$dat2); 
          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->getThisOrder($orderID);//getThisCusOrderRecBydate($status,$dat1,$dat2);
                 $c1++; 
                  $nr=$cp->rowCount();
                   ?>
 
                 <tr>
                 <td>  
                   <a href="#" onclick="var orno=document.getElementById('ono<?php echo $c1;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="ono<?php echo $c1;?>"><?php echo $p['orderID'];?></sup></a><?php $parts = explode('(', $p['cname']); echo $parts[0];?> 
                 </td>
 
                 <td align="right">
                  <?php echo number_format($p['payableA'],2);
                   $tbal=$tbal+$p['payableA'];
                   ?>  
                  </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>CURRENT DEBIT PAYMENT:</small>
    </td>
               
   </tr>
     <tr>

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

       $chp=$chkp->fetchAll();
  
        foreach ($chp as $key => $p) {
         $orderID=$p['orderID'];
        // $c= $con->getThisOrder($orderID)->fetch();
         //$cname=$p['cname'];
         $ch=$con->getDebitsPrecBcCdtID($orderID,$dat1,$dat2);
          if ($ch->rowCount()>0) {

           $h=$ch->fetchAll();
     
          foreach ($h as $key => $r) 
         {
              $r2++; 
          //$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'];
                   } 
                     //$tdbtP=$tdbtP+$r['paidAmount'];
                     
                if ($r['pOpt']=="DISCOUNT") {
                  
                   $disc=$disc+$r['paidAmount'];
                   } 
                 else{
                  $tPrice=$tPrice+$r['paidAmount'];
                   $tdbtP=$tdbtP+$r['paidAmount'];
                   }
            } 
          if ($tPrice<=0) {
                  
                   continue;
                   } 
                 else{
           ?>

            <tr>
            <td>
               <a href="#" onclick="var orno=document.getElementById('orn<?php echo $r2;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orn<?php echo $r2;?>"><?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
}
?>





  <small><h6>OLD DEBIT PAYMENT:</h6></small>
<table width="5%" class="table">
   
     <tr>

        <td>  
          <small>C'S NAME</small>
         </td>
        <td>  
           <small>AMOUNT</small>
         </td>          
    </tr>
 <?php 
$cc=0;
//$sdate= $_POST['sdate'];   
$tptdo=0;
$tbalo=0;
$tPriceo=0;
$tdbtPo=0;

$t1=0;
$q2=0;
$status="NOT PAID";
     
     //echo $sdate;
      $chkp= $con->getNonDebitsPrecBdt($dat1,$dat2); 
     //print_r($chkp);
       if ($chkp->rowCount()>0) {

       $chp=$chkp->fetchAll();
  
        foreach ($chp as $key => $p) {
             $r2++;
         $orderID=$p['orderID'];  
          ?>

            <tr>
            <td>
                <?php echo $p['orderID'].".". $p['cname'];?>
            </td>
            <td align="right">
               <?php echo number_format($p['paidAmount'],2);?>  
            </td> 
           </tr>
           <?php
             $tptdo=$tptdo+$p['paidAmount'];
                                     }       
           
       }
?>
<tr><td align="right">Total:</td><td align="right"><?php echo number_format($tptdo,2);?> </td></tr>
</table>



<small><h6>CUSTOMER DEPOSIT:</h6></small>
<table width="5%" class="table">
     <tr>
        <td>  
          <small>C'S NAME</small>
         </td>
        <td>  
           <small>AMOUNT</small>
         </td>          
    </tr>
 <?php 
$cc=0;    
$tdp=0;
$chkp= $con->getToDayCusDepoByDrange($dat1,$dat2); 
    if ($chkp->rowCount()>0) {

       $chp=$chkp->fetchAll();
  
        foreach ($chp as $key => $p) {
             $r2++;   
          ?>
            <tr>
            <td>
                <?php echo $p['recID'].".". $p['cname'];?>
            </td>
            <td align="right">
               <?php echo number_format($p['depoA'],2);?>  
            </td> 
           </tr>
           <?php
             $tdp=$tdp+$p['depoA'];
                                     }       
           
       }
?>
<tr><td align="right">Total:</td><td align="right"><?php echo number_format($tdp,2);?> </td></tr>
</table>

 <small><h6>SALES[DEDUCTION] FROM DEPOSIT:</h6></small>
<table width="5%" class="table">
     <tr>
        <td>  
          <small>C'S NAME</small>
         </td>
        <td>  
           <small>AMOUNT</small>
         </td>          
    </tr>
 <?php 
$cc=0;    
$tsdp=0;
$chkp= $con->getToDayCusDepoWthDrwBdRange($dat1,$dat2); 
    if ($chkp->rowCount()>0) {

       $chp=$chkp->fetchAll();
  
        foreach ($chp as $key => $p) {
             $r2++;   
          ?>
            <tr>
            <td>
                <?php echo $p['recID'].".". $p['cname'];?>
            </td>
            <td align="right">
               <?php echo number_format($p['depoA'],2);?>  
            </td> 
           </tr>
           <?php
             $tsdp=$tsdp+$p['depoA'];
                                     }       
           
       }
?>
<tr><td align="right">Total:</td><td align="right"><?php echo number_format($tsdp,2);?> </td></tr>
</table>




   
    EXPENSES REPORT 
   <?php  
      $chkp= $con->getPharmExRBydate($dat1,$dat2);   
        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
    }
?>
 
<?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-$tbal-$tex-$mps-$mps1-$atlM-$atlM1-$tgp-$tgp1-$hlp-$hlp1-$nmb-$nmb1-$crdb-$crdb1),2);?> 
</td>
</tr>
</table>
*/
?>
   <p/>TRANSACTION
   <?php  
      $chkp= $con->getTransacRBSortDt($dat1,$dat2);   
        if ($chkp->rowCount()>0) {
        $chp=$chkp->fetchAll();
         ?>    
        <table class="table">
         <tr> 
          <td>
           <small>TRANSACTION</small>
          </td>
          <td> 
          AMOUNT
          </td> 
         </tr>
        <?php
        $totalT=0;
        //echo "eett";
        foreach ($chp as $key => $p) {
        $recID=$p['recID'];
       
        ?>
        <tr>
        <td> 
          <?php echo $p['transac'];?>
         </td>
          <td> 
          <?php $totalT=$totalT+$p['amount']; 
           echo $p['amount'];?>
         </td>    
        </tr>
     <?php
        }
       $cc=0;
       ?>
      <tr>
       <td>
             Total:
  
       </td>
        <td> 
             <?php echo $totalT;?>

        </td>   
        </tr>  
     </table> 
      <?php
    }
?>
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+$tptdo),2);?> 
</td>
</tr>
<tr>
 <td>Customer deposit:</td>
<td align="right"> 
  <?php echo number_format(($tdp),2);?> 
</td>
</tr>
<tr>
 <td>Cash:</td>
<td align="right"> 
  <?php echo number_format(($tdbtP+$tsprice+$tptdo+$tdp),2);?> 
</td>
</tr>
<tr>
 <td>Sales[deduction] from deposit:</td>
<td align="right"> 
  <?php echo number_format(($tsdp),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+$tdbtP+$tptdo+$tdp-$tsdp-$tbal-$tex),2);?> 
</td>
</tr>
<tr>
  <td>Excess:</td><td align="right">
      <?php
      $b=0;
      
      $Qr=$con->getExcessmoneyRDTrange($dat1,$dat2);
      
      if($Qr->rowCount()>0){
          $r=$Qr->fetchAll(PDO::FETCH_ASSOC);
          foreach ($r as $d): 
          $b=$b+$d['amount'];
         endforeach;
        
      echo number_format(($b),2);
      }
      ?> 
</td>
</tr>
<tr>
  <td>Discount:</td><td align="right">
      <?php
       
      echo number_format(($disc),2);
      
      ?> 
</td>
</tr>
<tr>
 <td>Total cash in hand:</td>
<td align="right"> 
  <?php 
 //echo $tptdo;
  echo number_format(($tdbtP+$tptdo+$tsprice+$b+$tdp-$tsdp-$tbal-$tex),2);?> 
</td>
</tr>
 
 <tr>
<td>Profit from sales:</td>
<td align="right"> 
  <?php echo number_format(($tprofit-$tex),2);?> 
</td>
</tr>
</table>
 
  
 
 
 
 
</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
}
?>
</div>
 
</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;
}

</script>