<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
//header('Content-Type: application/json');

/////////////// Encryption for security and prevent unauthorized access by student(issue in SSD) 10-02-2023 ///////////////////////

$_GET['class']   = decryptMessage($_GET['p1']);
$_GET['idno']    = decryptMessage($_GET['p2']);
$_GET['session'] = decryptMessage($_GET['p3']);
$_GET['exam']    = decryptMessage($_GET['p4']);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$timeuniquedate = $db->getRows("SELECT DISTINCT examDate FROM examTimeTable where session='" . $_GET['session'] . "' and class='" . $_GET["class"] . "' and type='" . $_GET["exam"] . "' ORDER BY examDate ASC");

$aryForm = $db->getRow("SELECT * FROM student WHERE idno='" . $_GET["idno"] . "' AND session='" . $_GET["session"] . "' AND class='" . $_GET["class"] . "' AND status=1 and issue_TC=0 order by name");

$m = $_REQUEST["month"];
for ($i = 1; $i <= $m; $i++) {
    $md[] = 'COALESCE(i' . $i . ',0)';
    $md1[] = 'COALESCE(ip' . $i . ',0)';
    $pid[] = "'p" . $i . "'";
    $pm[] = "'" . $i . "'";
    $mdb[] = 'COALESCE(b' . $i . ',0)';
    $mdb1[] = 'COALESCE(bp' . $i . ',0)';
}

$mdf = implode('+', $md);
$mdf1 = implode('+', $md1);

$mdbf = implode('+', $mdb);
$mdbf1 = implode('+', $mdb1);

$pid1 = implode(',', $pid);
$pm1 = implode(',', $pm);


//////////////////////////////

function decryptMessage($encryptedString)
{
    $iv = "@@@@&&&&####$$$$";
    $key = 'lLvM1QLNMigKIpVX';

    $key = html_entity_decode($key);

    if (function_exists('openssl_decrypt')) {
        $data = openssl_decrypt($encryptedString, "AES-128-CBC", $key, 0, $iv);
    } else {
        $encrypted = base64_decode($encryptedString);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
        mcrypt_generic_init($td, $key, $iv);
        $data = mdecrypt_generic($td, $encrypted);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = pkcs5Unpad($data);
        $data = rtrim($data);
    }
    return $data;
}
function pkcs5Unpad($text) {
    $pad = ord($text[strlen($text) - 1]);
    if ($pad > strlen($text))
        return false;
    return substr($text, 0, -1 * $pad);
}

?>
<div class="out" style="margin-top:20px;margin-bottom:30px;padding:5px 20px;font-size:10px;">
    <div class="headPrint" style="text-align:center">
        <?php if ($aryForm['photo'] != "") { ?> <img
                src="<?php echo URL_ROOT ?>media/photo/<?php echo $aryForm["photo"] ?>" style="float:right"
                height="110px"> <?php } ?>
        <h1><img src="<?php echo $LinksDetails["logo"] ?>" height="55px" style="float:left;">
            <span style="top: 5px; margin-left:5px; display: inline-block; position: relative;color:#F00; font-size:35px;"><?php echo ucfirst(trim($LinksDetails["site_name"])) ?></span>
        </h1>
        <div style="color:#039; margin-top:8px; margin-left:125px; font-size:15px;"><?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></div>
        <br/>

        <br><br><br><br>
        <div style="color:#F00;font-size:19px; font-weight:bold;">ADMIT CARD</div>
        <div style="color:#077D14;font-size:12px; font-weight:bold;"><?php echo strtoupper($_GET['exam']); ?>
            EXAMINATION (<?php echo $_GET['session'] ?>)
        </div>
    </div>
    <br><br>
    <table style="width:100%;font-size:14px;font-weight: bold;color:#1224A5;float:left" cellpadding="0" cellspacing="0">
        <tr>
            <td width="20%">Name</td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["name"] ?></td>
            <td width="20%"><strong class="st">Admission No.</strong></td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["admissionno"] ?></td>
        </tr>
        <tr>
            <td width="20%">Class</td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["class"] . "/" . $aryForm["section"]; ?></td>
            <td width="20%">Roll No</td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["foccupation"] ?></td>
        </tr>
        <tr>
            <td width="20%"><strong class="st">Father's Name</strong></td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["fname"] ?></td>
            <td width="20%"><strong class="st">D.O.B</strong></td>
            <td width="2%">:</td>
            <td width="21%"><?php // echo  date("d-m-Y",strtotime($aryForm["dob"])) ?></td>

        </tr>
        <tr>
            <td width="21%"><strong class="st">Mother's Name</strong></td>
            <td width="2%">:</td>
            <td width="38%"><?php echo $aryForm["mname"] ?></td>
            <td width="21%"><strong class="st">Contact</strong></td>
            <td width="2%">:</td>
            <td width="21%"><?php echo $aryForm["mobile"] ?></td>
        </tr>
    </table>

    <table class="table" style="width:100%;font-size:13px; font-weight: bold; color:#1224A5;" border="1" cellpadding="2"
           cellspacing="0">
        <tr>
            <th>DATE</th>
            <th>DAY</th>
            <th>Subject 1st Sitting</th>
            <th>Subject 2nd Sitting</th>
        </tr>
        <?php
        foreach ($timeuniquedate as $table) {
            ?>
            <tr>
                <th><?php echo date("d-m-Y", strtotime($table["examDate"])); ?></th>
                <th><?php echo date("l", strtotime($table['examDate'])); ?></th>
                <th><?php $timesittingplan1 = $db->getval("SELECT subject FROM examTimeTable where session='" . $_GET['session'] . "' and class='" . $_GET["class"] . "' and type='" . $_GET["exam"] . "' and examDate='" . $table["examDate"] . "' and seating_plan='1st Seating' ");
                    if ($timesittingplan1) {
                        echo $timesittingplan1;
                    } else {
                        echo '-';
                    } ?></th>

                <th><?php $timesittingplan = $db->getval("SELECT subject FROM examTimeTable where session='" . $_GET['session'] . "' and class='" . $_GET["class"] . "' and type='" . $_GET["exam"] . "' and examDate='" . $table["examDate"] . "' and seating_plan='2nd Seating' ");
                    if ($timesittingplan) {
                        echo $timesittingplan;
                    } else {
                        echo '-';
                    } ?>
                </th>
            </tr>
        <?php } ?>
    </table>
    <ol style="color:#1224A5;">
        <?php
        //echo $_REQUEST["month"];

        $monthsList1 = array(
            1 => 'Apr',
            2 => 'May',
            3 => 'Jun',
            4 => 'Jul ',
            5 => 'Aug',
            6 => 'Sep',
            7 => 'Oct',
            8 => 'Nov',
            9 => 'Dec',
            10 => 'Jan',
            11 => 'Feb',
            12 => 'Mar'
        );

        $cls = $aryForm["class"];
        $type = $aryForm["stype"];
        $h_type = $aryForm["htype"];
        $_REQUEST[id] = $aryForm["idno"];

        $extrafee_list = $db->getRows("SELECT id from extrafee_type  WHERE class='$cls' and session='" . $_GET['session'] . "' ");

        foreach ($extrafee_list as $extra_list) {

            $extra_list1[] = $extra_list[0];

        }
        //print_r($extra_list1);
        $extralist = implode(',', $extra_list1);

        //echo '-';
        for ($i = 1; $i <= $m; $i++) {


            $total = $db->getVal("SELECT COALESCE(sum(i" . $i . "),0) as result from installment  WHERE idno='" . $_REQUEST[id] . "' and session='" . $_GET['session'] . "' and (ip" . $i . "='' OR ip" . $i . " IS NULL ) ");


            $total_bus = $db->getVal("SELECT COALESCE(sum(b" . $i . "),0)  as result from installment  WHERE idno='" . $_REQUEST[id] . "' and session='" . $_GET['session'] . "' and (bp" . $i . "='' OR bp" . $i . " IS NULL )");

            $total_extra = $db->getVal("SELECT sum(fee) as result from extrafee  WHERE type in ($extralist) and pid='p" . $i . "' and idno='" . $_REQUEST[id] . "' and session='" . $_GET['session'] . "' and (pay='' OR pay IS NULL)");

            //if($bpl==0){
            $total_other = $db->getVal("SELECT COALESCE(sum(fee),0) as result from otherfee_structure  WHERE class='$cls' and month='$i' and (otype='" . $_REQUEST[id] . "' or otype='$type' or otype='' ) and session='" . $_GET['session'] . "' ");

            $total_other_discount = $db->getVal("SELECT COALESCE(sum(discount),0) as result from otherfee_discount  WHERE  idno='" . $_REQUEST[id] . "' and month='$i' and class='$cls' and session='" . $_GET['session'] . "' ");
            //}
            // other fee given amt start
            $other_fee_name = $db->getRows("SELECT name from otherfee_structure  WHERE class='$cls' and month='$i' and (otype='" . $_REQUEST[id] . "' or otype='$type' or otype='') and session='" . $_GET['session'] . "' ");

            foreach ($other_fee_name as $other_fee_name1) {

                $other_fee_name1[name];
                $string .= "'$other_fee_name1[name]'" . ',';

            }

            $total_pay += $total + $total_bus + $total_extra + $total_other - $total_other_discount;

            //$total_pay+=$total_other-$total_other_discount;

        }

        $string = rtrim($string, ',');

        $total_other_given = $db->getVal("SELECT COALESCE(sum(fine),0) as result from installment_fine  WHERE  idno='" . $_REQUEST[id] . "' and reasion in($string)   and session='" . $_GET['session'] . "' ");

        $tf = $total_pay - $total_other_given;

        if ($m == 12) {
            $total23 = $db->getVal("select (COALESCE(i13,0)+COALESCE(b13,0)) from installment where idno='" . $aryForm["idno"] . "'");


            $total24 = $db->getVal("select (COALESCE(i13,0)) from hostelinstallment where idno='" . $aryForm["idno"] . "'");

            $te = $db->getVal("SELECT sum(fee-pay) FROM `extrafee` WHERE idno='" . $aryForm["idno"] . "' and pid='p12' and pay!='0'");
            $total22 = $total23 + $total24 + $te;
        }


        // hostel

        for ($i = 1; $i <= $m; $i++) {

            $h_total = $db->getVal("SELECT COALESCE(sum(i" . $i . "),0) as result from hostelinstallment  WHERE idno='" . $_REQUEST[id] . "' and session='" . $_GET['session'] . "' and (ip" . $i . "='' OR ip" . $i . " IS NULL ) ");

            $total_hostel_other = $db->getVal("SELECT COALESCE(sum(fee),0) as result from hostel_otherfee_structure  WHERE class='$cls' and month='$i' and (otype='" . $_REQUEST[id] . "' or otype='$h_type' or otype='' ) and session='" . $_GET['session'] . "' ");

            $total_hostel_other_discount = $db->getVal("SELECT COALESCE(sum(discount),0) as result from hostelotherfee_discount  WHERE  idno='" . $_REQUEST[id] . "' and month='$i'   and session='" . $_GET['session'] . "' ");

            $h_other_fee_name = $db->getRows("SELECT name from hostel_otherfee_structure  WHERE class='$cls' and month='$i' and (otype='" . $_REQUEST[id] . "' or otype='$h_type' or otype='') and session='" . $_GET['session'] . "' ");

            foreach ($h_other_fee_name as $h_other_fee_name1) {

                $h_other_fee_name1[name];
                $h_string .= "'$h_other_fee_name1[name]'" . ',';
            }

            $h_total_pay += $h_total + $total_hostel_other - $total_hostel_other_discount;
        }
        $h_string = rtrim($h_string, ',');

        $h_total_other_given = $db->getVal("SELECT COALESCE(sum(fine),0) as result from installment_fine  WHERE  idno='" . $_REQUEST[id] . "' and reasion in($h_string)   and session='" . $_GET['session'] . "' ");

        $h = $h_total_pay - $h_total_other_given;

        ?>
        <center><h1>Dues upto <?= $monthsList1[$m] ?>- <?php

                echo $tf + $total22 + $h;
                //echo  $tf;
                //echo  $h;

                unset($total);
                unset($h);
                unset($h_total);
                unset($total22);
                unset($total_other_given);
                unset($h_total_other_given);
                unset($total_pay);
                unset($h_total_pay);
                unset($tf);
                unset($string);
                unset($h_string);


                ?> /-</h1></center>
    </ol>
    <br>
    <table width="100%" cellspacing="0" cellpadding="0" style="color:#FF0127; font-size:13px; font-weight: bold;">
        <tr>
            <td> Examination Controller</td>
            <td>Clerk Signature</td>
            <td>Principal</td>
        </tr>
    </table>


    <!--<strong style="color:#FF0127"><?php echo nl2br($LinksDetails["site_name"]) ?></strong><br>-->
</div>

<script>
    window.print();
    //window.onfocus=function(){ window.close();}
</script>