<?php
include("../../pidbco/pidbjoin.php");

$comp  = strip_tags(base64_decode($_GET['company']));
$bran  = strip_tags(base64_decode($_GET['branch']));
$start = strip_tags(base64_decode($_GET['start']));
$end   = strip_tags(base64_decode($_GET['end']));

$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval  = $datetime1->diff($datetime2);
$days_between = $interval->format('%a');

$select1 = mysqli_query($con, "SELECT oid FROM `p_orders` WHERE `ocomp`='$comp' AND `obranch`='$bran' AND `ordr_date` BETWEEN '$start' AND '$end'");
$rowcount1=mysqli_num_rows($select1);
if($rowcount1 != 0){

$totalrows = $rowcount1;
$limitcount = $totalrows/30;

$exp =explode(".",$limitcount);
$roundvar = $exp[1];
if($roundvar != 0){
$limitcount= $limitcount+1;	
}

 $x=0;
 $j=0;

 ?>
  
   <style>
   	@media print {
   thead {display: table-header-group;}
}
   </style>	


  <body style="margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:12px;">
  	     
     <div id="invoice" style="width:750px; height:1010px; margin:0px auto; background-color:#FFFFFF; padding:25px 0px;">
    
    
            <div style=" width:700px; height:1100px; margin:0px auto;">

               <div style="text-align:right;margin-top: -25px;  " >
                <img src="./invoice/images/logo.jpg" alt="logo" />
               </div>

            <div style="text-align:left; margin-top:25px;font-weight:bold; font-size:20px;">
             <?php 
             $newDate1 = date("d M Y", strtotime($start));
             $newDate2 = date("d M Y", strtotime($end));
             echo 'Sales Register of  '.$newDate1.' to '.$newDate2;
             ?>
         </div>

         <br />

         <div style="width:100%; height:390px;">

            <table style="width:100%; font-size:11px; border:none; ">
 <thead>
                <tr style="text-align:left; background-color:#CCCCCC;">

                    <th style="width:4%; border:solid 1px #999999;">
                        No. 
                    </th>
                    <th style="width:14%; border:solid 1px #999999;">
                        Date
                    </th>
                     <th style=" border:solid 1px #999999;">
                        Customer
                    </th>
                    <th style="border:solid 1px #999999;">
                        Purticulares    
                    </th>
                    <th style="width:15%; border:solid 1px #999999;">
                        Amount
                    </th>

                      
                             
                </tr>
     </thead>  
<?php
$rowcount1 ='';
for(;$j<=$days_between;$j++){
 
  	
if($comp != ''){ 
$s1 = " `ocomp`='$comp'";
}
if($bran !=''){
 $s2 = " AND `obranch`='$bran'";
} 
if($start != '' && $end != ''){
 $s3 = " AND `ordr_date` ='$start' ";
}

 
 
 $xx  = $x;
 

 $sqq = mysqli_query($con, "SELECT * FROM `p_orders` WHERE ".$s1.$s2.$s3);
 $p=1;
 $rowcount = mysqli_num_rows($sqq);
 $rowcount1 = $rowcount1+$rowcount;
 while($res = mysqli_fetch_array($sqq)){
 	 $ocustm = $res['ocustm'];
  $sq5 = mysqli_query($con, "SELECT cname FROM `p_customers` WHERE `cid`='$ocustm'");
  $rs5 = mysqli_fetch_array($sq5);
 	?>
        <tr style="line-height:20px;">

        <td style="text-align:left; border:solid 1px #999999; border-top:none;">
       <?php echo $res['order_id']; ?>
        </td>

        <td style="padding-left:5px; border:solid 1px #999999; border-top:none;">
        <?php 
         $originalDate = $res['ordr_date'];
         $newDate = date("d M Y", strtotime($originalDate));
         echo $newDate;
         ?>
        </td>
       <td style="text-align:left; padding-left:3px; border:solid 1px #999999; border-top:none;">
       	<?php echo $rs5['cname']; ?>
       	</td>
        <td style="text-align:left; border:solid 1px #999999; border-top:none;padding-left:3px; ">
        <?php echo $res['ordr_type']; ?>
        </td>

        <td style="text-align:left; padding-right:5px; border:solid 1px #999999; border-top:none;">
        <?php echo $res['net_amnt']; ?>
        </td>

    </tr>
    
    <?php 
      } 
$start = date('Y/m/d', strtotime($start. ' + 1 days'));	
      }
?>
    
    
<?php 

$totpage = $rowcount1/30;
$whole = floor($totpage);      
$fraction = $totpage - $whole; 
if($fraction != ''){
	$val = 1;
}

$totpages = $whole+$val;

$totnum = $totpages*30;



   $loopcount = $totnum-$rowcount1;
   
   for($k=1;$k<=$loopcount;$k++){ ?>  
<tr style="line-height:20px;">
        <td style="text-align:left; border:solid 1px #999999; border-top:none;">&nbsp;
        </td>

        <td style="padding-left:5px; border:solid 1px #999999; border-top:none;">&nbsp;
        </td>

        <td style="text-align:left; border:solid 1px #999999; border-top:none;">&nbsp;
        </td>

        <td style="text-align:left; padding-right:5px; border:solid 1px #999999; border-top:none;">&nbsp;
        </td>

        <td style="text-align:left; padding-right:5px; border:solid 1px #999999; border-top:none;">&nbsp;
        </td>
</tr>
<?php }  ?>    
 
 
 
 <?php
$comp = strip_tags(base64_decode($_GET['company']));
$bran = strip_tags(base64_decode($_GET['branch']));
$start  = strip_tags(base64_decode($_GET['start']));
$end = strip_tags(base64_decode($_GET['end']));


if($comp != ''){ 
 $s1 = " `ocomp`='$comp'";
}
if($bran !=''){
 $s2 = " AND `obranch`='$bran'";
} 
if($start != '' && $end != ''){
 $s3 = " AND `ordr_date` BETWEEN '$start' AND '$end' ";
}

 $sqq = mysqli_query($con, "SELECT SUM(`net_amnt`) AS recamnt FROM `p_orders` WHERE ".$s1.$s2.$s3);
 $p=1;
 $res = mysqli_fetch_array($sqq);

 


?>

   <tr style="font-weight:bold; height:30px; width:100%; border:none; font-size:14px;margin-bottom:0px;">
    <td style="text-align:right; width:4%;border:solid 1px #999999;border-top:none;">

    </td>
    <td style="text-align:right; width:14%;border:solid 1px #999999;border-top:none;">

    </td>
    <td style="text-align:right; border:solid 1px #999999;border-top:none;">

    </td>
    <td style="text-align:right;border:solid 1px #999999;border-top:none; font-weight: bold;">
        Total &nbsp;
    </td>
     <td style="text-align:left; width:15%;border:solid 1px #999999;border-top:none;">
        <?php 
        echo number_format($res['recamnt'],2);
        ?>
    </td>
    
</tr>
  
   
   
</table>


</div>

<br />
<br />


</div>

</div>

</body>
<?php }else{echo "data not found";} ?>
