<?php
/**
 * This is a class file used for Student Library Management.
 *
 * 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 Book Management.
 *
 * @category School Management System
 * @package  School Management System
 * @author   Aum Infosys <admin@auminfosys.com>
 * @license  Aum Infosys
 * @version  Release: 1.0.1
 * @link     http://auminfosys.com/
 *
 */

class BookMgmt
{
    
    public $book_id;
	
	
    public $school_id;
	
	
    public $book_registerno;
	
	
    public $book_category;
    public $branch_name;
  //  public $no_of_copy;
    public $book_shelf_no;
    public $publisher_id;
	
	
    public $book_divno;
	
    public $book_name;
	
	
    public $book_edition;
	
    public $book_language;

	public $book_price;
	
	
	public $book_author;
	

	public $book_gift;
	

	public $book_vender;
	
	public $book_purchasedate;
	

	public $book_rack;
	
	
	public $register_id;
	
	
	public $register_name;
	
	public $book_active;
	
	
	public $book_available;
	
	
	public $book_canceldate;
	

	public $book_comment;
	
	
    public $deleteBook_Id;
	
    
    public $book_count;
   
    public $book_redirectfile;
  
  	
    public $bullet;
	
	
    public $book_msg;

	public $book_billno;
	public $book_isbn;
	public $book_classno;
	
    /**
     * Constructor to inialize book_count to zero
     *
     */
    function BookMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->book_count=0;
    }

    /**
     * This function is used to get a particular record.
     *
     * @param integer $page_id This is the database banner id.
     *
     * @return array
     */
    function getRecordById($book_id)
    {
        $qry = "SELECT * FROM lms_bookmaster where 1=1 ";

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

        $out=dbQuery($qry);

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

            $row=dbArray($out);
            extract($row);
			
            $this->school_id				= ($school_id);
			$this->book_id	            	= ($book_id);
			$this->book_registerno			= ($book_registerno);
			
		//	$this->no_of_copy			    = ($no_of_copy);
			$this->branch_name			    = ($branch_name);
			$this->book_shelf_no			= ($book_shelf_no);
			$this->publisher_id			    = ($publisher_id);
			
			$this->book_category			= ($book_category);
			$this->book_divno				= ($book_divno);
			$this->book_name				= ($book_name);
			//$this->book_pageno				= ($book_pageno);
			$this->book_edition				= ($book_edition);
			$this->book_language			= ($book_language);
			$this->book_price				= ($book_price);
			
			$this->book_author				= ($book_author);
			//$this->book_publishername		= ($book_publishername);
			//$this->book_publisheradd		= ($book_publisheradd);
			$this->book_gift				= ($book_gift);
			$this->book_vender				= ($book_vender);
			$this->book_purchasedate		= ($book_purchasedate);
			$this->book_rack				= ($book_rack);
			
			$this->book_registerid			= ($book_registerid);
			$this->book_active				= ($book_active);
			$this->book_canceldate			= ($book_canceldate);
			
			$this->book_available			= ($book_available);
			$this->book_comment				= ($book_comment);
			
			$this->login_id					= ($login_id);
			$this->createdon				= ($createdon);
			
			$this->book_billno					= ($book_billno);
			$this->book_isbn				= ($book_isbn);
			$this->book_classno					= ($book_classno);

			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->book_count=0;
		
        $qry = "SELECT * FROM lms_bookmaster 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->book_count=dbRows($out);            
			dbResult($out);
        }
		return $this->book_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 getpublisher()
    {
		
		
		$qry = "SELECT * FROM cms_publishermaster
				WHERE  school_id= '".$_SESSION["aCollegeId"]."' ";
				       
		
		
     // 	print $qry;
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
           
			return $out;							
        }
		return ;		
    }
    function getAllRecords($limit=0,$start=0,$condition,$order_field,$order_type)
    {
		$this->book_count=0;
		
		$qry = "SELECT 
					lbm.*,
					lbr.register_id, 
		               register_name 
				FROM 
				       lms_bookmaster lbm,
					   lms_bookregister lbr
				WHERE  school_id= '".$_SESSION["aCollegeId"]."' AND 
				       lbr.register_id=lbm.book_registerid"; 
		
		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->book_count=dbRows($out);
			return $out;							
        }
		return ;		
    }
	 function getAllRecords_with_publisher($limit=0,$start=0,$condition,$order_field,$order_type)
    {
		$this->book_count=0;
		
		$qry = "SELECT 
					lbm.*,pub.*,
					lbr.register_id, 
		               register_name 
				FROM 
				       lms_bookmaster lbm,
					   lms_bookregister lbr,
					   cms_publishermaster pub
				WHERE  lbm.school_id= '".$_SESSION["aCollegeId"]."' AND 
				       lbr.register_id=lbm.book_registerid and
					  lbm.publisher_id = pub.publisher_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";
        }
     //	print $qry;
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->book_count=dbRows($out);
			return $out;							
        }
		return ;		
    }
	
	/**
     * This is a function for getting the max no of records with/without condition
     *
     * @param string  $condition This is the condition for fetching records.	 
     * @return Total Records Found
     */
    function getMaxRecordNumber($reg_no)
    {
		 $qry = "SELECT MAX(book_registerno) as mxcode 
				FROM lms_bookmaster 
				WHERE school_id='".$_SESSION["aCollegeId"]."'  AND
				book_registerid = '".$reg_no."' ";	
				
		$out = dbQuery($qry);
		$result = dbArray($out);
		$num =$result['mxcode'];
		if($num==0) { 
		    $num=0;
			return $num;
		
		}else {
		
			
			return $num;
		}
    }   

    /**
     * This is a function for adding new record for Book.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
	    // print "hi";
		global $SQLDateFormat;
		
            $Qry1 = "";
			//$Qry1 .= "book_id='"	        .Escape($this->book_id)."',";
			$Qry1 .= "school_id='"			.Escape($this->school_id)."',";
		    $Qry1 .= "book_registerno='"	.Escape($this->book_registerno)."',";
			
		   // $Qry1 .= "no_of_copy='"	    .Escape($this->no_of_copy)."',";
            $Qry1 .= "branch_name='"	.Escape($this->branch_name)."',";
            $Qry1 .= "book_shelf_no='"	.Escape($this->book_shelf_no)."',";	
            $Qry1 .= "publisher_id='"	.Escape($this->publisher_id)."',";
			   
            $Qry1 .= "book_category='"	    .Escape($this->book_category)."',";			
            $Qry1 .= "book_divno='"	        .Escape($this->book_divno)."',";
			
			$Qry1 .= "book_name='"	   		.Escape($this->book_name)."',";
		    //$Qry1 .= "book_pageno='"		.Escape($this->book_pageno)."',";
            $Qry1 .= "book_edition='"		.Escape($this->book_edition)."',";			
            $Qry1 .= "book_language='"		.Escape($this->book_language)."',";
			
			$Qry1 .= "book_price='"			.Escape($this->book_price)."',";
		    $Qry1 .= "book_author='"		.Escape($this->book_author)."',";
           // $Qry1 .= "book_publishername='"	.Escape($this->book_publishername)."',";			
         //   $Qry1 .= "book_publisheradd='"	.replaceHtmlEditor($this->book_publisheradd)."',";
			
			$Qry1 .= "book_registerid='"	.Escape($this->book_registerid)."',";			
		
		   
            $Qry1 .= "book_comment='"		.Escape($this->book_comment)."',";
			
			$Qry1 .= "book_gift='"			.Escape($this->book_gift)."',";
		    $Qry1 .= "book_vender='"		.Escape($this->book_vender)."',";
			$Qry1 .= "book_purchasedate=STR_TO_DATE('".Escape($this->book_purchasedate)."','$SQLDateFormat'),";
			
            $Qry1 .= "book_rack='"	  			.Escape($this->book_rack)."',";	
			$Qry1 .= "login_id='"	            .Escape($_SESSION['aUserId'])."',";			
            $Qry1 .= "createdon='"	            .Escape(date('Y-m-d h:i:s'))."',";	
			$Qry1 .= "book_active='1',";
			$Qry1 .= "book_available='1',";
			$Qry1 .= "blnBackup = '0',";			
			
			 $Qry1 .= "book_billno='"	  			.Escape($this->book_billno)."',";	
			  $Qry1 .= "book_isbn='"	  			.Escape($this->book_isbn)."',";	
			   $Qry1 .= "book_classno='"	  			.Escape($this->book_classno)."',";	
			
	
			if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
              $qry = "insert into lms_bookmaster set " . $Qry1 ." "; /* book_active='1'   */
                dbQuery($qry);
                $this->book_id =dbInsertedId();
            }
    
      
	}
	
	/**
     * This is a function for editing a Book record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit_cancelbook()
    {
	      	global $SQLDateFormat;
            $Qry1 = "";
			$Qry1 .= "book_active='0',";
			$Qry1 .= "book_canceldate=STR_TO_DATE('".Escape($this->book_canceldate)."','$SQLDateFormat'),";
			$Qry1 .= "book_comment='".Escape($this->book_comment)."',";
			$Qry1 .= "blnBackup = '0',";
			if($Qry1!=""){
				$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
               	$qry = "update lms_bookmaster set " . $Qry1 . " 
				 		where 
							school_id='".$_SESSION["aCollegeId"]."' 
							and book_registerno=".$this->book_registerno."
							and book_registerid='".$this->book_registerid."'";
                dbQuery($qry);
            }
    }
	
	 
	/**
     * This is a function for editing a Book record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit_availabelbook()
    {
	      	
            $Qry1 = "";
			
			$Qry1 .= "book_available='".Escape($this->book_available)."',";
			$Qry1 .= "blnBackup = '0',";
			if($Qry1!=""){
				$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
               	$qry = "update lms_bookmaster set " . $Qry1 . " 
				 		where 
							school_id='".$_SESSION["aCollegeId"]."' 
							and book_id in (".$this->book_id.")";
                dbQuery($qry);
            }
    }
	
    
    /**
     * This is a function for editing a Book record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
	      global $SQLDateFormat;
            $Qry1 = "";
			
            $Qry1 .= "book_category='"	    .Escape($this->book_category)."',";			
            $Qry1 .= "book_divno='"	        .Escape($this->book_divno)."',";
          
		   // $Qry1 .= "no_of_copy='"	    .Escape($this->no_of_copy)."',";
            $Qry1 .= "branch_name='"	.Escape($this->branch_name)."',";
            $Qry1 .= "book_shelf_no='"	.Escape($this->book_shelf_no)."',";	
            $Qry1 .= "publisher_id='"	.Escape($this->publisher_id)."',";
			
			$Qry1 .= "book_name='"	   		.Escape($this->book_name)."',";
		   // $Qry1 .= "book_pageno='"		.Escape($this->book_pageno)."',";
            $Qry1 .= "book_edition='"		.Escape($this->book_edition)."',";			
            $Qry1 .= "book_language='"		.Escape($this->book_language)."',";
			
			$Qry1 .= "book_price='"			.Escape($this->book_price)."',";
		    $Qry1 .= "book_author='"		.Escape($this->book_author)."',";
         //   $Qry1 .= "book_publishername='"	.Escape($this->book_publishername)."',";			
         //   $Qry1 .= "book_publisheradd='"	.replaceHtmlEditor($this->book_publisheradd)."',";
			
					
		    $Qry1 .= "book_comment='"		.Escape($this->book_comment)."',";
			
			$Qry1 .= "book_gift='"			.Escape($this->book_gift)."',";
		    $Qry1 .= "book_vender='"		.Escape($this->book_vender)."',";
			$Qry1 .= "book_purchasedate=STR_TO_DATE('".Escape($this->book_purchasedate)."','$SQLDateFormat'),";
			
            $Qry1 .= "book_rack='"			.Escape($this->book_rack)."',";	
			$Qry1 .= "blnBackup = '0',";
			
			
			$Qry1 .= "book_billno='"	  			.Escape($this->book_billno)."',";	
			  $Qry1 .= "book_isbn='"	  			.Escape($this->book_isbn)."',";	
			   $Qry1 .= "book_classno='"	  			.Escape($this->book_classno)."',";	
			   
            if ($Qry1 != "") {
                 $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);               
				 
				 $qry = "update lms_bookmaster set " . $Qry1 . 
				  " where school_id='".$_SESSION["aCollegeId"]."' and book_id=".$this->book_id;
                 
				 dbQuery($qry);
            }
    }
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctLanguage()
    {
        $qry = "SELECT DISTINCT book_language FROM lms_bookmaster where book_active ='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctCategory()
    {
        $qry = "SELECT DISTINCT book_category FROM lms_bookmaster where book_active ='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctName()
    {
        $qry = "SELECT DISTINCT book_name FROM lms_bookmaster where book_active ='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctEdition()
    {
        $qry = "SELECT DISTINCT book_edition FROM lms_bookmaster where book_active ='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctAuthor()
    {
        $qry = "SELECT DISTINCT book_author FROM lms_bookmaster where book_active ='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctpublisherName()
    {
      
		$qry = "SELECT DISTINCT publisher_name 	 FROM cms_publishermaster where school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getDistinctVender()
    {
        $qry = "SELECT DISTINCT book_vender FROM lms_bookmaster where book_vender!='' AND book_vender IS NOT NULL AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	function getDistinctClassno()
    {
        $qry = "SELECT DISTINCT book_classno FROM lms_bookmaster where book_classno!='' AND book_classno IS NOT NULL AND school_id = '".$_SESSION['aCollegeId']."'";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to get Book register.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getBookRegister()
    {
		$qry="select * from lms_bookregister";
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
	
	/**
     * This function is used to Delete the Book Entry.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
	 
	function delete()
    {
        $qry = "delete from  lms_bookmaster 
				where book_id in (".$this->deleteBook_Id.") 
				and school_id='".$_SESSION["aCollegeId"]."'";
		dbQuery($qry);
     
    }
	/**
     * This function is used to remove duplication of cast.
     *
     * @param integer $school_id This is the database school id.
     *
     * @return array
     */
    function getBookNameByRegiste($regId)
    {
      	 $qry = "SELECT CONCAT(book_registerno,'. ',book_name) AS book_name,book_registerno 
		 		 FROM lms_bookmaster 
				 WHERE book_available='1' AND book_active='1' AND school_id = '".$_SESSION['aCollegeId']."'";
		if($regId!=""){
			$qry .= " AND book_registerid='$regId' ";
		}
		$out=dbQuery($qry);
		if (dbRows($out)>0) {
			return $out;							
        }
		return ;
	}
}
?>