<?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 InvoiceMgmt
{	
	 public $invoice_id;    	
 	 public $vendor_id; 	
 	 public $company_id; 	
	 public $invoice_no; 	
 	 public $invoice_date; 	
 	 public $invoice_paymenttype; 
	 public $createdby; 	
 	 public $createdon;

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

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

        if (dbRows($out)>0) {
			$row=dbArray($out);
            extract($row);		
            $this->invoice_count=$Count;            
			dbFreeResult($out);	
        }
		return $this->invoice_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.vendor_orgname
				FROM 
			  		lms_invoice li LEFT JOIN lms_vendor lv
					ON li.vendor_id=lv.vendor_id			  
			  	WHERE    							
					lv.company_id   = '".$_SESSION["cCompany_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->product_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
	
	
	 function getCurrentStockRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
     {

		$this->product_count=0;
	

		$qry = "SELECT
					pm.*,
					(
						SELECT SUM(ip.product_qty) 
						FROM lms_invoiceproduct ip 
						WHERE pm.product_id = ip.product_id
					) AS puchase,
					(
						SELECT SUM(cip.product_qty) 
						FROM lms_customerinvoiceproduct cip 
						WHERE pm.product_id = cip.product_id
					) AS sale
			   FROM
					lms_productmaster pm
			  where
					pm.company_id = '".$_SESSION["cCompany_id"]."'
					GROUP BY pm.product_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->product_count=dbRows($out);
					return $out;							
				}
				return $out;		
			}
		
    /**
     * This is a function for adding new record for Product.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
		global $SQLDateFormat;
		
		$Qry1 = "";
		$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";	
		$Qry1 .= "vendor_id='"	  .Escape($this->vendor_id)."',";
		$Qry1 .= "invoice_no='"	      .Escape($this->invoice_no)."',";	
		$Qry1 .= "invoice_date=    STR_TO_DATE('".Escape($this->invoice_date)."','$SQLDateFormat'),";	
		$Qry1 .= "invoice_paymenttype='" .Escape($this->invoice_paymenttype)."',";
		$Qry1 .= "createdby='"	      .Escape($_SESSION['cUserId'])."',";
		$Qry1 .= "createdon='"	      .Escape(date('Y-m-d h:i:s'))."',";
		
	if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
			$qry = "insert into  lms_invoice set " . $Qry1 . " ";
					
			dbQuery($qry);
			$this->invoice_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 .= "vendor_id='"	  .Escape($this->vendor_id)."',";
			$Qry1 .= "invoice_no='"	      .Escape($this->invoice_no)."',";	
	     	$Qry1 .= "invoice_date=    STR_TO_DATE('".Escape($this->invoice_date)."','$SQLDateFormat'),";	
			$Qry1 .= "invoice_paymenttype='" .Escape($this->invoice_paymenttype)."',";
			$Qry1 .= "createdby='"	      .Escape($_SESSION['cUserId'])."',";
			$Qry1 .= "createdon='"	      .Escape(date('Y-m-d h:i:s'))."',";
				
            if ($Qry1 != "") {
                $Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
           		$qry = "update  lms_invoice set " . $Qry1 . " where invoice_id=".$this->invoice_id;
                dbQuery($qry);
            }
	}
	 function delete()
    {		
			 $qry = "delete from   lms_invoice 
             	where invoice_id in (".$this->deleteId.") ";
        dbQuery($qry);
   	}
	
}
?>