<?php
include_once('include.php');
session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:index.php");
}
if(isset($_SESSION['usernames'])){
$usrr=$_SESSION['usernames'];}
?>

<!doctype html>
<html lang="en">
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<ol class="breadcrumb">
<li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
<li class="active">Dashboard</li>
</ol>
</section>
<br>
<?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","salehljr","!2n+l)a3iX~U","salehljr_budgeting");
$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, 10000, ",")) !== FALSE) {
$rows++;

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

$arr=explode("/",$data[0]);
if(count($arr)>1){
$dates=$arr[2]."-".$arr[1]."-".$arr[0];
}
$invoice=$data[2];
$agent=$data[1];
$porefferance=$data[3];
$grnnaration=$data[4];
$ponaration=$data[5];
$grnvalues=$data[8];
$grngrossvalue=$data[9];
$grnvatval=$data[11];
$grnnbtval=$data[10];
$itemcheck=$data[1];

$sqlstn="delete from grn_details where grn_number='".$invoice."' and grn_date='".$dates."'";
$qry=mysqli_query($conn,$sqlstn);

$sqlst="select grn_number from grn_details where grn_number='".$invoice."' and grn_date='".$dates."' and grn_itemname='".$itemcheck."' ";
#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'){
//if($state==1 && strlen($data[2])>0 && strlen($data[4])>0){
$unos++;
$items=$data[1];
$qty=$data[6];
$qtyrate=$data[7];
$grnitemval=(trim($data[8]))*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 poitemlist where item_name='".$items."'";
//$rqry=mysqli_query($conn,$rsqlst);
//if(mysqli_num_rows($rqry)==0){
//printf("following products 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 grn_details(grn_date,customer_names,grn_number,po_referance,grn_naration,po_naration,grn_itemname,grn_qty,grn_rates,grn_values,grn_gross,grn_vat,grn_nbt) VALUES('".$dates."','".$agent."','".$invoice."','".$porefferance."','".$grnnaration."','".$ponaration."','".$items."','".$qty."','".$qtyrate."','".$grnvalues."','".$grngrossvalue."','".$grnvatval."','".$grnnbtval."')";
$aqry=mysqli_query($conn,$sqlstv);
#echo $sqlstv;
}
}}}

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

</head>
<body style="font-family:calibri;font-size:small" onload="mnh('amnc','agentsb')">
</body>
<br>
<div class="alert alert-info" role="alert">
<h4 class="alert-heading">GRN Upload!</h4>
<p>Notes, Please download detailed GRN data From Tally ERP System</p>
<hr>
<p class="mb-0">Before you upload GRN to web system, be sure to save file as CSV File format and SET coulmns According to required cell format mode to keep things nice and tidy.</p>
</div>

<form role="form" enctype="multipart/form-data" method="POST" action="tallygrnupload.php?actp=0">
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="files" name="files">
<p class="help-block">Please follow above instruction before data uploads.</p>
</div>
<!-- /.box-body -->
<button type="submit" class="btn btn-primary" name="file_submit" value="Go" >Submit</button>
</div>
</form>
</div>
<?php include_once('footer.php');?>
</body>
</html>