<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='off_rebeatcl.php';   
$cuPage='off_rebeatcl.php';    
$aid=$_SESSION['uid'];
$uty=$_SESSION['utype'];    
$brid=$_SESSION['abrid'];     
}else{
header('Location:../index.php');
exit;    
}
$mhead='offer';
$menuh='Set Offer &amp; Rebeat';
$phead='rbclim';
$page='Add Rebeat Claim';
$dtnow = date("Y-m-d h:i:s", time());
$today = strftime("%Y-%m-%d", time());
?>
<?php
if(isset($_POST['supid']) && $_POST['supid']!=''){
$supid=$_POST['supid'];	
}else{
$supid=0;	
}

if(isset($_POST['tfdate'])){
$fdate=$_POST['tfdate'];    		
}else{
$fdate=date('Y-m-d', strtotime('-15 days', time()));	
}

if(isset($_POST['ttdate'])){
$tdate=$_POST['ttdate'];	
}else{
$tdate=$today;	
}

?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-3">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">Filter</h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<form action="off_rebeatcl.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">    
<div class="col-md-12 popup_details_div">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Supplier</label>   
<select class="form-control select2" name="supid" id="supid">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT DISTINCT tid AS id FROM tbl_traproduct WHERE mods='PU' AND (rbp>0 OR rbf>0)")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$supid){?>
<option selected value="<?php echo $rows['id'];?>"><?php echo get_fild_data('tbl_supplier',$rows['id'],'name');?></option>    
<?php }else{ ?>    
<option value="<?php echo $rows['id'];?>"><?php echo get_fild_data('tbl_supplier',$rows['id'],'name');?></option>
<?php } ?>
<?php } ?>    
</select> 
</div>    
</div>    
</div>           
<div class="row">
<div class="col-md-6">    
<div class="form-group" >
<label>From:</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
<input type="text" maxlength="18" class="form-control datetimepicker" value="<?php echo $fdate; ?>" name="tfdate" id="stdate" placeholder="From Date" autocomplete="off" readonly>
</div>
</div>
</div>
<div class="col-md-6">    
<div class="form-group" >
<label>To:</label>
<div class="input-group">
<span class="input-group-addon"><span class="fa fa-calendar"></span></span>
<input type="text" maxlength="18" class="form-control datetimepicker" value="<?php echo $tdate;?>" name="ttdate" id="endate" placeholder="End Date" autocomplete="off" readonly>
</div>
</div>
</div>
</div>    
</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-6"></div>
<div class="col-md-6 text-right" >
<input type="button" id="reset" class="btn btn-flat bg-red btn-sm " value="Reset"/>    
<input type="submit" name="filter_submit" id="submit" class="btn btn-flat bg-purple btn-sm " value="Submit"/>
</div> 
</div>
</form>    
</div>
</div>
</div>
</div>    
</div>    
<div class="col-md-9">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Rebeat Claim Item</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>    
<div class="col-md-12 table-responsive">
<div class="row">
<div class="col-md-4">
Supplier: <?php if($supid!=0){echo get_fild_data('tbl_supplier',$supid,'name');} ?>
</div>
<div class="col-md-4">
From: <?php if(isset($_POST['tfdate'])){echo $_POST['tfdate']; }else{echo date('Y-m-d', strtotime('-15 days', time()));} ?>
</div>
<div class="col-md-4">
To: <?php if(isset($_POST['ttdate'])){echo $_POST['ttdate']; }else{echo $today;} ?>
</div>
</div>
<div class="secdiv"><span>Item Details</span></div>
<br>    
<div class="row">     
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>Invoice</th>
<th>Customer</th>
<th>Quantity</th>
<th>Rebeat</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php

function get_rebqty($unqid){
global $con;
$qty=0;
$sql="SELECT COALESCE((SELECT avpqty FROM tbl_brstock WHERE unqid='$unqid'),0) AS avqty";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
$brow=mysqli_fetch_array($query);
    
$wsql="SELECT COALESCE((SELECT avqty FROM tbl_whstock WHERE unqid='$unqid'),0) AS avqty";
$wquery=mysqli_query($con,$wsql)or die(mysqli_error($con));
$wrow=mysqli_fetch_array($wquery);
$qty=$brow['avqty']+$wrow['avqty'];
    
return $qty;    
}    
        
function get_rebeatamo($supid,$reamo=''){
global $con;
$rec=0;
$trebamo=0;    
$sql="SELECT unqid,rbf FROM tbl_traproduct WHERE mods='PU' AND tid='$supid' AND rbf>0";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){   
$trebamo += (get_rebqty($row['unqid'])*$row['rbf']);
}
if($reamo!=''){
$rec = 	number_format((float)(($reamo/$trebamo)*100), 2, '.', '');
}else{
$rec=$trebamo;	
}    
return $rec;    
}
$total=0;
$name='';
$sql="SELECT reb.id,reb.pid,reb.colid,reb.sizid,reb.invno,reb.unqid,reb.cusid,reb.sqty,reb.rbf,reb.rbamo,sup.supid,req.srqty,pre.prqty FROM (SELECT id,pid,invno,unqid,colid,sizid,tid AS cusid,s_qty AS sqty,rbf,rbamo FROM tbl_traproduct WHERE mods='SE' AND rbamo>0 AND rbclno IS NULL AND apdate BETWEEN '$fdate' AND '$tdate') reb LEFT JOIN (SELECT unqid,tid AS supid FROM tbl_traproduct WHERE mods='PU' AND rbf>0) sup ON sup.unqid=reb.unqid LEFT JOIN (SELECT unqid,p_in AS srqty FROM tbl_traproduct WHERE mods='SR') req ON req.unqid=reb.unqid LEFT JOIN (SELECT unqid,p_out AS prqty FROM tbl_traproduct WHERE mods='PR') pre ON pre.unqid=reb.unqid WHERE sup.supid='$supid'   
ORDER BY reb.id ASC";
   
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$name=get_fild_data('tbl_item',$row['pid'],'name');
if($row['colid']!='0'){$name.=' - '.get_fild_data('tbl_color',$row['colid'],'name');}
if($row['sizid']!='0'){$name.=' - '.get_fild_data('tbl_size',$row['sizid'],'sval');}	
$total+=$row['rbamo'];    
?>    
<tr>
<td class="center"><?php echo count_id();?></td>
<td><?php echo $name; ?></td>
<td><?php echo $row['invno']; ?></td>
<td><?php echo get_fild_data('tbl_customer',$row['cusid'],'name'); ?></td>
<td><?php echo $row['sqty']; ?></td>
<td><?php echo numtolocal($row['rbf'],''); ?></td>
<td><?php echo numtolocal(($row['rbamo']*$row['sqty']),''); ?></td>
</tr>   
<?php } ?>
<tr>
<td colspan="5">
<strong>Total</strong>
<input type="hidden" id="supid" value="<?php echo $supid.'_'.get_fild_data('tbl_supplier',$supid,'name');?>" readonly>
<input type="hidden" id="dudate" value="<?php echo $fdate.'_'.$tdate;?>" readonly>
<input type="hidden" id="rebtotal" value="<?php echo $total;?>" readonly>
<input type="hidden" id="rebt" value="<?php echo get_rebeatamo($supid,$total);?>" readonly>
</td>
<td colspan="2"><strong><?php echo numtolocal($total,'').'</strong>  (<span style="color:red;">'.get_rebeatamo($supid,$total).'%</span>)';?></strong></td>
</tr>    
</tbody>    
</table>    
</div>    
</div>
<div class="clearfix" ></div>  
<div class="row"style="margin-top: 0px" >
<div class="col-md-12 table-responsive">    
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<a href="#" class="btn btn-flat bg-purple" id="addrcla">Save Claim</a>
</div>
</div>    
</div>    
</div>
</div>
</div>
</div>    
<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->
<?php include('../layout/rside.php'); ?>    
</div>
<?php
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).on('click', '#addrcla', function(e) {
$('#addsitem').html('');
$('.right-side-add').toggle('slide', { direction: 'right' }, 300);        
e.preventDefault();    
});

$(document).on('click', '#closepop', function() {
if($('.right-side-add').is(':visible')) {
$('.right-side-add').toggle('slide', { direction: 'right' }, 300); 
$('#addsitem').html('');
}   
});    
</script>    
<!-- /page script -->
</html>    