<?php
require "racs.php";
require "vtshowsBBB.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions']) || !isset($_SESSION['usernames'])){
header("location:index.php");}
?>
<html>
<title>DB-Add-Payment</title>
<head>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script src="datesps.js"></script>
<link rel="stylesheet" type="text/css" href="vtxstyles.css">

<style>
tr:hover.clss{background-color:silver;}
</style>
<script>

function scopys(pr)
{
var dts=document.getElementById("dt");
var sels=window.getSelection();
sels.removeAllRanges();
var ranges=document.createRange();
ranges.selectNodeContents(dts);
sels.addRange(ranges);
document.execCommand("copy");
alert("Copied - successfull");
sels.removeAllRanges();
}


function srgs(pr)
{
var sv=document.getElementById('srctxt').value.toLowerCase();
var dt=document.getElementById('dt');
var rn=dt.rows.length;
for(var cnt=1;cnt<rn-1;cnt++){
tvl=dt.rows[cnt].cells[1].innerText.toLowerCase();
rvl=dt.rows[cnt].cells[2].innerText.toLowerCase();
svl=dt.rows[cnt].cells[3].innerText.toLowerCase();

if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.indexOf(sv)>=0 || svl.indexOf(sv)>=0)){
dt.rows[cnt].style.display='table-row';}
else{
dt.rows[cnt].style.display='none';}
if(sv.length<=1){
dt.rows[cnt].style.display='table-row';}	
}
}
</script>
</head>

<body style='font-family:calibri;font-size:small;'>
<?php

$flt=racs(1);

if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\nContanct Sales and Marketing division</div>");
}
else{
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");
if(isset($_REQUEST['starts'])){
//echo "starts ok";	
$starts=$_REQUEST['starts'];}
else{
$starts=date('Y-m-01');}

if(isset($_REQUEST['ends'])){
$ends=$_REQUEST['ends'];}
else{
$ends=date('Y-m-t');}

if(isset($_REQUEST['zones'])){
$zones=$_REQUEST['zones'];}
else{
$zones='all';}

if(isset($_REQUEST['regions'])){
$regions=$_REQUEST['regions'];}
else{
$regions='all';}

if(isset($_REQUEST['agents'])){
$agents=$_REQUEST['agents'];}
else{
$agents='all';}

if(isset($_REQUEST['terrys'])){
$terrys=$_REQUEST['terrys'];}
else{
$terrys='all';}

if(isset($subregions)){
$subregions=$_REQUEST['subregions'];}
else{
$subregions='all';}

if(isset($_REQUEST['agenttypes'])){
$agenttypes=$_REQUEST['agenttypes'];}
else{
$agenttypes='all';}

if(isset($_REQUEST['items'])){
$items=$_REQUEST['items'];}
else{
$items='all';}

if(isset($_REQUEST['ranges'])){
$ranges=$_REQUEST['ranges'];}
else{
$ranges='all';}

if(isset($_REQUEST['subranges'])){
$subranges=$_REQUEST['subranges'];}
else{
$subranges='all';}

#$flt=racs(1);

if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($subregions)!='all'){
$flt=$flt." and sub_regions='".$subregions."' ";}
if(strtolower($agents)!='all'){
$flt=$flt." and agent_names='".$agents."' ";}
if(strtolower($agenttypes)!='all'){
$flt=$flt." and agent_types='".$agenttypes."' ";}
if(strtolower($terrys)!='all'){
$flt=$flt." and territory_names='".$terrys."' ";}
if(strtolower($items)!='all'){
$flt=$flt." and item_names='".$items."' ";}
if(strtolower($ranges)!='all'){
$flt=$flt." and item_ranges='".$ranges."' ";}
if(strtolower($subranges)!='all'){
$flt=$flt." and sub_ranges='".$subranges."' ";}

$flt=racs(1);
stps(1);
sacnew(1);//('pr','zone','gobs','sales','Periodic sales',$zones,$regions,$agents,$agenttypes,$terrys,$ranges,$subranges,$items);

printf("<div style='position:absolute;left:1%%;top:5%%;width:15%%;'>");

//stksnew(1);//prshows('pr','zone','gobs','sales','Periodic sales',$zones,$regions,$agents,$agenttypes,$terrys,$ranges,$subranges,$items);
//stksdvs(1);




printf("</div>");
printf("<div style='position:absolute;left:18%%;top:5%%;width:75%%;'>");
printf("<form method='post' action='addrecpay.php'>");
printf("<br><table id='fms' style='width:100%%'><tr>");
printf("<td><input type='text' id='inputField' name='starts' size=8  value='%s'
style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'>
 - <input type='text' id='inputField2' name='ends' size=8  value='%s'
 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'>
<input type='submit' value='Go'  style='font-size:13;background:#CACACA;color:#006699;border:1.5px solid #006699;border-radius:1pxfont-size:13;'>
</form>",$starts,$ends);
 printf("<td align='right' style='color:grey;font-size:12;'>Search
<input type='text' autocomplete='off' id='srctxt' size=28 style='font-family:calibri;font-size:13;border:1px solid silver;border-radius:1.5px;' onkeyup=\"srgs(event)\"></td>
<td style='font-size:12;color:green;text-align:center;background:silver;border:1px solid white;' onclick=\"scopys(1)\">Copy</td></tr></table>");
printf("<table id='dt' style='width:100%%;font-size:12;color:grey;border-collapse:collapse;'>");
printf("<tr style='border:1px solid silver;background:silver;'>
<td width='8%%' align='center'>Inv-Date</td>
<td width='8%%' align='center'>Dlv-Date</td>
<td width='8%%' align='center'>Invoice</td>
<td width='25%%' align='center'>Customer</td>
<td width='10%%' align='center'>Territory</td>
<td width='10%%' align='center'>Status</td>
<td width='10%%' align='center'>Mode</td>
<td width='10%%' align='center'>Invoice</td>
<td width='10%%' align='center'>Claim</td>
<td width='5%%' align='center'>Srtn</td>
<td width='5%%' align='center'>Undlv</td>
<td width='5%%' align='center'>Mrtn</td>
<td width='10%%' align='center'>Cash</td>
<td width='10%%' align='center'>Cheq</td>
<td width='10%%' align='center'>Charges</td>
<td width='10%%' align='center'>Chq-Rtn</td>
<td width='10%%' align='center'>Due</td>
<td width='10%%'>Payment</td>
<td width='10%%'>Status</td>
</tr>");

$result="select date,manubil,agent,agtype,invamount,srtnv,mrtnv,cashv,cheqv,claimamt,chgamt,disrate,undlv,dlvdate,claimdate,dlvdtpay,stas,vatt,cheqrtn,pmdo from(select distinct invoice_number as manubil from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' and invoice_types='primary invoice' order by dates) as ags

left join(select invoice_number,agent_names as agent,territory_names as agtype,dates as date,sum(box_qty*box_price) as invamount,sum(box_qty*box_price)*discount_rates as disrate,dlv_dates as dlvdate,payments as stas,(sum(box_qty*box_price)+sum(box_qty*box_price)*discount_rates)*vat_rates as vatt,paymode as pmdo from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by invoice_number) as agg on agg.invoice_number=manubil

left join(select invoice_number,sum(claim_amount) as claimamt,sum(charges_amount )as chgamt,sum(srtn_amount) as srtnv,sum(mrtn_amount) as mrtnv,sum(cash_amount) as cashv,sum(cheq_amount) as cheqv,sum(undlv_amount) as undlv,dlv_dates as dlvdtpay,claim_month as claimdate from st_paymode ".$flt." and dates>='".$starts."' and dates<='".$ends."' group by invoice_number) as agm on agm.invoice_number=manubil

left join(select invoice_number,sum(cheq_amount) as cheqrtn from st_paymode ".$flt." and dates>='".$starts."' and dates<='".$ends."' and pay_types='Cheque-Rtn' group by invoice_number) as agmd on agmd.invoice_number=manubil";

$due=0;
$invamt=0;
$netchq=0;
$qrym=mysqli_query($conn,$result);
while($rstv=mysqli_fetch_row($qrym)){
$netchq=$rstv[8]-$rstv[18];    
$invamt=$rstv[4]+$rstv[11]+$rstv[17];    
$due=($invamt+$rstv[10]+$rstv[18])-$rstv[9]-$rstv[5]-$rstv[6]-$rstv[7]-$netchq-$rstv[12];

$dhrf="<a href='rpmpaynotes.php?actp=dvnts&ddates=".$rstv[0]."&voucher=".$rstv[1]."&agents=".$rstv[2]."&claims=".$rstv[9]."&charges=".$rstv[10]."&srtnvl=".$rstv[5]."&mrtnvl=".$rstv[6]."&duamt=".$due."&undnvl=".$rstv[12]."&cldate=".$rstv[14]."&dlvdates=".$rstv[13]."&invs=".$invamt."' target='_blank'>Payment</a>";

$dhrff="<a href='rpmpaysts.php?actp=dvnts&voucher=".$rstv[1]."&agents=".$rstv[2]."&status=".$rstv[16]."' target='_blank'>Status</a>";

printf("<tr class='clss' style='border:1px solid silver;'><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td bgcolor='#7AD1F9'>%s</td><td bgcolor='#7AF9A6'>%s</td><td bgcolor='#E1E106'>%s</td><td bgcolor='#DCC4E5'>%s</td><td bgcolor='#F3637F'>%s</td><td bgcolor='#63C3F3'>%s</td><td>%s</td><td bgcolor='#F1D6F3'>%s</td><td bgcolor='#F1D6F3'>%s</td><td bgcolor='#A6F7CD' >%s</td><td>%s</td><td bgcolor='#D9DDDF'>%s</td></tr>",
$rstv[0],$rstv[13],$rstv[1],$rstv[2],$rstv[3],$rstv[16],$rstv[19],number_format($invamt,2,'.',','),number_format($rstv[9],2,'.',','),number_format($rstv[5],2,'.',','),number_format($rstv[12],2,'.',','),number_format($rstv[6],2,'.',','),number_format($rstv[7],2,'.',','),number_format($rstv[8]-$rstv[18],2,'.',','),number_format($rstv[10],2,'.',','),number_format($rstv[18],2,'.',','),number_format($due,2,'.',','),$dhrf,$dhrff);}
//}
printf("</div>");

}
?>
</div>
</body>
</html>