<?php
session_start();

include("../../php_crud/my_class.php");
$obj=new my_class();
extract($_POST);
function getMonthName($i)
{
    if($i=="1") { return "January";}
    else if($i=="2") { return "February";}
    else if($i=="3") { return "March";}
    else if($i=="4") { return "April";}
    else if($i=="5") { return "May";}
    else if($i=="6") { return "June";}
    else if($i=="7") { return "July";}
    else if($i=="8") { return "August";}
    else if($i=="9") { return "September";}
    else if($i=="10") { return "October";}
    else if($i=="11") { return "November";}
    else if($i=="12") { return "December";}
    else { return "Unknown";}
}
function randString($length,$id)
{
    $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $str = '';
    $count = strlen($charset);
    while ($length--) {
        $str .= $charset[mt_rand(0, $count-1)];
    }

    return $str.time().$id;
}
if($function=='fee_rollback'){

    $table_name="`_fee_master_invoice` a LEFT JOIN `_pf_std_personal_info` b ON a.`_std_id`=b.`_id`";
    $column="_invoice_no,_month,_year,_total_amount,_due_amount,_paid_amount,a._status master_status,b.`_full_name`,b.`_contact_mobile`,b.`_gender`,b.`_uniq_id`";
    $where_cond="a._id='$id'";
    $fee_master_invoice_data=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
 $_invoice_no=$fee_master_invoice_data[0]["_invoice_no"];
 $_month=$fee_master_invoice_data[0]["_month"];
 $_year=$fee_master_invoice_data[0]["_year"];
 $_total_amount=$fee_master_invoice_data[0]["_total_amount"];
 $_due_amount=$fee_master_invoice_data[0]["_due_amount"];
 $_paid_amount=$fee_master_invoice_data[0]["_paid_amount"];
 $_status=$fee_master_invoice_data[0]["master_status"];
 $_full_name=$fee_master_invoice_data[0]["_full_name"];
 $_contact_mobile=$fee_master_invoice_data[0]["_contact_mobile"];

 $_uniq_id=$fee_master_invoice_data[0]["_uniq_id"];
    if($_status=="U")
    {
        $nstatus_text="Unpaid";
        $nstatus="<div class='alert alert-info center' role='alert'>Rollback is not available</div>";
    }
    else if($_status=="P")
    {
        $nstatus_text="Paid";

        $nstatus="<button class='btn btn-small btn-success'  onclick='fee_rollback_confirm($id,$_due_amount)' data-dismiss='modal'>Confirm</button>";
    }
    else if($_status=="PR")
    {
        $nstatus_text="Partially Paid";
        $nstatus="<button class='btn btn-small btn-success'  onclick='fee_rollback_confirm($id,$_due_amount)' data-dismiss='modal'>Confirm</button>";
    }
 ?>

                <div class="modal-body">
                    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Invoice No:</b> '.$_invoice_no;?></span>
                    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Month:</b> '.getMonthName($_month);?></span>
                    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Session:</b> '.$_year;?></span>


                    <div class="panel panel-info" style="margin-top: 10px;">
                        <div class="panel-body">
<table class="table">
    <tr>
        <td><table class="table table-striped table-bordered">
                <tr><td>Student ID</td> <td><b><?php echo $_uniq_id;?></b></td></tr>
                <tr><td>Name</td> <td><b><?php echo $_full_name;?></b></td></tr>
                <tr><td>Total</td> <td><b><?php echo $_total_amount;?></b></td></tr>
                <tr><td>Due</td> <td><b><?php echo $_due_amount;?></b></td></tr>
                <tr><td>Paid</td> <td><b><?php echo $_paid_amount;?></b></td></tr>
                <tr><td>Status</td> <td><b><?php echo $nstatus_text;?></b></td></tr>





            </table></td>
        <td>
            <?php

            $table_name="_fee_details_invoice";
            $column="*";
            $where_cond="_invoice_no='$_invoice_no'";

            ?>
            <table class="table table-striped table-bordered">
                <?php
                foreach ($obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond) as $value)
                {
                    extract($value);
                    echo "<tr><td>$_fee_head</td> <td><b>$_amount</b></td></tr>";
                }
                ?>








            </table>

        </td>
    </tr>

</table>





                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <?php echo $nstatus;?>
                </div>
<?php

}

if($function=='fee_rollback_confirm'){

    $table_name="`_fee_master_invoice` ";
    $column="*";
    $where_cond="_id='$id'";
    $fee_master_invoice_data=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
    $date = date('Y-m-d');

    $_br_id=$fee_master_invoice_data[0]["_br_id"];
    $_class_id=$fee_master_invoice_data[0]["_class_id"];
    $_section_id=$fee_master_invoice_data[0]["_section_id"];
    $_std_id=$fee_master_invoice_data[0]["_std_id"];
    $_invoice_no=$fee_master_invoice_data[0]["_invoice_no"];
    $_month=$fee_master_invoice_data[0]["_month"];
    $_year=$fee_master_invoice_data[0]["_year"];
    $_total_amount=$fee_master_invoice_data[0]["_total_amount"];
    $_due_amount=$fee_master_invoice_data[0]["_due_amount"];
    $_paid_amount=$fee_master_invoice_data[0]["_paid_amount"];
    $_status=$fee_master_invoice_data[0]["_status"];

    ?>
    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Invoice No:</b> '.$_invoice_no;?></span>
    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Month:</b> '.getMonthName($_month);?></span>
    <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Session:</b> '.$_year;?></span>
    <br/><br/>
    <?php
    if($_status!="U")
    {


        $table_name="_fee_details_invoice";
        $column="*";
        $where_cond="_invoice_no='$_invoice_no'";
        foreach ($obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond) as $value)
        {

            $form_data=array(
                '_adjust_amount'=>"0"
            );
            $update=$obj->Update_Data('_fee_details_invoice', $form_data, "_invoice_no='".$_invoice_no."' AND _month='$_month' AND _year='$_year' and `_fee_head`='".$value["_fee_head"]."'  ");
        }


        $entry=$obj->Delete_Data('_fee_details_invoice', "_fee_head='Discount' and _invoice_no='".$_invoice_no."'");
        $entry=$obj->Delete_Data('_fee_total_receive', "_invoice_no='".$_invoice_no."'");



        $form_data=array(
            '_due_amount'=>$_total_amount,
            '_paid_amount'=>"0",
            '_last_paid_amount'=>"0",
            '_last_payment_date'=>$date,
            '_discount'=>"0",
            '_status'=>'U'
        );
        $update=$obj->Update_Data('_fee_master_invoice', $form_data, "_id=".$id);



        echo "<div class='alert alert-success' role='alert'>Payment Rollback Successful</b></div>";
    }
    else
    {
        echo "<div class='alert alert-danger' role='alert'>Unknown Request</div>";
    }
}


if($function=='fee_monthly_edit'){

    $table_name="`_fee_master_invoice` a LEFT JOIN `_pf_std_personal_info` b ON a.`_std_id`=b.`_id`";
    $column="_invoice_no,_month,_year,_total_amount,_due_amount,_paid_amount,a._status master_status,b.`_full_name`,b.`_contact_mobile`,b.`_gender`,b.`_uniq_id`";
    $where_cond="a._id='$id'";
    $fee_master_invoice_data=$obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond);
    $_invoice_no=$fee_master_invoice_data[0]["_invoice_no"];
    $_month=$fee_master_invoice_data[0]["_month"];
    $_year=$fee_master_invoice_data[0]["_year"];
    $_total_amount=$fee_master_invoice_data[0]["_total_amount"];
    $_due_amount=$fee_master_invoice_data[0]["_due_amount"];
    $_paid_amount=$fee_master_invoice_data[0]["_paid_amount"];
    $_status=$fee_master_invoice_data[0]["master_status"];
    $_full_name=$fee_master_invoice_data[0]["_full_name"];
    $_contact_mobile=$fee_master_invoice_data[0]["_contact_mobile"];

    $_uniq_id=$fee_master_invoice_data[0]["_uniq_id"];
    if($_status=="U")
    {
        $nstatus_text="Unpaid";
        $nstatus="<button class='btn btn-small btn-success'  onclick='fee_monthly_modify_confirm($id,$_due_amount)' data-dismiss='modal'>Confirm</button>";
    }
    else if($_status=="P")
    {
        $nstatus_text="Paid";
        $nstatus="<div class='alert alert-info center' role='alert'>You can only modify when it is unpaid</div>";

    }
    else if($_status=="PR")
    {
        $nstatus_text="Partially Paid";
        $nstatus="<div class='alert alert-info center' role='alert'>You can only modify when it is unpaid</div>";
    }
    ?>

    <div class="modal-body">
        <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Invoice No:</b> '.$_invoice_no;?></span>
        <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Month:</b> '.getMonthName($_month);?></span>
        <span style="font-size:11px;" class="alert alert-info inline no-margin"><?php echo '<b>Session:</b> '.$_year;?></span>


        <div class="panel panel-info" style="margin-top: 10px;">
            <div class="panel-body">
                <table class="table">
                    <tr>
                        <td><table class="table table-striped table-bordered">
                                <tr><td>Student ID</td> <td><b><?php echo $_uniq_id;?></b></td></tr>
                                <tr><td>Name</td> <td><b><?php echo $_full_name;?></b></td></tr>
                                <tr><td>Total</td> <td><b><?php echo $_total_amount;?></b></td></tr>
                                <tr><td>Due</td> <td><b><?php echo $_due_amount;?></b></td></tr>
                                <tr><td>Paid</td> <td><b><?php echo $_paid_amount;?></b></td></tr>
                                <tr><td>Status</td> <td><b><?php echo $nstatus_text;?></b></td></tr>





                            </table></td>
                        <td>
                            <?php

                            $table_name="_fee_details_invoice";
                            $column="*";
                            $where_cond="_invoice_no='$_invoice_no'";

                            ?>
                            <table class="table table-striped table-bordered">
                                <?php
                                foreach ($obj->View_all_data_row_column_By_Cond($table_name,$column,$where_cond) as $value)
                                {
                                    extract($value);
                                    echo "<tr><td>$_fee_head</td> <td><b>$_amount</b></td></tr>";
                                }
                                ?>








                            </table>

                        </td>
                    </tr>

                </table>





            </div>
        </div>
    </div>
    <div class="modal-footer">
        <?php echo $nstatus;?>
    </div>
    <?php

}


?>
