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

$maindb = "quotmain";
$trandb = "quottran";
$formname = "Quotation Register";
$file = "quotationreg.php";

//browse('Work In Progress');
$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"];
$item = $_POST["item"];
$prodtype = $_POST["prodtype"];
$dtype = $_POST["dtype"];
$scope = $_POST["scope"];
$printnar = $_POST["prna"];
$gstperc = $_POST["gstperc"];
$city = $_POST['city'];
$state = $_POST['state'];
$broadcast = $_POST['broadcast'];

$srcond = "";
$branch = trim($_POST["series"]);
$spltype = $_SESSION["spltype_" . $dbname];
if ($spltype != 1) {
    if (!empty($branch)) {
        $srcond = "and `series`='$branch'";
    }
}

$dtypecond = "";
$mscopecond = "";
$itemcond = "";
$gstcond = "";
$prodtypecond = "";
$citycond = "";
$statecond = "";
$broadcastcond = "";

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


if (!empty($party)) {
    $partycond = " AND a.`party`='$party' ";
}

if (!empty($broadcast)) {
    $broadcastcond = " AND b.`website` LIKE '%$broadcast%' ";
}
if (!empty($state)) {
    $statecond = " AND b.`state` = '$state' ";
}
if (!empty($city)) {
    $citycond = " AND b.`city` LIKE '%$city%' ";
}

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

$getpmain = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.name FROM `$maindb` a, accmast b where
                           a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' and a.party = b.accode $srcond $partycond  $itemcond 
                            $salemcond $prodtypecond $gstcond $broadcastcond $statecond $citycond")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-fluid">
            <?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>
                                <?php if ($spltype != '1') { ?>
                                    <th width='5%'>Branch</th>
                                <?php } ?>
                                <th width='5%'>Quotation</th>
                                <th width='6%'>Date</th>
                                <!--<th width='5%'>Invoice</th>-->
                                <th width='30%'>Customer</th>
                                <!--<th width='10%'>Salesman</th>-->
                                <!--<th width='5%' class="text-right">Transporter</th>-->
<!--                                <th width='5%' class="text-right">LR No.</th>
                                <th width='5%' class="text-right">LR Date</th>
                                <th width='5%' class="text-right">Salesman</th>
                                <th width='5%' class="text-right">No Of Packages</th>
                                <th width='5%' class="text-right">Packed By</th>-->
                                <th width='5%' class="text-right">Total Qty.</th>
                                <th width='5%' class="text-right">Subtotal</th>
                                <th width='8%' class="text-right">Less Other</th>
                                <th width='5%' class="text-right">Total Amount</th>
                                <th width='5%' class="text-right">Add Packing</th>
                                <th width='5%' class="text-right">TR Paid</th>
                                <!--<th width='5%' class="text-right">GST</th>-->
                                <th width='5%' class="text-right">Round Off</th>
                                <th width='5%' class="text-right">Net Amt.(Rs)</th>
<!--                                <th width='5%' class="text-right">Cash </th>
                                <th width='5%' class="text-right">Bank Name</th>
                                <th width='5%' class="text-right">CC</th>
                                <th width='5%' class="text-right">Credit</th>-->
                                <th width='5%' class="text-left">Remarks</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;


                            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);

                                $gtran = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`!='' ORDER by `name`")or die(mysqli_error($conn));
                                $trans = mysqli_fetch_array($gtran);
                                $transporter = $trans[name];

                                $gsales = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='040'")or die(mysqli_error($conn));
                                $sales = mysqli_fetch_array($gsales);
                                $salesman = $sales[name];

                                $qpackby = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode` = '040' order by name")or die(mysqli_error($conn));
                                $fpackby = mysqli_fetch_array($qpackby);
                                $packed = $fpackby[name];

                                $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[bankname]'");
                                $banknm = mysqli_fetch_array($getbank);
                                $gbank = $banknm[name];

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                if ($spltype != '1') {
                                    Echo "<td>$pmain[series]</td>";
                                }
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
//                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[name]</td>";
//                                Echo "<td>$transporter</td>";
//                                Echo "<td class='text-right'>$pmain[lrno]</td>";
//                                Echo "<td class='text-right'>$pmain[lrdate]</td>";
//                                Echo "<td>$salesman</td>";
//                                Echo "<td class='text-right'>$pmain[nopkgs]</td>";
//                                Echo "<td class='text-right'>$packed</td>";
                                Echo "<td class='text-right'>$pmain[totqty]</td>";
                                Echo "<td class='text-right'>$pmain[subtotal]</td>";
                                Echo "<td class='text-right'>$pmain[lesother]</td>";
                                Echo "<td class='text-right'>$pmain[totalamount]</td>";
                                Echo "<td class='text-right'>$pmain[packamt]</td>";
                                Echo "<td class='text-right'>$pmain[paidamt]</td>";
//                                Echo "<td class='text-right'>$pmain[gstamt]</td>";
                                Echo "<td class='text-right'>$pmain[roundoff]</td>";
                                Echo "<td class='text-right'>$pmain[netamt]</td>";
//                                Echo "<td class='text-right'>$pmain[cashamt]</td>";
//                                Echo "<td class='text-right'>$gbank</td>";
//                                Echo "<td class='text-right'>$pmain[ccamt]</td>";
//                                Echo "<td class='text-right'>$pmain[credamt]</td>";
                                Echo "<td class='text-right'>$pmain[remark1]</td>";
                                Echo "</tr>";

                                $ttotqty = $ttotqty + $pmain['totqty'];
                                $ttotamt = $ttotamt + $pmain['subtotal'];
                                $totitmpr = $totitmpr + $pmain['lesother'];
                                $tottaxamt = $tottaxamt + $pmain['totalamount'];
                                $totpack = $totpack + $pmain['packamt'];
                                $tpaidamt = $tpaidamt + $pmain['paidamt'];
                                $tgstamt = $tgstamt + $pmain['gstamt'];
                                $totroundoff = $totroundoff + $pmain['roundoff'];
                                $totbill = $totbill + $pmain['netamt'];
                                $tcashamt = $tcashamt + $pmain['cashamt'];
                                $tccamt = $tccamt + $pmain['ccamt'];
                                $tcredamt = $tcredamt + $pmain['credamt'];

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

                                    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''>Barcode</th>";
                                    Echo "<th width='8%' class='dtype'>Itemname</th>";
                                    Echo "<th width='8%' class='dtype'>Color</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;
                                    $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;


                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' and series = '$pmain[series]'")or die(mysqli_error($conn));
                                    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>$tran[barcode]</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 "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $totamt = $totamt + $tran['amount'];
                                        $totqty = $totqty + $tran['qty'];
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";

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

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

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            if ($spltype != '1') {

                                echo "<td style='color:$colr1'</td>";
                            }
                            echo "<td colspan='2'></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>$ttotqty</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>$tottaxamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totpack</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tpaidamt</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tgstamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totroundoff</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totbill</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tcashamt</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tccamt</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$tcredamt</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b></b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totroundoff</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totpack</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,";
    if ($spltype != '1') {
        $contents .= "Branch,";
    }
    $contents .= "Voucher, Date, Invoice, Customer, Salesman, Transporter, LR No., LR Date, Salesman, No. of Packages, Packed By"
            . "Total Qty., Subtotal, Less Other, Total Amount, Add Packing, TR Paid, Round Off, Net Amt., Cash, Bank Name, CC,"
            . "Credit, Remarks \n";

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


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

        $gtran = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`!='' ORDER by `name`")or die(mysqli_error($conn));
        $trans = mysqli_fetch_array($gtran);
        $transporter = $trans[name];

        $gsales = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='040'")or die(mysqli_error($conn));
        $sales = mysqli_fetch_array($gsales);
        $salesman = $sales[name];

        $qpackby = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode` = '040' order by name")or die(mysqli_error($conn));
        $fpackby = mysqli_fetch_array($qpackby);
        $packed = $fpackby[name];

        $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$get[bankname]'");
        $banknm = mysqli_fetch_array($getbank);
        $gbank = $banknm[name];

        $ctr++;

        $contents .= "$ctr" . ",";
        if ($spltype != '1') {
            $contents .= "$pmain[series]" . ",";
        }
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$transporter" . ",";
        $contents .= "$pmain[lrno]" . ",";
        $contents .= "$pmain[lrdate]" . ",";
        $contents .= "$salesman" . ",";
        $contents .= "$pmain[nopkgs]" . ",";
        $contents .= "$packed" . ",";
        $contents .= "$pmain[totqty]" . ",";
        $contents .= "$pmain[subtotal]" . ",";
        $contents .= "$pmain[lesother]" . ",";
        $contents .= "$pmain[totalamount]" . ",";
        $contents .= "$pmain[packamt]" . ",";
        $contents .= "$pmain[paidamt]" . ",";
        $contents .= "$pmain[roundoff]" . ",";
        $contents .= "$pmain[netamt]" . ",";
        $contents .= "$pmain[cashamt]" . ",";
        $contents .= "$gbank" . ",";
        $contents .= "$pmain[ccamt]" . ",";
        $contents .= "$pmain[credamt]" . ",";
        $contents .= "$pmain[remark1]" . "\n";

        $ttotqty = $ttotqty + $pmain['totqty'];
        $ttotamt = $ttotamt + $pmain['subtotal'];
        $totitmpr = $totitmpr + $pmain['lesother'];
        $tottaxamt = $tottaxamt + $pmain['totalamount'];
        $totpack = $totpack + $pmain['packamt'];
        $tpaidamt = $tpaidamt + $pmain['paidamt'];
        $totroundoff = $totroundoff + $pmain['roundoff'];
        $totbill = $totbill + $pmain['netamt'];
        $tcashamt = $tcashamt + $pmain['cashamt'];
        $tccamt = $tccamt + $pmain['ccamt'];
        $tcredamt = $tcredamt + $pmain['credamt'];

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



            $contents .= "Sr., Barcode, Itemname, Color, Qty., Rate, Amount\n";

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


            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' and series = '$pmain[series]'")or die(mysqli_error($conn));
            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 .= "$tran[barcode]" . ",";
                $contents .= "$item[name]" . ",";
                $contents .= "$color[name]" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[amount]" . "\n";

                $totamt = $totamt + $tran['amount'];
                $totqty = $totqty + $tran['qty'];
            }
            echo "<tr> ";
            $colr1 = "blue";

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

    $colr = "green";

    $contents .= "" . ",,,,,,,,,";
    if ($spltype != '1') {
        $contents .= "" . ",";
    }
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$ttotqty" . ",";
    $contents .= "$ttotamt" . ",";
    $contents .= "$totitmpr" . ",";
    $contents .= "$tottaxamt" . ",";
    $contents .= "$totpack" . ",";
    $contents .= "$tpaidamt" . ",";
    $contents .= "$totroundoff" . ",";
    $contents .= "$totbill" . ",";
    $contents .= "$tcashamt" . ",";
    $contents .= "" . ",";
    $contents .= "$tccamt" . ",";
    $contents .= "$tcredamt" . ",";
    $contents .= "" . ",";


    $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;
    }
}
?>