Your IP : 216.73.216.40


Current Path : /var/www/html/mmishra/erp/models/
Upload File :
Current File : /var/www/html/mmishra/erp/models/aaa_model.php

<?php
//if (!class_exists($erpModel)) {
	require_once __DIR__."/erp_model.php";
//}
class aaaModel extends erpModel {
	public function __construct() {
        // mysql connection using PDO
        try {
 		   	$this->pdo = new PDO('mysql:host=' .DB_SERVER. ';dbname=' .DB_NAME. ';charset=utf8', DB_USER, DB_PASSWD, $options);
			$this->pdo->query("use erp_aaa");
		} catch (PDOException $e) {
    		die("Error!: " . $e->getMessage());
		}
   	}

    public function __destruct() {
        //print mysql_error();
        $this->dbStmt = null;
        $this->pdo = null;
	}


	//***********************************************************************
	//*							aaa Dashboard data						*
	//***********************************************************************

	function aaaDashboard(&$programs, &$fees, &$candidates, &$applicants, &$students, &$slidding, &$withdrawns) {
		$this->dbStmt = $this->pdo->prepare("SELECT degreeLevel,COUNT(programID) AS no FROM erp_aaa.aaa_programs WHERE instituteID=" .$_SESSION["instituteID"]. " GROUP BY degreeLevel");
   	    if ($this->dbStmt->execute()) {
       		$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
            $programs = $rows;
        }

		$this->dbStmt = $this->pdo->prepare("SELECT p.programCode,CASE WHEN category = '*' THEN 'All' ELSE category END AS Cat,SUM(f.feeAmount) AS amount FROM erp_aaa.aaa_feeheads AS f LEFT JOIN erp_aaa.aaa_programs AS p ON f.programID=p.programID WHERE f.accountType=1 AND f.paymentTerm<9 GROUP BY f.programID,f.category");
   	    if ($this->dbStmt->execute()) {
       		$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
            $fees = $rows;
        }
	
		return $data;
	}	

    function listBatches() {
		$this->dbStmt = $this->pdo->query("SELECT DISTINCT batchYear FROM erp_aaa.aaa_enrolments ORDER BY batchYear");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}


	//***********************************************************************
	//*							aaa alert functions							*
	//***********************************************************************

    function saveAlert($subjectID,$message,$emailsTo,$emailsCC,$emailsBCC,$targetGroups,$schedule) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE alertID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET subjectID=$subjectID,message='$message',emailsTo='$emailsTo',emailsCC='$emailsCC',emailsBCC='$emailsBCC',targetGroups='$targetGroups',schedule='$schedule'
            WHERE alertID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(subjectID,message,attachments,emailsTo,emailsCC,emailsBCC,targetGroups,schedule)
            value($subjectID,'$message','$attachments','$emailsTo','$emailsCC','$emailsBCC','$targetGroups','$schedule')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }    
    }
	
    function updateAlert($alertID) {
		// reset dateXalert timestamp automatically on each update
        $this->sqlStmt = "UPDATE erp_aaa.aaa_autoalerts SET alertCounter=alertCounter+1 WHERE alertID=$alertID";
        $this->pdo->exec($this->sqlStmt);
	}

    function saveAlertAttachments($alertID,$attachments) {
        $this->sqlStmt = "UPDATE erp_aaa.aaa_autoalerts SET attachments=CONCAT(attachments, ',', '$attachments') WHERE alertID=" .$alertID;
        $this->pdo->exec($this->sqlStmt);
	}
	
    function removeAlertAttachments($alertID,$attachment) {
        $this->sqlStmt = "UPDATE erp_aaa.aaa_autoalerts SET attachments=REPLACE(attachments, '$attachment,', ''),attachments=REPLACE(attachments, '$attachment', '') WHERE alertID=" .$alertID;
        $this->pdo->exec($this->sqlStmt);
	}

	function enableAlert() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE alertID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableAlert() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE alertID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteAlert() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE alertID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getAlert($alertID=0) {
		if ($alertID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_autoalerts WHERE alertID=$alertID");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
		return $row;
    }

    function listAlerts() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_autoalerts ORDER BY subjectID");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectAlert() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_autoalerts WHERE status='Enabled' ORDER BY subjectID");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

	// list defaulters
	//public $reminders			= [1 => 'Document overdue reminder',2 => 'Fees overdue reminder',3 => 'Mess-charge overdue reminder',4 => 'Name correction for degree',5 => 'Photo submission for degree',6 => 'Arrival plan submission for convocation']; // used to generate auto alerts (email/sms)

	function listDefaulters($programID, $subjectID=0) {
		switch ($subjectID) {
			case 1: //document overdue
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,x.docheadCode,d.docheadName,x.remark 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN aaa_docheads AS d ON x.docheadCode=d.docheadCode AND x.docheadCode NOT IN (1, 2) LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' AND x.docheadCode NOT IN (1,2) ORDER BY e.enrolmentNo");
				break;
			case 2: // fees overdue
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,docheadCode,CASE docheadCode WHEN '1' THEN 'Fees' ELSE 'Mess' END AS docheadName, x.remark 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' AND x.docheadCode='1' ORDER BY e.enrolmentNo");
				break;
			case 3: // messcharge overdue
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,docheadCode,CASE docheadCode WHEN '1' THEN 'Fees' ELSE 'Mess' END AS docheadName, x.remark 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' AND x.docheadCode='2' ORDER BY e.enrolmentNo");
				break;
			case 4: // name correction for degree
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,d.docheadName 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN aaa_docheads AS d on x.docheadCode=d.docheadCode LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' ORDER BY e.enrolmentNo");
				break;
			case 5: // photo submission for degree
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,d.docheadName 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN aaa_docheads AS d on x.docheadCode=d.docheadCode LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' ORDER BY e.enrolmentNo");
				break;
			case 6: // arrival plan submission for convocation
				$this->dbStmt = $this->pdo->query("SELECT enrolmentNo,nameEnglish,nameHindi,e.category,e.phoneNo,e.email,d.docheadName 
				FROM erp_aaa.aaa_exemptions AS x LEFT JOIN aaa_docheads AS d on x.docheadCode=d.docheadCode LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' ORDER BY e.enrolmentNo");
				break;
			default:
				$this->dbStmt = $this->pdo->query("SELECT * 
				FROM erp_aaa.aaa_enrolmentsv 
				WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID AND estatus='Enrolled' ORDER BY enrolmentNo");
				break;				
		}	
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}


	//***********************************************************************
	//*							aaa course functions						*
	//***********************************************************************

    function saveCourse($courseCode,$courseName,$courseNameH) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE courseID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET courseCode='$courseCode',courseName='$courseName',courseNameH='$courseNameH'
            WHERE courseID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else {
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(courseCode,courseName,courseNameH)
            value('$courseCode','$courseName','$courseNameH')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

	function enableCourse() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE courseID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableCourse() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE courseID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteCourse() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE courseID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getCourse($courseID=0) {
		if ($courseID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_courses WHERE courseID=$courseID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listCourses() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_courses ORDER BY courseCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectCourse() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_courses WHERE status='Enabled' ORDER BY courseCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	//***********************************************************************
	//*							aaa branch functions						*
	//***********************************************************************

    function saveBranch($branchCode,$branchName,$branchNameH) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE branchID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET branchCode='$branchCode',branchName='$branchName',branchNameH='$branchNameH'
            WHERE branchID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(branchCode,branchName,branchNameH)
            value('$branchCode','$branchName','$branchNameH')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

	function enableBranch() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE branchID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableBranch() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE branchID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteBranch() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE branchID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getBranch($branchID=0) {
		if ($branchID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_branches WHERE branchID=$branchID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listBranches() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_branches ORDER BY branchCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectBranch() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_branches WHERE status='Enabled' ORDER BY branchCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	//***********************************************************************
	//*							aaa specialization functions						*
	//***********************************************************************

    function saveSpecialization($specializationCode,$specializationName,$specializationNameH) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE specializationID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET specializationCode='$specializationCode',specializationName='$specializationName',specializationNameH='$specializationNameH'
            WHERE specializationID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(specializationCode,specializationName,specializationNameH)
            value('$specializationCode','$specializationName','$specializationNameH')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }        
    }

	function enableSpecialization() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE specializationID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableSpecialization() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE specializationID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteSpecialization() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE specializationID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getSpecialization($specializationID=0) {
		if ($specializationID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_specializations WHERE specializationID=$specializationID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listSpecializations() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_specializations ORDER BY specializationCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectSpecialization() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_specializations WHERE status='Enabled' ORDER BY specializationCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

	//***********************************************************************
	//*							aaa program functions						*
	//***********************************************************************

    function saveProgram($programCode,$programTitle,$programTitleH,$courseID,$branchID,$specializationID,$degreeLevel,$timeDuration,$rollnoPrefix,$entranceBoard,$entranceName,$intakeLimit,$lastRollno) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE programID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET instituteID=" .$_SESSION["instituteID"]. ",programCode='$programCode',programTitle='$programTitle',programTitleH='$programTitleH',courseID=$courseID,branchID=$branchID,specializationID=$specializationID,degreeLevel='$degreeLevel',timeDuration=$timeDuration,rollnoPrefix='$rollnoPrefix',entranceBoard='$entranceBoard',entranceName='$entranceName',intakeLimit=$intakeLimit,lastRollno=$lastRollno
            WHERE programID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(instituteID,programCode,programTitle,programTitleH,courseID,branchID,specializationID,degreeLevel,timeDuration,rollnoPrefix,entranceBoard,entranceName,intakeLimit,lastRollno)
            value(" .$_SESSION["instituteID"]. ",'$programCode','$programTitle','$programTitleH',$courseID,$branchID,$specializationID,'$degreeLevel',$timeDuration,'$rollnoPrefix','$entranceBoard','$entranceName',$intakeLimit,$lastRollno)";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

	function enableProgram() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE programID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableProgram() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE programID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteProgram() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE programID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
/* moved to erp_model.php
    function getProgram($programID=0) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT p.*,c.courseName,c.courseNameH,b.branchName,b.branchNameH,s.specializationName,s.specializationNameH 
			FROM erp_aaa.aaa_programs AS p LEFT JOIN erp_aaa.aaa_courses AS c ON p.courseID=c.courseID LEFT JOIN erp_aaa.aaa_branches AS b ON p.branchID=b.branchID LEFT JOIN erp_aaa.aaa_specializations AS s ON p.specializationID=s.specializationID 
			WHERE programID=$programID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }
*/
    function listPrograms() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_programs WHERE instituteID=" .$_SESSION["instituteID"]. " ORDER BY degreeLevel desc,programCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
/* moved to erp_model
    function selectProgram($degreeLevel='') {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_programs WHERE instituteID=" .$_SESSION["instituteID"]. ($degreeLevel ? " AND degreeLevel='$degreeLevel'" : ''). " AND status='Enabled' ORDER BY programCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
*/	
    function printPrograms() {
		$this->dbStmt = $this->pdo->query("SELECT p.degreeLevel,p.programCode,CONCAT(p.programTitle,' (',p.programTitleH,')') programName,CONCAT(c.courseName,' (',c.courseNameH,')') courseName,CONCAT(b.branchName,' (',b.branchNameH,')') branchName,CONCAT(s.specializationName,' (',s.specializationNameH,')') specialization,CONCAT(p.timeDuration,' years') duration,p.intakeLimit seats,p.rollnoPrefix series 
		FROM erp_aaa.aaa_programs p LEFT JOIN erp_aaa.aaa_courses c ON p.courseID=c.courseID LEFT JOIN erp_aaa.aaa_branches b ON p.branchID=b.branchID LEFT JOIN erp_aaa.aaa_specializations s ON p.specializationID=s.specializationID 
		WHERE p.instituteID=" .$_SESSION["instituteID"]. " ORDER BY p.degreeLevel desc,p.programCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

    function selectDegree() {
		$this->dbStmt = $this->pdo->query("SELECT DISTINCT degreeLevel FROM erp_aaa.aaa_programs WHERE instituteID=" .$_SESSION["instituteID"]. " AND status='Enabled' ORDER BY programCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

	//***********************************************************************
	//*							aaa fee head functions						*
	//***********************************************************************

    function saveFeehead($feeheadCode,$feeheadName,$accountType,$paymentTerm) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE feeheadID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET feeheadCode='$feeheadCode',feeheadName='$feeheadName',accountType=$accountType,paymentTerm=$paymentTerm
            WHERE feeheadID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(feeheadCode,feeheadName,accountType,paymentTerm)
            value('$feeheadCode','$feeheadName',$accountType,$paymentTerm)";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

    function deleteFeehead() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE feeheadID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getFeehead($feeheadID=0) {
		if ($feeheadID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_feeheads WHERE feeheadID=$feeheadID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listFeeheads() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_feeheads ORDER BY accountType,paymentTerm,feeheadName");
        return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    }

    function selectFeehead() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_feeheads WHERE status='Enabled' ORDER BY feeheadName");
        return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    }

	//***********************************************************************
	//*							aaa fees chart functions						*
	//***********************************************************************

    function saveFeechart($feeheadID,$currency,$feeAmount,$category,$programID) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE feechartID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET feeheadID=$feeheadID,currency='$currency',feeAmount=$feeAmount,category='$category',programID='$programID',batchYear=" .$_SESSION['batchYear']. "
            WHERE feechartID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE feeheadID=$feeheadID AND feeAmount=$feeAmount AND batchYear=" .$_SESSION['batchYear'])->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET category=CONCAT(category,',$category'),programID=CONCAT(programID,',$programID')
            WHERE feeheadID=$feeheadID AND feeAmount=$feeAmount";
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(feeheadID,currency,feeAmount,category,programID,batchYear)
            value($feeheadID,'$currency',$feeAmount,'$category','$programID'," .$_SESSION['batchYear']. ")";
			$this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

    function deleteFeechart() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE feechartID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getFeechart($feechartID=0) {
		if ($feechartID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_feecharts WHERE feechartID=$feechartID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listFeecharts() {
		$this->dbStmt = $this->pdo->query("SELECT feeheadCode,feeheadName,accountType,paymentTerm,c.* 
		FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND paymentTerm<9
		ORDER BY c.feeheadID");
        
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    }

    function selectFeechart($programID, $category='', $feeheadID=0) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT feechartID,h.feeheadID,feeheadCode,feeheadName,accountType,paymentTerm,currency,feeAmount,category,programID 
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)')" .($category ? " AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')" : ''). ($feeheadID ? " AND h.feeheadID=$feeheadID" : ''). " AND c.status='Enabled'
			ORDER BY accountType,paymentTerm,feeheadName");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
	
    function listPayables($programID, $category='') {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT feechartID,h.feeheadID,feeheadCode,feeheadName,accountType,paymentTerm,currency,feeAmount,category,programID 
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)')" .($category ? " AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')" : ''). " AND paymentTerm<>9 
			ORDER BY accountType,paymentTerm,feeheadName");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
	
    function getPayable($programID, $category, $account) {
		if ($programID && $category) {
			$this->dbStmt = $this->pdo->query("SELECT MIN(currency) AS curency,SUM(feeAmount) AS amount
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)') AND accountType=$account AND paymentTerm<9");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    	}
	}

    function getAlumniFund($programID, $category) {
		if ($programID && $category) {
			$this->dbStmt = $this->pdo->query("SELECT currency AS curency,feeAmount AS amount 
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)') AND accountType=1 AND feeheadName='Alumni Fund'");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    	}
	}


	//***********************************************************************
	//*							aaa seat matrix functions					*
	//***********************************************************************

    function saveSeatmatrix($programID,$category,$gender,$seatNo) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE matrixID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET seatNo=$seatNo,gender='$gender'
            WHERE matrixID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(programID,batchYear,category,gender,seatNo)
            value($programID," .$_SESSION['batchYear']. ",'$category','$gender',$seatNo)";
			$this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

    function deleteSeatmatrix() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE matrixID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getSeatmatrix($programID,$category,$gender) {
		if ($programID && $category && $gender) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_seatmatrix 
			WHERE programID=$programID AND batchYear=" .$_SESSION['batchYear']. " AND category='$category' AND gender='$gender'");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listSeatmatrixs() {
		$this->dbStmt = $this->pdo->query("SELECT * 
		FROM erp_aaa.aaa_seatmatrix 
		WHERE batcYear=" .$_SESSION['batchYear']. " ORDER BY category,gender");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    }

	//***********************************************************************
	//*							aaa doc head functions						*
	//***********************************************************************

    function saveDochead($docheadCode,$docheadName,$specification,$original) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE docheadID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET docheadCode='$docheadCode',docheadName='$docheadName',specification='$specification',original='$original'
            WHERE docheadID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(docheadCode,docheadName,specification,original)
            value('$docheadCode','$docheadName','$specification','$original')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

    function deleteDochead() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE docheadID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getDochead($docheadID=0) {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_docheads WHERE docheadID=$docheadID");
        return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    }

    function listDocheads() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_docheads ORDER BY docheadCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectDochead() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_docheads WHERE status='Enabled' ORDER BY docheadName");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	//***********************************************************************
	//*							aaa doc chart functions						*
	//***********************************************************************

    function saveDocchart($docheadID,$category,$programID) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE docchartID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET docheadID=$docheadID,category='$category',programID='$programID',batchYear=" .$_SESSION['batchYear']. "
            WHERE docchartID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE docheadID=$docheadID AND batchYear=" .$_SESSION['batchYear'])->rowCount()) {
           $this->sqlStmt = "UPDATE " .$this->tableID. " SET programID=CONCAT(programID,',','$programID')
            WHERE docheadID=$docheadID";
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(docheadID,category,programID,batchYear)
            value($docheadID,'$category','$programID'," .$_SESSION['batchYear']. ")";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

    function deleteDocchart() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE docchartID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getDocchart($docchartID=0) {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_doccharts WHERE docchartID=$docchartID");
        return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    }

    function listDoccharts($category='') {
		$this->dbStmt = $this->pdo->query("SELECT c.*,h.docheadCode,h.docheadName,h.specification,h.original 
		FROM erp_aaa.aaa_doccharts AS c LEFT JOIN erp_aaa.aaa_docheads AS h ON c.docheadID=h.docheadID 
		WHERE c.batchYear=" .$_SESSION['batchYear']. ($category ? " AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')" : ''). " 
		ORDER BY h.docheadCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function listChartheads($programID, $category='') {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT docheadCode 
			FROM erp_aaa.aaa_docheads 
			WHERE docheadID IN (SELECT docheadID FROM erp_aaa.aaa_doccharts WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)')" .($category ? " AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')" : ''). ") AND status='Enabled' 
			ORDER BY docheadCode");
	    	return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
	}

    function selectDocchart($programID, $category='') {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT c.*,h.docheadCode,h.docheadName,h.specification,h.original 
			FROM erp_aaa.aaa_doccharts AS c LEFT JOIN erp_aaa.aaa_docheads AS h ON c.docheadID=h.docheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)')" .($category ? " AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')" : ''). " AND c.status='Enabled' 
			ORDER BY h.docheadCode");
	    	return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
	}

	//***********************************************************************
	//*							aaa candidate functions						*
	//***********************************************************************

    function saveCandidate($programID,$roundNo,$degreeLevel,$rollNo,$nameEnglish,$category,$pwdStatus,$gender,$dateBirth,$stateEligibility,$nationality,$programName,$allotedCategory,$allotedRank,$choiceNo,$quota,$roundName,$willingness,$status,$rcName,$alumni,$remark) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE candidateID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET instituteID=" .$_SESSION["instituteID"]. ",programID=$programID,batchYear=" .$_SESSION['batchYear']. ",roundNo=$roundNo,degreeLevel='$degreeLevel',rollNo='$rollNo',nameEnglish='$nameEnglish',category='$category',pwdStatus='$pwdStatus',gender='$gender',dateBirth='$dateBirth',stateEligibility='$stateEligibility',nationality='$nationality',programName='$programName',allotedCategory='$allotedCategory',allotedRank=$allotedRank,choiceNo=$choiceNo,quota='$quota',roundName='$roundName',willingness='$willingness',status='$status',rcName='$rcName',alumni='$alumni',remark='$remark'
            WHERE candidateID=" .$this->tupleID;
			$this->pdo->exec($this->sqlStmt);
        } else {
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(instituteID,programID,batchYear,roundNo,degreeLevel,rollNo,nameEnglish,category,pwdStatus,gender,dateBirth,stateEligibility,nationality,programName,allotedCategory,allotedRank,choiceNo,quota,roundName,willingness,status,rcName,alumni,remark)
            VALUES(" .$_SESSION["instituteID"]. ",$programID," .$_SESSION['batchYear']. ",$roundNo,'$degreeLevel','$rollNo','$nameEnglish','$category','$pwdStatus','$gender','$dateBirth','$stateEligibility','$nationality','$programName','$allotedCategory',$allotedRank,$choiceNo,'$quota','$roundName','$willingness','$status','$rcName','$alumni','$remark')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

	function deleteCandidate() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_applicants WHERE rollNo=(SELECT rollNo FROM " .$this->tableID. " WHERE candidateID=" .$this->tupleID.")");
		$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
      	$studentID 	= $row['studentID'];
		$status		= $row['status'];
		
		if ($studentID && $status == 'Enrolled') {
			return 'Enrolled';
		} else {
			$this->sqlStmt = "DELETE FROM erp_aaa.aaa_students WHERE studentID=$studentID";
			$this->pdo->exec($this->sqlStmt);

			$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE candidateID=" .$this->tupleID;
			$this->pdo->exec($this->sqlStmt);

			$this->sqlStmt = "DELETE FROM erp_core.erp_users WHERE loginID=(SELECT rollNo FROM " .$this->tableID. " WHERE candidateID=" .$this->tupleID.")";
			$this->pdo->exec($this->sqlStmt);
		}
	}
	
    function setOfferedProgram($programName,$programID,$roundNo) {
		$this->sqlStmt = "UPDATE erp_aaa.aaa_candidates SET programID=$programID
		WHERE instituteID=" .$_SESSION["instituteID"]. " AND batchYear=" .$_SESSION['batchYear']. " AND programName='$programName' AND roundNo=$roundNo";
		$this->pdo->exec($this->sqlStmt);
		
	}

    function setOfferedCategory($aCategory,$oCategory,$roundNo) {
		$this->sqlStmt = "UPDATE erp_aaa.aaa_candidates SET offerCategory='$oCategory' 
		WHERE instituteID=" .$_SESSION["instituteID"]. " AND batchYear=" .$_SESSION['batchYear']. " AND allotedCategory='$aCategory' AND roundNo=$roundNo";
		$this->pdo->exec($this->sqlStmt);
		
	}

    function getCandidate($candidateID) {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_candidates WHERE candidateID=" .$candidateID);
		return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
	}
	
    function listCandidates($degreeLevel, $roundNo=0) {
		if ($degreeLevel) {
			$this->dbStmt = $this->pdo->query("SELECT candidateID,roundNo,degreeLevel,programName,rollNo,nameEnglish,category,pwdStatus,gender,dateBirth,stateEligibility,nationality,programName,allotedCategory,allotedRank,choiceNo,quota,willingness,roundName,status,rcName,remark,programID,batchYear,offerCategory 
			FROM erp_aaa.aaa_candidates 
			WHERE instituteID=" .$_SESSION['instituteID']. " AND batchYear=" .$_SESSION['batchYear']. " AND degreeLevel='$degreeLevel'" .($roundNo ? " AND roundNo=$roundNo" : ''). " ORDER BY roundNo desc,degreeLevel,rollNo");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

    function selectCandidate($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_candidates 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY nameEnglish");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}

	function listOffers() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_applicantsv 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND programID IN (SELECT programID FROM aaa_programs WHERE instituteID=" .$_SESSION["instituteID"]. ") ORDER BY applicantID");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);	
	}
	
	function listOfferedPrograms($degreeLevel, $roundNo=0) {
		if ($degreeLevel) {
			$this->dbStmt = $this->pdo->query("SELECT COUNT(*) AS no,programName AS aProgramID, MIN(programID) AS oProgramID 
			FROM erp_aaa.aaa_candidates 
			WHERE instituteID=" .$_SESSION["instituteID"]. " AND batchYear=" .$_SESSION['batchYear']. " AND degreeLevel='$degreeLevel'" .($roundNo ? " AND roundNo=$roundNo" : ''). " GROUP BY aProgramID");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
    function listOfferedCategorys($degreeLevel, $roundNo=0) {
		if ($degreeLevel) {
			$this->dbStmt = $this->pdo->query("SELECT COUNT(*) AS no,allotedCategory AS aCategory, MIN(offerCategory) AS oCategory 
			FROM erp_aaa.aaa_candidates 
			WHERE instituteID=" .$_SESSION["instituteID"]. " AND batchYear=" .$_SESSION['batchYear']. " AND degreeLevel='$degreeLevel'" .($roundNo ? " AND roundNo=$roundNo" : ''). " GROUP BY aCategory");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		} 
		return $rows;
	}


	
	//***********************************************************************
	//*				aaa student, parent, education functions				*
	//***********************************************************************

    function saveStudent0($studentID,$nameEnglish,$dateBirth,$gender,$pwdStatus,$category,$nationality,$homeState,$remark) {
		// check for student records, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM erp_aaa.aaa_students WHERE studentID=$studentID")->rowCount()) {
            // this is from the imported list from CCMT
            $this->sqlStmt = "UPDATE erp_aaa.aaa_students SET nameEnglish='$nameEnglish',dateBirth='$dateBirth',gender='$gender',pwdStatus='$pwdStatus',category='$category',nationality='$nationality',homeState=$homeState,remark='$remark'
            WHERE studentID=$studentID";
            $this->pdo->exec($this->sqlStmt);
		}
		
		return $studentID;
	}
	
	function saveStudent($studentID,$nameEnglish,$nameHindi,$dateBirth,$gender,$bloodGroup,$pwdStatus,$maritalStatus,$category,$subCategory,$religion,$nationality,$aadhaarNo,$hhIncome,$phoneNo,$email,$homeAddress,$homePIN,$homeState,$homeDistrict,$area,$policeStation,$commAddress,$commPIN,$remark,$status) {
		// check for student records, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM erp_aaa.aaa_students WHERE studentID=$studentID")->rowCount()) {
            // this is from the imported list from CCMT
            $this->sqlStmt = "UPDATE erp_aaa.aaa_students SET nameEnglish='$nameEnglish',nameHindi='$nameHindi',dateBirth='$dateBirth',gender='$gender',
            bloodGroup='$bloodGroup',pwdStatus='$pwdStatus',subCategory='$subCategory',category='$category',religion='$religion',maritalStatus='$maritalStatus',nationality='$nationality',
            aadhaarNo='$aadhaarNo',hhIncome=$hhIncome,homeAddress='$homeAddress',homePIN=$homePIN,homeState=$homeState,homeDistrict=$homeDistrict,commAddress='$commAddress',
            commPIN=$commPIN,area=$area,policeStation='$policeStation',phoneNo='$phoneNo',email='$email',remark='$remark',status='$status'
            WHERE studentID=$studentID";
            $this->pdo->exec($this->sqlStmt);
		} else {
			// this is direct enrollment at AAA counter
            $this->sqlStmt = "INSERT INTO erp_aaa.aaa_students(nameEnglish,nameHindi,dateBirth,gender,bloodGroup,pwdStatus,subCategory,category,religion,maritalStatus,nationality,aadhaarNo,hhIncome,
			homeAddress,homePIN,homeState,homeDistrict,commAddress,commPIN,area,policeStation,phoneNo,email,remark,status)
			VALUES('$nameEnglish','$nameHindi','$dateBirth','$gender','$bloodGroup','$pwdStatus','$subCategory','$category','$religion','$maritalStatus','$nationality','$aadhaarNo',$hhIncome,
			'$homeAddress',$homePIN,$homeState,$homeDistrict,'$commAddress',$commPIN,$area,'$policeStation','$phoneNo','$email','$remark','$status')";
            $this->pdo->exec($this->sqlStmt);		
            $studentID = $this->pdo->lastInsertId();
		}
		
		return $studentID;
	}	

    function getStudent($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_studentsv WHERE studentID=$studentID");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		}
	}

    // moved to erp_model.php
/*	function getStudents($enrolmentNo) {
		if ($enrolmentNo) {
			$this->dbStmt = $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,
			enrolmentNo,p.programCode,enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,bloodGroup,category,
			commAddress,commPIN,e.phoneNo,e.email,fatherName,motherName,guardianName,guardianPhone,guardianEmail,guardianAddress 
			FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID 
			WHERE enrolmentNo LIKE '$enrolmentNo%' ORDER BY e.enrolmentNo");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
*/
    function listStudentsx($programID=0) {
		$this->dbStmt = $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,
		enrolmentNo,date(dateEnrolment) AS Dated," .($programID ? '' : "p.programCode,"). "enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,bloodGroup,pwdStatus,category,subCategory,religion,maritalStatus,
		nationality,aadhaarNo,homeAddress,homePIN,s.stateName,d.districtName,commAddress,commPIN,CASE WHEN e.area = 1 THEN 'Urban' ELSE 'Rural' END AS nativeArea,
		policeStation,e.phoneNo,e.email,xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,
		fatherName,fatherProfession,fatherPhone,fatherEmail,fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,
		guardianName,guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress 
		FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO'" .($programID ? '' : " INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID"). " INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND estatus='Enrolled'" .($programID ? " AND e.programID=$programID" : ''). " ORDER BY e.enrolmentNo");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function listStudents($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT *
			FROM erp_aaa.aaa_enrolmentsv
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY enrolmentNo");			
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
    function printStudents($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,CONCAT(nameEnglish,' (',nameHindi,')') studentName,CASE gender WHEN 'M' THEN 'Male' WHEN 'F' THEN 'Female' ELSE 'Other' END sex,dateBirth,bloodGroup,category,aadhaarNo,phoneNo,email,CONCAT(homeAddress,'-',homePIN) home,policeStation,CONCAT(commAddress,'-',commPIN) address 
			FROM erp_aaa.aaa_enrolments e LEFT JOIN erp_aaa.aaa_students s on e.studentID=s.studentID LEFT JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO'
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY enrolmentNo");
			$rows =  $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

    function saveParent($parentID,$studentID,$fatherName,$fatherProfession,$fatherPhone,$fatherEmail,$fatherAddress,$motherName,$motherProfession,$motherPhone,$motherEmail,$motherAddress,$guardianName,$guardianProfession,$relationWith,$guardianPhone,$guardianEmail,$guardianAddress) {
		// check for student records, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM erp_aaa.aaa_parents WHERE parentID=$parentID")->rowCount()) {
            // this is from the imported list from CCMT
            $this->sqlStmt = "UPDATE erp_aaa.aaa_parents SET studentID=$studentID,fatherName='$fatherName',fatherProfession='$fatherProfession',fatherPhone='$fatherPhone',fatherEmail='$fatherEmail',fatherAddress='$fatherAddress',
			motherName='$motherName',motherProfession='$motherProfession',motherPhone='$motherPhone',motherEmail='$motherEmail',motherAddress='$motherAddress',
			guardianName='$guardianName',guardianProfession='$guardianProfession',relationWith='$relationWith',guardianPhone='$guardianPhone',guardianEmail='$guardianEmail',guardianAddress='$guardianAddress'
			WHERE parentID=$parentID";
            $this->pdo->exec($this->sqlStmt);
		} else {
			// this is direct enrolment at AAA counter
            $this->sqlStmt = "INSERT INTO erp_aaa.aaa_parents(studentID,fatherName,fatherProfession,fatherPhone,fatherEmail,fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,
			guardianName,guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress)
			VALUES($studentID,'$fatherName','$fatherProfession','$fatherPhone','$fatherEmail','$fatherAddress','$motherName','$motherProfession','$motherPhone','$motherEmail','$motherAddress',
			'$guardianName','$guardianProfession','$relationWith','$guardianPhone','$guardianEmail','$guardianAddress')";
            $this->pdo->exec($this->sqlStmt);		
            $parentID = $this->pdo->lastInsertId();
		}
		//echo $this->sqlStmt;
		
		return $parentID;
	}	

    function saveEducation($educationID,$studentID,$xGrade,$xYear,$xBoard,$xiiGrade,$xiiYear,$xiiBoard,$ugGrade,$ugYear,$ugInstitute,$pgGrade,$pgYear,$pgInstitute,$lastScore,$categoryRank) {
		// check for student records, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM erp_aaa.aaa_educations WHERE educationID=$educationID")->rowCount()) {
            $this->sqlStmt = "UPDATE erp_aaa.aaa_educations SET studentID=$studentID,xGrade='$xGrade',xYear=$xYear,xBoard='$xBoard',xiiGrade='$xiiGrade',xiiYear=$xiiYear,xiiBoard='$xiiBoard',ugGrade='$ugGrade',ugYear=$ugYear,ugInstitute='$ugInstitute',pgGrade='$pgGrade',pgYear=$pgYear,pgInstitute='$pgInstitute',lastScore=$lastScore,categoryRank=$categoryRank
            WHERE educationID=$educationID";
            $this->pdo->exec($this->sqlStmt);
		} else {
			// this is direct enrolment at AAA counter
            $this->sqlStmt = "INSERT INTO erp_aaa.aaa_educations(studentID,xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,categoryRank)
			VALUES($studentID,'$xGrade',$xYear,'$xBoard','$xiiGrade',$xiiYear,'$xiiBoard','$ugGrade',$ugYear,'$ugInstitute','$pgGrade',$pgYear,'$pgInstitute',$lastScore,$categoryRank)";
            $this->pdo->exec($this->sqlStmt);		
            $educationID = $this->pdo->lastInsertId();
		}
		//echo $this->sqlStmt;

		return $educationID;
	}	

    function saveAccount($accountID,$studentID,$accountNo,$holderName,$bankName,$ifscCode) {
		// check for student records, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM erp_aaa.aaa_accounts WHERE accountID=$accountID")->rowCount()) {
            $this->sqlStmt = "UPDATE erp_aaa.aaa_accounts SET studentID=$studentID,accountNo='$accountNo',holderName='$holderName',bankName='$bankName',ifscCode='$ifscCode'
            WHERE accountID=$accountID";
            $this->pdo->exec($this->sqlStmt);
		} else {
			// this is direct enrolment at AAA counter
            $this->sqlStmt = "INSERT INTO erp_aaa.aaa_accounts(studentID,accountNo,holderName,bankName,ifscCode)
			VALUES($studentID,'$accountNo','$holderName','$bankName','$ifscCode')";
            $this->pdo->exec($this->sqlStmt);		
            $educationID = $this->pdo->lastInsertId();
		}
		//echo $this->sqlStmt;

		return $accountID;
	}	

	//***********************************************************************
	//*							aaa applicant functions						*
	//***********************************************************************

    function saveApplicant($rollNo,$studentID,$programID,$offerCategory,$offerRound,$remark) {
		if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE applicantID=" .$this->tupleID)->rowCount()) {
            // interested applicants thru self registration
			if ($offerRound) {
				$this->sqlStmt = "UPDATE " .$this->tableID. " SET rollNo='$rollNo',studentID=$studentID,programID=$programID,batchYear=" .$_SESSION['batchYear']. ",offerCategory='$offerCategory',offerRound=$offerRound,remark='$remark'
				WHERE applicantID=" .$this->tupleID;
			} else {
				$this->sqlStmt = "UPDATE " .$this->tableID. " SET dateResponse=now(),status='Interested'
				WHERE applicantID=" .$this->tupleID;
            }
			$this->pdo->exec($this->sqlStmt);
		} else {
			// offered applicant thru manageOffer or edit candidature
			$this->sqlStmt = "INSERT INTO " .$this->tableID. "(rollNo,studentID,programID,batchYear,offerCategory,offerRound,remark,status)
            VALUES('$rollNo',$studentID,$programID," .$_SESSION['batchYear']. ",'$offerCategory',$offerRound,'$remark','Offered')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
		}

		$data 			= $this->getStudent($studentID);
		$userName		= $data['nameEnglish']; // for password
		$dob			= $data['dateBirth']; // for password
		$groupID		= 6; // groupID of applicants from erp_groups

		// insert login credentials		
		$this->sqlStmt = "INSERT INTO erp_core.erp_users(loginID,userName,groupID,authMode,secretCode)
		values('$rollNo','$userName',$groupID,'MDB',password('$dob'))";
		
		if ($this->pdo->exec($this->sqlStmt)) {
			$userID = $this->pdo->lastInsertId();
			// increment member count in group
			$this->sqlStmt = "UPDATE erp_core.erp_groups SET memberCount=memberCount+1 WHERE groupID=$groupID";
			$this->pdo->exec($this->sqlStmt);
			
			// grant access privilege on aaaApplicant view
			$menuID = $this->getMenuID('applicant');
			$this->sqlStmt = "INSERT INTO erp_core.erp_uacls(userID,menuID,aclRead,aclInsert,aclUpdate,aclDelete)
			values($userID,$menuID,1,0,1,0)";
			$this->pdo->exec($this->sqlStmt);
			$aclID = $this->pdo->lastInsertId();
		} else {
			$this->sqlStmt = "UPDATE erp_core.erp_users SET secretCode=password('$dob') WHERE loginID='$rollNo'";
			$this->pdo->exec($this->sqlStmt);
		}
		//echo $this->sqlStmt;
    }

    function deleteApplicant() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE applicantID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getApplicant($applicantID=0) {
		if ($applicantID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_applicantsv WHERE applicantID=$applicantID");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    	}
		return $row;
	}
	
    function listApplicants($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,rollNo,dateOffer,
			p.programCode,batchYear,offerCategory,nameEnglish,a.nameHindi,gender,dateBirth,
			bloodGroup,pwdStatus,category,subCategory,religion,maritalStatus,nationality,aadhaarNo,homeAddress,homePIN,s.stateName,
			d.districtName,commAddress,commPIN,CASE WHEN a.area = 1 THEN 'Urban' ELSE 'Rural' END AS nativeArea,policeStation,a.phoneNo,a.email,xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,
			ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,fatherName,fatherProfession,fatherPhone,fatherEmail,
			fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,guardianName,
			guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress,astatus as status
			FROM erp_aaa.aaa_applicantsv AS a LEFT JOIN erp_aaa.aaa_uploads AS u ON a.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_programs AS p ON a.programID=p.programID INNER JOIN erp_core.erp_states AS s ON a.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON a.homeDistrict=d.districtID
			WHERE batchYear=" .$_SESSION['batchYear']. " AND a.programID=$programID ORDER BY a.rollNo");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
    }
	
	function selectApplicant($programID) {
		if ($programID) {
			$this->dbStmt 	= $this->pdo->query("SELECT * FROM erp_aaa.aaa_applicantsv 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID AND astatus IN ('Offered', 'Interested') ORDER BY nameEnglish");
			$rows 			= $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
	function selectApplicant1($programID) {
		if ($programID) {
			$this->dbStmt 	= $this->pdo->query("SELECT * FROM erp_aaa.aaa_applicantsv 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY rollNo");
			$rows 			= $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

	//***********************************************************************
	//*							aaa upload functions						*
	//***********************************************************************

    function saveUpload($studentID,$docheadCode,$fileName) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE studentID=$studentID AND docheadCode='$docheadCode'")->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET fileName='$fileName' WHERE studentID=$studentID AND docheadCode='$docheadCode'";
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(studentID,docheadCode,fileName) VALUES($studentID,'$docheadCode','$fileName')";
            $this->pdo->exec($this->sqlStmt);
            // not needed as it is called on studentID not on uploadID...$this->tupleID = $this->pdo->lastInsertId();
        }
	}

    function deleteUpload() {
		$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE uploadID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getUpload($uploadID=0) {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_uploads WHERE uploadID=$uploadID");
		return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    }

/* moved to erp_model.php
    function getMyUpload($studentID, $docheadCode='') {
		if ($studentID && $docheadCode) {
			$this->dbStmt = $this->pdo->query("SELECT fileName 
			FROM erp_aaa.aaa_uploads 
			WHERE studentID=$studentID AND docheadCode='$docheadCode'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			
			return $row['fileName'];
    	}
    }
*/
    function listMyUploads($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT u.*,d.docheadName 
			FROM erp_aaa.aaa_uploads AS u LEFT JOIN aaa_docheads AS d ON u.docheadCode=d.docheadCode 
			WHERE u.studentID=$studentID AND u.docheadCode NOT IN ('PHO', 'SIG') ORDER BY u.fileName,u.docheadCode");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
    }

/* moved to erp_model.php
    function listUploads($studentID, $programID) {
		if ($studentID && $programID) {
			$this->dbStmt = $this->pdo->query("SELECT DISTINCT u.*,h.docheadName,h.original 
			FROM erp_aaa.aaa_uploads AS u LEFT JOIN aaa_docheads AS h ON u.docheadCode=h.docheadCode INNER JOIN aaa_doccharts AS c ON c.docheadID=h.docheadID 
			WHERE u.studentID=$studentID AND c.programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') 
			ORDER BY uploadID");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}	
	}
*/
	//***********************************************************************
	//*							aaa payment functions						*
	//***********************************************************************

    function savePayment($studentID,$accountType,$paymentMode,$paymentNo,$bankName,$payAmount,$currency,$datePayment) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE paymentID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET accountType=$accountType,paymentMode='$paymentMode',paymentNo='$paymentNo',bankName='$bankName',payAmount=$payAmount,currency='$currency',datePayment='$datePayment'
			WHERE paymentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(studentID,accountType,paymentMode,paymentNo,bankName,payAmount,currency,datePayment) 
			VALUES($studentID,$accountType,'$paymentMode','$paymentNo','$bankName',$payAmount,'$currency','$datePayment')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
		//echo $this->sqlStmt;
	}

	function getPayment($studentID, $account) {
		if ($studentID && $account) {
			$this->dbStmt = $this->pdo->query("SELECT SUM(payAmount) AS amount 
			FROM erp_aaa.aaa_payments 
			WHERE studentID=$studentID AND accountType=$account");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        	
			return $row['amount'];
		}
	}
	
    function listPayments($studentID=0) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_payments 
			WHERE studentID=$studentID ORDER BY accountType,datePayment");
    	} else {
			$this->dbStmt = $this->pdo->query("SELECT a.rollNo,a.nameEnglish,CASE WHEN p.accountType = 1 THEN 'Fees Amount' ELSE 'Mess Charges' END AS 'Head',p.PaymentMode,p.PaymentNo,p.BankName,p.DatePayment,CONCAT(p.Currency, ' ', p.payAmount) AS amount 
			FROM erp_aaa.aaa_payments AS p, aaa_applicantsv AS a 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND p.studentID=a.studentID ORDER BY p.studentID,p.accountType,p.datePayment");
		}
   		$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		return $rows;
	}

    function getPrepaid($programID, $category) {
		if ($programID && $category) {
			$this->dbStmt = $this->pdo->query("SELECT SUM(feeAmount) AS amount
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)') AND paymentTerm=9");
   			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			
			return $row['amount'];
    	}
	}
	
    function listPrepaids($programID, $category) {
		if ($programID && $category) {
			$this->dbStmt = $this->pdo->query("SELECT feeheadName,feeAmount,currency
			FROM erp_aaa.aaa_feecharts AS c INNER JOIN erp_aaa.aaa_feeheads AS h ON c.feeheadID=h.feeheadID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)') AND paymentTerm=9 AND accountType=1");
			$row = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $row;
	}

	function getPaidAmount($programID, $account) {
		if ($programID && $account) {
			$this->dbStmt 	= $this->pdo->query("SELECT SUM(payAmount) AS amount, MIN(currency) AS curency 
			FROM erp_aaa.aaa_payments AS p LEFT JOIN aaa_applicants AS a ON p.studentID=a.studentID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID AND accountType=$account");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['curency']. ' ' .$row['amount'];
		}
	}

    function paymentSummary($programID=0) {
		$this->dbStmt = $this->pdo->query("SELECT a.rollNo,e.enrolmentNo,a.nameEnglish,CASE WHEN p.accountType=1 THEN 'Fees Amount' ELSE 'Mess Charges' END AS 'Head',p.PaymentMode,p.PaymentNo,p.BankName,p.DatePayment,CONCAT(p.Currency, ' ', p.payAmount) AS amount 
		FROM erp_aaa.aaa_payments AS p LEFT JOIN aaa_applicantsv AS a ON p.studentID=a.studentID LEFT JOIN aaa_enrolments AS e ON p.studentID=e.studentID
		WHERE a.batchYear=" .$_SESSION['batchYear']. ($programID ? " AND a.programID=$programID" : ''). " ORDER BY p.accountType,e.enrolmentNo,p.datePayment");
		$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		return $rows;
	}
	
    function getMyPayment($studentID, $account) {
		if ($studentID && $account) {
			$this->dbStmt = $this->pdo->query("SELECT SUM(payAmount) AS amount 
			FROM erp_aaa.aaa_payments 
			WHERE studentID=$studentID AND accountType=$account");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			
			return $row['amount'];
    	}
    }

    function listMyPayments($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT accountType AS account,SUM(payAmount) AS amount 
			FROM erp_aaa.aaa_payments 
			WHERE studentID=$studentID");
			
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
    }

	//***********************************************************************
	//*							aaa exemption functions						*
	//***********************************************************************

    function saveExemption($studentID,$docheadCode,$dateRemind,$remark) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE studentID=$studentID AND docheadCode='$docheadCode'")->rowCount() > 0) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET dateRemind='$dateRemind',remark='$remark'
            WHERE studentID=$studentID AND docheadCode='$docheadCode'";
            $this->pdo->exec($this->sqlStmt);
        } else {
		    $this->sqlStmt = "INSERT INTO " .$this->tableID. "(studentID,docheadCode,dateRemind,remark) 
		    VALUES($studentID,'$docheadCode','$dateRemind','$remark')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $studentID; // $this->pdo->lastInsertId();
        }
    }
	
    function deleteExemption($studentID,$docheadCode) {
        $this->sqlStmt = "DELETE FROM erp_aaa.aaa_exemptions WHERE studentID=$studentID AND docheadCode='$docheadCode'";
        $this->pdo->exec($this->sqlStmt);
	}
	
	function settleExemption($studentID,$docheadCode) {
        if ($studentID && $docheadCode) {
            $this->sqlStmt = "UPDATE erp_aaa.aaa_exemptions 
			SET dateClear=curdate() WHERE studentID=$studentID AND docheadCode='$docheadCode'";
            $this->pdo->exec($this->sqlStmt);
        }
    }    

    function getExemption($exemptionID=0) {
		$this->dbStmt = $this->pdo->query("SELECT * 
		FROM erp_aaa.aaa_exemptions 
		WHERE exemptionID=$exemptionID");
        return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    }

/* moved to erp_model.php
    function listExemptions($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT e.*,h.docheadName,h.original
			FROM aaa_exemptions AS e LEFT JOIN aaa_docheads AS h ON e.docheadCode=h.docheadCode
			WHERE e.studentID=$studentID ORDER BY h.docheadCode");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
	}
*/
   function getMyExemption($studentID, $docheadCode='') {
		if ($studentID && $docheadCode) {
			$this->dbStmt = $this->pdo->query("SELECT dateRemind 
			FROM erp_aaa.aaa_exemptions 
			WHERE studentID=$studentID AND docheadCode='$docheadCode'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		
			return $row['dateRemind'];
    	}
    }

    function listMyExemptions($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT dateRemind 
			FROM erp_aaa.aaa_exemptions 
			WHERE studentID=$studentID");
			
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
    }

	function exemptionSummary($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT DISTINCT a.enrolmentNo,CONCAT(nameEnglish,' (',s.nameHindi,')') AS studentName,s.gender,s.category,s.phoneNo,s.email,h.docheadName
			FROM erp_aaa.aaa_exemptions AS e INNER JOIN aaa_docheads AS h on e.docheadCode=h.docheadCode INNER JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_students AS s ON e.studentID=s.studentID INNER JOIN aaa_enrolments AS a ON s.studentID=a.studentID INNER JOIN erp_aaa.aaa_programs AS p ON a.programID=p.programID 
			WHERE a.batchYear=" .$_SESSION['batchYear']. " AND a.programID=$programID AND a.status='Enrolled' ORDER BY a.enrolmentNo");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

	function selectDeficit($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT DISTINCT x.studentID,e.enrolmentNo,e.nameEnglish 
			FROM erp_aaa.aaa_exemptions AS x LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON x.studentID=e.studentID 
			WHERE e.batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND e.estatus='Enrolled' ORDER BY e.enrolmentNo");
        	$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    	}
		return $rows;
	}

	//***********************************************************************
	//*							aaa enrolment functions						*
	//***********************************************************************

	function saveEnrolment($enrolmentNo,$studentID,$programID,$enrolCategory,$hbookNo,$abookNo,$remark) {
		// check for enrolment record, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID)->rowCount()) {
            // update on enrolment table
            $this->sqlStmt 	= "UPDATE " .$this->tableID. " SET studentID=$studentID,programID=$programID,batchYear=" .$_SESSION['batchYear']. ",enrolCategory='$enrolCategory',hbookNo=$hbookNo,abookNo='$abookNo',remark='$remark',status='Enrolled'
            WHERE enrolmentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else { // rollNo=enrolmentNo by defaul that may change in second semester on merit basis
			$dateEnrolment 	= date("Y-m-d");
			$loginID		= $_SESSION['loginID'];
			$this->sqlStmt 	= "INSERT INTO " .$this->tableID. "(enrolmentNo,studentID,programID,batchYear,enrolCategory,hbookNo,abookNo,remark,loginID,status)
            VALUES('$enrolmentNo',$studentID,$programID," .$_SESSION['batchYear']. ",'$enrolCategory',$hbookNo,'$abookNo','$remark','$loginID','Enrolled')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID 	= $this->pdo->lastInsertId();
			//echo $this->sqlStmt;
		
			// increment rollno by 1
			$this->sqlStmt 	= "UPDATE erp_aaa.aaa_programs SET lastRollNo=lastRollNo+1 WHERE programID=$programID";
            $this->pdo->exec($this->sqlStmt);

			// update applicant status to enrolled
			$this->sqlStmt 	= "UPDATE erp_aaa.aaa_applicants SET status='Enrolled' WHERE studentID=$studentID";
			$this->pdo->exec($this->sqlStmt);
		}    
		//echo $this->sqlStmt;
	}
	
    function deleteEnrolment() {
       	$this->sqlStmt 	= $this->pdo->query("DELETE FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID);
        $this->dbStmt->exec($this->sqlStmt);
		
		// set status to offered
		$studentID 		= $this->getStudentIDByEnrolmentID($this->tupleID);
		$applicantID	= $this->getApplicantIDByStudentID($studentID);
       	$this->sqlStmt 	= $this->pdo->query("UPDATE erp_aaa.aaa_applicants SET status='Offered' WHERE applicantID=$applicantID");
        $this->dbStmt->exec($this->sqlStmt);
		
		
	}
	
    // moved to erp_model.php
    //function getEnrolment($enrolmentID=0) {
	//	if ($enrolmentID) {
	//		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_enrolmentsv WHERE enrolmentID=$enrolmentID");
	//		return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
	//	}
	//}
	
    function listEnrolments($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,
			enrolmentNo,a.rollNo,date(dateEnrolment) AS Dated,enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,
			pwdStatus,category,subCategory,religion,nationality,aadhaarNo,d.districtName,s.stateName,e.phoneNo,e.email,fatherName,motherName 
			FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_applicants AS a ON e.studentID=a.studentID INNER JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO'" .($programID ? '' : " INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID"). " INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID
			WHERE e.batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID ORDER BY e.enrolmentNo");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
    function selectEnrolment($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_enrolmentsv 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID AND estatus='Enrolled' ORDER BY enrolmentNo");
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
    function printEnrolments($programID=0) {
		$this->sqlStmt = "SELECT * FROM erp_aaa.aaa_enrolmentsv 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND estatus='Enrolled'" .($programID ? " AND programID=$programID" : ''). " ORDER BY enrolmentNo";
		$this->dbStmt = $this->pdo->query($this->sqlStmt);
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
    
	function listDatedEnrolments($programID=0) {
		$this->sqlStmt = "SELECT date(dateEnrolment) AS Dated, count(enrolmentID) AS no FROM erp_aaa.aaa_enrolments 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND status='Enrolled'" .($programID ? " AND programID=$programID" : ''). " GROUP BY dated desc";
		$this->dbStmt = $this->pdo->query($this->sqlStmt);
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

	function datedEnrolments($dated) {
        if ($dated) {
			$this->sqlStmt = "SELECT * FROM erp_aaa.aaa_enrolmentsv 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND status='Enrolled' AND date(dateEnrolment)='$dated' ORDER BY programID,enrolmentNo";
			$this->dbStmt = $this->pdo->query($this->sqlStmt);
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

    function totalEnrolments() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_enrolmentsv 
		WHERE batchYear=" .$_SESSION['batchYear']. " AND estatus='Enrolled' ORDER BY enrolmentNo");
		$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		return $rows;
	}

	
	//***********************************************************************
	//*							aaa mcaip functions						*
	//***********************************************************************

    function saveInsurence($spouseName,$spouseAge,$spousePhone,$spouseEmail,$spouseAddress,$spousePIN,$child1Name,$child1Age,$child1Gender,$child1Phone,$child1Email,$child1Address,$child1PIN,$child2Name,$child2Age,$child2Gender,$child2Phone,$child2Email,$child2Address,$child2PIN,$knownDiseases) {
		// check for insurence record, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID)->rowCount()) {
            // update on enrolment table
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET spouseName='$spouseName',spouseAge=$spouseAge,spousePhone='$spousePhone',spouseEmail='$spouseEmail',spouseAddress='$spouseAddress',spousePIN=$spousePIN,child1Name='$child1Name',child1Age=$child1Age,child1Gender='$child1Gender',child1Phone='$child1Phone',child1Email='$child1Email',child1Address='$child1Address',child1PIN=$child1PIN,child2Name='$child2Name',child2Age=$child2Age,child2Gender='$child2Gender',child2Phone='$child2Phone',child2Email='$child2Email',child2Address='$child2Address',child2PIN=$child2PIN,knownDiseases='$knownDiseases'
            WHERE enrolmentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else {
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(enrolmentID,spouseName,spouseAge,spousePhone,spouseEmail,spouseAddress,spousePIN,child1Name,child1Age,child1Gender,child1Phone,child1Email,child1Address,child1PIN,child2Name,child2Age,child2Gender,child2Phone,child2Email,child2Address,child2PIN,knownDiseases)
            VALUES(" .$this->tupleID. ",'$spouseName',$spouseAge,'$spousePhone','$spouseEmail','$spouseAddress',$spousePIN,'$child1Name',$child1Age,'$child1Gender','$child1Phone','$child1Email','$child1Address',$child1PIN,'$child2Name',$child2Age,'$child2Gender','$child2Phone','$child2Email','$child2Address',$child2PIN,'$knownDiseases')";
            $this->pdo->exec($this->sqlStmt);
            //$this->tupleID = $this->pdo->lastInsertId();
		}
	}
	
    function getInsurence($enrolmentID) {
		if ($enrolmentID) {
			$this->dbStmt = $this->pdo->query("SELECT e.*,m.* FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_mcaips AS m ON e.enrolmentID=m.enrolmentID WHERE e.enrolmentID=$enrolmentID");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		}
	}
	
    function listInsurences() {
		$this->sqlStmt = "SELECT e.enrolmentNo,e.nameEnglish, m.* 
		FROM erp_aaa.aaa_mcaips AS m LEFT JOIN aaa_enrolmentsv AS e ON m.enrolmentID=e.enrolmentID ORDER BY mcaipID";
		$this->dbStmt = $this->pdo->query($this->sqlStmt);
   	    return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
    }


	//***********************************************************************
	//*							aaa sliding  functions						*
	//***********************************************************************

    function saveSliding($enrolmentNo,$studentID,$programID,$enrolCategory,$hbookNo,$abookNo,$hostelID,$roomID,$remark) {
		// nothing to do with student record
		// just allot new enrollment no. under new program 
		// tag old enrollment no. with status as slided
		
		// check for enrolment record, if exists, update otherwise insert
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID)->rowCount()) {
            // update enrolment table
			$programCode = $this->getProgramCodeByProgramID($programID);
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET remark=CONCAT(remark,' [slided to $programCode $enrolmentNo]'),status='Slided'
            WHERE enrolmentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
	    
			// new enrolment
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(enrolmentNo,studentID,programID,batchYear,enrolCategory,hbookNo,abookNo,loginID,remark)
            VALUES('$enrolmentNo',$studentID,$programID," .$_SESSION['batchYear']. ",'$enrolCategory',$hbookNo,'$abookNo','" .$_SESSION['loginID']. "','$remark')";
            $this->pdo->exec($this->sqlStmt);
            $enrolmentID = $this->pdo->lastInsertId();

			//increment last rollno if successful
			if ($enrolmentID) {
				$this->sqlStmt = "UPDATE erp_aaa.aaa_programs SET lastRollNo=lastRollNo+1 WHERE programID=$programID";
				$this->pdo->exec($this->sqlStmt);
			}
		}
	}
	
	function listSlidings($programID) {
		if ($programID) {
			$this->sqlStmt = "SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,dateEnrolment,
			p.programCode,batchYear,enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,
			bloodGroup,pwdStatus,category,subCategory,religion,maritalStatus,nationality,aadhaarNo,homeAddress,homePIN,s.stateName,
			d.districtName,commAddress,commPIN,CASE WHEN e.area = 1 THEN 'Urban' ELSE 'Rural' END AS nativeArea,policeStation,e.phoneNo,e.email,xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,
			ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,fatherName,fatherProfession,fatherPhone,fatherEmail,
			fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,guardianName,
			guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress,e.studentID 
			FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND estatus='Slided' AND e.programID=$programID ORDER BY e.enrolmentNo";
			$this->dbStmt = $this->pdo->query($this->sqlStmt);
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}
	
	//***********************************************************************
	//*							aaa withdrawal functions					*
	//***********************************************************************
							
    function saveWithdrawal($studentID,$dateWithdraw,$withdrawReason,$accountNo,$holderName,$bankName,$ifscCode,$remark) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE withdrawID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt 	= "UPDATE " .$this->tableID. " SET studentID=$studentID,dateWithdraw='$dateWithdraw',withdrawReason='$withdrawReason',accountNo='$accountNo',holderName='$holderName',bankName='$bankName',ifscCode='$ifscCode',remark='$remark',loginID='" .$_SESSION['loginID']. "'
            WHERE withdrawID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
	    } else {
            $this->sqlStmt 	= "INSERT INTO " .$this->tableID. "(studentID,dateWithdraw,withdrawReason,accountNo,holderName,bankName,ifscCode,remark,loginID)
			VALUES($studentID,'$dateWithdraw','$withdrawReason','$accountNo','$holderName','$bankName','$ifscCode','$remark','" .$_SESSION['loginID']. "')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();

            //echo $this->sqlStmt;
			// update enrolment record
			$this->sqlStmt 	= "UPDATE erp_aaa.aaa_enrolments SET status='Withdrawn',remark='$remark' WHERE studentID=$studentID";
            $this->pdo->exec($this->sqlStmt);

            // update student record
			$this->sqlStmt = "UPDATE erp_aaa.aaa_students SET status='Withdrawn',remark='$remark' WHERE studentID=$studentID";
            $this->pdo->exec($this->sqlStmt);
		}
	}

    function deleteWithdrawal($studentID,$remark) {
        $this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE withdrawID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);

		$this->sqlStmt 	= "UPDATE erp_aaa.aaa_enrolments SET status='Enrolled',remark='$remark' WHERE studentID=$studentID";
        $this->pdo->exec($this->sqlStmt);
	}
		
    function getWithdrawal($withdrawID=0) {
		if ($withdrawID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_withdrawals WHERE withdrawID=$withdrawID");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		}
	}
	
	function listWithdrawals($programID) {
		if ($programID) {
			$this->sqlStmt = "SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,
			w.dateWithdraw,w.withdrawReason,e.accountNo,e.holderName,e.bankName,e.ifscCode,w.remark,dateEnrolment,
			p.programCode,batchYear,enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,bloodGroup,
			pwdStatus,category,subCategory,religion,maritalStatus,nationality,aadhaarNo,homeAddress,homePIN,s.stateName,d.districtName,
			commAddress,commPIN,CASE WHEN e.area = 1 THEN 'Urban' ELSE 'Rural' END AS nativeArea,policeStation,e.phoneNo,e.email,
			xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,
			fatherName,fatherProfession,fatherPhone,fatherEmail,fatherAddress,motherName,motherProfession,motherPhone,motherEmail,
			motherAddress,guardianName,	guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress,e.enrolmentID,withdrawID
			FROM erp_aaa.aaa_withdrawals AS w LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON w.studentID=e.studentID INNER JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID
			WHERE e.batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID ORDER BY e.enrolmentNo";
			$this->dbStmt = $this->pdo->query($this->sqlStmt);
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

	function selectWithdrawal($programID) {
		if ($programID) {
			$this->sqlStmt = "SELECT w.*,programID,enrolmentNo,nameEnglish 
			FROM erp_aaa.aaa_withdrawals AS w LEFT JOIN erp_aaa.aaa_enrolmentsv AS e ON w.studentID=e.studentID
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY enrolmentNo";
			$this->dbStmt = $this->pdo->query($this->sqlStmt);
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
	
	//***********************************************************************
	//*							aaa course functions						*
	//***********************************************************************

    function saveSection($sectionCode,$programID,$enrolmentID,$strength) {
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE sectionID=" .$this->tupleID)->rowCount()) {
            $this->sqlStmt = "UPDATE " .$this->tableID. " SET sectionCode='$sectionCode',programID=$programID,enrolmentID=$enrolmentID,strength=$strength
            WHERE sectionID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
        } else {
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(sectionCode,programID,enrolmentID,strength)
            value('$sectionCode',$programID,$enrolmentID,$strength)";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
    }

	function enableSection() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Enabled' WHERE sectionID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function disableSection() {
        $this->sqlStmt = "UPDATE " .$this->tableID. " SET status='Disabled' WHERE sectionID=" .$this->tupleID;
        $this->pdo->exec($this->sqlStmt);
    }

    function deleteSection() {
       	$this->sqlStmt = "DELETE FROM " .$this->tableID. " WHERE sectionID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getSection($sectionID=0) {
		if ($sectionID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_sections WHERE sectionID=$sectionID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
        }
    }

    function listSections() {        
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_sections ORDER BY sectionCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
    function selectSection() {
		$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_sections WHERE status='Enabled' ORDER BY sectionCode");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	
	//***********************************************************************
	//*							aaa branch change  functions				*
	//***********************************************************************

	function saveBranchChange($enrolmentNo,$programID,$remark) {
		$loginID		= $_SESSION['loginID'];
		// check for enrolment record, if exists, update
        if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID)->rowCount()) {
            // update on enrolment table
            $this->sqlStmt 	= "UPDATE " .$this->tableID. " SET remark='Changed to $enrolmentNo',status='Changed'
            WHERE enrolmentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);

			// create new record
			$this->sqlStmt 	= "INSERT INTO " .$this->tableID. "(enrolmentNo,programID,remark,status,loginID,studentID,batchYear,enrolCategory)
            SELECT '$enrolmentNo',$programID,'$remark','Enrolled','$loginID',studentID,batchYear,enrolCategory FROM " .$this->tableID. " WHERE enrolmentID=" .$this->tupleID;
            $this->pdo->exec($this->sqlStmt);
            //$this->tupleID 	= $this->pdo->lastInsertId();
		}
	}
	
	function listBranchChanges($programID=0) {
		if ($programID) {
			$this->sqlStmt = "SELECT CONCAT('<img src=\'uploads/photos/', u.fileName, '\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,dateEnrolment,
			p.programCode,batchYear,enrolCategory,nameEnglish,e.nameHindi,gender,dateBirth,
			bloodGroup,pwdStatus,category,subCategory,religion,maritalStatus,nationality,aadhaarNo,homeAddress,homePIN,s.stateName,
			d.districtName,commAddress,commPIN,CASE WHEN e.area = 1 THEN 'Urban' ELSE 'Rural' END AS nativeArea,policeStation,e.phoneNo,e.email,xGrade,xYear,xBoard,xiiGrade,xiiYear,xiiBoard,
			ugGrade,ugYear,ugInstitute,pgGrade,pgYear,pgInstitute,lastScore,fatherName,fatherProfession,fatherPhone,fatherEmail,
			fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,guardianName,
			guardianProfession,relationWith,guardianPhone,guardianEmail,guardianAddress 
			FROM erp_aaa.aaa_enrolmentsv AS e LEFT JOIN erp_aaa.aaa_uploads AS u ON e.studentID=u.studentID AND u.docheadCode='PHO' INNER JOIN erp_aaa.aaa_programs AS p ON e.programID=p.programID INNER JOIN erp_core.erp_states AS s ON e.homeState=s.stateID INNER JOIN erp_core.erp_districts AS d ON e.homeDistrict=d.districtID 
			WHERE e.batchYear=" .$_SESSION['batchYear']. " AND e.programID=$programID AND estatus='Changed' ORDER BY e.enrolmentNo";
			$this->dbStmt = $this->pdo->query($this->sqlStmt);
			$rows = $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}


	//***********************************************************************
	//*							aaa miscellaneous functions					*
	//***********************************************************************

    function listProgramsA() {
		$this->dbStmt = $this->pdo->query("SELECT count(*) AS no,ProgramAllocated FROM erp_aaa.aaa_candidates GROUP BY ProgramAllocated");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}

	function getAlertIDBySubjectID($subjectID) {
		if ($subjectID) {
			$this->dbStmt = $this->pdo->query("SELECT alertID FROM erp_aaa.aaa_autoalerts WHERE subjectID=$subjectID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['alertID'];
		}
	}

	function getCandidateIDByRollNo($rollNo) {
		if ($rollNo) {
			$this->dbStmt = $this->pdo->query("SELECT candidateID FROM erp_aaa.aaa_candidates WHERE RollNo='$rollNo'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['candidateID'];
		}
	}
		
    function getApplicantIDByRollNo($rollNo) {
		if ($rollNo) {
			$this->dbStmt = $this->pdo->query("SELECT applicantID FROM erp_aaa.aaa_applicants WHERE rollNo='$rollNo'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['applicantID'];
		}
    }
	
    function getApplicantIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT applicantID FROM erp_aaa.aaa_applicants WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['applicantID'];
		}
	}
	
    function getRollNoByApplicantID($applicantID) {
		if ($applicantID) {
			$this->dbStmt = $this->pdo->query("SELECT rollNo FROM erp_aaa.aaa_applicants WHERE applicantID=$applicantID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['rollNo'];
		}
	}
	
    function getRollNoByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT rollNo FROM erp_aaa.aaa_applicants WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['rollNo'];
		}
	}

    function getEnrolmentIDByRollNo($rollNo) {
		if ($rollNo) {
			$this->dbStmt = $this->pdo->query("SELECT enrolmentID FROM erp_aaa.aaa_enrolments WHERE status='Enrolled' AND studentID=(SELECT studentID FROM erp_aaa.aaa_applicants WHERE rollNo='$rollNo')");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['enrolmentID'];
		}
	}
	
    function getEnrolmentIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT enrolmentID FROM erp_aaa.aaa_enrolments WHERE studentID=$studentID ORDER BY enrolmentID DESC LIMIT 0,1");// AND status='Enrolled'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['enrolmentID'];
		}
	}
	
    // moved to erp_model.php
	//function getEnrolmentIDByEnrolmentNo($enrolmentNo) {
	//	if ($enrolmentNo) {
	//		$this->dbStmt = $this->pdo->query("SELECT enrolmentID FROM erp_aaa.aaa_enrolments WHERE enrolmentNo='$enrolmentNo'");
	//		$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
	//		return $row['enrolmentID'];
	//	}
	//}
	
	function getEnrolmentIDByWithdrawID($withdrawID) {
		if ($withdrawID) {
			$this->dbStmt = $this->pdo->query("SELECT enrolmentID FROM erp_aaa.aaa_withdrawals WHERE withdrawID=$withdrawID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['enrolmentID'];
		}
	}

    function getEnrolmentNoByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT enrolmentNo FROM erp_aaa.aaa_enrolments WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['enrolmentNo'];
		}
	}
	
    function getRollNoByEnrolmentID($enrolmentID) {
		if ($enrolmentID) {
			$this->dbStmt = $this->pdo->query("SELECT rollNo FROM erp_aaa.aaa_applicants WHERE studentID=(SELECT studentID FROM erp_aaa.aaa_enrolments WHERE enrolmentID=$enrolmentID)");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['rollNo'];
		}
	}

	function getProgramNameByProgramID($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT programCode FROM erp_aaa.aaa_programs WHERE programID=$programID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['programName'];
		}
	}
	
    function getStudentIDByApplicantID($applicantID) {
		if ($applicantID) {
			$this->dbStmt = $this->pdo->query("SELECT studentID FROM erp_aaa.aaa_applicants WHERE applicantID=$enrolmentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['studentID'];
		}
	}
	
    function getStudentIDByEnrolmentID($enrolmentID) {
		if ($enrolmentID) {
			$this->dbStmt = $this->pdo->query("SELECT studentID FROM erp_aaa.aaa_enrolments WHERE enrolmentID=$enrolmentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['studentID'];
		}
	}

    function getStudentIDByRollNo($rollNo) {
		if ($rollNo) {
			$this->dbStmt = $this->pdo->query("SELECT studentID FROM erp_aaa.aaa_applicants WHERE rollNo='$rollNo'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['studentID'];
		}
	}

    function getStudentIDByMcaipID($mcaipID) {
		if ($mcaipID) {
			$this->dbStmt = $this->pdo->query("SELECT studentID FROM erp_aaa.aaa_enrolments WHERE mcaipID=$mcaipID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['studentID'];
		}
	}
	
	function getDocheadNameByDocheadCode($docheadCode, $programID) {
		$this->dbStmt = $this->pdo->query("SELECT docheadName FROM erp_aaa.aaa_documents WHERE docheadCode='$docheadCode' AND programID=$programID");
        $row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		return $row['docheadName'];
    }

	function getFileNameBydocheadCode($studentID, $docheadCode) {
		if ($studentID && $docheadCode) {
			$this->dbStmt = $this->pdo->query("SELECT fileName FROM erp_aaa.aaa_uploads WHERE studentID=$studentID AND docheadCode='$docheadCode'");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['fileName'];
    	}
	}
	
	function getPendingUploads($studentID) {
		if ($studentID) {
			// get uploaded documents
			$this->dbStmt = $this->pdo->query("SELECT count(docheadCode) AS no FROM erp_aaa.aaa_uploads WHERE studentID=$studentID");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			$upNos = $row['no'];
			
			// get program and category of the student
			$this->dbStmt = $this->pdo->query("SELECT programID,enrolCategory FROM erp_aaa.aaa_enrolmentsv WHERE studentID=$studentID");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			$programID = $row['programID'];
			$category = $row['enrolCategory'];
			
			// check document requirements
			$this->dbStmt = $this->pdo->query("SELECT count(docheadID) AS no FROM erp_aaa.aaa_doccharts WHERE batchYear=" .$_SESSION['batchYear']. " AND programID REGEXP CONCAT('(^|,)(', REPLACE('$programID', ',', '|'), ')(,|$)') AND category REGEXP CONCAT('(^|,)(', REPLACE('$category', ',', '|'), ')(,|$)')");
        	$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			$total = $row['no'] - 2; //reduce photo and signature files
			
			// check pendency
			if ($total > $upNos) return TRUE;
			else return FALSE;
    	}
	}

	function getProgramIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT programID FROM erp_aaa.aaa_applicantsv WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['programID'];
		}
	}

	function getProgramIDByProgramCode($programCode) {
		if ($programCode) {
			$this->dbStmt = $this->pdo->query("SELECT programID FROM erp_aaa.aaa_programs WHERE programCode='$programCode'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['programID'];
		}
	}

	function getProgramCodeByProgramID($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT programCode FROM erp_aaa.aaa_programs WHERE programID=$programID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['programCode'];
		}
	}

	function getParentIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT parentID FROM erp_aaa.aaa_parents WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['parentID'];
		} else {
			return 0;
		}
	}

   	function getRoundNoByInstituteID($degreeLevel, $programID=0) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT MAX(roundNo) as mroundNo FROM erp_aaa.aaa_candidates 
			WHERE programID=$programID AND instituteID=" .$_SESSION["instituteID"]. ") AND batchYear=" .$_SESSION['batchYear']);
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);	
		} else if ($degreeLevel) {
			$this->dbStmt = $this->pdo->query("SELECT MAX(roundNo) as mroundNo FROM erp_aaa.aaa_candidates 
			WHERE instituteID=" .$_SESSION["instituteID"]. " AND batchYear=" .$_SESSION['batchYear']. " AND degreeLevel='$degreeLevel'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);	
		}
		return $row['mroundNo'];
	}
	
    function getEducationIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT educationID FROM erp_aaa.aaa_educations WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['educationID'];
		} else {
			return 0;
		}
	}

    function getAccountIDByStudentID($studentID=0) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT accountID FROM erp_aaa.aaa_accounts WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return ($row['accountID'] ? $row['accountID'] : 0);
		} else {
			return 0;
		}
	}

	function getWithdrawIDByStudentID($studentID) {
		if ($studentID) {
			$this->dbStmt = $this->pdo->query("SELECT withdrawID FROM erp_aaa.aaa_withdrawals WHERE studentID=$studentID");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return ($row['withdrawID'] ? $row['withdrawID'] : 0);
		} else {
			return 0;
		}
	}
	
    function getGraduandIDByEnrolmentNo($enrolmentNo) {
		if ($enrolmentNo) {
			$this->dbStmt = $this->pdo->query("SELECT graduandID FROM erp_aaa.aaa_graduands WHERE enrolmentNo='$enrolmentNo'");
			$row = $this->dbStmt->fetch(PDO::FETCH_ASSOC);
			return $row['graduandID'];
		} else {
			return 0;
		}	
	}

 	function saveOTP($loginID,$groupID=6,$authMode='MDB',$otpSecret=0,$aclRead,$aclInsert=1,$aclUpdate=1,$aclDelete=0) {
        if ($this->pdo->query("SELECT * FROM erp_core.erp_users WHERE loginID='$loginID'")->rowCount()) {
            $this->sqlStmt = "UPDATE erp_core.erp_users SET loginID='$loginID',groupID=$groupID,authMode='$authMode',otpSecret=$otpSecret,aclRead=$aclRead,aclInsert=$aclInsert,aclUpdate=$aclUpdate,aclDelete=$aclDelete
            WHERE loginID='$loginID'";
 		} else {
			$this->sqlStmt = "INSERT INTO erp_core.erp_users(loginID,groupID,authMode,otpSecret,aclRead,aclInsert,aclUpdate,aclDelete)
            VALUES('$loginID',$groupID,'$authMode',$otpSecret,$aclRead,$aclInsert,$aclUpdate,$aclDelete)";
 		}
        $this->pdo->exec($this->sqlStmt);
	}
	
    function dumpCSV($SN,$RollNo,$Name,$Category,$PwD,$Gender,$DOB,$StateEligibility,$Nationality,$ProgramAllocated,$AllocatedCat,$AllocatedRank,$ChoiceNo,$Quota,$Round,$Status,$RCName,$RemarksbyRC) {
        if ($this->pdo->query("SELECT * FROM aaa_candidates WHERE RollNo='" .$this->tupleID. "'")->rowCount()) {
            $this->sqlStmt = "UPDATE aaa_cadidates SET RollNo='$RollNo',Name='$Name',Category='$Category',Gender='$Gender',DOB='$DOB',StateEligibility=$StateEligibility',
			Nationality='$Nationality',ProgramAllocated='$ProgramAllocated',AllocatedCat='$AllocatedCat',AllocatedRank='$AllocatedRank',ChoiceNo='$ChoiceNo',Quota='$Quota',
			$Round,$Status,$RCName,$RemarksbyRCphoneNo='$phoneNo',email='$email',homeAddress='$homeAddress',homeDistrict=$homeDistrict,homePIN=$homePIN,commAddress='$commAddress',
            WHERE RollNo='$RollNo'";
            $this->pdo->exec($this->sqlStmt);
        } else{
            $this->sqlStmt = "INSERT INTO " .$this->tableID. "(rollNo,nameEnglish,nameHindi,gender,dateBirth,phoneNo,email,homeAddress,homeDistrict,homeState,homePIN,commAddress,
			subCategory,category,religion,nationality,pwdStatus,bloodGroup,aadhaarNo,batchYear,programID,status,
			fatherAddress,fatherAddress,fatherAddress,fatherAddress,fatherAddress,motherName,motherProfession,motherPhone,motherEmail,motherAddress,guardianName,guardianProfession,guardianPhone,guardianEmail,guardianAddress)
            VALUES('$rollNo','$nameEnglish','$nameHindi','$gender','$dateBirth','$phoneNo','$email','$homeAddress',$homeDistrict,$homeState,$homePIN,'$commAddress',
			'$subCategory','$category','$religion','$nationality','$pwdStatus','$bloodGroup','$aadhaarNo'," .$_SESSION['batchYear']. ",$programID,'Offered',
			'$fatherName','$fatherProfession','$fatherPhone','$fatherEmail','$fatherAddress','$motherName','$motherProfession','$motherPhone','$motherEmail','$motherAddress','$guardianName','$guardianProfession','$guardianPhone','$guardianEmail','$guardianAddress')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
        }
	}	
     

	//***********************************************************************
	//*							aaa degree functions						*
	//***********************************************************************

    function saveDegree($programID,$hindiText1,$hindiText2,$hindiText3,$hindiText4,$englishText1,$englishText2,$englishText3,$englishText4) {
		if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE degreeID=" .$this->tupleID)->rowCount()) {
			$this->sqlStmt = "UPDATE " .$this->tableID. " SET programID=$programID,hindiText1='$hindiText1',hindiText2='$hindiText2',hindiText3='$hindiText3',hindiText4='$hindiText4',englishText1='$englishText1',englishText2='$englishText2',englishText3='$englishText3',englishText4='$englishText4'
			WHERE degreeID=" .$this->tupleID;
			$this->pdo->exec($this->sqlStmt);	
		} else {
			// offered Graduand thru manageOffer or edit candidature
			$this->sqlStmt = "INSERT INTO " .$this->tableID. "(programID,hindiText1,hindiText2,hindiText3,hindiText4,englishText1,englishText2,englishText3,englishText4)
            VALUES($programID,'$hindiText1','$hindiText2','$hindiText3','$hindiText4','$englishText1','$englishText2','$englishText3','$englishText4')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
		}
    }

    function getDegree($programID) {
		if ($programID) {
			$this->dbStmt = $this->pdo->query("SELECT * FROM erp_aaa.aaa_degrees WHERE programID=$programID");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
		}
	}
	
	//***********************************************************************
	//*							aaa graduand functions						*
	//***********************************************************************

    function saveGraduand($programID,$enrolmentNo,$nameEnglish,$nameHindi,$gradePoint,$topicPHD,$contactNo,$email) {
		if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE graduandID=" .$this->tupleID)->rowCount()) {
			if ($gradePoint) { // update by AAA staff
				$this->sqlStmt 	= "UPDATE " .$this->tableID. " SET programID=$programID,enrolmentNo='$enrolmentNo',nameEnglish='$nameEnglish',nameHindi='$nameHindi',gradePoint=$gradePoint,topicPHD='$topicPHD',contactNo='$contactNo',email='$email'
				WHERE graduandID=" .$this->tupleID;
			} else { // update by graduand
				$this->sqlStmt 	= "UPDATE " .$this->tableID. " SET nameEnglishx='$nameEnglish',nameHindix='$nameHindi',topicPHD='$topicPHD',contactNo='$contactNo',email='$email'
				WHERE graduandID=" .$this->tupleID;
			}
			$this->pdo->exec($this->sqlStmt);	
		} else {
			// offered Graduand thru manageOffer or edit candidature
			$this->sqlStmt = "INSERT INTO " .$this->tableID. "(programID,enrolmentNo,nameEnglish,nameHindi,gradePoint,topicPHD,contactNo,email)
            VALUES($programID,'$enrolmentNo','$nameEnglish','$nameHindi',$gradePoint,'$topicPHD','$contactNo','$email')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
		}
		//echo $this->sqlStmt;
    }
	
	function deleteGraduand() {
       	$this->sqlStmt 		= "DELETE FROM " .$this->tableID. " WHERE graduandID=" .$this->tupleID;
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getGraduand($graduandID) {
		if ($graduandID) {
			$this->dbStmt 	= $this->pdo->query("SELECT g.*,p.programCode 
			FROM erp_aaa.aaa_graduands AS g LEFT JOIN erp_aaa.aaa_programs AS p ON g.programID=p.programID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND g.graduandID=$graduandID");
        	return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    	}
	}
	
    function listGraduands($programID) {
        if ($programID) {
			//$this->dbStmt 	= $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', lcase(enrolmentNo), '.jpg\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,nameEnglish,nameEnglishx,nameHindi,nameHindix,gradePoint,topicPHD 
			//FROM erp_aaa.aaa_graduands AS g LEFT JOIN erp_aaa.aaa_programs AS p ON g.programID=p.programID 
			//WHERE batchYear=" .$_SESSION['batchYear']. " AND g.programID=$programID ORDER BY g.enrolmentNo");
			$this->dbStmt 	= $this->pdo->query("SELECT programCode,enrolmentNo,ucase(nameEnglish) as NameInEnglish,nameHindi as nameInHindi 
			FROM erp_aaa.aaa_graduands AS g LEFT JOIN erp_aaa.aaa_programs AS p ON g.programID=p.programID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND g.programID=$programID ORDER BY programCode,g.enrolmentNo");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
    
	function selectGraduand($programID) {
		if ($programID) {
			$this->dbStmt 	= $this->pdo->query("SELECT graduandID,CONCAT('<img src=\'uploads/photos/', lcase(enrolmentNo), '.jpg\' width=\'30\' height=\'40\'>') AS photo,enrolmentNo,nameEnglish,nameEnglishx,nameHindi,nameHindix,gradePoint,topicPHD 
			FROM erp_aaa.aaa_graduands 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY enrolmentNo");
			$rows 			= $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
		return $rows;
	}

    function printGraduands($programID=0) {
		$this->dbStmt 	= $this->pdo->query("SELECT enrolmentNo,ucase(nameEnglish) as NameEnglish,nameHindi 
		FROM erp_aaa.aaa_graduands 
		WHERE batchYear=" .$_SESSION['batchYear']. ($programID ? " AND programID=$programID" : ''). " ORDER BY enrolmentNo");
		return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
	}
	//***********************************************************************
	//*							aaa travelplan functions						*
	//***********************************************************************

    function saveTravelplan($enrolmentNo,$dateArrival,$dateDeparture,$campusStay,$companion,$contactNo,$email,$remark) {
		if ($this->pdo->query("SELECT * FROM " .$this->tableID. " WHERE enrolmentNo='$enrolmentNo'")->rowCount()) {
			$this->sqlStmt 	= "UPDATE " .$this->tableID. " SET dateArrival='$dateArrival',dateDeparture='$dateDeparture',campusStay='$campusStay',companion=$companion,remark='$remark'
			WHERE enrolmentNo='$enrolmentNo'";
			$this->pdo->exec($this->sqlStmt);	
		} else {
			// offered Graduand thru manageOffer or edit candidature
			$this->sqlStmt = "INSERT INTO " .$this->tableID. "(enrolmentNo,dateArrival,dateDeparture,campusStay,companion,remark)
            VALUES('$enrolmentNo','$dateArrival','$dateDeparture','$campusStay',$companion,'$remark')";
            $this->pdo->exec($this->sqlStmt);
            $this->tupleID = $this->pdo->lastInsertId();
		}
		//echo $this->sqlStmt;
		$graduandID = $this->getGraduandIDByEnrolmentNo($enrolmentNo);
		$this->sqlStmt 	= "UPDATE erp_aaa.aaa_graduands SET contactNo='$contactNo',email='$email'
		WHERE graduandID=$graduandID";
		$this->pdo->exec($this->sqlStmt);	
		
		//echo $this->sqlStmt;
    }
	
	function deleteTravelplan() {
       	$this->sqlStmt 		= "DELETE FROM " .$this->tableID. " WHERE enrolmentNo='$enrolmentNo'";
       	$this->pdo->exec($this->sqlStmt);
	}
	
    function getTravelplan($enrolmentNo) {
		if ($enrolmentNo) {
			$this->dbStmt 	= $this->pdo->query("SELECT g.*,p.programCode,t.planID,t.dateArrival,t.dateDeparture,t.campusStay,t.companion,t.remark 
			FROM erp_aaa.aaa_graduands AS g LEFT JOIN erp_aaa.aaa_travelplans AS t ON g.enrolmentNo=t.enrolmentNo LEFT JOIN erp_aaa.aaa_programs AS p ON g.programID=p.programID 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND g.enrolmentNo='$enrolmentNo'");
			return $this->dbStmt->fetch(PDO::FETCH_ASSOC);
    	}
	}
	
    function listTravelplans($programID) {
        if ($programID) {
			$this->dbStmt 	= $this->pdo->query("SELECT CONCAT('<img src=\'uploads/photos/', lcase(t.enrolmentNo), '.jpg\' width=\'30\' height=\'40\'>') AS photo,
			t.enrolmentNo,CONCAT(nameEnglish,' (',nameHindi,')') AS name,contactNo,email,dateArrival,dateDeparture,campusStay,companion,t.remark 
			FROM erp_aaa.aaa_travelplans AS t LEFT JOIN erp_aaa.aaa_graduands AS g ON t.enrolmentNo=g.enrolmentNo 
			WHERE batchYear=" .$_SESSION['batchYear']. " AND programID=$programID ORDER BY t.enrolmentNo");
			return $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
		}
	}
    
	function test() {
		$this->dbStmt 	= $this->pdo->query("SELECT studentID,nameHindi FROM erp_aaa.aaa_students");
		$rows 			= $this->dbStmt->fetchAll(PDO::FETCH_ASSOC);
			
		return $rows;
	}
}

//			try {
//        		$this->pdo->beginTransaction();
//	            $this->dbStmt->execute([$enrollNo, $applicantID, $this->tupleID]);
//        		$this->pdo->commit();
//    		} catch(PDOExecption $e) {
//        		$this->pdo->rollback();
//        		$this->dbError = "Error!: " . $e->getMessage();
//        	}            

?>