<?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"];
$formname = 'Credit Note Others Register';

$file = 'credothreg_fr.php';

IF (isset($_GET[credtype]) != "") {
    $zcredtype = "$_GET[credtype]";
} ELSE {
    $zcredtype = "O";
}

$keycond = " AND `debtype`='$zcredtype' ";


$party = $_POST["party"];
$dtype = $_POST["dtype"];

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

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 (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">
            <?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 debtype = 'O' $partycond $keycond 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 Others 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-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="8%;">Credit No.</th>
                                    <th width="8%;">Credit Date</th>
                                    <th width="30%;">Party Name</th>                                
                                    <th width="8%;">Invoice No.</th>
                                    <th width="8%;">Date</th>
                                    <th class="text-right" width="10%;">Subtotal</th>
                                    <th class="text-right" width="10%;">Add VAT</th>
                                    <th class="text-right" width="15%;">Net Amt</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = $tgross = $tbill = 0;
                                while ($pmain = mysqli_fetch_array($getpmain2)) {


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


                                    $vouchno = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $partyname = $sparty['name'];
                                    $invno = $pmain['invno'];
                                    $invdt = $pmain['invdate'];
                                    $grossamt = $pmain['grossamt'];
                                    $totvat = $pmain['totvat'];
                                    $billamt = $pmain['billamt'];

                                    $ctr++;
                                    Echo"<tr data-href='credoth.php?mode=edit&vouchno=$vouchno' class='clickable-row2' [target]='_blank'>";
                                    Echo"<td>$ctr</td>";
                                    Echo"<td>$vouchno</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td>$partyname</td>";
                                    Echo"<td>$invno</td>";
                                    Echo"<td>$invdt</td>";
                                    Echo "<td class='text-right'>$grossamt</td>";
                                    Echo"<td class='text-right'>$totvat</td>";
                                    Echo"<td class='text-right'>$billamt</td>";
                                    Echo"</tr>";

                                    $tgross = $tgross + $grossamt;
                                    $tbill = $tbill + $billamt;

                                    if ($dtype == 2) {
                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='35'>";
                                        Echo "<table style='width:120%;' 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:50%;' class=''>Description</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Amount.</th>";
                                        Echo "<th style='width:18%;' class='text-right'>VAT%</th>";
                                        Echo "<th style='width:8%;' class='text-right'>VAT</th>";
                                        Echo "<th style='width:8%;' class='text-right'>Total</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";
//
//
                                        $ctr2 = $tamt = $tvat = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `credoth` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $descrip = $tran['descrip'];
                                            $amount = $tran['amount'];
                                            $vatperc = $tran['vatperc'];
                                            $vat = $tran['vat'];
                                            $totalamt = $tran['total'];

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$descrip</td>";
                                            Echo "<td class='text-right'>$amount</td>";
                                            Echo "<td class='text-right'>$vatperc</td>";
                                            Echo "<td class='text-right'>$vat</td>";
                                            Echo "<td class='text-right'>$totalamt</td>";
                                            Echo "</tr>";

                                            $tamt = $tamt + $amount;
                                            $tvat = $tvat + $vat;
                                            $ttotamt = $ttotamt + $totalamt;
                                        }

                                        Echo "<tr style='background-color:#d8e0e6'>";
                                        Echo "<td ></td>";
                                        Echo "<td class='text-right' colspan='1'>Total</td>";
                                        Echo "<td class='text-right'>$tamt</td>";
                                        Echo "<td class='text-right'></td>";
                                        Echo "<td class='text-right'>$tvat</td>";
                                        Echo "<td class='text-right'>$totalamt</td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }

                                Echo "<tr style='background-color: #98F9F0'>";
                                Echo "<td class='text-right' colspan='6'><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>$tgross</b></td>";
                                Echo "<td class='numeric text-right'><b></b></td>";
                                Echo "<td class='numeric text-right'><b>$tbill</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 debtype = 'O' $suppcond $ptypecond  $gstcond $pcond order by vouchdt,vouchno")or die(mysqli_error($conn));

    $getno2 = mysqli_num_rows($getpmain2);

    if ($getno2 > 0) {

        $contents .= "Sr,Voucher,Invoice No.,Date,Party Name,Sub Total,Disc Amt,Vat,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);

            $voucher = $pmain['vouchno'];
            $invno = $pmain['invno'];
            $mydt = $pmain['mydt'];
            $party = $sparty['name'];
            $billamt = $pmain['billamt'];
            $subtot = $pmain['grossamt'];
            $totvat = $pmain['totvat'];
            $totdisc = $pmain['totdisc'];

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

            if ($ptype != "") {
                $contents .= "$ptyped" . ",";
            }
            $contents .= "$subtot" . ",";
            $contents .= "$totdisc" . ",";
            $contents .= "$totvat" . ",";
            $contents .= "$billamt" . "\n";

            $pgrsubtot = $pgrsubtot + $subtot;
            $pgrtotdis = $pgrtotdis + $totdisc;
            $pgrtotvat = $pgrtotvat + $totvat;
            $pgrtotbillamt = $pgrtotbillamt + $billamt;

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

                $contents .= "Sr,Code,Item,Ctn Qty.,Rate,Amount,Disc % ,Discount,Vat %,Vat,Total\n";

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

                $gettran = mysqli_query($conn, "SELECT * FROM `saletran` 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[itemcode]'") or die(mysqli_error($conn));
                    $showitem = mysqli_fetch_array($getitem);

                    $ctr2++;
                    $contents .= "$ctr2" . ",";
                    $contents .= "$tran[barcode]" . ",";
                    $contents .= "$showitem[name]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$tran[cost]" . ",";
                    $contents .= "$tran[amount]" . ",";
                    $contents .= "$tran[discperc]" . ",";
                    $contents .= "$tran[discamt]" . ",";
                    $contents .= "$tran[vatperc]" . ",";
                    $contents .= "$tran[vat]" . ",";
                    $contents .= "$tran[total]" . "\n";

                    $pcartqty = $pcartqty + $tran['cartqty'];
                    $pqty = $pqty + $tran['qty'];
                    $ptotqty = $ptotqty + $tran['totqty'];
                    $ptotamount = $ptotamount + $tran['amount'];
                    $ptotdiscount = $ptotdiscount + $tran['discamt'];
                    $ptotvat = $ptotvat + $tran['vat'];
                    $ptotal = $ptotal + $tran['total'];
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= "$pqty" . ",";
                $contents .= "" . ",";
                $contents .= "$ptotamount" . ",";
                $contents .= "" . ",";
                $contents .= "$ptotdiscount" . ",";
                $contents .= "" . ",";
                $contents .= "$ptotvat" . ",";
                $contents .= "$ptotal" . "\n";
            }
        }
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";

        if ($ptype != "") {
            $contents .= "" . ",";
        }
        $contents .= "Total" . ",";
        $contents .= "$pgrsubtot" . ",";
        $contents .= "$pgrtotdis" . ",";
        $contents .= "$pgrtotvat" . ",";
        $contents .= "$pgrtotbillamt" . "\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;
        }
    }
}
?>