Skip to content

Enable numeric skus, and sku/ids intersection #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
class Openlabs_OpenERPConnector_Helper_Data extends Mage_Core_Helper_Abstract
{
//Linux4ever_MagentoXtender_Model_Api
}
<?php
class Openlabs_OpenERPConnector_Helper_Data extends Mage_Core_Helper_Abstract
{
//Linux4ever_MagentoXtender_Model_Api
}
?>
52 changes: 52 additions & 0 deletions app/code/community/Openlabs/OpenERPConnector/Helper/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Created because the regular expression causes some issues with the ERP. When you have
* numeric references (SKUs) such expresion (only try to find products by sku when it is not numeric)
* causes issues.
*
* @author Daniel Lozano Morales [email protected]
*/
class Openlabs_OpenERPConnector_Helper_Product extends Mage_Catalog_Helper_Product
{
/**
* Return loaded product instance
*
* @param int|string $productId (SKU or ID)
* @param int $store
* @param string $identifierType
* @return Mage_Catalog_Model_Product
*/
public function getProduct($productId, $store, $identifierType = null)
{
/** @var $product Mage_Catalog_Model_Product */
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore($store)->getId());

$expectedIdType = false;
if ($identifierType === null) {
/**
* I didnt deleted the regular expression, but i commented it.
* It used to cause issues when your Magento SKU is numeric because Magento treats it as an integer.
* @author Daniel Lozano Morales [email protected]
*/
if (is_string($productId) /*&& !preg_match("/^[+-]?[1-9][0-9]*$|^0$/", $productId)*/) {
$expectedIdType = 'sku';
}
}

if ($identifierType == 'sku' || $expectedIdType == 'sku') {
$idBySku = $product->getIdBySku($productId);
if ($idBySku) {
$productId = $idBySku;
} else if ($identifierType == 'sku') {
// Return empty product because it was not found by originally specified SKU identifier
return $product;
}
}

if ($productId && is_numeric($productId)) {
$product->load((int) $productId);
}

return $product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @author Daniel Lozano Morales <[email protected]>
*/
class Openlabs_OpenERPConnector_Model_Adminhtml_Source_Attribute
{
/**
* Colección de atributos
*
* @var Mage_Catalog_Model_Resource_Product_Attribute_Collection
*/
private $collection;

public function __construct()
{
$this->collection = Mage::getResourceModel('catalog/product_attribute_collection');
}

/**
* Devolver array con opciones para el multiselect
*
* @return array
*/
public function toOptionArray()
{
$result = array();

// Ordenar colección alfabéticamente.
$attributes = $this->collection->setOrder('frontend_label', 'ASC')->getItems();

if (count($attributes) > 0) {
foreach ($attributes as $attribute) {
$value = $attribute->getData('attribute_code');
$label = $attribute->getData('frontend_label');

if ($label !== null && $value !== null) {
$result[] = array(
'value' => $value,
'label' => $label
);
}
}
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* @author Daniel Lozano Morales <[email protected]>
*/
class Openlabs_OpenERPConnector_Model_Adminhtml_Source_Categories
{
/**
* Colección de categorías
*
* @var Mage_Catalog_Model_Category_Collection
*/
private $collection;

/**
* Constructor
*/
public function __construct()
{
/**
* Obtener el store id desde la parte de administración. No hay
* ninguna store cargada por lo que hay que buscar en la
* configuración
*/
if (strlen($code = Mage::getSingleton('adminhtml/config_data')->getStore())) {
$store_id = Mage::getModel('core/store')->load($code)->getId();
} elseif (strlen($code = Mage::getSingleton('adminhtml/config_data')->getWebsite())) {
$website_id = Mage::getModel('core/website')->load($code)->getId();
$store_id = Mage::app()->getWebsite($website_id)->getDefaultStore()->getId();
} else {
$store_id = 0;
}

$store = Mage::getModel('core/store')->load($store_id);
$rootId = $store->getRootCategoryId();
$category = Mage::getModel('catalog/category')->load($rootId);
/**
* Obtener colección de categorias
*/
$collection = $category->getCollection()
->addAttributeToSelect(array('name', 'entity_id' , 'is_active'))
->addFieldToFilter('is_active', array('in' => array(0, 1, 2, 3, 4)));

/** @var Mage_Catalog_Model_Category_Collection */
$this->collection = $collection;
}

/**
* Devolver array de opciones para el selector
* múltiple
*
* @return array
*/
public function toOptionArray()
{
// Valor para vaciar las categorías.
$array = array(
array('value' => null, 'label' => 'None')
);
// TODO: optimizar, eso se ejecuta cada vez que se carga esta opción....
if (count($this->collection) > 0) {
foreach ($this->collection as $category) {
$array[] = array(
'value' => $category->getId(),
'label' => $category->getName() . '(' . $category->getId() . ')'
);
}
}
return $array;
}
}
67 changes: 67 additions & 0 deletions app/code/community/Openlabs/OpenERPConnector/Model/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
*
* @author Mohammed NAHHAS
* @package Openlabs_OpenERPConnector
*
*/

class Openlabs_OpenERPConnector_Model_Observer extends Mage_Core_Model_Abstract
{
/* Initialize attribtue 'imported' if version < 1.4.0.0 */
public function initImported($observer) {
if(str_replace('.','',Mage::getVersion()) < 1400) {
$order = $observer->getOrder();
try {
$order->setImported(0)->save();
}catch (Exception $e) {
/* If logs are enabled (backend : system->configuration->developer->logSettings), it creates a file named OpenErp_Connector.log in /var/log/ which contains the errors */
Mage::log('Error, order increment_id = '.$order->getIncrementId().', attribute "imported" was not initialized - error : '.$e->getMessage(), null, 'OpenErp_Connector.log');
}
$order->setImported(0)->save();
}
}
/**
* Función para solucionar el problema de importación con el coste de pago por contra reembolso.
* La solución consiste en añadir una línea al pedido, de un producto oculto.
*/
public function addCashOnDelivery($observer)
{
$order = $observer->getOrder();
$paymentMethodCode = $order->getPayment()->getMethodInstance()->getCode();

if ($paymentMethodCode == 'msp_cashondelivery')
{
$fakeProductId = Mage::getModel('catalog/product')->getIdBySku('CASHONDELIVERY');

if ($fakeProductId)
{
$fakeProduct = Mage::getModel('catalog/product')->load($fakeProductId);

$attributes = $order->getData();
$cashOnDeliveryFee = (float) $attributes['msp_cashondelivery'];
$cashOnDeliveryFee = number_format($cashOnDeliveryFee, 4, '.', '');

$orderItem = Mage::getModel('sales/order_item')
->setStoreId($order->getStoreId())
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($fakeProduct->getId())
->setProductType($fakeProduct->getTypeId())
->setQtyBackordered(NULL)
->setQtyOrdered(1)
->setTotalQtyOrdered(1)
->setName($fakeProduct->getName())
->setSku($fakeProduct->getSku())
->setPrice($cashOnDeliveryFee)
->setBasePrice($cashOnDeliveryFee)
->setOriginalPrice($cashOnDeliveryFee)
->setRowTotal($cashOnDeliveryFee)
->setBaseRowTotal($cashOnDeliveryFee);

$order->addItem($orderItem);
$order->save();
}
}
}
}
Loading