<?php

class QuotationMgmt
{
	//start variables

	// lms_quotationmaster
	public $quotation_id;
	public $inquiry_id;
	public $company_id;
	public $quotation_toname;
	public $quotation_toaddress;
	public $quotation_referenceby;
	public $quotation_subject;
	public $quotation_header;
	public $quotation_footer;
	public $quotation_total;
	public $quotation_date;
	
	// lms_quotationdetail
	public $quotationdetail_id;
	public $product_id;
	public $product_qty;
	public $product_rate;
	public $product_vat1;
	public $product_vat2;
	public $product_servicetax;
    
	// lms_quotationtemplate
	public $quotationtemplate_id;
	
	//end variables
	
	//start functions
	
    function quotationtemplate_getRecordById(){
        $qry = "SELECT * FROM lms_quotationtemplate WHERE company_id='".$_SESSION['cCompany_id']."' ";
        $out=dbQuery($qry);
        if(dbRows($out)>0){
  			$row=dbArray($out);
            extract($row);
			$this->quotationtemplate_id	= ($quotationtemplate_id);
			$this->quotation_header		= ($quotation_header);
			$this->quotation_footer		= ($quotation_footer);
			$this->company_id			= ($company_id);
			dbFreeResult($out);
        }
    }
	
	function quotationtemplate_add(){
  		$Qry1 = "";
		$Qry1 .= "quotation_header='"	.addslashes(Escape($this->quotation_header))."',";
        $Qry1 .= "quotation_footer='"	.addslashes(Escape($this->quotation_footer))."',";
        $Qry1 .= "company_id='"			.addslashes(Escape($_SESSION['cCompany_id']))."',";
		if ($Qry1 != ""){
    		$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "INSERT INTO lms_quotationtemplate SET " . $Qry1 . " ";
            dbQuery($qry);
       	}            
    }

	function quotationtemplate_edit(){
  		$Qry1 = "";
		$Qry1 .= "quotation_header='"	.addslashes(Escape($this->quotation_header))."',";
        $Qry1 .= "quotation_footer='"	.addslashes(Escape($this->quotation_footer))."',";
		if ($Qry1 != ""){
    		$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "UPDATE lms_quotationtemplate SET ".$Qry1." WHERE company_id='".$_SESSION['cCompany_id']."' ";
            dbQuery($qry);
       	}            
    }

	function inquiry_getAllRecords($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT inquiry_id, inquierorg_name, inquier_address, contactperson, appointmentdate, appointmenttime, product_ids 
				FROM lms_inquiry WHERE 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) {
            return $out;							
        }
		return $out;		
	}
	
	function product_getAllRecords_ADD($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT * FROM lms_productmaster pm 
		 		WHERE 
					company_id = '".$_SESSION["cCompany_id"]."' AND
					product_active = '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";
        }
		
	//	print $qry; exit;
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            return $out;							
        }
		return $out;		
	}	
	
	function product_getAllRecords_EDIT($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT 
					pm.product_id,
					pm.product_name,
					pm.product_measurement,
					qd.product_rate as product_price,
					qd.product_vat1 as product_vat1,
					qd.product_vat2 as product_vat2,
					qd.product_servicetax as product_servicetax,
					qd.product_qty
					
					 	 		
				FROM 
					lms_productmaster pm,
					lms_quotationmaster qm,
					lms_quotationdetail qd
				WHERE 
					pm.product_id = qd.product_id AND
					qm.quotation_id = qd.quotation_id AND
					pm.company_id = '".$_SESSION["cCompany_id"]."' AND 
					qm.company_id = '".$_SESSION["cCompany_id"]."' AND
					qd.company_id = '".$_SESSION["cCompany_id"]."' AND
					product_active = '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";
        }
		
		//print $qry;
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            return $out;							
        }
		return $out;		
	}
	
	function quotation_getAllRecords($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT lms_quotationmaster.*,inquierorg_name FROM lms_quotationmaster,lms_inquiry 
				WHERE 
					lms_quotationmaster.inquiry_id = lms_inquiry.inquiry_id AND
					lms_quotationmaster.company_id = '".$_SESSION["cCompany_id"]."' AND
					lms_inquiry.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";
        }
		
		//print $qry;
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            return $out;							
        }
		return $out;		
	}
	
	function quotation_getProductIds($limit=0,$start=0,$condition="",$order_field="",$order_type=""){
		$qry = "SELECT GROUP_CONCAT(product_id) AS product_ids FROM lms_quotationdetail WHERE 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";
        }
		
		//print $qry;
		$out=dbQuery($qry);
		
        if (dbRows($out)>0) {
            return $out;							
        }
		return $out;		
	}
	
	function add_quotationmaster(){
		
		global $SQLDateFormat;
	
		$Qry1 = "";
       	$Qry1 .= "inquiry_id='"				.Escape($this->inquiry_id)."',";			
        $Qry1 .= "company_id='"	  			.Escape($this->company_id)."',";
		$Qry1 .= "quotation_toname='"	  	.Escape($this->quotation_toname)."',";
		$Qry1 .= "quotation_toaddress='"	.replaceHtmlEditor(Escape($this->quotation_toaddress))."',";
		$Qry1 .= "quotation_referenceby='"	.Escape($this->quotation_referenceby)."',";
		$Qry1 .= "quotation_subject='"	    .Escape($this->quotation_subject)."',";
		$Qry1 .= "quotation_header='"		.addslashes(Escape($this->quotation_header))."',";
        $Qry1 .= "quotation_footer='"		.addslashes(Escape($this->quotation_footer))."',";
    //	$Qry1 .= "quotation_total='"	    .Escape($this->quotation_total)."',";
    	$Qry1 .= "quotation_tax='"	    .Escape($this->quotation_tax)."',";
    		$Qry1 .= "quotation_cost='"	    .Escape($this->quotation_cost)."',";
    	$Qry1 .= "quotation_quantity='"	    .Escape($this->quotation_quantity)."',";
    		$Qry1 .= "quotation_total='"	    .Escape($this->quotation_total)."',";
    		$Qry1 .= "product_name='"	    .Escape($this->product_name)."',";
    	
    	
		$Qry1 .= "quotation_date = STR_TO_DATE('".Escape($this->quotation_date)."','$SQLDateFormat'),";
		
					
		if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "INSERT INTO lms_quotationmaster SET " . $Qry1 . " ";
            dbQuery($qry);
            $this->quotation_id=dbInsertedId();
		}	
	}
	
 	function edit_quotationmaster(){
			
		global $SQLDateFormat;
			
		$Qry1 = "";
		$Qry1 .= "inquiry_id='"				.Escape($this->inquiry_id)."',";			
        $Qry1 .= "company_id='"	  			.Escape($this->company_id)."',";
		$Qry1 .= "quotation_toname='"	  	.Escape($this->quotation_toname)."',";
		$Qry1 .= "quotation_toaddress='"	.replaceHtmlEditor(Escape($this->quotation_toaddress))."',";
		$Qry1 .= "quotation_referenceby='"	.Escape($this->quotation_referenceby)."',";
		$Qry1 .= "quotation_subject='"	    .Escape($this->quotation_subject)."',";
		$Qry1 .= "quotation_header='"		.addslashes(Escape($this->quotation_header))."',";
        $Qry1 .= "quotation_footer='"		.addslashes(Escape($this->quotation_footer))."',";
    	$Qry1 .= "quotation_total='"	    .Escape($this->quotation_total)."',";
		$Qry1 .= "quotation_date = STR_TO_DATE('".Escape($this->quotation_date)."','$SQLDateFormat'),";
					
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "UPDATE lms_quotationmaster SET " . $Qry1 . " WHERE quotation_id=".$this->quotation_id. " AND company_id = '".$_SESSION['cCompany_id']."' ";
            dbQuery($qry);
      	}
	}

	function add_quotationdetail(){
		
		$Qry1 = "";
       	$Qry1 .= "quotation_id='"		.Escape($this->quotation_id)."',";			
        $Qry1 .= "product_id='"	  		.Escape($this->product_id)."',";
		$Qry1 .= "company_id='"			.Escape($this->company_id)."',";
		$Qry1 .= "product_qty='"	    .Escape($this->product_qty)."',";
        $Qry1 .= "product_rate='"	    .Escape($this->product_rate)."',";			
        $Qry1 .= "product_vat1='"	  	.Escape($this->product_vat1)."',";
    	$Qry1 .= "product_vat2='"	    .Escape($this->product_vat2)."',";
		$Qry1 .= "product_servicetax='" .Escape($this->product_servicetax)."',";
					
		if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "INSERT INTO lms_quotationdetail SET " . $Qry1 . " ";
            dbQuery($qry);
            $this->quotationdetail_id = dbInsertedId();
		}	
	}
	
 	function edit_quotationdetail(){
			
		global $SQLDateFormat;
			
		$Qry1 = "";
       	$Qry1 .= "quotation_id='"		.Escape($this->quotation_id)."',";			
        $Qry1 .= "product_id='"	  		.Escape($this->product_id)."',";
		$Qry1 .= "company_id='"			.Escape($this->company_id)."',";
		$Qry1 .= "product_qty='"	    .Escape($this->product_qty)."',";
        $Qry1 .= "product_rate='"	    .Escape($this->product_rate)."',";			
        $Qry1 .= "product_vat1='"	  	.Escape($this->product_vat1)."',";
    	$Qry1 .= "product_vat2='"	    .Escape($this->product_vat2)."',";
		$Qry1 .= "product_servicetax='" .Escape($this->product_servicetax)."',";
					
        if ($Qry1 != "") {
        	$Qry1 = substr($Qry1, 0, strlen($Qry1)-1);
            $qry = "UPDATE lms_quotationdetail SET " . $Qry1 . " WHERE quotation_id=".$this->quotation_id. " AND company_id = '".$_SESSION['cCompany_id']."' ";
            dbQuery($qry);
      	}
	}
	
	function getDistinctReferenceBy(){
        $this->student_count=0;
		
        $qry = "SELECT DISTINCT quotation_referenceby 
				FROM lms_quotationmaster 
				WHERE company_id = '".$_SESSION["cCompany_id"]."'  ";
		
        $qry.= " order by quotation_referenceby ASC ";

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->student_count=dbRows($out);
			return $out;							
        }
		return $out;
	}
	
	function getDistinctSubject(){
        $this->student_count=0;
		
        $qry = "SELECT DISTINCT quotation_subject 
				FROM lms_quotationmaster 
				WHERE company_id = '".$_SESSION["cCompany_id"]."'  ";
		
        $qry.= " order by quotation_subject ASC ";

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->student_count=dbRows($out);
			return $out;							
        }
		return $out;
	}
	
	function getDistinctproduct(){
        $this->student_count=0;
		
        $qry = "SELECT DISTINCT  product_name  
				FROM lms_productmaster 
				WHERE product_active = '1'  ";
		
        $qry.= " order by  product_name ASC ";

        $out=dbQuery($qry);

        if (dbRows($out)>0) {
            $this->student_count=dbRows($out);
			return $out;							
        }
		return $out;
	}
	
	//end functions
}
?>