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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Slip Register";
$file = "slipreg.php";

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

$dtye = $_POST["dtype"];
$name = $_POST["account"];
$shift = $_POST["shift"];
$ord = $_POST["ord"];

//$fromdate = $_POST["fromdate"];
//browse($fromdate);

$accountcond = "";
$custcodecond = "";
$shiftcond = "";
$productcond = "";


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

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

if (!empty($shift)) {
    $shiftcond = " AND `shift`='$shift' ";
}

if (!empty($ord)) {
    if ($ord == 1) {

        $ordcond = " order by name";
    } else {
        $ordcond = " order by custcode+0";
    }
}

$fromdate = $_POST["todate"];


//    if (empty($_POST["fromdate"])) {
//        header("refresh:1; slipreg.php");
//    }
//    if (empty($_POST["todt"])) {
//        header("refresh:1; slipreg.php");
//    }AND a.vouchdt = '$fromdate'
//    $getpmain = mysqli_query($conn, "SELECT a.vouchno,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,a.shift,a.party,b.accode,b.name,b.custcode FROM `$maindb` a,accmast b where a.party= b.accode AND grpcode = '010' $accountcond ")or die(mysqli_error($conn));


$getpmain = mysqli_query($conn, "SELECT * FROM `accmast` where grpcode= '010' $ordcond ")or die(mysqli_error($conn));

$get = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold;
        }
        th{
            background: #6082BF;
        }

    </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 table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:8%">A/c No.</th>
                                <th style="width:8%">Type</th>
                                <th style="width:25%">Account Name</th>
                                <th style="width:17%" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totqty = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $maccode = $pmain['accode'];
                                $mnamee = $pmain['name'];
                                $deposit = $pmain['deposit'];

                                if ($pmain['billtype'] == 'M') {
                                    $billtype = "Multiple Bill";
                                }
                                if ($pmain['billtype'] == 'S') {
                                    $billtype = "Single Bill";
                                }

                                $getrec = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' and date <= '$fromdate'") or die(mysqli_error($conn));
                                $fetrec = mysqli_fetch_array($getrec);
                                $ramt = $fetrec['amt'];

                                $mbal = $ramt * -1;
                                $diffamt = 0;
                                if ($mbal > $deposit) {
                                    $diffamt = abs($deposit) - abs($mbal);
                                }
                                if ($diffamt < 0) {
                                    $amount = abs($diffamt);
                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$pmain[custcode]</td>";
                                    Echo "<td>$billtype</td>";
                                    Echo "<td>$pmain[name]</td>";
                                    Echo "<td class='text-right'>$amount</td>";
                                    Echo "</tr>";
                                    $tamount = $tamount + $amount;
                                }
                            }
                            Echo "<tr id='boldfont'>";
                            Echo "<td></td>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'>Total</td>";
                            Echo "<td class='text-right'>$tamount</td>";
                            Echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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

    $contents .= "Sr,Voucher No.,Date,Vehicle No.,Account Name,Product,Shift\n";

    $ctr = 0;
    $totqty = 0;

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

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

        $nname = mysqli_query($conn, "SELECT * FROM `product` where code='$pmain[product]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($nname);

        $ctr++;

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[vehicleno]" . ",";
        $contents .= "$fetname[name]" . ",";
        $contents .= "$godwn[name]" . ",";
        $contents .= "$pmain[shift]" . "\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;
    }
}
?>