<?php
/********
Amitai Sasson
tcpdump to ascii conversion

grabFile.php
This is the file that does
all the cleanup, stores the file and uploads 
the information into the database.
*********/
include ("header.php");

//global $strDesc; 
global $fileUpload; 
global $fileUpload_name; 
global $fileUpload_size; 
global $fileUpload_type;

echo $HTTP_POST_VARS["frmUploadFile"];

#
// Read the file contents
#
$path1 = $_FILES['fileUpload']['tmp_name'][0];
//echo $path1;
// Make sure both a description and 
// file have been entered 
if($fileUpload == "none")
die("You must enter a file");

$sConn = mysql_connect($dbServer, $dbUser, $dbPass) 
or die("Couldn't connect to database server"); 

$dConn = mysql_select_db($dbDatabase, $sConn) 
or die("Couldn't connect to database $dbDatabase");

$row = 0; //number of rows

//insert into database
$dbSet = "DROP TABLE IF EXISTS amazonVOrders";
mysql_query($dbSet) or die("Couldn't add file to database");

$dbQuery = "CREATE TABLE `amazonVOrders` (
			`primary` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
			`orderID` VARCHAR( 54 ) NOT NULL, 
			`SKU` VARCHAR( 54 ) , 
			`artSKU` VARCHAR( 54 ) , 
			`frameSKU` VARCHAR( 54 ) , 
			`UPC` VARCHAR( 54 ) , 
			`ASIN` VARCHAR( 54 ) , 
			`Title` TEXT( 256 ) ,
			`qty` INT( 24 ) ,
			`galleryPrice` DECIMAL ( 16,2 ) ,
			`salePrice` DECIMAL ( 16,2 ) ,
			`DC` VARCHAR( 54 ) 
			) TYPE = MYISAM ;";
			
mysql_query($dbQuery) or die("Couldn't create table to database");
	
$handle = fopen($path1, "r");
$arrContent = file($path1);
$numLines = count( $arrContent ); // Count the elements in the array
$row = 0; //number of rows
//$cost = $data[19];
//$sumCost = 0;
/*$desc = $data[2];
$credit = $data[4];
echo "order # " . $orderid . " desc: " . $desc . " amount: " . $credit . "<br>";*/
$previousid = "";
$previousCostship = 0;

while (($data = fgetcsv($handle, 1000, "	")) !== FALSE) 
{
$artSKU = "";
$frameSKU = "";
$tileSKU = "";
	
	if ($row > 0)
	{	
	//echo "in the while loop";
	//$name = addslashes($data[1]);
	if ($data[0] != "")
		$orderID = addslashes($data[0]);
    else $orderID = "";
	
/*	if ($data[1] != "")
		$lineNumber = $data[1];
	else $lineNumber = 1;
*/
	if ($data[1] != "")
		$upc = $data[1];
	else $upc = 1;

	if ($data[2] != "")
		$SKU = $data[2];
	else $SKU = "";

	if ($data[3] != "")
		$ASIN = $data[3];
	else $ASIN = "";
	
	if ($data[4] != "")
		$Title = addslashes($data[4]);
	else $Title = "";

	if ($data[5] != "")
	{
		$galleryPrice = $data[5];
		$galleryPrice= preg_replace('/[\$,]/', '', $galleryPrice);
	}

	if ($data[7] != "")
	{
		$salePrice = $data[7];
		$salePrice= preg_replace('/[\$,]/', '', $salePrice);
	}
	
	if ($data[8] != "")
		$qty = $data[8];
	else $qty = "";
	
	if ($data[22] != "")
		$DC = $data[22];
	else $DC = "";
		
	$pos = strpos($SKU, "FR-");
	$pos2 = strpos($SKU, "-GAL");
	if ($pos)
	{
	$artSKU = substr($SKU,0,$pos-1);
	$frameSKU = substr($SKU,$pos); //find the frame part number
	}
	else if ($pos2)
	{
	$artSKU = substr($SKU,0,$pos2);
	$frameSKU = substr($SKU,$pos2+1); //find the frame part number
	}
	else
	{
	$artSKU = $SKU;
	$frameSKU = "";	
	}
	
	//update amazon Vendor POs table
	$query_amazonV = "INSERT INTO `amazonVOrders` (orderID,SKU,artSKU,frameSKU,UPC,ASIN,Title,qty,galleryPrice,salePrice,DC) VALUES ('$orderID','$SKU','$artSKU','$frameSKU','$upc','$ASIN','$Title','$qty','$galleryPrice','$salePrice','$DC');";
	echo "row #" . $row . " the query: " . $query_amazonV . "<br>";
	mysql_query($query_amazonV); // or die("Couldn't add row2 $name");
	}
	
	$row++;   
}
//echo "order # " . $orderid . " Ship Cost: " . $cost . "<br>";
/*$query = "UPDATE `manual_orders` SET cost = '$sumCost' WHERE orderid = '$prev_orderid' LIMIT 1; ";
mysql_query($query) or die("Couldn't add row 1"); 
*/
fclose($handle);
?>