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

//require_once('library/fpdf/fpdf.php');
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdir"];

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

$maindb = '';
$trandb = '';

        
if ($_GET["para"] == 1) {
    $mvouch = $_REQUEST["barcode"];
    $mvouchdt = date("Y-m-d");
    $maindb = 'product';
    $trandb = '';
} elseif ($_GET["para"] == 2) {
    $mvouch = $_REQUEST["vouchno"];
    $maindb = 'openmain';
    $trandb = 'opentran';
    $mvouchdt = date("Y-m-d");
} elseif ($_GET["para"] == 3) {
    $mvouch = $_GET["barcode"];
    $maindb = 'purmain';
    $trandb = 'purtran';
} elseif ($_GET["para"] == 4) {
    $mvouch = $_REQUEST["vouchno"];
    $maindb = 'inwmain';
    $trandb = 'inwtran';
}

if ($_GET["para"] == 1) {
    //    require("includes/dbconnect.php");
    $cmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code='$mvouch'") or die(mysqli_error($conn));
    $num = $_GET["number"];
    $xnum = $num - 1;
} else {
    mysqli_select_db($conn, $zdb);
    $cmain = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$mvouch' group by itemcode ORDER BY srno") or die(mysqli_error($conn));
    $num = mysqli_num_rows($cmain);
    $xnum = $num - 1;
}

$ctr = 0;
$breaker = 1;
require ('../library_all/fpdf/code128.php');
$pdf = new PDF_Code128('P', 'mm', array(46, 44));

while ($ctmp = mysqli_fetch_object($cmain)) {
    $ctr++;
    if ($_GET["para"] == 1) {
        $num_qty = $_GET["number"];
        $color = $_GET["color"];
        
        
        $mrp = round($ctmp->rate,0);
        $article = trim($ctmp->name);
        $barcode = trim($ctmp->code);
        

        } else {

        $num_qty = $ctmp->qty;
        $num_qty = 1 ;           // default article and color only
        
        $itemcode = $ctmp->barcode;
        $colr = $ctmp->colrcode;
        
        $citemname = mysqli_query($conn, "SELECT * FROM `product` WHERE code='$itemcode' ") or die(mysqli_error($conn));
        $fitemname = mysqli_fetch_array($citemname);
        
        $article = trim($fitemname['name']);
        $barcode = trim($fitemname['barcode']);
        $mrp = round($fitemname['rate'],0);
        
        $ccolr = mysqli_query($conn, "SELECT * FROM `colour` WHERE code='$colr' ") or die(mysqli_error($conn));
        $fcolr = mysqli_fetch_array($ccolr);
        $color = $fcolr['name'];
        
    }
    $x = 7;
    for ($i = 1; $i <= $num_qty; $i++) {
        $pdf->AddPage();


        $art = 'ART : ' . $article ;
        $pdf->SetXY($x,2);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(30,4, $art, 0, 0, 'C');
        
//        $pdf->SetXY(2,3);
        $pdf->Code128($x,7, $barcode, 32, 6);

        $pdf->SetXY($x,13);
        $pdf->SetFont('Arial', 'B', 7);
        $pdf->Cell(30,4, $barcode, 0, 0, 'C');
        
//        $mrpd = 'RATE : ' . $mrp ;          
        $mrpd = $mrp ;          
        
        $pdf->SetXY($x,16);
        $pdf->SetFont('Arial', 'B', 8);
        $pdf->Cell(14, 4, 'Rate : ' , 0, 0, 'R');
        $pdf->Image('images/rupeesymbol.jpg', $x+14, 17 , +2);
        $pdf->Cell(15, 4, $mrpd , 0, 0, 'C');
        
        // $pdf->SetXY($x,19);
        // $pdf->SetFont('Arial', 'B', 8);
        // $pdf->Cell(30, 4, $color , 0, 0, 'C');

//        $nxtdate = date("d-m-Y", strtotime($expdate)) ;
//        $bb = 'Best Before : ' . $nxtdate;          
//        $pdf->SetXY(5,26);
//        $pdf->SetFont('Arial', 'B', 7);
//        $pdf->Cell(28, 4, $bb , 0, 0, 'C');

//        $licno = 'FSSAI LIC NO.:11520001000123'  ;          
//        $pdf->SetXY(3,29);
//        $pdf->SetFont('Times', 'B', 7);
//        $pdf->Cell(32, 4, $licno , 0, 0, 'C');

//        $pdf->SetXY("3", '15');
//        $pdf->SetFont('Arial', 'B', 9);
//
//        $pdf->Cell(0, 0, $itemname, 0);
//
//        $pdf->SetFont('Arial', 'B', 11);
//
//        $pdf->SetXY("10", '21');
//        $pdf->Cell(0, 0, 'BENHUR', 0);
//
//        $pdf->SetXY("6", '27');
//        $pdf->Cell(0, 0, 'MRP : ' . $mrp, 0);

    }   
        
}
$pdf->Output();

?>