bebeboutik/modules/labelgenerate/AdminLabelGenerate.php

157 lines
8.2 KiB
PHP
Raw Normal View History

2016-02-22 12:33:51 +01:00
<?php
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';
class AdminLabelGenerate extends AdminTab {
2016-03-09 16:33:48 +01:00
public $_html = '';
public function display() {
global $cookie;
$current_sale = Tools::getValue('id_sale');
if ($current_sale) {
$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);
2016-03-22 17:31:19 +01:00
$barcode->assocProduct($combination['id_product'], $combination['id_product_attribute'], $quantity, $product->reference);
2016-03-09 16:33:48 +01:00
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']
);
2016-03-10 09:53:45 +01:00
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']
);
2016-03-09 16:33:48 +01:00
}
}
}
} else {
if (empty($product->ean13)) {
$result_quantities = Db::getInstance()->executeS('
SELECT od.`product_quantity`,od.`product_quantity_reinjected`
FROM '._DB_PREFIX_.'order_detail od
2016-03-10 09:53:45 +01:00
WHERE od.`product_id` = ' . (int)$product->id . ' AND `product_attribute_id` = 0'
2016-03-09 16:33:48 +01:00
);
$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);
2016-03-22 17:31:19 +01:00
$barcode->assocProduct($product->id, NULL, $quantity, $product->reference);
2016-03-09 16:33:48 +01:00
Db::getInstance()->execute('
UPDATE
`'._DB_PREFIX_.'product`
SET `ean13` = '. $ean_generate . '
WHERE `id_product` = ' . (int)$product->id
);
2016-03-10 09:53:45 +01:00
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
');
2016-03-09 16:33:48 +01:00
}
}
}
}
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="margin-form">
<label for="id_sale" style="display: inline; float: none; width: auto;">'.$this->l('Select a sale:').'</label>
<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>
</div>
</fieldset>
<br /><br />';
$this->_html.= '<link type="text/css" rel="stylesheet" href="'._MODULE_DIR_.'bulkupdate/chosen.min.css" />';
$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() {
if($(this).val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&id_sale=\' + $(this).val();
}
});
});
</script>';
echo $this->_html;
}
2016-02-22 12:33:51 +01:00
}