//error_reporting(E_ALL);
assert($_REQUEST['login']);
error_reporting(0);
$debug=$_GET["debug"];
$magentoorderid=$_GET["magentoorderid"];
$db=mysql_connect("localhost","shopdive_mmcnatt","Markmcna77");
mysql_select_db("shopdive_sg");
//which orders to process
$q="DROP TABLE IF EXISTS aacatchordertemp1;";
$r=mysql_query($q);
$q="CREATE TEMPORARY TABLE aacatchordertemp1 AS SELECT DISTINCT increment_id
FROM 9eu_sales_flat_order WHERE created_at >DATE_ADD(NOW(), INTERVAL -31 DAY) AND STATUS IN ('processing');";
$r=mysql_query($q);
$q="ALTER TABLE aacatchordertemp1 ADD ind INT;";
$r=mysql_query($q);
$q="UPDATE aacatchordertemp1 a, catchorderprocess b SET a.ind=1 WHERE b.magentoorderid=a.increment_id;";
$r=mysql_query($q);
//#select * from aacatchordertemp1 where ind is null
$q="INSERT INTO catchorderprocess SELECT DISTINCT increment_id,NULL FROM aacatchordertemp1 WHERE ind IS NULL;";
$r=mysql_query($q);
if(strlen($magentoorderid)>0){
$q="update catchorderprocess set processdate=null where magentoorderid='$magentoorderid';";
$r=mysql_query($q);
}
//#delete from catchorderprocess where magentoorderid in ('100000032','100000033')
//#SELECT * FROM 9eu_sales_flat_order WHERE increment_id LIKE '%33';
$q="select min(magentoorderid) minoid,count(*) cnt from catchorderprocess where processdate IS NULL;";
$r=mysql_query($q);
$rr=mysql_fetch_assoc($r);
$minoid=$rr["minoid"];
$cntorderstoinsert=$rr["cnt"];
if($debug==1){echo "ORDERS to process:".$cntorderstoinsert."";}
if(strlen($cntorderstoinsert)>0 & strlen($minoid)>0){
//#order master
$q="SELECT increment_id AS magentoorderid,
status,shipping_method,customer_id,
base_discount_amount,
base_shipping_amount,
base_grand_total,base_subtotal,base_tax_amount,base_total_invoiced,base_total_paid,
grand_total,
shipping_amount,shipping_tax_amount,
subtotal,subtotal_invoiced,total_invoiced,total_paid
FROM 9eu_sales_flat_order
WHERE increment_id IN (SELECT magentoorderid FROM catchorderprocess where processdate IS NULL);";
$r=mysql_query($q);
//if($debug==1){echo "ordermasterQ:
".$q;}
$string="";
while ($rr=mysql_fetch_assoc($r)){
$string .= "
".$rr["magentoorderid"]."|".$rr["status"]."|".$rr["shipping_method"]."|".$rr["customer_id"]."|".$rr["base_discount_amount"]."|".$rr["base_shipping_amount"]."|".$rr["base_grand_total"]."|".$rr["base_subtotal"]."|".$rr["base_tax_amount"]."|".$rr["base_total_invoiced"]."|".$rr["base_total_paid"]."|".$rr["grand_total"]."|".$rr["shipping_amount"]."|".$rr["shipping_tax_amount"]."|".$rr["subtotal"]."|".$rr["subtotal_invoiced"]."|".$rr["total_invoiced"]."|".$rr["total_paid"];
}
if($debug==1){echo "
ordermaster:".$string;}
$myfile = "orders/ordermaster".$minoid.".txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $string);
fclose($fh);
//#order detail
$q="SELECT DISTINCT
increment_id AS magentoorderid,
9eu_sales_flat_order_item.price,
9eu_sales_flat_order_item.sku,
9eu_sales_flat_order_item.qty_ordered,
9eu_sales_flat_order_item.row_total,
9eu_sales_flat_order_item.weight ,
9eu_sales_flat_order_item.product_type
FROM 9eu_sales_flat_order
INNER JOIN 9eu_sales_flat_order_item ON 9eu_sales_flat_order_item.order_id = 9eu_sales_flat_order.entity_id
WHERE increment_id IN (SELECT magentoorderid FROM catchorderprocess where processdate IS NULL);";
$r=mysql_query($q);
$string="";
while ($rr=mysql_fetch_assoc($r)){
$string .="
".$rr["magentoorderid"]."|".$rr["price"]."|".str_replace('|','',$rr["sku"])."|".$rr["qty_ordered"]."|".$rr["weight"]."|".$rr["row_total"]."|".str_replace('|','',$rr["product_type"]);
}
if($debug==1){echo "
orderdetail:".$string;}
$myfile = "orders/orderdetail".$minoid.".txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $string);
fclose($fh);
//shipping
$q="SELECT DISTINCT
9eu_sales_flat_order.increment_id AS magentoorderid,
9eu_sales_flat_order_address.address_type,
9eu_sales_flat_order_address.firstname,
9eu_sales_flat_order_address.lastname,
9eu_sales_flat_order_address.street,
9eu_sales_flat_order_address.city,
9eu_sales_flat_order_address.region,
9eu_sales_flat_order_address.postcode ,
9eu_sales_flat_order_address.country_id,
9eu_sales_flat_order_address.telephone,
9eu_sales_flat_order_address.fax,
9eu_sales_flat_order_address.company,
9eu_sales_flat_order_address.email
FROM 9eu_sales_flat_order
INNER JOIN 9eu_sales_flat_order_item ON 9eu_sales_flat_order_item.order_id = 9eu_sales_flat_order.entity_id
INNER JOIN 9eu_sales_flat_order_address ON 9eu_sales_flat_order_address.parent_id = 9eu_sales_flat_order.entity_id
WHERE increment_id IN (SELECT magentoorderid FROM catchorderprocess where processdate IS NULL);";
$r=mysql_query($q);
$string="";
while ($rr=mysql_fetch_assoc($r)){
$string .= "
".$rr["magentoorderid"]."|".str_replace('|','',$rr["address_type"])."|".str_replace('|','',$rr["firstname"])."|".str_replace('|','',$rr["lastname"])."|".str_replace('|','',$rr["street"])."|".str_replace('|','',$rr["city"])."|".str_replace('|','',$rr["region"])."|".str_replace('|','',$rr["postcode"])."|".$rr["country_id"]."|".str_replace('|','',$rr["telephone"])."|".str_replace('|','',$rr["fax"])."|".str_replace('|','',$rr["company"])."|".str_replace('|','',$rr["email"]);
}
if($debug==1){echo "
ordershipping:".$string;}
$myfile = "orders/ordershipping".$minoid.".txt";
$fh = fopen($myfile, 'w') or die("can't open file");
fwrite($fh, $string);
fclose($fh);
}
$q="update catchorderprocess set processdate=now() WHERE processdate IS NULL;";
$r=mysql_query($q);
mysql_close($db);
?>