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

$maindb = "accmast";
$formname = "Other A/c. Print";
$file = "othlst_fr.php";

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


if (isset($_POST["view"])) {
    $name = $_POST['name'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $state = $_POST['state'];

    $namecond = "";
    $countrycond = "";
    $citycond = "";
    $statecond = "";

    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }
    if (!empty($country)) {
        $countrycond = " AND `country` LIKE '%$country%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    if (!empty($state)) {
        $statecond = " AND `state` LIKE '%$state%' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `grpcode` > '002' $namecond $countrycond $citycond $statecond") or die(mysqli_error($conn));

    include("includes/linkshead.php");
?>
    <script src="MyScript/item.js" type="text/javascript"></script>

    <style>
        @media print {

            .no-print,
            .no-print * {
                display: none !important;
            }
        }
    </style>
    </head>

    <body>
        <div class="container">
            <?php include('includes/head_rep.php') ?>
            <table class="table table-condensed table-bordered table-hover">
                <tr class="mySearchTr">
                    <th>Sr. No.</th>
                    <th>Name</th>
                    <th>Address</th>
                    <th>GST No.</th>
                    <th>Country</th>
                    <th>Email</th>
                    <th>Pin</th>
                    <th>Tel.</th>
                    <th>Mobile</th>
                </tr>
                <?php
                $ctr = 1;
                while ($pmain = mysqli_fetch_array($getpmain)) {

                    echo "<tr>";
                    echo "<td>$ctr</td>";
                    echo "<td>$pmain[name]</td>";
                    echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                    echo "<td>$pmain[gstno]</td>";
                    echo "<td>$pmain[country]</td>";
                    echo "<td>$pmain[email]</td>";
                    echo "<td>$pmain[pin]</td>";
                    echo "<td>$pmain[tel]</td>";
                    echo "<td>$pmain[mobile]</td>";
                    echo "</tr>";
                    $ctr++;
                }
                ?>
            </table>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>

    </html>
<?
}

if (isset($_POST["excel"])) {
    $name = $_POST['name'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $state = $_POST['state'];

    $namecond = "";
    $countrycond = "";
    $citycond = "";
    $statecond = "";

    if (!empty($name)) {
        $namecond = " AND `accode`='$name' ";
    }
    if (!empty($country)) {
        $countrycond = " AND `country` LIKE '%$country%' ";
    }
    if (!empty($city)) {
        $citycond = " AND `city` LIKE '%$city%' ";
    }
    if (!empty($state)) {
        $statecond = " AND `state` LIKE '%$state%' ";
    }
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `grpcode` > '002' $namecond $countrycond $citycond $statecond") or die(mysqli_error($conn));

    $contents .= "Sr. No., Name, Address, GST No., Country, Email, Pin, Tel., Mobile \n";

    $ctr = 1;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[add1]" . "$pmain[add2]" . "$pmain[add3]" . ",";
        $contents .= "$pmain[gstno]" . ",";
        $contents .= "$pmain[country]" . ",";
        $contents .= "$pmain[email]" . ",";
        $contents .= "$pmain[pin]" . ",";
        $contents .= "$pmain[tel]" . ",";
        $contents .= "$pmain[mobile]" . "\n";
        $ctr++;
    }

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