<?php

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


header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$success = false;
$maindb = "kotmain";
$trandb = "kottran";
$concat = $concat1 = $concat2 = $concat3 = "";
//if (isset($_GET["print"])) {

$getkotprint = mysqli_query($conn, "SELECT `vouchno`,`subvouch` FROM `$trandb` WHERE `kotprint`=0 and vouchno in (SELECT `vouchno` from `$maindb` where billdone = '') group by `vouchno` order by `vouchno` asc LIMIT 0,1") or die(mysqli_error($conn));
while ($fvouch = mysqli_fetch_object($getkotprint)) {
    $vouchno = $fvouch->vouchno;
    $subvouch = $fvouch->subvouch;

##############################Print Start Here##################################
    $mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d/%m/%Y')as mydt FROM $maindb WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
    $fmain = mysqli_fetch_object($mainqry);

    $tranqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d/%m/%Y')as mydt FROM $trandb WHERE vouchno='$vouchno' and kotprint=0 order by srl") or die(mysqli_error($conn));
    $totqty = $fmain->totqty;

    $compqry = mysqli_query($conn, "SELECT * FROM $zroot.company WHERE code='1'") or die(mysqli_error($conn));
    $fcomp = mysqli_fetch_object($compqry);
    $gettable = mysqli_query($conn, "SELECT name from tablemast where code = '$fmain->tableno' ")or die(mysqli_error($conn));
    $ftable = mysqli_fetch_object($gettable);


    if ($fmain->parcel == "Y") {
        $parcel = "PARCEL";
    } else {
        $inhouse = "IN HOUSE";
    }

    $concat1 .= "<div class='subpage'><br>" .
            "<table style='padding-bottom: 2px'>" .
            "<tr><td class='text-center thicker' colspan='3' style='font-size: 17px'>  $fcomp->pname</td></tr>" .
            "<tr><td class='text-center thicker' colspan='3' style='font-size: 19px'>TOKEN NO. : $fmain->vouchno ($subvouch)</td></tr>" .
            "<tr ><td class='bold text-center' style='width:45%;font-size:14px;'> $inhouse </td>" .
            "<td class='bold' style='width:15%;'><div  style='border:1px solid black; border-radius:5px;padding: 2px;font-size: 17px' class='text-center'>&nbsp;&nbsp;KOT&nbsp;&nbsp;</div></td>" .
            "<td class='bold text-center' style='width:40%;font-size:14px;'> $parcel</td>" .
            "</tr>" .
            "<tr><td colspan='3'>&nbsp;</td></tr></table>" .
            "<table>" .
            "<tr>" .
            "<td style='width:19%;' class='lpadd f15'><b> $fmain->mydt</b></td>" .
            "<td style='width:30%' class='bold'></td>" .
            "<td style='width:50%' class='f15'><b>Time $fmain->vouchtime</b></td>" .
            "</tr>" .
            "<tr>" .
            "<td class='lpadd f15'>T. No. : <b> $ftable->name </b></td>" .
            "<td class='bold'></td>" .
            "<td class='' style='font-size:19px'><b> $fmain->user </b></td>" .
            "</tr>" .
            "</table>" .
            "<table>" .
            "<tr style='border-top: 1px solid black;border-bottom: 1px solid black'>" .
            "<th class='bold lpadd f15 black' colspan='' style='width:11%'>Sr.</th>" .
            "<th class='bold lpadd f15 black' style='width:69%'>Particulars</th>" .
            "<th class='bold text-right lpadd f15 black' style='width:20%'>Qty.</th>" .
            "</tr>";
    $ctr = 0;
    while ($ftran = mysqli_fetch_object($tranqry)) {
        $itemqry = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code` = '$ftran->item'") or die(mysqli_error($conn));
        $fitem = mysqli_fetch_array($itemqry);

        $ctr++;
        $concat2 .= "<tr style=''>" .
                "<td  class='lpadd f17'> $ctr </td>" .
                "<td  class='lpadd f19' > $fitem[name] </td>" .
                "<td  class='text-right lpadd  f17'> $ftran->qty </td></tr>";
    }

    $concat3 .= "<tr style = 'border-top:1px solid black;border-bottom:1px solid black;padding-bottom: 15px'><td></td>" .
            "<td class = 'text-right bold f15' colspan = ''>Total Qty.</td>" .
            "<td class = 'text-right rpadd bold f17' >" . round($totqty) . "</td>" .
            "</tr>" .
            "</table>" .
            "<table><tr>" .
            "<th>&nbsp;</th>" .
            "</tr>" .
            "</table>" .
            "</div>";
    $concat .= $concat1 . $concat2 . $concat3;
    $concat1 = $concat2 = $concat3 = "";
}
mysqli_query($conn, "UPDATE `kottran` SET `kotprint` = 1 WHERE `vouchno` = '$vouchno'") or die(mysqli_error($conn));

//}
//echo "event: newreq\n";
//echo 'data:1' . json_encode($data);

$time = date('r');
echo "data: {$concat}\n\n";
flush();
?>