<?

class report_parser {
	
	var $_dc = array();

	function report_parser($bed_id, $ckre_id=false, $org_id=false, $org_id_partner=false) {

		$this->_initialize($bed_id, $ckre_id, $org_id, $org_id_partner);
	
	}

	function _initialize($bed_id, $ckre_id=false, $org_id=false, $org_id_partner=false) {
		
		if (isset($GLOBALS['g_obj_html']) && is_object($GLOBALS['g_obj_html'])) {

			$GLOBALS['g_obj_html']->createObject('crbh0100', 'crbh0100');

			//--- DE 
			$GLOBALS['g_obj_html']->createObject('ccrd0200', 'ccrd0200');
			if (isset($GLOBALS['g_obj_ccrd0200']) && is_object($GLOBALS['g_obj_ccrd0200'])) {
				$this->setDataContainer('DE', &$GLOBALS['g_obj_ccrd0200'], $bed_id);
			}

			//--- CKRE
			if (is_numeric($ckre_id)) {
				$GLOBALS['g_obj_html']->createObject('ccrd0100', 'ccrd0100');
				if (isset($GLOBALS['g_obj_ccrd0100']) && is_object($GLOBALS['g_obj_ccrd0100'])) {
					$this->setDataContainer('CKRE', &$GLOBALS['g_obj_ccrd0100'], $ckre_id);
				}
			}

			//--- ORG
			if (is_numeric($org_id)) {
				$GLOBALS['g_obj_html']->createObject('ti3a0365', 'troyceI3A');
				if (isset($GLOBALS['g_obj_ti3a0365']) && is_object($GLOBALS['g_obj_ti3a0365'])) {
					$this->setDataContainer('ORG', &$GLOBALS['g_obj_ti3a0365'], $org_id);
				}
			}

			//--- PARTNER
			if (is_numeric($org_id_partner)) {
				$GLOBALS['g_obj_html']->createObject('ti3a0365', 'troyceI3A');
				if (isset($GLOBALS['g_obj_ti3a0365']) && is_object($GLOBALS['g_obj_ti3a0365'])) {
					$this->setDataContainer('PART', &$GLOBALS['g_obj_ti3a0365'], $org_id_partner);
				}
			}

		} else {

		}
	}

	function setDataContainer($code, &$object, $id) {
		$code = strtoupper($code);
		$this->_dc[$code] = array(	'code'	 => $code
								,	'object' => &$object
								,	'id'	 => $id);
	}

	function _fieldSplit($field) {
		/*
			code:field_name|iets
		*/
		
		$field_code = substr($field, 0, strpos($field, ':'));
		$rest = substr($field, strpos($field, ':')+1);
		if (strpos($rest, '|') === false) {
			$field_name = $rest;
			$field_option = '';
		} else {
			$field_name = substr($rest, 0, strpos($rest, '|'));
			$field_option = substr($rest, strpos($rest, '|')+1);
		}

		return array(strtoupper($field_code), strtoupper($field_name), $field_option);

	}

	function _handleFieldOption($option, $data) {
		// format fmt:"Nummer is: %s"
		// format alt:"Geen nummer beschikbaar"

		if (preg_match('/([a-z]+)="([^"]*)"/i',$option, $matches)) {

			switch ($matches[1]) {
				case 'fmt':

					if (strlen($data) > 0) {
						return sprintf ($matches[2], $data);
					} else {
						return '';
					}

					break;

				case 'alt':

					if (strlen($data) == 0) {
						return $matches[2];
					} else {
						return $data;
					}

					break;
			}

		}
	}

	function parseField($field, $output_format_html=false) {

		list($field_code, $field_name, $field_option) = $this->_fieldSplit($field);
		

		switch ($field_code) {
			case 'ALG':
				
				switch ($field_name) {
					case 'NOW':
						setlocale (LC_TIME, 'nl_NL');

						return strftime('%e %B %Y');
						break;

					case 'PAGENR':
						return '\thepage';
						break;
				}
				

				break;

			case 'DE':

				if (!is_object($this->_dc['DE']['object'])) return '[ERROR: DATACONTAINER NOT AVAILABLE] - ['.$field_code.':'.$field_name.']';

				$output_format = $output_format_html ? CRD_PARSE_OUTPUT_HTML : CRD_PARSE_OUTPUT_PLAIN;

				if (($data = $this->_dc['DE']['object']->parseField($this->_dc['DE']['id'], $field_name, $output_format)) !== false) {
					
					if ($field_option) {
						$data = $this->_handleFieldOption($field_option, $data);
					}

					return trim($data);

				} else {
					return "[ERROR: onbekende code: '". $field_code.":".$field_name."']";
				}


				break;

			case 'ORG':
				
				if (!is_object($this->_dc['ORG']['object'])) return '[ERROR: DATACONTAINER NOT AVAILABLE] - ['.$field_code.':'.$field_name.']';

				$valid_fields = array('ORG_ID','ORG_NAME','GATT_ORG_NUMBER','GATT_KR_PARTNER','GATT_KR_FOOTER_TEXT', 'GATT_KR_DOCTYPE');

				$this->_dc['ORG']['object']->getOrganizations($orgs, array(array('ORG_ID',' = '.$this->_dc['ORG']['id']), 'ORG_NAME'),array('GATT_ORG_NUMBER','GATT_KR_PARTNER','GATT_KR_FOOTER_TEXT', 'GATT_KR_DOCTYPE'));
				$org = array_change_key_case(current($orgs), CASE_UPPER);

				if (in_array($field_name, $valid_fields)) {
					return $org[$field_name];
				} else {
					return "[ERROR: onbekende code: '". $field_code.":".$field_name."']";
				}

				break;

			case 'PART':

				if (!is_object($this->_dc['PART']['object'])) return '[ERROR: DATACONTAINER NOT AVAILABLE] - ['.$field_code.':'.$field_name.']';
				if (!is_numeric($this->_dc['PART']['id'])) $this->_dc['PART']['id'] = 1; #default partner is creditDevice

				$valid_fields = array('ORG_ID','ORG_NAME','GATT_KR_PARTNER','GATT_KR_FOOTER_TEXT');

				$this->_dc['ORG']['object']->getOrganizations($orgs, array(array('ORG_ID',' = '.$this->_dc['PART']['id']), 'ORG_NAME'),array('GATT_KR_PARTNER','GATT_KR_FOOTER_TEXT'));
				$org = array_change_key_case(current($orgs), CASE_UPPER);

				if (in_array($field_name, $valid_fields)) {
					return $org[$field_name];
				} else {
					return "[ERROR: onbekende code: '". $field_code.":".$field_name."']";
				}

				break;

			case 'CKRE':
	
				if (!is_object($this->_dc['CKRE']['object'])) return '[ERROR: DATACONTAINER NOT AVAILABLE] - ['.$field_code.':'.$field_name.']';
				$krediet_data = $this->_dc['CKRE']['object']->getKredietAanvraag($this->_dc['CKRE']['id']);
				$krediet_data = array_change_key_case($krediet_data, CASE_UPPER);

				if (array_key_exists($field_name, $krediet_data)) {
					if ($field_name == 'CKRE_AMOUNT') {
						if (strlen(trim($krediet_data[$field_name])) > 0) {
							return str_replace(',00', '', number_format($krediet_data[$field_name], 2, ',','.'));
						} else {
							return '';
						}
					} else {
						return $krediet_data[$field_name];
					}
				} else {
					return "[ERROR: onbekende code: '". $field_code.":".$field_name."']";
				}

				break;
			
			case 'GR':

				if (!is_object($this->_dc['DE']['object'])) return '[ERROR: DATACONTAINER NOT AVAILABLE] - ['.$field_code.':'.$field_name.']';
	
				switch ($field_name) {
					
					case 'GRAFIEK_1':
					case 'TREND':
					case 'CURRENTRATIO':
					case 'QUICKRATIO':
					case 'RATIO':

						$cs = $this->_dc['DE']['object']->getCreditScoring($this->_dc['DE']['id'], $bedrijf);

						$graph_data = array();
						switch ($field_name) {
							case 'TREND': 
								$graph_data = $cs->data['eigen_vermogen']['source_data']; 
								$graph_type = 'bar_small';
								break;
							case 'GRAFIEK_1': 
								$graph_data = $cs->data['eigen_vermogen']['source_data']; 
								$graph_type = 'bar_small';
								break;
							case 'CURRENTRATIO': 
								$graph_data = $cs->data['current_ratio']['source_data']; 
								$graph_type = 'bar_small';
								$graph_line = 1.5;
								break;
							case 'QUICKRATIO': 
								$graph_data = $cs->data['quick_ratio']['source_data']; 
								$graph_type = 'bar_small';
								$graph_line = 1;
								break;
							case 'RATIO':
								$graph_data['current'] = $cs->data['current_ratio']['source_data'];
								$graph_data['quick'] = $cs->data['quick_ratio']['source_data'];
								$graph_line = array('current' => 1.5, 'quick' => 1);
								$graph_type = 'ratio';
								break;
						}
						
						$graph_ok = false;
						if ($graph_type == 'ratio') {
							if (count($graph_data['current']) > 1 && count($graph_data['quick']) > 1) $graph_ok = true;
						} elseif (count($graph_data) > 1) {
							$graph_ok = true;
						}

						if ($graph_ok) {

							$data_string = base64_encode(serialize($graph_data));

							$url = $this->obj_crbh0100->getSetting('graph_url') . '/generate/generate.php?type='.$graph_type.'&data='.$data_string;

							if ($graph_line) {
								if (is_array($graph_line)) {
									$url .= '&line='.base64_encode(serialize($graph_line));
								} else {
									$url .= '&line='.$graph_line;
								}
							}

							file_get_contents($url);
							$filename = 'graph_'.md5($data_string).'.png';
							return $filename;

						} else {
							return false;
						}

						break;

					case 'CREDITSCORE':

						$cs = $this->_dc['DE']['object']->getCreditScoring($this->_dc['DE']['id'], $bedrijf);
						if ($cs->grand_total_text) {						
							return 'graph_cs_'. $cs->grand_total_text . '.png';
						} else {
							return false;
						}
						break;
				
				}


				break;

			default:

				return "[ERROR: onbekende code: '". $field_code."']";
				break;

		}

	}
}

?>