<?php

//

//$iid = get_invoice_id_using_txnref($orderId);
$iid = "1335583028";

$invoice_type = get_invoice_type($iid); //get invoice type
$invoice_to = get_invoice_invoice_to($iid);
$pay_to = get_invoice_pay_to($iid); //get who to pay the invoce to
$invoice_for = get_invoice_for($iid); //get what the invoice is made for
$invoice_title = get_invoice_title($iid); // get the invocie title
$invoice_desc = get_invoice_desc($iid); //get the invoice desc
$invoice_amount = get_invoice_amount($iid); //get the invocie amount
$total_amount = db_escape(get_invoice_total_amount($iid)); //get invoice total amount
//     $credit = get_invoiced_credit($iid); //get invoice credit

//     $payment_status = get_invoice_payment_status($iid); //get the invoice payment status

//cleanse the invoice //
//newly added
$invoice_to = db_escape($invoice_to);

//pass invoice to user id
$user_id = $invoice_to;

//$level = get_user_level($user); //get user level
//get user level
$level = db_escape('100');
$level2 = db_escape('200'); //for de students
$levelr = db_escape('REM'); //set the rem too

//get department and course the student is admitted into
$dept_id = db_escape(get_user_department_admitted_to($invoice_to));
$prog_t = db_escape(get_user_programme_type_admitted_to($invoice_to));
$prog_id = db_escape(get_user_programme_admitted_to($invoice_to));
$payment_year = db_escape(date('Y')); //payment year

//get current session
//$session = get_current_session($id = 1); //get current session
$session = db_escape('7');
$session_title = get_session_title($session); //get session title

//get current semester
$semester = get_current_semester($id = 1); //get current semester
$semester_title = get_semester_title($semester); //get semester title

$remita_share_fix = '300';
$vat_fix = '15';

$extra_tot_m = $remita_share_fix + $vat_fix;

$full_name = get_user_fullname($invoice_to);
$email = db_escape(get_user_email($invoice_to));
$phone_no = get_user_phone_no($invoice_to);

//collect matric number
//$m_no = get_user_mat_no($user_id);

//get application number
$appno = db_escape(get_user_app_no($invoice_to));

if ($prog_t == 1) {

    //if successful transaction enter the students and generate matric number

    $student_id = db_escape(get_student_id_abr_application_number($appno, $level));
}

///check if he / she has paid fee
if (student_id_abr_exist_payments($student_id, $session)) {

    exit();

} else {

    //getting applicant info
    $qapp = "SELECT * FROM `applicants_2019`
																	WHERE application_number = $appno";
    $rapp = mysqli_query(dbc(), $qapp);

    if ($rapp) {

        //if succesfully fetch applicant record

        //$initial = 0 ;

        $total_in_course = get_total_course_students_abr($dept_id, $prog_id, $level);

        $total_in_course = $total_in_course + 1;

        //generate matric number
        $applicant_matric_number = db_escape(generate_matric_number_abr($prog_t, $dept_id, $prog_id, $level, $total_in_course));
        //$applicant_matric_number = db_escape('11111');
    }

    while ($row = mysqli_fetch_array($rapp)) //fetch data to be migrated
    {

        //chedk if student has been added already an collect info if added //review this dont depend on this function
        if (!application_number_exist_abr($appno, $level)) {

            //collect the variables for inserting and check for special string using
            $fn = db_escape($row['first_name']); //first name
            // $on =db_escape($row['other_names']);
            $pn = db_escape($row['phone_number']);
            $gend = db_escape($row['gender']);
            $dob = db_escape($row['birthdate']);
            // $noc =db_escape($row['number_of_children']);
            $prog_type = db_escape($row['programme_type_applied_for']);
            $dept = db_escape($row['department_applied_for']);
            $prog = db_escape($row['programme_applied_for']);
            $em = db_escape($row['email']);
            $add = db_escape($row['address']);
            $padd = db_escape($row['permanent_address']);
            $pob = db_escape($row['place_of_birth']);
            $gn = db_escape($row['guardian_name']);
            $gt = db_escape($row['guardian_tel']);
            $ga = db_escape($row['guardian_address']);
            $img = db_escape($row['image']);
            $lga = db_escape($row['lga_id']);
            $state = db_escape($row['state_id']);
            $town = db_escape($row['town']);
            $rn = db_escape($row['referee_name']);
            $ra = db_escape($row['referee_address']);
            $pdf_file = db_escape($row['pdf_file']);
            //$other_langs =db_escape($row['other_languages']);
            //$appn =db_escape($row['appointment']);
            $rel = db_escape($row['religion']);
            //$empl =db_escape($row['employer']);
            $mari = db_escape($row['marital_status']);
            /*$f = db_escape($row['frm']);
            $t = db_escape($row['t']);*/
            $dis = db_escape($row['disability']);
            $bld = db_escape($row['blood_type']);
            $appno = db_escape($row['application_number']);
            $batch = db_escape($row['batch']);
            $pass = db_escape(md5('0000'));
        }
    }

    //get applicants info
    $qq34 = "INSERT INTO `students`
								(`first_name`, `gender`,
									`username`, `password`, `email`, `birthdate`, `phone_number`,  `address`,`permanent_address`, `image`, `user_status`,   `date_added`, `state_id`,
																`lga_id`, `town`,`nok_name`, `nok_address`, `nok_number`,`type_id`,`department_id`,`course_id`,`applied_department`,`applied_course`,`place_of_birth`,`religion`, `marital_status`,  `ref_name`, `ref_add`,`disability`, `blood_group`,`admission_number`, `application_number`, `pdf_file`, `level`,`batch`
																, `session_id`)
																VALUES ($fn,$gend,$applicant_matric_number,$pass,$em,$dob,
																$pn,$add,$padd,$img,1,NOW(),$state,$lga,$town,$gn,$ga,$gt,$prog_t,$dept_id,$prog_id,$dept, $prog,$pob,$rel,$mari,$rn,$ra,$dis,$bld,$applicant_matric_number,$appno,$pdf_file,$level,$batch,$session)";

    $rr304 = mysqli_query(dbc(), $qq34);

    if ($rr304) {

        ///$latest_student_id =  mysqli_insert_id(dbc()); //do something like inserting in school fee payment for the latest student added
        $latest_student_id = db_escape(get_student_id_abr_application_number($appno, $level2));
        $stud_id = $latest_student_id;
    }

    if (!student_id_abr_exist_payments($latest_student_id, $session)) {

        $qry44 = "INSERT INTO `school_fee_payment` ( `student_id`,`department_id`,`course_id`,`level`,  `invoice_id`,
																										 `amount`, `payment_year`, `session`,  `payment_method`,
																										 `date_entered`)
																					 VALUES ( $latest_student_id, $dept_id, $prog_id,  $level, $iid, $total_amount, $payment_year, $session,1,NOW())";
        $rst44 = mysqli_query(dbc(), $qry44);
    }

    if ($rst44) //mysqli_affected_rows(dbc())
    {

        //if done finally collect all info for receipt
        //query
        $q441 = "UPDATE invoices SET payment_status = 1 WHERE invoice_id = $iid"; //update invoice to paid where payment status is 1
        $r441 = mysqli_query(dbc(), $q441);
    }

}

