'DANMARK', 'Gen_Bus_Posting_Group' => 'INDLAND', 'VAT_Bus_Posting_Group' => 'INDLAND' ); protected $_config = array( 'xmlport_page' => 'MagentoWebFunctions', 'base_url' => '' ); protected $_ordersConfig = array ( 'create_as' => 'customer', 'customer_no' => null, 'company_no' => null, 'shipping_item_type' => 'Item', 'shipping_item_no' => null, 'customer_no_series' => null, 'date_format' => 'm/d/y', 'order_status_range' => '1 month' ); public function __construct() { $this->_baseUrl = $this->_config['base_url'] . "/Page/"; } protected function mapItem( $itemType, $item, $params ) { return $item; } public function shippingLine ( $order, $type = 'Item' ) { $navisionItem = array(); $tax_info = $order->getFullTaxInfo(); $navisionItem['DocumentType'] = "Order"; $navisionItem['Type'] = $type; $navisionItem['ItemNo'] = $this->_ordersConfig['shipping_item_no']; $navisionItem['Description'] = "Freight line"; $navisionItem['Quantity'] = 1; $navisionItem['Unit_Price'] = $order->getBaseShippingInclTax(); $navisionItem['Amount'] = $order->getBaseShippingAmount(); $navisionItem['Amount_Including_VAT'] = $order->getBaseShippingInclTax(); $navisionItem['VAT_Percent'] = (int)( $order->getBaseShippingTaxAmount() / $order->getBaseShippingAmount() ) * 100; $navisionItem['Line_Discount_Amount'] = $order->getBaseShippingDiscountAmount() - $order->getBaseShippingHiddenTaxAmount(); $navisionItem['Item_Id'] = $this->_ordersConfig['shipping_item_no']; $navisionItem = $this->mapItem ( 'MagentoOrderItem', $navisionItem, array ( 'MagentoOrderItem' => $item, 'OrderProduct' => "ShippingItem" ) ); return $navisionItem; } public function convertOrders ( $order ) { $billingAddress = $order->getBillingAddress(); $shippingAddress = $order->getShippingAddress(); if ( !$billingAddress && $shippingAddress ) $billingAddress = $shippingAddress; $customer = null; if ( $order->getCustomerId() ) { $customer = Mage::getModel('customer/customer')->load ( $order->getCustomerId() ); } $billingFullName = $billingAddress->getFirstname() . ' ' . $billingAddress->getMiddlename() . ' ' . $billingAddress->getLastname(); $shippingFullName = $shippingAddress->getFirstname() . ' ' . $shippingAddress->getMiddlename() . ' ' . $shippingAddress->getLastname(); $billingCompanyName = $billingAddress->getCompany(); $shippingCompanyName = $shippingAddress->getCompany(); $customer_comment = $order->getOnestepcheckoutCustomercomment(); $navisionOrder = array ( 'Web_orderno' => $order->getIncrementId(), 'Sell_to_Customer_Name' => $billingCompanyName ? $billingCompanyName : $billingFullName, 'Sell_to_Contact' => $billingFullName, 'Sell_to_Address' => substr($billingAddress->getStreet(1), 0, 30), 'Sell_to_Address_2' => $billingAddress->getStreet(2), 'Sell_to_City' => $billingAddress->getCity(), 'Sell_to_Post_Code' => $billingAddress->getPostcode(), 'Sell_to_Country_Region_Code' => $billingAddress->getCountryId(), 'Ship_to_Name' => $shippingCompanyName ? $shippingCompanyName : $shippingFullName, 'Ship_to_Contact' => $shippingFullName, 'Ship_to_Address' => substr($shippingAddress->getStreet(1), 0, 30), 'Ship_to_Address_2' => $shippingAddress->getStreet(2), 'Ship_to_City' => $shippingAddress->getCity(), 'Ship_to_Post_Code' => $shippingAddress->getPostcode(), 'Ship_to_Country_Region_Code' => $shippingAddress->getCountryId(), 'Bill_to_Customer_Name' => $billingCompanyName ? $billingCompanyName : $billingFullName, 'Bill_to_Contact' => $billingFullName, 'Bill_to_Address' => substr($billingAddress->getStreet(1), 0, 30), 'Bill_to_Address_2' => $billingAddress->getStreet(2), 'Bill_to_City' => $billingAddress->getCity(), 'Bill_to_Post_Code' => $billingAddress->getPostcode(), 'Bill_to_Country_Region_Code' => $billingAddress->getCountryId(), 'Phone_No' => $billingAddress->getTelephone(), 'E_Mail' => $billingAddress->getEmail() ? $billingAddress->getEmail() : $order->getCustomerEmail(), 'No' => '', 'DocumentType' => 'Order', 'Base_Shipping_Invoiced' => $order->getBaseShippingInclTax(), 'Order_Amount_Excl_VAT' => ($order->getBaseGrandTotal() - $order->getBaseTaxAmount()), 'Order_Amount_Incl_VAT' => $order->getBaseGrandTotal(), 'OrderDate' => date($this->_ordersConfig['date_format'],strtotime($order->getCreatedAt())), 'PostingDate' => date($this->_ordersConfig['date_format'],strtotime($order->getCreatedAt())), 'DocumentDate' => date($this->_ordersConfig['date_format'],strtotime($order->getCreatedAt())), 'ExternalDocumentNo' => $order->getIncrementId(), 'SellToCustomerNo' => '', 'BillToCustomerNo' => '' ); if ( $customer_comment ) { $customer_comment = str_replace ( "\n", " ", $customer_comment ); $customer_comment = str_replace ( "\r", "", $customer_comment ); $customer_comment = wordwrap ( $customer_comment, 50, "\n", true ); $customer_comment = explode ( "\n", $customer_comment ); $comments = array(); foreach ( $customer_comment as $i => $comment ) { //$comment = substr($comment,0,50); if ( $i >= 10 ) break; $commentLine = array ( 'DocumentNo' => '', 'LineNo' => $i * 10000, 'DocumentType' => 'Order', 'Comment' => $comment, 'Date' => date($this->_ordersConfig['date_format'],strtotime($order->getCreatedAt())), 'DocumentLine' => 0 ); $comments []= $commentLine; } $navisionOrder['Comments'] = array ('Comment' => $comments); } $navisionOrder = $this->mapItem ( 'MagentoOrder', $navisionOrder, array( 'MagentoOrder' => $order, 'OrderCustomer' => $customer ) ); $navisionItems = array(); $lineNo = 1; $_simpleProductBarcodes = array(); $order_items = array(); // always process simple products first foreach ( $order->getItemsCollection() as $item ) { $_product = Mage::getModel('catalog/product')->load($item->getProductId()); if ( $_product->getTypeId() == 'simple' ) array_push ( $order_items, $item ); } foreach ( $order->getItemsCollection() as $item ) { $_product = Mage::getModel('catalog/product')->load($item->getProductId()); if ( $_product->getTypeId() == 'configurable' ) array_push ( $order_items, $item ); } //foreach( $order->getItemsCollection() as $item ) { foreach ( $order_items as $item ) { $navisionItem = array(); //var_dump ( $item->getData() ); $_product = Mage::getModel('catalog/product')->load($item->getProductId()); //if ( $_product->getTypeId() != 'configurable' ) continue; //var_dump ( $item->getData() );die(); if ( $_product->getTypeId() == 'simple' ) { if ( $item->getParentItemId() ) { $_simpleProductBarcodes[$item->getParentItemId()] = $_product->getSku(); foreach ( $navisionItems as $index => $navisionItem ) { if ( $navisionItem['Item_Id'] == $item->getParentItemId() ) { $navisionItem['Barcode'] = $_product->getSku(); $navisionItems[$index] = $navisionItem; } } continue; } } //$navisionItem['WEB_Orderno'] = $order->getIncrementId(); $navisionItem['LineNo'] = $lineNo*1000; if ( $_simpleProductBarcodes[$item->getItemId()] ) $navisionItem['Barcode'] = $_simpleProductBarcodes[$item->getItemId()]; else $navisionItem['Barcode'] = $_product->getSku(); $navisionItem['DocumentType'] = "Order"; $navisionItem['Type'] = "Item"; $navisionItem['ItemNo'] = $navisionItem['Barcode']; $navisionItem['Description'] = $_product->getName(); $navisionItem['Quantity'] = $item->getQtyOrdered(); $navisionItem['Unit_Price'] = $item->getBasePrice(); $navisionItem['Amount'] = $item->getBaseRowTotal(); $navisionItem['Amount_Including_VAT'] = $item->getBaseRowTotalInclTax(); $navisionItem['VAT_Percent'] = $item->getTaxPercent(); $navisionItem['Line_Discount_Amount'] = $item->getBaseDiscountAmount() - $item->getBaseHiddenTaxAmount(); $navisionItem['Item_Id'] = $item->getItemId(); $navisionItem = $this->mapItem ( 'MagentoOrderItem', $navisionItem, array ( 'MagentoOrderItem' => $item, 'OrderProduct' => $_product ) ); array_push ( $navisionItems, $navisionItem); $lineNo++; } if ( $this->_ordersConfig['shipping_item_no'] && $order->getBaseShippingAmount() ) { $shippingLine = $this->shippingLine($order, $this->_ordersConfig['shipping_item_type'] ); $shippingLine['LineNo'] = $lineNo*1000; $lineNo++; array_push ( $navisionItems, $shippingLine ); } $navisionOrder['SalesLine'] = array ( 'Line' => $navisionItems ); return array ( $navisionOrder ); } public function readCustomer ( $email ) { $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; if ( $this->_ordersConfig['create_as'] == 'customer' ) { $data = $this->callCustomFunction ( $this->_config['xmlport_page'], "ExportCustomers", array('customersExport' => array (), 'customerFilter' => $email ) ); $entry = $data->customersExport->Customer; } else if ( $this->_ordersConfig['create_as'] == 'contact' ) { $data = $this->callCustomFunction ( $this->_config['xmlport_page'], "ExportContacts", array('exportContacts' => array (), 'contactFilter' => $email ) ); $entry = $data->exportContacts->Contact; } $this->_baseUrl = $this->_config['base_url'] . "/Page/"; return (array)$entry; } public function getContactExternalId ( $order ) { if ( is_array($order) && $order['Web_orderno'] ) return "ORDER_" . $order['Web_orderno']; return "ORDER_" . $order->getIncrementId(); } public function createCustomer ( $order ) { $customer = array ( 'Email' => $order['E_Mail'], 'Name' => $order['Sell_to_Customer_Name'], 'Name_2' => '', 'Address' => $order['Sell_to_Address'], 'Address_2' => $order['Sell_to_Address_2'], 'City' => $order['Sell_to_City'], 'Phone_No' => $order['Phone_No'], 'Customer_Posting_Group' => $this->_navisionDefaultData['Customer_Posting_Group'], 'Gen_Bus_Posting_Group' => $this->_navisionDefaultData['Gen_Bus_Posting_Group'], 'VAT_Bus_Posting_Group' => $this->_navisionDefaultData['VAT_Bus_Posting_Group'], 'No.' => '' ); if ( $this->_ordersConfig['customer_no_series'] ) { $customer['NoSeries'] = $this->_ordersConfig['customer_no_series']; } $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; if ( $this->_ordersConfig['create_as'] == 'customer' ) { $data = $this->callCustomFunction ( $this->_config['xmlport_page'], "ImportCustomers", array('importCustomers' => array ( 'Customer' => $customer ) ) ); } else if ( $this->_ordersConfig['create_as'] == 'contact' ) { $customer['Type'] = 'Person'; $customer['CompanyNo'] = $this->_ordersConfig['company_no']; $customer['ExternalId'] = $this->getContactExternalId ( $order ); $data = $this->callCustomFunction ( $this->_config['xmlport_page'], "ImportContacts", array('importContacts' => array ( 'Contact' => $customer ) ) ); //$external_id = } $this->_baseUrl = $this->_config['base_url'] . "/Page/"; } public function putOrders( $orders ) { foreach ( $orders as $order ) { $order_page = $this->_config['order_page']; $this->_baseUrl = $this->_config['base_url'] . "/Page/"; //unset ( $order['WebLines'] ); //var_dump ( $order ); try { //echo $order['E_Mail']; $customer = array(); if ( $this->_ordersConfig['create_as'] == 'customer' ) { $customer = $this->readCustomer ( $order['E_Mail'] ); if ( !count($customer) || !$customer[0]->Email ) { $this->createCustomer ( $order ); $customer = $this->readCustomer ( $order['E_Mail'] ); } $customer = (array)($customer[0]); } if ( $this->_ordersConfig['create_as'] == 'contact' ) { $contact = $this->readCustomer( $this->getContactExternalId ( $order ) ); $customer['No.'] = $this->_ordersConfig['customer_no']; if ( !count($contact) || !$contact[0]->Email ) { $this->createCustomer ( $order ); $contact = $this->readCustomer( $this->getContactExternalId ( $order ) ); } $contact = (array)$contact[0]; $order['SellToContactNo'] = $contact['No.']; $order['BillToContactNo'] = $contact['No.']; } $order['SellToCustomerNo'] = $customer['No.']; $order['BillToCustomerNo'] = $customer['No.']; //var_dump ( $order );die(); //$order['BillToCustomerNo'] = $customer['No.']; //var_dump ( $customer );die(); $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; $this->callCustomFunction ( $this->_config['xmlport_page'], "ImportOrders", array('ordersImport' => array ( 'SalesHeader' => $order ) ) ); $this->_baseUrl = $this->_config['base_url'] . "/Page/"; } catch ( Exception $e ) { // ignore the "The record already exists." message from Navision - means that the order was successfully transferred at some point.. if ( $e->getMessage() != "The record already exists." ) throw $e; } // no exceptions - confirm the order $this->confirmOrders ( array ( $order ) ); } } public function confirmOrders ( $orders ) { foreach ( $orders as $order ) { $orderId = $order['Web_orderno']; $this->_adapterPublisher->confirmOrder ( $orderId ); } } // read back the updated information public function getOrders () { } public function convertOrdersStatus( $entry ) { $orderStatusEntry = new Varien_Object(); $orderStatusEntry->setIncrementId ( $entry->OrderId ); $orderStatusEntry->setStatus ( 'completed' ); return $orderStatusEntry; } public function getOrdersStatus() { $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; $soapOrders = $this->callCustomFunction ( $this->_config['xmlport_page'], "ExportOrdersStatus", array('ordersStatus' => array(), 'dateFromFilter' => date($this->_ordersConfig['date_format'],strtotime(date('Y-m-d') . " - " . $this->_ordersConfig['order_status_range'] )) ) ); $this->_baseUrl = $this->_config['base_url'] . "/Page/"; return $soapOrders->ordersStatus->OrdersStatus; } public function getStockData () { $params = $this->_adapterParams; // fetch basic information about the product try { $this->regenerateStock(); // fetch basic information about the product $stockData = $this->readData ( $this->_config['stock_page'] ); } catch ( Exception $e ) { die ( $e->getMessage() ); } return $stockData; } public function convertStockData ( $entry ) { $stockEntry = new Varien_Object(); $stockEntry->setSku ( $entry->Item_No ); $stockEntry->setQty ( $entry->Inventory ); return $stockEntry; } public function convertProducts ( $product ) { $magentoProduct = new Varien_Object(); $itemNo = $product->Item_no; $magentoProduct->setSku ( $product->No ); $magentoProduct->setQty ( $product->Inventory ); $magentoProduct->setPrice ( $product->Price ); $magentoProduct->setManufacturer ( $product->Brand_ID ); $magentoProduct->setName ( $product->Brand_ID . " " . $product->Description . " " . $product->TypeDescription[0] ); $magentoProduct->setDescription ( $product->Description ); $magentoProduct->setType ( $product->Type[0] ); $magentoProduct->setTypeId ( 'simple' ); return $magentoProduct; } public function getProducts () { $params = $this->_adapterParams; // fetch basic information about the product //$soapProducts = $this->readData ( $this->_config['product_page'] ); $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; $soapProducts = $this->callCustomFunction ( $this->_config['xmlport_page'], "ExportItems", array('exportItems' => array() ) ); $this->_baseUrl = $this->_config['base_url'] . "/Page/"; return $soapProducts->exportItems->Item; //var_dump ( $soapProducts );die(); } public function regenerateStock() { if ( $this->_config['stock_codeunit_page'] ) { $this->_baseUrl = $this->_config['base_url'] . "/Codeunit/"; $this->callCustomFunction ( $this->_config['stock_codeunit_page'], "CalcInventory", array() ); $this->_baseUrl = $this->_config['base_url'] . "/Page/"; } } }