<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$tyrenum_veh='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();



$tablename='cf_dealercsv';
            
		if(isset($_POST['save'])){
		   			
			
			if(isset($_POST['filename'])){
			$filename=addslashes($_POST['filename']);
			}
			
			
			$import_file=strtolower($_FILES['import_file']['name']);
			$tmp_file=$_FILES['import_file']['tmp_name'];
			
			if($import_file!="")
			{
				$path=pathinfo($import_file);
				
				$ext=$path['extension'];
				$allowed=array('csv');
				if(in_array($ext,$allowed))
				{
						$uploaddir = 'import/';
						$uploadfile = $uploaddir.$import_file;
						if($fal=move_uploaded_file($tmp_file,$uploadfile))
						{
							$filename=$uploadfile;
							$id = fopen($filename, "r"); 
							while ($data = fgetcsv($id, filesize($filename))) 
								$table[] = $data; 
							fclose($id); 
							
							//echo "<table border='1'>\n";
							$num=1;
							foreach($table as $row)
							{
								if($num==1)
								{
									$num++;
									continue;
								}
								//echo "<tr>";
							   foreach($row as $data)
							   {
									$arr[]=$data;
									//echo "<td>$data</td>";
							   }
							   //if($arr[0]==="" || $arr[1]==="" || $arr[2]==="" ||$arr[3]==="" || $arr[4]==="")
							  /* if(trim($arr[2])=="" && trim($arr[4])=="")
							   {
									unset($arr);
									continue;
							   }*/
							   $item_id=trim($arr[0]);
							  //-----------------------------------------------------------------------------//
							 
							 $clientcode = $arr[0];
							 $dealername = $arr[1];
							 $conperson = $arr[2];
							 $location = $arr[3];
							 $address = $arr[4];
							 $phone = $arr[5];
							 
							 $chclient="SELECT * FROM cf_client WHERE client_code='".$clientcode."'";
							 $resclient=mysql_query($chclient);
							 
							 $row = mysql_fetch_array($resclient);
							 $clientsubid = $row['client_sub_id'];
							 $clientid = $row['client_id'];
							 
							 $chdistrict="SELECT * FROM cf_district WHERE district='".$location."'";
							 $resdistrict=mysql_query($chdistrict);
							 $rows1 = mysql_fetch_array($resdistrict);
							 $districtid = $rows1['district_id'];
							 
							 $query5="INSERT INTO cf_reseller (client_subid,client_id,client_code,res_name,re_consperson,re_location,re_address,re_phoneno,re_addate) 
									VALUES ('".$clientsubid."','".$clientid."','".$clientcode."','".$dealername."','".$conperson."','".$districtid."','".$address."','".$phone."','".date("Y-m-d")."')";
							 mysql_query($query5) or die("Some Data base Error".mysql_error());
							
							 unset($arr);
								//echo "<td>$query</td></tr>\n";
							} 
							/*$chk_qry52="SELECT * FROM tbl_menucsv WHERE item_id='".$item_id."'";
							   $chk_res52=mysql_query($chk_qry52);
							   if($chk_res52 && mysql_num_rows($chk_res52)==0)
							   {*/
							 $queryin="INSERT INTO cf_dealercsv (filename,add_date) 
															VALUES ('".trim($import_file)."','".date("Y-m-d")."')";
							 mysql_query($queryin) or die("Some Data base Error".mysql_error());
								//}
							//@unlink($filename);
							echo "<script>alert('Succesfully Uploaded to Database');</script>";
							echo "<script> document.location='export-file.php?user=".$user."';</script>";
							exit;
							
							//echo "</table>\n";
						}
						else
						{
							echo "<script>alert('Could not upload the file');</script>";
							echo "<script> document.location='export-file.php?user=".$user."';</script>";
							exit;
						}
		
				}
				else
				{
					echo "<script> document.location='export-file.php?user=".$user."';</script>";
					exit;
				}
			}
			else
			{
				echo "<script> document.location='export-file.php?user=".$user."';</script>";
				exit;
			}
			
			$today = date('Y-m-d');
															
			$sqlObj->tablename=$tablename;
			if($townname!=''){
			$sqlObj->fieldname='(filename,date)';
			$sqlObj->fieldvalue='"'.$filename.'","'.$today.'"';
			
			echo $qsy= "insert into ".$tablename.$sqlObj->fieldname." values(".$sqlObj->fieldvalue.")";
								
			$CreateClient 	= mysql_query($qsy);
			
			
			}
			
			if(isset($_REQUEST['page'])){
			$page=$_REQUEST['page'];
			header("location:route_list.php?page=$page");
			}else{
			header("location:route_list.php");
			}
}

			$districtname='';
			$districtrate='';
			$distance='';
			$distclientcode='';
						
			if(isset($_REQUEST['districtid'])){
				$id=$_REQUEST['districtid'];
				$tablename='cf_district';
				$sqlObj->tablename="cf_route";
				$condition=" where district_id='$id'";
				$sqlObj->condition=$condition;
				$sqlObj->fieldvalue="*";
				$fieldvalue=$sqlObj->fieldvalue;
				$sql=$sqlObj->SelectTable();
				
				$clientcode= $sql['route_client_code'];
				$clientname= $sql['route_client_name'];				
				$routename= $sql['route_name'];
								
				
				if(isset($_POST['client_code'])){
				$client_code=addslashes($_POST['client_code']);
				}
				
				if(isset($_POST['clientname'])){
				$clientname=addslashes($_POST['clientname']);
				}
				
				if(isset($_POST['routename'])){
				$routename=addslashes($_POST['routename']);
				}
				
				$sqlObj->tablename=$tablename;
				if(isset($_POST['client_code']))
				{
					$result=mysql_query("update cf_route set route_client_code = '$client_code',route_client_name='$clientname',route_name='$routename' where route_id='$id'");
					header("location:route_list.php");
				}
						
			}
			


?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Transport Integrated Logistic System</title>
<link href="css/styles.css" rel="stylesheet"  />
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type="text/javascript" src="scripts_date/jquery.js"></script>
<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script src="js/addmoretown.js"></script>



<script type="text/javascript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
function validation(){
if(document.townname.townname.value==''){
alert("Enter the Town Name");
document.townname.townname.focus();
return false;
}

if(document.townname.rate.value==''){
alert("Enter the Rate");
document.townname.rate.focus();
return false;
}


/*var cnt=document.getElementById('divcnt').value;chasisnum1 enginenum1
for(i=1;i<=cnt;i++){alert(document.getElementById('vehiclenum'+i).value);
if(document.getElementById('vehiclenum'+i)){
if(document.getElementById('vehiclenum'+i).value==''){
alert("Enter the Vehicle Number");
document.getElementById('vehiclenum'+i).focus();
return false;
}
}
}*/
}

function showClient(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","clientajax.php?str="+str,true);
xmlhttp.send();
}


</script>


<style>
.lableentrytext{
font-size: 12px;
color: #505050;
clear: both;
margin-bottom: 5px;
text-transform: capitalize;
}
</style>
</head>

<body>
<div class="wrapper">
  <div>
<?php 
	include("header.php");
?>
 
</div>
 
 
 <div class="tabbedArea">
  
  <div class="tabBody">
   <h1>Add Dealer Csv</h1>
   <form name="routename" method="post" action="" enctype="multipart/form-data">
   <?php
	$div=1;
	?>
<input type="hidden" id="divcnt" value="<?=$div?>" name="cnt"/>
   <div class="addEmp" style="margin-bottom:0px; border-bottom:0px solid;width:1000px;">
        <table width="70%" border="0" cellspacing="0" cellpadding="10">
          <tr>
            <td valign="top">
            <div class="form1">
            
                                  
           <div>     
      			<p class="input1">Dealer Csv<span style="color:#FF3300">*</span></p><input name="import_file" type="file" class="content" id="import_file">  
     	   </div>
           
          
          </div>
        </td>
            <td valign="top">
            <div class="form1">
            <div>
           		<input name="save" type="submit" class="input1"  value="Save" />
            </div>
            </div></td>
          </tr>
        
        </table>
	
	
        
<table  border="0" cellspacing="0" cellpadding="0" class="tableList">
  <tr class="heading">
    <td>No</td>    
    <td>Csv Filename</td>
    <td>Add Date</td>
    <td>Option</td>
  </tr>
 <?php
//$array=mysql_query("select * from tbl_bill_amnt order by bill_no asc");
$i=1;
$filename='';
$sq_arr="select * from cf_dealercsv where 1 ";
/* if(isset($_REQUEST['search'])){
	   if(isset($_REQUEST['files'])){
	  $filename=$_REQUEST['files'];
	   }
	   }
if($filename!=''){
$sq_arr.=" and filename like '".$filename."%' ";
}

if($sdate_12!=''){
$sq_arr.=" and date='".$sdate."' ";
}*/
$sq_arr.=" order by dealercsv_id desc ";
$query=$sq_arr;
$function="Searchdate";
include("paging.php");
$sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=10;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
while($res=mysql_fetch_array($array))
{
/*$dat_today=$res['date'];
$dat_today=explode("-",$dat_today);*/
?>	
 <tr>
 	<td><?=$i;?></td>
    <td><?=$res['filename'];?></td>
    <td><?=$res['add_date'];?></td>  
    <td class="tr_td"><a href="import/<?=$res['filename'];?>"><font color="#000000">Click</font></a></td> 
 </tr>
 <?php
$i++;
}?>
<tr><td colspan="4" align="right"><?=$pagination?></td></tr>
<?
}
?>  
</table>
       

<div class="mand"><p>Fields marked <span style="color:#FF3300">*</span> are Mandatory</p></div>
</div>
   </form>
   </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<div class="quicLInksCover">
  <?php 
	include("leftlink.php");
?>

</div>
</body>
</html>


		






