<?php
require "racs.php";
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");}
?>
<html>
<title>RPM-Asriel</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_asriel");
$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[3])>0 && strlen($data[4])>0 && $rows>=8){
$adstate=1;
$state=0;
$dates=0;

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

$sqlstn="delete from rc_invoices where invoice_number='".$invoice."'";
$qry=mysqli_query($conn,$sqlstn);

$sqlst="select invoice_number from rc_invoices 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[2])>0 && strlen($data[4])>0){
$unos++;
$items=$data[1];
$qty=$data[8];
$grval=(trim($data[9]))*1;

//$qtypcs=(trim($data[5]," pcs"))*1;
//$prices=trim($data[6],"/Box");


$rsqlst="select distinct item_name,item_ranges,packets_per_box,packet_weight from itemsmrd where item_name='".$items."'";
$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);
$itemranges=$trst[1];
$packetsperbox=$trst[2];
$packetweights=$trst[3];
}

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

$sqlstv="INSERT INTO rc_invoices(dates,invoice_number,item_names,item_ranges,packets_per_box,packet_weights,box_qty,grand_values,zones,regions,executive_names,stores_names,territory_names,zone_orders,sub_regions,chanell) VALUES('".$dates."','".$invoice."','".$items."','".$itemranges."','".$packetsperbox."','".$packetweights."','".$qty."','".$grval."','".$rsta[0]."','".$rsta[1]."','".$rsta[2]."','".$rsta[3]."','".$rsta[4]."','".$rsta[5]."','".$rsta[6]."','".$rsta[7]."')";
$aqry=mysqli_query($conn,$sqlstv);
#echo $sqlstv;
}
}}}

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

</head>
<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
</body>
</html>
<?php
printf("<div class='mnf'>");
printf("<b>Import GRN Invoices form from Tally 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='asriel.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>
