<?php
require_once'Mage/Adminhtml/controllers/Sales/OrderController.php';
class EM_DeleteOrder_Adminhtml_Sales_OrderController extendsMage_Adminhtml_Sales_OrderController
{
publicfunction reportorderAction(){
$orderIds=$this->getRequest()->getPost('order_ids');
$write =Mage::getSingleton('core/resource')->getConnection('core_write');
$table_sales_flat_order =Mage::getSingleton('core/resource')->getTableName('sales_flat_order');
header("content-type:application/octet-stream");
header("accept-ranges: bytes");
header("accept-length: ".strlen($outstr));
header("content-disposition: attachment;filename=order_detail.csv");
echo
"orders_id,";
echo
"order_no,";
echo
"customers_id,";
echo
"customers_email_address,";
echo
"customers_phone,";
echo
"customers_name,";
echo
"customers_street_address,";
echo
"customers_city,";
echo
"customers_postcode,";
echo
"customers_state,";
echo
"customers_country,";
echo
"delivery_name,";
echo "delivery_street_address,";
echo
"delivery_city,";
echo
"delivery_postcode,";
echo
"delivery_phone,";
echo
"delivery_state,";
echo
"delivery_country,";
echo
"payment_method,";
echo
"date_purchased,";
echo
"orders_status_name,";
echo
"currency,";
echo
"products_id,";
echo
"products_model,";
echo
"products_name,";
echo
"products_price,";
echo
"products_tax,";
echo
"final_price,";
echo
"products_quantity,";
echo
"order_subtotal,";
echo
"coupon_code,";
echo
"discount_coupon,";
echo
"group_discount,";
echo
"gift_certificates,";
echo
"shipping_method,";
echo
"shipping,";
echo
"tax,";
echo
"grand_total,\n";
if(!empty($orderIds)){
foreach ($orderIds as $orderId){
$query=null;
$order =Mage::getModel('sales/order')->load($orderId);
if($order){
$query="SELECT
o.entity_id
AS orders_id,
o.increment_id
ASorder_no,
oab.customer_id
AScustomers_id,
o.customer_email
AS customers_email_address,
oab.telephone
AS customers_phone,
CONCAT(oab.lastname,' ' ,oab.firstname) AScustomers_name,
oab.street AS customers_street_address,
oab.city AS customers_city,
oab.postcode AS customers_postcode,
oab.region AS customers_state,
oab.country_id AS customers_country,
CONCAT(oas.lastname,' ' ,oas.firstname) ASdelivery_name,
oas.street AS delivery_street_address,
oas.city AS delivery_city,
oas.postcode AS delivery_postcode,
oas.telephone AS delivery_phone,
oas.region AS delivery_state,
oas.country_id AS delivery_country,
'' AS payment_method,
o.created_at AS date_purchased,
o.status AS orders_status_name,
o.order_currency_code AS currency,
oi.product_id AS products_id,
oi.sku AS products_model,
oi.name AS products_name,
oi.price AS products_price,
oi.tax_amount AS products_tax,
oi.row_total AS final_price,
o.total_qty_ordered AS products_quantity,
o.subtotal AS order_subtotal,
o.coupon_code AS coupon_code,
'' AS discount_coupon,
'' AS group_discount,
'' AS gift_certificates,
o.shipping_method AS shipping_method,
o.shipping_amount AS shipping,
o.tax_amount AS tax,
o.grand_total AS grand_total
FROM sales_flat_order AS o
LEFT JOIN sales_flat_order_address AS oab
ON o.billing_address_id = oab.entity_id
LEFT JOIN sales_flat_order_address AS oas
ON o.shipping_address_id = oas.entity_id
INNER JOIN sales_flat_order_item AS oi
ON o.entity_id = oi.order_id
WHERE
o.increment_id IN('".$order->getIncrementId()."');";
$rs=$write->fetchAll($query);
foreach ($rs as $r)
{
echo $r['orders_id'].',';
echo $r['order_no'].',';
echo $r['customers_id'].',';
echo $r['customers_email_address'].',';
echo $r['customers_phone'].',';
echo $r['customers_name'].',';
echo $r['customers_street_address'].',';
echo $r['customers_city'].',';
echo $r['customers_postcode'].',';
echo $r['customers_state'].',';
echo $r['customers_country'].',';
echo $r['delivery_name'].',';
echo $r['delivery_street_address'].',';
echo $r['delivery_city'].',';
echo $r['delivery_postcode'].',';
echo $r['delivery_phone'].',';
echo $r['delivery_state'].',';
echo $r['delivery_country'].',';
echo $r['payment_method'].',';
echo $r['date_purchased'].',';
echo $r['orders_status_name'].',';
echo $r['currency'].',';
echo $r['products_id'].',';
echo $r['products_model'].',';
echo $r['products_name'].',';
echo $r['products_price'].',';
echo $r['products_tax'].',';
echo $r['final_price'].',';
echo $r['products_quantity'].',';
echo $r['order_subtotal'].',';
echo $r['coupon_code'].',';
echo $r['discount_coupon'].',';
echo $r['group_discount'].',';
echo $r['gift_certificates'].',';
echo $r['shipping_method'].',';
echo $r['shipping'].',';
echo $r['tax'].',';
echo $r['grand_total'].",\n";
}
}
}
}
}
publicfunction deleteorderAction()
{
$orderIds =$this->getRequest()->getPost('order_ids');
$flag = false;
$write =Mage::getSingleton('core/resource')->getConnection('core_write');
$query="show tables";
$rsc_table=$write->fetchCol($query);
$table_sales_flat_order =Mage::getSingleton('core/resource')->getTableName('sales_flat_order');
$table_sales_flat_creditmemo_comment=Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_comment');
$table_sales_flat_creditmemo_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_item');
$table_sales_flat_creditmemo=Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo');
$table_sales_flat_creditmemo_grid=Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_grid');
$table_sales_flat_invoice_comment=Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_comment');
$table_sales_flat_invoice_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_item');
$table_sales_flat_invoice=Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice');
$table_sales_flat_invoice_grid=Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_grid');
$table_sales_flat_quote_address_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address_item');
$table_sales_flat_quote_item_option=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_item_option');
$table_sales_flat_quote=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote');
$table_sales_flat_quote_address=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$table_sales_flat_quote_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_item');
$table_sales_flat_quote_payment=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_payment');
$table_sales_flat_shipment_comment=Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_comment');
$table_sales_flat_shipment_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_item');
$table_sales_flat_shipment_track=Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_track');
$table_sales_flat_shipment=Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment');
$table_sales_flat_shipment_grid=Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_grid');
$table_sales_flat_order_address=Mage::getSingleton('core/resource')->getTableName('sales_flat_order_address');
$table_sales_flat_order_item=Mage::getSingleton('core/resource')->getTableName('sales_flat_order_item');
$table_sales_flat_order_payment=Mage::getSingleton('core/resource')->getTableName('sales_flat_order_payment');
$table_sales_flat_order_status_history=Mage::getSingleton('core/resource')->getTableName('sales_flat_order_status_history');
$table_sales_flat_order_grid=Mage::getSingleton('core/resource')->getTableName('sales_flat_order_grid');
$table_log_quote=Mage::getSingleton('core/resource')->getTableName('log_quote');
$quoteId='';
if (!empty($orderIds)) {
foreach ($orderIds as $orderId) {
$query=null;
$order =Mage::getModel('sales/order')->load($orderId);
if($order->increment_id){
$incId=$order->increment_id;
if(in_array($table_sales_flat_order,$rsc_table)){
$query='SELECTentity_id
FROM
'.$table_sales_flat_order.'
WHERE increment_id="'.mysql_escape_string($incId).'"';
$rs=$write->fetchAll($query);
$query='SELECTquote_id
FROM
'.$table_sales_flat_order.'
WHERE entity_id="'.mysql_escape_string($orderId).'"';
$rs1=$write->fetchAll($query);
$quoteId=$rs1[0]['quote_id'];
}
$query='SET FOREIGN_KEY_CHECKS=1';
$rs3=$write->query($query);
//print_r($rsc_table);
//echo$table_sales_flat_creditmemo_comment;
if(in_array($table_sales_flat_creditmemo_comment,$rsc_table)){
//echo "DELETE FROM".$table_sales_flat_creditmemo_comment."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_creditmemo." WHEREorder_id=".$orderId.")";
//die;
$write->query("DELETE FROM".$table_sales_flat_creditmemo_comment."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_creditmemo." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
//die;
if(in_array('sales_flat_creditmemo_item',$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_creditmemo_item."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_creditmemo." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_creditmemo,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_creditmemo."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_creditmemo_grid,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_creditmemo_grid."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_invoice_comment,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_invoice_comment." WHERE parent_id IN (SELECTentity_id FROM ".$table_sales_flat_invoice." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_invoice_item,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_invoice_item."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_invoice." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_invoice,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_invoice."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_invoice_grid,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_invoice_grid."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if($quoteId){
if(in_array($table_sales_flat_quote_address_item,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_address_item."
WHERE parent_item_id
IN (SELECT address_id FROM".$table_sales_flat_quote_address." WHEREquote_id=".$quoteId.")");
}
$table_sales_flat_quote_shipping_rate=Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_shipping_rate');
if(in_array($table_sales_flat_quote_shipping_rate,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_shipping_rate."
WHERE address_id IN (SELECT address_id FROM".$table_sales_flat_quote_address." WHEREquote_id=".$quoteId.")");
}
if(in_array($table_sales_flat_quote_item_option,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_item_option."
WHERE item_id IN (SELECT item_id FROM".$table_sales_flat_quote_item." WHEREquote_id=".$quoteId.")");
}
if(in_array($table_sales_flat_quote,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote."
WHERE entity_id=".$quoteId);
}
if(in_array($table_sales_flat_quote_address,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_address."
WHERE quote_id=".$quoteId);
}
if(in_array($table_sales_flat_quote_item,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_item."
WHERE quote_id=".$quoteId);
}
if(in_array('sales_flat_quote_payment',$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_quote_payment."
WHERE quote_id=".$quoteId);
}
}
if(in_array($table_sales_flat_shipment_comment,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_shipment_comment."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_shipment." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_shipment_item,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_shipment_item."
WHERE parent_id IN (SELECT entity_id FROM".$table_sales_flat_shipment." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_shipment_track,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_shipment_track."
WHERE order_id
IN (SELECT entity_id FROM".$table_sales_flat_shipment." WHEREorder_id='".mysql_escape_string($orderId)."')");
}
if(in_array($table_sales_flat_shipment,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_shipment."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_shipment_grid,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_shipment_grid."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_order,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order."
WHERE entity_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_order_address,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order_address."
WHERE parent_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_order_item,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order_item."
WHERE order_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_order_payment,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order_payment."
WHERE parent_id='".mysql_escape_string($orderId)."'");
}
if(in_array($table_sales_flat_order_status_history,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order_status_history."
WHERE parent_id='".mysql_escape_string($orderId)."'");
}
if($incId&&in_array($table_sales_flat_order_grid,$rsc_table)){
$write->query("DELETE FROM".$table_sales_flat_order_grid."
WHERE increment_id='".mysql_escape_string($incId)."'");
}
$query="show tables like'%".$table_log_quote."'";
$rsc_table_l=$write->fetchCol($query);
if($quoteId&&$rsc_table_l){
$write->query("DELETE FROM".$table_log_quote." WHEREquote_id=".$quoteId);
}
$write->query("SETFOREIGN_KEY_CHECKS=1");
}
}
$this->_getSession()->addSuccess($this->__('Order(s)deleted.'));
}else{
$this->_getSession()->addError($this->__('Order(s)error.'));
}
$this->_redirect('*/*/');
}
}
require_once'Mage/Adminhtml/controllers/Sales/OrderController.php';
class EM_DeleteOrder_Adminhtml_Sales_OrderController extendsMage_Adminhtml_Sales_OrderController
{
FROM sales_flat_order AS o
}