2016-02-22 12:33:51 +01:00
< ? php
2018-01-05 15:14:30 +01:00
if ( ! defined ( '_PS_VERSION_' )) {
2016-03-09 16:33:48 +01:00
exit ;
2016-02-22 12:33:51 +01:00
}
require_once dirname ( __FILE__ ) . '/../privatesales/Sale.php' ;
require_once dirname ( __FILE__ ) . '/models/generatebarcode.php' ;
2018-01-05 15:14:30 +01:00
require_once PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php' ;
2016-03-10 10:40:06 +01:00
2018-01-05 15:14:30 +01:00
class AdminLabelGenerate extends AdminTab
{
2016-03-09 16:33:48 +01:00
public $_html = '' ;
public function display () {
global $cookie ;
2016-04-08 10:27:46 +02:00
$rack = Tools :: getValue ( 'rack' );
2016-03-09 16:33:48 +01:00
$current_sale = Tools :: getValue ( 'id_sale' );
2016-04-08 10:27:46 +02:00
if ( $rack ) {
// button commenté en fin de page
$barcode = new GenerateBarcode ();
$barcode -> createRackFolder ();
$rackCodes = array ();
2016-04-12 16:11:08 +02:00
for ( $i = 0 ; $i < 21 ; $i ++ ) {
2016-04-08 10:27:46 +02:00
for ( $j = 1 ; $j < 6 ; $j ++ ) {
$name = $i . $j ;
$barcode -> generateRack ( $name );
$rackCodes [] = $name ;
}
}
$barcode -> printRackPDF ( $rackCodes );
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/rack/rack.pdf' )) {
2017-01-30 16:53:12 +01:00
$this -> displaySucess ( 'Rack générés : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/rack/rack.pdf">Télécharger le PDF rack</a>' );
2016-04-08 10:27:46 +02:00
}
2018-01-08 16:45:38 +01:00
}
elseif ( $current_sale && Tools :: getValue ( 'generateRef' )) {
2016-12-27 10:33:56 +01:00
$sale = new Sale (( int ) $current_sale );
2017-02-08 12:04:06 +01:00
$big_label = Tools :: getValue ( 'big_label' );
2016-12-27 10:33:56 +01:00
if ( ! Validate :: isLoadedObject ( $sale )) {
throw new Exception ( 'This sale doesnt exist' );
} else {
$products = $sale -> getProducts ();
$products_generate = array ();
$barcode = new GenerateBarcode ();
$barcode -> id_sale = $sale -> id ;
$barcode -> createFolder ( $barcode -> id_sale );
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
2016-12-27 10:33:56 +01:00
WHERE od . `product_id` = ' . (int)$combination[' id_product '] .'
2017-04-13 14:10:43 +02:00
AND od . `product_attribute_id` = ' . (int)$combination[' id_product_attribute '].'
AND o . valid != 0 '
2016-12-27 10:33:56 +01:00
);
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
if ( $quantity > 0 ) {
$barcode -> assocProduct ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $quantity , $product -> reference );
}
}
} else {
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0 AND o . valid != 0 '
2016-12-27 10:33:56 +01:00
);
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
if ( $quantity > 0 ) {
$barcode -> assocProduct ( $product -> id , NULL , $quantity , $product -> reference );
}
}
}
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/reference.pdf' )) {
2017-01-30 16:53:12 +01:00
$this -> displayWarning ( 'Les références ont déjà été générées<br>Dernières ref générées : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/reference.pdf">Télécharger le PDF</a>' );
2016-12-27 10:33:56 +01:00
} else {
2017-02-08 12:04:06 +01:00
if ( $barcode -> printPDF ( $big_label , 'reference' )) {
2017-02-03 16:55:47 +01:00
$this -> displaySucess ( 'Génération terminée : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/reference.pdf">Télécharger le PDF</a>' );
} else {
$this -> _html .= $this -> displayError ( 'Pas de produit à générer !' );
}
2016-12-27 10:33:56 +01:00
}
}
2018-01-08 16:45:38 +01:00
}
elseif ( $current_sale && Tools :: getValue ( 'deleteEAN' )) {
2016-04-04 15:20:32 +02:00
$sale = new Sale (( int ) $current_sale );
if ( ! Validate :: isLoadedObject ( $sale )) {
throw new Exception ( 'This sale doesnt exist' );
} else {
$products = $sale -> getProducts ();
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
if ( ! empty ( $combination [ 'ean13' ])) {
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = " "
2016-04-04 15:20:32 +02:00
WHERE `id_product` = ' . (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `id_product_attribute` = ' . (int)$combination[' id_product_attribute ' ]
2016-04-04 15:20:32 +02:00
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " "
2016-04-04 15:20:32 +02:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = '. (int)$combination[' id_product_attribute ' ]
2016-04-04 15:20:32 +02:00
);
}
}
} else {
if ( ! empty ( $product -> ean13 )) {
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product`
SET `ean13` = " "
2016-04-04 15:20:32 +02:00
WHERE `id_product` = ' . ( int ) $product -> id
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " "
2016-04-04 15:20:32 +02:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = 0
2016-04-04 15:20:32 +02:00
' );
}
}
}
2017-01-30 16:53:12 +01:00
$this -> _html .= $this -> displaySuccess ( 'Suppression terminée' );
2016-04-04 15:20:32 +02:00
}
2018-01-05 15:14:30 +01:00
}
// Generate ALL EAN (sell quantities)
elseif ( $current_sale && Tools :: getValue ( 'generateALLEAN' )) {
2017-02-03 16:55:47 +01:00
$sale = new Sale (( int ) $current_sale );
2017-02-08 12:04:06 +01:00
$big_label = Tools :: getValue ( 'big_label' );
2017-02-03 16:55:47 +01:00
if ( ! Validate :: isLoadedObject ( $sale )) {
throw new Exception ( 'This sale doesnt exist' );
} else {
$products = $sale -> getProducts ();
$ean_generate = '' ;
$products_generate = array ();
$barcode = new GenerateBarcode ();
$barcode -> id_sale = $sale -> id ;
$barcode -> createFolder ( $barcode -> id_sale );
2018-01-08 16:45:38 +01:00
2017-02-03 16:55:47 +01:00
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
$quantity = 0 ;
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
2017-02-03 16:55:47 +01:00
WHERE od . `product_id` = ' . (int)$combination[' id_product '] .'
2017-04-13 14:10:43 +02:00
AND od . `product_attribute_id` = ' . (int)$combination[' id_product_attribute '].'
AND o . `valid` != 0 '
2017-02-03 16:55:47 +01:00
);
foreach ( $result_quantities as $od ) {
$quantity += (( int ) $od [ 'product_quantity' ] - ( int ) $od [ 'product_quantity_reinjected' ]);
}
2018-01-08 16:45:38 +01:00
if ( $quantity > 0 ){
2017-02-03 16:55:47 +01:00
if ( empty ( $combination [ 'ean13' ])) {
$ean_generate = $barcode -> generateBarcode ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $product -> reference );
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = '. $ean_generate . '
2017-02-03 16:55:47 +01:00
WHERE `id_product` = ' . (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `id_product_attribute` = ' . (int)$combination[' id_product_attribute ' ]
2017-02-03 16:55:47 +01:00
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
2017-02-03 16:55:47 +01:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = '. (int)$combination[' id_product_attribute ' ]
2017-02-03 16:55:47 +01:00
);
} else {
$ean_generate = $barcode -> generateBarcode ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $product -> reference , $combination [ 'ean13' ]);
}
$barcode -> assocProduct ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $quantity , $product -> reference );
}
}
} else {
$quantity = 0 ;
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0 AND o . `valid` != 0 '
2017-02-03 16:55:47 +01:00
);
foreach ( $result_quantities as $od ) {
$quantity += (( int ) $od [ 'product_quantity' ] - ( int ) $od [ 'product_quantity_reinjected' ]);
}
2018-01-05 15:14:30 +01:00
if ( $quantity > 0 ) {
2017-02-03 16:55:47 +01:00
if ( empty ( $product -> ean13 )) {
$ean_generate = $barcode -> generateBarcode ( $product -> id , NULL , $product -> reference );
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product`
SET `ean13` = '. $ean_generate . '
2017-02-03 16:55:47 +01:00
WHERE `id_product` = ' . ( int ) $product -> id
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
2017-02-03 16:55:47 +01:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = 0
2017-02-03 16:55:47 +01:00
' );
} else {
$ean_generate = $barcode -> generateBarcode ( $product -> id , NULL , $product -> reference , $product -> ean13 );
}
$barcode -> assocProduct ( $product -> id , NULL , $quantity , $product -> reference );
}
}
}
if ( ! empty ( $ean_generate )) {
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all.pdf' )) {
2017-02-03 16:55:47 +01:00
unlink ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all.pdf' );
}
2017-02-08 12:04:06 +01:00
if ( $barcode -> printPDF ( $big_label , 'barcode_all' )) {
2018-01-10 12:49:28 +01:00
$this -> _html .= $this -> displaySuccess ( 'Génération terminée :<br><a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode_all.pdf?v=' . time () . '">Télécharger le PDF</a>' );
2017-02-03 16:55:47 +01:00
} else {
$this -> _html .= $this -> displayError ( 'Pas de produit à générer !' );
}
} else {
$message = 'Aucun ean trouvé ou généré !' ;
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all.pdf' )) {
2018-01-10 12:49:28 +01:00
$message .= '<br>Dernières étiquettes générées : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode_all.pdf?v=' . time () . '">Télécharger le PDF</a>' ;
2017-02-03 16:55:47 +01:00
}
$this -> _html .= $this -> displayWarning ( $message );
}
}
2018-01-05 15:14:30 +01:00
}
// Generate ALL EAN (initial quantities)
elseif ( $current_sale && Tools :: getValue ( 'generateALLEAN_initial' )) {
2017-02-08 12:04:06 +01:00
$sale = new Sale (( int ) $current_sale );
$big_label = Tools :: getValue ( 'big_label' );
2016-05-26 13:01:58 +02:00
2018-01-08 16:45:38 +01:00
if ( ! Validate :: isLoadedObject ( $sale )) {
2016-05-26 13:01:58 +02:00
throw new Exception ( 'This sale doesnt exist' );
} else {
$products = $sale -> getProducts ();
$ean_generate = '' ;
$products_generate = array ();
$barcode = new GenerateBarcode ();
$barcode -> id_sale = $sale -> id ;
$barcode -> createFolder ( $barcode -> id_sale );
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
2017-02-03 16:55:47 +01:00
$quantity = ( int ) $combination [ 'quantity' ];
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
2017-02-03 16:55:47 +01:00
WHERE od . `product_id` = ' . (int)$combination[' id_product '] .'
2017-04-13 14:10:43 +02:00
AND od . `product_attribute_id` = ' . (int)$combination[' id_product_attribute '].'
AND o . `valid` != 0 '
2017-02-03 16:55:47 +01:00
);
foreach ( $result_quantities as $od ) {
$quantity += (( int ) $od [ 'product_quantity' ] - ( int ) $od [ 'product_quantity_reinjected' ]);
}
2016-05-26 13:01:58 +02:00
if ( empty ( $combination [ 'ean13' ])) {
$ean_generate = $barcode -> generateBarcode ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $product -> reference );
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = '. $ean_generate . '
2016-05-26 13:01:58 +02:00
WHERE `id_product` = ' . (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `id_product_attribute` = ' . (int)$combination[' id_product_attribute ' ]
2016-05-26 13:01:58 +02:00
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
2016-05-26 13:01:58 +02:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = '. (int)$combination[' id_product_attribute ' ]
2016-05-26 13:01:58 +02:00
);
2017-02-03 16:55:47 +01:00
} else {
$ean_generate = $barcode -> generateBarcode ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $product -> reference , $combination [ 'ean13' ]);
2016-05-26 13:01:58 +02:00
}
2017-02-03 16:55:47 +01:00
$barcode -> assocProduct ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $quantity , $product -> reference );
2016-05-26 13:01:58 +02:00
}
} else {
2017-02-03 16:55:47 +01:00
$quantity = $product -> quantity ;
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0 AND o . `valid` != 0 '
2017-02-03 16:55:47 +01:00
);
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
2016-05-26 13:01:58 +02:00
if ( empty ( $product -> ean13 )) {
$ean_generate = $barcode -> generateBarcode ( $product -> id , NULL , $product -> reference );
Db :: getInstance () -> execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'product`
SET `ean13` = '. $ean_generate . '
2016-05-26 13:01:58 +02:00
WHERE `id_product` = ' . ( int ) $product -> id
);
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
2016-05-26 13:01:58 +02:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-05 15:14:30 +01:00
AND `product_attribute_id` = 0
2016-05-26 13:01:58 +02:00
' );
2017-02-03 16:55:47 +01:00
} else {
$ean_generate = $barcode -> generateBarcode ( $product -> id , NULL , $product -> reference , $product -> ean13 );
2016-05-26 13:01:58 +02:00
}
2017-02-03 16:55:47 +01:00
$barcode -> assocProduct ( $product -> id , NULL , $quantity , $product -> reference );
2016-05-26 13:01:58 +02:00
}
}
if ( ! empty ( $ean_generate )) {
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all_initial.pdf' )) {
2017-02-03 16:55:47 +01:00
unlink ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all_initial.pdf' );
}
2017-02-08 12:04:06 +01:00
if ( $barcode -> printPDF ( $big_label , 'barcode_all_initial' )) {
2018-01-10 12:49:28 +01:00
$this -> _html .= $this -> displaySuccess ( 'Génération terminée :<br><a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode_all_initial.pdf?v=' . time () . '">Télécharger le PDF</a>' );
2017-02-03 16:55:47 +01:00
} else {
$this -> _html .= $this -> displayError ( 'Pas de produit à générer !' );
}
2016-05-26 13:01:58 +02:00
} else {
2017-02-17 15:23:58 +01:00
$message = 'Aucun ean trouvé ou généré !' ;
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode_all_initial.pdf' )) {
2018-01-10 12:49:28 +01:00
$message .= '<br>Dernières étiquettes générées : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode_all_initial.pdf?v=' . time () . '">Télécharger le PDF</a>' ;
2016-05-26 13:01:58 +02:00
}
2017-01-30 16:53:12 +01:00
$this -> _html .= $this -> displayWarning ( $message );
2016-05-26 13:01:58 +02:00
}
}
2018-01-05 15:14:30 +01:00
}
// Generate EAN on missing products
elseif ( $current_sale && Tools :: getValue ( 'generateEAN' )) {
2016-03-09 16:33:48 +01:00
$sale = new Sale (( int ) $current_sale );
2017-02-08 12:04:06 +01:00
$big_label = Tools :: getValue ( 'big_label' );
2016-03-09 16:33:48 +01:00
if ( ! Validate :: isLoadedObject ( $sale )) {
throw new Exception ( 'This sale doesnt exist' );
2018-01-05 15:14:30 +01:00
}
else {
2016-03-09 16:33:48 +01:00
$products = $sale -> getProducts ();
$ean_generate = '' ;
$products_generate = array ();
$barcode = new GenerateBarcode ();
$barcode -> id_sale = $sale -> id ;
$barcode -> createFolder ( $barcode -> id_sale );
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
2018-01-05 15:14:30 +01:00
// Product with attributes
2016-03-09 16:33:48 +01:00
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
2018-01-08 16:45:38 +01:00
if ( empty ( $combination [ 'ean13' ])) {
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$combination[' id_product '] .'
AND od . `product_attribute_id` = ' . (int)$combination[' id_product_attribute '].'
AND o . `valid` != 0 '
);
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
if ( $quantity > 0 ) {
$ean_generate = $barcode -> generateBarcode ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $product -> reference );
$barcode -> assocProduct ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $quantity , $product -> reference );
Db :: getInstance () -> execute ( '
UPDATE `'._DB_PREFIX_.'product_attribute`
SET `ean13` = '. $ean_generate . '
WHERE `id_product` = ' . (int)$product->id .'
AND `id_product_attribute` = ' . (int)$combination[' id_product_attribute ' ]
);
Db :: getInstance () -> Execute ( '
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
WHERE `product_id` = '. (int)$product->id .'
AND `product_attribute_id` = '. (int)$combination[' id_product_attribute ' ]
);
}
}
}
}
// Product without attributes
else {
if ( empty ( $product -> ean13 )) {
2016-03-09 16:33:48 +01:00
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
2017-04-13 14:10:43 +02:00
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
2018-01-08 16:45:38 +01:00
WHERE od . `product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0 AND o . `valid` != 0 '
2016-03-09 16:33:48 +01:00
);
2018-01-08 16:45:38 +01:00
2016-03-09 16:33:48 +01:00
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
2018-01-08 16:45:38 +01:00
2018-01-05 15:14:30 +01:00
if ( $quantity > 0 ) {
2018-01-08 16:45:38 +01:00
$ean_generate = $barcode -> generateBarcode ( $product -> id , NULL , $product -> reference );
$barcode -> assocProduct ( $product -> id , NULL , $quantity , $product -> reference );
2016-03-09 16:33:48 +01:00
Db :: getInstance () -> execute ( '
2018-01-08 16:45:38 +01:00
UPDATE `'._DB_PREFIX_.'product`
2018-01-05 15:14:30 +01:00
SET `ean13` = '. $ean_generate . '
2018-01-08 16:45:38 +01:00
WHERE `id_product` = ' . ( int ) $product -> id
2016-03-09 16:33:48 +01:00
);
2016-03-10 09:53:45 +01:00
Db :: getInstance () -> Execute ( '
2018-01-05 15:14:30 +01:00
UPDATE `'._DB_PREFIX_.'order_detail`
SET `product_ean13` = " '. pSQL( $ean_generate ) .' "
2016-03-10 09:53:45 +01:00
WHERE `product_id` = '. (int)$product->id .'
2018-01-08 16:45:38 +01:00
AND `product_attribute_id` = 0
' );
2016-03-09 16:33:48 +01:00
}
}
}
}
if ( ! empty ( $ean_generate )) {
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode.pdf' )) {
2017-02-03 16:55:47 +01:00
unlink ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode.pdf' );
}
2018-01-10 12:49:28 +01:00
if ( $barcode -> printPDF ( $big_label )) {
$this -> _html .= $this -> displaySuccess ( 'Génération terminée :<br><a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode.pdf?v=' . time () . '">Télécharger le PDF</a>' );
2017-02-03 16:55:47 +01:00
} else {
$this -> _html .= $this -> displayError ( 'Pas de produit à générer !' );
}
2016-03-09 16:33:48 +01:00
} else {
2017-01-30 16:53:12 +01:00
$message = 'Tous les produits de la vente ont déjà des EANS' ;
2018-01-05 15:14:30 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/barcode.pdf' )) {
2018-01-10 12:49:28 +01:00
$message .= '<br>Dernières étiquettes générées : <a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/barcode.pdf?v=' . time () . '">Télécharger le PDF</a>' ;
2016-03-09 16:33:48 +01:00
}
2017-01-30 16:53:12 +01:00
$this -> _html .= $this -> displayWarning ( $message );
2016-03-09 16:33:48 +01:00
}
}
}
2018-03-06 14:34:46 +01:00
// Generate label with ref and attribute name
elseif ( $current_sale && Tools :: getValue ( 'generateIdentification' )) {
$sale = new Sale (( int ) $current_sale );
$big_label = Tools :: getValue ( 'big_label' );
if ( ! Validate :: isLoadedObject ( $sale )) {
throw new Exception ( 'This sale doesnt exist' );
}
else {
$products = $sale -> getProducts ();
$ean_generate = '' ;
$products_generate = array ();
$barcode = new GenerateBarcode ();
$barcode -> id_sale = $sale -> id ;
$barcode -> createFolder ( $barcode -> id_sale );
foreach ( $products as $key => $id_product ) {
$product = new Product ( $id_product , FALSE , ( int ) $cookie -> id_lang );
$attributes = array ();
$combinations = $product -> getAttributeCombinaisons ( $cookie -> id_lang );
// Product with attributes
if ( $combinations ) {
foreach ( $combinations as $key => $combination ) {
2018-03-08 14:54:52 +01:00
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$combination[' id_product '] .'
AND od . `product_attribute_id` = ' . (int)$combination[' id_product_attribute '].'
AND o . `valid` != 0 '
);
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
if ( $quantity > 0 ) {
$barcode -> assocProduct ( $combination [ 'id_product' ], $combination [ 'id_product_attribute' ], $quantity , $product -> reference );
}
2018-03-06 14:34:46 +01:00
}
}
// Product without attributes
else {
2018-03-08 14:54:52 +01:00
$result_quantities = Db :: getInstance () -> executeS ( '
SELECT od . `product_quantity` , od . `product_quantity_reinjected`
FROM '._DB_PREFIX_.' order_detail od
LEFT JOIN '._DB_PREFIX_.' orders o ON ( o . `id_order` = od . `id_order` )
WHERE od . `product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0 AND o . `valid` != 0 '
);
2018-03-06 14:34:46 +01:00
2018-03-08 14:54:52 +01:00
$quantity = 0 ;
foreach ( $result_quantities as $od ) {
$quantity += ( $od [ 'product_quantity' ] - $od [ 'product_quantity_reinjected' ]);
}
if ( $quantity > 0 ) {
$barcode -> assocProduct ( $product -> id , null , $quantity , $product -> reference );
}
2018-03-06 14:34:46 +01:00
}
}
2018-03-08 14:57:54 +01:00
2018-03-08 14:54:52 +01:00
if ( file_exists ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/identification.pdf' )) {
unlink ( _PS_MODULE_DIR_ . 'labelgenerate/img/' . $barcode -> id_sale . '/identification.pdf' );
}
if ( $barcode -> printIdentification ( $big_label )) {
$this -> _html .= $this -> displaySuccess ( 'Génération terminée :<br><a class="btn btn-primary btn-xs" target="_blank" href="/modules/labelgenerate/img/' . $barcode -> id_sale . '/identification.pdf?v=' . time () . '">Télécharger le PDF</a>' );
} else {
$this -> _html .= $this -> displayError ( 'Pas de produit à générer !' );
}
2018-03-06 14:34:46 +01:00
}
}
2016-03-09 16:33:48 +01:00
2017-01-27 15:16:30 +01:00
$helperForm = new HelperFormBootstrap ();
$helperForm -> _select2 = true ;
2017-02-08 12:04:06 +01:00
$helperForm -> _inputSwitch = true ;
2017-01-27 15:16:30 +01:00
$id_sale_options = array ();
2017-10-30 15:11:20 +01:00
foreach ( Sale :: getSalesBack ( NULL , NULL , NULL , NULL , FALSE , FALSE , '`date_start` DESC' , 500 ) as $sale ) {
2017-01-27 15:16:30 +01:00
$id_sale_options [( int ) $sale -> id ] = array (
'label' => $sale -> id . ' - ' . $sale -> title [( int ) $cookie -> id_lang ],
'value' => ( int ) $sale -> id
);
if ( $sale -> id == $current_sale ) {
$id_sale_options [( int ) $sale -> id ][ 'selected' ] = true ;
}
}
$this -> _html .= $helperForm -> renderStyle ();
$this -> _html .= ' < div class = " panel " >
< div class = " panel-title " >
< h2 >< span class = " anticon anticon-barcode " ></ span > '.$this->l(' Génération EAN ').' </ h2 >
< div class = " clearfix " ></ div >
2016-03-09 16:33:48 +01:00
</ div >
2017-01-27 15:16:30 +01:00
< div class = " panel-content " >
< div class = " col-md-5 col-md-offset-3 " > ' ;
$input = array (
'type' => 'select2' ,
'label' => $this -> l ( 'Select a sale:' ),
'label-class' => 'text-left' ,
'select-class' => 'text-left' ,
2018-03-08 14:54:52 +01:00
'name' => 'id_sale' ,
'id' => 'id_sale' ,
2017-01-27 15:16:30 +01:00
'options' => $id_sale_options ,
);
2017-02-08 12:04:06 +01:00
$this -> _html .= $helperForm -> generateInput ( $input );
$input = array (
'type' => 'switch' ,
'label' => '' ,
'label-class' => 'col-md-4' ,
'input-class' => 'col-md-6' ,
'class-group' => 'switch' ,
2018-03-08 14:54:52 +01:00
'name' => 'big_label' ,
2017-02-08 12:04:06 +01:00
'title' => $this -> l ( 'Big labels ?' ),
);
2017-01-27 15:16:30 +01:00
$this -> _html .= $helperForm -> generateInput ( $input ) . '
2017-02-08 12:04:06 +01:00
< div class = " clearfix " ></ div >
2017-02-03 16:55:47 +01:00
</ div >
< div class = " col-md-3 col-md-offset-1 lateral-solid-l " >
< p class = " hidden " >< button class = " btn btn-primary " id = " generateRef " data - toggle = " tooltip " data - placement = " left " title = " Produits vendus " > Générer Refs </ button ></ p >
< p >< button class = " btn btn-primary " id = " generateEAN " data - toggle = " tooltip " data - placement = " left " title = " EAN manquants des quantités vendus " > Générer EANs ( manquants ) </ button ></ p >
< p >< button class = " btn btn-primary " id = " generateALLEAN " data - toggle = " tooltip " data - placement = " left " title = " Tous les EAN et génère les manquants des quantités vendus " > Générer EANs ( qté vendus ) </ button ></ p >
< p >< button class = " btn btn-primary " id = " generateALLEAN_initial " data - toggle = " tooltip " data - placement = " left " title = " Tous les EAN et génère les manquants des quantités initiales " > Générer EANs ( qté initiales ) </ button ></ p >
< p >< button class = " btn btn-default " id = " deleteEAN " data - toggle = " tooltip " data - placement = " left " title = " Supprimer tous les EAN de la vente " > Supprimer les EANs </ button ></ p >
< p class = " hidden " >< button class = " hidden btn btn-default " id = " generateRack " > Générer Rack </ button ></ p >
2018-03-06 14:34:46 +01:00
< p >& nbsp ; </ p >
< p >< button class = " btn btn-primary " id = " generateIdentification " data - toggle = " tooltip " data - placement = " left " title = " Générer Etiquette Référence Bebeboutik " > Générer Référence </ button ></ p >
2017-02-03 16:55:47 +01:00
</ div >
< div class = " clear " ></ div >
2017-01-27 15:16:30 +01:00
</ div >
</ div > ' ;
2016-05-26 13:01:58 +02:00
2017-01-27 15:16:30 +01:00
$helperForm -> _js .= ' < script type = " text/javascript " >
$ ( function () {
2017-02-08 12:04:06 +01:00
var big_label = 0 ;
2017-02-03 16:55:47 +01:00
$ ( \ ' [ data - toggle = " tooltip " ] \ ' ) . tooltip ();
2016-12-27 10:33:56 +01:00
2017-02-08 12:04:06 +01:00
$ ( \ ' . js - switch \ ' ) . change ( function (){
if ( $ ( this ) . is ( \ ' : checked\ ' )) {
big_label = 1 ;
}
});
2017-01-27 15:16:30 +01:00
$ ( " #generateEAN " ) . click ( function (){
if ( $ ( " #id_sale " ) . val () != 999999 ) {
$ ( \ ' #category_selector\').hide();
2018-03-08 14:54:52 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & generateEAN = 1 & big_label = \ ' + big_label + \ ' & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2017-01-27 15:16:30 +01:00
}
});
2016-04-04 15:20:32 +02:00
2017-01-27 15:16:30 +01:00
$ ( " #generateRef " ) . click ( function (){
if ( $ ( " #id_sale " ) . val () != 999999 ) {
$ ( \ ' #category_selector\').hide();
2018-03-08 14:54:52 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & generateRef = 1 & big_label = \ ' + big_label + \ ' & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2017-01-27 15:16:30 +01:00
}
});
2016-03-09 16:33:48 +01:00
2017-01-27 15:16:30 +01:00
$ ( " #generateALLEAN " ) . click ( function (){
if ( $ ( " #id_sale " ) . val () != 999999 ) {
$ ( \ ' #category_selector\').hide();
2018-03-08 14:54:52 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & generateALLEAN = 1 & big_label = \ ' + big_label + \ ' & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2017-01-27 15:16:30 +01:00
}
});
2017-02-03 16:55:47 +01:00
$ ( " #generateALLEAN_initial " ) . click ( function (){
if ( $ ( " #id_sale " ) . val () != 999999 ) {
$ ( \ ' #category_selector\').hide();
2018-03-08 14:54:52 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & generateALLEAN_initial = 1 & big_label = \ ' + big_label + \ ' & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2017-02-03 16:55:47 +01:00
}
});
2017-01-27 15:16:30 +01:00
$ ( " #deleteEAN " ) . click ( function (){
ok = confirm ( " Êtes vous sur de vouloir supprimer les eans de cette vente ? " )
if ( ok == true ){
2018-03-06 14:34:46 +01:00
if ( $ ( " #id_sale " ) . val () != 999999 ) {
2018-03-08 14:54:52 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & deleteEAN = 1 & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2018-03-06 14:34:46 +01:00
}
2017-01-27 15:16:30 +01:00
}
});
2016-04-08 10:27:46 +02:00
2017-01-27 15:16:30 +01:00
$ ( " #generateRack " ) . click ( function (){
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & rack = 1 \ ' ;
2016-03-09 16:33:48 +01:00
});
2016-04-04 15:54:18 +02:00
2018-03-08 14:54:52 +01:00
$ ( " #generateIdentification " ) . click ( function (){
2018-03-13 15:18:14 +01:00
document . location . href = \ 'index.php?tab=AdminLabelGenerate&token=' . Tools :: getAdminTokenLite ( 'AdminLabelGenerate' ) . ' & generateIdentification = 1 & big_label = \ ' + big_label + \ ' & id_sale = \ ' + $ ( " #id_sale " ) . val ();
2018-03-08 14:54:52 +01:00
});
2017-01-27 15:16:30 +01:00
});
</ script > ' ;
2018-03-06 14:34:46 +01:00
2017-01-04 10:27:55 +01:00
$this -> _html .= $helperForm -> renderScript ();
2016-03-09 16:33:48 +01:00
echo $this -> _html ;
}
2017-01-30 16:53:12 +01:00
public function displaySuccess ( $message ){
return ' < div class = " col-sm-6 col-sm-offset-3 alert-custom alert-ok " style = " margin-bottom: 10px; " >
< i class = " glyphicon glyphicon-ok " ></ i > < span class = " text-alert " > '.$message.' </ span >
</ div > ' ;
}
public function displayWarning ( $message ){
return ' < div class = " col-sm-6 col-sm-offset-3 alert-custom alert-warning " style = " margin-bottom: 10px; " >
< i class = " anticon anticon-notification " ></ i > < span class = " text-alert " > '.$message.' </ span >
</ div > ' ;
}
public function displayError ( $message ){
return ' < div class = " col-sm-6 col-sm-offset-3 alert-custom alert-error " style = " margin-bottom: 10px; " >
< i class = " glyphicon glyphicon-remove " ></ i > < span class = " text-alert " > '.$message.' </ span >
</ div > ' ;
}
2016-02-22 12:33:51 +01:00
}