<?php
class StudentFeesRecMgmt
{
	public $feesreceive_id;
	public $student_id;
	public $semester_id;
	public $branch_id;
	public $division_id;
	public $college_id;
	public $year_id;
	public $receipt_no;
	public $receipt_date;
	public $receive_amt;
	public $cheque_no;
	public $ref_no;
	public $bank_name;
	public $comment_show;
	public $comments;
	public $login_id;
	public $createdon;
	public $allsem;
	public $all_receive_amt;
	public $discount;
	public $total_receive_amt;
	public $amount_discount;


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

		$qry = "
		SELECT
		frm.feesreceive_id,
		frm.receipt_no,
		frm.receipt_date,
		frm.receive_amt,
		CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
		sm.student_gturegno,
		frm.branch_id,
		frm.semester_id,
		frm.year_id,
		frm.branchyear_id,
		(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id=frm.branch_id) AS branch_name,
	  	(SELECT year_name 
				FROM cms_yearmaster ym  WHERE  ym.year_id  =frm.year_id) AS year_name,
		(SELECT branchyear_name
			   	 FROM cms_branchyear bry 
			     WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = frm.branchyear_id) AS branchyear_name,
		(SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = frm.semester_id) AS semester_name	  	
		FROM
		cms_feesreceivemaster frm,
		cms_studentmaster sm
		WHERE
		frm.student_id = sm.student_id AND
		frm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";


		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";
		}

		$out = dbQuery($qry);

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

		return $out;
	}
	function getCountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->feesrec_count=0;
		$qry = "";
        $qry = "SELECT Count(*) AS Count	  		   	 
		  	  FROM
			  cms_feesreceivemaster frm,
				cms_studentmaster sm,
				cms_studentdetail sd
						
		  	  WHERE 
			  	sm.student_id   = sd.student_id and 
				sm.college_id    = sd.college_id and 
				frm.student_id  = sm.student_id and						  
				frm.student_id  = sd.student_id and						    				
				sd.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' and 
				sm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "'and 	
				frm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' 	";
				
						
		if ($condition != "" ) {
			$qry.= $condition;
        }
		
		$out=dbQuery($qry);

        if (dbRows($out)>0) {
			$row=dbArray($out);
            extract($row);		
            $this->feesrec_count=$Count;            
			dbResult($out);
        }
		return $this->feesrec_count;
    }
	
	function getAllRecordsexam($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->feesrec_count = 0;

		$qry = "
		SELECT
			*
		FROM
		cms_chequefeereceive frm,					
		cms_branchmaster bm,
		cms_branchmaster bm,
		cms_branchyear bym,
		cms_yearmaster ym,
		cms_semestermaster sem,
		cms_usermaster um	
		WHERE
		um.user_id = frm.login_id AND	
		frm.semester_id = sem.semester_id AND
		frm.branch_id = bm.branch_id AND
		frm.branchyear_id = bym.branchyear_id AND
		frm.year_id = ym.year_id AND
		frm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";


		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";
		}

		$out = dbQuery($qry);

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

		return $out;
	}

	function getAllRecordsForFees($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		$qry = "
		select
		fh.head_name,
		fh.head_id,
		fh.fees_type,
		fs.setup_id,
		fs.semester_id,
		fs.fees_amount
		from
		cms_feeshead fh left join cms_feessetup fs on fh.head_id = fs.head_id";

		if ($condition != "") {
			$qry .= $condition;
		}
		$qry .= "WHERE
		( fs.fees_amount!='' OR fh.head_active !=0 )
		AND fh.college_id = " . $_SESSION['VedantnrCollegeId'];

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

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

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getAllRecordsRecivedFees($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		/* start new try */

		$qry = "
		select
		frm.discount,
		frm.receive_amt,
		frd.recdetail_amt
		from
		cms_feesreceivedetail frd,
		cms_feesreceivemaster frm
		where
		frm.feesreceive_id = frd.feesreceive_id 
		AND frm.college_id = " . $_SESSION['VedantnrCollegeId'];



		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);
		return $out;
	}

	function CheckAdmission($student_id)
	{
		$qry = "";
		$qry = "
		select
		student_admby,
		tfw_scheme
		from
		cms_studentmaster
		where
		student_id='" . $student_id . "'
		AND college_id = " . $_SESSION['VedantnrCollegeId'];

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$resut = dbArray($out);
			return $resut;
		}
		return out;
	}

	function checknewadmission($student_id)
	{
		$qry = "";
		$qry = "SELECT *
		FROM `cms_feesreceivemaster`
		where
		student_id='" . $student_id . "'
		AND college_id = " . $_SESSION['VedantnrCollegeId'];

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$resut = dbArray($out);
			if ($resut["student_id"] != "") {
				return "1";
			}
		} else {
			return "0";
		}
	}

	function getAllRecordsRecivedFeesView($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		$qry = "select frm.feesreceive_id,
		frm.receipt_no,
		frm.branch_id,
		frm.semester_id,
		frm.branchyear_id,
		frm.year_id,
		frm.student_id,
		frm.receipt_date,
		frm.receive_amt,
		frm.discount,
		frm.month_duration_to,
		frm.month_duration_from,
	
		(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id= frm.branch_id) AS branch_name,
	  	(SELECT year_name 
				FROM cms_yearmaster ym  WHERE  ym.year_id = frm.year_id) AS year_name,
		(SELECT branchyear_name
			   	 FROM cms_branchyear bry 
			     WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = frm.branchyear_id) AS branchyear_name,
		(SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = frm.semester_id) AS semester_name	  	
		
		from cms_feesreceivemaster as frm
		where
		frm.college_id = " . $_SESSION['VedantnrCollegeId'];
		//college_id = ".$_SESSION['VedantnrCollegeId']." AND year_id = ".$_SESSION["VedantnrYearId"];
		//college_id = ".$_SESSION['VedantnrCollegeId']." AND year_id = ".$_SESSION["VedantnrYearId"]

		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) {
			return $out;
		}
		return $out;
	}

	function getAllRecordsRecivedFeesDetailView($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		$qry = "select
		fd.head_id,
		fh.head_name,
		fd.recdetail_amt
		from
		cms_feesreceivedetail fd, cms_feeshead fh where ";


		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) {
			return $out;
		}
		return $out;
	}

	function getreciptNO1()
	{
		$qry = "";
		$qry = "SELECT Max(`receipt_no`)+ 1 As receipt_no
		FROM `cms_feesreceivemaster`
		
		where fees_type = 'T' 
		AND college_id = " . $_SESSION['VedantnrCollegeId'];

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

		if (dbRows($out) > 0) {
			$resut = dbArray($out);
			if ($resut["receipt_no"] == "") {
				return "1";
			} else {
				$qry1 = "SELECT receipt_date
									FROM `cms_feesreceivemaster`
								 where fees_type = 'T' 
									 AND college_id = " . $_SESSION['VedantnrCollegeId'] . "
									 AND receipt_no = 1";
				$out = dbQuery($qry1);

				if (dbRows($out) > 0) {
					$resut1 = dbArray($out);
					$qry2 = "SELECT Max(`receipt_no`)+1 As receipt_no
										FROM `cms_feesreceivemaster`
										where fees_type = 'T' 
										 AND college_id = " . $_SESSION['VedantnrCollegeId'] . "
										 AND receipt_date >= '" . $resut1["receipt_date"] . "'";
					$out = dbQuery($qry2);

					if (dbRows($out) > 0) {
						$resut2 = dbArray($out);
					}
				}
				return $resut2["receipt_no"];
			}
		}
		return out;
	}

	// function getreciptNO($condition)
	// {
	// 	$qry = "";
	// 	$qry = "SELECT Max(`receipt_no`)+1 As receipt_no
	// 	FROM `cms_feesreceivemaster`
	// 	where
	// 	college_id = " . $_SESSION['VedantnrCollegeId'];


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

	// 	if (dbRows($out) > 0) {
	// 		$resut = dbArray($out);
	// 		if ($resut["receipt_no"] == "") {
	// 			return "1";
	// 		} else {

	// 			return $resut["receipt_no"];
	// 		}
	// 	}
	// 	return $out;
	// }
	function getreciptNO($condition)
	{
		$qry = "";
		$qry = "SELECT CASE WHEN
				MAX(CAST(SUBSTRING_INDEX(receipt_no, '/', -1) AS UNSIGNED)) +1 > 0 THEN MAX(CAST(SUBSTRING_INDEX(receipt_no, '/', -1) AS UNSIGNED)) +1 ELSE 1
				END AS receipt_no,
				receipt_date
				FROM cms_feesreceivemaster 
				WHERE
					college_id =" . $_SESSION['VedantnrCollegeId'] . " AND YEAR(receipt_date) = YEAR(NOW()) AND MONTH(receipt_date) >= 4";


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

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


		return $out;
	}
	function getbranchyear( $condition="")
	{
		$qry = "";
		$qry = "SELECT b.branch_name,
		y.year_name FROM cms_branchmaster b, cms_yearmaster y WHERE 1=1";

		if ($condition != "") {
			$qry .= $condition;
		}
		
		//	echo $qry; exit;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}
	function getstudentreciptNO()
	{
		$qry = "";
		$qry = "SELECT receipt_no
		FROM `cms_feesreceivemaster`
		where
		student_id='" . $this->student_id . "'
		AND receipt_date = '" . $this->receipt_date . "'";

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$resut = dbArray($out);
			if ($resut["receipt_no"] == "") {
				return "1";
			} else {
				return $resut["receipt_no"];
			}
		}
		return $out;
	}

	function verifyDate($condition, $date_entered)
	{

		$date_entered = explode("-", $date_entered);

		$date_entered[2] = substr($date_entered[2], 0, 2);

		$qry = "select receipt_date from cms_feesreceivemaster where college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";


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

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			$date = $row["receipt_date"];
			$date = explode("-", $date);
			if (mktime(0, 0, 0, $date[1], $date[2], $date[0]) > mktime(0, 0, 0, $date_entered[1], $date_entered[2], $date_entered[0])) {
				return $row["receipt_date"];
			}
			// verify date if false return false
		}

		$qry1 = "select receipt_date from cms_feesreceivemaster where college_id ='" . $_SESSION["VedantnrCollegeId"] . "'  and receipt_no = '$feesrec_no'+1 AND year_id = " . $year_id;
		$out1 = dbQuery($qry1);
		if (dbRows($out1) > 0) {
			$row1 = dbArray($out1);
			$date = $row1["receipt_date"];
			$date = explode("-", $date);
			if (mktime(0, 0, 0, $date[1], $date[2], $date[0]) < mktime(0, 0, 0, $date_entered[1], $date_entered[2], $date_entered[0])) {
				return $row["receipt_date"];
			}
			// verify date if false return false
		}
		return "true";
	}
	function getAllRecordsRecivedFeesEdit1($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		$qry = "SELECT
		fh.head_id,
		fh.head_name,
		fh.fees_type,
		frm.comments,
		frm.fees_type,
		frm.month_duration_to,
		frm.month_duration_from,
		frm.discount,
		frm.receipt_no,
		fs.fees_amount AS fees_amount,
		sum(frd.recdetail_amt) AS recdetail_amt
		FROM
		cms_feessetup fs,
		cms_feeshead fh,
		cms_feesreceivedetail frd,
		cms_feesreceivemaster frm
		WHERE
		 frd.head_id = fs.head_id and
		fs.year_id = 14 AND frm.semester_id = fs.semester_id and
		frm.feesreceive_id = frd.feesreceive_id AND
		frd.head_id = fh.head_id
		AND frm.year_id = 14
		AND frm.college_id = " . $_SESSION['VedantnrCollegeId'];
		if ($condition != "") {
			$qry .= $condition;
		}
		$qry .= "GROUP BY frd.head_id
		ORDER BY frd.head_id ASC";
		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) {
			return $out;
		}
		return $out;
	}
	function getAllRecordsRecivedFeesEdit($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "";
		$qry = "SELECT
		fh.head_id,
		fh.head_name,
		fh.fees_type,
		frm.comments,
		frm.fees_type,
		frm.month_duration_to,
		frm.month_duration_from,
		frm.discount,
		frm.receipt_no,
		(SELECT fees_amount
		FROM cms_feessetup fs
		WHERE fs.year_id = frm.year_id AND frm.branchyear_id = fs.branchyear_id AND frm.semester_id = fs.semester_id AND frd.head_id = fs.head_id) AS fees_amount,
		sum(frd.recdetail_amt) AS recdetail_amt
		FROM
		cms_feeshead fh,
		cms_feesreceivedetail frd,
		cms_feesreceivemaster frm
		WHERE
		frm.feesreceive_id = frd.feesreceive_id AND
		frd.head_id = fh.head_id
		AND frm.college_id = " . $_SESSION['VedantnrCollegeId'];
		if ($condition != "") {
			$qry .= $condition;
		}
		$qry .= "GROUP BY frd.head_id
		ORDER BY frd.head_id ASC";

		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) {
			return $out;
		}
		return $out;
	}
	function add()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "student_id='" . Escape($this->student_id) . "',";
		$Qry1 .= "semester_id='" . Escape($this->semester_id) . "',";
		$Qry1 .= "branch_id='" . Escape($this->branch_id) . "',";
		$Qry1 .= "branchyear_id='" . Escape($this->branchyear_id) . "',";
		$Qry1 .= "college_id='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
		$Qry1 .= "year_id='" . Escape($this->year_id) . "',";
		$Qry1 .= "receipt_no='" . Escape($this->receipt_no) . "',";
		$Qry1 .= "receipt_date=	 STR_TO_DATE('" . Escape($this->receipt_date) . "','$SQLDateFormat'),";
		$Qry1 .= "receive_amt='" . Escape($this->receive_amt) . "',";

		$Qry1 .= "discount='" . Escape($this->discount) . "',";
		$Qry1 .= "total_receive_amt='" . Escape($this->total_receive_amt) . "',";

		$Qry1 .= "fees_type='" . Escape($this->fee_typehidden) . "',";
		$Qry1 .= "ref_no='" . Escape($this->ref_no) . "',";
		$Qry1 .= "cheque_no='" . Escape($this->cheque_no) . "',";
		$Qry1 .= "bank_name='" . Escape($this->bank_name) . "',";
		$Qry1 .= "comment_show='" . Escape($this->comment_show) . "',";
		$Qry1 .= "comments='" . Escape($this->comments) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into cms_feesreceivemaster set " . $Qry1 . " ";
			//echo $qry; exit;
			dbQuery($qry);
			$this->feesreceive_id = dbInsertedId();
		}
	}
	function edit()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "feesreceive_id='"			.Escape($this->feesreceive_id)."',";
		$Qry1 .= "student_id='" . Escape($this->student_id) . "',";
		$Qry1 .= "semester_id='" . Escape($this->semester_id) . "',";
		$Qry1 .= "branch_id='" . Escape($this->branch_id) . "',";
		$Qry1 .= "branchyear_id='" . Escape($this->branchyear_id) . "',";
		$Qry1 .= "college_id='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
		$Qry1 .= "year_id='" . Escape($this->year_id) . "',";
		$Qry1 .= "receipt_no='" . Escape($this->receipt_no) . "',";
		$Qry1 .= "receipt_date=	 STR_TO_DATE('" . Escape($this->receipt_date) . "','$SQLDateFormat'),";
		$Qry1 .= "receive_amt='" . Escape($this->receive_amt) . "',";

		$Qry1 .= "discount='" . Escape($this->discount) . "',";
		$Qry1 .= "total_receive_amt='" . Escape($this->total_receive_amt) . "',";

		$Qry1 .= "fees_type='" . Escape($this->fee_typehidden) . "',";
		$Qry1 .= "ref_no='" . Escape($this->ref_no) . "',";
		$Qry1 .= "cheque_no='" . Escape($this->cheque_no) . "',";
		$Qry1 .= "bank_name='" . Escape($this->bank_name) . "',";
		$Qry1 .= "comment_show='" . Escape($this->comment_show) . "',";
		$Qry1 .= "comments='" . Escape($this->comments) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";
		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_feesreceivemaster set " . $Qry1 . " where feesreceive_id =" . $this->feesreceive_id;
			//echo $qry;
			dbQuery($qry);
		}
	}
	function delete()
	{
		$qry = "delete from  cms_feesreceivemaster
		where feesreceive_id in (" . $this->feesreceive_id . ")
		and college_id='" . $_SESSION["VedantnrCollegeId"] . "'";
		//echo $qry;
		dbQuery($qry);
	}
	function deleteexam()
	{
		$qry = "delete from  cms_chequefeereceive
		where cheque_id in (" . $this->cheque_id . ")
		and college_id='" . $_SESSION["VedantnrCollegeId"] . "' ";
		dbQuery($qry);

	}
	function addcheque()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "student_name='" . Escape($this->student_name) . "',";
		$Qry1 .= "semester_id='" . Escape($this->semester_id) . "',";
		$Qry1 .= "branch_id='" . Escape($this->branch_id) . "',";
		$Qry1 .= "division_id='" . Escape($this->division_id) . "',";
		$Qry1 .= "college_id='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
		$Qry1 .= "year_id='" . Escape($this->year_id) . "',";
		$Qry1 .= "receipt_no='" . Escape($this->receipt_no) . "',";
		$Qry1 .= "student_rollno='" . Escape($this->student_rollno) . "',";
		$Qry1 .= "receipt_date=	 STR_TO_DATE('" . Escape($this->receipt_date) . "','$SQLDateFormat'),";
		$Qry1 .= "receive_amt='" . Escape($this->receive_amt) . "',";
		$Qry1 .= "fees_type='" . Escape($this->fee_typehidden) . "',";
		$Qry1 .= "cheque_no='" . Escape($this->cheque_no) . "',";
		$Qry1 .= "bank_name='" . Escape($this->bank_name) . "',";
		$Qry1 .= "ifsc_code='" . Escape($this->ifsc_code) . "',";
		$Qry1 .= "latefee='" . Escape($this->latefee) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into cms_chequefeereceive set " . $Qry1 . " ";
			//echo $qry; exit;

			dbQuery($qry);
			$this->feesreceive_id = dbInsertedId();
		}
	}
	function getreciptchequeNO($semester_id)
	{
		$qry = "";
		$qry = "SELECT Max(`receipt_no`)+ 1 As receipt_no
		FROM `cms_chequefeereceive`
		where 
		semester_id = $semester_id A
		AND college_id = " . $_SESSION['VedantnrCollegeId'];

		$out = dbQuery($qry);
		//echo $out; exit;
		if (dbRows($out) > 0) {
			$resut = dbArray($out);
			if ($resut["receipt_no"] == "") {
				return "1";
			} else {
				$qry1 = "SELECT receipt_date
									FROM `cms_chequefeereceive`
								 where  semester_id = $semester_id 
									 AND college_id = " . $_SESSION['VedantnrCollegeId'] . "
									 AND receipt_no = 1";
				$out = dbQuery($qry1);
				if (dbRows($out) > 0) {
					$resut1 = dbArray($out);
					$qry2 = "SELECT Max(`receipt_no`)+1 As receipt_no
										FROM `cms_chequefeereceive`
										where semester_id = $semester_id 
										 AND college_id = " . $_SESSION['VedantnrCollegeId'] . "
										 AND receipt_date >= '" . $resut1["receipt_date"] . "'";
					$out = dbQuery($qry2);

					if (dbRows($out) > 0) {
						$resut2 = dbArray($out);
					}
				}
				return $resut2["receipt_no"];
			}
		}
		return out;
	}
	function duplicatecheck()
	{
		$qry = "select * from cms_feesreceivemaster where receipt_no='" . $this->receipt_no . "' AND college_id = '" . $_SESSION['VedantnrCollegeId'] . "'";
		$out = dbQuery($qry);
		return $out;
	}
	//add new for duplicate fees receipt
	function getAllFeesReceipt($condition)
	{
		$qry = "SELECT 
		sm.student_id,
		sm.student_grno,
		sm.student_gturegno,
				  CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
				fm.feesreceive_id,
				fm.student_id,
				fm.branchyear_id,
				fm.branch_id,
				fm.year_id,
				fm.semester_id,
				fm.receipt_date,					
				fm.receive_amt,	
				fm.receipt_no,					
				fm.discount,
				fm.ref_no,										
				fm.cheque_no,
				fm.bank_name,
				fm.comments,
				(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id=fm.branch_id) AS branch_name,
				(SELECT year_name 
						FROM cms_yearmaster ym  WHERE  ym.year_id  =fm.year_id) AS year_name,
				(SELECT branchyear_name
						FROM cms_branchyear bry 
						WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = fm.branchyear_id) AS branchyear_name,
				(SELECT semester_name
						FROM cms_semestermaster sem 
						WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = fm.semester_id) AS semester_name	  	
				FROM
				cms_feesreceivemaster fm,
				cms_studentmaster sm
				WHERE 
				fm.student_id  = sm.student_id and		
				sm.college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' and		  					  
				fm.college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' ";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);
		return $out;
	}
	function getfeesdetailrecord($condition)
	{
		$qry = "select fm.*, 
		fd.recdetail_amt
		
		from 
		cms_feesreceivemaster fm,
		cms_feesreceivedetail fd
		where fm.college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' AND 
		fd.college_id  = '" . $_SESSION["VedantnrCollegeId"] . "' AND
			fm.feesreceive_id= fd.feesreceive_id";
		if ($condition != "") {
			$qry .= $condition;
		}

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


		return $out;

	}

	function getStudentbranch($branch_id)
	{

		$qry = "SELECT branch_id 
		FROM cms_feesreceivemaster 
		WHERE branch_id = $branch_id ";
		//echo $qry;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}


	function getStudentSemester($semester_id)
	{
		$qry = "SELECT semester_id FROM cms_feesreceivemaster WHERE semester_id = $semester_id ";

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

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}


	function getStudentSemesters($student_id)
	{
		$qry = "SELECT semester_id FROM cms_feesreceivemaster WHERE student_id in( $student_id )";
		//echo $qry;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getAllFeesReceiptdetails($condition)
	{
		$qry = "SELECT fm.*, fd.*,fh.head_name
                                 FROM cms_feesreceivemaster fm,
                                 cms_feesreceivedetail fd,
								 cms_feeshead fh 
                                 WHERE fm.feesreceive_id = fd.feesreceive_id and 
								 fd.head_id = fh.head_id and  
								 fm.college_id = '" . Escape($_SESSION['VedantnrCollegeId']) . "' ";
		if ($condition != "") {
			$qry .= $condition;
		}
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getFeesReceipt($condition)
	{
		$qry = "SELECT * FROM cms_feesreceivemaster fm
			WHERE  fm.college_id = " . $_SESSION['VedantnrCollegeId'];
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getAllFeesReceipthead($condition)
	{
		$qry = " SELECT frm.*,
						fh.head_name,
						frd.recdetail_amt
					FROM
						cms_feesreceivemaster frm,
						cms_feesreceivedetail frd,
						cms_feeshead fh	
					WHERE
						frd.head_id = fh.head_id AND
						frm.feesreceive_id = frd.feesreceive_id AND
						frm.college_id = " . $_SESSION['VedantnrCollegeId'] . " ";
		if ($condition != "") {
			$qry .= $condition;
		}

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

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	//student-fees-records
	function getAllFees($condition)
	{
		$qry = "SELECT * FROM cms_feesreceivemaster WHERE college_id = " . $_SESSION['VedantnrCollegeId'];

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

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

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getstudentfeesdata($condition)
	{
		$qry = "
		SELECT frm.discount, fh.head_name, frd.recdetail_amt
		FROM cms_feesreceivemaster frm,
		 cms_feesreceivedetail frd ,
		 cms_feeshead fh 
		WHERE frm.feesreceive_id = frd.feesreceive_id and 
		frd.head_id = fh.head_id ";

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

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

		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	//student-feereceipt
	function getAllRecord($condition)
	{


		$qry = "SELECT 
    frm.feesreceive_id,
    frm.student_id,
    frm.receipt_no,
    frm.receipt_date,
	frm.ref_no,
    frm.cheque_no,
    frm.bank_name,
    frm.comment_show,
    frm.comments,
    frm.branch_id,
    frm.semester_id,
    frm.createdon,
    frm.discount
FROM
    cms_feesreceivemaster frm
WHERE
     frm.college_id = '" . $_SESSION['VedantnrCollegeId'] . "'";


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


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

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

	function getstudentfeereceipt($condition)
	{


		$qry = "SELECT stud.student_gturegno,
								stud.student_grno,
								CONCAT(stud.student_surname,' ',stud.student_firstname,' ',SUBSTR(stud.student_lastname,1,1),'.') AS student_name,
								(SELECT branch_name 
										FROM cms_branchmaster bm 
										WHERE bm.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sd.cur_branchid =bm.branch_id) AS branch_name,
								(SELECT year_name 
										FROM cms_yearmaster ym  WHERE  sd.year_id  =ym.year_id) AS year_name,
								(SELECT branchyear_name
										FROM cms_branchyear bry 
										WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_branchyearid = bry.branchyear_id) AS branchyear_name,
								(SELECT semester_name
										FROM cms_semestermaster sem 
										WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_semesterid = sem.semester_id) AS semester_name	  												
							FROM
								cms_studentmaster stud,
								cms_studentdetail sd	
							WHERE
								stud.student_id = sd.student_id  AND
								sd.college_id = '" . $_SESSION['VedantnrCollegeId'] . "'  AND
								stud.college_id = '" . $_SESSION['VedantnrCollegeId'] . "' ";
		if ($condition != "") {
			$qry .= $condition;
		}

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

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

		return $out;
	}

	function getstudentfeehead($condition)
	{


		$qry = "SELECT
		   fh.head_name,
		   frd.recdetail_amt
	   FROM
		   cms_feesreceivemaster frm,
		   cms_feesreceivedetail frd,
		   cms_feeshead fh			
	   WHERE
		   fh.head_name != 'College Dev. Fee' AND
		   fh.head_name != 'Scope Fee'  AND
		   frm.college_id = '" . $_SESSION['VedantnrCollegeId'] . "'  AND
		   frm.feesreceive_id = frd.feesreceive_id AND
		   frd.head_id = fh.head_id ";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	//fees collection
	function getfeescollection($condition)
	{

		$qry = "SELECT
		frm.feesreceive_id,
					frm.receipt_no,
					sm.student_gturegno,
					CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
					frm.receive_amt,
					frm.total_receive_amt,
					frm.ref_no,
					frm.cheque_no,
					frm.discount,
					frm.branch_id,
					frm.year_id,
					frm.branchyear_id,
					frm.semester_id,
					frm.student_id,
					frm.receipt_date,
					(SELECT user_name 
								FROM cms_usermaster um 
								WHERE um.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and frm.login_id =um.user_id) AS user_name,
					
					(SELECT branch_name 
								FROM cms_branchmaster bm 
								WHERE bm.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and frm.branch_id =bm.branch_id) AS branch_name,
						(SELECT year_name 
								FROM cms_yearmaster ym  WHERE  frm.year_id  =ym.year_id) AS year_name,
						(SELECT branchyear_name
								FROM cms_branchyear bry 
								WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND frm.branchyear_id = bry.branchyear_id) AS branchyear_name,
						(SELECT semester_name
								FROM cms_semestermaster sem 
								WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND frm.semester_id = sem.semester_id) AS semester_name	  							
			
				FROM
					cms_feesreceivemaster frm,
					cms_studentmaster sm	
				WHERE
					sm.student_id = frm.student_id ";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}
	function getfeescollection2($condition)
	{

		$qry = "SELECT SUM(receive_amt -discount) AS receive_amt,
										um.user_id,
										um.user_name
									FROM
										cms_feesreceivemaster frm,
										cms_usermaster um	 		 
									WHERE
										um.user_id = frm.login_id ";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}
	
	function getfeescollection3($condition)
	{

		$qry = "SELECT head_id,head_name FROM cms_feeshead WHERE head_name != 'ACPDC Adm Fee' ";

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

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

		return $out;
	}

	function getfeescollection4($condition)
	{

		$qry = "SELECT 
								cms_feeshead.head_id,
								recdetail_amt 
							FROM 
								cms_feeshead LEFT JOIN cms_feesreceivedetail
								ON cms_feeshead.head_id = cms_feesreceivedetail.head_id";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	function getfeesdueamt($condition)
	{

		$qry = "
		SELECT
			sm.student_id,
			sm.student_gturegno,
			CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
			YEAR(sm.student_admdate) as adm_year,
			sm.student_admby,
			sm.student_techcerti,
			sm.tfw_scheme,
			sm.student_scholarship,
			student_mobilestudents,
			student_mobileparents,
			(SELECT branch_name 
					FROM cms_branchmaster bm 
					WHERE bm.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sd.cur_branchid =bm.branch_id) AS branch_name,
			(SELECT year_name 
					FROM cms_yearmaster ym  WHERE  sd.year_id  =ym.year_id) AS year_name,
			(SELECT branchyear_name
					FROM cms_branchyear bry 
					WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_branchyearid = bry.branchyear_id) AS branchyear_name,
			(SELECT semester_name
					FROM cms_semestermaster sem 
					WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sd.cur_semesterid = sem.semester_id) AS semester_name	  							
		FROM
			cms_studentmaster sm,
			cms_studentdetail sd
		WHERE
			sm.student_id = sd.student_id AND
			sm.student_active = 1";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	function getfeesdueamt1($headNames, $condition)
	{

		$qry = "SELECT sd.student_id ";
		foreach ($headNames as $head) {
			$qry .= ", sd.{$head['fee_field']} , sd.{$head['mafi_field']}";
		}
		$qry .= " FROM cms_studentdetail sd
								   WHERE 1=1 ";


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

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

		return $out;
	}

	function getfeesdueamt3($condition)
	{

		$qry = "
		SELECT
			SUM(recdetail_amt) AS receive_amt
		FROM
			cms_feesreceivemaster frm,
			cms_feesreceivedetail frd
		WHERE
			frm.feesreceive_id = frd.feesreceive_id ";


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

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

		return $out;
	}


	function getfeessummary($condition)
	{

		$qry = "SELECT
		frm.feesreceive_id,
		MAX(frm.receipt_no) as receipt,
		MIN(frm.receipt_no) as receipt1,
		sm.student_gturegno,
		CONCAT(sm.student_surname,' ',sm.student_firstname,' ',sm.student_lastname) AS student_name,
		SUM(frm.receive_amt) as receive_amt ,
		frm.total_receive_amt,
		frm.discount,
		frm.ref_no,
		frm.cheque_no,
		frm.receipt_date,
		(SELECT user_name 
				FROM cms_usermaster um 
				WHERE um.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and frm.login_id =um.user_id) AS user_name,
	
	(SELECT branch_name 
				FROM cms_branchmaster bm 
				WHERE bm.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and frm.branch_id =bm.branch_id) AS branch_name,
		(SELECT year_name 
				FROM cms_yearmaster ym  WHERE  frm.year_id  =ym.year_id) AS year_name,
		(SELECT branchyear_name
				FROM cms_branchyear bry 
				WHERE  bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND frm.branchyear_id = bry.branchyear_id) AS branchyear_name,
		(SELECT semester_name
				FROM cms_semestermaster sem 
				WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND frm.semester_id = sem.semester_id) AS semester_name	  							

	FROM
		cms_feesreceivemaster frm,
		cms_studentmaster sm
				
	WHERE 
		sm.student_id = frm.student_id  ";

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

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

		return $out;
	}


	function getfeessummary1($condition)
	{

		$qry = " SELECT SUM(receive_amt) AS receive_amt
					FROM
						cms_feesreceivemaster frm WHERE 1=1";

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

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

		return $out;
	}

	function getfeessummary2($condition)
	{

		$qry = "SELECT SUM(receive_amt + total_receive_amt - amount_discount) AS receive_amt,
										um.user_id,
										um.user_name
									FROM
										cms_feesreceivemaster frm,
										cms_usermaster um	 		
									WHERE
										um.user_id = frm.login_id ";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	function getfeessummary3($condition)
	{

		$qry = "SELECT SUM(receive_amt + total_receive_amt - amount_discount) AS receive_amt
			FROM
		cms_feesreceivemaster frm WHERE 1=1";
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	function getfees360($condition)
	{

		$qry ="SELECT * FROM cms_feesreceivemaster WHERE 1=1 ";
                
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}

	function getfeeshead360($condition)
	{

		$qry ="  SELECT frm.discount, fh.head_name, frd.recdetail_amt
                                    FROM cms_feesreceivemaster frm,
                                     cms_feesreceivedetail frd ,
									 cms_feeshead fh 
                                    WHERE frm.feesreceive_id = frd.feesreceive_id and 
									frd.head_id = fh.head_id ";
                
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}


//student-list-result.php
	function getstudentfees($condition)
	{

		$qry ="SELECT * FROM cms_feesreceivemaster 
								 WHERE 1=1 ";
								 	
                
		if ($condition != "") {
			$qry .= $condition;
		}
		//echo $qry;
		$out = dbQuery($qry);

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

		return $out;
	}


	
}
?>