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

$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"];

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();
 
$ctr = 0;
$breaker = 1;

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

while ($ctmp = mysqli_fetch_object($cmain)) {
    $ctr++;
    if ($_GET["para"] == 3) {
        $num_qty = 1;
        $vouchdt = date("Y-m-d");
    } else {
        $num_qty = $ctmp->qty;
        $vouchdt = $ctmp->vouchdt;
    }
    $nameprint = $ctmp->name;
    $weight = $ctmp->weight;
    $unit = $ctmp->unit;

    $batch = $ctmp->batch;

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

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

    // Add pages and content for each quantity
    $ht = 8;
    for ($i = 1; $i <= $num_qty; $i++) {
        $ctr = 0;
        $x = 9;

        $pdf->AddPage();
        $pdf->SetXY($x, 4);

        $wt = $weight . $unit . ' e ';

        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(86, $ht, "$nameprint", 0, 0, 'C');
//        $pdf->Cell(43, $ht, 'TRAVOLIC', 0, 0, 'C');

        $pdf->SetXY($x, 15);
        $pdf->Code128($x, 15, $barcode, 86, 12);

        $pdf->SetXY($x, 26);
        $pdf->Cell(86, $ht, "$barcode", 0, 0, 'C');

        $pdf->SetXY($x, 32);
        $pdf->SetFont('Arial', 'B', 14);
        $pdf->Cell(86, $ht, "N/A", 0, 0, 'C');
//        $pdf->Cell(86, $ht, "$wt", 0, 0, 'C');

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

        $bbdt = date("d", strtotime($vouchdt));
        $bbyr = $bbfyr;

//        $bestbef = 'Best Before : ' . $bbdt . '-' . $bbmon . '-' . $bbyr;
        $bestbef = 'Best Before : N/A';
        $pdf->SetFont('Arial', 'B', 12);
        $pdf->SetXY($x, 40);
//        $pdf->SetFont('aefurat', 'B', 8);
        $pdf->Cell(86, 5, $bestbef, 0, 0, 'C');

        $coforg = 'INDIA';
        $counorg = 'Country Of Origin : ' . $coforg;

        $pdf->SetXY($x, 46);
        $pdf->SetFont('Arial', 'B', 11);
        $pdf->Cell(86, $ht, "$counorg", 0, 0, 'C');

        $batchname = 'Batch : ' . $batch;

        $pdf->SetXY($x, 52);
        $pdf->SetFont('Arial', 'B', 11);
        $pdf->Cell(86, $ht, "N/A", 0, 0, 'C');
//        $pdf->Cell(86, $ht, "$batchname", 0, 0, 'C');
    }
}

$pdf->Output();
?>