<?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>GT-MT SKU</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">

<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[0].innerText.toLowerCase();
rvl=dt.rows[cnt].cells[1].innerText.toLowerCase();
if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.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;'>

<div style='width=30%;height:100%%;'>

<?php


$flt=racs(1);

if($flt=="access failed"){
printf("<div style='font-color:red;'>>Access failed\Contact Sales and Marketing division</div>");
}
else{

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");

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($_REQUEST['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."' ";}


stps(1);
printf("<div class='sds'>");
newdash(1);
printf("</div>");
printf("<div class='mnf'>");

printf("<div style='font-size:16;color:#006699;border-bottom:1px solid silver;width:20%%;'>Region - Sales</div>");

printf("<BR><BR>");
$sprt="&zones=".$zones."&regions=".$regions."&subregions=".$subregions."&agents=".$agents."&agenttypes=".$agenttypes."&terrys=".$terrys."&ranges=".$ranges."&items=".$items."&subranges=".$subranges;

$sqlst="select distinct rgs from (select distinct zone_orders as rds,zones as rgs from rc_invoicesbkd union select distinct zone_orders,zones from rc_targetsbkd order by rds) as zns";
$qry=mysqli_query($conn,$sqlst);

printf("<div style='width:95%%;height:5%%;border:0px solid silver;border-radius:2px;display:table;vertical-align:middle;'>");

printf("<form method='post' action='rpmprregionsalesnuwan2.php?actp=rpt%s'><table style='width:80%%;font-size:13;'>",$sprt);
printf("<tr><td width=5%%><input type='text' id='inputField' value='%s' name='starts' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td><td width=1%%>-</td><td width=5%%><input type='text' id='inputField2' value='%s' name='ends' size=8 style='font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'></td>
<td width=20%%><select name='zones' style='width:95%%;font-size:13;background:#CACACA;border:1px solid #006699;border-radius:1px;font-family:calibri;color:#006699;'><option>%s</option><option>All</option>",$starts,$ends,$zones);
while($rst=mysqli_fetch_row($qry)){
printf("<option>%s</option>",$rst[0]);}	
printf("</select></td><td><input type='submit' value='Go' style='font-size:13;background:#CACACA;color:#006699;border:1.5px solid #006699;border-radius:1pxfont-size:13;'></td></form>",$starts,$ends);

printf("<td width=38%% align='right' style='color:grey;'>Search</td><td width=20%%>
<input type='text' id='srctxt' size=28 style='font-family:calibri;font-size:14;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>");
printf("</table>");



printf("<table id='dt' style='width:80%%;color:#006699;font-size:13;border-collapse:collapse;'>");
printf("<tr style='text-align:right;'><td style='background:silver;border:1px solid white;text-align:left'>Region</td><td style='background:silver;border:1px solid white;' >Qty</td>
<td style='background:silver;border:1px solid white;' >Tonnage</td><td style='background:silver;border:1px solid white;' >Gross Value</td>
<td style='background:silver;border:1px solid white;' >Discount</td>
<td style='background:silver;border:1px solid white;' >Vat</td>
<td style='background:silver;border:1px solid white;'>Net value</td>
<td style='background:silver;border:1px solid white;'>Net Sales[Gros+Vat]</td><td style='background:silver;border:1px solid white;' align='center'>Range</td><td style='background:silver;border:1px solid white;' align='center'>Agents</td><td style='background:silver;border:1px solid white;' align='center'>Items</td></tr>");


$sqlst="select regions,sum(box_qty),sum(box_qty*packets_per_box*packet_weights/1000000),sum(box_qty*box_price),sum(box_qty*box_price*discount_rates),sum(box_qty*box_price*vat_rates) from 
rc_invoicesbkd ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell<>'GENERAL TRADE' AND regions<>'' group by regions order by zone_orders ";

$tqty=0;
$twt=0;
$tvl=0;
$tds=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tqty=$tqty+$rst[1];
$twt=$twt+$rst[2];
$tvl=$tvl+$rst[3];
$tds=$tds+$rst[4];
$vattot=$vattot+$rst[5];
	
printf("<tr style='border-bottom:1px solid silver;'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='right'>%s</td><td align='right'>%s</td><td align='center'><a href='rpmprrtrangewise.php?starts=%s&ends=%s&regions=%s' target='_blank'>Range</a></td><td align='center'><a href='rpmpragentsales.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agents</a></td><td align='center'><a href='rpmregionitemwise.php?starts=%s&ends=%s&regions=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[1],0,'.',','),
number_format($rst[2],3,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[3]+$rst[4]+$rst[5],0,'.',','),
number_format($rst[3]+$rst[5],0,'.',','),
$starts,$ends,($rst[0]),$starts,$ends,($rst[0]),$starts,$ends,($rst[0]));
}
	
printf("<tr style='font-weight:bold;border-bottom:1px solid silver;'>
<td>Total</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='center'><a href='rpmprrtrangewisemt.php?starts=%s&ends=%s' target='_blank'>Range</a></td></tr>",
number_format($tqty,0,'.',','),
number_format($twt,2,'.',','),
number_format($tvl,0,'.',','),
number_format($tds,0,'.',','),
number_format($vattot,0,'.',','),
number_format($tvl+$tds+$vattot,0,'.',','),
number_format($tvl+$vattot,0,'.',','),
$starts,$ends);

printf("</table>");

printf("<BR>");
//Chanell Sales
printf("<table id='dt' style='width:80%%;color:#006699;font-size:13;border-collapse:collapse;'>");
printf("<tr style='text-align:right;'><td style='background:silver;border:1px solid white;text-align:left'>Region</td><td style='background:silver;border:1px solid white;' >Qty</td>
<td style='background:silver;border:1px solid white;' >Tonnage</td><td style='background:silver;border:1px solid white;' >Gross Value</td>
<td style='background:silver;border:1px solid white;' >Discount</td>
<td style='background:silver;border:1px solid white;' >Vat</td>
<td style='background:silver;border:1px solid white;'>Net value</td>
<td style='background:silver;border:1px solid white;'>Net Sales[Gros+Vat]</td><td style='background:silver;border:1px solid white;' align='center'>Range</td><td style='background:silver;border:1px solid white;' align='center'>Agents</td><td style='background:silver;border:1px solid white;' align='center'>Items</td></tr>");


$sqlst="select chanell,sum(box_qty),sum(box_qty*packets_per_box*packet_weights/1000000),sum(box_qty*box_price),sum(box_qty*box_price*discount_rates),sum(box_qty*box_price*vat_rates) from 
rc_invoicesbkd ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell<>'GENERAL TRADE' AND chanell<>'' group by chanell order by zone_orders ";

$tqty=0;
$twt=0;
$tvl=0;
$tds=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tqty=$tqty+$rst[1];
$twt=$twt+$rst[2];
$tvl=$tvl+$rst[3];
$tds=$tds+$rst[4];
$vattotM=$vattotM+$rst[5];
	
printf("<tr style='border-bottom:1px solid silver;'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='right'>%s</td><td align='right'>%s</td><td align='center'><a href='rpmprrtrangewise.php?starts=%s&ends=%s&chanell=%s' target='_blank'>Range</a></td><td align='center'><a href='rpmpragentsales.php?starts=%s&ends=%s&chanell=%s' target='_blank'>Agents</a></td><td align='center'><a href='rpmregionitemwise.php?starts=%s&ends=%s&chanell=%s' target='_blank'>items</a></td></tr>",
$rst[0],
number_format($rst[1],0,'.',','),
number_format($rst[2],3,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[4],0,'.',','),
number_format($rst[5],0,'.',','),
number_format($rst[3]+$rst[4]+$rst[5],0,'.',','),
number_format($rst[3]+$rst[5],0,'.',','),
$starts,$ends,($rst[0]),$starts,$ends,($rst[0]),$starts,$ends,($rst[0]));
}
	
printf("<tr style='font-weight:bold;border-bottom:1px solid silver;'>
<td>Total</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='center'><a href='rpmprrtrangewisemt.php?starts=%s&ends=%s' target='_blank'>Range</a></td></tr>",
number_format($tqty,0,'.',','),
number_format($twt,2,'.',','),
number_format($tvl,0,'.',','),
number_format($tds,0,'.',','),
number_format($vattotM,0,'.',','),
number_format($tvl+$tds+$vattot,0,'.',','),
number_format($tvl+$vattot,0,'.',','),
$starts,$ends);

printf("</table></div>");


////////GT TABLE ////////////

printf("<table id='dt' style='width:80%%;color:#006699;font-size:13;border-collapse:collapse;'>");
printf("<tr style='text-align:right;'><td style='background:silver;border:1px solid white;text-align:left'>Region</td><td style='background:silver;border:1px solid white;' >Qty</td>
<td style='background:silver;border:1px solid white;' >Tonnage</td><td style='background:silver;border:1px solid white;' >Gross Value</td>
<td style='background:silver;border:1px solid white;' >Discount</td><td style='background:silver;border:1px solid white;'>Net value</td><td style='background:silver;border:1px solid white;' align='center'>Range</td><td style='background:silver;border:1px solid white;' align='center'>Agents</td><td style='background:silver;border:1px solid white;' align='center'>Items</td></tr>");


$sqlst="select chanell,sum(box_qty),sum(box_qty*packets_per_box*packet_weights/1000000),sum(box_qty*box_price),sum(box_qty*box_price*discount_rates) from 
rc_invoicesbkd ".$flt." and dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' group by chanell order by zone_orders ";

$tqty=0;
$twt=0;
$tvl=0;
$tds=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tqty=$tqty+$rst[1];
$twt=$twt+$rst[2];
$tvl=$tvl+$rst[3];
$tds=$tds+$rst[4];
	
printf("<tr style='border-bottom:1px solid silver;'><td>%s</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='right'>%s</td><td align='right'>%s</td>
<td align='center'><a href='rpmprrtrangewise.php?starts=%s&ends=%s&chanell=%s' target='_blank'>Range</a></td>
<td align='center'><a href='rpmpragentsales.php?starts=%s&ends=%s&regions=%s' target='_blank'>Agents</a></td>
<td align='center'><a href='rpmregionitemwisenuwan.php?starts=%s&ends=%s&chanell=%s' target='_blank'>items</a></td>
</tr>",
$rst[0],number_format($rst[1],0,'.',','),number_format($rst[2],3,'.',','),number_format($rst[3],0,'.',','),number_format($rst[4],0,'.',','),
number_format($rst[3]+$rst[4],0,'.',','),$starts,$ends,$rst[0],$starts,$ends,($rst[0]),$starts,$ends,($rst[0]));
}
	
printf("<tr style='font-weight:bold;border-bottom:1px solid silver;'><td>Total</td><td align='right'>%s</td><td align='right'>%s</td><td align='right'>%s</td>
<td align='right'>%s</td><td align='right'>%s</td></tr>",
number_format($tqty,0,'.',','),number_format($twt,2,'.',','),number_format($tvl,0,'.',','),number_format($tds,0,'.',','),
number_format($tvl+$tds,0,'.',','));

printf("</table></div>");

}


?>

</div>
</body>
</html>