<?php
error_reporting(0);
require "racs.php";
require "vtshowsBBB.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>

<html>
<title>Diana-Range targets</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();
pvl=dt.rows[cnt].cells[2].innerText.toLowerCase();

if(sv.length>1 && (tvl.indexOf(sv)>=0 || rvl.indexOf(sv)>=0 || pvl.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\nContanct Sales and Marketing division</div>");
}
else{
$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_diana");

if(isset($_REQUEST['starts'])){
$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['rmid'])){
$rmid=$_REQUEST['rmid'];}
else{
$rmid='all';}

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

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

if(isset($_REQUEST['sm_id'])){
$sm_id=$_REQUEST['sm_id'];}
else{
$sm_id='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';}

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

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

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

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

#$flt=racs(1);

if(strtolower($zones)!='all'){
$flt=$flt." and zones='".$zones."' ";}
if(strtolower($rmid)!='all'){
$flt=$flt." and rm_id='".$rmid."' ";}
if(strtolower($chanell)!='all'){
$flt=$flt." and chanell='".$chanell."' ";}
if(strtolower($regions)!='all'){
$flt=$flt." and regions='".$regions."' ";}
if(strtolower($sm_id)!='all'){
$flt=$flt." and sm_id='".$sm_id."' ";}
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."' ";}
if(strtolower($executive)!='all'){
$flt=$flt." and executive_names='".$executive."' ";}
if(strtolower($arm_names)!='all'){
$flt=$flt." and arm_names='".$arm_names."' ";}
if(strtolower($asm_names)!='all'){
$flt=$flt." and asm_names='".$asm_names."' ";}
if(strtolower($rsm_names)!='all'){
$flt=$flt." and rsm_names='".$rsm_names."' ";}
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:30%%;'>RANGE PERFORMANCE</div>");
printf("<div style='font-size:16;color:#006699;border-bottom:1px solid silver;width:30%%;'>REGION-$zones</div>");
printf("<div style='font-size:16;color:#006699;border-bottom:1px solid silver;width:30%%;'>AREA-$regions</div>");
printf("<div style='font-size:16;color:#006699;border-bottom:1px solid silver;width:30%%;'>AGENTS-$agents</div>");


$tv=0;
$tz=0;
$arr=array();

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

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

printf("<form method='post' action='subrangenew.php?actp=rpt%s'><table style='width:80%%;font-size:13;'>",$sprt);
printf("<tr><td style='color:grey;'>Select the date range</td><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>
<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 width=40%% 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%%;font-size:13;border-collapse:collapse;color:#006699;'>
<tr style='background:silver;border:1px solid white;text-align:right;'>
<td style='border:1px solid white;text-align:center;'>Range</td>
<td style='border:1px solid white;text-align:center;'>Target Value</td>
<td style='border:1px solid white;text-align:center;'>Primary Order</td>
<td style='border:1px solid white;text-align:center;'>Primary Sale</td>
<td style='border:1px solid white;text-align:center;'>Pri%%</td>
<td style='border:1px solid white;text-align:center;'>Pri-Diff</td>
<td style='border:1px solid white;text-align:center;'>RD Sale</td>
<td style='border:1px solid white;text-align:center;'>Rd%%</td>
<td style='border:1px solid white;text-align:center;'>Target-Ton</td>
<td style='border:1px solid white;text-align:center;'>Order-Ton</td>
<td style='border:1px solid white;text-align:center;'>RD-Ton</td>
<td style='border:1px solid white;text-align:center;'>Primary-Ton</td>
<td style='border:1px solid white;text-align:center;'>Ton%%</td>
<td style='border:1px solid white;text-align:center;'>Ton-Diff</td>
<td style='border:1px solid white;text-align:center;'>Direct</td>
<td style='border:1px solid white;text-align:center;'>SKU</td>
</tr>");


////////////////////////////without balance stock///////////////////////////////

$sqlst="select mranges,tgvl,prvl,odvl,rdvl,prvldir,prton,trton,odton,prdton,rddton from (select distinct sub_ranges as mranges from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends."' union select distinct sub_ranges from rc_targets ".$flt." and dates>='".$starts."' and dates<='".
$ends."' union select distinct sub_ranges from rc_orders ".$flt." and dates>='".$starts."' and dates<='".
$ends."' union select distinct sub_ranges from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".
$ends."') as ags 

left join (select sub_ranges,sum(box_qty*box_price) as tgvl,sum(box_qty*packets_per_box*packet_weights/1000000) as trton from rc_targets ".$flt." and 
dates>='".$starts."' and dates<='".$ends."' and chanell='GENERAL TRADE' group by sub_ranges) as tgs on tgs.sub_ranges=mranges

left join (select sub_ranges,sum(box_qty*box_price) as prvl,sum(box_qty*packets_per_box*packet_weights/1000000) as prton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends.
"' and chanell='GENERAL TRADE' AND zones<>'H-OFFICE' group by sub_ranges) as acs on acs.sub_ranges=mranges

left join (select sub_ranges,sum(box_qty*box_price) as prvldir,sum(box_qty*packets_per_box*packet_weights/1000000) as prdton from rc_invoices ".$flt." and dates>='".$starts."' and dates<='".$ends.
"' and chanell='GENERAL TRADE' and agent_types='DIRECT' AND zones<>'H-OFFICE' group by sub_ranges) as acsdir on acsdir.sub_ranges=mranges

left join (select sub_ranges,sum(pcs_qty*pcs_price) as rdvl,sum(pcs_qty*packet_weights/1000000) as rddton from rd_saleinv ".$flt." and dates>='".$starts."' and dates<='".$ends.
"' group by sub_ranges) as rdc on rdc.sub_ranges=mranges

left join (select sub_ranges,sum(box_qty*box_price) as odvl,sum(box_qty*packets_per_box*packet_weights/1000000) as odton from rc_orders ".$flt." and dates>='".$starts."' and dates<='".$ends.
"' and order_status<>'CANCELLED' and chanell='GENERAL TRADE' AND zones<>'H-OFFICE' group by sub_ranges) as acss on acss.sub_ranges=mranges";
 
$tgv=0;
$acv=0;
$tgw=0;
$acw=0;
$rdsl=0;
$rdqtt=0;
$optstktot=0;
$clostktot=0;
$prton=0;
$trton=0;
$totodton=0;
$tottondif=0;
$rdtontot=0;

$qry=mysqli_query($conn,$sqlst);
while($rst=mysqli_fetch_row($qry)){
$tgvll=$tgvll+$rst[1];
$prvll=$prvll+$rst[2];
$odwl=$odwl+$rst[3];
$rdsl=$rdsl+$rst[4]+$rst[5];
//$optstktot=$optstktot+$rst[7]+$rst[9]+$rst[11]-$rst[8];
//$clostktot=$clostktot+$rst[6]+$rst[7]+$rst[10]-$rst[5];
$prton=$prton+$rst[6];
$trton=$trton+$rst[7];
$diftot=$diftot+$rst[1]-$rst[2];
$tondif=$tondif+$rst[7]-$rst[6];
$totodton=$totodton+$rst[8];
$rdtontot=$rdtontot+$rst[9]+$rst[10];
$prdr=$prdr+$rst[5];


if($rst[1]==0){
$prii=0;}
else{
$prii=$rst[2]*100/$rst[1];}
	
if($rst[1]==0){
$rdii=0;}
else{
$rdii=($rst[4]+$rst[5])*100/$rst[1];}

if($rst[7]==0){
    $prtonn=0;}
    else{
        $prtonn=$rst[6]*100/$rst[7];}
        
        
//This Need to check Later ///        

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'style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='right'style='color:black'bgcolor='#DAF7A6'>%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'style='color:black'bgcolor='#DAF7A6'>%s%%</td>
<td align='right'>%s</td>
<td align='right'>%s</td>
<td align='center'><a href='rpmtgttitemwisetgsummary2.php?starts=%s&ends=%s&regions=%s&ranges=%s&chanell=%s&agents=%s&zones=%s' target='_blank'>items</a></td>",
$rst[0],
number_format($rst[1],0,'.',','),
number_format($rst[3],0,'.',','),
number_format($rst[2],0,'.',','),
number_format($prii,1,'.',','),
number_format($rst[1]-$rst[2],0,'.',','),
number_format($rst[4]+$rst[5],0,'.',','),
number_format($rdii,1,'.',','),
number_format($rst[7],2,'.',','),
number_format($rst[8],2,'.',','),
number_format($rst[9]+$rst[10],2,'.',','),
number_format($rst[6],2,'.',','),
number_format($prtonn,1,'.',','),
number_format($rst[7]-$rst[6],2,'.',','),
number_format($rst[5],0,'.',','),
$starts,$ends,$regions,$rst[0],'GENERAL TRADE',$agents,$zones);
}


if($tgvll==0){
$pcs=0;}
else{
$pcs=$prvll*100/$tgvll;}

if($prton==0){
$toton=0;}
else{
$toton=$prton*100/$trton;}
	
if($tgvll==0){
$wpcs=0;}
else{
$wpcs=$rdsl*100/$tgvll;}

printf("<tr style='border-bottom:1px solid silver;font-weight:bold;'>
<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='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='rpmtgttitemwisetgsummary2.php?starts=%s&ends=%s&chanell=%s&regions=%s&agents=%s&zones=%s' target='_blank'>items</a></td>

</tr></table>",
number_format($tgvll,0,'.',','),
number_format($odwl,0,'.',','),
number_format($prvll,0,'.',','),
number_format($pcs,1,'.',','),
number_format($diftot,0,'.',','),
number_format($rdsl,0,'.',','),
number_format($wpcs,1,'.',','),
number_format($trton,2,'.',','),
number_format($totodton,2,'.',','),
number_format($rdtontot,2,'.',','),
number_format($prton,2,'.',','),
number_format($toton,2,'.',','),
number_format($trton-$prton,2,'.',','),
number_format($prdr,0,'.',','),
$starts,$ends,'GENERAL TRADE',$regions,$agents,$zones);



printf("</div>");

}

?>

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