<?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>Organization Donation</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">

<?php
if(isset($_REQUEST['file_submit'])){
$csvf=$_FILES['files']['tmp_name'];
update_invoices(0,$csvf);
}

function update_invoices($acl,$fl)
{
set_time_limit(0);

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$cols=0;
$rows=0;
$state=0;
$unos=0;

$csvf=$fl;
if(strlen($fl)>2){
printf("%s<br>",$csvf);

$cols=0;
$rows=0;
$state=0;
$unos=0;


if (($handle = fopen($csvf, "r")) != FALSE) {
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
$rows++;

if(strlen($data[0])>0 && strlen($data[1])>0 && $rows>=5){
$adstate=1;
$state=0;
$dates=0;

$arr=explode("/",$data[0]);
if(count($arr)>1){
$dates=$arr[2]."-".$arr[1]."-".$arr[0];
}
$agent=$data[4];
$naration=$data[20];
$invoice=$data[1];

//$sqlstn="delete from st_notes where ref_numbers='".$invoice."' and note_types='FG GRN' ";
//$qry=mysqli_query($conn,$sqlstn);

$sqlst="select invoice_number from rc_orgdonation where invoice_number='".$invoice."' and dates='".$dates."'";
//echo $sqlst."<br>";
$qry=mysqli_query($conn,$sqlst);

if(mysqli_num_rows($qry)==0){
$state=1;}
}
else{
$adstate=0;
}


//if($state==1 && strlen($data[0])==0 && strlen($data[3])==0 && $adstate==0 && $data[5]!='Totals'){
//$unos++;
//$items=$data[5];
//$qty=(trim($data[5]," Box"))*1;
//$qtypcs=(trim($data[5]," pcs"))*1;
//$prices='0';

if($state==1 && $data[5]!='Totals'){
$unos++;
$items=$data[5];
$qty=(trim($data[6]))*1;
$qtypcs=(trim($data[9]))*1;
$bprices=$data[8];
$pcsprices=$data[11];
$mrpbox=$data[23];
$locationw=trim($data[22]);
$itemcode=trim($data[21]);

$sqlst="select zone,region,rsm_name,executive_name,rep_name,agent_name, agent_type,territory_name,zone_order,sub_region,chanelname from agentsmrd where agent_replc='".$agent."'";
$aqry=mysqli_query($conn,$sqlst);
if(mysqli_num_rows($aqry)==0){
printf("following agents do not exist - %s<br>",$agent);}
$rsta=mysqli_fetch_row($aqry);
#echo $sqlst;


$rsqlst="select distinct item_name,item_ranges,sub_ranges,(packets_per_box*pcs_per_packet),packet_weight,location,retail_price from itemsmrd where itemcode='".$itemcode."'";
$rqry=mysqli_query($conn,$rsqlst);
if(mysqli_num_rows($rqry)==0){
printf("following items do not exist - %s<br>",$items);}
//else{
$trst=mysqli_fetch_row($rqry);
$itemnamee=$trst[0];
$itemranges=$trst[1];
$subranges=$trst[2];
$packetsperbox=$trst[3];
$packetweights=$trst[4];
//$pcsprice=$trst[6];
$lcnss=$trst[5];
//}

//echo $rsqlst;

if($qty>0){
$details="DONATION";
}
//if($qtpcs>0){
//$details="FG GRN";
//}

//$mxn=0;
//$sqlstm="select max(sq_number) from st_notes where note_types='FG GRN' ";
//$qry=mysqli_query($conn,$sqlstm);
//$rst=mysqli_fetch_row($qry);
//$mxn=$rst[0]+1;
//echo $sqlstm;

$sqlstv="INSERT INTO rc_orgdonation(dates,invoice_number,invoice_types,item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,pcs_price,box_price,box_mrp,pcs_qty,box_qty,user_names,user_dates,naration,agent_names,agent_types,zone_orders,zones,regions,chanell,sub_regions,rsm_names,executive_names,territory_names,rep_names,stores_names,itemcodes) VALUES('".$dates."','".$invoice."','".$details."','".$itemnamee."','".$itemranges."','".$subranges."',".$packetsperbox.",".$packetweights.",".$pcsprices.",".$bprices.",".$mrpbox.",".$qtypcs.",".$qty.",'test user',now(),'".$naration."','".$rsta[5]."','".$rsta[6]."','".$rsta[8]."','".$rsta[0]."','".$rsta[1]."','".$rsta[10]."','".$rsta[9]."','".$rsta[2]."','".$rsta[3]."','".$rsta[7]."','".$rsta[4]."','".$locationw."','".$itemcode."')";
$aqry=mysqli_query($conn,$sqlstv);
//echo $sqlstv;
}
}}}

printf("<div class='cmp'>");
printf("Upload complete<br>Total invoices uploaded - %s<br>",$unos);
printf("</div>");
}
?>


<link rel="stylesheet" tupe"text/css" href="rdsty.css">
<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 territorysn()
{
var rg=document.getElementById('regions');
var ag=document.getElementById('territorys');
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);}
}}
var ops=document.createElement("option");
ops.text="All";
ag.add(ops,null);
httpr.open("POST","aj.php?actp=territorysn&sparams="+rvl,true);
httpr.send();
}


function mnh(mn,bn)
{
document.getElementById("agentsb").style.display="none";
document.getElementById("itemsb").style.display="none";
document.getElementById("areasb").style.display="none";
var cs=document.getElementsByClassName('amn');
var cn;

for(cn=0;cn<cs.length;cn++){
cs[cn].style.background="white";}
document.getElementById(bn).style.display="block";
document.getElementById(mn).style.background="silver";
}


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


function mnc(mn)
{
if(mn.style.background!='silver'){
mn.style.background='white';}
}
</script>
</head>


<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
</body>
</html>
<?php
$dates=0;
$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;

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


printf("<div class='mnf'>");
printf("<b>Import FREE ISSUE Invoices form from Busy system<br></b>");
printf("The file type should be CSV. Export in detailed View<br><br>");
printf("Select the file:");

printf("<form  enctype='multipart/form-data' method='POST' action='rcorgdonation.php?actp=0'>");
printf("<input type='file' id='files' name='files' ><br><br>");
printf("<input type='submit' name='file_submit' value='Go' ><br>");
printf("</form>");
?>
</body>
</html>
