<?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 = "credothreg_fr.php";

$zroot = 'cepheisy_msp_root';
$keycond = "AND `credtype`='O'";

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

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

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

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




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


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

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

    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' $keycond $partycond $mscopecond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);
    include("includes/linkshead.php");
        $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
             <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">Voucher.No</th>
                                <th style="width:10%">Date</th>
                                <th style="width:30%">Party</th>
                                <th style="width:15%" class="text-right">Amount</th>
                                <th style="width:8%"  class="text-right">Discount</th>
                                <th style="width:6%"  class="text-right">V.A.T</th>
                                <th style="width:15%" class="text-right">Bill.Amt</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totdisc = 0;
                            $totbill = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {

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

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$sparty[name]</td>";
                                Echo "<td class='text-right'>$pmain[subtotal]</td>";
                                Echo "<td class='text-right'>$pmain[discount]</td>";
                                Echo "<td class='text-right'>$pmain[vatamt]</td>";
                                Echo "<td class='text-right'>$pmain[netamt]</td>";
                                Echo "</tr>";
                                $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%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:80%; class='numeric''>Description</th>";
                                    Echo "<th style='background-color: #cccccc;width:10%;' class='numeric'>Amount</th>";

                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $tot = 0;

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

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



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


                                        $tot = $tran[amount] + $tot;
                                    }
                                    if ($_POST["dtype"] == 2) {
                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td class='numeric'style='color:blue;'>Sub Total</td>";
                                        Echo "<td class='numeric' style='color:blue;'>$tot</td>";
                                        Echo "</tr>";
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:red;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totamt</b></h4></td>";
                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totdisc</b></h4></td>";
                            echo "<td align='right' style='color:red;' id='font'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:red;' id='font'><h4><b>$totbill</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"];
    $dtype = $_POST["dtype"];

    $dtypecond = "";
    $keycond = "AND `credtype`='O'";

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




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


//    if (!empty($frmdt)) {
//        $frmdtcond = " AND `vouchdt`='$frmdt' ";
//    }
//
//    if (!empty($todt)) {
//        $todtcond = " AND `todt`='$todt' ";
//    }

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

    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' $keycond $partycond ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents.="Sr.,Voucher No.,Date,Party,Amount,Discount,V.A.T,Bill. Amt.,\n";

    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

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

        $ctr++;
        $pvouchno = $pmain[vouchno];
        $pmydt = $pmain[mydt];
        $pname = $sparty[name];
        $psubtotal = $pmain[subtotal];
        $pdiscount = $pmain[discount];
        $pvatamt = $pmain[vatamt];
        $pnetamt = $pmain[netamt];

        $contents.=$ctr . ",";
        $contents.=$pvouchno . ",";
        $contents.=$pmydt . ",";
        $contents.=$pname . ",";
        $contents.=$psubtotal . ",";
        $contents.=$pdiscount . ",";
        $contents.=$pvatamt . ",";
        $contents.=$pnetamt . "\n";

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

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

            $contents. "Sr.,Description,Amount\n";

            $ctr2 = 0;
            $tot = 0;

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

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

                $ctr2++;
                $pdescription = $tran[descrip];
                $pamount = $tran[amount];

                $contents.=$ctr2 . ",";
                $contents.=$pdescription . ",";
                $contents.=$pamount . "\n";
            }
            $tot = $tran[amount] + $tot;

            $contents.=" " . ",";
            $contents.="SubTotal" . ",";
            $contents.="$tot" . "\n";
        }
    }
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.=" " . ",";
    $contents.="Grand Total" . ",";
    $contents.="$totamt" . ",";
    $contents.="$totdisc" . ",";
    $contents.=" " . ",";
    $contents.="$totbill" . "\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;
    }
}
?>