277 lines
9.3 KiB
PHP
277 lines
9.3 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;
|
|
|
|
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 = $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
|
|
);
|
|
}
|
|
} else {
|
|
for ($i=1; $i <= $quantity; $i++) {
|
|
$this->products[] = array(
|
|
'key' => $id_product.'-0',
|
|
'label' => $p['name'],
|
|
'ref' => $ref
|
|
);
|
|
}
|
|
}
|
|
//$this->products[] = array();
|
|
}
|
|
|
|
public function printPDF() {
|
|
global $cookie;
|
|
|
|
// 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",
|
|
"ref" => "FIX-06-86763"
|
|
);
|
|
}
|
|
for ($i=31; $i < 80; $i++) {
|
|
$this->products[$i] = array(
|
|
'key' => "125416-332",
|
|
'label' => "03 mois (62 cm)- Ensemble grenouill",
|
|
"ref" => "FIX-04-86763"
|
|
);
|
|
}*/
|
|
|
|
if (empty($this->products)) {
|
|
die('Tous les produits ont déjà un EAN');
|
|
}
|
|
$this->products = $this->_sortProducts($this->products, 'ref');
|
|
|
|
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($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_);
|
|
|
|
if($nb_per_page == self::_TOTAL_PER_PAGE_) {
|
|
$pdf->addPage();
|
|
$nb_per_page = 0;
|
|
}
|
|
|
|
foreach ($product_per_line as $key => $product) {
|
|
if (empty($product)) {
|
|
$pdf->Cell(48, 4, '', 0,0, 'C');
|
|
} else {
|
|
$this->_printLabel($pdf, $product['label'], 1);
|
|
}
|
|
}
|
|
$pdf->Ln(4);
|
|
|
|
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);
|
|
}
|
|
}
|
|
$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');
|
|
}
|
|
}
|
|
|
|
private function _printEAN($pdf, $ean_image, $limit = 1) {
|
|
for ($i=0; $i < $limit; $i++) {
|
|
$pdf->Cell(48, 10, $pdf->Image($ean_image, $pdf->GetX(), $pdf->GetY(),48, 10), 0,0, 'C');
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|