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

$maindb = "medihelp";
$formname = "Member Register";
$file = "medihelprep_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdbroot"];

mysqli_select_db($conn, $zdb);

$memno = $_POST["memno"];
$memname = $_POST["memname"];
$gender = $_POST["gender"];
$occupation = $_POST["occupation"];
$scope = $_POST["scope"];

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);

$memnocond = "";
$memnamecond = "";
$gendercond = "";
$occupationcond = "";

if (empty($_POST["fromdt"])) {
    browse("From Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    echo "<script>window.close()</script>";
}

if ($scope == "P") {
    $scopecond = " AND `sancamt` = '' ";
} elseif ($scope == "S") {
    $scopecond = " AND `sancamt` != '' ";
} else {
    $scopecond = "";
}



if (!empty($memno)) {
    $memnocond = " AND `memno`='$memno ' ";
}
if (!empty($memname)) {
    $memnamecond = " AND `memname`='$memname ' ";
}
if (!empty($gender)) {
    $gendercond = " AND `gender`='$gender ' ";
}
if (!empty($occupation)) {
    $occupationcond = " AND `occupation`='$occupation ' ";
}


$getrecp = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt,DATE_FORMAT(`sancdt`, '%d-%m-%Y')as sancdt
                                        FROM `$maindb` WHERE vouchno != '' AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                        $memnocond $memnamecond $gendercond $occupationcond $scopecond  order by vouchno") or die(mysqli_error($conn));

$get = mysqli_num_rows($getrecp);

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

    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;
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>

    <body>
        <div class="pad">
            <?php include("includes/head_rep.php"); ?>
            <div class="row pad">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:2%">Sr No.</th>
                                <th style="width:10%">Date</th>
                                <th style="width:4%;">Member No</th>
                                <th style="width:12%;">Member Name</th>
                                <th style="width:12%;">Address</th>
                                <th style="width:5%;">Married Status</th>
                                <th style="width:5%">Mobile</th>
                                <th style="width:4%;">Blood Group</th>
                                <th style="width:10%;">Qualification</th>
                                <th style="width:10%;">Occupation</th>
                                <th style="width:5%;">Physically Disabled</th>
                                <th style="width:10%;">Reason</th>
                                <th style="width:10%;">Sanctioned</th>
                                <th style="width:10%;">Sanctioned Date</th>
                                <th style="width:10%;">Sanctioned By</th>
                                <th style="width:10%;">Received By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            while ($pmain = mysqli_fetch_array($getrecp)) {

                                $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$pmain[memno]</td>";
                                echo "<td>$pmain[memname]</td>";
                                echo "<td>$address</td>";
                                echo "<td>$pmain[marstat]</td>";
                                echo "<td>$pmain[mobile]</td>";
                                echo "<td>$pmain[bloodgrp]</td>";
                                echo "<td>$pmain[qualification]</td>";
                                echo "<td>$pmain[occupation]</td>";
                                echo "<td>$pmain[phydis]</td>";
                                echo "<td>$pmain[reqreason] $pmain[reqreason2]</td>";
                                echo "<td style='text-align:right'>$pmain[sancamt]</td>";
                                echo "<td>$pmain[sancdt]</td>";
                                echo "<td>$pmain[givenby]</td>";
                                echo "<td>$pmain[recvby]</td>";
                                echo "</tr>";


                                $sancamttot = $pmain['sancamt'] + $sancamttot;

                                $sancamt = number_format($sancamttot, 2);

                                echo "</tr>";
                            }
                            echo "<tr id='boldfont'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;color:#A8B97D;font-size:18px'>Total</td>";
                            echo "<td style='text-align:right;color:#A8B97D;font-size:18px'>$sancamt</td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            
                            echo "</tr>";


                            ?>
                        </tbody>

                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}

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

    $contents .= "Sr No.,Date,Member No,Member Name,Address, Married Status,Mobile,Blood Group,Qualification,Occupation,Physically Disabled,Reason,Sanctioned,Sanctioned,Sanctioned Date,Sanctioned By,Received By\n";
            
        $ctr = 0;
        while ($pmain = mysqli_fetch_array($getrecp)) {

            $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

            $ctr++;

            $contents .= "$ctr" . ",";
            $contents .= "$pmain[vouchdt]" . ",";
            $contents .= "$pmain[memno]" . ",";
            $contents .= "$pmain[memname]" . ",";
            $contents .= "$address" . ",";
            $contents .= "$pmain[marstat]" . ",";
            $contents .= "$pmain[mobile]" . ",";
            $contents .= "$pmain[bloodgrp]" . ",";
            $contents .= "$pmain[qualification]" . ",";
            $contents .= "$pmain[occupation]" . ",";
            $contents .= "$pmain[phydis]" . ",";
            $contents .= "$pmain[reqreason]" . "$pmain[reqreason2]" . ",";
            $contents .= "$pmain[sancamt]" . ",";
            $contents .= "$pmain[sancdt]" . ",";
            $contents .= "$pmain[givenby]" . ",";
            $contents .= "$pmain[recvby]" . "\n";

            $sancamttot = $pmain['sancamt'] + $sancamttot;

        }

        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "Total". ",";
        $contents .= "$sancamttot". ",";
        $contents .= "". ",";
        $contents .= "". ",";
        $contents .= "". "\n";


    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}


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

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

    class pdf extends fpdf
    {

        function Footer()
        {

            $this->SetY(-5);

            $this->SetFont('Arial', 'I', 8);
            $this->Cell(0, 5, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

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

    $pdf->AliasNbPages();
    //    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->SetTextColor(0, 0, 0);
    $ht = 6;

    $pdf->AddPage();
    $pdf->SetFont('Arial', '', 7);
    $pdf->Cell(10, $ht, "Member", 'TLR', 0, 'C');
    $pdf->Cell(9, $ht, "Family", 'TLR', 0, 'C');
    $pdf->Cell(11, $ht, "Surname", 'TLR', 0, 'C');
    $pdf->Cell(35, $ht, "Name", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "Gender", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "Status", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "DOB", 'TLR', 0, 'L');
    $pdf->Cell(6, $ht, "Age", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "Mobile", 'TLR', 0, 'L');
    $pdf->Cell(14, $ht, "Qualification", 'TLR', 0, 'C');
    $pdf->Cell(17, $ht, "Profession", 'TLR', 0, 'L');
    $pdf->Cell(74, $ht, "Address", 'TLR', 0, 'L');
    $pdf->Cell(10, $ht, "City", 'TLR', 0, 'L');
    $pdf->Cell(11, $ht, "Taluka", 'TLR', 0, 'L');
    $pdf->Cell(16, $ht, "District", 'TLR', 0, 'L');
    if ($phydisbaled == 'YES') {
        $pdf->Cell(15, $ht, "State", 'TLR', 0, 'L');
        $pdf->SetFont('Arial', '', 5);

        $pdf->Cell(10, $ht, "Physically", 'TLR', 0, 'L');
    } else {
        $pdf->SetFont('Arial', '', 7);

        $pdf->Cell(15, $ht, "State", 'TLR', 0, 'L');
    }
    $pdf->ln(3);
    $pdf->Cell(10, $ht, "No.", 'LBR', 0, 'L');
    $pdf->Cell(9, $ht, "ID", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(35, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(6, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(14, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(17, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(74, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(10, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(11, $ht, "", 'LBR', 0, 'L');
    $pdf->Cell(16, $ht, "", 'LBR', 0, 'L');
    if ($phydisbaled == 'YES') {
        $pdf->Cell(15, $ht, "", 'LBR', 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "Disabled", 'LBR', 0, 'L');
    } else {
        $pdf->SetFont('Arial', '', 7);

        $pdf->Cell(15, $ht, "", 'LBR', 0, 'L');
    }
    $pdf->ln($ht);

    $ctr = 0;
    while ($rmain = mysqli_fetch_array($getrecp)) {

        if ($rmain['mstatus'] == 'M') {
            $mstatus = 'MARRIED';
        } elseif ($rmain['mstatus'] == 'S') {
            $mstatus = 'SINGLE';
        } elseif ($rmain['mstatus'] == 'D') {
            $mstatus = 'DIVORCE';
        } elseif ($rmain['mstatus'] == 'W') {
            $mstatus = 'WIDOW';
        } else {
            $mstatus = '';
        }

        if ($rmain['gender'] == 'M') {
            $gender = 'MALE';
        } elseif ($rmain['gender'] == 'F') {
            $gender = 'FEMALE';
        }

        $current_year = date('Y');
        $dob = date_parse($rmain['dob']);
        $year = $dob["year"];
        $age = ($current_year - $year);

        if ($rmain['dob'] == '00-00-0000') {
            $age1 = "";
        } else {
            $age1 = $age;
        }

        if ($rmain['dob'] == '00-00-0000') {
            $dob = "";
        } else {
            $dob = $rmain['dob'];
        }
        if ($rmain['phydisabled'] == 'YES') {
            $phydis = $rmain['phydisabledy'];
        } else {
            $phydis = $rmain['phydisabled'];
        }

        $capsurname = strtoupper($rmain['surname']);
        $capname = strtoupper($rmain['name'] . ' ' . $rmain['name2']);
        $address1 = $rmain['add1'] . $rmain['add2'] . $rmain['add3'];
        $address = str_replace(",", " ", $address1);

        $ctr++;

        $pdf->SetFont('Arial', '', 6);
        $pdf->Cell(10, $ht, "$rmain[accode]", 1, 0, 'L');
        $pdf->Cell(9, $ht, "$rmain[familyid]", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$capsurname", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(35, $ht, "$capname", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "$gender", 1, 0, 'L');
        $pdf->Cell(10, $ht, "$mstatus", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$dob", 1, 0, 'L');
        $pdf->Cell(6, $ht, "$age1", 1, 0, 'C');
        $pdf->Cell(11, $ht, "$rmain[mobile]", 1, 0, 'C');
        $pdf->Cell(14, $ht, "$rmain[qualification]", 1, 0, 'L');
        $pdf->Cell(17, $ht, "$rmain[profession]", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 4);
        $pdf->Cell(74, $ht, "$address", 1, 0, 'L');
        $pdf->SetFont('Arial', '', 5);
        $pdf->Cell(10, $ht, "$rmain[city]", 1, 0, 'L');
        $pdf->Cell(11, $ht, "$rmain[taluka]", 1, 0, 'L');
        $pdf->Cell(16, $ht, "$rmain[district]", 1, 0, 'L');
        if ($phydisbaled == 'YES') {
            $pdf->Cell(15, $ht, "$rmain[area]", 1, 0, 'L');
            $pdf->Cell(10, $ht, "$phydis", 1, 0, 'L');
        } else {
            $pdf->Cell(15, $ht, "$rmain[area]", 1, 0, 'L');
        }
        $pdf->SetFont('Arial', '', 6);
        $pdf->ln($ht);
    }
    $pdf->Output("$formname" . ".pdf", "I");
}
?>