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

$maindb = "jobgiven";
//$trandb = "inwtran";
$formname = "Job Given Register";
$file = "jobgvnreg_fr.php";

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

$prodcode = $_POST["prodcode"];
$colorname = $_POST["colorname"];
$process = $_POST["process"];
$karigar = $_POST["karigar"];
$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];

$prodcond = "";
$colorcond = "";
$process = "";
$karigar = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

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


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

if (!empty($colorname)) {
    $colorcond = " AND `colorname`='$colorname' ";
}

if (!empty($process)) {
    $processcond = " AND `process`='$process' ";
}
if (!empty($karigar)) {
    $karigarcond = " AND `karigar`='$karigar' ";
}


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

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt , DATE_FORMAT(`saleorddate`, '%d-%m-%Y') as saledate,
                                    DATE_FORMAT(`expectdate`, '%d-%m-%Y') as expect FROM `jobgiven` WHERE `vouchdt` >= '$mfrmdate' 
                                    AND `vouchdt` <= '$mtodate' $prodcond $colorcond $processcond $karigarcond")or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    $get = mysqli_num_rows($getpmain);
    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; 
        }
    </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-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%">Sr</th>
                                <th style="width:8%">Date</th>
                                <th style="width:5%">Order</th>
                                <th style="width:8%">Customer</th>
                                <th style="width:8%">Sale Date</th>
                                <th style="width:10%">Product</th>
                                <th style="width:8%">Color</th>
                                <th style="width:8%">Process</th>
                                <th style="width:8%">Karigar</th>
                                <th style="width:8%">Expected</th>
                                <th style="width:5%;" class="text-right">Rate</th>
                                <th style="width:5%" class="text-right">Qty.</th>
                                <th style="width:6%" class="text-right">Amount</th>
                                <th style="width:10%; padding-left:20px;"  class="">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                $fcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$pmain[colorname]'") or die(mysqli_error($conn));
                                $lcolor = mysqli_fetch_array($fcolor);

                                $fparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
                                $lparty = mysqli_fetch_array($fparty);

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

                                $fitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$pmain[prodcode]'") or die(mysqli_error($conn));
                                $litem = mysqli_fetch_array($fitem);

                                $fprocess = mysqli_query($conn, "SELECT `code`,`name` FROM `process` WHERE `code`='$pmain[process]'") or die(mysqli_error($conn));
                                $lprocess = mysqli_fetch_array($fprocess);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[saleorder]</td>";
                                Echo "<td>$lparty[name]</td>";
                                Echo "<td>$pmain[saledate]</td>";
                                Echo "<td>$litem[name]</td>";
                                Echo "<td>$lcolor[name]</td>";
                                Echo "<td>$lprocess[name]</td>";
                                Echo "<td>$lkarigar[name]</td>";
                                Echo "<td>$pmain[expect]</td>";
                                Echo "<td class='text-right' >$pmain[rate]</td>";
                                Echo "<td class='text-right' >$pmain[qty]</td>";
                                Echo "<td class='text-right'>$pmain[billamt]</td>";
                                Echo "<td style='padding-left:20px;'>$pmain[remark] &nbsp;   &nbsp;  $pmain[remark2]</td>";

                                $mtqty = $pmain[qty] + $mtqty;
                                $mgtot = $pmain[billamt] + $mgtot;

                                Echo "</tr>";

                                $ctr2 = 0;
                                $tqty = 0;
                                $tamt = 0;
                            }

                            echo "<tr> ";
                            echo "<td colspan='9'></td>";
                            echo "<th colspan='2' class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<th class='text-right' style='color: red;'>$mtqty</td>";
                            echo "<th class='text-right' style='color: red;'>$mgtot</td>";
                            echo "<td></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $get = mysqli_num_rows($getpmain);
    $hd = '';
    if ($spltype != '') {
        $hd = 'Series,';
    }

    $contents .= "Sr.,$hd Voucher no.,Date,Qty,Total,Remark\n";

    $ctr = 0;
    $mtqty = 0;
    $mgtot = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $ctr++;
        $pvouchno = $pmain['vouchno'];
        $pdate = $pmain['vouchdt'];
        $pqty = $pmain['qty'];
        $ptotal = $pmain['total'];
        $premark = $pmain['remark'] . $pmain['remark2'];

        $contents .= $ctr . ",";
        if ($spltype != '1') {

            $contents .= $pmain['series'] . ",";
        }
        $contents .= $pvouchno . ",";
        $contents .= $pdate . ",";
        $contents .= $pqty . ",";
        $contents .= $ptotal . ",";
        $contents .= $premark . "\n";

        $mtqty = $pmain[qty] + $mtqty;
        $mgtot = $pmain[total] + $mgtot;

        $contents .= "Sr.,Barcode,Itemname,Qty,Unit,Mrp,Rate,Amount,Msp\n";

        $ctr2 = 0;
        $tqty = 0;
        $tamt = 0;

        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' and series = '$pmain[series]' and series = '$pmain[series]' $itemcond $itemnamecond  ")or die(mysqli_error($conn));
        while ($tran = mysqli_fetch_array($gettran)) {

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

            $ctr2++;
            $pitemcode = $tran['itemcode'];
            $pitemname = $item['name'];
            $ptrqty = $tran['trqty'];
            $punit = $tran['unit'];
            $pmrp = $tran['mrp'];
            $prate = $tran['rate'];
            $pamount = $tran['amount'];
            $pmsp = $tran['msp'];

            $contents .= $ctr2 . ",";
            $contents .= $pitemcode . ",";
            $contents .= $pitemname . ",";
            $contents .= $ptrqty . ",";
            $contents .= $punit . ",";
            $contents .= $pmrp . ",";
            $contents .= $prate . ",";
            $contents .= $pamount . ",";
            $contents .= $pmsp . "\n";
        }
        $tqty = $tran['trqty'] + $tqty;
        $tamt = $tran['amount'] + $tamt;

        $contents .= " " . ",";
        $contents .= " " . ",";

        $contents .= "Total" . ",";
        $contents .= "$tqty" . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "$tamt" . ",";
        $contents .= " " . "\n";
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    if ($spltype != '1') {

        $contents .= " " . ",";
    }
    $contents .= "Grand Total" . ",";
    $contents .= "$mtqty" . ",";
    $contents .= "$mgtot" . ",";
    $contents .= " " . "\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;
    }
}
?>