<?

require_once('class.stdModule.php');

class ccrd0100 extends stdModule {

	function ccrd0100() {
		$this->stdModule('ccrd0100');
	}

	function setKredietAanvraag($values) {
		
		if (!is_numeric($_SESSION['session_usr_id'])) {
			return;
		}

		$ka_values = array();
		$ka_values['CKRE_ID']				= 'CCRD_KREDIET_SEQ.NEXTVAL';
		$ka_values['CKRE_USR_ID']			= $_SESSION['session_usr_id'];
		$ka_values['CKRE_ORG_ID']			= (is_numeric($_SESSION['session_org_id']) ? $_SESSION['session_org_id'] : $this->getSetting('default_org_id'));
		$ka_values['CKRE_CSTA_ID']			= 1;
		$ka_values['CKRE_REQUEST_DATE']		= 'SYSDATE';
		$ka_values['CKRE_CLOSE_DATE']		= 'NULL';
		$ka_values['CKRE_NAME']				= "'".$this->_db->db_escape_string($values['CKRE_NAME'])."'";
		$ka_values['CKRE_ADDRESS_STREET']	= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_STREET'])."'";
		$ka_values['CKRE_ADDRESS_NUMBER']	= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_NUMBER'])."'";
		$ka_values['CKRE_ADDRESS_ZIP']		= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_ZIP'])."'";
		$ka_values['CKRE_ADDRESS_CITY']		= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_CITY'])."'";
		$ka_values['CKRE_ADDRESS_COUNTRY']	= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_COUNTRY'])."'";
		$ka_values['CKRE_ADDRESS_PHONE']	= "'".$this->_db->db_escape_string($values['CKRE_ADDRESS_PHONE'])."'";
		$ka_values['CKRE_ADDRESS_COUNTRY']	= "'".($values['CKRE_ADDRESS_COUNTRY'] ? $this->_db->db_escape_string($values['CKRE_ADDRESS_COUNTRY']) : 'NL')."'";
		$ka_values['CKRE_BTW_NUMBER']		= "'".$this->_db->db_escape_string($values['CKRE_BTW_NUMBER'])."'";
		$ka_values['CKRE_CPRO_ID']			= $values['CKRE_CPRO_ID'];
		$ka_values['CKRE_AMOUNT']			= ($values['CKRE_AMOUNT'] ? (float)str_replace(',','.',$values['CKRE_AMOUNT']) : 'NULL');
		$ka_values['CKRE_CURRENCY']			= "'".($values['CKRE_CURRENCY'] ? $values['CKRE_CURRENCY'] : 'EUR')."'";
		$ka_values['CKRE_REASON']			= "'".$this->_db->db_escape_string($values['CKRE_REASON'])."'";
		$ka_values['CKRE_REFERENCE_NAME']	= "'".$this->_db->db_escape_string($values['CKRE_REFERENCE_NAME'])."'";
		$ka_values['CKRE_US_FREQ']			= (is_numeric($values['CKRE_US_FREQ']) ? (int)$values['CKRE_US_FREQ'] : 'NULL');
		$ka_values['CKRE_US_BASE_ID']		= (is_numeric($values['CKRE_US_BASE_ID']) ? (int)$values['CKRE_US_BASE_ID'] : 'NULL');
		$ka_values['CKRE_BED_ID']			= (is_numeric($values['CKRE_BED_ID']) ? (int)$values['CKRE_BED_ID'] : 'NULL');

		$sql = "INSERT INTO CCRD_KREDIET (".implode(", ",array_keys($ka_values)).") VALUES (".implode(", ",array_values($ka_values)).")";
		$this->_db->execute($sql);

	}

	function deleteKredietAanvraag($ckre_id) {
		if (!is_numeric($ckre_id)) return false;

		$sql = "DELETE FROM ccrd_krediet WHERE ckre_id = ".$ckre_id;
		$this->_db->execute($sql);

	}

	function removeDupKredietAanvraag($values) {
		
		if (!isset($values['CKRE_USR_ID'])) $values['CKRE_USR_ID'] = $_SESSION['session_usr_id'];
		if (!isset($values['CKRE_ORG_ID'])) $values['CKRE_ORG_ID'] = (is_numeric($_SESSION['session_org_id']) ? $_SESSION['session_org_id'] : $this->getSetting('default_org_id'));

		$sql = "SELECT ckre_id 
				FROM ccrd_krediet 
				WHERE ckre_usr_id = ".$values['CKRE_USR_ID']."
				AND	ckre_org_id = ".$values['CKRE_ORG_ID']."
				AND ckre_request_date > SYSDATE-1
				AND ckre_cpro_id = ".$this->getSetting('creditscore_product_id')."
				AND ckre_csta_id = 1
				AND LOWER(ckre_name) LIKE '%".strtolower($this->_db->db_escape_string($values['CKRE_NAME']))."%'";

		
		$rs = $this->_db->query($sql);
		while ($r = $rs->row()) {
			$this->deleteKredietAanvraag($r['CKRE_ID']);	
			$rs->moveNext();
		}
		$rs->Close();
	}

	function getKredietAanvraag($ckre_id) {
		if (!is_numeric($ckre_id)) return false;

		$sql = "SELECT * 
				FROM ccrd_krediet 
				LEFT JOIN ccrd_product ON cpro_id = ckre_cpro_id
				LEFT JOIN ccrd_countries ON ccou_code = ckre_address_country
				WHERE ckre_id = ".$ckre_id;
		$rs = $this->_db->query($sql);
		$ka = $rs->Row();
		$rs->Close();

		return $ka;

	}

	function getTemplatesByProduct($cpro_id) {
		if (!is_numeric($cpro_id)) return false;
		$sql = "SELECT * 
				FROM ccrd_product_templates
				WHERE ctpl_cpro_id = ".$cpro_id;

		$result = array();
		$rs = $this->_db->query($sql);
		while ($r = $rs->row()) {
			$result[] = $r;
			$rs->moveNext();
		}
		$rs->Close();

		return $result;

	}
}

?>