moveProcessedFile ( $filename, $location ); } } public function moveProcessedFile ( $filename, $location ) { $base = Mage::getBaseDir('base') . "/"; if ( !is_dir ( $base . $location ) ) { mkdir ( $base . $location, 0755, true ); } rename ( $filename, $base . $location . "/" . basename($filename) ); } public function parseData ( $srcCsvFile, $csvColSep ) { if (($handle = fopen($srcCsvFile, "r")) !== FALSE) { $rowNumber = 1; //Loop over each row while (($row = fgetcsv($handle, 0, $csvColSep)) !== FALSE) { if ($row == '' || !$row || !is_array($row)) continue; //CSV_VALID_COLUMN_COUNT is defined in the subclass. Check there are the correct number of columns if ( $this->_checkCount ) { if (($colCount = count($row)) != count($this->_columns)) { throw new Exception("Invalid column count on row {$rowNumber} of " . DATA_SOURCE_FILE . ". Column count should be " . count($this->_columns) . " but is {$colCount}"); continue; } } //array_walk($row, array('Neye_Helper', 'convertToUtf8')); //Add the data to the rawData $this->_csvData[] = $row; $cnt1 = count ( $row ); $cnt2 = count ( $this->_columns ); if ( $cnt2 < $cnt1 ) { die ( 'Invalid column count in CSV header: ' . $cnt2 . ", there is a line with " . $cnt1 . " columns, the amount of columns in the header should be more then the maximum amount of columns\n" ); } if ( $cnt1 < $cnt2 ) $row = array_pad ( $row, $cnt2, '' ); //echo "count row:" . count($row) . ", count columns:" . count($this->_columns) . "\n"; $this->_csvParsedData[] = array_combine ( $this->_columns, $row ); $rowNumber++; } fclose($handle); } else { //throw new Exception("Could not read datafile: " . $srcCsvFile); } } public function fetchFileFtp ( $host, $username, $pass, $path, $dest ) { $ftp = new Varien_Io_Ftp(); if (!$ftp->open ( array('user' => $username, 'password' => $pass, 'host' => $host ) )) throw new Exception('Failed to connect to FTP'); $ftp->read ( $path, $dest ); } public function getOrderMapping ( $order, $orderItemParent, $orderItem, $product, $parentProduct, $property, $caller ) { if ( !$property ) return $property; $product = Mage::getModel('catalog/product')->load ( $orderItem->getProductId() ); if ( !$order->getShippingAddress() ) $shippingAddress = $order->getBillingAddress(); else $shippingAddress = $order->getShippingAddress(); $patterns = array ('/order::(\w*)/i', '/billing_address::(\w*)/i', '/shipping_address::(\w*)/i', '/order_item::(\w*)/i', '/order_item_parent::(\w*)/i', '/product::(\w*)/i', '/parent_product::(\w*)/i', '/payment_info::(\w*)/i'); $replacements = array ( ' $order->getData("$1") ', ' $order->getBillingAddress()->getData("$1") ', ' $shippingAddress->getData("$1") ', ' $orderItem->getData("$1") ', ' $orderItemParent->getData("$1") ', ' $product->getData("$1") ', ' $parentProduct->getData("$1") ', ' $order->getPaymentInfo() ? $order->getPaymentInfo()->getData("$1") : "" ' ); $result = preg_replace ( $patterns, $replacements, $property ); $property = $result; if ( !preg_match ( $result, "/orderItem/" ) ) { $this->_hasOrderItems = true; } //echo $property."\n"; $value = eval ( "return (" . $property . "); " ); return $value; } public function mapOrder ( $order, $is_memo = false, $caller = null ) { $lines = array(); if ( $this->_visibleOnly && !$is_memo ) { $orderItems = $order->getAllVisibleItems(); } else $orderItems = $order->getItemsCollection(); // reorganize item collection $order_items = array(); foreach ( $orderItems as $orderItem ) { if ( $is_memo ) { $orderItem->setItemId ( $orderItem->getOrderItemId() ); $realOrderItem = Mage::getModel('sales/order_item')->load ( $orderItem->getOrderItemId() ); $data = $realOrderItem->getData(); $memoItemData = $orderItem->getData(); foreach ( $data as $key => $value ) { if ( !array_key_exists( $key, $memoItemData ) ) { $orderItem->setData ( $key, $value ); } } } $order_items[$orderItem->getItemId()] = $orderItem; } foreach ( $orderItems as $orderItem ) { $this->_hasOrderItems = false; $line = array(); if ( $orderItem->getParentItemId() ) $orderItemParent = $order_items[$orderItem->getParentItemId()]; else $orderItemParent = $orderItem; $product = Mage::getModel('catalog/product')->load ( $orderItem->getProductId() ); $parentProduct = Mage::getModel('catalog/product')->load ( $orderItemParent->getProductId() ); foreach ( $this->_orderColumns as $_column ) { $value = $this->getOrderMapping ( $order, $orderItemParent, $orderItem, $product, $parentProduct, $_column, $caller ); $line[] = $value; } if ( method_exists ( $caller, "mapItem" ) ) { $line = $caller->mapItem ( $order, $orderItem, $line ); } if ( !empty($line) ) array_push ( $lines, $line ); if ( !$this->_hasOrderItems ) break; } return $lines; } }