<?php


class CustomerMgmt
{
	public $customer_id;  		    	   	 
	public $company_id;  	 
	public $customer_orgname; 		  	  	 
	public $customer_address; 		  	  	 
	public $district_id; 	 	  	 
	public $customer_phone1; 	 	  	  	 
	public $customer_phone2; 	  	  	  	 
	public $customer_email; 	  	  	  	 
	public $customer_website; 		  	  	 
	public $customer_balance;
	public $customer_baltype; 	  	  	  	 
	public $customer_cpname1; 		  	  	 
	public $customer_cpdesignation1; 	  	  	  	 
	public $customer_cpmobile1; 	 	  	  	 
	public $customer_cpemail1; 	
	public $customer_tinno;	  	  	 
	public $customer_cstno; 	  	  	  	 
	public $customer_servicetaxno; 	  	  	  	 
	public $customer_panno; 	  	  	  	 
	public $customer_remark; 		  	  	 
	public $createdby; 	 	  	  	 
	public $createdon; 
   
	
    function CustomerMgmt()
    {
        $this->bullet = "<font size='2'>&raquo;</font>";
        $this->bullet = htmlentities($this->bullet);
        $this->inquiry_count=0;
    }

   
	
	/**
     * 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->inquiry_count=0;
		
		$qry = "SELECT lc.*,(select ld.designation_name from  
				lms_designation ld	
				where ld.designation_id = lc.customer_cpdesignation1)as designation_name
		     from
				lms_customer lc		  
			 WHERE 
				lc.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) {
            $this->inquiry_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
    
    function getAllRecordsCustReport($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->inquiry_count=0;
		
		$qry = "SELECT lc.*,dm.dist_nameeng,(select ld.designation_name from  
				lms_designation ld	
				where ld.designation_id = lc.customer_cpdesignation1)as designation_name
		     from
				lms_customer lc
				left join lms_distmaster dm	  ON lc.district_id = dm.dist_id
			 WHERE 
				lc.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) {
            $this->inquiry_count=dbRows($out);
			return $out;							
        }
		return $out;		
    }
		
	 function getCountRecords($limit=0,$start=0,$condition="",$order_field="",$order_type="")
    {
		$this->inquiry_count=0;
		
       	$qry = "SELECT COUNT(*) AS Count
				FROM lms_customer WHERE 1=1 ";
		
		if ($condition != "" ) {
            $qry.= $condition;
        }
		
        $out=dbQuery($qry);

        if (dbRows($out)>0) {
			$row=dbArray($out);
            extract($row);		
            $this->inquiry_count=$Count;            
			dbFreeResult($out);	
        }
		return $this->inquiry_count;
    }	
		
	

    /**
     * This is a function for adding new record for Employee.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function add()
    {
		global $SQLDateFormat;
		
		$Qry1 = "";
		
			$Qry1 .= "company_id='"	  .Escape($_SESSION['cCompany_id'])."',";	
			$Qry1 .= "customer_orgname='"	  .Escape($this->customer_orgname)."',"; 	
			$Qry1 .= "customer_address='" .replaceHtmlEditor(Escape($this->customer_address))."',";	 	
			$Qry1 .= "district_id='"	  .Escape($this->district_id)."',"; 	
			$Qry1 .= "customer_phone1='"	  .Escape($this->customer_phone1)."',"; 	
			$Qry1 .= "customer_phone2='"	  .Escape($this->customer_phone2)."',"; 	
			$Qry1 .= "customer_email='"	  .Escape($this->customer_email)."',"; 	
			$Qry1 .= "customer_website='"	  .Escape($this->customer_website)."',"; 	
			$Qry1 .= "customer_balance='"	  .Escape($this->customer_balance)."',"; 
			$Qry1 .= "customer_baltype='"	  .Escape($this->customer_baltype)."',"; 
			$Qry1 .= "customer_cpname1='"	  .Escape($this->customer_cpname1)."',"; 	
			$Qry1 .= "customer_cpdesignation1='"	  .Escape($this->customer_cpdesignation1)."',"; 	
			$Qry1 .= "customer_cpmobile1='"	  .Escape($this->customer_cpmobile1)."',"; 	
			$Qry1 .= "customer_cpemail1='"	  .Escape($this->customer_cpemail1)."',"; 	
			$Qry1 .= "customer_tinno='"	  .Escape($this->customer_tinno)."',"; 	
			$Qry1 .= "customer_cstno='"	  .Escape($this->customer_cstno)."',"; 	
			$Qry1 .= "customer_servicetaxno='"	  .Escape($this->customer_servicetaxno)."',"; 	
			$Qry1 .= "customer_panno='"	  .Escape($this->customer_panno)."',"; 	
			$Qry1 .= "customer_remark='" .replaceHtmlEditor(Escape($this->customer_remark))."',";	 	
			$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_customer set ". $Qry1 ." ";
		//	echo $qry;		
			dbQuery($qry);
			$this->customer_id=dbInsertedId();
		}
	}

    /**
     * This is a function for editing a Employee 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_orgname='"	  .Escape($this->customer_orgname)."',"; 	
			$Qry1 .= "customer_address='" .replaceHtmlEditor(Escape($this->customer_address))."',";	 	
			$Qry1 .= "district_id='"	  .Escape($this->district_id)."',"; 	
			$Qry1 .= "customer_phone1='"	  .Escape($this->customer_phone1)."',"; 	
			$Qry1 .= "customer_phone2='"	  .Escape($this->customer_phone2)."',"; 	
			$Qry1 .= "customer_email='"	  .Escape($this->customer_email)."',"; 	
			$Qry1 .= "customer_website='"	  .Escape($this->customer_website)."',"; 	
			$Qry1 .= "customer_balance='"	  .Escape($this->customer_balance)."',"; 	
			$Qry1 .= "customer_baltype='"	  .Escape($this->customer_baltype)."',"; 
			$Qry1 .= "customer_cpname1='"	  .Escape($this->customer_cpname1)."',"; 	
			$Qry1 .= "customer_cpdesignation1='"	  .Escape($this->customer_cpdesignation1)."',"; 	
			$Qry1 .= "customer_cpmobile1='"	  .Escape($this->customer_cpmobile1)."',"; 	
			$Qry1 .= "customer_cpemail1='"	  .Escape($this->customer_cpemail1)."',"; 	
			$Qry1 .= "customer_tinno='"	  .Escape($this->customer_tinno)."',"; 	
			$Qry1 .= "customer_cstno='"	  .Escape($this->customer_cstno)."',"; 	
			$Qry1 .= "customer_servicetaxno='"	  .Escape($this->customer_servicetaxno)."',"; 	
			$Qry1 .= "customer_panno='"	  .Escape($this->customer_panno)."',"; 	
			$Qry1 .= "customer_remark='" .replaceHtmlEditor(Escape($this->customer_remark))."',";	 	
			$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_customer set " . $Qry1 . " where customer_id=".$this->customer_id. " and company_id = '".$_SESSION['cCompany_id']."' ";
                dbQuery($qry);
            }
	}
	
	/**
     * This is a function for deleting the Employee record.
     *
     * @return void The redirect file name and the message are passed.
     *
     */
    function delete($customer_id,$support_id)
    {		

		 $qry_cc="UPDATE  csc_customer set invoice_status='Pending' WHERE customer_id in ($customer_id)";
		if(dbQuery($qry_cc))
	{
		$qry_cs = "delete from lms_customersupport 
		where customer_id in ($customer_id) and support_id in ($support_id)";
				
			if(dbQuery($qry_cs))
			{
			$qry_se="DELETE from lms_supportexpenses where support_id in ($support_id)";
		if(dbQuery($qry_se))
			{
				return 1;
			}
			else
			{
				return "support_id not found in lms_supportexpenses";
			}
			}
			else
			{
			return "support_id not found in lms_customersupport";
			}

				}
				else
				{
					return "customer_id not found in csc_customer";
				}

		
   	}
	
	
}
?>