<?php
require "racs.php";
require "fgstyles.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>
<html>
<title>Diana||FG-GTN-BUSY</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_diana");
$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[1])>0 && strlen($data[2])>0 && $rows>=5){
$adstate=1;
$state=0;
$dates=0;

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

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

$sqlst="select ref_numbers from st_notes where ref_numbers='".$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[1]!='Grand Total'){
//$unos++;
//$items=$data[1];
//$qty=(trim($data[4]," Box"))*1;
//$qtypcs=(trim($data[5]," pcs"))*1;
//$prices=trim($data[6],"/Box");

if($state==1 && $data[5]>'0' && $data[4]!='Totals'){
$unos++;
$items=$data[4];
$qty=(trim($data[5]))*1;
$qtypcs=(trim($data[8]))*1;
$prices=trim($data[7]);
$pcsprices=trim($data[10]);
$itemcode=trim($data[16]);
$inventoryloaction=trim($data[3]);
$boxmrp=trim($data[17]);


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

//echo $rsqlst;

if($qty>0){
$details="FG GTN";
}
if($pcsprices==""){
$pcsprices="0";
}

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

$sqlstv="INSERT INTO st_notes(dates,note_number,ref_dates,ref_numbers,ref_types,sq_number,note_types,stores_names,locations,itemcodes,item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,box_price,box_qty,pcs_qty,pcs_price,stock_box_qty,user_names,user_dates,com_tecs,box_mrp) VALUES('".$dates."','GTN:".$invoice."','".$dates."','".$invoice."','FG-GTN','".$mxn."','".$details."','".$inventoryloaction."','".$lcnss."','".$itemcode."','".$itemss."','".$itemranges."','".$subranges."',".$packetsperbox.",".$packetweights.",".$prices.",".$qty.",".$qtypcs.",".$pcsprices.",".$qty.",'test user',now(),'".$cmtecc."','".$boxmrp."')";
$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'>");
stksnew(1);
printf("</div>");


printf("<div class='mnf'>");
printf("<b>Import FG-GTN-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='grntallyupload.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>
