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

//$maindb = "sordmain";
//$trandb = "sordtran";
$stkdb = "jobsacn";

$formname = "Pending Job Register";
$file = "pendjob_fr.php";

$zroot = $_SESSION["zdir"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$party = $_POST["party"];
$saleorder = $_POST["saleorder"];
$product = $_POST["product"];
$process = $_POST["process"];
$color = $_POST['color'];
$dtype = $_POST['dtype'];    //  1 - summary  - ---2 - detail

$srcond = "";
$branch = trim($_POST["series"]);
$fseries = $branch ;

$spltype = $_SESSION["spltype_" . $dbname];

if ($spltype != 1) {
    if (!empty($branch)) {
        $srcond = "and `series`='$branch'";
    }
}
$partycond = "";
$prodcond = "";
$proccond = "";
$colrcond = "";
$salordcond = "";

$mfrmdate = $_POST["fdate"];
$mtodate = $_POST["tdate"];

if (!empty($karigar)) {
    $partycond = " AND `karigar`='$karigar' ";
}
if (!empty($saleorder)) {
    $salordcond = " AND `ordno`='$saleorder' ";
}

if (!empty($process)) {
    $proccond = " AND `process`='$process' ";
}

if (!empty($product)) {
    $prodcond = " AND `barcode`='$product' ";
}

if (!empty($color)) {
    $colrcond = " AND `color`='$color' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; pendjob.php");
}

if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; pendjob.php");
}

//browse($mfrmdate);
//browse($mtodate);
//browse($partycond);
//browse($prodcond);
//browse($colrcond);
//$qry = mysqli_query($conn, "select saleorder from jobgiven where vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' $partycond  ") or die(mysqli_error($conn));

$qry = mysqli_query($conn, "SELECT ordno, barcode, itemcode, color, karigar, process, sum(qty) as sqty from $stkdb where series = '$fseries' 
                                   $prodcond $colrcond $salordcond and 
                                   concat(series, ordno) in (select concat(series, saleorder) from jobgiven where vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate' $partycond )
                                   group by series, ordno, itemcode, color having sum(qty) > 0 order by ordno ") or die(mysqli_error($conn));

//print table($qry);
//die();
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .dtype{
            background-color: #C89595;
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='5%'>Sale Order</th>
                                <th width='12%'>Date</th>
                                <th width='6%' class='dtype'>Karigar</th>
                                <th width='6%' class='dtype'>Process</th>
                                <th width='6%' class='dtype'>Barcode</th>
                                <th width='6%' class='dtype'>Product</th>
                                <th width='6%' class='dtype'>Color</th>
                                <th class="text-right" width='10%'>Qty</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;

                            $ctr2 = 0;
                            $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                            while ($tran = mysqli_fetch_array($qry)) {
                                $ordno = $tran['ordno'];
                                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);

                                $getdt = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `sordmain` WHERE `vouchno`= '$ordno' ") or die(mysqli_error($conn));
                                $fetdt = mysqli_fetch_array($getdt);

                                $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'")or die(mysqli_error($conn));
                                $color = mysqli_fetch_array($getcolor);

                                $getsupp = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$tran[karigar]' ")or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);

                                $getproc = mysqli_query($conn, "SELECT `name` FROM `process` WHERE `code`='$tran[process]'")or die(mysqli_error($conn));
                                $proc = mysqli_fetch_array($getproc);


                                $ctr2++;
                                Echo "<tr>";
                                Echo "<td>$ctr2</td>";
                                Echo "<td class=''>$tran[ordno]</td>";
                                Echo "<td class=''>$fetdt[mydt]</td>";
                                Echo "<td class=''>$supp[name]</td>";
                                Echo "<td class=''>$proc[name]</td>";
                                Echo "<td class=''>$tran[barcode]</td>";
                                Echo "<td class=''>$item[name]</td>";
                                Echo "<td class=''>$color[name]</td>";
                                Echo "<td class='numeric'>$tran[sqty]</td>";
                                Echo "</tr>";
                                $totqty = $totqty + $tran['sqty'];

                                $tqty = number_format($totqty, 2);
                            }
                            $colr1 = "blue";
                            echo "<tr> ";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1'</td>";
                            echo "<td style='color:$colr1' ></td>";
                            echo "<td style='color:$colr1' ></td>";
                            echo "<td style='color:$colr1'><b>Total</b></td>";
                            echo "<td style='color:$colr1'class='text-right'><b>$tqty</b></td>";
                            echo "</tr>";

                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}


if (isset($_POST["excel"])) {
    

                            $contents .= "Sr,Sale Order,Date,Karigar,Process,Barcode,Product,Color,Qty\n";
                           
                            $ctr = 0;
                            $ttotamt = 0;
                            $totpack = 0;
                            $totbill = 0;
                            $totitmpr = 0;
                            $totdisc = 0;
                            $tottaxamt = 0;
                            $totcgst = 0;
                            $totsgst = 0;
                            $totigst = 0;
                            $totroundoff = 0;

                            $ctr2 = 0;
                            $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                            while ($tran = mysqli_fetch_array($qry)) {
                                $ordno = $tran['ordno'];
                                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$tran[itemcode]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);

                                $getdt = mysqli_query($conn, "SELECT DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `sordmain` WHERE `vouchno`= '$ordno' ") or die(mysqli_error($conn));
                                $fetdt = mysqli_fetch_array($getdt);

                                $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'")or die(mysqli_error($conn));
                                $color = mysqli_fetch_array($getcolor);

                                $getsupp = mysqli_query($conn, "SELECT `name` FROM `accmast` WHERE `accode`='$tran[karigar]' ")or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);

                                $getproc = mysqli_query($conn, "SELECT `name` FROM `process` WHERE `code`='$tran[process]'")or die(mysqli_error($conn));
                                $proc = mysqli_fetch_array($getproc);


                                $ctr2++;
                                
                                $contents .= "$ctr2" . ",";
                                $contents .= "$tran[ordno]" . ",";
                                $contents .= "$fetdt[mydt]" . ",";
                                $contents .= "$supp[name]" . ",";
                                $contents .= "$proc[name]" . ",";
                                $contents .= "$tran[barcode]" . ",";
                                $contents .= "$item[name]" . ",";
                                $contents .= "$color[name]" . ",";
                                $contents .= "$tran[sqty]" . "\n";
                            
                                $totqty = $totqty + $tran['sqty'];

                            }
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Total" . ",";
                            $contents .= "$totqty" . "\n";
                        

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>