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

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

$file = 'credreg_fr.php';

$supplier = $_POST["supplier"];
$p = $_GET["p"];
$ptype = $_POST["ptype"];
$pchtype = $_POST["pchtype"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

if (!empty($supplier)) {
    $suppcond = " AND `party` = '$supplier' ";
    $suppcond2 = " AND `accode` = '$supplier' ";
}
if (!empty($p)) {
    $pcond = " AND `party` = '$p' ";
}
if (!empty($ptype)) {
    $ptypecond = " AND `paidby`= '$ptype'";
}

if ($ptype == "C") {
    $paytype = "CASH";
} else {
    $paytype = "CREDIT";
}

$formname = 'Credit Note/Goods Return Register';

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


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

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; credreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; credreg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

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

if (isset($_POST["view"]) || ($_GET["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-fluid">
            <?php
            include("includes/head_rep2.php");

            $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `credmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate'  and credtype = 'G' $suppcond $ptypecond  $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));

            $getno2 = mysqli_num_rows($getpmain2);

            if ($getno2 > 0) {
                ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>Credit Note Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <?php if ($ptype != "") { ?>
                            <table width="100%" class="table" style="background-color: #98F9F0">
                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                            </table>
                        <?php } ?>
                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="4%;">Voucher</th>
                                    <th width="6%;">Date</th>
                                    <th width="18%;">Party Name</th>                                
                                    <th width="9%;" class="text-right">Godown</th>
                                    <th width="9%;" class="text-right">Total Qty</th>
                                    <th width="8%;" class="text-right">Sub Total</th>
                                    <th width="7%;">Less Discount %</th>

                                    <?php // if ($ptype != "") { ?>
            <!--<th width="7%;">Pay type</th>-->
                                    <?php // } ?>
                                    <th width="6%;" class="text-right">Less Discount Amt</th>
                                    <th width="6%;" class="text-right">Add Packing</th>
                                    <th width="6%;" class="text-right">Net Amount</th>
                                    <th width="22%;"> Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ptotamt = 0;
                                $ptotdiscamt = 0;
                                $ptottaxable = 0;
                                $ptotgst3 = 0;
                                $ptotgst5 = 0;
                                $ptotgst12 = 0;
                                $ptotgst18 = 0;
                                $ptotgst28 = 0;
                                $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
                                $pchtypecond = "";


                                while ($pmain = mysqli_fetch_array($getpmain2)) {
                                    if ($pmain['paidby'] == "C") {
                                        $ptyped = "CASH";
                                    } else {
                                        $ptyped = "CREDIT";
                                    }

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
                                    $sparty = mysqli_fetch_array($gparty);

                                    $gtgdn = mysqli_query($conn, "SELECT * FROM `godown`")or die(mysqli_error($conn));
                                    $ftgdn = mysqli_fetch_array($gtgdn);
                                    $gcode = $ftgdn['gcode'];

                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $mydt = $pmain['mydt'];
                                    $party = $sparty['name'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $litemdisc = $pmain['ldiscperc'];
                                    $ldiscamt = $pmain['ldiscamt'];
                                    $totvat = $pmain['tottaxamt'];
                                    $addpack = $pmain['addpack'];
                                    $qty = $pmain['totalqty'];
                                    $totbillamt = $pmain['totbillamt'];
                                    $remark1 = $pmain['remark1'];
                                    $remark2 = $pmain['remark2'];

                                    $pctr++;
                                    Echo"<tr data-href='purchase.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td>$party</td>";
                                    Echo"<td>$gcode</td>";
                                    if ($ptype != "") {
                                        Echo"<td>$ptyped</td>";
                                    }
                                    Echo"<td class='text-right'>$qty</td>";
                                    Echo"<td class='text-right'>$subtot</td>";
                                    Echo"<td class='text-right'>$litemdisc</td>";
                                    Echo"<td class='text-right'>$ldiscamt</td>";
                                    Echo"<td class='text-right'>$addpack</td>";
                                    Echo"<td class='text-right'>$billamt</td>";
                                    Echo"<td>$remark1 $remark2</td>";
                                    Echo"</tr>";

                                    $pgrsubtot = $pgrsubtot + $subtot;
                                    $pgrtotdis = $pgrtotdis + $totdisc;
                                    $pgrtotvat = $pgrtotvat + $totvat;
                                    $pgrtotbillamt = $pgrtotbillamt + $billamt;
                                    $totalqty = $totalqty + $qty;
                                    $plitemdisc = $plitemdisc + $litemdisc;
                                    $ptotgst = $ptotgst + $totgst;

                                    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 "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:5%;'>Barcode</th>";
                                        Echo "<th style='width:15%;' class=''>Itemname</th>";
                                        Echo "<th style='width:4%;' class=''>Stk.</th>";
                                        Echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                        Echo "<th style='width:5%;' class='text-right'>Amount</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>Discount</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>Taxable Value</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>CGST Rate</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>CGST Amt</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>SGST Rate</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>SGST Amt</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>IGST Rate</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>IGST Amt</th>";
//                                        Echo "<th style='width:5%;' class='text-right'>Total</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";


                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[barcode]</td>";
                                            Echo "<td>$showitem[name]</td>";
                                            Echo "<td class='text-right'>$tran[stk]</td>";
                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                            Echo "<td class='text-right'>$tran[rate]</td>";
                                            Echo "<td class='text-right'>$tran[amount]</td>";
//                                            Echo "<td class='text-right'>$tran[discount]</td>";
//                                            Echo "<td class='text-right'>$tran[subtot]</td>";
//                                            Echo "<td class='text-right'>$tran[cgstrate]</td>";
//                                            Echo "<td class='text-right'>$tran[cgstamt]</td>";
//                                            Echo "<td class='text-right'>$tran[sgstrate]</td>";
//                                            Echo "<td class='text-right'>$tran[sgstamt]</td>";
//                                            Echo "<td class='text-right'>$tran[igstrate]</td>";
//                                            Echo "<td class='text-right'>$tran[igstamt]</td>";
//                                            Echo "<td class='text-right'>$tran[total]</td>";
                                            Echo "</tr>";

                                            $pcartqty = $pcartqty + $tran['cartqty'];
                                            $pqty = $pqty + $tran['qty'];
                                            $prate = $prate + $tran['rate'];
                                            $pamount = $pamount + $tran['amount'];
                                            $pdiscount = $pdiscount + $tran['discount'];
                                            $psubtot = $psubtot + $tran['subtot'];
                                            $pcgstrate = $pcgstrate + $tran['cgstrate'];
                                            $pcgstamt = $pcgstamt + $tran['cgstamt'];
                                            $psgstrate = $psgstrate + $tran['sgstrate'];
                                            $psgstamt = $psgstamt + $tran['sgstamt'];
                                            $pigstrate = $pigstrate + $tran['igstrate'];
                                            $pigstamt = $pigstamt + $tran['igstamt'];
                                            $ptotal = $ptotal + $tran['total'];
                                        }

                                        Echo "<tr style='background-color:#d8e0e6'>";
                                        Echo "<td colspan='3'></td>";
                                        Echo "<td>Total</td>";
                                        Echo "<td class='text-right'>$pqty</td>";
                                        Echo "<td class='text-right'></td>";
                                        Echo "<td class='text-right'>$pamount</td>";
//                                        Echo "<td class='text-right'></td>";
//                                        Echo "<td class='text-right'>$psubtot</td>";
//                                        Echo "<td class='text-right'></td>";
//                                        Echo "<td class='text-right'>$pcgstamt</td>";
//                                        Echo "<td class='text-right'></td>";
//                                        Echo "<td class='text-right'>$psgstamt</td>";
//                                        Echo "<td class='text-right'></td>";
//                                        Echo "<td class='text-right'>$pigstamt</td>";
//                                        Echo "<td class='text-right'>$ptotal</td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }

                                Echo "<tr style='background-color: #98F9F0'>";
                                Echo "<td colspan='4'></td>";

                                if ($ptype != "") {
                                    Echo "<td></td>";
                                }
                                Echo "<td class='text-right'><b>Total</b></td>";
                                Echo "<td class='text-right'><b>$totalqty</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrsubtot</b></td>";
                                Echo "<td class='numeric text-right'><b>$plitemdisc</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotvat</b></td>";
                                Echo "<td class='numeric text-right'><b>$ptotgst</b></td>";
                                Echo "<td class='numeric text-right'><b>$pgrtotbillamt</b></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div>

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

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

    $getpmain2 = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `credmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate'  and credtype = 'G' $suppcond $ptypecond  $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));

    $getno2 = mysqli_num_rows($getpmain2);

    if ($getno2 > 0) {

        $contents .= "Credit Note Register\n";
        $contents .= "Total No., Of Record:" . ",";
        $contents .= "$getno2" . "\n";
        if ($ptype != "") {
            $contents .= "Pay Type :" . ",";
            $contents .= "$paytype" . "\n";
        }

        $contents .= "Sr., Voucher, Date, Party Name, Godown, Total Qty, Sub Total, Less Discount %,";
        if ($ptype != "") {
            $contents .= "Pay Type,";
        }
        $contents .= "Less Discount Amt, Add Packing, Net Amount, Remarks \n";

        $pctr = 0;
        $ptotamt = 0;
        $ptotdiscamt = 0;
        $ptottaxable = 0;
        $ptotgst3 = 0;
        $ptotgst5 = 0;
        $ptotgst12 = 0;
        $ptotgst18 = 0;
        $ptotgst28 = 0;
        $ptotpackamt = $ptotstamt = $ptotothamt = $ptotbillamt = 0;
        $pchtypecond = "";


        while ($pmain = mysqli_fetch_array($getpmain2)) {
            if ($pmain['paidby'] == "C") {
                $ptyped = "CASH";
            } else {
                $ptyped = "CREDIT";
            }

            $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
            $sparty = mysqli_fetch_array($gparty);

            $gtgdn = mysqli_query($conn, "SELECT * FROM `godown`")or die(mysqli_error($conn));
            $ftgdn = mysqli_fetch_array($gtgdn);
            $gcode = $ftgdn['gcode'];

            $voucher = $pmain['vouchno'];
            $invno = $pmain['invno'];
            $mydt = $pmain['mydt'];
            $party = $sparty['name'];
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $litemdisc = $pmain['ldiscperc'];
            $ldiscamt = $pmain['ldiscamt'];
            $totvat = $pmain['tottaxamt'];
            $addpack = $pmain['addpack'];
            $qty = $pmain['totalqty'];
            $totbillamt = $pmain['billamt'];
            $remark1 = $pmain['remark1'];
            $remark2 = $pmain['remark2'];

            $pctr++;
            $contents .= "$pctr" . ",";
            $contents .= "$voucher" . ",";
            $contents .= "$mydt" . ",";
            $contents .= "$party" . ",";
            $contents .= "$gcode" . ",";

            if ($ptype != "") {
                $contents .= "$ptyped" . ",";
            }
            $contents .= "$qty" . ",";
            $contents .= "$subtot" . ",";
            $contents .= "$litemdisc" . ",";
            $contents .= "$ldiscamt" . ",";
            $contents .= "$addpack" . ",";
            $contents .= "$totbillamt" . ",";
            $contents .= "$remark1 $remark2" . "\n";

            $pgrsubtot = $pgrsubtot + $subtot;
            $pgrtotdis = $pgrtotdis + $totdisc;
            $pgrtotvat = $pgrtotvat + $totvat;
            $pgrtotbillamt = $pgrtotbillamt + $billamt;
            $totalqty = $totalqty + $qty;
            $plitemdisc = $plitemdisc + $litemdisc;
            $ptotgst = $ptotgst + $totgst;

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


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

                $ctr2 = 0;
                $pctr2 = 0;
                $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

                    $ctr2++;

                    $contents .= "$ctr2" . ",";
                    $contents .= "$tran[barcode]" . ",";
                    $contents .= "$showitem[name]" . ",";
                    $contents .= "$tran[stk]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$tran[rate]" . ",";
                    $contents .= "$tran[amount]" . "\n";

                    $pcartqty = $pcartqty + $tran['cartqty'];
                    $pqty = $pqty + $tran['qty'];
                    $prate = $prate + $tran['rate'];
                    $pamount = $pamount + $tran['amount'];
                    $pdiscount = $pdiscount + $tran['discount'];
                    $psubtot = $psubtot + $tran['subtot'];
                    $pcgstrate = $pcgstrate + $tran['cgstrate'];
                    $pcgstamt = $pcgstamt + $tran['cgstamt'];
                    $psgstrate = $psgstrate + $tran['sgstrate'];
                    $psgstamt = $psgstamt + $tran['sgstamt'];
                    $pigstrate = $pigstrate + $tran['igstrate'];
                    $pigstamt = $pigstamt + $tran['igstamt'];
                    $ptotal = $ptotal + $tran['total'];
                }


                $contents .= "" . ",";
                $contents .= "" . ",,";
                $contents .= "Total" . ",";
                $contents .= "$pqty" . ",";
                $contents .= "" . ",";
                $contents .= "$pamount" . "\n";
            }
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";

        if ($ptype != "") {
            $contents .= "" . ",";
        }
        $contents .= "Total" . ",";
        $contents .= "$totalqty" . ",";
        $contents .= "$pgrsubtot" . ",";
        $contents .= "$plitemdisc" . ",";
        $contents .= "$pgrtotvat" . ",";
        $contents .= "$ptotgst" . ",";
        $contents .= "$pgrtotbillamt" . ",";
        $contents .= "" . ",";
        $contents .= "" . "\n";
    }

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>