2016-09-22 13:40:56 +02:00
< ? php
class Print_Ean extends Module {
2016-09-23 13:09:34 +02:00
public function __construct ()
{
2016-09-22 13:40:56 +02:00
$this -> 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' );
}
2016-09-23 13:09:34 +02:00
public function install ()
{
2016-09-22 13:40:56 +02:00
$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 ;
}
2016-09-23 13:09:34 +02:00
public function uninstall ()
{
2016-09-22 13:40:56 +02:00
if ( parent :: uninstall () == false ) {
return false ;
}
return true ;
}
2016-09-23 13:09:34 +02:00
public function hookAdmin_Product_Postprocess ( $params )
{
2016-09-22 13:40:56 +02:00
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 ();
2016-09-23 13:09:34 +02:00
$this -> _updateAttributeEan ( $product -> id , $combination [ 'id_product_attribute' ], $ean_generate );
2016-09-22 13:40:56 +02:00
} else {
$ean_generate = $combination [ 'ean13' ];
}
2017-03-06 17:05:28 +01:00
$label = array (
'ref' => $product -> reference ,
2017-03-09 10:35:56 +01:00
'name' => $product -> name [ 2 ],
'attribute' => $combination [ 'attribute_name' ]
2017-03-06 17:05:28 +01:00
);
$this -> printEAN13 ( $ean_generate , $label , 1 , Configuration :: get ( 'LOGISTICS_QUEUE_' . ( int ) $cookie -> id_employee ));
2016-09-22 13:40:56 +02:00
}
} else {
if ( empty ( $product -> ean13 )) {
$ean_generate = $this -> _getEanNumber ();
2016-09-23 13:09:34 +02:00
$this -> _updateProductEan ( $product -> id , $ean_generate );
2016-09-22 13:40:56 +02:00
} else {
$ean_generate = $product -> ean13 ;
}
2017-03-06 17:05:28 +01:00
$label = array (
'ref' => $product -> reference ,
'name' => $product -> name [ 2 ],
2017-03-09 10:35:56 +01:00
'attribute' => " " ,
2017-03-06 17:05:28 +01:00
);
$this -> printEAN13 ( $ean_generate , $label , 1 , Configuration :: get ( 'LOGISTICS_QUEUE_' . ( int ) $cookie -> id_employee ));
2016-09-22 13:40:56 +02:00
}
2016-09-23 13:16:44 +02:00
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' ));
2016-09-23 13:09:34 +02:00
}
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 );
2016-09-29 16:11:06 +02:00
} else {
$ean_generate = $combination [ 'ean13' ];
2016-09-23 13:09:34 +02:00
}
2017-03-06 17:05:28 +01:00
$label = array (
'ref' => $product -> reference ,
2017-03-09 10:35:56 +01:00
'name' => $product -> name [ 2 ],
'attribute' => $combination [ 'attribute_name' ],
2017-03-06 17:05:28 +01:00
);
$this -> printEAN13 ( $ean_generate , $label , 1 , Configuration :: get ( 'LOGISTICS_QUEUE_' . ( int ) $cookie -> id_employee ));
2016-09-23 13:09:34 +02:00
}
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' ));
2016-09-22 13:40:56 +02:00
}
}
2016-09-23 13:09:34 +02:00
public function hookAdmin_Product_Extra ( $params )
{
global $cookie , $smarty , $currentIndex ;
2016-09-22 13:40:56 +02:00
$product = new Product (( int ) $params [ 'product' ] -> id );
$combinations = $product -> getAttributeCombinaisons (( int ) $cookie -> id_lang );
2016-09-23 13:09:34 +02:00
$_html .= ' < style type = " text/css " >
/**@import "'.__PS_BASE_URI__.'css/custom_admin.css";**/
2016-09-22 13:40:56 +02:00
. table - ean {
text - align : center ;
}
</ style > ' ;
2016-09-23 13:09:34 +02:00
$_html .= ' < div class = " tab-page " id = " step8 " >
< h4 class = " tab " > 8. '.$this->l(' EAN Management ').' </ h4 >
2016-09-22 13:40:56 +02:00
< fieldset >
< legend > '.$this->l(' Print & Generate EAN ').' </ legend >
< div > ' ;
if ( $combinations ){
$_html .= '<h4>' . $this -> l ( 'Le produit présente des attributs : ' ) . ' </ h4 >
< table border = " 0 " cellpadding = " 0 " cellspacing = " 0 " class = " table table-ean " >
< tbody >
< tr >
< th > ID </ th >
< th > Déclinaisons </ th >
< th > EAN13 </ th >
2016-09-23 13:09:34 +02:00
< th > Action </ th >
2016-09-22 13:40:56 +02:00
</ tr > ' ;
foreach ( $combinations as $key => $combination ) {
2016-09-23 13:09:34 +02:00
$_html .= ' < tr style = " background-color:#FFF " >
2016-09-22 13:40:56 +02:00
< td > '.$combination[' id_product_attribute '].' </ td >
< td > '.$combination[' attribute_name '].' </ td >
< td > '.(!empty($combination[' ean13 '])?$combination[' ean13 ']:$this->l(' Pas encore d\ 'ean' )) . ' </ td >
2016-09-23 13:09:34 +02:00
< td >< a class = " generateEanAttr " href = " # " data - id = " '. $combination['id_product_attribute'] .' " > '
. ( ! empty ( $combination [ 'ean13' ]) ? '<img src="../img/admin/printer.gif"/> ' . $this -> l ( 'Imprimer' ) : '<img src="../img/admin/add.gif"/> ' . $this -> l ( 'Généré/imprimer' )) .
' </ a ></ td >
2016-09-22 13:40:56 +02:00
</ tr > ' ;
}
2016-09-23 13:09:34 +02:00
$_html .= ' </ tbody ></ table >
< script type = " text/javascript " >
$ ( function () {
$ ( " .generateEanAttr " ) . click ( function ( e ){
e . preventDefault ();
id_attr = $ ( this ) . data ( " id " );
console . log ( id_attr );
window . location = " ?tab=AdminCatalog&id_category='.(!empty( $_REQUEST['id_category'] )? $_REQUEST['id_category'] :'1').'&updateproduct&id_product='. $product->id .'&generateattrean= " + id_attr + " &tabs=7&token='.Tools::getAdminTokenLite('AdminCatalog').' " ;
return false ;
});
});
</ script > ' ;
2016-09-22 13:40:56 +02:00
} else {
if ( ! empty ( $product -> ean13 )) {
$_html .= '
< h4 > '.$this->l(' Le produit présente déjà un code ean ').' : < strong > '.$product->ean13.' </ strong ></ h4 > ' ;
} else {
$_html .= '
< h4 > '.$this->l(' Le produit n\ 'a pas encore de code ean' ) . '</h4>' ;
}
}
$_html .= ' < div class = " clear " ></ div >
</ div >
2016-09-23 13:09:34 +02:00
< p >< button class = " button " type = " submit " name = " generateEan " > '.((!empty($product->ean13) && (!$combinations||empty($combinations)))?$this->l(' Lancer l\ 'impression' ) : $this -> l ( 'Lancer la génération/impression' )) . ' </ button ></ p >
2016-09-22 13:40:56 +02:00
</ fieldset >
</ div > ' ;
return $_html ;
}
private function _getEanNumber () {
// min 1111111111
$number = Configuration :: get ( 'BARCODE_NUMBER' ) + 1 ;
Configuration :: updateValue ( 'BARCODE_NUMBER' , $number );
return $number ;
}
2016-09-23 13:09:34 +02:00
public function _getAttributeCombination ( $id_product , $id_attribute )
{
return Db :: getInstance () -> getRow ( '
2017-03-09 10:35:56 +01:00
SELECT pa .* , al . `name` as `attribute_name`
2016-09-23 13:09:34 +02:00
FROM `'._DB_PREFIX_.'product_attribute` pa
2017-03-09 10:35:56 +01:00
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` )
2016-09-23 13:09:34 +02:00
WHERE pa . `id_product` = ' . (int)$id_product .'
AND pa . `id_product_attribute` = ' . (int)$id_attribute.'
2017-03-09 10:35:56 +01:00
AND al . `id_lang` = 2
2016-09-23 13:09:34 +02:00
' );
}
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
' );
}
2017-03-06 17:05:28 +01:00
public function printEAN13 ( $ean13 , $label , $quantity , $queue ) {
2016-09-22 13:40:56 +02:00
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 ();
2016-09-29 15:56:17 +02:00
// .'1'.(strlen($ean13) == 12? 'B': 'F').'0000000150340'.$ean13."\n"
2017-03-09 10:35:56 +01:00
// .'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"
2016-09-22 13:40:56 +02:00
$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 "
2017-03-09 10:35:56 +01:00
. '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 "
2016-09-22 13:40:56 +02:00
. 'Q' . sprintf ( '%04d' , $quantity ) . " \n "
. 'E' . " \n "
),
'queue' => $queue ,
))
);
}
}