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

$maindb = "purrawmain";
$trandb = "purrawtran";
$formname = "Purchase Raw Material Register";
$file = "purchrawreg.php";

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

$invno = $_POST["invno"];
$party = $_POST["party"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$printnar = $_POST["prna"];

$dtypecond = "";
$mscopecond = "";
$itemcond = "";
$gstcond = "";
$prodtypecond = "";

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

if (!empty($party)) {
    $partycond = " AND `party`='$party' ";
}
//    browse($partycond);
if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}

if (!empty($scope)) {
    $mscopecond = " AND `type`='$scope' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close();</script>";
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` where
                                        `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond 
                                        order by vouchno") or die(mysqli_error($conn));

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: #cccccc;
        }
    </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%'>Invoice</th>
                                <th width='12%'>Date</th>
                                <th width='19%'>Supplier</th>
                                <th width='5%' class="text-right">Amount</th>
                                <th width='5%' class="text-right">Discount</th>
                                <th width='5%' class="text-right">Transport/Packing</th>
                                <th width='5%' class="text-right">GT</th>
                                <th width='11%' class="text-right">Bill.Amt</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;
                            $totcouamt = 0;
                            $totgstamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);
                                $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'") or die(mysqli_error($conn));
                                $sale = mysqli_fetch_array($getsale);

                                $ctr++;

                                $url = "purchmat.php?vouchno=$pmain[vouchno]&mode=edit";
                                echo "<tr id='boldfont' class='clickable-row2' data-href='$url' data-target='_blank'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[invno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$supp[name]</td>";
                                echo "<td class='text-right'>$pmain[subtotal]</td>";
                                echo "<td class='text-right'>$pmain[lesother]</td>";
                                echo "<td class='text-right'>$pmain[couamt]</td>";
                                echo "<td class='text-right'>$pmain[gstamt]</td>";
                                echo "<td class='text-right'>$pmain[netamt]</td>";
                                echo "</tr>";

                                $ttotamt += $pmain['subtotal'];
                                $totitmpr += $pmain['lesother'];
                                $totcouamt += $pmain['couamt'];
                                $totgstamt += $pmain['gstamt'];

                                $totbill += $pmain['netamt'];

                                if ($_POST["dtype"] == 2 || $_POST["dtype"] == 3) {

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    echo "<th width='5%;'class='dtype'>Sr</th>";
                                    echo "<th width='8%' class='dtype'>Group</th>";
                                    echo "<th width='8%' class='dtype'>Material</th>";
                                    echo "<th width='8%' class='dtype'>Color</th>";
                                    echo "<th width='6%' class='dtype'>Lot</th>";
                                    echo "<th width='6%' class='numeric dtype'>Qty</th>";
                                    echo "<th width='6%' class='numeric dtype'>Rate</th>";
                                    echo "<th width='6%' class='numeric dtype'>Amount</th>";
                                    echo "<tr>";

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


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

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

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

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


                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$itemgrp[name]</td>";
                                        echo "<td>$item[name]</td>";
                                        echo "<td>$color[name]</td>";
                                        echo "<td>$tran[lot]</td>";
                                        echo "<td class='numeric'>$tran[qty]</td>";
                                        echo "<td class='numeric'>$tran[rate]</td>";
                                        echo "<td class='numeric'>$tran[amount]</td>";
                                        echo "</tr>";

                                        $totamt = $totamt + $tran['amount'];
                                        $tottaxable = $tottaxable + $tran['subtot'];
                                        $totyard = $totyard + $tran['yard'];
                                        $totmtrs = $totmtrs + $tran['meters'];
                                        $totqty = $totqty + $tran['qty'];
                                    }

                                    $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' id='font1' class='text-right'><h5><b></b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>Total</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totqty</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b></b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totamt</b></h5></td>";
                                    echo "</tr>";

                                    echo "</table>";
                                    echo "</td>";
                                }

                                if ($_POST["dtype"] == 3) {
                                    $gettran = mysqli_query($conn, "SELECT * FROM `prodmake` WHERE `vouchno`='$pmain[vouchno]' ") or die(mysqli_error($conn));
                                    if (mysqli_num_rows($gettran) > 0) {
                                        echo "<tr style='height:10px;'>";
                                        echo "<td colspan='25'>";
                                        echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        echo "<h4>Product Making</h4>";
                                        echo "<th width='5%;' style='background-color:#0a8bfa' class='dtype'>Sr</th>";
                                        echo "<th width='8%'  style='background-color:#0a8bfa' class='dtype'>Item</th>";
                                        echo "<th width='8%'  style='background-color:#0a8bfa' class='dtype'>Color</th>";
                                        echo "<th width='6%'  style='background-color:#0a8bfa' class='numeric dtype'>Qty</th>";
                                        echo "<th width='6%'  style='background-color:#0a8bfa' class='numeric dtype'>Rate</th>";
                                        echo "<tr>";

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

                                        while ($tran = mysqli_fetch_array($gettran)) {

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

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


                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$item[name]</td>";
                                            echo "<td>$color[name]</td>";
                                            echo "<td class='numeric'>$tran[qty]</td>";
                                            echo "<td class='numeric'>$tran[rate]</td>";
                                            echo "</tr>";
                                        }

                                        echo "</table>";
                                        echo "</td>";
                                    }
                                }
                            }

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";

                            echo "<td align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttotamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totitmpr</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totcouamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totgstamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totbill</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
    <?php
}


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

    $contents .= "Sr,Inv.No,Date,Supplier,Amount,Discount, Transport/Packing, GT, Bill.Amt\n";

    $ctr = 0;
    $ttotamt = 0;
    $totpack = 0;
    $totbill = 0;
    $totitmpr = 0;
    $totdisc = 0;
    $tottaxamt = 0;
    $totcgst = 0;
    $totsgst = 0;
    $totigst = 0;
    $totcouamt = 0;
    $totgstamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
        $supp = mysqli_fetch_array($getsupp);

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['invno'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $supp['name'] . ",";
        $contents .= $pmain['subtotal'] . ",";
        $contents .= $pmain['lesother'] . ",";
        $contents .= $pmain['couamt'] . ",";
        $contents .= $pmain['gstamt'] . ",";
        $contents .= $pmain['netamt'] . "\n";


        $ttotamt += $pmain['subtotal'];
        $totitmpr += $pmain['lesother'];
        $totcouamt += $pmain['couamt'];
        $totgstamt += $pmain['gstamt'];
        $totbill += $pmain['netamt'];

        if ($_POST["dtype"] == 2 || $_POST["dtype"] == 3) {

            $contents .= "Sr, Group, Material, Color, Lot, Qty, Rate, Amount\n";


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


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

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

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

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

                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $itemgrp['name'] . ",";
                $contents .= $item['name'] . ",";
                $contents .= $color['name'] . ",";
                $contents .= $tran['lot'] . ",";
                $contents .= $tran['qty'] . ",";
                $contents .= $tran['rate'] . ",";
                $contents .= $tran['amount'] . "\n";

                $totyard = $totyard + $tran['yard'];
                $totmeters = $totmeters + $tran['meters'];
                $totamt = $totamt + $tran['amount'];
                $totqty = $totqty + $tran['qty'];
            }


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

        if ($_POST["dtype"] == 3) {

            $gettran = mysqli_query($conn, "SELECT * FROM `prodmake` WHERE `vouchno`='$pmain[vouchno]'") or die(mysqli_error($conn));
            if (mysqli_num_rows($gettran) > 0) {

                $contents .= "Sr,Item,Color,Qty,Rate\n";

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

                while ($tran = mysqli_fetch_array($gettran)) {

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

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

                    $ctr2++;
                    $contents .= $ctr2 . ",";
                    $contents .= $item['name'] . ",";
                    $contents .= $color['name'] . ",";
                    $contents .= $tran['rate'] . ",";
                    $contents .= $tran['qty'] . "\n";
                }
            }
        }

    }

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= 'Grand Total' . ",";
    $contents .= $ttotamt . ",";
    $contents .= $totitmpr . ",";
    $contents .= $totcouamt . ",";
    $contents .= $totgstamt . ",";
    $contents .= $totbill . "\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;
    }
}
?>