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

$maindb = 'credmain';
$trandb = 'credoth';
$formname = "Sale Returns / Credit Note(Others) Register";
$file = "ticredothreg_fr.php";
//$keycond = "AND `credtype`='G'";


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

if (isset($_POST["view"])) {
    $party = $_POST["party"];
    $godown = $_POST["godown"];
    $oms = $_POST["oms"];
    $dtype = $_POST["dtype"];

    $dtypecond = "";
    $mscopecond = "";

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

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

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

    if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }


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

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

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' and `credtype` = 'O' $partycond $gstcond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
    ?>
    <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-8 col-md-8 col-sm-8 col-xs-8">
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed ">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr</th>
                                <th style="width:8%">Voucher No.</th>
                                <th style="width:6%">Invoice</th>
                                <th style="width:7%">Date</th>
                                <th style="width:17%">Party</th>
                                <th style="width:6%">Godown</th>
                                <th style="width:5%">GST%</th>
                                <th style="width:6%" class="text-right">Subtotal</th>
                                <th style="width:7%"  class="text-right">Discount</th>
                                <th style="width:8%"  class="text-right">Taxable Amount</th>
                                <th style="width:6%" class="text-right">CGST  </th>
                                <th style="width:6%" class="text-right">SGST  </th>
                                <th style="width:6%" class="text-right">IGST  </th>
                                <th style="width:8%" class="text-right">Net Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $grsubtot = $grtotdisc = $grtaxable = $grcgstamt = $grsgstamt = $grigstamt = $grtotbillamt = 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);
                                $getgodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code`='$pmain[godown]'")or die(mysqli_error($conn));
                                $godw = mysqli_fetch_array($getgodw);



                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[voucher]</td>";
                                Echo "<td>$pmain[invno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
                                Echo "<td>$godw[gcode]</td>";
                                Echo "<td>$pmain[gsttype]%</td>";
                                Echo "<td class='text-right'>$pmain[grossamt]</td>";
                                Echo "<td class='text-right'>$pmain[discamt]</td>";
                                Echo "<td class='text-right'>$pmain[taxableamt]</td>";
                                Echo "<td class='text-right'>$pmain[cgstamt]</td>";
                                Echo "<td class='text-right'>$pmain[sgstamt]</td>";
                                Echo "<td class='text-right'>$pmain[igstamt]</td>";
                                Echo "<td class='text-right'>$pmain[billamt]</td>";
                                Echo "</tr>";




                                $grsubtot = $grsubtot + $pmain['grossamt'];
                                $grtotdisc = $grtotdisc + $pmain['discamt'];
                                $grtaxable = $grtaxable + $pmain['taxableamt'];
                                $grcgstamt = $grcgstamt + $pmain['cgstamt'];
                                $grsgstamt = $grsgstamt + $pmain['sgstamt'];
                                $grigstamt = $grigstamt + $pmain['igstamt'];
                                $grtotbillamt = $grtotbillamt + $pmain['billamt'];


                                $totbill = $totbill + $pmain['netamt'];
                                $totamt = $totamt + $pmain['subtotal'];
                                $totdisc = $totdisc + $pmain['discount'];

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

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;'  class='table table-condensed table-bordered'>";
                                    Echo "<tr class='mySearchTr2'>";
                                    Echo "<td style='width:5%;'>Sr</td>";
                                    Echo "<td style='width:85%;'>Description</td>";
                                    Echo "<td style='width:10%;' class='text-right'>Amount</td>";
                                    Echo "</tr>";


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

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[descrip]</td>";
                                        Echo "<td class='text-right'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $totamount = $totamount + $tran['amount'];
                                    }
                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td class='text-right'>Total</td>";
                                    Echo "<td class='text-right'>$totamount</td>";
                                    Echo "</tr>";

                                    Echo "</table>";
                                    Echo "</td>";
                                    Echo "</tr>";
                                }
                            }
                            echo "<tr class='mySearchTr'>";
                            echo "<td align='right' colspan='7'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' colspan=''><h4><b>$grsubtot</b></h4></td>";
                            echo "<td align='right'><h4><b>$grtotdisc</b></h4></td>";
                            echo "<td align='right'><h4><b>$grtaxable</b></h4></td>";
                            echo "<td align='right'><h4><b>$grcgstamt</b></h4></td>";
                            echo "<td align='right'><h4><b>$grsgstamt</b></h4></td>";
                            echo "<td align='right'><h4><b>$grigstamt</b></h4></td>";
                            echo "<td align='right'><h4><b>$grtotbillamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

if (isset($_POST["excel"])) {
    $party = $_POST["party"];
    $godown = $_POST["godown"];
    $oms = $_POST["oms"];
    $dtype = $_POST["dtype"];

    $dtypecond = "";
    $mscopecond = "";

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

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

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

    if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }


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

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

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



    $contents .= "Sr., Credit No., Invoice, Date, Party, Godown, GST%, Subtotal, Discount, Taxable Amount, CGST, SGST, IGST, Net Amount\n";

    $ctr = 0;
    $grsubtot = $grtotdisc = $grtaxable = $grcgstamt = $grsgstamt = $grigstamt = $grtotbillamt = 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);
        $getgodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code`='$pmain[godown]'")or die(mysqli_error($conn));
        $godw = mysqli_fetch_array($getgodw);



        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['voucher'] . ",";
        $contents .= $pmain['invno'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $supp['name'] . ",";
        $contents .= $godw['gcode'] . ",";
        $contents .= $pmain['gsttype'] . "%" . ",";
        $contents .= $pmain['grossamt'] . ",";
        $contents .= $pmain['discamt'] . ",";
        $contents .= $pmain['taxableamt'] . ",";
        $contents .= $pmain['cgstamt'] . ",";
        $contents .= $pmain['sgstamt'] . ",";
        $contents .= $pmain['igstamt'] . ",";
        $contents .= $pmain['billamt'] . "\n";





        $grsubtot = $grsubtot + $pmain['grossamt'];
        $grtotdisc = $grtotdisc + $pmain['discamt'];
        $grtaxable = $grtaxable + $pmain['taxableamt'];
        $grcgstamt = $grcgstamt + $pmain['cgstamt'];
        $grsgstamt = $grsgstamt + $pmain['sgstamt'];
        $grigstamt = $grigstamt + $pmain['igstamt'];
        $grtotbillamt = $grtotbillamt + $pmain['billamt'];


        $totbill = $totbill + $pmain['netamt'];
        $totamt = $totamt + $pmain['subtotal'];
        $totdisc = $totdisc + $pmain['discount'];

        if ($_POST["dtype"] == 2) {
            $contents .= "Sr., Description, Amount\n";

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

                $ctr2++;
                $contents .= $ctr2 . ",";
                $contents .= $tran['descrip'] . ",";
                $contents .= $tran['amount'] . ",";

                $totamount = $totamount + $tran['amount'];
            }

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

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $grsubtot . ",";
    $contents .= $grtotdisc . ",";
    $contents .= $grtaxable . ",";
    $contents .= $grcgstamt . ",";
    $contents .= $grsgstamt . ",";
    $contents .= $grigstamt . ",";
    $contents .= $grtotbillamt . "\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;
    }
}
?>