<?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);
$maindb = '';
$trandb = '';
$mvouch = $_REQUEST["vouchno"];

if ($_GET["para"] == 1) {
    $maindb = 'purmain';
    $trandb = 'purtran';
    $mvouchdt = $_REQUEST["vouchdt"];
} elseif ($_GET["para"] == 2) {
    $maindb = 'openmain';
    $trandb = 'opentran';
    $mvouchdt = date("Y-m-d");
} elseif ($_GET["para"] == 3) {
    $mvouchdt = date("Y-m-d");
    $maindb = 'item';
    $trandb = '';
}
$d = date_parse_from_format("Y-m-d", $mvouchdt);
$month = $d["month"];

$year = $d["year"];
$tyear = substr($year, 2);
$year1 = substr($tyear, 1);
$year2 = substr($tyear, 0, 1);

switch ($month) {
    case '1':
        $monn = 'A';
        break;
    case '2':
        $monn = 'B';
        break;
    case '3':
        $monn = 'C';
        break;
    case '4':
        $monn = 'D';
        break;
    case '5':
        $monn = 'E';
        break;
    case '6':
        $monn = 'F';
        break;
    case '7':
        $monn = 'G';
        break;
    case '8':
        $monn = 'H';
        break;
    case '9':
        $monn = 'I';
        break;
    case '10':
        $monn = 'J';
        break;
    case '11':
        $monn = 'K';
        break;
    case '12':
        $monn = 'L';
        break;

    default:
        break;
}

$append_file = 'inw_add.php';
$file = 'inward.php';
$grid = 'inwdmain.php';

if ($_GET["para"] == 3) {
//    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' ORDER BY srno") or die(mysqli_error($conn));
    $num = mysqli_num_rows($cmain);
    $xnum = $num - 1;
}

$ctr = 0;
$breaker = 1;
require ('library/fpdf/code128.php');
    $pdf = new PDF_Code128('P', 'mm', array(38, 48));

while ($ctmp = mysqli_fetch_object($cmain)) {
    $ctr++;
    if ($_GET["para"] == 3) {
        $num_qty = $_GET["number"];
        $itemname = $ctmp->name;
        $mrp = $ctmp->mrp;
        $barcode = $ctmp->code;
    } else {
        $num_qty = $ctmp->qty;
        $itemcode = $ctmp->itemcode;
        $citemname = mysqli_query($conn, "SELECT * FROM `item` WHERE code='$itemcode' ") or die(mysqli_error($conn));
        $fitemname = mysqli_fetch_array($citemname);
        $itemname = $fitemname[name];
        $barcode = $fitemname[code];
        $mrp = $ctmp->mrp;
    }



    for ($i = 1; $i <= $num_qty; $i++) {
//        $pdf = new FPDF('P', 'mm', array(35, 40));


        $pdf->AddPage();
        $pdf->SetXY("15", '4');
        $pdf->SetFont('Arial', 'B', 11);

        $pdf->Cell(0, 0, $barcode, 0);
        $pdf->Code128(3, 6, $barcode, 32, 6);


        $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->Cell(0, 5, $barcode, 0, 0, 'C');
//        $pdf->Ln(2);
////        $pdf->Cell(0, 5, $barcode, 0, 0, 'C');
////        $code='barcode.php?text='.$barcode;
//        $pdf->Code128(50, 20, $barcode, 80, 20);
//        $pdf->Ln(2);
//        $pdf->Cell(0, 5, $itemname, 0, 0, 'C');
//        $pdf->Ln(2);
//        $pdf->Cell(0, 5, 'BENHUR', 0, 0, 'C');
//        $pdf->Ln(2);
//        $pdf->Cell(0, 5, 'MRP : ' . $mrp, 0, 0, 'C');
    }
}


$pdf->Output();
?>