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

$maindb = "repgivmain";
$trandb = "repgivtran";
$formname = "Repair Register";
$file = "repaireg.php";


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

$para = $_POST["para"];


if ($para == 'G') {
    $maindb = "repgivmain";
    $trandb = "repgivtran";
    $formname = "Repair Given Register";
} else {
    $maindb = "reptakmain";
    $trandb = "reptaktran";
    $formname = "Repair Recd Register";
}
if (isset($_POST["view"])) {
    $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");
        header("refresh:1; slgstreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; slgstreg.php");
    }

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

    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='3%'>Sr</th>
                                <th width='3%'>Voucher</th>
                                <th width='10%'>Date</th>
                                <th width='19%'>Karigar</th>
                                <th width='5%' class="text-right">SubTotal</th>
                                <th width='10%' class="text-right">Round Off</th>
                                <th width='10%' class="text-right">Less Other</th>
                                <th width='5%' class="text-right">Amount</th>
                                <th width='10%' class="text-right">Bank Name</th>
                                <th width='5%' class="text-right">Cash </th>
                                <th width='5%' class="text-right">Card</th>
                                <th width='5%' class="text-right">Credit</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $ttotamt = 0;
                            $totsub = 0;
                            $totround = 0;
                            $totamt = 0;
                            $totcash = 0;
                            $totcard = 0;
                            $totcredit = 0;
                            $totles = 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);
                                $bankqry = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[bankname]'")or die(mysqli_error($conn));
                                $bank = mysqli_fetch_array($bankqry);
                                $bankname = $bank['name'];
                                $ctr++;


                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
//                                Echo "<td>$sale[name]</td>";
                                Echo "<td class='text-right'>$pmain[subtotal]</td>";
                                Echo "<td class='text-right'>$pmain[roundoff]</td>";
                                Echo "<td class='text-right'>$pmain[lesother]</td>";
                                Echo "<td class='text-right'>$pmain[netamt]</td>";
                                Echo "<td class='text-right'>$bankname</td>";
                                Echo "<td class='text-right'>$pmain[cashamt]</td>";
                                Echo "<td class='text-right'>$pmain[ccamt]</td>";
                                Echo "<td class='text-right'>$pmain[credamt]</td>";
                                Echo "</tr>";

                                $totsub    = $ttotamt   + $pmain['subtotal'];
                                $totround  = $totround  + $pmain['roundoff'];
                                $totnetamt = $totnetamt + $pmain['netamt'];
                                $totcash   = $totcash   + $pmain['cashamt'];
                                $totcard   = $totcard   + $pmain['card'];
                                $totcredit = $totcredit + $pmain['credamt'];
                                $totles    = $totles    + $pmain['lesother'];

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

                                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='3%;' style='background-color:#0a8bfa' class='dtype'>Sr</th>";
                                    Echo "<th width='3%'  style='background-color:#0a8bfa' class='dtype'>Barcode</th>";
                                    Echo "<th width='15%'  style='background-color:#0a8bfa' class='dtype'>Item</th>";
                                    Echo "<th width='6%'  style='background-color:#0a8bfa' class='numeric 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 "<th width='6%'  style='background-color:#0a8bfa' class='numeric dtype'>Amount</th>";

                                    Echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = $totrate = $totalamt = 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 `item` WHERE `code`='$tran[itemcode]'")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'];
                                        $tottaxable = $tottaxable + $tran['subtot'];
                                        $totyard    = $totyard    + $tran['yard'];
                                        $totmtrs    = $totmtrs    + $tran['meters'];
                                        $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' class='text-right'><h5><b>$totrate</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totalamt</b></h5></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>$totsub</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totround</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totles</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totnetamt</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>$totcash</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totcard</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totcredit</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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


if (isset($_POST["excel"])) {
    $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");
        header("refresh:1; slgstreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; slgstreg.php");
    }

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

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    
    
    $contents .= "Sr., Voucher, Date, Karigar, Subtotal, Round Off, Less Other, Amount, Bank Name, Cash, Card, Credit \n ";

    $ctr = 0;
                            $ttotamt = 0;
                            $totsub = 0;
                            $totround = 0;
                            $totamt = 0;
                            $totcash = 0;
                            $totcard = 0;
                            $totcredit = 0;
                            $totles = 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);
                                $bankqry = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[bankname]'")or die(mysqli_error($conn));
                                $bank = mysqli_fetch_array($bankqry);
                                $bankname = $bank['name'];
                                $ctr++;

                                $contents .= "$ctr" . ",";
                                $contents .= "$pmain[vouchno]" . ",";
                                $contents .= "$pmain[mydt]" . ",";
                                $contents .= "$supp[name]" . ",";
                                $contents .= "$pmain[subtotal]" . ",";
                                $contents .= "$pmain[roundoff]" . ",";
                                $contents .= "$pmain[lesother]" . ",";
                                $contents .= "$pmain[netamt]" . ",";
                                $contents .= "$bankname" . ",";
                                $contents .= "$pmain[cashamt]" . ",";
                                $contents .= "$pmain[ccamt]" . ",";
                                $contents .= "$pmain[credamt]" . "\n";

                                $totsub    = $ttotamt   + $pmain['subtotal'];
                                $totround  = $totround  + $pmain['roundoff'];
                                $totnetamt = $totnetamt + $pmain['netamt'];
                                $totcash   = $totcash   + $pmain['cashamt'];
                                $totcard   = $totcard   + $pmain['card'];
                                $totcredit = $totcredit + $pmain['credamt'];
                                $totles    = $totles    + $pmain['lesother'];

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

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


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

                                    $ctr2 = 0;
                                    $totqty = $totrate = $totalamt = 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 `item` WHERE `code`='$tran[itemcode]'")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'];
                                        $tottaxable = $tottaxable + $tran['subtot'];
                                        $totyard    = $totyard    + $tran['yard'];
                                        $totmtrs    = $totmtrs    + $tran['meters'];
                                        $totqty     = $totqty     + $tran['qty'];
                                    }
                                    
                                    $contents .= "" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "Total" . ",";
                                    $contents .= "$totqty" . ",";
                                    $contents .= "$totrate" . ",";
                                    $contents .= "$totalamt" . ",";
                                }
                            }

 
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Grand Total" . ",";
                            $contents .= "$totsub" . ",";
                            $contents .= "$totround" . ",";
                            $contents .= "$totles" . ",";
                            $contents .= "$totnetamt" . ",";
                            $contents .= "" . ",";
                            $contents .= "$totcash" . ",";
                            $contents .= "$totcard" . ",";
                            $contents .= "$totcredit" . "\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;
    
    }
    }
?>
