In the following script changes made to full fill client requirement like :
- The order id exported never will export again :
- delimiter should be ; .
- Every value should be closed in “” .
- The File copy should be upload to third party ftp server.
- in csv only configurable product item will export.
File Location:app\code\community\Raveinfosys\Exporter\Model\Exportorders.php
<?php ini_set("auto_detect_line_endings", true); class Raveinfosys_Exporter_Model_Exportorders extends Raveinfosys_Exporter_Model_Exporter { const ENCLOSURE = '"'; const DELIMITER = ';'; public function exportOrders() { $id = 1; $model = Mage::getModel('exporter/exporter')->load($id); $lastOrderId = $model->getLastOrderId(); $orders = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('state', Mage_Sales_Model_Order::STATE_PROCESSING) ->addAttributeToSelect('entity_id') ->addAttributeToFilter('entity_id', array('gt' => $lastOrderId)); $order_arr = array(); foreach ($orders as $order) { $order_arr[] = $order->getId(); } if($order_arr){ $orderIdToInsert = max($order_arr); $data = array('last_order_id'=>$orderIdToInsert); $model = Mage::getModel('exporter/exporter')->load($id)->addData($data); try { //$model->setId($id)->save(); } catch (Exception $e){ echo $e->getMessage(); } } $fileName = 'Order-' . date("Y-m-d-H-i-s") . '.csv'; $fp = fopen(Mage::getBaseDir('export') . '/' . $fileName, 'w'); $this->writeHeadRow($fp); foreach ($order_arr as $order) { $order = Mage::getModel('sales/order')->load($order); $this->writeOrder($order, $fp); } fclose($fp); $contents = file_get_contents(Mage::getBaseDir('export') . '/' . $fileName); $contents = str_replace("#@ @#", "", $contents); file_put_contents(Mage::getBaseDir('export') . '/' . $fileName, $contents); $ftp_server = "your_server"; $ftp_username = "username"; $ftp_userpass = "password"; $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); // upload file ftp_put($ftp_conn, "/ECOM_MV/MBTUK/SALES/FRONTEND/TEST/". $fileName, Mage::getBaseDir('export') . '/' . $fileName, FTP_ASCII); // close connection ftp_close($ftp_conn); return $fileName; } protected function writeHeadRow($fp) { fputcsv($fp, $this->getHeadRowValues(), self::DELIMITER, self::ENCLOSURE); } protected function writeOrder($order, $fp) { $common = $this->getCommonOrderValues($order); $orderItems = $order->getItemsCollection(); $itemInc = 0; $data = array(); $count = 0; foreach ($orderItems as $item) { if($item->_getData("product_type") == "configurable"){ $record = array_merge($common, $this->getOrderItemValues($item, $order, ++$itemInc)); fputcsv($fp, $record, self::DELIMITER, self::ENCLOSURE); } } } protected function getHeadRowValues() { return array( "OrderId", "OrderDate", "StoreName", "CustomerFirstname", "CustomerLastname", "CustomerEmail", "CustomerDob", "CustomerBalTotalRefunded", "CustomerBalanceAmount", "CustomerBalanceInvoiced", "CustomerBalanceRefunded", "CustomerGender", "CustomerGroupId", "CustomerId", "CustomerIsGuest", "CustomerPrefix", "CustomerSuffix", "CustomerTaxVat", "CustomerNote", "CustomerNoteNotify", "CustomerTelephone", "ShippingCountry", "ShippingRegion", "ShippingCity", "ShippingStreet", "ShippingPostcode", "State", "Status", "Currency", "TaxAmount", "Subtotal", "OrderShipping", "ShippingInclTax", "TotalPaid", "PaymentType", "ProductName", "ProductSku", "ProductColor", "ProductSize", "ProductPrice", "ProductTaxAmount", "ProductPriceInclTax", "ProductType", "ProductQtyOrdered", "ProductQtyInvoiced", "ProductQtyShipped", "RowTotal", "RowTotalInclTax", "DiscountAmount", "DiscountPercent", "" ); } //Common orders value protected function getCommonOrderValues($order) { $shippingAddress = !$order->getIsVirtual() ? $order->getShippingAddress() : null; $billingAddress = $order->getBillingAddress(); if (!$shippingAddress) $shippingAddress = $billingAddress; $credit_detail = $this->getCreditMemoDetail($order); if($order->getData('total_paid')){ $total_paid = $order->getData('total_paid'); } else{ $total_paid = 0; } return array( $order->getIncrementId()."#@ @#", $order->getData('created_at')."#@ @#", preg_replace("/[\r\n]*/","",$order->getData('store_name'))."#@ @#", $this->formatText($order->getData('customer_firstname'))."#@ @#", $this->formatText($order->getData('customer_lastname'))."#@ @#", $order->getData('customer_email')."#@ @#", $order->getData('customer_dobs')."#@ @#", "#@ @#", "#@ @#", "#@ @#", "#@ @#", $order->getData('customer_gender')."#@ @#", $order->getData('customer_group_id')."#@ @#", $order->getData('customer_id')."#@ @#", $order->getCustomerIsGuest()."#@ @#", $this->formatText($order->getData('customer_prefix'))."#@ @#", $this->formatText($order->getData('customer_suffix'))."#@ @#", $order->getData('customer_taxvat')."#@ @#", $order->getData('customer_note')."#@ @#", $order->getData('customer_note_notify')."#@ @#", $order->getBillingAddress()->getData('telephone')."#@ @#", $shippingAddress->getData('country_id')."#@ @#", $this->formatText($shippingAddress->getData('region'))."#@ @#", $this->formatText($shippingAddress->getData('city'))."#@ @#", preg_replace("/[\r\n]*/","",$this->formatText($shippingAddress->getData('street')))."#@ @#", $shippingAddress->getData('postcode')."#@ @#", $order->getState()."#@ @#", $order->getStatus()."#@ @#", $order->getData('order_currency_code')."#@ @#", $order->getData('tax_amount')."#@ @#", $order->getData('subtotal')."#@ @#", $order->getData('shipping_amount')."#@ @#", $order->getData('shipping_incl_tax')."#@ @#", number_format($total_paid, 2)."#@ @#", $this->getPaymentMethod($order)."#@ @#" ); } //To return the array of ordered items protected function getOrderItemValues($item, $order, $itemInc = 1) { $options = $item->getProductOptions(); $customOptions = $options['attributes_info']; if(!empty($customOptions)) { foreach ($customOptions as $option) { $size = $option['value']; //if(strlen($size) >= 2 ){ // $sizeinsert = $size*100; //} //else{ // $sizeinsert = $size; //} } } $product = Mage::getModel('catalog/product')->load($item->getProductId()); return array( $this->formatText($item->getName())."#@ @#", $this->getItemSku($item)."#@ @#", $product->getAttributeText('color')."#@ @#", $size."#@ @#", $item->getPrice()."#@ @#", $item->getTaxAmount()."#@ @#", $item->getPriceInclTax()."#@ @#", $item->getProductType()."#@ @#", $item->getQtyOrdered()."#@ @#", $item->getQtyInvoiced()."#@ @#", $item->getQtyShipped()."#@ @#", $item->getBaseRowTotal()."#@ @#", $item->getRowTotal()."#@ @#", $item->getDiscountAmount()."#@ @#", $item->getDiscountPercent()."#@ @#", "" ); } }
0 Comments