<?php
class Bysoft_Export_Model_Order extends Mage_Core_Model_Abstract
{
protected function _construct(){
$this->_init("export/order");
$this->_write = Mage::getSingleton('core/resource')->getConnection('core_write');
}
public function truncate_table() {
$sql = 'truncate table bysoft_exportorder';
$this->_write->query($sql);
}
public function run($start = '', $end = '') {
$this->truncate_table();
//get all complete or processing or shipped order
$order_collection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', array('in'=>array('complete','processing','shipped')));
if ($start) {
$start_year = substr($start, 0, 4);
$start_month = substr($start, 4, 2);
$start_date = substr($start, 6, 2);
$start_datetime = $start_year . '-' . $start_month . '-' .$start_date . ' 0:0:0';
$order_collection->addFieldToFilter('created_at', array('gt'=>$start_datetime));
}
if ($end) {
$end_year = substr($end, 0, 4);
$end_month = substr($end, 4, 2);
$end_date = substr($start, 6, 2);
$end_datetime = $end_year . '-' . $end_month . '-' .$end_date . ' 23:59:59';
$order_collection->addFieldToFilter('created_at', array('lt'=>$end_datetime));
}
$order_collection->setOrder('created_at', 'ASC');
foreach ($order_collection as $_order) {
//get all order itemes
$order_increment_id = $_order->getData('increment_id');
$purchased_on = $_order->getData('created_at');
$ordered_items = $_order->getAllVisibleItems();
$shippingId = $_order->getShippingAddress()->getId();
$shippingAddress = Mage::getModel('sales/order_address')->load($shippingId);
$shipping_to_name = $shippingAddress->getData('firstname') . ' ' . $shippingAddress->getData('lastname');
$status = $_order->getData('status');
foreach ($ordered_items as $item) {
$sku = $item->getSku();
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if ($_product) {
$_product = Mage::getModel('catalog/product')->load($_product->getId());
//get config sku
if ($_product->getTypeId() == 'simple') {
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
->getParentIdsByChild($_product->getId());
$conifg_product = Mage::getModel('catalog/product')->load($parentIds[0]);
} else {
$conifg_product = $_product;
}
$config_sku = $conifg_product->getSku();
//get third category name
$categoryIds = $conifg_product->getCategoryIds();
foreach ($categoryIds as $cid) {
$cate = Mage::getModel('catalog/category')->load($cid);
if ($cate->getLevel() == 4) {
//get category name
$cate_name = $cate->getName();
$cate_name_parent = Mage::getModel('catalog/category')->load($cate->getParentId());
if ($cate_name_parent) {
//get gender
$gender_cate = Mage::getModel('catalog/category')->load($cate_name_parent->getParentId());
$gender = $gender_cate->getName();
}
}
}
//get en name
$en_name = $_product->getData('en_name');
//get size
$size = $_product->getAttributeText('size');
//get qty
$qty = $item->getData('qty_ordered');
//get original_price
$org_price = $_product->getData('price');
//get sp price
$special_price = $_product->getSpecialPrice();
//get row_total
$paid_amount =$item->getPrice() - $item->getDiscountAmount();
//save into order object
$export_oder = Mage::getModel('export/order');
$export_oder->setData('order_increment_id', $order_increment_id);
$export_oder->setData('purchased_on',$purchased_on);
$export_oder->setData('ship_to_name', $shipping_to_name);
$export_oder->setData('status', $status);
$export_oder->setData('cate_name', $cate_name);
$export_oder->setData('gender', $gender);
$export_oder->setData('en_name', $en_name);
$export_oder->setData('config_sku', $config_sku);
$export_oder->setData('size', $size);
$export_oder->setData('qty', $qty);
$export_oder->setData('org_price',$org_price);
$export_oder->setData('special_price', $special_price);
$export_oder->setData('paid_amount', $paid_amount);
$export_oder->save();
var_dump('order:' . $order_increment_id . ' item id:' .$item->getId() . ' save successed!');
}
}
}
}
}