<?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">View 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();

$dates=$_REQUEST['dates'];
$invoices=$_REQUEST['invoices'];

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

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

showsdsr();

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


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$flt="";
if($_REQUEST['agents']!=0 && $_REQUEST['agents']!=""){
$flt=$flt." agent_names='".$_REQUEST['agents'];}
if($_REQUEST['territorys']!=0 && $_REQUEST['territorys']!=""){
$flt=$flt." territory_names='".$_REQUEST['territorys'];}

$sqlst="select distinct agent_names,dates,invoice_number,zones,regions,rsm_names,executive_names,rep_names,territory_names,number_of_items,discount_values,
rtdiscount_values,wsdiscount_values,market_return_values,sum(scalls),sum(mcalls),sum(pcalls),routes,sum(pcs_price*pcs_qty),count(item_names) from dsr_invoices where dates='".$dates."' and invoice_number='".$invoices."' ".$flt;
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);
#echo $sqlst;

printf("<div class='fra' style='font-size:16'>Date:%s</div>",$rst[1]);
printf("<div class='fre' style='font-size:16'>Reference number:%s</div>",$rst[2]);
printf("<div class='frg' style='font-size:16;left:60%%;'>Customer:%s</div>",$rst[0]);


printf("<div class='tdt'><br><table style='font-size:13'><th border='white' bgcolor='silver' colspan='12'><b>Invoice</th>");

printf("<tr bgcolor='grey' style='color:#E6E6E6;'><td width=300 align='center'  style='border:1px solid white' >Item</td><td width=70 align='center'  style='border:1px solid white' >Price/box</td><td width=90 align='center'  style='border:1px solid white' >Boxes</td><td width=70 align='center'  style='border:1px solid white' >Price/Pcs</td><td width=90 align='center'  style='border:1px solid white' >Pieces</td><td width=100 align='center'  style='border:1px solid white' >Value</td><td align='center' width=80  style='border:1px solid white' >Free issue qty</td></tr>");

$sqlst="select item_names,box_price,box_qty,pcs_price,pcs_qty,(pcs_qty*pcs_price),free_qtys from dsr_invoices where dates='".$dates."' and invoice_number='".$invoices."' ".$flt;
$qry=mysqli_query($conn,$sqlst);

$cnt=0;

while($arst=mysqli_fetch_row($qry)){
$cnt++;
printf("<tr class='cls' bgcolor='white' onmouseover=\"this.style.backgroundColor='orange';\" onmouseout=\"this.style.backgroundColor='white';\"><td>%s</td><td align='right'>%s</td align='right'><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>",$arst[0],number_format($arst[1],2,'.',','),number_format($arst[2],0,'.',','),number_format($arst[3],2,'.',','),number_format($arst[4],0,'.',','),number_format($arst[5],2,'.',','),number_format($arst[6],0,'.',','));
}
printf("</tr></table></div>");

printf("<div class='dts' style='left:940;top:100;'>");
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>Rep</td><td>: %s</td></tr>",$rst[7]);
printf("<tr><td height=20>Territory</td><td>: %s</td></tr></table></div>",$rst[8]);

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($rst[19],0,'.',','));
printf("<tr><td  height=20 width=100>Gross value</td><td align='right'>%s</td></tr>",number_format($rst[18],2,'.',','));
printf("<tr><td height=20 >Retail disc. (%s%%)</td><td align='right'>%s</td></tr>",number_format(($rst[12])*100/$rst[18],2,'.',','),number_format($rst[12],2,'.',','));
printf("<tr><td height=20 >W/S disc. (%s%%)</td><td align='right'>%s</td></tr>",number_format(($rst[11])*100/$rst[18],2,'.',','),number_format($rst[11],2,'.',','));
printf("<tr><td height=20 >Net Value</td><td align='right'>%s</td></tr>",number_format($rst[18]-$rst[11]-$rst[12],2,'.',','));
printf("<tr><td height=20 >Market returns</td><td align='right'>%s</td></tr>",number_format($rst[13],2,'.',','));
printf("<tr><td height=20 >Schedule calls</td><td align='right'>%s</td></tr>",number_format($rst[14],2,'.',','));
printf("<tr><td height=20 >Made calles</td><td align='right'>%s</td></tr>",number_format($rst[15],2,'.',','));
printf("<tr><td height=20 >Productive calls</td><td align='right'>%s</td></tr>",number_format($rst[16],2,'.',','));
printf("<tr><td height=20 >Route</td><td align='right'>%s</td></tr></table>",$rst[17]);




?> 