<?
ob_start();
session_start();
if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}
$page=1;
$fuel_no='';
$sdate='';
$singdate='';
$user='';
$filename='';
$sdate_12='';
$sdate='';

$img_hr="images/menu4.jpg";
$img_acc="images/menu3.jpg";
$img_veh="images/menu2.jpg";
$img_con="images/menu1-o.jpg";
include("include/config.php");
//---------------------------------------------

if(isset($_POST['save']))
{
	$import_file=strtolower($_FILES['import_file']['name']);
	$tmp_file=$_FILES['import_file']['tmp_name'];
	if(isset($_REQUEST['user'])){
	$user=$_REQUEST['user'];
	}
	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]);
					 
					  //-----------------------------------------------------------------------------//
					 
					
					  
					 
						//code to check see whther its a new category_name or not
						//--------------------------first insert into delivery cost---------------------------------//
					$chk_qrydd3="SELECT * FROM tbl_deliveryarea  WHERE ref_no='".$item_id."'";
					   $chk_resdd3=mysql_query($chk_qrydd3);
					 	
					   if($chk_resdd3 && mysql_num_rows($chk_resdd3)!=0)
					   {
					    	$up_qrydd3="UPDATE tbl_deliveryarea SET from_location='".addslashes(trim($arr[1]))."',to_location='".addslashes(trim($arr[2]))."',delivery_cost='".addslashes(trim($arr[3]))."',driver_cost='".addslashes(trim($arr[4]))."',cleaner_cost='".addslashes(trim($arr[5]))."',add_date='".date('Y-m-d')."' WHERE ref_no='".$item_id."'";
							$up_resdd3=mysql_query($up_qrydd3) or die("Some Data base Error in Updation".mysql_error());
					   }
					   if($chk_resdd3 && mysql_num_rows($chk_resdd3)==0)
					   {
					   
					  		 $querydd3="INSERT INTO tbl_deliveryarea (ref_no,from_location,to_location,delivery_cost,driver_cost,cleaner_cost,add_date) 
													VALUES ('".addslashes(trim($arr[0]))."','".addslashes(trim($arr[1]))."','".addslashes(trim($arr[2]))."','".addslashes(trim($arr[3]))."','".addslashes(trim($arr[4]))."','".addslashes(trim($arr[5]))."','".date('Y-m-d')."')";
							 mysql_query($querydd3) or die("Some Data base Error".mysql_error());
							 //$in_id=mysql_insert_id();
							
						} 
					 //---------------------------------------------------------------------------------------// 
						
						unset($arr);
						//echo "<td>$query</td></tr>\n";
					} 
					
					
					//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;
	}
	
	$queryin="INSERT INTO tbl_deliverycsv (filename,dates) 
													VALUES ('".trim($import_file)."','".date("Y-m-d")."')";
							 mysql_query($queryin) or die("Some Data base Error".mysql_error());
	
	
}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Brothers - ERP</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="style/default.css">

</head>
<body>

<!--start main div-->
<div id="main_div">
<? include("header.php");?>

<div style="width:1024px; float:left; clear:none">
  <div id="matter_center_in">
 
	
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	  <td>&nbsp;</td>
	  </tr>
  <tr><td colspan="2"><table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <th width="8%" height="41" align="left" valign="middle" class="tr_td">No</th>
      <th width="74%" align="left" valign="middle" class="tr_td">File Name</th>
    <th colspan="2" align="left" valign="middle" class="tr_td">OPTION</th>
      </tr>

<?php
//$array=mysql_query("select * from tbl_bill_amnt order by bill_no asc");
$i=1;
$filename='';
$sq_arr="select * from tbl_deliverycsv 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 dates='".$sdate."' ";
}
$sq_arr.=" order by drv_id desc ";
$query=$sq_arr;
$function="Searchdate";
include("paging.php");
$sq_arr.=" LIMIT $start, $limit";

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
while($res=mysql_fetch_array($array))
{
$dat_today=$res['dates'];
$dat_today=explode("-",$dat_today);
?>	
    <tr>
      <td class="tr_td"><?php echo $i; ?></td>
      <td class="tr_td"><?php echo $res['filename'];?></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></td></tr>
</table>

	<? 
include("container_bottom_footer.php");?>
  </div>
  <?php include("footer.php");?>

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