Esempio n. 1
0
    public function printPDF($type, $categorie_id = 1, $products = array(), $declinaisons = array(), $unitmeasure = 'mm', $orientation = 'P', $font_size = 10, $color = '000000', $margins = array('top' => 0, 'left' => 0, 'right' => 0, 'bottom' => 0), $barheight = 80, $barwidth = 50, $blocheight = 150, $blocwidth = 100, $blockborder = 0, $dimension = 'A4', $stretch = 0, $setborder = 1, $showname = 1, $showref = 1, $showprice = 1, $nbproduct = 1000, $manufacturer_id = 0)
    {
        $usereduction = Configuration::get('JC-EANUPC-usereduction');
        $font_size_barcode = $font_size;
        $unitmeasure == 'mm' ? $font_size *= 2.84 : ($font_size *= 1);
        $color = $this->hex2RGB(trim($color));
        $max_length = 1000;
        if (Configuration::get('JC-EANUPC-truncateproductname')) {
            $max_length = Configuration::get('JC-EANUPC-truncateproductname');
        }
        $allproduct = array();
        if (!isset($products) || empty($products) || !$products || $products['0'] == '0') {
            $allproduct = Product::getProducts((int) $this->context->language->id, 0, '3000', 'id_product', 'ASC', $categorie_id);
        } else {
            if (empty($declinaisons)) {
                $i = 0;
                foreach ($products as $product) {
                    $p = new Product($product, false, $this->context->language->id);
                    $allproduct[$i]['id_product'] = $p->id;
                    $allproduct[$i]['name'] = $p->name;
                    $allproduct[$i]['price'] = Product::getPriceStatic($p->id, true, null, 6, null, false, $usereduction);
                    $allproduct[$i]['reference'] = $p->reference;
                    $allproduct[$i]['ean13'] = $p->ean13;
                    $allproduct[$i]['upc'] = $p->upc;
                    $allproduct[$i]['id_manufacturer'] = $p->id_manufacturer;
                    $allproduct[$i]['quantity'] = Product::getQuantity($p->id);
                    $i++;
                }
            } else {
                $i = 0;
                $comb_array = array();
                foreach ($products as $product) {
                    $p = new Product($product, $this->context->language->id);
                    if ($p->id) {
                        /* Build attributes combinations */
                        $combinations = $p->getAttributeCombinations($this->context->language->id);
                        $groups = array();
                        if (is_array($combinations)) {
                            foreach ($combinations as $k => $combination) {
                                if (in_array($combination['id_product_attribute'], $declinaisons)) {
                                    // $price_to_convert = Tools::convertPrice($p->getPrice(true), $this->context->currency);
                                    // $price = Tools::displayPrice($price_to_convert, $this->context->currency);
                                    $comb_array[$combination['id_product_attribute']]['id_product_attribute'] = $combination['id_product_attribute'];
                                    $comb_array[$combination['id_product_attribute']]['attributes'][] = $combination['group_name'] . '-' . $combination['attribute_name'];
                                    $comb_array[$combination['id_product_attribute']]['wholesale_price'] = $combination['wholesale_price'];
                                    $comb_array[$combination['id_product_attribute']]['price'] = Product::getPriceStatic($p->id, true, $combination['id_product_attribute'], 6, null, false, $usereduction);
                                    $comb_array[$combination['id_product_attribute']]['weight'] = $combination['weight'] . Configuration::get('PS_WEIGHT_UNIT');
                                    $comb_array[$combination['id_product_attribute']]['unit_impact'] = $combination['unit_price_impact'];
                                    $comb_array[$combination['id_product_attribute']]['reference'] = $combination['reference'];
                                    $comb_array[$combination['id_product_attribute']]['ean13'] = $combination['ean13'];
                                    $comb_array[$combination['id_product_attribute']]['quantity'] = $combination['quantity'];
                                    $comb_array[$combination['id_product_attribute']]['upc'] = $combination['upc'];
                                    $comb_array[$combination['id_product_attribute']]['product_name'] = $p->name[$this->context->language->id];
                                }
                            }
                        }
                    }
                }
                $i = 0;
                foreach ($comb_array as $product) {
                    // $p = new Product($product, false, $this->context->language->id);
                    // $allproduct[$i]['id_product'] = $product['id_product'];
                    $allproduct[$i]['name'] = $product['product_name'] . ' | ' . implode(',', $product['attributes']);
                    $allproduct[$i]['price'] = $product['price'];
                    $allproduct[$i]['reference'] = $product['reference'];
                    $allproduct[$i]['ean13'] = $product['ean13'];
                    $allproduct[$i]['upc'] = $product['upc'];
                    $allproduct[$i]['quantity'] = $product['quantity'];
                    // $allproduct[$i]['id_manufacturer'] = $p->id_manufacturer;
                    $i++;
                }
            }
        }
        $array = array(1 => 'one', 2 => 'two', 3 => 'three');
        if ($manufacturer_id != 0) {
            foreach ($allproduct as $key => $product) {
                if ($product['id_manufacturer'] != $manufacturer_id) {
                    unset($allproduct[$key]);
                }
            }
        }
        if ($dimension == 'C76') {
            $custom_layout = array($blocwidth + $margins['left'] + $margins['right'], $blocheight + $barheight);
            $pdf = new TCPDF($orientation, $unitmeasure, $custom_layout);
        } else {
            $pdf = new TCPDF($orientation, $unitmeasure, $dimension);
        }
        $pdf->SetCreator('bqbarcodegeneration');
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $pdf->SetFont('helvetica', '', $font_size);
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);
        // set font
        $pdf->SetFont('freeserif', '', $font_size);
        $pdf->SetMargins($margins['left'], $margins['top'], $margins['right'], false);
        $pdf->SetHeaderMargin(0);
        $pdf->SetFooterMargin(0);
        // set auto page breaks
        $pdf->SetAutoPageBreak(true, $margins['bottom']);
        $page_width = TcpdfStatic::getPageSizeFromFormat($dimension);
        $nblignes = 1;
        if ($dimension == 'C76') {
            $nbcol = 1;
        } else {
            $nbcol = (int) $page_width[0] / ($blocwidth / 25.4 * 72);
            $nblignes = (int) $page_width[1] / ($blocheight / 25.4 * 72);
        }
        if ((int) $nbcol == 0) {
            $nbcol = 1;
        }
        // limit division by zero
        $c = 0;
        if ($type == 'upc') {
            $type2 = 'UPCA';
        } else {
            $type2 = $type;
        }
        $htmloutput = '<table border="0" ><tr>';
        foreach ($allproduct as $product) {
            $quantity = 0;
            if ($nbproduct == 0) {
                $quantity = $product['quantity'];
            } else {
                $quantity = $nbproduct;
            }
            if ($quantity == 0) {
                $quantity++;
            }
            for ($i = 0; $i < $quantity; $i++) {
                $barcode = $product[$type];
                $pdf_params = $pdf->serializeTCPDFtagParameters(array(Tools::substr($barcode, 0, -1), $type2, '', '', $barwidth, $barheight, 0.4, array('position' => 'C', 'border' => $setborder == 2 ? true : false, 'padding' => 0, 'fitwidth' => false, 'fgcolor' => $color, 'bgcolor' => array(255, 255, 255), 'text' => true, 'font' => 'Helvetica', 'font_size' => $font_size_barcode, 'stretchtext' => $stretch, 'label' => ''), 'N'));
                if ($c % $nbcol == 0 && $c > 0 && $dimension != 'C76') {
                    $htmloutput .= '</tr><tr>';
                }
                // $currency = Currency::getCurrent();
                // convert to pixel unit 1mm = 2.84
                if (empty($barcode)) {
                    $this->html .= '<div class=" error">' . $this->l('There is one or more missed code in selected type, plz regenerate missed code and try again') . '</div>';
                    return $this->html;
                }
                $autre = '';
                if ($showname) {
                    $autre .= ' <tr><td><div style="text-align:left;">' . Tools::truncate($product['name'], $max_length) . '</div></td></tr>';
                }
                // if ($showprice)
                // $autre .= ' <tr><td><p style="text-align:left">'.$this->l('Price').': '.Tools::displayPrice(Product::getPriceStatic($product['id_product'])).'</p></td></tr>';
                if ($showprice) {
                    $autre .= ' <tr><td><p style="text-align:left">' . $this->l('Price') . ': ' . Tools::displayPrice($product['price']) . '</p></td></tr>';
                }
                if ($showref) {
                    $autre .= ' <tr><td><p style="text-align:left">' . $this->l('Reference') . ': ' . $product['reference'] . '</p></td></tr>';
                }
                $htmloutput .= '<td style="' . ($blockborder == 1 ? 'border:1px dashed #555; border-right:0px solid #fff; ' : '') . ' font-size:' . $font_size . '%;margin:0;padding:0">
			&nbsp;&nbsp;<table>
			' . $autre . '
			</table>';
                $htmloutput .= '<table border="0" ><tr><td colspan=2>
			<tcpdf method="write1DBarcode" params="' . $pdf_params . '"/></td></tr>
			<tr><td style="font-size:150%"><p style="text-align:center">' . Tools::substr($barcode, 0, 1) . ' ' . wordwrap(Tools::substr($barcode, 1, 12), 4, '  ', true) . '</p></td></tr></table>';
                $htmloutput .= '</td>';
                $c++;
                if ($dimension == 'C76') {
                    $htmloutput .= '</tr></table>';
                    $pdf->AddPage();
                    $pdf->writeHTML($htmloutput, true, 0, true, 0);
                    $htmloutput = '<table border="0" ><tr>';
                }
            }
        }
        $htmloutput .= '</tr></table> ';
        if ($dimension != 'C76') {
            // $pdf->SetAutoPageBreak(true, 0);
            $pdf->AddPage();
            $pdf->writeHTML($htmloutput, true, 0, true, 0);
        }
        $filename = dirname(__FILE__) . '/pdf/' . $type . '.pdf';
        //Output the document
        ob_clean();
        $pdf->Output($filename, 'F');
        header('Content-type: application/pdf; charset=utf-8');
        header('Content-Disposition: inline; filename="$filename"');
        header('Content-Length: ' . filesize($filename));
        readfile($filename);
    }