bebeboutik/modules/labelgenerate/AdminLabelGenerate.php
Marion Muszynski 72545630a3 fix conflicts
2016-06-06 11:32:25 +02:00

346 lines
18 KiB
PHP

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
require_once dirname(__FILE__).'/../privatesales/Sale.php';
require_once dirname(__FILE__).'/models/generatebarcode.php';
class AdminLabelGenerate extends AdminTab {
public $_html = '';
public function display() {
global $cookie;
$rack = Tools::getValue('rack');
$current_sale = Tools::getValue('id_sale');
if ($rack) {
// button commenté en fin de page
$barcode = new GenerateBarcode();
$barcode->createRackFolder();
$rackCodes = array();
for ($i=0;$i<21;$i++) {
for ($j=1;$j<6;$j++) {
$name = $i.$j;
$barcode->generateRack($name);
$rackCodes[] = $name;
}
}
$barcode->printRackPDF($rackCodes);
if (glob(_PS_MODULE_DIR_.'labelgenerate/img/rack/rack.pdf')) {
$this->_html .= '<p class="conf">Rack générés : <a target="_blank" href="/modules/labelgenerate/img/rack/rack.pdf">Télécharger le PDF rack</a></p>';
}
} elseif ($current_sale && Tools::getValue('deleteEAN')) {
$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('
UPDATE
`'._DB_PREFIX_.'product_attribute`
SET `ean13` = ""
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` = ""
WHERE `product_id` = '. (int)$product->id .'
AND `product_attribute_id` = '. (int)$combination['id_product_attribute']
);
}
}
} else {
if (!empty($product->ean13)) {
Db::getInstance()->execute('
UPDATE
`'._DB_PREFIX_.'product`
SET `ean13` = ""
WHERE `id_product` = ' . (int)$product->id
);
Db::getInstance()->Execute('
UPDATE
`'._DB_PREFIX_.'order_detail`
SET `product_ean13` = ""
WHERE `product_id` = '. (int)$product->id .'
AND `product_attribute_id` = 0
');
}
}
}
$this->_html .= '<p class="conf">Suppression terminée </p>';
}
} elseif ($current_sale && Tools::getValue('generateALLEAN')) {
$sale = new Sale((int)$current_sale);
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);
if ($combinations) {
foreach ($combinations as $key => $combination) {
if (empty($combination['ean13'])) {
$quantity = $combination['quantity'];
$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)) {
$quantity = $product->quantity;
$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)) {
$barcode->printPDF();
$this->_html .= '<p class="conf">Génération terminée : <a target="_blank" href="/modules/labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf">Télécharger le PDF</a></p>';
} else {
$this->_html .= '<p class="conf">Tous les produits de la vente ont déjà des EANS</p>';
if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) {
$this->_html .= '<p class="conf">Dernières étiquettes générées : <a target="_blank" href="/modules/labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf">Télécharger le PDF</a></p>';
}
}
}
} elseif ($current_sale && Tools::getValue('generateEAN')) {
$sale = new Sale((int)$current_sale);
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);
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
WHERE od.`product_id` = ' . (int)$combination['id_product'] .'
AND od.`product_attribute_id`= ' . (int)$combination['id_product_attribute']
);
$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
WHERE od.`product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 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)) {
$barcode->printPDF();
$this->_html .= '<p class="conf">Génération terminée : <a target="_blank" href="/modules/labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf">Télécharger le PDF</a></p>';
} else {
$this->_html .= '<p class="conf">Tous les produits de la vente ont déjà des EANS</p>';
if (glob(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) {
$this->_html .= '<p class="conf">Dernières étiquettes générées : <a target="_blank" href="/modules/labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf">Télécharger le PDF</a></p>';
}
}
}
}
$this->_html .= '<fieldset>
<legend>'.$this->l('Sale selection').'</legend>
<div class="" style="text-align:center;">
<label for="id_sale" style="display: inline; float: none; width: auto;">'.$this->l('Select a sale:').'</label><br/><br class="clear"/>
<select class="chosen-select" id="id_sale" name="id_sale">
<option value=""></option>
';
foreach(Sale::getSales(NULL, NULL, NULL, NULL, FALSE, FALSE, '`date_start` DESC', NULL) as $sale) {
$this->_html .= '<option value="'.(int) $sale->id.'"'.($sale->id == $current_sale? ' selected="selected"': '').'>'.$sale->id.' - '.$sale->title[(int) $cookie->id_lang].'</option>';
}
$this->_html .= '
</select>
<br class="clear"/>
<br class="clear"/>
<div style="text-align:center">
<button class="button" id="generateEAN" style="display:inline-block">Générer les EANs pour produits vendus</button>
</div>
<br class="clear"/>
<div style="text-align:center">
<button class="button" id="generateALLEAN" style="display:inline-block">Générer les EANs pour tous les produits d\'une vente</button>
</div>
<br class="clear"/>
<div style="text-align:center">
<button class="button" id="deleteEAN" style="display:inline-block">Supprimer les EANs</button>
</div>
</div>
</fieldset>
<br /><br />';
/*$this->_html .='<fieldset>
<legend>'.$this->l('Rack barcode').'</legend>
<div class="margin-form">
<button class="button" id="generateRack">'.$this->l('Generate rack').'</button>
</div>
</fieldset>
<br /><br />';*/
$this->_html.= '<link type="text/css" rel="stylesheet" href="'._MODULE_DIR_.'bulkupdate/chosen.min.css" />';
$this->_html.= '<style>.chosen-drop{text-align:left;}</style>';
$this->_html.= '<script type="text/javascript" src="'._MODULE_DIR_.'bulkupdate/chosen.jquery.min.js"></script>';
$this->_html.= '<script type="text/javascript">
$(function() {
$(".chosen-select").chosen(
{
allow_single_deselect:true,
placeholder_text_single : "Choisir une vente",
no_results_text : "Aucun résultat",
enable_split_word_search : true,
search_contains : true,
}
);
$(".chosen-select").chosen().change( function() {});
$("#generateEAN").click(function(){
if ($(".chosen-select").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateEAN=1&id_sale=\' + $(".chosen-select").val();
}
});
$("#generateALLEAN").click(function(){
if ($(".chosen-select").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateALLEAN=1&id_sale=\' + $(".chosen-select").val();
}
});
$("#deleteEAN").click(function(){
ok = confirm("Êtes vous sur de vouloir supprimer les eans de cette vente ?")
if (ok == true){
if ($(".chosen-select").val() != 999999) {
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&deleteEAN=1&id_sale=\' + $(".chosen-select").val();
}
}
});
$("#generateRack").click(function(){
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&rack=1\';
});
});
</script>';
echo $this->_html;
}
}