<?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"];
$product = $_POST["product"];

//$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($product)) {
    $productcond = " AND b.`product`='$product' ";
}

$fromdate = $_POST["fromdate"];
$todate = $_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 a.*,DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.* FROM `$maindb` a,`$trandb` b 
                    where a.vouchno= b.vouchno AND a.vouchdt = '$fromdate'  $accountcond $shiftcond $productcond")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_rep2.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 98%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <!--<th style="width:7%">Sr</th>-->
                                    <th style="width:8%">A/c</th>
                                    <th style="width:10%">Slip No.</th>
                                    <th style="width:15%">Vehicle No.</th>
                                    <th style="width:12%">Voucher No.</th>
                                    <th style="width:13%">Date</th>
                                    <th style="width:17%">Product</th>
                                    <th style="width:10%">Shift</th>
                                    <th style="width:10%" class="text-right">Qty.</th>
                                    <th style="width:10%" class="text-center">Unit</th>
                                    <th style="width:10%" class="text-right">Rate</th>
                                    <th style="width:10%" class="text-right">Amount</th>

                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $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 prodcode='$pmain[product]'")or die(mysqli_error($conn));
                                    $godwn = mysqli_fetch_array($nname);

                                    $ctr++;
                                    Echo "<tr id='boldfont' >";
//                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$fetname[custcode]</td>";
                                    Echo "<td>$pmain[slipno]</td>";
                                    Echo "<td>$pmain[vehicleno]</td>";
                                    Echo "<td>$pmain[vouchno]</td>";
                                    Echo "<td>$pmain[mydt]</td>";
//                                    Echo "<td>$fetname[name]</td>";
                                    Echo "<td>$godwn[name]</td>";
                                    Echo "<td>$pmain[shift]</td>";
                                    Echo "<td  class='text-right'>$pmain[qty]</td>";
                                    Echo "<td  class='text-center'>$pmain[unit]</td>";
                                    Echo "<td  class='text-right'>$pmain[rate]</td>";
                                    Echo "<td  class='text-right'>$pmain[amount]</td>";
                                    Echo "</tr>";
                                    $tqty = $tqty + $pmain['qty'];
                                    $tamount = $tamount + $pmain['amount'];
                                }
                                Echo "<tr id='boldfont' style='background-color:#4EB3C7'>";
                                    Echo "<td colspan='7' class='text-right' style='color:yellow;font-size:20px'>Total</td>";
                                    Echo "<td  class='text-right'  style='color:yellow;font-size:20px'>$tqty</td>";
                                    Echo "<td  class='text-center'></td>";
                                    Echo "<td  class='text-right'></td>";
                                    Echo "<td  class='text-right'  style='color:yellow;font-size:20px'>$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;
    }
}
?>