diff --git a/modules/labelgenerate/AdminLabelGenerate.php b/modules/labelgenerate/AdminLabelGenerate.php index 36b95c4a..c6193dd7 100644 --- a/modules/labelgenerate/AdminLabelGenerate.php +++ b/modules/labelgenerate/AdminLabelGenerate.php @@ -1,14 +1,14 @@ printRackPDF($rackCodes); - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/rack/rack.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/rack/rack.pdf')) { $this->displaySucess('Rack générés : Télécharger le PDF rack'); } } elseif ($current_sale && Tools::getValue('generateRef')) { @@ -86,7 +86,7 @@ class AdminLabelGenerate extends AdminTab { } } } - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/reference.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/reference.pdf')) { $this->displayWarning('Les références ont déjà été générées
Dernières ref générées : Télécharger le PDF'); } else { if($barcode->printPDF($big_label, 'reference')) { @@ -113,42 +113,40 @@ class AdminLabelGenerate extends AdminTab { foreach ($combinations as $key => $combination) { if (!empty($combination['ean13'])) { Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product_attribute` - SET `ean13` = "" + UPDATE `'._DB_PREFIX_.'product_attribute` + SET `ean13` = "" WHERE `id_product` = ' . (int)$product->id .' - AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] + AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "" + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "" WHERE `product_id` = '. (int)$product->id .' - AND `product_attribute_id` = '. (int)$combination['id_product_attribute'] + AND `product_attribute_id` = '. (int)$combination['id_product_attribute'] ); } } } else { if (!empty($product->ean13)) { Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product` - SET `ean13` = "" + UPDATE `'._DB_PREFIX_.'product` + SET `ean13` = "" WHERE `id_product` = ' . (int)$product->id ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "" + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "" WHERE `product_id` = '. (int)$product->id .' - AND `product_attribute_id` = 0 + AND `product_attribute_id` = 0 '); } } } $this->_html .= $this->displaySuccess('Suppression terminée'); } - } elseif ($current_sale && Tools::getValue('generateALLEAN')) { + } + // Generate ALL EAN (sell quantities) + elseif ($current_sale && Tools::getValue('generateALLEAN')) { $sale = new Sale((int)$current_sale); $big_label = Tools::getValue('big_label'); @@ -187,18 +185,16 @@ class AdminLabelGenerate extends AdminTab { if (empty($combination['ean13'])) { $ean_generate = $barcode->generateBarcode($combination['id_product'], $combination['id_product_attribute'],$product->reference); Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product_attribute` - SET `ean13` = '. $ean_generate . ' + UPDATE `'._DB_PREFIX_.'product_attribute` + SET `ean13` = '. $ean_generate . ' WHERE `id_product` = ' . (int)$product->id .' - AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] + AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "'. pSQL($ean_generate) .'" + 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'] + AND `product_attribute_id` = '. (int)$combination['id_product_attribute'] ); } else { $ean_generate = $barcode->generateBarcode($combination['id_product'], $combination['id_product_attribute'],$product->reference, $combination['ean13']); @@ -217,22 +213,20 @@ class AdminLabelGenerate extends AdminTab { foreach ($result_quantities as $od) { $quantity += ((int)$od['product_quantity'] - (int)$od['product_quantity_reinjected']); } - if($quantity>0){ + if ($quantity > 0) { $count++; if (empty($product->ean13)) { $ean_generate = $barcode->generateBarcode($product->id, NULL, $product->reference); Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product` - SET `ean13` = '. $ean_generate . ' + UPDATE `'._DB_PREFIX_.'product` + SET `ean13` = '. $ean_generate . ' WHERE `id_product` = ' . (int)$product->id ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "'. pSQL($ean_generate) .'" + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "'. pSQL($ean_generate) .'" WHERE `product_id` = '. (int)$product->id .' - AND `product_attribute_id` = 0 + AND `product_attribute_id` = 0 '); } else { $ean_generate = $barcode->generateBarcode($product->id, NULL, $product->reference, $product->ean13); @@ -243,7 +237,7 @@ class AdminLabelGenerate extends AdminTab { } if (!empty($ean_generate)) { - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all.pdf')) { unlink(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all.pdf'); } if($barcode->printPDF($big_label,'barcode_all')) { @@ -253,13 +247,15 @@ class AdminLabelGenerate extends AdminTab { } } else { $message = 'Aucun ean trouvé ou généré !'; - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all.pdf')) { $message .= '
Dernières étiquettes générées : Télécharger le PDF'; } $this->_html .= $this->displayWarning($message); } } - } elseif ($current_sale && Tools::getValue('generateALLEAN_initial')) { + } + // Generate ALL EAN (initial quantities) + elseif ($current_sale && Tools::getValue('generateALLEAN_initial')) { $sale = new Sale((int)$current_sale); $big_label = Tools::getValue('big_label'); @@ -296,18 +292,16 @@ class AdminLabelGenerate extends AdminTab { if (empty($combination['ean13'])) { $ean_generate = $barcode->generateBarcode($combination['id_product'], $combination['id_product_attribute'],$product->reference); Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product_attribute` - SET `ean13` = '. $ean_generate . ' + UPDATE `'._DB_PREFIX_.'product_attribute` + SET `ean13` = '. $ean_generate . ' WHERE `id_product` = ' . (int)$product->id .' - AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] + AND `id_product_attribute` = ' . (int)$combination['id_product_attribute'] ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "'. pSQL($ean_generate) .'" + 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'] + AND `product_attribute_id` = '. (int)$combination['id_product_attribute'] ); } else { $ean_generate = $barcode->generateBarcode($combination['id_product'], $combination['id_product_attribute'],$product->reference, $combination['ean13']); @@ -328,17 +322,15 @@ class AdminLabelGenerate extends AdminTab { if (empty($product->ean13)) { $ean_generate = $barcode->generateBarcode($product->id, NULL, $product->reference); Db::getInstance()->execute(' - UPDATE - `'._DB_PREFIX_.'product` - SET `ean13` = '. $ean_generate . ' + UPDATE `'._DB_PREFIX_.'product` + SET `ean13` = '. $ean_generate . ' WHERE `id_product` = ' . (int)$product->id ); Db::getInstance()->Execute(' - UPDATE - `'._DB_PREFIX_.'order_detail` - SET `product_ean13` = "'. pSQL($ean_generate) .'" + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "'. pSQL($ean_generate) .'" WHERE `product_id` = '. (int)$product->id .' - AND `product_attribute_id` = 0 + AND `product_attribute_id` = 0 '); } else { $ean_generate = $barcode->generateBarcode($product->id, NULL, $product->reference, $product->ean13); @@ -348,7 +340,7 @@ class AdminLabelGenerate extends AdminTab { } if (!empty($ean_generate)) { - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all_initial.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all_initial.pdf')) { unlink(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all_initial.pdf'); } if($barcode->printPDF($big_label,'barcode_all_initial')) { @@ -358,19 +350,22 @@ class AdminLabelGenerate extends AdminTab { } } else { $message = 'Aucun ean trouvé ou généré !'; - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all_initial.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode_all_initial.pdf')) { $message .= '
Dernières étiquettes générées : Télécharger le PDF'; } $this->_html .= $this->displayWarning($message); } } - } elseif ($current_sale && Tools::getValue('generateEAN')) { + } + // Generate EAN on missing products + elseif ($current_sale && Tools::getValue('generateEAN')) { $sale = new Sale((int)$current_sale); $big_label = Tools::getValue('big_label'); if(!Validate::isLoadedObject($sale)) { throw new Exception('This sale doesnt exist'); - } else { + } + else { $products = $sale->getProducts(); $ean_generate = ''; $products_generate = array(); @@ -384,76 +379,72 @@ class AdminLabelGenerate extends AdminTab { $attributes = array(); $combinations = $product->getAttributeCombinaisons($cookie->id_lang); + // Product with attributes if ($combinations) { foreach ($combinations as $key => $combination) { - 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'] - ); - } - } - } - } else { - if (empty($product->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)$product->id . ' AND `product_attribute_id` = 0 AND o.`valid` != 0' + 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($product->id, NULL, $product->reference); - $barcode->assocProduct($product->id, NULL, $quantity, $product->reference); + 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` - SET `ean13` = '. $ean_generate . ' - WHERE `id_product` = ' . (int)$product->id + 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) .'" + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "'. pSQL($ean_generate) .'" WHERE `product_id` = '. (int)$product->id .' - AND `product_attribute_id` = 0 - '); + AND `product_attribute_id` = '. (int)$combination['id_product_attribute'] + ); } } } + // Product without attributes + else { + $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' + ); + $quantity = 0; + foreach ($result_quantities as $od) { + $quantity += ($od['product_quantity'] - $od['product_quantity_reinjected']); + } + if ($quantity > 0) { + $ean_generate = $barcode->generateBarcode($product->id, NULL, $product->reference); + $barcode->assocProduct($product->id, NULL, $quantity, $product->reference); + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'product` + SET `ean13` = '. $ean_generate . ' + WHERE `id_product` = ' . (int)$product->id + ); + Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'order_detail` + SET `product_ean13` = "'. pSQL($ean_generate) .'" + WHERE `product_id` = '. (int)$product->id .' + AND `product_attribute_id` = 0 + '); + } + + } } if (!empty($ean_generate)) { - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) { unlink(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf'); } if($barcode->printPDF($big_label)) { @@ -463,7 +454,7 @@ class AdminLabelGenerate extends AdminTab { } } else { $message = 'Tous les produits de la vente ont déjà des EANS'; - if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) { + if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) { $message .= '
Dernières étiquettes générées : Télécharger le PDF'; } $this->_html .= $this->displayWarning($message); @@ -477,8 +468,6 @@ class AdminLabelGenerate extends AdminTab { $id_sale_options = array(); foreach(Sale::getSalesBack(NULL, NULL, NULL, NULL, FALSE, FALSE, '`date_start` DESC', 500) as $sale) { - - $id_sale_options[(int)$sale->id] = array( 'label' => $sale->id.' - '.$sale->title[(int) $cookie->id_lang], 'value' => (int) $sale->id