name = 'print_ean';
$this->tab = 'administration';
$this->author = 'Antadis';
$this->version = '1';
parent::__construct();
$this->displayName = $this->l('Print Ean');
$this->description = $this->l('Allows to generate product ean and print it');
}
public function install()
{
$langs = Db::getInstance()->ExecuteS('
SELECT `id_lang`, `iso_code`
FROM `'._DB_PREFIX_.'lang`
');
# Add custom hooks
$hooks = array(
'admin_product_extra' => array('Display Admin Product extra', 'Called on Admin product to show extra tabs'),
'admin_product_postprocess' => array('Admin Product Post Process', 'Called on PostProcess Admin Product'),
);
foreach($hooks as $k => $v) {
if(count(Db::getInstance()->ExecuteS('
SELECT `id_hook`
FROM `'._DB_PREFIX_.'hook`
WHERE `name` = "'.$k.'"
LIMIT 1
')) == 0) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'hook`
VALUES (DEFAULT, "'.$k.'", "'.$v[0].'", "'.$v[1].'", 0, 0)
');
}
}
# Register hooks
if(!parent::install()
|| !$this->registerHook('admin_product_extra')
|| !$this->registerHook('admin_product_postprocess')) {
return false;
}
return true;
}
public function uninstall()
{
if(parent::uninstall() == false) {
return false;
}
return true;
}
public function hookAdmin_Product_Postprocess($params)
{
if(Tools::isSubmit('generateEan') && Tools::getValue('id_product')){
global $cookie, $smarty, $currentIndex;
$product = new Product(Tools::getValue('id_product'));
$combinations = $product->getAttributeCombinaisons($cookie->id_lang);
if ($combinations) {
foreach ($combinations as $key => $combination) {
if (empty($combination['ean13'])) {
$ean_generate = $this->_getEanNumber();
$this->_updateAttributeEan($product->id, $combination['id_product_attribute'], $ean_generate);
} else {
$ean_generate = $combination['ean13'];
}
$label = array(
'ref' => $product->reference,
'name' => $product->name[2],
'attribute' => $combination['attribute_name']
);
$this->printEAN13($ean_generate,$label,1,Configuration::get('LOGISTICS_QUEUE_'.(int) $cookie->id_employee));
}
} else {
if (empty($product->ean13)) {
$ean_generate = $this->_getEanNumber();
$this->_updateProductEan($product->id, $ean_generate);
} else {
$ean_generate = $product->ean13;
}
$label = array(
'ref' => $product->reference,
'name' => $product->name[2],
'attribute' => "",
);
$this->printEAN13($ean_generate,$label,1,Configuration::get('LOGISTICS_QUEUE_'.(int) $cookie->id_employee));
}
Tools::redirectAdmin($currentIndex.'&id_product='.(int)$product->id.'&id_category='.(!empty($_REQUEST['id_category'])?$_REQUEST['id_category']:'1').'&updateproduct'.'&tabs=7&token='.Tools::getAdminTokenLite('AdminCatalog'));
}
if (Tools::getValue('generateattrean') && Tools::getValue('id_product')) {
global $cookie, $smarty, $currentIndex;
$id_attribute = Tools::getValue('generateattrean');
$product = new Product(Tools::getValue('id_product'));
$combination = $this->_getAttributeCombination((int)$product->id, (int)$id_attribute);
if ($combination) {
if(empty($combination['ean13'])) {
$ean_generate = $this->_getEanNumber();
$this->_updateAttributeEan((int)$product->id, $id_attribute, $ean_generate);
} else {
$ean_generate = $combination['ean13'];
}
$label = array(
'ref' => $product->reference,
'name' => $product->name[2],
'attribute' => $combination['attribute_name'],
);
$this->printEAN13($ean_generate,$label,1,Configuration::get('LOGISTICS_QUEUE_'.(int) $cookie->id_employee));
}
Tools::redirectAdmin($currentIndex.'&id_product='.(int)$product->id.'&id_category='.(!empty($_REQUEST['id_category'])?$_REQUEST['id_category']:'1').'&updateproduct'.'&tabs=7&token='.Tools::getAdminTokenLite('AdminCatalog'));
}
}
public function hookAdmin_Product_Extra($params)
{
global $cookie, $smarty, $currentIndex;
$product = new Product((int)$params['product']->id);
$combinations = $product->getAttributeCombinaisons((int)$cookie->id_lang);
$_html .= '';
$_html .= '
8. '.$this->l('EAN Management').'
';
return $_html;
}
private function _getEanNumber() {
// min 1111111111
$number = Configuration::get('BARCODE_NUMBER') + 1;
Configuration::updateValue('BARCODE_NUMBER', $number);
return $number;
}
public function _getAttributeCombination($id_product, $id_attribute)
{
return Db::getInstance()->getRow('
SELECT pa.*, al.`name` as `attribute_name`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` ac ON (ac.`id_product_attribute` = pa.`id_product_attribute`)
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` al ON (al.`id_attribute` = ac.`id_attribute`)
WHERE pa.`id_product` = ' . (int)$id_product .'
AND pa.`id_product_attribute` = ' . (int)$id_attribute.'
AND al.`id_lang` = 2
');
}
public function _updateAttributeEan($id_product, $id_attribute, $ean)
{
Db::getInstance()->Execute('
UPDATE
`'._DB_PREFIX_.'product_attribute`
SET `ean13` = '. $ean . '
WHERE `id_product` = ' . (int)$id_product .'
AND `id_product_attribute` = ' . (int)$id_attribute
);
Db::getInstance()->Execute('
UPDATE
`'._DB_PREFIX_.'order_detail`
SET `product_ean13` = "'. pSQL($ean) .'"
WHERE `product_id` = '. (int)$id_product .'
AND `product_attribute_id` = '. (int)$id_attribute
);
}
public function _updateProductEan($id_product, $ean)
{
Db::getInstance()->execute('
UPDATE
`'._DB_PREFIX_.'product`
SET `ean13` = '. $ean . '
WHERE `id_product` = ' . (int)$id_product
);
Db::getInstance()->Execute('
UPDATE
`'._DB_PREFIX_.'order_detail`
SET `product_ean13` = "'. pSQL($ean) .'"
WHERE `product_id` = '. (int)$id_product .'
AND `product_attribute_id` = 0
');
}
public function printEAN13($ean13, $label, $quantity, $queue) {
if((string) $queue == '') {
return;
}
if($quantity > 30 || $quantity < 1) {
$quantity = 1;
}
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/OptionInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/AbstractOption.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/Processor/CommandProcessingInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/PrefixableCommandInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/CommandInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/AbstractCommand.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/PrefixableCommand.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Command/PubSubPublish.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/SingleConnectionInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Helpers.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/PredisException.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/CommunicationException.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionException.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/AbstractConnection.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/StreamConnection.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionParametersInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionParameters.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionFactoryInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Connection/ConnectionFactory.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Profile/ServerProfileInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Profile/ServerProfile.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Profile/ServerVersion26.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientCluster.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientPrefix.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientExceptions.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientReplication.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientConnectionFactory.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientProfile.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientOptionsInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Option/ClientOptions.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/BasicClientInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/ClientInterface.php';
require_once dirname(__FILE__).'/libs/predis/lib/Predis/Client.php';
$redis = new Predis\Client();
// .'1'.(strlen($ean13) == 12? 'B': 'F').'0000000150340'.$ean13."\n"
// .'1'.(strlen($ean13) == 12? 'B': (strlen($ean13) == 13? 'F': 'E')).'0000000150340'.$ean13."\n"
// .'1E022000015'.sprintf('%04d', (int) ((1020 - round((5.5 * strlen($ean) + 35) * 0.42 * 10)) / 2)).'B'.$ean13."\n"
$redis->publish(
$queue,
json_encode(array(
'data' => base64_encode(
chr(2).'m'."\n"
.chr(2).'O0100'."\n"
.chr(2).'L'."\n"
.'C0000'."\n"
.'D11'."\n"
.chr(2).'L'."\n"
.'121100002500'.(strlen($label['ref']) > 20? '330': '380').substr(str_replace(array('é','è','à','ï','ù'),array('e','e','a','i','u'),$label['ref']),0,30)."\n"
.'121100002000300'.substr(str_replace(array('é','è','à','ï','ù'),array('e','e','a','i','u'),$label['name']),0,30)."\n"
.'121100001500340'.substr(str_replace(array('é','è','à','ï','ù'),array('e','e','a','i','u'),$label['attribute']),0,30)."\n"
.'1E0000000150'.(strlen($ean13) == 12? '300': (strlen($ean13) == 13? '300': '340')).$ean13."\n"
.'Q'.sprintf('%04d', $quantity)."\n"
.'E'."\n"
),
'queue' => $queue,
))
);
}
}