<?php

/**
 * This is a class file used for Book Issue Detail operations.
 *
 * PHP version 5
 *
 * @category Admin_Tool
 * @package  Admin_Tool
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  CVS:1.0.1
 * @link     http://auminfosys.com/
 * @filesource
 */

/**
 * This is a class named IssuedDetail.
 *
 * @category Book Issue Detail
 * @package  Book Issue Detail
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */

class IssueDetailMgmt
{
    /**
     * @var public $issuedetail_id This is the IssueDetail Id feteched from the database(lms_issuedetail).
     */
    public $issuedetail_id;
	
	/**
     * @var public $issue_id This is the Issue id.
     */
	public $issue_id;
	
	/**
     * @var public $book_id This is the  Book Id.
     */
	public $book_id;
	
	/**
     * @var public $issue_returndate This is the Issue ReturnDate.
     */
	public $issue_returndate;
	
	/**
     * @var public $issue_isreturned This is the Issue Isreturned.
     */
	public $issue_isreturned;
	
	/**
     * @var public $issue_returnlastdate This is the Issue returnlastdate. 
     */
	public $issue_returnlastdate;
	
	public $issue_returnfine;

	public $issue_late_reason;
	
	/**
     * @var public $book_registerid This is the  book registerid.
     */
	public $book_registerid;
	
	/**
     * @var public $school_id This is the School Id.
     */
	public $school_id;
	
	/**
     * @var public $login_id This is the issuedetail login id.
     */
	public $login_id;
	
	/**
     * @var public $createdon This is the issuedetail createdon.
     */
	public $createdon;
	
	//this below function is common
	
	/**
     * @var public $issuedetail_msg This is the message.
     */
    public $issuedetail_msg;
    /**
     * @var public $issuedetail_count This is a counter.
     */
    public $issuedetail_count;
    /**
     * @var public $issuedetail_redirectfile This is file name to be redirected on after the operation.
     */
    public $issuedetail_redirectfile;
    
    /**
     * @var public $deleteId List of Id's to be deleted.
     */
    public $deletissuedetail_Id;
    /**
     * @var public $bullet This is bullet.
     */
    public $bullet;


    /**
     * Constructor to inialize issuedetail_count to zero
     *
     */
    function IssueDetailMgmt()
    {
		$this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->issuedetail_count=0;
    }

    /**
     * This function is used to get a particular record.
     *
     * @param integer $issuedetail_id This is the database issuedetail_id.
     *
     * @return array<!-- -->
     */
    function getRecordById($issuedetail_id,$school_id)
    {
        $qry = "select * from lms_issuedetail  where 1=1 ";

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

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->issuedetail_count=1;

            $row=dbArray($out);
            extract($row);

			$this->issuedetail_id				= ($issuedetail_id);
			$this->issue_id						= ($issue_id);
			$this->book_id						= ($book_id);
			$this->issue_returndate				= ($issue_returndate);
			$this->issue_isreturned				= ($issue_isreturned);
			$this->issue_returnlastdate			= ($issue_returnlastdate);
			$this->school_id					= ($school_id);
			$this->book_registerid				= ($book_registerid);
			
			
			dbResult($out);
        }
    }
		
	/**
     * This is a function for getting the total no of records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Total Records Found
     */
    function getCountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->issuedetail_count=0;
		
		$qry = "select * from lms_issuedetail  where school_id = '".$_SESSION['aCollegeId']."' ";
		
		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";
        }
		
		//print $qry;
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->issuedetail_count=dbRows($out);            
			dbResult($out);
        }
		return $this->issuedetail_count;
    }
	
	/**
     * This is a function for getting the all records with/without condition
     *
     * @param integer $limit This is the limit of number or records.
     * @param integer $start This is the starting location of records.
     * @param string  $condition This is the condition for fetching records.	 
     * @param string  $order_field This is the field for fetching records orderby.	 
     * @param string  $order_type This is the order direction to fetch records orderby.	 	 
     *
     * @return Mix Result Set
     */
    function getAllRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->issuedetail_count=0;
		
		$qry = "select * from lms_issuedetail  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";
        }
		
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            $this->issuedetail_count=dbRows($out);
			return $out;							
        }
		return ;		
    }
		

    /**
     * This is a function for adding new record for Issue Detail.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
			global $SQLDateFormat;
			
            $Qry1 = "";

            $Qry1 .= "school_id='"	                 .Escape($_SESSION['aCollegeId'])."',";			
            $Qry1 .= "issue_id='"	         		 .Escape($this->issue_id)."',";
			$Qry1 .= "book_id='"	             	 .Escape($this->book_id)."',";
			$Qry1 .= "issue_returnlastdate = STR_TO_DATE('".Escape($this->issue_returnlastdate)."','%d/%m/%Y'), ";
			$Qry1 .= "book_registerid='"	         .Escape($this->book_registerid)."',";
			$Qry1 .= "login_id='"	                 .Escape($_SESSION['aUserId'])."',";			
            $Qry1 .= "createdon='"	                 .Escape(date('Y-m-d h:i:s'))."',";		
			$Qry1 .= "blnBackup = '0',";	
						
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
              $qry = "insert into lms_issuedetail set " . $Qry1 . " ";
				dbQuery($qry);
                $this->issuedetail_id=dbInsertedId();
            }

            $this->issuedetail_msg = "<strong>".$this->bullet." Book return successfully.</strong>";
            $this->issuedetail_redirectfile="viewmenu.php";
    }

    /**
     * This is a function for editing a Issuedetail record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
			global $SQLDateFormat;
			$Qry1 = "";
		    $Qry1 .= "issue_returndate  = STR_TO_DATE('".Escape($this->issue_returndate)."','%d/%m/%Y'), ";
		   	$Qry1 .= "issue_returnfine='"	         .Escape($this->issue_returnfine)."',";
			$Qry1 .= "issue_isreturned='"	         .Escape($this->issue_isreturned)."',";
			$Qry1 .= "blnBackup = '0',";
			$Qry1 .= "issue_late_reason ='"	         .Escape($this->issue_late_reason)."',";
					
		
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			  	$qry= "update lms_issuedetail set " . $Qry1 . " where 
					  issuedetail_id in(".$this->issue_id.") and 
				      school_id   = '".$_SESSION['aCollegeId']."' " ;
               dbQuery($qry);
            }

            $this->issuedetail_msg= "<strong>".$this->bullet." Book return updated successfully.</strong>";
            $this->issuedetail_redirectfile="viewmenu.php";
			return "";
    }
	
	
	 function editlostbook()
    {
			global $SQLDateFormat;
			$Qry1 = "";
		   $Qry1 .= "issue_returndate  = STR_TO_DATE('".Escape($this->issue_returndate)."','%d/%m/%Y'), ";
		   	$Qry1 .= "issue_returnfine='"	         .Escape($this->issue_returnfine)."',";
			$Qry1 .= "issue_isreturned='"	         	.Escape($this->issue_isreturned)."',";
			$Qry1 .= "issue_islost='"	         	.Escape($this->issue_islost)."',";
			$Qry1 .= "blnBackup = '0',";
					
		
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
				
		  	$qry= "update lms_issuedetail set " . $Qry1 . " where 
					  issuedetail_id in(".$this->issue_id.") and 
				      school_id   = '".$_SESSION['aCollegeId']."' " ;
					  
                
                dbQuery($qry);
            }

            $this->issuedetail_msg= "<strong>".$this->bullet." Book return updated successfully.</strong>";
            $this->issuedetail_redirectfile="viewmenu.php";
    }
	
	/**
     * This is a function for deleting the Issued Detail record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function delete()
    {		
		$qry = "delete from  lms_issuedetail 
             	where issuedetail_id in (".$this->deletissuedetail_Id.") 
				and school_id='".$_SESSION["aCollegeId"]."'";
        dbQuery($qry);
   	}
	
	function isReturn($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->issuedetail_count=0;
		
		$qry = "SELECT 
					bm.book_id,
					im.issue_id,
					id.issuedetail_id,
					bm.book_registerno,
					im.issue_registerno,
					im.member_type,
					CASE WHEN(im.member_type='1') 
						 THEN ('Student')
							
						WHEN(im.member_type='2') 
						THEN ('Emplopyee')
							
						WHEN(im.member_type='3') 
						THEN ('Other')
					END AS member_name,
					im.issue_date,
					id.issue_returnlastdate,
					br.register_name,
					book_name,
					issue_returndate 
				FROM 
					lms_bookmaster bm, 
					lms_bookregister br,
					lms_issuedetail id,
					lms_issuemaster im 
				WHERE 
					bm.book_id = id.book_id AND
					id.issue_id = im.issue_id AND
					bm.book_registerid = br.register_id AND
					bm.school_id ='".$_SESSION["aCollegeId"]."' AND 
					im.school_id ='".$_SESSION["aCollegeId"]."' AND 
					id.school_id ='".$_SESSION["aCollegeId"]."' ";
					
		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";
        }
		
		//print $qry;
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            $this->issuedetail_count=dbRows($out);
			return $out;							
        }
		return ;		
    }

}
?>