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

$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');

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;
    }
/////////////////////////////


//    require("library/fpdf/fpdf.php");


    $pdf = new PDF('P', 'mm', '35,40');
    $bar = code128($barcode);

    for ($i = 1; $i <= $num_qty; $i++) {

        $pdf->SetMargins(6, 6, 6);
        $pdf->AddPage();

        $pdf->SetFont('Times', 'B', 10);
        $pdf->Cell(32, 6, $barcode, 0, 0, 'C');

        $pdf->ln();
        $pdf->SetFont('Times', 'B', 10);
        $pdf->Cell(32, 6, $bar, 0, 0, 'C');

        $pdf->ln();
        $pdf->SetFont('Times', 'B', 10);
        $pdf->Cell(32, 6, $itemname, 0, 0, 'C');
    }
}

$pdf->Output();

?>
   