<?php

/**
 * This is a class file used for Book Issue Master 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 Issued Master.
 *
 * @category Book Issue Master
 * @package  Book Issue Master
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */

class IssueMgmt
{
    /**
     * @var public $issue_id This is the Issue Id feteched from the database(lms_issuemaster ).
     */
    public $issue_id;
	
	/**
     * @var public $member_type This is the Member Type id.
     */
	public $member_type;
	
	/**
     * @var public $issue_registerno This is the Issue Register No.
     */
	public $issue_registerno;
	
	/**
     * @var public $member_id This is the  Member Id.
     */
	public $member_id;
	
	/**
     * @var public $school_id This is the School Id.
     */
	public $school_id;
	
	/**
     * @var public $issue_date This is the Issue Date.
     */
	public $issue_date;
	
	
	/**
     * @var public $login_id This is the issue login id.
     */
	public $login_id;
	
	/**
     * @var public $createdon This is the issue createdon.
     */
	public $createdon;
	
	//this below function is common
	
	/**
     * @var public $issue_msg This is the message.
     */
    public $issue_msg;
    /**
     * @var public $issue_count This is a counter.
     */
    public $issue_count;
    /**
     * @var public $issue_redirectfile This is file name to be redirected on after the operation.
     */
    public $issue_redirectfile;
    
    /**
     * @var public $deleteId List of Id's to be deleted.
     */
    public $deletissue_Id;
    /**
     * @var public $bullet This is bullet.
     */
    public $bullet;


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

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

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

        $out=dbQuery($qry);

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

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

			$this->issue_id				= ($issue_id);
			$this->member_type			= ($member_type);
			$this->member_id			= ($member_id);
			$this->issue_date			= ($issue_date);
			$this->school_id			= ($school_id);
			
			
			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->issue_count=0;
		
		$qry = "select * from lms_issuemaster  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->issue_count=dbRows($out);            
			dbResult($out);
        }
		return $this->issue_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,$start,$condition,$order_field="",$order_type="")
    {
		$this->issue_count=0;
	
		$qry="SELECT   issuedetail_id,
					   lim.member_type,
					   lim.member_id,
		      		   lim.issue_id, 
					   issue_returnlastdate, 
					   issue_isreturned,					  					   
					   issue_date
				FROM 
						lms_issuedetail lid, 						
						lms_issuemaster lim
				WHERE   
				        lid.school_id ='".$_SESSION["aCollegeId"]."' AND
					    lim.school_id ='".$_SESSION["aCollegeId"]."' AND
													
						lid.issue_id = lim.issue_id ";
			
					   					   				
		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->issue_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()
    {
			
			$SQLDateFormat="%d/%m/%Y";
			
            $Qry1 = "";
			
            $Qry1 .= "school_id='"	           .Escape($_SESSION['aCollegeId'])."',";			
            $Qry1 .= "member_type='"	       .Escape($this->member_type)."',";
			$Qry1 .= "issue_registerno='"      .Escape($this->issue_registerno)."',";
			$Qry1 .= "member_id='"	           .Escape($this->member_id)."',";
			$Qry1 .= "issue_date = STR_TO_DATE('".Escape($this->issue_date)."','$SQLDateFormat'),";
			$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_issuemaster set " . $Qry1 . " ";
						
                dbQuery($qry);
                $this->issue_id=dbInsertedId();
            }

            $this->issue_msg = "<strong>".$this->bullet." Book issue successfully.</strong>";
            $this->issue_redirectfile="viewmenu.php";
    }

    /**
     * This is a function for editing a Issue Masterrecord.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
		$SQLDateFormat="%d/%m/%Y";
			$Qry1 = "";

           		
            $Qry1 .= "member_type='"	         	 .Escape($this->member_type)."',";
			$Qry1 .= "member_id='"	             	 .Escape($this->member_id)."',";
			$Qry1 .= "issue_date=        		 	 STR_TO_DATE('".Escape($this->issue_date)."','$SQLDateFormat'),";
			$Qry1 .= "blnBackup = '0',";			
		
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
                $qry = "update lms_issuemaster set " . $Qry1 . " where issue_id=".$this->issue_id;
                dbQuery($qry);
            }

            $this->issue_msg= "<strong>".$this->bullet." Book Issue updated successfully.</strong>";
            $this->issue_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_issuemaster 
             	where issue_id in (".$this->deletissue_Id.") 
				and school_id='".$_SESSION["aCollegeId"]."'";
        dbQuery($qry);
   	}
	
	/**
     * This is a function for deleting the Issued Detail record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
	function getMaxRecordNumber()
    {
		$qry = "SELECT MAX(issue_registerno) as issueid 
				FROM lms_issuemaster 
				WHERE school_id='".$_SESSION["aCollegeId"]."'";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['issueid'];
		$num =$num + 1;
		return $num;
    }
	
	/**
     * This is a function for getdistincDate the Issued Detail record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
	 function getdistincDate($limit=0,$start=0,$condition="",$order_field="",$order_type="")
	{
	   $qry = "SELECT DISTINCT issue_date 
	   		   FROM lms_issuedetail lid,
					lms_issuemaster lim 
			   WHERE lid.school_id ='".$_SESSION["aCollegeId"]."' AND 
					 lim.school_id ='".$_SESSION["aCollegeId"]."' AND
					 lid.issue_id = lim.issue_id ";
	   
	   if($condition != ""){
	   		$qry .= $condition ;
	   }
	   
	  // $qry .= "ORDER BY issue_date ASC" ;
	  if ($order_field !="" && $order_type !="") {
            $qry.= " order by $order_field $order_type";
        }

	   $out=dbQuery($qry);

        if (dbRows($out)>0) {
            return $out;
		}
		return  ;
	} 
	
	/**
     * This is a function for GetDistincBookReturnDate the Issued Detail record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
	 function GetDistincBookReturnDate($limit=0,$start=0,$condition="",$order_field="",$order_type="")
	{
	   $qry = "SELECT DISTINCT issue_returndate 
	   		   FROM lms_issuedetail lid,
					lms_issuemaster lim 
			   WHERE lid.school_id ='".$_SESSION["aCollegeId"]."' AND 
					 lim.school_id ='".$_SESSION["aCollegeId"]."' AND
					 lid.issue_id = lim.issue_id ";
	   
	   if($condition != ""){
	   		$qry .= $condition ;
	   }
	   
	   //$qry .= "ORDER BY issue_returndate ASC" ;
        if ($order_field !="" && $order_type !="") {
            $qry.= " order by $order_field $order_type";
        }
	   
	   $out=dbQuery($qry);

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