<?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>Prasanna Mallawa</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;
$packetweights=0;

$csvf=$fl;

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



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

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

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


$agent=str_replace("'","\'",trim($data[1]));
$invoice=$data[2];
$voucherr=$data[3];
$values=$data[8];
$naration=$data[4];

$discounts=0;
$discountvalues=0;
$netvalues=0;
$disrates=0;

if(strlen($data[11])>0){
$discounts=($data[11]*-1)/$values;
$discountvalues=trim($data[11]," Dr");
}
$netvalues=$values-$discountvalues;
$disrates=$discounts;


$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[0])==0 && strlen($data[3])==0 && $adstate==0){
$unos++;
$items=trim($data[1]);
$qty=trim($data[5]," Box");
$prices=trim($data[7],"/Box");
$qtpcs=trim($data[6]," pcs");

$qty=trim($qty,"/nos");
$prices=trim($prices,"/nos");

#$qty=number_format($qty,2);
#$prices=number_format($prices,2);

$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;

$packetweights=0;

$rsqlst="select distinct item_name,item_ranges,sub_ranges,(packets_per_box*pcs_per_packet),packet_weight from itemsmrd where item_name='".$items."'";
#echo $rsqlst;
$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];
$subranges=$trst[2];
$packetsperbox=$trst[3];
$packetweights=$trst[4];
$packprice=$prices/$packetsperbox;
}

$mgsqlst="select distinct margin_rates from item_margins where item_names='".$items."' ";
$mgqry=mysqli_query($conn,$mgsqlst);
if(mysqli_num_rows($mgqry)==0){
printf("following Product cost rates for items do not exist - %s<br>",$items);}
else{
$mgrst=mysqli_fetch_row($mgqry);
$marginrates=$mgrst[0];
}

#echo $rsqlst;

if($qty>0){
$details="Tax Invoice";
$odsts="PENDING";
}
if($qtpcs>0){
$details="Tax Invoice";
$odsts="PENDING";
}


$vsqlst="insert into rc_invoices(dates,sale_types,invoice_types,invoice_number,voucher_number,naration,agent_names,agent_types,zone_orders,zones,regions,sub_regions,rsm_names,executive_names,territory_names,chanell,rep_names, ";
$vsqlst=$vsqlst." item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,box_price,pcs_price,box_qty,pcs_qty,discount_values, ";
$vsqlst=$vsqlst."grand_values,discount_rates,net_values,margin_rates,user_names,user_dates) values ('";
$vsqlst=$vsqlst.$dates."','Primary Order','".$details."','".$invoice."','".$voucherr."', '".$naration."','".$rsta[5]."','".$rsta[6]."','".$rsta[8]."','".$rsta[0]."', '".$rsta[1]."','".$rsta[9]."','". $rsta[2]."','".$rsta[3]."','".$rsta[7]."','".$rsta[10]."','".$rsta[4];
$vsqlst=$vsqlst."','".$items."', '".$itemranges."','".$subranges."',".$packetsperbox.",".$packetweights.",".$prices.",".$packprice.",".$qty.",".$qtpcs.",".
$discountvalues.",".$values.",".$disrates.",".$netvalues.",".$marginrates."";
$vsqlst=$vsqlst.",'test user',now())";
#echo $vsqlst;
$aqry=mysqli_query($conn,$vsqlst);
}

}}}

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'>");
stkssdash(1);
printf("</div>");


printf("<div class='mnf'>");

printf("<b>Import TAX Invoices form from Tally system<br></b>");
printf("The file type should be CSV. Export in detailed view with db-codes<br><br>");
printf("Select the file:");

printf("<form  enctype='multipart/form-data' method='POST' action='taxinv_upload.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>
