bebeboutik/modules/labelgenerate/models/generatebarcode.php
2017-03-02 10:32:48 +01:00

379 lines
13 KiB
PHP

<?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;
const _NB_PER_LINE_BIG_ = 4;
const _TOTAL_PER_PAGE_BIG_ = 10;
public function __construct() {
if (is_null(self::$barcode_directory)) {
self::$barcode_directory = dirname(__FILE__).'/../img/';
}
}
public function createRackFolder() {
if (!(is_dir(self::$barcode_directory.'rack'))) {
mkdir(self::$barcode_directory.'rack', 0775);
}
}
public function generateRack($name) {
$width = 320;
$height = 80;
$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", $name, 3, 50);
imagettftext($im, 11, 0, $width / 2 - (strlen('*'.$name.'*')* 4), $height - 10 , $black, dirname(__FILE__).'/arial.ttf', '*'.$name.'*' );
imagegif($im, self::$barcode_directory.'rack/img-'.$name.'.gif');
}
public function printRackPDF($codes) {
ob_start();
$pdf = new FPDF('P', 'mm', 'A4');
$nb_per_page = 0;
$pdf->SetMargins(9,12,9);
$pdf->SetAutoPageBreak(FALSE);
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$nb_line = ceil((count($codes)/4));
for ($i=1; $i <= $nb_line; $i++) {
$img_per_line = array_slice($codes, 0, 4);
$codes = array_splice($codes, 4);
if($nb_per_page == self::_TOTAL_PER_PAGE_) {
$pdf->addPage();
$nb_per_page = 0;
}
foreach ($img_per_line as $key => $value) {
$this->_printLabel($pdf, $value, 1);
}
$pdf->Ln(4);
foreach ($img_per_line as $key => $value) {
$image = self::$barcode_directory.'rack/img-'.$value.'.gif';
$this->_printEAN($pdf, $image, 1);
}
$pdf->Ln(13);
$nb_per_page += 1;
}
$pdf->Output(self::$barcode_directory.'rack/rack.pdf', 'F');
ob_clean();
return TRUE;
}
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 = NULL) {
if($ean == null) {
$ean = $this->_getEanNumber();
}
$str = $ean;
$width = 320;
$height = 80;
$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.'*' );
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() {
// min 1111111111
$number = Configuration::get('BARCODE_NUMBER') + 1;
Configuration::updateValue('BARCODE_NUMBER', $number);
return $number;
}
public function assocProduct($id_product, $id_product_attribute = NULL, $quantity, $ref) {
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);
if ($id_product_attribute) {
$details = Product::getDetailsCombination($id_product_attribute, $cookie->id_lang);
$name_combination = '';
foreach ($details as $key_attr => $detail) {
$name_combination .= $detail['attribute_name'].'-';
}
for ($i=1; $i <= $quantity; $i++) {
$this->products[] = array(
'key' => ($id_product.'-'.$id_product_attribute),
'label' => ($name_combination . ' ' . $p['name']),
'ref' => $ref,
'decli' => substr($name_combination,0,-1)
);
}
} else {
for ($i=1; $i <= $quantity; $i++) {
$this->products[] = array(
'key' => $id_product.'-0',
'label' => $p['name'],
'ref' => $ref,
'decli' => ''
);
}
}
//$this->products[] = array();
}
public function printPDF($big_label = false, $file = 'barcode') {
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;
}
// données test
// for ($i=0; $i < 60; $i++) {
// $this->products[$i] = array(
// 'key' => "125415-323",
// 'label' => "06 mois (68 cm)- Lot de 2 bodies Ri",
// "ref" => "FIX-06-86763",
// "decli" => '06 mois (68 cm)'
// );
// }
// for ($i=61; $i < 80; $i++) {
// $this->products[$i] = array(
// 'key' => "125416-332",
// 'label' => "03 mois (62 cm) - Ensemble grenouill",
// "ref" => "FIX-04-86763",
// "decli" => '03 mois (62 cm)'
// );
// }
// for ($i=81; $i < 100; $i++) {
// $this->products[$i] = array(
// 'key' => "125415-322",
// 'label' => "03 mois (62 cm) - Ensemble grenouill",
// "ref" => "FIX-06-86763",
// "decli" => '03 mois (62 cm)'
// );
// }
// for ($i=101; $i < 145; $i++) {
// $this->products[$i] = array(
// 'key' => "125417-325",
// 'label' => "06 mois (68 cm) - Ensemble grenouill",
// "ref" => "FIX-18-32094",
// "decli" => '06 mois (68 cm)'
// );
// }
// for ($i=146; $i < 206; $i++) {
// $this->products[$i] = array(
// 'key' => "125417",
// 'label' => "06 mois (68 cm) - Ensemble grenouill",
// "ref" => "FIX-16-32094",
// "decli" => '06 mois (68 cm)'
// );
// }
if (empty($this->products)) {
return false;
}
// if($generate_ref){
// $this->products = $this->_multiSortProduct($this->products);
// } else {
// $this->products = $this->_sortProducts($this->products, 'ref');
// }
$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;
}
if($big_label){
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell($width, $height_text, '', 0,0, 'C');
} else {
$this->_printLabel($pdf, $product['ref'], 1,$width,$height_text, $font_size_ref);
}
}
$pdf->Ln(($ln_text));
}
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell($width, $height_text, '', 0,0, 'C');
} else {
$this->_printLabel($pdf, $product['label'], 1,$width,$height_text, $font_size);
}
}
$pdf->Ln(($ln_text));
foreach ($product_per_line as $key => $product) {
if (empty($product)) {
$pdf->Cell($width, $height_label, '', 0,0, 'C');
} else {
$ean_image = $this->directory.'/ean-'.$product['key'].'.gif';
$this->_printEAN($pdf, $ean_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);
for ($i=0; $i < $limit; $i++) {
$pdf->Cell($width, $height, substr(utf8_decode($label), 0, 35), 0,0, 'C');
}
}
private function _printEAN($pdf, $ean_image, $limit = 1, $width = 48, $height = 10) {
for ($i=0; $i < $limit; $i++) {
$pdf->Cell($width, $height, $pdf->Image($ean_image, $pdf->GetX(), $pdf->GetY(),($width-2), $height), 0,0, 'C');
}
}
private function _multiSortProduct($products = array()){
if (!empty($products)) {
$ref = array();
$decli = array();
foreach ($products as $key => $row) {
$ref[$key] = $row['ref'];
$decli[$key] = $row['decli'];
}
array_multisort($ref, SORT_ASC, $decli, SORT_ASC, $products);
$ref = '';
$result = array();
foreach ($products as $k => $v) {
if ($ref !== $v['ref'] && !empty($ref)) {
$result[] = array();
$result[] = $v;
$ref = $v['ref'];
continue;
}
$result[] = $v;
$ref = $v['ref'];
}
}
return $result;
}
private function _sortProducts($array = array(), $field, $order=SORT_ASC) {
$new_array = array();
$sortable_array = array();
$products = array();
if (!empty($array)) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $field) {
$sortable_array[$k] = $v2;
}
}
} elseif (!empty($v)) {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[] = $array[$k];
}
$ref = '';
foreach ($new_array as $k => $v) {
if ($ref !== $v['ref'] && !empty($ref)) {
$products[] = array();
$products[] = $v;
$ref = $v['ref'];
continue;
}
$products[] = $v;
$ref = $v['ref'];
}
}
return $products;
}
}