<?php
//ini_set('display_errors', 1);
     //ini_set('display_startup_errors', 1);
    //error_reporting(E_ALL);
$disc=0;
$tex=0;
$expID=0;
$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;
$tdbtP=0;
$c2=0;
$c1=0;
$r2=0;
$status="NOT PAID";
 
if(isset($_POST["sdate"]))
{
  $sdate=$_POST["sdate"];
  require_once"labClass.php";
  $con=new labClass();
 
  ?>  
  <div class="container-fluid" id="co"> 
  <small><h6>SELLS REPORTS:</h6></small>
  <table class="table"><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('pharmSellsRByDate.php',{sdate:sdate,dat1:dat1,dat2:dat2},function(data){$('#dpl').html(data).show();});">Generate</button>
 </td>  
 <td><button type="submit" class="btn btn-primary" value="Print" style="width:30%; height:40px;" onclick="printPageArea();">Print </button></td>
 </tr>
 </table>

 <div class="container-fluid " id="dpl">
 <?php
  $rQ= $con->getSellsR($sdate);
  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>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>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['ipprice']*$p['eQty'],2);
      ?>
    <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->getThisDateOrder($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);
                 $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->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) 
         {
              $c2++; 
          
          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'];
                   } 
            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 $c2;?>').innerHTML; $.post('pharmInvDetail.php',{orno:orno},function(data){$('#dpl').html(data).show();});"><sup id="orn<?php echo $c2;?>"><?php echo $r['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>













  
  <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->getDebitsNonPrecBsysD($sdate); 
     //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>
 

 
<?php
}
?>
<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->getToDayCusDepo($sdate); 
    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->getToDayCusDepoWthDrw($sdate); 
    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->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
    }
?>
<?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><?php
*/ ?>


    <p/>TRANSACTION
   <?php  
      $chkp= $con->getTransacRBsysD($sdate);   
        if ($chkp->rowCount()>0) {
        $chp=$chkp->fetchAll();
         ?>    
        <table class="table">
         <tr> 
          <td>
           <small>TRANSACTION</small>
          </td>
          <td> 
          AMOUNT
          </td> 
         </tr>
        <?php
        $totalT=0;
        
        foreach ($chp as $key => $p) {
        $recID=$p['recID'];
       
        ?>
        <tr>
        <td> 
          <?php echo $p['transac'];?>
         </td>
          <td> 
             <?php $totalT=$totalT+$p['amount'];
          
           
           if($p['transac']=="CASH" && $p['statusB']=="Saved"){
               ?>
               
               <sup id="csh"><?php echo $p['amount'];?></sup> 
               <input type="text" name="scsh" id="scsh" style="border-radius:12px;border-color:blue;width:35%; height:40px;display:none;" value=""/>
               <button type="submit" id="cos" class="btn btn-primary" style="width:35%; height:40px;" onclick=" var sdate= $('#sdate').val();$('#scsh').show();$('#sv').show(); $('#cos').hide();"><small>Confirm</small></button>
               <button type="submit" id="sv" class="btn btn-secondary" style="width:25%; height:40px; display:none;" onclick="var ccash= document.getElementById('csh').innerHTML; var cash= $('#scsh').val();var sdate= $('#sdate').val();if(parseFloat(ccash)==parseFloat(cash)){$.post('confirmCsh.php',{sdate:sdate},function(data){$('#shw').html(data).show();});$('#sv').hide();}else{document.getElementById('shw').innerHTML='Invalid value!..';}"><small>Save</small></button>
               
               <sup id="shw"></sup>
               <?php
               
           }else if($p['transac']=="CASH" && $p['statusB']!="Saved"){
                
                  ?>
               <sup id="cshr"><?php echo $p['amount'];?></sup> 
               <input type="text" name="scshr" id="scshr" style="border-radius:12px;border-color:blue;width:35%; height:40px;display:none;" value="<?php echo $p['amount'];?>"/>
               
               <button type="submit" id="svr" class="btn btn-secondary" style="width:55%; height:40px;" onclick="var ccash= document.getElementById('cshr').innerHTML; var cash= $('#scshr').val();var sdate= $('#sdate').val();if(parseFloat(ccash)==parseFloat(cash)){$.post('undo_confirm.php',{sdate:sdate},function(data){$('#shw').html(data).show();});$('#svr').hide();}else{document.getElementById('shw').innerHTML='Invalid value!..';}"><small>Undo confirm</small></button>
               
               <sup id="shw"></sup>
               <?php
           } 
            else{
                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->getExcessmoneyRbdt($sdate);
      if($Qr->rowCount()>0){
        $r=$Qr->fetch();
        $b=$r['amount'];
      echo number_format(($r['amount']),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 number_format(($tdbtP+$tsprice+$tptdo+$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>