bebeboutik/modules/labelgenerate/models/generatebarcode.php

173 lines
6.0 KiB
PHP
Raw Normal View History

2016-02-22 12:33:51 +01:00
<?php
require_once dirname(__FILE__).'/php-barcode.php';
include_once(_PS_FPDF_PATH_.'fpdf.php');
class GenerateBarcode {
static private $barcode_directory = NULL;
private $directory;
public $id_sale;
public $products = array();
const _NB_PER_LINE_ = 4;
const _TOTAL_PER_PAGE_ = 16;
public function __construct() {
if (is_null(self::$barcode_directory)) {
self::$barcode_directory = dirname(__FILE__).'/../img/';
}
}
public function createFolder($name) {
if (!(is_dir(self::$barcode_directory.$name))) {
mkdir(self::$barcode_directory.$name, 0775);
}
$this->directory = self::$barcode_directory.$name;
}
public function generateBarcode($id_product, $id_product_attribute = NULL, $reference = NULL) {
$ean = $this->_getEanNumber();
$str = $ean;
$width = 320;
$height = 100;
$im = imagecreatetruecolor($width, $height);
$black = ImageColorAllocate($im, 0x00, 0x00, 0x00);
$white = ImageColorAllocate($im, 0xff, 0xff, 0xff);
imagefilledrectangle($im, 0, 0, $width, $height, $white);
Barcode::gd($im, $black, $width / 2, $height / 2 - 10 , 0, "code128", $str, 3, 50);
imagettftext($im, 11, 0, $width / 2 - (strlen('*'.$reference.'*')* 4), $height - 10 , $black, dirname(__FILE__).'/arial.ttf', '*'.$reference.'*' );
2016-03-09 16:33:48 +01:00
2016-02-22 12:33:51 +01:00
if ($id_product_attribute) {
imagegif($im, $this->directory.'/ean-'.$id_product.'-'.$id_product_attribute.'.gif');
} else {
imagegif($im, $this->directory.'/ean-'.$id_product.'-0.gif');
}
return $ean;
}
private function _getEanNumber() {
2016-03-09 16:33:48 +01:00
// min 1111111111
2016-02-22 12:33:51 +01:00
$number = Configuration::get('BARCODE_NUMBER') + 1;
Configuration::updateValue('BARCODE_NUMBER', $number);
return $number;
}
public function assocProduct($id_product, $id_product_attribute = NULL, $quantity) {
2016-03-09 16:33:48 +01:00
global $cookie;
$p = Db::getInstance()->getRow('
SELECT pl.name
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON pl.`id_product` = p.`id_product`
WHERE p.`id_product` = '.(int)$id_product.'
AND pl.`id_lang`='.(int)$cookie->id_lang);
2016-02-22 12:33:51 +01:00
if ($id_product_attribute) {
2016-03-09 16:33:48 +01:00
$details = Product::getDetailsCombination($id_product_attribute, $cookie->id_lang);
2016-02-22 12:33:51 +01:00
$name_combination = '';
foreach ($details as $key_attr => $detail) {
$name_combination .= $detail['attribute_name'].'-';
}
2016-03-09 16:33:48 +01:00
for ($i=1; $i <= $quantity; $i++) {
$this->products[] = array(
'key' => ($id_product.'-'.$id_product_attribute),
'label' => ($name_combination . ' ' . $p['name'])
);
}
2016-02-22 12:33:51 +01:00
} else {
2016-03-09 16:33:48 +01:00
for ($i=1; $i <= $quantity; $i++) {
$this->products[] = array(
'key' => $id_product.'-0',
'label' => $p['name']
);
}
2016-02-22 12:33:51 +01:00
}
2016-03-09 16:33:48 +01:00
$this->products[] = array();
2016-02-22 12:33:51 +01:00
}
public function printPDF() {
global $cookie;
2016-03-09 16:33:48 +01:00
// données test
/*for ($i=0; $i < 30; $i++) {
$this->products[$i] = array(
'key' => "125415-322",
'label' => "03 mois (62 cm)- Lot de 2 bodies Ri"
);
2016-02-22 12:33:51 +01:00
}
2016-03-09 16:33:48 +01:00
$this->products[count($this->products)] = array();
for ($i=31; $i < 80; $i++) {
$this->products[$i] = array(
'key' => "125416-332",
'label' => "03 mois (62 cm)- Ensemble grenouill"
);
2016-02-22 12:33:51 +01:00
}
2016-03-09 16:33:48 +01:00
$this->products[count($this->products)] = array();*/
2016-02-22 12:33:51 +01:00
if (empty($this->products)) {
die('Tous les produits ont déjà un EAN');
}
ob_start();
$pdf = new FPDF();
$nb_per_page = 0;
$pdf->SetMargins(8,15,8);
$pdf->SetAutoPageBreak(FALSE);
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
2016-03-09 16:33:48 +01:00
$nb_line = ceil((count($this->products)/self::_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, self::_NB_PER_LINE_);
$this->products = array_splice($this->products, self::_NB_PER_LINE_);
2016-02-22 12:33:51 +01:00
if($nb_per_page == self::_TOTAL_PER_PAGE_) {
$pdf->addPage();
$nb_per_page = 0;
}
2016-03-09 16:33:48 +01:00
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell(48, 4, '', 0,0, 'C');
} else {
$this->_printLabel($pdf, $product['label'], 1);
}
2016-02-22 12:33:51 +01:00
}
$pdf->Ln(4);
2016-03-09 16:33:48 +01:00
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell(48, 10, '', 0,0, 'C');
} else {
$ean_image = $this->directory.'/ean-'.$product['key'].'.gif';
$this->_printEAN($pdf, $ean_image, 1);
}
2016-02-22 12:33:51 +01:00
}
$pdf->Ln(13);
$nb_per_page += 1;
}
}
$pdf->Output($this->directory.'/barcode.pdf', 'F');
ob_clean();
return TRUE;
}
private function _printLabel($pdf, $label, $limit = 1) {
$pdf->SetFont('Arial', '', 5);
for ($i=0; $i < $limit; $i++) {
$pdf->Cell(48, 4, substr(utf8_decode($label), 0, 35), 0,0, 'C');
}
}
2016-03-09 16:33:48 +01:00
2016-02-22 12:33:51 +01:00
private function _printEAN($pdf, $ean_image, $limit = 1) {
2016-03-09 16:33:48 +01:00
for ($i=0; $i < $limit; $i++) {
2016-02-22 12:33:51 +01:00
$pdf->Cell(48, 10, $pdf->Image($ean_image, $pdf->GetX(), $pdf->GetY(),48, 10), 0,0, 'C');
}
}
}