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

$maindb = "attendlist";
$formname = "Attend List Print";
$file = "attendlist_print.php";
$mgrpcode = "";

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

$name = $_POST['name'];
$area = $_POST['area'];
$city = $_POST['city'];
$pin = $_POST['pin'];
$brno = $_POST['brno'];
$address = $_POST['address'];
$member = $_POST['member'];
$groupmast = $_POST['groupmast'];
$cbsno = $_POST['cbsno'];
$oldno = $_POST['oldno'];
$orderby = $_POST['orderby'];
$region = $_POST['region'];
$yearno = $_POST['yearno'];
$removdup = $_POST['removdup'];
$namecond = "";
$membercond = "";
$areacond = "";
$brnocond = "";
$citycond = "";
$pincond = "";
$addresscond = "";
$groupmastcond = "";
$orderbycond = "";
$cbscond = "";
$oldcond = "";
$regioncond = "";
$agm50 = $_POST['agm50'];
$agm51 = $_POST['agm51'];
$agm52 = $_POST['agm52'];
$agm53 = $_POST['agm53'];
$splagm = $_POST['splagm'];

if (!empty($agm50)) {
    $agm50cond = " AND `agm50`='1' ";
}
if (!empty($agm51)) {
    $agm51cond = " AND `agm51`='1' ";
}
if (!empty($agm52)) {
    $agm52cond = " AND `agm52`='1' ";
}
if (!empty($agm53)) {
    $agm53cond = " AND `agm53`='1' ";
}
if (!empty($splagm)) {
    $splagmcond = " AND `splagm`='1' ";
}
if (!empty($groupmast)) {
    $groupmastcond = " and accode in (select memcode from grouptran where code != '' and `code`='$groupmast')";
}
if (!empty($name)) {
    $namecond = " AND `accode`='$name' ";
}
if (!empty($member)) {
    $membercond = " AND `name` LIKE '%$member%' ";
}
if (!empty($brno)) {
    $brnocond = " AND `brno` LIKE '%$brno%' ";
}
if (!empty($yearno)) {
    $yearnocond = " AND `yearno` = '$yearno' ";
}
if (!empty($address)) {
    $addresscond = "  AND ( `add1` LIKE '%$address%' or  `add2` LIKE '%$address%' or  `add3` LIKE '%$address%' )";
}
if (!empty($area)) {
    $areacond = " AND `station` LIKE '%$area%' ";
}
if (!empty($region)) {
    $regioncond = " AND `region` = '$region' ";
}
if (!empty($brno)) {
    $brnocond = " AND `brno` LIKE '%$brno%' ";
}
if (!empty($pin)) {
    $pincond = " AND `pin` LIKE '%$pin%' ";
}
if (!empty($city)) {
    $citycond = " AND `city` LIKE '%$city%' ";
}
if (!empty($cbsno)) {
    $cbscond = " AND `memno` LIKE '%$cbsno%' ";
}
if (!empty($oldno)) {
    $oldcond = " AND `oldno` LIKE '%$oldno%' ";
}
if (!empty($orderby)) {
    if ($orderby == "1") {
        $orderbycond = " order by name";
    } else {
        $orderbycond = " order by oldno";
    }
}
if ($removdup == "1") {
    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`!='' $regioncond $cbscond $oldcond $namecond $addresscond $brnocond $membercond $pincond $citycond $areacond $yearnocond  $groupmastcond $agm50cond $agm51cond $agm52cond $agm53cond $splagmcond group by brno $orderbycond") or die(mysqli_error($conn));
} else {

    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `accode`!='' $regioncond $cbscond $oldcond $namecond $addresscond $brnocond $membercond $pincond $citycond $areacond $yearnocond $groupmastcond $agm50cond $agm51cond $agm52cond $agm53cond $splagmcond $orderbycond") or die(mysqli_error($conn));
}

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

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

    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()" class="btn btn-primary">Go Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr" style="">
                            <th>Sr</th>
                            <th>Cust No.</th>
                            <!--<th>Old No.</th>-->
                            <th>Br No.</th>
                            <?php if ($removdup != '1') { ?>
                                <!--<th>Year No.</th>-->
                            <?php } ?>
                            <!--<th>Join Date</th>-->
                            <th>Cust Id.</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Region</th>
                            <th>Area</th>
                            <th>Email</th>
                            <th>Pin</th>
                            <th>Tel/Mobile</th>
                            <th>AGM 50</th>
                            <th>AGM 51</th>
                            <th>AGM 52</th>
                            <th>AGM 53</th>
                            <th>SPL AGM</th>
                            <th>Shares</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        //                        browse($ctr);
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            echo "<tr>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[memno]</td>";
                            //                            Echo "<td>$pmain[oldno]</td>";
                            echo "<td>$pmain[brno]</td>";
                            if ($removdup != '1') {
                                //                                Echo "<td>$pmain[yearno]</td>";
                            }
                            //                            Echo "<td>$pmain[joindate]</td>";
                            echo "<td>$pmain[cbsno]</td>";
                            echo "<td>$pmain[name]</td>";
                            echo "<td>$pmain[add1] $pmain[add2] $pmain[add3] </td>";
                            echo "<td>$pmain[region]</td>";
                            echo "<td>$pmain[station]</td>";
                            echo "<td>$pmain[email]</td>";
                            echo "<td>$pmain[pin]</td>";
                            echo "<td>$pmain[tel]  $pmain[mobile]</td>";
                            echo "<td class='text-center'>$pmain[agm50]</td>";
                            echo "<td class='text-center'>$pmain[agm51]</td>";
                            echo "<td class='text-center'>$pmain[agm52]</td>";
                            echo "<td class='text-center'>$pmain[agm53]</td>";
                            echo "<td class='text-center'>$pmain[splagm]</td>";
                            echo "<td class='text-center'>$pmain[shareqty]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}

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

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf
    {

        function Header()
        {
        }

        function Footer()
        {

            //Position at 1.5 cm from bottom
            $this->SetY(-15);
            //Arial italic 8
            $this->SetFont('Arial', 'I', 7);
            //Page number
            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    //    $pdf->SetMargins(10, 10, 10);
    //  $pdf->SetXY(7, 5);

    $pdf->SetFillColor(255, 255, 255);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(285, 7, $formname, 0, 0, 'C', 1);
    $pdf->ln();

    $pdf->SetTextColor(255, 255, 255);
    $pdf->SetFillColor(96, 130, 191);

    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Cell(7, 7, 'Sr.', 1, 0, '', 1);
    $pdf->Cell(12, 7, 'Cust No', 1, 0, '', 1);
    $pdf->Cell(10, 7, 'Br No.', 1, 0, '', 1);
    $pdf->Cell(10, 7, 'Cust Id.', 1, 0, '', 1);
    $pdf->Cell(45, 7, 'Name', 1, 0, '', 1);
    $pdf->Cell(120, 7, 'Address', 1, 0, '', 1);
    $pdf->Cell(10, 7, 'Region', 1, 0, '', 1);
    // $pdf->Cell(10, 7, 'Area', 1, 0, '', 1);
    // $pdf->Cell(25, 7, 'Email', 1, 0, '', 1);
    // $pdf->Cell(8, 7, 'Pin', 1, 0, '', 1);
    $pdf->Cell(25, 7, 'Tel/Mobile', 1, 0, '', 1);
    $pdf->Cell(5, 7, '50', 1, 0, '', 1);
    $pdf->Cell(5, 7, '51', 1, 0, '', 1);
    $pdf->Cell(5, 7, '52', 1, 0, '', 1);
    $pdf->Cell(5, 7, '53', 1, 0, '', 1);
    $pdf->SetFont('Arial', 'B', 6);
    $pdf->Cell(5, 7, 'SP', 1, 0, '', 1);
    $pdf->SetFont('Arial', 'B', 7);
    $pdf->Cell(10, 7, 'Shares', 1, 0, '', 1);

    $pdf->SetFont('Arial', '', 1, 0);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Ln();

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;
        $pdf->SetFont('Arial', '', 7);
        $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

        $pdf->Cell(7, 7, $ctr, 1, 0);
        $pdf->Cell(12, 7, $pmain['memno'], 1, 0);
        $pdf->SetFont('Arial', '', 6);
        $pdf->Cell(10, 7, $pmain['brno'], 1, 0);
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, 7, $pmain['cbsno'], 1, 0, "L");
        $pdf->SetFont('Arial', '', 6);
        $pdf->Cell(45, 7, $pmain['name'], 1, 0);
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(120, 7, $address, 1, 0);
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, 7, $pmain['region'], 1, 0);
        $pdf->SetFont('Arial', '', 6);
        // $pdf->Cell(10, 7, $pmain['station'], 1, 0);
        // $pdf->SetFont('Arial', '', 4);
        // $pdf->Cell(25, 7, $pmain['email'], 1, 0);
        // $pdf->Cell(8, 7, $pmain['pin'], 1, 0, 'R');
        $pdf->Cell(25, 7, $pmain['tel'] . $pmain['mobile'], 1, 0);
        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(5, 7, $pmain['agm50'], 1, 0);
        $pdf->Cell(5, 7, $pmain['agm51'], 1, 0);
        $pdf->Cell(5, 7, $pmain['agm52'], 1, 0);
        $pdf->Cell(5, 7, $pmain['agm53'], 1, 0);
        $pdf->Cell(5, 7, $pmain['splagm'], 1, 0);
        $pdf->SetFont('Arial', '', 7);
        $pdf->Cell(10, 7, $pmain['shareqty'], 1, 0);
        $pdf->Ln();
    }

    $pdf->Output("Member Print.pdf", "I");

}

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

    $contents .= "Date - , ";
    $contents .=  Date("d-m-Y") . "\n";

    $contents .= "Print - , ";
    $contents .=  "$formname" . "\n";

    $contents .= "Sr, Cust No., Br No, Cust Id., Name, Address, Region, Area, Email, Pin, Tel/Mobile, AGM 50, AGM 51, AGM 52, AGM53,SPL AGM, Shares\n";

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

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[memno]" . ",";
        $contents .= "$pmain[brno]" . ",";
        $contents .= "$pmain[cbsno]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[add1]" . "$pmain[add2]" . "$pmain[add3]" . ",";
        $contents .= "$pmain[region]" . ",";
        $contents .= "$pmain[station]" . ",";
        $contents .= "$pmain[email]" . ",";
        $contents .= "$pmain[pin]" . ",";
        $contents .= "$pmain[tel]" . "$pmain[mobile]" . ",";
        $contents .= "$pmain[agm50]" . ",";
        $contents .= "$pmain[agm51]" . ",";
        $contents .= "$pmain[agm52]" . ",";
        $contents .= "$pmain[agm53]" . ",";
        $contents .= "$pmain[splagm]" . ",";
        $contents .= "$pmain[shareqty]" . "\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;
    }
}
?>