<?

class connect_cvwk0100 extends stdICHModule {


	// Constructor
	function connect_cvwk0100($lng_code){
		global $globalSettings;
	
		$this->stdICHModule("cvwk0100", $lng_code);

		require_once $globalSettings->ti3a0365->paths->ddi;

		$this->_i3a = new troyceI3A();
		
	}

	function updateOrganizationContract($params) {
		// $params[0] = org_id
		// $params[1] = org exists (false), org is new (true)

		$org_id = $params[0];
		$org_is_new = $params[1];

		if (is_numeric($org_id)) {

			$this->_i3a->getOrganizations($org_info, array(array('ORG_ID','='.$org_id),'ORG_NAME','ORG_STS_CODE', 'ORG_DEF_CODE'), false);
			$org_info = current($org_info);
			
			if ($org_info['ORG_DEF_CODE'] == 'MAT') {

				$rs = $this->_db->query("SELECT ctr_id FROM vwk_contract WHERE ctr_org_id = ".$org_id);

				if (!is_numeric($rs->Row('ctr_id'))) {

					//--- 
					$sql = "INSERT INTO vwk_contract SET 
								ctr_org_id	= ".$org_id.",
								ctr_org_state	= ".(($org_info['ORG_STS_CODE']=='act')?'1':'0').",
								ctr_org_name	= '".mysql_escape_string($org_info['ORG_NAME'])."',
								ctr_changedate = NOW(),
								ctr_createdate = NOW()";

					$this->_db->execute($sql);
				
				} else {
					$sql = "UPDATE vwk_contract SET 
								ctr_org_state	= ".(($org_info['ORG_STS_CODE']=='act')?'1':'0').",
								ctr_org_name	= '".mysql_escape_string($org_info['ORG_NAME'])."'
							WHERE ctr_org_id = ".$org_id;

					$this->_db->execute($sql);
				
				}
			}
		}
	}
	

}


?>