<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];

$compcd = $_SESSION["comp_" . $dbname];
$series = $_SESSION["series_" . $dbname];

$maindb = 'product';
$trandb = '';
$mvouch = $_REQUEST["barcode"];
$tabname = $_REQUEST["tabname"];

function nf($val)
{
    $val = str_replace(',', '', $val);
    // check numeric and greater than zero
    if (!is_numeric($val) || $val <= 0) {
        return "Nil";
    }
    return number_format((float)$val, 1);
}



if ($_GET["para"] == 3) {
    $mvouchdt = date("Y-m-d");
    $maindb = 'product';
    $trandb = '';
} else {
    $mvouch = $_REQUEST["vouchno"];
}

if ($_GET["para"] == 3) {
    $cmain = mysqli_query($conn, "SELECT * FROM `$maindb` where barcode != '' order by code") or die(mysqli_error($conn));
    $num = 1;
    $xnum = $num - 1;

//    $gcomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
//    $fcomp = mysqli_fetch_object($gcomp);
//    $cname = $fcomp->name;
} else {
    $cmain = mysqli_query($conn, "SELECT a.*, b.name, b.barcode, b.mrp FROM `inwtran` a, product b WHERE a.vouchno = '$mvouch' and a.itemcode = b.code order by srno") or die(mysqli_error($conn));
}
//print table($cmain);
//die();
$getcmpquery = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
$ftcmpquery = mysqli_fetch_object($getcmpquery);


$ctr = 0;
$breaker = 1;

$add1 = $ftcmpquery->hoadd1;
$add2 = $ftcmpquery->hoadd2;

require('library/fpdf/code128.php');
$pdf = new PDF_Code128('P', 'mm', array(75, 120));

while ($ctmp = mysqli_fetch_object($cmain)) {
    $ctr++;
    if ($_GET["para"] == 3) {
        $num_qty = 1;
        $vouchdt = date("Y-m-d");
        $batch = $ctmp->batch;
        $unit = $ctmp->unit;
        $bbfmon = $ctmp->bbf;
        if ($bbfmon < 10) {
            $bestbef = '0' . $bbfmon . " Months";
        } else {
            $bestbef = $bbfmon . " Months";
        }
    } else {
        $num_qty = $ctmp->qty;
        $vouchdt = $ctmp->vouchdt;
        if ($ctmp->batchno != "") {

            $batch = $ctmp->batchno;
        } else {
            $batch = $ctmp->batch;
        }
        $unit = $ctmp->unitgm;
        $bbfmon = $ctmp->bbfmon;

        if ($bbfmon < 10) {
            $bbmon = '0' . $bbfmon;
        } else {
            $bbmon = $bbfmon;
        }

        $bbfyr = $ctmp->bbfyear;
        $bbyr = $bbfyr;


        $bestbef = $bbmon . ' / ' . $bbyr;
    }
    $nameprint = $ctmp->name;
    $weight = $ctmp->weight;
    $origin = $ctmp->origin;


    $rate = round($ctmp->rate, 0);
    $barcode = trim($ctmp->barcode);


    $energy = $ctmp->energy;
    $kcal = $ctmp->kcal;
    $fat = $ctmp->fat;
    $saturate = $ctmp->saturate;
    $carbohydrate = $ctmp->carbohydrate;
    $sugar = $ctmp->sugar;
    $protein = $ctmp->protein;
    $fibre = $ctmp->fibre;
    $salt = $ctmp->salt;
    $prodtype = $ctmp->type;

    $prodrem1 = $ctmp->prodrem1;
    $prodrem2 = $ctmp->prodrem2;
    $prodrem3 = $ctmp->prodrem3;

    // Add pages and content for each quantity
    $ht = 8;
    for ($i = 1; $i <= $num_qty; $i++) {
        $ctr = 0;
        $x = 3;
        $nt = 35;
        $nty = 31;
        $y = 5;

        $pdf->AddPage();

        $font = 'Arial';
        $h = 8;

        $produce =  $origin;
        $wt = $weight . '  ' . $unit;

        $pdf->SetXY($x, $y);

        $pdf->SetFont($font, 'B', 12);
        $pdf->Cell(70, $h, "$nameprint", 0, 0, 'C');

        $y = $y + 6;
        $pdf->SetXY($x, $y);
        $pdf->SetFillColor(0, 0, 0);
        $image1 = "images/organiclogo1.jpeg";
        $image2 = "images/organiclogo2.jpeg";
        $pdf->Code128(18, $y, "$barcode", 40, 10);

        if ($prodtype == "Organic") {
            // Add logos beside barcode
            $pdf->Image("images/organiclogo2.jpeg", 3, $y, 12, 10);
            $pdf->Image("images/organiclogo1.jpeg", 60, $y, 12, 10);
        }

        $y = $y + 9;
        $pdf->SetXY($x, $y);
        $pdf->SetFont($font, 'B', 9);
        $pdf->Cell(72, $ht, "$barcode", 0, 0, 'C');

        $y = $y + 5;
        $pdf->SetFont($font, 'B', 10);
        $pdf->SetXY(8, $y);
        $pdf->Cell(20, $h, "Net Weight : ", 0, 0, 'R');

        $y = $y + 2;
        $pdf->SetXY(3, $y);
        $pdf->SetDrawColor(0, 0, 0);
        $pdf->SetFillColor(200, 220, 255);
        $pdf->RoundedRect(30, 28, 42, 34, 4, 'D');

        $pdf->SetXY(30, $y);
        $pdf->SetFont($font, 'U', 8);
        $pdf->Cell($nt, $h, "Nutritional Information per 100g", 0, 0, 'L');

        $y = $y + 4;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'B', 15);
        $pdf->Cell(21, $h, "N/A", 0, 0, 'C');
        $pdf->SetFont($font, 'B', 18);
        $pdf->Cell(5, $h, "", 0, 0, 'L');

        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Energy (kJ)", 0, 0, 'L');
        // if ($energy > 0) {
        //     $energy = number_format($energy, 1);
        // } else {
        //     $energy = "Nil";
        // }

        $pdf->Cell(5, $h,  nf($energy), 0, 0, 'R');

        $y = $y + 3;
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Energy (kcal)", 0, 0, 'L');
        // if ($kcal > 0) {
        //     $kcal = number_format($kcal, 1);
        // } else {
        //     $kcal = "Nil";
        // }
        $pdf->Cell(5, $h, nf($kcal), 0, 0, 'R');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(25, $h, "Country of Origin", 0, 0, 'L');
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Fat(g)", 0, 0, 'L');
        // if ($fat > 0) {
        //     $fat = number_format($fat, 1);
        // } else {
        //     $fat = "Nil";
        // }
        $pdf->Cell(5, $h,  nf($fat), 0, 0, 'R');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'B', 8);
        $pdf->Cell(25, $h, "$produce", 0, 0, 'L');
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "of which Satuarates (g)", 0, 0, 'L');
        // if ($saturate > 0) {
        //     $saturate = number_format($saturate, 1);
        // } else {
        //     $saturate = "Nil";
        // }
        $pdf->Cell(5, $h,  nf($saturate), 0, 0, 'R');

        $y = $y + 3;
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Carbohydrate (g)", 0, 0, 'L');
        // if ($carbohydrate > 0) {
        //     $carbohydrate = number_format($carbohydrate, 1);
        // } else {
        //     $carbohydrate = "Nil";
        // }
        $pdf->Cell(5, $h,  nf($carbohydrate), 0, 0, 'R');


        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(25, $h, "Best Before End :", 0, 0, 'L');
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "of Which Sugars(g)", 0, 0, 'L');
        // if ($sugar > 0) {
        //     $sugar = number_format($sugar, 1);
        //     } else {
        //         $sugar = "Nil";
        // }
        $pdf->Cell(5, $h,  nf($sugar), 0, 0, 'R');


        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'B', 8);
        $pdf->Cell(25, $h, "N/A", 0, 0, 'L');
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Fibre(g)", 0, 0, 'L');
        // if ($fibre > 0) {
        //     $fibre = number_format($fibre, 1);
        // } else {
        //     $fibre = "Nil";
        // }
        $pdf->Cell(5, $h,  nf($fibre), 0, 0, 'R');

        $y = $y + 3;
        $pdf->SetXY($nty, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell($nt, $h, "Protein(g)", 0, 0, 'L');
        // if ($protein > 0) {
        //     $protein = number_format($protein, 1);
        // } else {  
        //     $protein = "Nil";
        // }
            $pdf->Cell(5, $h,  nf($protein), 0, 0, 'R');

            $y = $y + 3;
            $pdf->SetXY($nty, $y);
            $pdf->SetFont($font, '', 8);
            $pdf->Cell($nt, $h, "Salt(g)", 0, 0, 'L');
            // if ($salt > 0) {
            //     $salt = number_format($salt, 1);
            //     } else {
            //         $salt = "Nil";
            //     }
            $pdf->Cell(5, $h,  nf($salt), 0, 0, 'R');

        $y = $y + 2;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(25, $h, "Batch Code :", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'B', 8);
        $pdf->Cell(25, $h, "N/A", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY($nty + 2, $y);
        $pdf->Cell($nt, $h, "Store in a cool, dry place", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(72, $h, "Ingredients", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 6);
        $pdf->Cell(72, $h, "$prodrem1", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 6);
        $pdf->Cell(72, $h, "$prodrem2", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 6);
        $pdf->Cell(72, $h, "$prodrem3", 0, 0, 'L');

        $allergy1 = 'It may contain traces of nuts, peanut, seasame seeds,';
        $allergy2 = 'mustard, soya, wheat(gluten), bakery(gluten), milk, celery, sulphites ';

        $y = $y + 5;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'BU', 6);
        $pdf->Cell(12, $h, "Allergens : ", '', 0, 'L');
        $pdf->SetFont($font, 'B', 6);
        $pdf->Cell(57, $h, "$allergy1", 0, 0, 'L');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, 'B', 6);
        $pdf->Cell(72, $h, "$allergy2", 0, 0, 'L');

        $y = $y + 5;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(72, $h, "$add1", 0, 0, 'C');

        $y = $y + 3;
        $pdf->SetXY(3, $y);
        $pdf->SetFont($font, '', 8);
        $pdf->Cell(72, $h, "$add2", 0, 0, 'C');

        ////////////////

        //        $pdf->Cell(59, $h, "7 Constitution hill Unit 19,", 0, 0, 'R');
        //        $pdf->Cell(51, $h, "Unit 21 Matrix House", 0, 0, 'R');
        //        $pdf->Cell(44.5, $h, "Leicester LE1 1PL UK", 0, 0, 'R');
        //        $pdf->ln(4);
    }
}

$pdf->Output("new.pdf", "I");
