<?php
class StudentDetailMgmt
{
	public $studentdetail_id;
	public $student_id;
	public $student_rollno;
	public $cur_branchid;
	public $cur_semesterid;
	public $college_id;
	public $current_active;
	public $year_id;
	public $cur_shift;
	public $cur_branchyearid;
	public $student_tutionfee;

	public $student_tutionmafifee;
	public $student_admissionfee;

	public $student_admissionmafifee;

	function add()
	{
		$Qry1 = "";
		$Qry1 .= "student_id ='" . Escape($this->student_id) . "',";
		$Qry1 .= "cur_branchid ='" . Escape($this->cur_branchid) . "',";
		$Qry1 .= "cur_semesterid ='" . Escape($this->cur_semesterid) . "',";
		$Qry1 .= "cur_branchyearid ='" . Escape($this->cur_branchyearid) . "',";
		$Qry1 .= "student_rollno ='" . Escape($this->student_rollno) . "',";
		$Qry1 .= "college_id ='" . Escape($_SESSION["VedantnrCollegeId"]) . "',";
		$Qry1 .= "current_active ='" . Escape($this->current_active) . "',";
		$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";
		$Qry1 .= "ip_address='" . Escape($_SERVER['REMOTE_ADDR']) . "',";
		$Qry1 .= "createdon = '" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "INSERT INTO cms_studentdetail SET " . $Qry1 . " ";
			dbQuery($qry);
			$this->studentdetail_id = dbInsertedId();
		}
	}

	function AddNewSemester()
	{


		$Qry1 = "";
		$Qry1 .= "student_id ='" . Escape($this->student_id) . "',";
		$Qry1 .= "cur_branchid ='" . Escape($this->cur_branchid) . "',";
		$Qry1 .= "cur_semesterid ='" . Escape($this->cur_semesterid) . "',";
		$Qry1 .= "college_id ='" . Escape($_SESSION["VedantnrCollegeId"]) . "',";
		$Qry1 .= "current_active ='" . Escape($this->current_active) . "',";
		$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
		$Qry1 .= "cur_branchyearid ='" . Escape($this->cur_branchyearid) . "',";
		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "INSERT INTO cms_studentdetail SET " . $Qry1 . " ";
			//	echo $qry; exit;
			dbQuery($qry);
			$this->studentdetail_id = dbInsertedId();
		}
	}

	function EditNewSemester()
	{
		$Qry1 = "";
		$Qry1 .= "student_id = '" . Escape($this->student_id) . "',";
	//	$Qry1 .= "cur_branchid = '" . Escape($this->cur_branchid) . "',";
	//	$Qry1 .= "cur_semesterid = '" . Escape($this->cur_semesterid) . "',";
	//	$Qry1 .= "cur_branchyearid ='" . Escape($this->cur_branchyearid) . "',";
	//	$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "UPDATE cms_studentdetail SET " . $Qry1 . " 
						WHERE student_id ='" . Escape($this->student_id) . "' AND
						college_id='" . $_SESSION['VedantnrCollegeId'] . "' and
						current_active = 1  ";

//echo $qry;
			dbQuery($qry);

			$this->studentdetail_id = dbInsertedId();
			//	echo  $qry; exit; 
		}
	}

	function UpdateOldSemester($old_branch_id,$old_year_id,$old_branchyear_id , $old_semester_id, $new_student_id)
	{
		$qry = "UPDATE 
						cms_studentdetail 
					SET 
						current_active = $this->current_active 
					WHERE 
						cur_branchid = $old_branch_id 
						AND year_id = $old_year_id
						AND cur_branchyearid = $old_branchyear_id 
						AND cur_semesterid = $old_semester_id 
						AND student_id in($new_student_id)";
		dbQuery($qry);
	}

	function DeleteOldStudent($old_branch_id, $old_year_id,$old_branchyear_id ,$old_semester_id, $new_student_id)
	{
		$qry = "DELETE FROM 
						cms_studentdetail 
					WHERE 
						cur_branchid = $old_branch_id 
						AND year_id = $old_year_id
						AND cur_branchyearid = $old_branchyear_id 
						AND cur_semesterid = $old_semester_id 
						AND student_id in($new_student_id)";
		dbQuery($qry);
	}
	function UpdateBatch($batch_id, $student_ids)
	{
		$qry = "UPDATE cms_studentdetail SET batch_id = $batch_id
				WHERE 
					 student_id in($student_ids) AND college_id = ".$_SESSION['VedantnrCollegeId'];
					// echo $qry;
		dbQuery($qry);
	}
	
	function DeleteStudentDetail()
	{
		  $qry = "DELETE FROM 
						cms_studentdetail 
					WHERE 
						student_id in(" . $this->student_id . ")
						";
		dbQuery($qry);
	}
	function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}
		//ECHO $qry;
		//EXIT();
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return $out;

	}

	//for semester transfer
	function getAllRecords1($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}
//echo $qry;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return $out;
	}

	function getAllRecords2($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}

		//  	echo $qry; exit;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return $out;
	}

	function getnottransfferedids($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  group_concat(student_id) as student_ids FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}
		$qry .= " group by year_id";
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			$student_ids = $row['student_ids'];
			return $student_ids;
		}
		return out;
	}
	//new for fees
	function updatfee($fee_head, $amount, $student_id, $branch_id, $year_id, $branchyear_id, $semester_id)
	{
		$fee_column = strtolower(str_replace(' ', '_', $fee_head));

		$qry1 = "Update cms_studentdetail set $fee_column = '" . $amount . "'  where student_id = '" . $student_id . "' and cur_branchid  = '" . $branch_id . "' and year_id  = '" . $year_id . "' and cur_branchyearid   = '" . $branchyear_id . "' and cur_semesterid    = '" . $semester_id . "'  and college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' ";
		//echo $qry1;
		dbQuery($qry1);

	}

	function updatmafifee($fee_head, $amount,$student_id, $mafifees_date,$mafifees_refname, $branch_id, $year_id, $branchyear_id, $semester_id)
	{
		$fee_column = strtolower(str_replace(' ', '_', $fee_head));


		$qry1 = "Update cms_studentdetail set $fee_column = '" . $amount . "', mafifees_date = '" . $mafifees_date . "', mafifees_refname = '" . $mafifees_refname . "' where student_id = '" . $student_id . "' and cur_branchid  = '" . $branch_id . "' and year_id  = '" . $year_id . "' and cur_branchyearid   = '" . $branchyear_id . "' and cur_semesterid    = '" . $semester_id . "'  and college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' ";
		//echo $qry1;
		dbQuery($qry1);

	}

	
	function getfeesrecord($headNames,$condition)
	{
		$qry = "SELECT sd.student_id,sm.transport_facility, CONCAT(sm.student_surname , ' ' , sm.student_firstname , ' ' ,sm.student_lastname) AS student_name, "; 

		foreach ($headNames as $head) {
			$qry .= " sd." . $head['fee_field'] . ", ";
			$qry .= "sd." . $head['mafi_field'] . ", ";
		}
    $qry = rtrim($qry, ", ");
    $qry .= " FROM cms_studentdetail sd, cms_studentmaster sm
              WHERE sd.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' and 
			  sd.student_id=sm.student_id AND 
			  
			  sd.current_active =1 and sm.student_active =1";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		//exit;
		$out = dbQuery($qry);
		return $out;
	}
//save-suspend
	function Updatesuspend($condition)
	{
		$qry = "update cms_studentdetail set current_active=0 where 1=1";

		if ($condition != "") {
			$qry .= $condition;
		}
		dbQuery($qry);
	}

	function UpdateStudentStatus($student_id, $year_id)
	{

		$qry= "Update cms_studentdetail set current_active='1' WHERE student_id in(".Escape($student_id).") and current_active='0' and year_id= ".Escape($year_id);	
		
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

//report form i card
	function getstudentIcard($condition)
	{
		
		$qry = "SELECT 
		sm.student_id,
		sm.student_surname,
		sm.student_firstname, 
		sm.student_lastname ,
		sm.student_gturegno ,
		sm.student_gender,
		(SELECT branch_name FROM cms_branchmaster sec WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id = sd.cur_branchid) AS branch_name,
			(SELECT year_name FROM cms_yearmaster ym WHERE  ym.year_id = sd.year_id) AS year_name,
		(SELECT semester_name FROM cms_semestermaster sem  WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = sd.cur_semesterid) AS semester_name,
		(SELECT branchyear_name  FROM cms_branchyear bry  WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "'AND bry.branchyear_id = sd.cur_branchyearid) AS branchyear_name		    	 	   	 	
	  FROM cms_studentmaster sm,cms_studentdetail sd 
		WHERE sm.student_id = sd.student_id   ";


		
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function updatalumini($condition)
	{
		
		$qry = "update cms_studentdetail set current_active=0 where 1=1";	//echo $qry1;

		if ($condition != "") {
			$qry .= $condition;
		}
		dbQuery($qry);

	}


	function getstudentalimini($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		
		$qry = "SELECT 
		sm.student_id,
	  sm.student_gturegno,
	  sm.student_admdate, 
	  CONCAT(sm.student_surname , ' ' , sm.student_firstname , ' ' ,sm.student_lastname) AS studentname,
	  sm.student_birthdate,
	  sm.student_gender,
	  sm.student_admby,
	  sm.student_category,	
	  sm.student_mobilestudents,
	  sm.student_mobileparents, 
	  sm.leave_type,
	  sm.leave_reason,
	  sm.leave_date,
	  sd.year_id,
	  sd.cur_branchyearid,
	  sd.cur_semesterid,	  
		(SELECT branch_name 
	  FROM cms_branchmaster sec 
	  WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id = sd.cur_branchid) AS branch_name,
		  (SELECT year_name
		  FROM cms_yearmaster ym 
	   WHERE ym.year_id = sd.year_id) AS year_name,
	  (SELECT branchyear_name
		  FROM 	cms_branchyear bry 
	   WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = sd.cur_branchyearid) AS branchyear_name,
	   (SELECT semester_name
		  FROM cms_semestermaster sem 
	   WHERE sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = sd.cur_semesterid) AS semester_name		   	 
								
	  FROM
		cms_studentmaster sm,
	  cms_studentdetail sd				
	  WHERE								
	  sm.college_id  = '".$_SESSION["VedantnrCollegeId"]."' 
	  AND sm.student_id=sd.student_id
	  AND sm.student_active=0
	  AND sm.leave_type='a'
	  AND sd.current_active = 0";


		
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function updateStudentFeesHeads($headName,$headNameMafi)
	{
		
		$qry = "ALTER TABLE cms_studentdetail
				ADD COLUMN IF NOT EXISTS `$headName` INT(11),
				ADD COLUMN IF NOT EXISTS `$headNameMafi` INT(11)";
		
		dbQuery($qry);

	}


	function updateStudentFeesHeadsDrop($headName,$headNameMafi)
	{
		
		$qry = "ALTER TABLE cms_studentdetail
						DROP COLUMN IF EXISTS `$headName`,
						DROP COLUMN IF EXISTS `$headNameMafi`";
		
		dbQuery($qry);

	}

	function updateStudentFeesHeadschange($oldHeadNameFee,$oldHeadNameMafi,$headName,$headNameMafi)
			
	{
		
		$qry = "ALTER TABLE cms_studentdetail 
                          CHANGE `$oldHeadNameFee` `$headName` INT(11),
                          CHANGE `$oldHeadNameMafi` `$headNameMafi` INT(11)";
		
		dbQuery($qry);

	}

	function updateyeartransferpenddingfees($condition,$total_remain)
	{
		$qry = "UPDATE cms_studentmaster SET last_year_debit = '$total_remain' where 1=1 ";
		if ($condition != "") {
			$qry .= $condition;
		}
		echo $qry;
							$out = dbQuery($qry);
							if (dbRows($out) > 0) {
								return $out;
							}
							return $out;
	}
}
?>