<?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");
include ("config.php");

$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
$data = ''; //start clean data

/*$header1 = "Order Number	Line number	Order Date	RCI SKU	Vendor SKU	pnt pn	SKU Description	Quantity	Ship Method	Billing Name	Shipping Name	Shipping Address Line 1	Shipping Address Line 2	Shipping City	Shipping State	Shipping Zip	Shipping Phone	Price" . " \n";*/
$header1 = "Customer	Order/PO Number	External ID	Model Number	Pnt	ASIN	New Title	Cost	New Price	Quantity Ordered	Fulfillment Center" . " \n";

$count = 1;
$query1 = "SELECT * FROM `amazonVOrders`";
$result = mysql_query($query1);
while($myrow = mysql_fetch_array($result))
     {
		$orderID = $myrow["orderID"];
		$SKU = $myrow["SKU"];
		$artSKU = $myrow["artSKU"];
		$frameSKU = $myrow["frameSKU"];
		$UPC = $myrow["UPC"];
		$ASIN = $myrow["ASIN"];
		$Title = $myrow["Title"];
		$qty = $myrow["qty"];
		$DC = $myrow["DC"];
		$totalPrice = $myrow["salePrice"];		
		$Title = $Title . " (" . $ASIN . ")"; 
		$customerName = "Amazon Commercial";
		
		
		if ($lastOrder == $orderID)
				$cntLine++;
		else 
		{ 
		$cntLine=1;
		$lastOrder = $orderID; 
		}

		if($frameSKU == "")
		{
		$price = number_format($totalPrice,2,'.',',');
		$line = $customerName . "	" . $orderID . "	" . $UPC . "	" . $SKU . "	" . $artSKU . "	" . $ASIN . "	" . $Title . "	" . number_format($totalPrice,2,'.',',') . "	" . $price . "	" . $qty  . "	" . $DC . " \n"; 
	   //start line for excell output.
	   $data .= $line ;
	   $cntLine++;
		}
		else
		{
		$price = $totalPrice/2;
		$price = number_format($price,2,'.',',');
		$line = $customerName . "	" . $orderID . "	" . $UPC . "	" . $SKU . "	" . $artSKU . "	" . $ASIN . "	" . $Title . "	" . number_format($totalPrice,2,'.',',') . "	" . $price . "	" . $qty  . "	" . $DC . " \n"; 
	   //start line for excell output.
	   $data .= $line ;
	   $cntLine++;
	  	$line = $customerName . "	" . $orderID . "	" . $UPC . "	" . $SKU . "	" . $frameSKU . "	" . $ASIN . "	" . $Title . "	" . number_format($totalPrice,2,'.',',') . "	" . $price . "	" . $qty  . "	" . $DC . " \n"; 
		$data .= $line ;
		}
		$count++;
}

header("Content-type: text/xml");
//can change the name of file here to the 
header("Content-Disposition: attachment; filename=amazonVOrders_" . date("Ymd") . ".txt");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: application/x-msdownload");
echo $header1 . $data;
?>