<?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']));
$s11=1;
$s22=1;
$s33=1;
$s1=1;
$s2=1;
$s3=1;  

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

$select1 = mysqli_query($con, "SELECT rid FROM `p_receipts` WHERE `rcomp`='$comp' AND `rbran`='$bran' AND `rdate` BETWEEN '$start' AND '$end'");
$rowcount1=mysqli_num_rows($select1);

$select2 = mysqli_query($con, "SELECT vid FROM `p_voucher` WHERE `vcomp`='$comp' AND `vbranch`='$bran' AND `vdate` BETWEEN '$start' AND '$end'");
$rowcount2=mysqli_num_rows($select2);

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


    if($rowcount1 <= 15 && $rowcount2 <= 15){
        $y=15;
        $y1=$y;
    }
    if($rowcount1 > 15 && $rowcount2 > 15){
        $y=$rowcount1;
        $y1=$rowcount2;
    }
    if($rowcount1 <= 15 && $rowcount2 > 15){
        $y=15;
        $y1=$rowcount2;
    }
    if($rowcount1 > 15 && $rowcount2 <= 15){
       // $y=$rowcount1;
       //  $y1=15;
        $y=30-$rowcount2;
		$y1=15;
    }
	
 $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 'Journal 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:8%; border:solid 1px #999999;">
                         No. 
                    </th>
                    <th style="width:14%; border:solid 1px #999999;">
                        Date
                    </th>
                    <th style="border:solid 1px #999999;">
                        Purticulares    
                    </th>
                    <th style="width:15%; border:solid 1px #999999;">
                        Credit
                    </th>

                    <th style="width:15%; border:solid 1px #999999;">
                        Debit
                    </th>     
                             
                </tr>
     </thead>  
<?php
//for($n=1;$n<=30;$n++){

/* $datetime1 = new DateTime($start);
   $datetime2 = new DateTime($end);
   $interval = $datetime1->diff($datetime2);
   $days_between = $interval->format('%a');  */
$rowcount1 = '';
$rowcount2 = '';
for(;$j<=$days_between;$j++){
 
  	
if($comp != ''){ 
$s1 = " `rcomp`='$comp'";
}
if($bran !=''){
 $s2 = " AND `rbran`='$bran'";
} 
if($start != '' && $end != ''){
 $s3 = " AND `rdate` ='$start' ";
}

 
 
 $xx  = $x;
 

 $sqq = mysqli_query($con, "SELECT * FROM `p_receipts` WHERE ".$s1.$s2.$s3);
 $p=1;
 $rowcount11 = mysqli_num_rows($sqq);
 $rowcount1 = $rowcount1 + $rowcount11;
 while($res = mysqli_fetch_array($sqq)){
 	?>
        <tr style="line-height:20px;">

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

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

        <td style="text-align:left; border:solid 1px #999999; border-top:none; padding-left: 5px;">
<?php 
        $cusid = $res['rcust'];
        $sq2 = mysqli_query($con,"SELECT cname FROM `p_customers` WHERE `cid`='$cusid' ");
        $rs2 = mysqli_fetch_array($sq2);
        echo $rs2['cname'];
         ?>
        </td>

        <td style="text-align:left; padding-left:5px; border:solid 1px #999999; border-top:none;">
<?php  echo number_format($res['ramount'],2);?>
        </td>

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

        </td>
        
    </tr>
    
    <?php 
  
}  

 
if($comp != ''){ 
$s11 = " `vcomp`='$comp'";
}
if($bran !=''){
 $s22 = " AND `vbranch`='$bran'";
} 
if($start != '' && $end != ''){
 $s33 = " AND `vdate`='$start' ";
}
 
$sqq2 = mysqli_query($con, "SELECT * FROM `p_voucher` WHERE ".$s11.$s22.$s33);
$rowcount22 = mysqli_num_rows($sqq2);
$rowcount2 = $rowcount2 + $rowcount22;
while($res22 = mysqli_fetch_array($sqq2)){
?>	
	
  <tr style="line-height:20px;">

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

        <td style="padding-left:5px; border:solid 1px #999999; border-top:none;">
        <?php 
         $originalDate = $res22['vdate'];
         $newDate = date("d M Y", strtotime($originalDate));
         echo $newDate;
         ?>
        </td>

        <td style="text-align:left; border:solid 1px #999999; border-top:none;padding-left: 5px;">
<?php 
    $vvendr = $res22['vvendr'];
    $sq2 = mysqli_query($con,"SELECT v_company	FROM `p_vendor` WHERE `vid`='$vvendr' ");
    $rs2 = mysqli_fetch_array($sq2);
    echo $rs2['v_company'];
         ?>
        </td>

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

        <td style="text-align:left; padding-right:5px; border:solid 1px #999999; border-top:none;">
          <?php echo number_format($res22['vamnt'],2);?>
        </td>
        
    </tr>	
	
<?php  } 	
$start = date('Y/m/d', strtotime($start. ' + 1 days'));	


}


?>
    
<?php 
$tot_count = $rowcount1 + $rowcount2;
$totpage = $tot_count/30;
$whole = floor($totpage);      
$fraction = $totpage - $whole; 
if($fraction != ''){
	$val = 1;
}
$totpages = $whole+$val;
$totnum   = $totpages*30;


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

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

        <td style="text-align:right; 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 = " `rcomp`='$comp'";
}

if($bran !=''){
 $s2 = " AND `rbran`='$bran'";
} 
 
if($start != '' && $end != ''){
 $s3 = " AND `rdate` BETWEEN '$start' AND '$end' ";
}

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

 
 
if($comp != ''){ 
$s11 = " `vcomp`='$comp'";
}
if($bran !=''){
 $s22 = " AND `vbranch`='$bran'";
} 
if($start != '' && $end != ''){
 $s33 = " AND `vdate` BETWEEN '$start' AND '$end' ";
}
$sqq2 = mysqli_query($con, "SELECT SUM(`vamnt`) AS voamnt FROM `p_voucher` WHERE ".$s11.$s22.$s33);
$res22 = mysqli_fetch_array($sqq2);

?>

   <tr style="font-weight:bold; height:30px; width:100%; border:none; font-size:14px;margin-bottom:0px;">
    <td style="text-align:right; width:8%;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; 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>
    <td style="text-align:left; width:15%;border:solid 1px #999999;border-top:none;">
       
         <?php 
        echo number_format($res22['voamnt'],2);
        ?>
    </td>
</tr>
  
   
   
</table>
</div><br /><br /></div></div>
</body>