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

class CustomerInvoiceMgmt
{	
	 public $customerinvoice_id;    	
 	 public $customer_id; 	
 	 public $company_id; 	
	 public $customerinvoice_no; 	
 	 public $customerinvoice_date; 	
 	 public $remark; 
	 public $tamount;
	 public $dueamount;
	 public $year_id;
	 public $status;
	 public $createdby; 	
 	 public $createdon;

    function CustomerInvoiceMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->customerinvoice_count=0;
    }

    function getCountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->customerinvoice_count=0;
		
       	$qry = "SELECT COUNT(*) AS Count
				FROM lms_customerinvoice WHERE 1=1 ";
		
		if ($condition != "" ) {
            $qry.= $condition;
        }
		
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
			$row=dbArray($out);
            extract($row);		
            $this->customerinvoice_count=$Count;            
			dbFreeResult($out);	
        }
		return $this->customerinvoice_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->product_count=0;
		
				
		$qry = "SELECT li.*,lv.customer_orgname,customer_address
				FROM 
			  		 lms_customerinvoice li LEFT JOIN lms_customer lv
					ON li.customer_id=lv.customer_id			  
			  	WHERE    							
					lv.company_id   = '".$_SESSION["cCompany_id"]."' and year_id='".$_SESSION["cYear"]."'";
		
		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->product_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
		
		
	function getInvoiceNosofcustomer($condition="")
    {	
		$qry = "SELECT * FROM lms_customerinvoice WHERE company_id   = '".$_SESSION["cCompany_id"]."' and year_id= '".$_SESSION["cYear"]."'";
		
		if ($condition != "" ) {
            $qry.= $condition;
        }
	//	echo $qry;exit;
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->product_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
	
  
	 
	 function getInvoiceNo()
    	{	
			$qry = "";
			 $qry = " SELECT MAX(customerinvoice_no) + 1  as invoice_no  FROM lms_customerinvoice where company_id= '".$_SESSION["cCompany_id"]."' ";
					
			
			$out=dbQuery($qry);
			
			if (dbRows($out)>0){
            	$resut=dbArray($out);
				if($resut["invoice_no"]=="")
				{
					return "1";
				}
				else{		 
					return $resut["invoice_no"];							
				}
        	}
			return $out;
		}


    function add()
    {
		global $SQLDateFormat;
		
		$Qry1 = "";
		$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";	
		$Qry1 .= "customer_id='"	  .Escape($this->customer_id)."',";
		$Qry1 .= "customerinvoice_no='"	      .Escape($this->customerinvoice_no)."',";	
		$Qry1 .= "customerinvoice_date=    STR_TO_DATE('".Escape($this->customerinvoice_date)."','$SQLDateFormat'),";	
		$Qry1 .= "remark='" .Escape($this->remark)."',";
		$Qry1 .= "tamount='" .Escape($this->tamount)."',";
		$Qry1 .= "dueamount='" .Escape($this->dueamount)."',";
		$Qry1 .= "year_id='"	      .Escape($_SESSION['cYear'])."',";
		$Qry1 .= "status='" .Escape($this->status)."',";
		$Qry1 .= "createdby='"	      .Escape($_SESSION['cUserId'])."',";
		$Qry1 .= "createdon='"	      .Escape(date('Y-m-d h:i:s'))."',";
		$Qry1 .= "ip_address='"		 .Escape($_SERVER['REMOTE_ADDR'])."',";	
		
	if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			$qry = "insert into lms_customerinvoice set " . $Qry1 . " ";
					
			dbQuery($qry);
			$this->customerinvoice_id=dbInsertedId();
		}
	}

    /**
     * This is a function for editing a Product record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function edit()
    {
			global $SQLDateFormat;
			
			$Qry1 = "";
			$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";	
			$Qry1 .= "customer_id='"	  .Escape($this->customer_id)."',";
			$Qry1 .= "customerinvoice_no='"	      .Escape($this->customerinvoice_no)."',";	
	     	$Qry1 .= "customerinvoice_date=    STR_TO_DATE('".Escape($this->customerinvoice_date)."','$SQLDateFormat'),";	
			$Qry1 .= "remark='" .Escape($this->remark)."',";
			$Qry1 .= "tamount='" .Escape($this->tamount)."',";
			$Qry1 .= "dueamount='" .Escape($this->dueamount)."',";
			$Qry1 .= "year_id='"	      .Escape($_SESSION['cYear'])."',";
			$Qry1 .= "status='" .Escape($this->status)."',";
			$Qry1 .= "createdby='"	      .Escape($_SESSION['cUserId'])."',";
			$Qry1 .= "createdon='"	      .Escape(date('Y-m-d h:i:s'))."',";
			$Qry1 .= "ip_address='"		 .Escape($_SERVER['REMOTE_ADDR'])."',";	
				
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
           		$qry = "update lms_customerinvoice set " . $Qry1 . " where customerinvoice_id=".$this->customerinvoice_id;
                dbQuery($qry);
            }
	}
	
	 function updatestatus($customerinvoice_id,$con)
    {		
			 $qry = "update lms_customerinvoice 
             	set ".$con." where customerinvoice_id='".$customerinvoice_id."'";
        dbQuery($qry);
   	}
	
	 function delete()
    {		
			 $qry = "delete from  lms_customerinvoice 
             	where customerinvoice_id in (".$this->deleteId.") ";
        dbQuery($qry);
   	}
	
	function cancelinvoice()
    {		
			 $qry = "update lms_customerinvoice set status='Cancelled' where customerinvoice_id in (".$this->deleteId.") ";
        dbQuery($qry);
   	}
	
}
?>