<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php

    require_once 'db.php';

    /*
	We want to return one or more rows of data that match what would
	have been returned from company_crm_v.
    */

    class Company_CRM {
	function Company_CRM() {
	  global $dbi;
	  $this->db = &$dbi;

	  $this->error = "";
	  $this->order = array(
	    "company_id",
	    "crm_id",
	    "crm_name",
	    "crm_short_name",
	    "updated_date",
	    "created_date",
	    "updated_by",
	    "created_by",
	    "default_problem_id",
	    "default_source_id",
	    "email_source_id",
	    "default_support_person_id",
	    "default_escalated_to_id",
	    "email_domain",
	    "users_url",
	    "email_sig",
	    "backup_email",
	    "extended_email_sig",
	    "new_ticket_notify",
	    "web_page_id",
	    "confirmation_url",
	    "web_domain",
	    "web_path",
	);
      }

	##
	## This does all the work, really.
	##
	## Returns an array of associative arrays, ordered as described
	## in $this->order
	##
	function GetData($crm_id = '') {

	    $sql = "
		select company_id,
		       crm_id,
		       crm_name,
		       crm_short_name,
		       updated_date,
		       created_date,
		       updated_by,
		       created_by,
		       email_domain,
		       users_url,
		       email_sig,
		       backup_email,
		       extended_email_sig,
		       new_ticket_notify,
		       web_page_id,
		       confirmation_url,
		       web_domain,
		       web_path,
		       default_problem_id,
		       default_source_id,
		       email_source_id,
		       default_support_person_id,
		       default_escalated_to_id
		  from company_crm cc
		";

	    $binds = array();
	    if ($crm_id != '') {
		$sql .= "where crm_id = ?";
		array_push($binds,$crm_id);
	    }
	    $rows = $this->db->getAll($sql,$binds,DB_FETCHMODE_ASSOC);

	    if (DB::isError($rows)) {
		$this->error = "Company_CRM::GetData(): ".$rows->getMessage();
		return;
	    }

	    $ret = array();

	    ## that was the easy part.
	    foreach ($rows as $row) {

		$tmp = array();
		foreach ($row as $k => $v) {
		    $tmp[$k] = $v;
		}

		$crm_id = $row['crm_id'];
		$tmp['default_problem_id'] = $this->GetDefaultProblemId($crm_id, $row['default_problem_id']);
		$tmp['default_source_id'] = $this->GetDefaultSourceId($crm_id,$row['default_source_id']);
		$tmp['email_source_id'] = $this->GetEmailSourceId($crm_id, $row['email_source_id']);

		if (!($tmp['default_support_person_id'] = 
		      $this->GetDefaultSupportPersonId($crm_id, 
			$row['default_support_person_id']))) {
		  $this->error = "Company_CRM::GetData(): " . $this->error;
		  return;
		}

		$tmp['default_escalated_to_id'] = $this->GetDefaultEscalatedToId($crm_id, $row['default_escalated_to_id']);

		$r = array();
		foreach ($this->order as $column) {
		    $r[$column] = $tmp[$column];
		}
		array_push($ret, $r);

	    }

	    return $ret;
	}

	function GetDefaultProblemId($crm_id,$default_problem_id) {
	    global $dbi;
	    $sql = "
		    select problem_id
		    from problem_type
		    where crm_id = ? -- cc.crm_id
		    and problem_id = ? -- cc.default_problem_id
		   ";
	    $res = $dbi->getOne($sql,array($crm_id,$default_problem_id));
	    if (DB::isError($res)) {
		$this->error = "Company_CRM::GetDefaultProblemId(): ".$res->getMessage();
		$res = null;
	    }

	    if (is_null($res)) {
		$sql = "
			select problem_id
			from problem_type
			where crm_id = ? -- cc.crm_id
			limit 1
		       ";
		$res = $dbi->getOne($sql,array($crm_id));
		if (DB::isError($res)) {
		    $this->error = "Company_CRM::GetDefaultProblemId(): ".$res->getMessage();
		    $res = null;
		}
	    }

	    return $res;
	}

	function GetDefaultSourceId($crm_id,$default_source_id) {
	    global $dbi;

	    $sql = "select source_id
		      from source
		     where crm_id = ?
		       and source_id = ?
		   ";
	    $res = $dbi->getOne($sql,array($crm_id,$default_source_id));
	    if (DB::isError($res)) {
		$this->error = "Company_CRM::GetDefaultSourceId(): ".$res->getMessage();
		$res = null;
	    }

	    if (is_null($res)) {
		$sql = "select source_id
			  from source
			 where crm_id = ?
			 limit 1
		       ";
		$res = $dbi->getOne($sql,array($crm_id));
		if (DB::isError($res)) {
		    $this->error = "Company_CRM::GetDefaultSourceId(): ".$res->getMessage();
		    $res = null;
		}
	    }

	    return $res;
	}

	function GetEmailSourceId($crm_id, $email_source_id) {
	    global $dbi;

	    $sql = "select source_id
		      from source
		     where crm_id = ? -- cc.crm_id
		       and source_id = ? -- cc.email_source_id
		   ";
	    $res = $dbi->getOne($sql,array($crm_id,$email_source_id));
	    if (DB::isError($res)) {
		$this->error = "Company_CRM::GetEmailSourceId(): ".$res->getMessage();
		$res = null;
	    }

	    if (is_null($res)) {
		$sql = "select source_id
			  from source
			 where crm_id = ? -- cc.crm_id
			   and upper(source) like '%E%MAIL%'
			 limit 1
		       ";
		$res = $dbi->getOne($sql,array($crm_id));
		if (DB::isError($res)) {
		    $this->error = "Company_CRM::GetEmailSourceId(): ".$res->getMessage();
		    $res = null;
		}
	    }

	    if (is_null($res)) {
		$sql = "select source_id
			  from source
			 where crm_id = ? -- cc.crm_id
			 limit 1
		       ";
		$res = $dbi->getOne($sql,array($crm_id));
		if (DB::isError($res)) {
		    $this->error = "Company_CRM::GetEmailSourceId(): ".$res->getMessage();
		    $res = null;
		}
	    }

	    return $res;
	}

  function GetDefaultSupportPersonId($crm_id, $default_support_person_id) {
    if (!$default_support_person_id) {
      $c = $this->GetData($crm_id);
      $default_support_person_id = $c[0]['default_support_person_id'];
    }

    $pid = $this->Get_Person_ID_by_Type( $crm_id, 'Support', 
      $default_support_person_id);

    if ($pid) return $pid;

    if (is_null($pid)) {
      $this->error = "Company_CRM::GetDefaultSupportPersonId(): "
	. $this->error;
      return;
    }

    $pid = $this->Get_Person_ID_by_Type( $crm_id, 'Support' );

    if (is_null($pid)) {
      $this->error = "Company_CRM::GetDefaultSupportPersonId(): "
	. $this->error;
      return;
    }
      
    if ($pid) return $pid;

    $this->error = 'Company_CRM::GetDefaultSupportPersonId(): '
      . 'No Support Person Found';
    return;
  }

  function GetDefaultEscalatedToId($crm_id, $default_escalated_to_id) {
    if (!$default_escalated_to_id) {
      $c = $this->GetData($crm_id);
      $default_escalated_to_id = $c[0]['default_escalated_to_id'];
    }

    $pid = $this->Get_Person_ID_by_Type( $crm_id, 'Escalated Support', 
      $default_escalated_to_id );

    if ($pid) return $pid;

    $pid = $this->Get_Person_ID_by_Type( $crm_id, 'Escalated Support' );

    if ($pid) return $pid;

    $this->error .= "couldn't find an 'Escalated Support' person";

    return null;
  }

      function Get_Person_ID_by_Type( $crm_id, $type='', $pid='' ) {
        $binds = array( $crm_id );
        $where = '';

        if ($type) {
          $binds[] = $type;
          $where .= ' and pt.person_type = ? ';
        }

        if ($pid) {
          $binds[] = $pid;
          $where .= ' and p.person_id = ? ';
        }

        $sql = "
          SELECT p.person_id
            FROM person p INNER JOIN 
		 person_crm_map pcm ON (p.person_id = pcm.person_id) INNER JOIN 
		 person_crm_type_assign pt ON (pcm.pcrm_map_id = pt.pcrm_map_id)
           WHERE pcm.crm_id = ?
                 $where
        ";

        $h = $this->db->prepare( $sql );
        $res = $this->db->execute( $h, $binds );

        if (DB::isError($res)) {
          $this->error = $res->getMessage() . "\n" . 
	    $res->getUserInfo();
          return;
        }

        if ($r = $res->fetchRow()) return $r[0];
       
        return 0;
      }

    }
?>
