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

$maindb = "custtokari";
$formname = "Customer To Karigar";
$file = "slmatreg.php";

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

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

    $partycond = "";
    $karigarcond = "";

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


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

    if (!empty($scope)) {
        $mscopecond = " AND `type`='$scope' ";
    }

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; custokar.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; custokar.php");
    }

    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond $karigarcond 
                            ")or die(mysqli_error($conn));

    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; 
        }
        .dtype{
            background-color: #cccccc;
        }
    </style>
    </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-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='5%'>Voucher</th>
                                <th width='10%'>Date</th>
                                <th width='10%'>Customer</th>
                                <th width='10%'>Karigar</th>
                                <th width='10%'>Bank Name</th>
                                <th width='10%'>Ac No.</th>
                                <th width='10%'>Cheque No.</th>
                                <th width='10%'>Chq Date.</th>
                                <th width='10%'>RTGS No.</th>
                                <th width='10%'>Remark</th>
                                <th width='10%' class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 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);
                                $getkar = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[karigar]'")or die(mysqli_error($conn));
                                $karr = mysqli_fetch_array($getkar);
                                $mvouch = $pmain[vouchno];
                                $action = "custtokari.php?vouchno=$mvouch&mode=edit";

                                $ctr++;
//                                browse($mvouch);
                                
                                Echo "<tr id='boldfont' class='clickable-row ' data-href='$action' data-target='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$mvouch</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$supp[name]</td>";
                                Echo "<td>$karr[name]</td>";
                                Echo "<td>$pmain[drawbank]</td>";
                                Echo "<td>$pmain[acnum]</td>";
                                Echo "<td>$pmain[chqno]</td>";
                                Echo "<td>$pmain[chqdate]</td>";
                                Echo "<td>$pmain[rtgsno]</td>";
                                Echo "<td>$pmain[remark]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "</tr>";

                                $ttotamt = $ttotamt + $pmain[amount];

                                $totbill = $totbill + $pmain[netamt];
//                                Echo "</table>";
                            }

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td align='right' style='color:$colr;'><h4><b>Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$ttotamt</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"];
    $karigar = $_POST["karigar"];

    $partycond = "";
    $karigarcond = "";

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


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

    if (!empty($scope)) {
        $mscopecond = " AND `type`='$scope' ";
    }

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; custokar.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; custokar.php");
    }

    $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond $karigarcond 
                            ")or die(mysqli_error($conn));

    $contents.="Sr, Voucher, Date, Customer, Karigar, Bank Name, AcNo, Cheque No, Chq Date, RTGS No, Remark , Amount\n";

    $ctr = 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);
        $getkar = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[karigar]'")or die(mysqli_error($conn));
        $karr = mysqli_fetch_array($getkar);
        $mvouch = $pmain[vouchno];
        $action = "custtokari.php?vouchno=$mvouch&mode=edit";

        $ctr++;
//        browse($mvouch);
        $contents.= $ctr . ",";
        $contents.= $ctr . ",";
        $contents.= $mvouch . ",";
        $contents.= $pmain[mydt] . ",";
        $contents.= $supp[name] . ",";
        $contents.= $karr[name] . ",";
        $contents.= $pmain[drawbank] . ",";
        $contents.= $pmain[acnum] . ",";
        $contents.= $pmain[chqno] . ",";
        $contents.= $pmain[chqdate] . ",";
        $contents.= $pmain[rtgsno] . ",";
        $contents.= $pmain[remark] . ",";
        $contents.= $pmain[amount] . "\n";

        $ttotamt = $ttotamt + $pmain[amount];

        $totbill = $totbill + $pmain[netamt];
    }

    $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;
    }
}
?>