'Mag_Web_Order', 'product_page' => 'Mag_Web_Products', 'prices_page' => 'Mag_Web_Prices', 'stock_page' => 'Mag_Inventory', 'creditmemo_page' => 'Mag_Creditmemo', 'stock_codeunit_page' => 'Web_API', 'base_url' => '' ); public function __construct() { $this->_baseUrl = $this->_config['base_url'] . "/Page/"; } protected function mapItem( $itemType, $item, $params ) { return $item; } public function convertOrdersStatus( $order ) { $orderStatus = new Varien_Object(); $orderStatus->setIncrementId ( $order->Web_orderno ); $orderStatus->setStatus ( "completed" ); return $orderStatus; } public function getOrdersStatus() { $orders = $this->readData ( $this->_config['order_page'] ); $completedOrders = array(); foreach ( $orders as $order ) { if ( $order->Completely_Shipped == "true" ) { $completedOrders []= $order; } } return $completedOrders; //var_dump ( $orders );die(); } 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' => $billingAddress->getStreet(1), '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' => $shippingAddress->getStreet(1), '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(), 'Phone_No' => $billingAddress->getTelephone(), 'E_Mail' => $billingAddress->getEmail() ? $billingAddress->getEmail() : $order->getCustomerEmail(), 'Payment_Method_Code' => $order->getPayment()->getMethodInstance()->getCode(), 'Shipment_Method_Code' => $order->getShippingMethod(), 'Base_Shipping_Invoiced' => $order->getBaseShippingInclTax(), 'Order_Amount_Excl_VAT' => ($order->getBaseGrandTotal() - $order->getBaseTaxAmount()), 'Order_Amount_Incl_VAT' => $order->getBaseGrandTotal(), 'Currency_Code' => $order->getBaseCurrencyCode() ); 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 ); foreach ( $customer_comment as $i => $comment ) { //$comment = substr($comment,0,50); if ( $i >= 10 ) break; $navisionOrder[ "Comment_" . ($i+1) ] = $comment; } } $navisionOrder = $this->mapItem ( 'MagentoOrder', $navisionOrder, array( 'MagentoOrder' => $order, 'OrderCustomer' => $customer ) ); $navisionItems = array(); $lineNo = 0; $_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; } } //var_dump ( $item->getData() ); //die ( $item->getPrice() ); $navisionItem['WEB_Orderno'] = $order->getIncrementId(); $navisionItem['Line_No'] = $lineNo; if ( $_simpleProductBarcodes[$item->getItemId()] ) $navisionItem['Barcode'] = $_simpleProductBarcodes[$item->getItemId()]; else $navisionItem['Barcode'] = $_product->getSku(); $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(); $navisionItem['Item_Id'] = $item->getItemId(); $navisionItem = $this->mapItem ( 'MagentoOrderItem', $navisionItem, array ( 'MagentoOrderItem' => $item, 'OrderProduct' => $_product ) ); array_push ( $navisionItems, $navisionItem); $lineNo++; } $navisionOrder['WebLines'] = array ( 'Web_Orderlines' => $navisionItems ); return array ( $navisionOrder ); } 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 { $this->createData ( $order_page, array ($order_page => $order ) ); } catch ( Exception $e ) { // ignore the "The reconrd 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 convertCreditmemos ( $creditmemo ) { //var_dump ( $creditmemo );die(); $items = $creditmemo->getItemsCollection(); //var_dump ( $items->count() ); $order_id = $creditmemo->getOrderId(); $order = Mage::getModel('sales/order')->load ( $order_id ); $billingAddress = $order->getBillingAddress(); $shippingAddress = $order->getShippingAddress(); $navision_creditmemo = array( 'Web_orderno' => $order->getIncrementId(), 'Sell_to_Customer_Name' => $billingAddress->getFirstname() . ' ' . $billingAddress->getMiddlename() . ' ' . $billingAddress->getLastname(), 'Sell_to_Address' => $billingAddress->getStreet(1), 'Sell_to_Address_2' => '', 'Sell_to_City' => $billingAddress->getCity(), 'Sell_to_Post_Code' => $billingAddress->getPostcode(), 'Sell_to_Country_Region_Code' => $billingAddress->getCountryId(), 'Ship_to_Customer_Name' => $shippingAddress->getFirstname() . ' ' . $shippingAddress->getMiddlename() . ' ' . $shippingAddress->getLastname(), 'Ship_to_Address' => $shippingAddress->getStreet(1), 'Ship_to_Address_2' => '', 'Ship_to_City' => $shippingAddress->getCity(), 'Ship_to_Post_Code' => $shippingAddress->getPostcode(), 'Ship_to_Country_Region_Code' => $shippingAddress->getCountryId(), 'Phone_No' => $billingAddress->getTelephone(), 'Email' => $billingAddress->getEmail() ? $billingAddress->getEmail() : $order->getCustomerEmail(), 'Payment_Method_Code' => $order->getPayment()->getMethodInstance()->getCode(), 'Shipment_Method_Code' => $order->getShippingMethod(), 'Amount_Incl_VAT' => $creditmemo->getBaseGrandTotal(), 'Currency' => $creditmemo->getBaseCurrencyCode(), 'Shipping_amount_incl_tax' => $creditmemo->getBaseShippingInclTax(), 'Web_items' => array(), 'Web_creditmemo_id' => $creditmemo->getEntityId() ); $navision_items = array(); // var_dump ( $items );die(); $line_no = 1; foreach ( $items as $item ) { // var_dump ( $item );die(); // item type // barcode // name // quantity // unit price // line amount $navision_item = array ( 'Web_creditmemo_id' => $creditmemo->getEntityId(), 'Line_no' => $line_no, 'item_type' => 'item', 'Barcode' => $item->getSku(), 'name' => $item->getName(), 'Quantity' => $item->getQty(), 'Unit_Price' => $item->getBasePriceInclTax(), 'Line_Amount' => $item->getBaseRowTotal() ); array_push ( $navision_items, $navision_item ); $line_no++; } $navision_creditmemo['WebLines'] = $navision_items; return $navision_creditmemo; //var_dump ( $navision_creditmemo ); //die(); } public function putCreditmemos ( $creditmemos ) { foreach ( $creditmemos as $creditmemo ) { $creditmemo_page = $this->_config['creditmemo_page']; $this->_baseUrl = $this->_config['base_url'] . "/Page/"; //unset ( $order['WebLines'] ); //var_dump ( $order ); $this->createData ( $creditmemo_page, array ($creditmemo_page => $creditmemo ) ); // no exceptions - confirm the order $this->confirmCreditmemos ( array ( $creditmemo ) ); } var_dump ( $creditmemos ); //die(); } public function confirmOrders ( $orders ) { foreach ( $orders as $order ) { $orderId = $order['Web_orderno']; $this->_adapterPublisher->confirmOrder ( $orderId ); } } public function confirmCreditmemos ( $creditmemos ) { foreach ( $creditmemos as $creditmemo ) { } } // read back the updated information public function getOrders () { } 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 ) { // fetch product additional information ( pricing, etc ) $magentoProduct = new Varien_Object(); $price_page = $this->_config['prices_page']; $itemNo = $product->Item_no; $productPrices = array(); //die ( $price_page ); $prices = $this->readData ( $price_page, array ( 'Item_No' => $itemNo )); //$price = $this->readData ( $price_page ); //$productPrices[ $price->Sales_Code ] = $price->Unit_Price; /* var_dump ( $prices );die(); foreach ( $prices as $price ) { var_dump ( $price );die(); $productPrices[ $price->Sales_Code ] = $price->Unit_Price; } */ $magentoProduct->setPrices ( $prices ); return $magentoProduct; } public function getProducts () { $params = $this->_adapterParams; // fetch basic information about the product $soapProducts = $this->readData ( $this->_config['product_page'] ); return $soapProducts; //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/"; } } }