我正在尝试在Mage_Sales_Model_Resource_Report_Order_Createdat中加入两个表,
$columns = array(
// convert dates from UTC to current admin timezone
'period' => $periodExpr,
'store_id' => 'o.store_id',
'order_status' => 'o.status',
'orders_count' => new Zend_Db_Expr('COUNT(o.entity_id)'),
'total_qty_ordered' => new Zend_Db_Expr('SUM(oi.total_qty_ordered)'),
'total_qty_invoiced' => new Zend_Db_Expr('SUM(oi.total_qty_invoiced)'),
'total_income_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_grand_total', 0),
$adapter->getIfNullSql('o.base_total_canceled',0),
$adapter->getIfNullSql('o.base_to_global_rate',0)
)
),
'total_revenue_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s - %s - (%s - %s - %s)) * %s)',
$adapter->getIfNullSql('o.base_total_invoiced', 0),
$adapter->getIfNullSql('o.base_tax_invoiced', 0),
$adapter->getIfNullSql('o.base_shipping_invoiced', 0),
$adapter->getIfNullSql('o.base_total_refunded', 0),
$adapter->getIfNullSql('o.base_tax_refunded', 0),
$adapter->getIfNullSql('o.base_shipping_refunded', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_profit_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s - %s - %s - %s) * %s)',
$adapter->getIfNullSql('o.base_total_paid', 0),
$adapter->getIfNullSql('o.base_total_refunded', 0),
$adapter->getIfNullSql('o.base_tax_invoiced', 0),
$adapter->getIfNullSql('o.base_shipping_invoiced', 0),
$adapter->getIfNullSql('o.base_total_invoiced_cost', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_invoiced_amount' => new Zend_Db_Expr(
sprintf('SUM(%s * %s)',
$adapter->getIfNullSql('o.base_total_invoiced', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_canceled_amount' => new Zend_Db_Expr(
sprintf('SUM(%s * %s)',
$adapter->getIfNullSql('o.base_total_canceled', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_paid_amount' => new Zend_Db_Expr(
sprintf('SUM(%s * %s)',
$adapter->getIfNullSql('o.base_total_paid', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_refunded_amount' => new Zend_Db_Expr(
sprintf('SUM(%s * %s)',
$adapter->getIfNullSql('o.base_total_refunded', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_tax_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_tax_amount', 0),
$adapter->getIfNullSql('o.base_tax_canceled', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_tax_amount_actual' => new Zend_Db_Expr(
sprintf('SUM((%s -%s) * %s)',
$adapter->getIfNullSql('o.base_tax_invoiced', 0),
$adapter->getIfNullSql('o.base_tax_refunded', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_shipping_amount' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_shipping_amount', 0),
$adapter->getIfNullSql('o.base_shipping_canceled', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_shipping_amount_actual' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_shipping_invoiced', 0),
$adapter->getIfNullSql('o.base_shipping_refunded', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_discount_amount' => new Zend_Db_Expr(
sprintf('SUM((ABS(%s) - %s) * %s)',
$adapter->getIfNullSql('o.base_discount_amount', 0),
$adapter->getIfNullSql('o.base_discount_canceled', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
),
'total_discount_amount_actual' => new Zend_Db_Expr(
sprintf('SUM((%s - %s) * %s)',
$adapter->getIfNullSql('o.base_discount_invoiced', 0),
$adapter->getIfNullSql('o.base_discount_refunded', 0),
$adapter->getIfNullSql('o.base_to_global_rate', 0)
)
)
);
$select = $adapter->select();
$selectOrderItem = $adapter->select();
$qtyCanceledExpr = $adapter->getIfNullSql('qty_canceled', 0);
$cols = array(
'order_id' => 'order_id',
'total_qty_ordered' => new Zend_Db_expr("SUM(qty_ordered - {$qtyCanceledExpr})"),
'total_qty_invoiced' => new Zend_Db_expr('SUM(qty_invoiced)'),
);
$selectOrderItem->from($this->getTable('sales/order_item'), $cols)
->where('parent_item_id IS NULL')
->group('order_id');
$select->from(array('o' => $this->getTable('sales/order')), $columns)
->join(array('oi' => $selectOrderItem), 'oi.order_id = o.entity_id', array())
->where('o.state NOT IN (?)', array(
Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
Mage_Sales_Model_Order::STATE_NEW
));
有些人可以向我解释一下内部发生了什么:
$select->from(array('o' => $this->getTable('sales/order')), $columns)
->join(array('oi' => $selectOrderItem), 'oi.order_id = o.entity_id', array())
->where('o.state NOT IN (?)', array(
Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
Mage_Sales_Model_Order::STATE_NEW
));
据我所知,sales / order_item表正在加入销售/订单......(有人可以解释一下这种语法吗?)我正在尝试了解如何正确连接表,因为我需要加入sales / order_grid还有销售/订单。
谢谢!