<?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';
$maindb = 'credmain';
$trandb = 'credtran';

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

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

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

$formname = 'Credit Note Register';

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


if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
}
$fseries = $_SESSION["series"];
$keycond = "AND series = '$fseries'";
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; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.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 = "";
}


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


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

            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>
                        <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%;">Invoice No.</th>
                                    <th width="6%;">Date</th>
                                    <th width="30%;">Party Name</th>
                                    <th width="10%;" class="text-right">Total Qty</th>
                                    <th width="10%;" class="text-right">Sub Total</th>
                                    <!-- <th width="7%;">Less Discount</th> -->
                                    <th width="7%;">Cartage/Courier</th>
                                    <th width="10%;" class="text-right">Net Amount</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);

                                    $godown = $sgodw['gcode'];
                                    $voucher = $pmain['vouchno'];
                                    $invno = $pmain['invno'];
                                    $mydt = $pmain['mydt'];
                                    $party = $sparty['name'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totvat = $pmain['totvat'];
                                    $totdisc = $pmain['totdisc'];
                                    $totqty = $pmain['totalqty'];
                                    $packchgs = $pmain['packchgs'];

                                    $pctr++;
                                    echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$invno</td>";
                                    echo "<td>$mydt</td>";
                                    echo "<td>$party</td>";
                                    echo "<td class='text-right'>$totqty</td>";
                                    echo "<td class='text-right'>$subtot</td>";
                                    // echo "<td class='text-right'>$totdisc</td>";
                                    echo "<td class='text-right'>$packchgs</td>";
                                    echo "<td class='text-right'>$billamt</td>";
                                    echo "</tr>";

                                    $mtotqty   += $totqty;
                                    $msubtot   += $subtot;
                                    $mtotdisc  += $totdisc;
                                    $mpackchgs += $packchgs;
                                    $mbillamt  += $billamt;

                                    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:8%;'>Code</th>";
                                        echo "<th style='width:15%;' class=''>Item</th>";
//                                        echo "<th style='width:4%;' class='text-right'>Packing</th>";
                                        echo "<th style='width:4%;' class='text-right'>Bags</th>";
                                        echo "<th style='width:4%;' class='text-right'>Qty.</th>";
                                        echo "<th style='width:5%;' class='text-right'>Rate/ 10kg</th>";
                                        echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                        // echo "<th style='width:5%;' class='text-right'>Disc %</th>";
                                        // echo "<th style='width:5%;' class='text-right'>Disc Amt</th>";
                                        echo "<th style='width:5%;' class='text-right'>Total</th>";
                                        echo "</tr>";
                                        echo "<thead>";
                                        echo "<tr>";
                                        

                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $tcartqty = 0;
                                        $tqty = 0;
                                        $tcost = 0;
                                        $tamount = 0;
                                        $tdiscperc = 0;
                                        $tdiscamt = 0;
                                        $ttotal = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

                                            $getitem = mysqli_query($conn, "SELECT `code`,`name`,`unit` FROM `product` WHERE `code`='$tran[itemcode]'") 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[pcscart]</td>";
                                            echo "<td class='text-right'>$tran[cartqty]</td>";
                                            echo "<td class='text-right'>$tran[qty]</td>";
                                            echo "<td class='text-right'>$tran[cost]</td>";
                                            echo "<td class='text-right'>$tran[amount]</td>";
                                            // echo "<td class='text-right'>$tran[discperc]</td>";
                                            // echo "<td class='text-right'>$tran[discamt]</td>";
                                            echo "<td class='text-right'>$tran[total]</td>";
                                            echo "</tr>";

                                            $tpcscart  += $tran['pcscart'];
                                            $tcartqty  += $tran['cartqty'];
                                            $tqty      += $tran['qty'];
                                            $tcost     += $tran['cost'];
                                            $tamount   += $tran['amount'];
                                            $tdiscperc += $tran['discperc'];
                                            $tdiscamt  += $tran['discamt'];
                                            $ttotal    += $tran['total'];
                                        }

                                        echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "<td class='text-right'>Total</td>";
//                                        echo "<td class='text-right'>$tpcscart</td>";
                                        echo "<td class='text-right'>$tcartqty</td>";
                                        echo "<td class='text-right'>$tqty</td>";
                                        echo "<td class='text-right'>$tcost</td>";
                                        echo "<td class='text-right'>$tamount</td>";
                                        // echo "<td class='text-right'>$tdiscperc</td>";
                                        // echo "<td class='text-right'>$tdiscamt</td>";
                                        echo "<td class='text-right'>$ttotal</td>";
                                        echo "</tr>";
                                        echo "</table>";
                                    }
                                }
                                echo "<tr style='background-color: #98F9F0;color: red;'>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td class='text-right'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$mtotqty</b></td>";
                                echo "<td class='numeric text-right'><b>$msubtot</b></td>";
                                // echo "<td class='numeric text-right'><b>$mtotdisc</b></td>";
                                echo "<td class='numeric text-right'><b>$mpackchgs</b></td>";
                                echo "<td class='numeric text-right'><b>$mbillamt</b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
<?php
}

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

$contents .="Sr, Voucher, Invoice No., Date, Party Name, Total Qty, Sub Total, Cartage/Courier, Net Amount\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);

    $godown = $sgodw['gcode'];
    $voucher = $pmain['vouchno'];
    $invno = $pmain['invno'];
    $mydt = $pmain['mydt'];
    $party = $sparty['name'];
    $billamt = $pmain['billamt'];
    $subtot = $pmain['grossamt'];
    $totvat = $pmain['totvat'];
    $totdisc = $pmain['totdisc'];
    $totqty = $pmain['totalqty'];
    $packchgs = $pmain['packchgs'];

    $pctr++;

    $contents .="$pctr" . ",";
    $contents .="$voucher" . ",";
    $contents .="$invno" . ",";
    $contents .="$mydt" . ",";
    $contents .="$party" . ",";
    $contents .="$totqty" . ",";
    $contents .="$subtot" . ",";
    $contents .="$packchgs" . ",";
    $contents .="$billamt" . "\n";

    $mtotqty   += $totqty;
    $msubtot   += $subtot;
    $mtotdisc  += $totdisc;
    $mpackchgs += $packchgs;
    $mbillamt  += $billamt;

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

        $contents .="Sr, Code, Item, Bags, Qty., Rate/ 10kg, Amount, Total\n";

        $ctr2 = 0;
        $pctr2 = 0;
        $tcartqty = 0;
        $tqty = 0;
        $tcost = 0;
        $tamount = 0;
        $tdiscperc = 0;
        $tdiscamt = 0;
        $ttotal = 0;
        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

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

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

            $ctr2++;

            $contents .="$ctr2" . ",";
            $contents .="$tran[barcode]" . ",";
            $contents .="$showitem[name]" . ",";
            $contents .="$tran[cartqty]" . ",";
            $contents .="$tran[qty]" . ",";
            $contents .="$tran[cost]" . ",";
            $contents .="$tran[amount]" . ",";
            $contents .="$tran[total]" . "\n";

            $tpcscart  += $tran['pcscart'];
            $tcartqty  += $tran['cartqty'];
            $tqty      += $tran['qty'];
            $tcost     += $tran['cost'];
            $tamount   += $tran['amount'];
            $tdiscperc += $tran['discperc'];
            $tdiscamt  += $tran['discamt'];
            $ttotal    += $tran['total'];
        }

        $contents .="" . ",";
        $contents .="" . ",";
        $contents .="Total" . ",";
        $contents .="$tcartqty" . ",";
        $contents .="$tqty" . ",";
        $contents .="$tcost" . ",";
        $contents .="$amount" . ",";
        $contents .="$ttotal" . "\n ";
    }
}
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="" . ",";
$contents .="Total" . ",";
$contents .="$mtotqty" . ",";
$contents .="$msubtot" . ",";
$contents .="$mpackchgs" . ",";
$contents .="$mbillamt" . "\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;
    }
}
?>