<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.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) {
    $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(55, 43));
$pdf = new PDF_Code128('P', 'mm', array(50, 43));


while ($ctmp = mysqli_fetch_object($cmain)) {
    $ctr++;
    if ($_GET["para"] == 3) {
        $num_qty = $_GET["number"];
        $itemname = $ctmp->name;
        $mrp = $ctmp->mrp;
        $barcode = $ctmp->code;
        $rate = $ctmp->rate;
    } 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];
        $rate = $fitemname[rate];
        $mrp = $ctmp->mrp;
    }


    $newrate = substr($rate, 0, -3);

    $convmsp = (string) $newrate;
    $clen = strlen($convmsp);

    $dcode = "";
    for ($i = 0; $i < $clen; $i++) {
        $c1 = substr($convmsp, $i, 1);
        $getmsp = mysqli_query($conn,"Select * from $zroot.msp where no = '$c1'") or die(mysqli_error($conn));
        $fetmsp = mysqli_fetch_array($getmsp);
        $codmsp = $fetmsp[code];
        $dcode = $dcode . $codmsp;
    }
    $x = 11 ;
    for ($i = 1; $i <= $num_qty; $i++) {
        
        $pdf->AddPage();

        $pdf->SetXY($x,2);
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell(32, 4, $barcode, 0, 0, 'C');

        $pdf->SetXY($x,5);
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell(32, 4, $itemname, 0, 0, 'C');
        
        $pdf->SetXY($x,14);
        $pdf->Code128($x, 9, $barcode, 32, 6);


        $pdf->SetXY($x,15);
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell(32, 4, $dcode, 0, 0, 'C');
        
        
        $mrpd = 'www.zuby.online' ;          
        $pdf->SetXY($x,18);
        $pdf->SetFont('Arial', 'B', 9);
        $pdf->Cell(32, 4, $mrpd , 0, 0, 'C');

    }
}


$pdf->Output();
?>