New label

This commit is contained in:
Michael RICOIS 2018-03-08 14:54:52 +01:00
parent 0b835a3af5
commit 1d5eb5ec97
2 changed files with 155 additions and 69 deletions

View File

@ -492,73 +492,54 @@ class AdminLabelGenerate extends AdminTab
// 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);
}
}
$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->generateIdentification($combination['id_product'], $combination['id_product_attribute'], $product->reference, $combination['attribute_name']);
$barcode->assocProduct($combination['id_product'], $combination['id_product_attribute'], $quantity, $product->reference);
}
}
}
// Product without attributes
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'
);
$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']);
}
$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) {
$barcode->generateIdentification($combination['id_product'], null, $product->reference);
$barcode->assocProduct($product->id, null, $quantity, $product->reference);
}
}
}
if (!empty($ean_generate)) {
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)) {
$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>');
} else {
$this->_html .= $this->displayError('Pas de produit à générer !');
}
} else {
$message = 'Tous les produits de la vente ont déjà des EANS';
if (file_exists(_PS_MODULE_DIR_.'labelgenerate/img/'.$barcode->id_sale.'/barcode.pdf')) {
$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>';
}
$this->_html .= $this->displayWarning($message);
}
exit;
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 !');
}
}
}
@ -593,8 +574,8 @@ class AdminLabelGenerate extends AdminTab
'label' => $this->l('Select a sale:'),
'label-class' => 'text-left',
'select-class' => 'text-left',
'name' => 'id_sale2',
'id' => 'id_sale2',
'name' => 'id_sale',
'id' => 'id_sale',
'options' => $id_sale_options,
);
$this->_html .= $helperForm->generateInput($input);
@ -604,7 +585,7 @@ class AdminLabelGenerate extends AdminTab
'label-class' => 'col-md-4',
'input-class' => 'col-md-6',
'class-group' => 'switch',
'name' => 'big_label2',
'name' => 'big_label',
'title' => $this->l('Big labels ?'),
);
$this->_html .= $helperForm->generateInput($input).'
@ -639,28 +620,28 @@ class AdminLabelGenerate extends AdminTab
$("#generateEAN").click(function(){
if ($("#id_sale").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateEAN=1&big_label=\'+ big_label +\'&id_sale=\' + $("#id_sale").val();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateEAN=1&big_label=\'+ big_label +\'&id_sale=\'+$("#id_sale").val();
}
});
$("#generateRef").click(function(){
if ($("#id_sale").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateRef=1&big_label=\'+ big_label +\'&id_sale=\' + $("#id_sale").val();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateRef=1&big_label=\'+ big_label +\'&id_sale=\'+$("#id_sale").val();
}
});
$("#generateALLEAN").click(function(){
if ($("#id_sale").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateALLEAN=1&big_label=\'+ big_label +\'&id_sale=\' + $("#id_sale").val();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateALLEAN=1&big_label=\'+ big_label +\'&id_sale=\'+$("#id_sale").val();
}
});
$("#generateALLEAN_initial").click(function(){
if ($("#id_sale").val() != 999999) {
$(\'#category_selector\').hide();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateALLEAN_initial=1&big_label=\'+ big_label +\'&id_sale=\' + $("#id_sale").val();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateALLEAN_initial=1&big_label=\'+ big_label +\'&id_sale=\'+$("#id_sale").val();
}
});
@ -668,7 +649,7 @@ class AdminLabelGenerate extends AdminTab
ok = confirm("Êtes vous sur de vouloir supprimer les eans de cette vente ?")
if (ok == true){
if ($("#id_sale").val() != 999999) {
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&deleteEAN=1&id_sale=\' + $("#id_sale").val();
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&deleteEAN=1&id_sale=\'+$("#id_sale").val();
}
}
});
@ -677,6 +658,9 @@ class AdminLabelGenerate extends AdminTab
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&rack=1\';
});
$("#generateIdentification").click(function(){
document.location.href=\'index.php?tab=AdminLabelGenerate&token='.Tools::getAdminTokenLite('AdminLabelGenerate').'&generateIdentification=1&id_sale=\'+$("#id_sale").val();
});
});
</script>';

View File

@ -117,9 +117,32 @@ class GenerateBarcode
return $ean;
}
public function generateIdentification()
public function generateIdentification($id_product, $id_product_attribute = null, $reference = null, $attribute_ref = null, $big_label = false)
{
$width = 320;
$height = 80;
preg_match('/.*-(.*)-.*/', $reference, $matches);
$ref = $matches[1];
$im = imagecreatetruecolor($width, $height);
$black = ImageColorAllocate($im, 0x00, 0x00, 0x00);
$white = ImageColorAllocate($im, 0xff, 0xff, 0xff);
imagefilledrectangle($im, 0, 0, $width, $height, $white);
imagettftext($im, 11, 0, $width / 2, $height / 2 - 10, $black, dirname(__FILE__).'/arial.ttf', $ref);
if ($id_product_attribute !== null) {
imagettftext($im, 11, 0, $width / 2 - (strlen($attribute_ref)* 4), $height - 10 , $black, dirname(__FILE__).'/arial.ttf', $attribute_ref);
}
if ($id_product_attribute) {
$result = imagegif($im, $this->directory.'/id-'.$id_product.'-'.$id_product_attribute.'.gif');
} else {
$result = imagegif($im, $this->directory.'/id-'.$id_product.'-0.gif');
}
return $result;
}
private function _getEanNumber()
@ -310,6 +333,85 @@ class GenerateBarcode
return TRUE;
}
public function printIdentification($big_label = false, $file = 'identification')
{
global $cookie;
if ($big_label) {
// 52*30
$width = 51.5;
$height_label = 14;
$height_text = 7;
$ln_label = 15;
$ln_text = 7.1;
$nb_per_line = self::_NB_PER_LINE_BIG_;
$total_per_page = self::_TOTAL_PER_PAGE_BIG_;
$font_size = 9;
$font_size_ref = 10;
$margin = 3;
$margin_top = 3;
} else {
// 48*17
$width = 48;
$height_label = 10;
$height_text = 4;
$ln_label = 13;
$ln_text = 4;
$nb_per_line = self::_NB_PER_LINE_;
$total_per_page = self::_TOTAL_PER_PAGE_;
$font_size = 5;
$margin = 9;
$margin_top = 12;
}
if (empty($this->products)) {
return false;
}
$this->products = $this->_multiSortProduct($this->products);
ob_start();
$pdf = new FPDF('P', 'mm', 'A4');
$nb_per_page = 0;
$pdf->SetMargins($margin,$margin_top,$margin);
$pdf->SetAutoPageBreak(FALSE);
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$nb_line = ceil((count($this->products)/$nb_per_line));
for ($i=1; $i <= $nb_line; $i++) {
if (empty($this->products[0])) {
unset($this->products[0]);
}
if (!empty($this->products)) {
$product_per_line = array_slice($this->products, 0, $nb_per_line);
$this->products = array_splice($this->products, $nb_per_line);
if($nb_per_page == $total_per_page) {
$pdf->addPage();
$nb_per_page = 0;
}
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell($width, $height_label, '', 0, 0, 'C');
} else {
$image = $this->directory.'/id-'.$product['key'].'.gif';
$this->_printEAN($pdf, $image, 1, $width, $height_label);
}
}
$pdf->Ln($ln_label);
$nb_per_page += 1;
}
}
$pdf->Output($this->directory.'/'.$file.'.pdf', 'F');
ob_clean();
return true;
}
private function _printLabel($pdf, $label, $limit = 1, $width = 48, $height = 4, $font_size = 5)
{
$pdf->SetFont('Arial', '', $font_size);