<?php

require "showshp.php";

session_start();
if(!isset($_SESSION['titles']) || !isset($_SESSION['positions'])|| !isset($_SESSION['usernames'])){
header("location:prdlogings.php");
}


?>

<html>

<head>

<link rel="stylesheet" type="text/css" href="rdsty.css">
<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="dtps.js"></script>


<?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=6;
$state=0;
$unos=0;
$packetweights=0;

$csvf=$fl;

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

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

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

if(strlen($data[0])>0 && strlen($data[2])>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];
$values=0;

$discounts=0;
$discountvalues=0;

//if(strlen($data[10])>0){
$discounts=0;
//($data[10]*-1)*100/$values;
$discountvalues=0;
//$data[10];
//}

$netvalues=$values-$discountvalues;

$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[6],"/Box");
$prices=0;

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

#$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 from agentsmrd where agent_name='".$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];
}

$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 margin rates for items do not exist - %s<br>",$items);}
else{
$mgrst=mysqli_fetch_row($mgqry);
$marginrates=$mgrst[0];
}

#echo $rsqlst;

if($qty>0){
$details="Primary invoice";}
if($qty<=0){
$details="Un-delivered";}

$vsqlst="insert into rc_invoices(dates,sale_types,invoice_types,invoice_number,agent_names,agent_types,zone_orders,zones,regions,sub_regions,rsm_names,executive_names,territory_names,rep_names, ";
$vsqlst=$vsqlst." item_names,item_ranges,sub_ranges,packets_per_box,packet_weights,box_price,box_qty,discount_values, ";
$vsqlst=$vsqlst."grand_values,net_values,margin_rates,user_names,user_dates) values ('";
$vsqlst=$vsqlst.$dates."','Free issue invoice','".$details."', '".$invoice."','".$agent."','".$rsta[6]."','".$rsta[8]."','".$rsta[0]."', '".$rsta[1]."','".$rsta[9]."','". $rsta[2]."','".$rsta[3]."','".$rsta[7]."','".$rsta[4];
$vsqlst=$vsqlst."','".$items."', '".$itemranges."','".$subranges."',".$packetsperbox.",".$packetweights.",".$prices.",".$qty.",".
$discountvalues.",".$values.",".$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')">
<div class="hdn"></div>

<?php
printf("<div class='ses'>%s (%s)</div>",$_SESSION['titles'],$_SESSION['positions']);
?>

<div class="tops" align="center">Primary sales upload</div>
<div class="homes"><a href="prdlogings.php" style='color:#A9BCF5;'>Sign out</div>

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




showshps();


printf("</div><div class='srg'></div>");
printf("<div class='mains'><div class='tdt'>");

printf("<b>Import 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='fruploads.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>
