<?php

require "showshp.php";

session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}


?>


<html>

<link rel="stylesheet" type="text/css" href="rdsty.css">
<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="dtps.js"></script>


<script>

function mnh(mn,bn)
{



}


function mch(mn)
{
if(mn.style.background!='silver'){
mn.style.background='orange';}
}


function mnc(mn)
{
if(mn.style.background!='silver'){
mn.style.background='white';}

}


function agentsn()
{
var rg=document.getElementById('regions');
var ag=document.getElementById('agents');

var rvl=rg.options[rg.selectedIndex].text;
ag.length=0;

var httpr= new XMLHttpRequest();
httpr.onreadystatechange=function(){
if(httpr.readyState==4 && httpr.status==200){
var vl=httpr.responseText;
var tmp=vl.split("###");
for(cn=0;cn<tmp.length;cn++){
var ops=document.createElement("option");
ops.text=tmp[cn];
ag.add(ops,null);}
}}

httpr.open("POST","aj.php?actp=agentsn&sparams="+rvl,true);
httpr.send();

var ops=document.createElement("option");
ops.text="All";
ag.add(ops,null);
}

</script>
</head>




<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
<div class="hdn"></div>

<?php
printf("<div class='ses'>%s (%s)</div>",$_SESSION['titles'],$_SESSION['positions']);
?>

<div class="tops" align="center">Free Issue Invoice</div>
<div class="homes"><a href="index.php" style='color:#A9BCF5;'>Sign out</a></div>

</body>

</html>




<?php


$grt=0;
$grq=0;
$arr=array();
$arvl=array();
$tvt=0;

$starts=$_REQUEST['starts'];
$ends=$_REQUEST['ends'];
$invoices=$_REQUEST['agent_names'];

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

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

if(isset($_REQUEST['territorys'])){
$territorys=$_REQUEST['territorys'];}
else{
$territorys="All";}

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

if(isset($_REQUEST['rsms'])){
$rsms=$_REQUEST['rsms'];}
else{
$rsms="All";}

if(isset($_REQUEST['executives'])){
$executives=$_REQUEST['executives'];}
else{
$executives="All";}

if(isset($_REQUEST['reps'])){
$reps=$_REQUEST['reps'];}
else{
$reps="All";}

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

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

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

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


$rpsty="regions=".$regions."&agents=".urlencode($agents)."&territorys=".$territorys."&ranges=".$ranges."&zones=".$zones."&rsms=".$rsms."&executives=".$executives."&reps=".$reps."&items=".$items."&subranges=".$subranges; 


$months=date('m');
$days=date('t');
$years=date('Y');


if($starts==0 || $starts==''){
$starts=$years."-".$months."-01";}
if($ends==0 || $ends==''){
$ends=$years."-".$months."-".$days;}


showsdsr();

#reportdets("Return note summary");

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$seflt=" where agent_names is not null ";

$sesqlst="select * from acs where user_names='".$_SESSION['usernames']."'";
$seqry=mysqli_query($conn,$sesqlst);

while($serst=mysqli_fetch_row($seqry)){
if(strtolower($serst[1])!='all'){
$seflt=$seflt." and zones='".$serst[1]."' ";}
if(strtolower($serst[2])!='all'){
$seflt=$seflt." and regions='".$serst[2]."' ";}
if(strtolower($serst[3])!='all'){
$seflt=$seflt." and territory_names='".$serst[3]."' ";}
if(strtolower($serst[4])!='all'){
$seflt=$seflt." and agent_names='".$serst[4]."' ";}
if(strtolower($serst[5])!='all'){
$seflt=$seflt." and item_names='".$serst[5]."' ";}
if(strtolower($serst[6])!='all'){
$seflt=$seflt." and item_ranges='".$serst[6]."' ";}
if(strtolower($serst[7])!='all'){
$seflt=$seflt." and sub_ranges='".$serst[7]."' ";}
}

$flt=$seflt;


printf("</div><div class='srg'></div>");
printf("<div class='mains'>");


$sqlst="select distinct agent_names,chanell,zones,regions,rsm_names,executive_names,territory_names from dis_free ".$flt." and 
agent_names='".$invoices."' ";

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
#echo $sqlst;

printf("<div class='fra' style='font-size:16'>CUSTOMER:%s</div>",$rst[0]);
#printf("<div class='fre' style='font-size:16'>Regions:%s</div>",$rst[3]);
#printf("<div class='frg' style='font-size:16;left:50%%;'>CUSTOMER:%s</div>",$rst[0]);


printf("<div class='tdt'><br><table style='font-size:13'><tr style='border:1px solid white;background:silver;'><td colspan='3'><b>Free Issue Item Details</td><td align='center'><a href='#'><b>Print</b></a></td></tr>",$starts,$agents,$invoices,$regions);

printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=600 align='center'  style='border:1px solid white' >Item</td>
<td width=60 align='center'  style='border:1px solid white;padding-right:1%%;' >Price/Pcs</td><td width=60 align='center'  style='border:1px solid white;padding-right:1%%;' >Pieces</td>
<td width=70 align='center'  style='border:1px solid white;padding-right:1%%;' >Free Value</td></tr>");

$sqlst="select item_ranges,item_names,pcs_price,pcs_qty,(pcs_price*pcs_qty)from dis_free ".$flt." and agent_names='".$invoices."' and item_ranges<>'DISCOUNTS' order by item_ranges ";

#echo $sqlst;

$qry=mysqli_query($conn,$sqlst);

$gvt=0;
$dvt=0;
$nvt=0;
$tvt=0;

$tmr="";
$tmg=0;
$tmd=0;
$tmn=0;
$rng=0;

while($rst=mysqli_fetch_row($qry)){
$tvt++;
$gvt=$gvt+$rst[4];
$dvt=$dvt+$rst[5];
$nvt=$nvt+$rst[6];

if($tmr!=$rst[0] && $tmg>0){
printf("<tr style='font-weight:bold;'><td>%s total</td><td align='right' style='padding-right:1%%;'>%s</td>
<td align='right' style='padding-right:1%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s</td><td></td><td></td></tr><tr><td style='color:transparent;'>-</td></tr>",$rng,number_format($tmn,2,'.',','),
number_format($tmd,2,'.',','),number_format($tmg,2,'.',','));
$tmg=0;
$tmd=0;
$tmn=0;
}

$rng=$rst[0];

if($tmr!=$rst[0] && $tmg==0){
printf("<tr><td colspan='4' style='background:#E6E6E6;'><b>%s</b></td></tr>",$rst[0]);}
$tmr=$rst[0];
$tmg=$tmg+$rst[4];
$tmd=$tmd+$rst[5];
$tmn=$tmn+$rst[6];

printf("<tr class='cls'><td>%s</td><td align='right' style='padding-right:1%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s</td>
<td align='right' style='padding-right:1%%;'>%s</td></tr>",
$rst[1],number_format($rst[2],2,'.',','),number_format($rst[3],2,'.',','),number_format($rst[4],2,'.',','));
}

if($tmg>0){
printf("<tr style='font-weight:bold;'><td>%s total</td><td align='right' style='padding-right:1%%;'>%s</td>
<td align='right' style='padding-right:1%%;'>%s</td><td align='right' style='padding-right:1%%;'>%s</td></tr><tr><td>-</td></tr>",$rng,number_format($tmn,2,'.',','),
number_format($tmd,2,'.',','),number_format($tmg,2,'.',','));
}

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

$sqlst="select distinct agent_names,dates,bill_num,zones,regions,rsm_names,executive_names,territory_names,areas from dis_free ".$flt." and 
agent_names='".$invoices."' ";

$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

printf("<div class='dts' style='left:930;top:75;'>");
printf("<a href='#'><b>Open PDF</b></a><br>",$rst[2],$rst[1]);
printf("<table style='font-size:13'><tr bgcolor='silver'><td width=200><b>Agent details</td></tr</table>");
printf("<table style='font-size:13'><tr><td height=20>Agent</td><td>: %s</td></tr>",$rst[0]);
printf("<tr><td height=20>Zone</td><td>: %s</td></tr>",$rst[3]);
printf("<tr><td height=20>Region</td><td>: %s</td></tr>",$rst[4]);
printf("<tr><td height=20>RSM</td><td>: %s</td></tr>",$rst[5]);
#printf("<tr><td height=20>Executive</td><td>: %s</td></tr>",$rst[6]);
#printf("<tr><td height=20>Territory</td><td>: %s</td></tr></div>",$rst[7]);
printf("<tr><td height=20>Territory</td><td>: %s</td></tr></table></div>",$rst[7]);

"<br>";"<br>";"<br>";

printf("<div id='dtf' class='dtf'><table style='font-size:13'><tr bgcolor='silver'><td  height=20 width=200><b>Invoice summary</td></tr</table><table style='font-size:13'>");
printf("</tr><td height=20 width=100>No of items</td><td align='right' width=100>%s</td></tr>",number_format($tvt,0,'.',','));
printf("<tr><td  height=20 width=100>Free value</td><td align='right'>%s</td></tr>",number_format($gvt,2,'.',','));
printf("<tr><td height=20 >Discount (%s%%)</td><td align='right'>%s</td></tr>",number_format($dvt*100/$gvt,2,'.',','),number_format($dvt,2,'.',','));
printf("<tr><td height=20 >Bill Value</td><td align='right'>%s</td></tr></table>",number_format($nvt,2,'.',','));

?> 