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

$maindb = 'credmain';
$trandb = 'credoth';
$formname = "Credit Note (Others) Register";
$file = "ticredothregoth_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);

    $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);
    
if (isset($_POST["view"])) {
    
    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"); ?>
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed ">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Sr. No.</th>
                                <th style="width:8%">Voucher No.</th>
                                <th style="width:6%">Invoice No.</th>
                                <th style="width:7%">Date</th>
                                <th style="width:17%">Party</th>
                                <th style="width:6%">Godown</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">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>";
                                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 class='text-right'>$pmain[grossamt]</td>";
                                Echo "<td class='text-right'>$pmain[totdisc]</td>";
                                Echo "<td class='text-right'>$pmain[totbillamt]</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['totbillamt'];
 

                                $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 style='background-color:#BABFC2'>";
                                    Echo "<td style='width:5%;'>Sr. No.</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>";
                            echo "<td align='right' colspan='6'><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>$grtotbillamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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


    $contents .= "Sr., Credit No., Invoice, Date, Party, Godown, Subtotal, Discount, Taxable Amount,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['grossamt'] . ",";
        $contents .= $pmain['discamt'] . ",";
        $contents .= $pmain['taxableamt'] . ",";
        $contents .= $pmain['billamt'] . "\n";





        $grsubtot = $grsubtot + $pmain['grossamt'];
        $grtotdisc = $grtotdisc + $pmain['discamt'];
        $grtaxable = $grtaxable + $pmain['taxableamt'];
        $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'] . "\n";

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

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

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