<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'imports.php');
include(DIR_WS_CLASSES.'class.dbform.php');

$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';
$imports = new imports;
$currencies = new currencies;
$dbf = new dbform('imports','imports_id',$conn);

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);
if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$import = $imports->get_import($dbf->select_id);
	// EDIT DES IMPORTS IST SEPARAT
	$default_currency = $export->data->currency;
}else{
	$mode = 'add';
	$default_currency = yes_get_currencies_id_from_code(DEFAULT_CURRENCY);
}

$dbf->fields['date_added']->set_title(ENTRY_DATE_OF_RECEIPT,5);
$dbf->fields['date_added']->set_group_id('main');

$dbf->fields['import_number']->set_title(ENTRY_IMPORT_NUMBER,0);
$dbf->fields['import_number']->set_default_value($imports->get_next_import_number());
$dbf->fields['import_number']->set_required(1);
$dbf->fields['import_number']->set_group_id('main');

// WENN MIND EINE RECHNUNG - STATUSWECHSEL
if(xtc_not_null($dbf->select_id)){
	if($import->data->invoices and is_array($import->data->invoices) and sizeOf($import->data->invoices)>0){
		$dbf->fields['status']->set_title(IMAGE_STATUS,6);
		$dbf->fields['status']->set_options(array(0=>array('id'=>'0','text'=>yes_get_imports_status_name(0)),1=>array('id'=>'1','text'=>yes_get_imports_status_name(1))),$import->data->status);
		$dbf->fields['status']->set_group_id('main');
	}
}


// absender
$dbf->fields['shipper_id']->set_title(ENTRY_SHIPPER,10);
if($mode == 'edit'){
	$shipper_options = array(0=>array('id'=>$import->data->shipper_id,'text'=>yes_get_companies_name($import->data->shipper_id)));
}else{
	if(isset($_GET['default_shipper_id'])){
		// TODO
	}
	$shipper_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}

$dbf->fields['shipper_id']->set_options($shipper_options,'');
$dbf->fields['shipper_id']->set_required(1);
$dbf->fields['shipper_id']->set_group_id('main',15);
$dbf->fields['shipper_id']->set_ajax_function('getCompaniesByLetters');

//$dbf->fields['documents_present']->set_title(ENTRY_DOCUMENTS_PRESENT,12);
//$dbf->fields['documents_present']->set_options(array(0=>array('id'=>'0','text'=>NO),1=>array('id'=>'1','text'=>YES)),false);
//$dbf->fields['documents_present']->set_group_id('main');

//$dbf->fields['gassed']->set_title(TEXT_GASSED,20);
//$dbf->fields['gassed']->set_options(array(0=>array('id'=>'','text'=>TEXT_SELECT),1=>array('id'=>'0','text'=>NO),2=>array('id'=>'1','text'=>YES)),false);
//$dbf->fields['gassed']->set_group_id('main');
//$dbf->fields['gassed']->set_required(1);

$dbf->fields['currency']->set_title(ENTRY_CURRENCY,25);
$dbf->fields['currency']->set_options(yes_get_currencies(),$default_currency);
$dbf->fields['currency']->set_group_id('main');

$dbf->fields['comments']->set_title(COMMENTS,30);
$dbf->fields['comments']->set_group_id('main');

$dbf->add_field('location','int(11)',$extern_table=true);
$dbf->fields['location']->set_title(ENTRY_LOCATION_TYPE,35);
if(DEFAULT_IMPORT_LOCATION > 0)
	$location_options = array(0=>array('id'=>DEFAULT_IMPORT_LOCATION,'text'=>yes_get_location_name(DEFAULT_IMPORT_LOCATION)));
else
	$location_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
$dbf->fields['location']->set_options($location_options,'');
$dbf->fields['location']->set_group_id('main');
$dbf->fields['location']->set_required(1);
$dbf->fields['location']->	set_ajax_function('getAirportsByLetters');

// absender referenz nr
//$dbf->fields['shipper_reference_number']->set_title(ENTRY_SHIPPER_REFERENCE_NUMBER,40);
//$dbf->fields['shipper_reference_number']->set_required(1);
//$dbf->fields['shipper_reference_number']->set_group_id('main');


if($mode == 'edit'){
	$default_transport_types_id = $import->data->transport_types_id;
}else{
	$default_transport_types_id = 'none';
}
$dbf->fields['transport_types_id']->set_title(ENTRY_TRANSPORT_TYPE,50);
$dbf->fields['transport_types_id']->set_options(yes_get_transport_types(),$default_transport_types_id);
$dbf->fields['transport_types_id']->set_show_field_relations(array(
    array('target_field'=>'routing_source','source_value'=>'1','action'=>'show'),
    array('target_field'=>'routing_target','source_value'=>'1','action'=>'show')
));
$dbf->fields['transport_types_id']->set_group_id('main');

$dbf->fields['transport_types_code']->set_title(ENTRY_TRANSPORT_TYPES_CODE,60);
$dbf->fields['transport_types_code']->set_comments(TEXT_TRANSPORT_TYPES_CODE);
$dbf->fields['transport_types_code']->set_required(1);
$dbf->fields['transport_types_code']->set_group_id('main');

$routing_array = yes_get_locations();
if(xtc_not_null($dbf->select_id)){
    $default_routing_source = $export->data->routing_source;
    $default_routing_target = $export->data->routing_target;
}

$dbf->fields['routing_source']->set_title(ENTRY_ROUTING_SOURCE,61);
$dbf->fields['routing_source']->set_group_id('main');
$dbf->fields['routing_source']->set_options($routing_array,$default_routing_source);
$dbf->fields['routing_source']->set_ajax_function('getAirportsByLetters');

$dbf->fields['routing_target']->set_title(ENTRY_ROUTING_TARGET,62);
$dbf->fields['routing_target']->set_group_id('main');
$dbf->fields['routing_target']->set_options($routing_array,$default_routing_target);
$dbf->fields['routing_target']->set_ajax_function('getAirportsByLetters');

if($mode == 'edit'){
	$haulage_service_options = array(0=>array('id'=>$import->data->haulage_service_id,'text'=>yes_get_companies_name($import->data->haulage_service_id)));
}else{
	$haulage_service_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}
$dbf->fields['haulage_service_id']->set_title(ENTRY_HAULAGE_SERVICE,70);
$dbf->fields['haulage_service_id']->set_group_id('main');
$dbf->fields['haulage_service_id']->set_options($haulage_service_options,'');
$dbf->fields['haulage_service_id']->set_ajax_function('getCompaniesByLetters');

// agent
if($mode == 'edit'){
	$transport_agent_options = array(0=>array('id'=>$import->data->transport_agent_id,'text'=>yes_get_companies_name($import->data->transport_agent_id)));
}else{
	$transport_agent_options = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}
$dbf->fields['transport_agent_id']->set_title(ENTRY_TRANSPORT_AGENT,80);
$dbf->fields['transport_agent_id']->set_options($transport_agent_options,'');
$dbf->fields['transport_agent_id']->set_group_id('main');
$dbf->fields['transport_agent_id']->set_ajax_function('getCompaniesByLetters');

// costs
if($mode == 'add'){
	$alloc_array = array('table'=>TABLE_IMPORT_COSTS,'src'=>'imports_id','target'=>'imports_id');
	$dbf->add_field('costs_companies_id','int(11)',$extern_table=true);
	$dbf->fields['costs_companies_id']->set_title(ENTRY_COSTS_POSITION,4);
	$dbf->fields['costs_companies_id']->set_group_id('sub3_1');
	$dbf->fields['costs_companies_id']->set_options(array(0=>array('id'=>'','text'=>TEXT_SELECT)),'');
	$dbf->fields['costs_companies_id']->set_ajax_function('getCompaniesByLetters');
	
	$dbf->fields['costs_companies_id']->set_array(true);
	$dbf->fields['costs_companies_id']->set_array_allocation($alloc_array);
	
	$dbf->add_field('costs','int(11)',$extern_table=true);
	$dbf->fields['costs']->set_title(ENTRY_COSTS,5);
	$dbf->fields['costs']->set_required(1,'float');
	$dbf->fields['costs']->set_comments(TEXT_CURRENCY_FIELD);
	$dbf->fields['costs']->set_group_id('sub3_1');
	$dbf->fields['costs']->set_array(true);
	$dbf->fields['costs']->set_array_allocation($alloc_array);
	
	/* WEG ERSTMAL
	$dbf->add_field('orderer_id','int(11)',$extern_table=true);
	$dbf->fields['orderer_id']->set_title(ENTRY_ORDERER,10);

	$default_orderer = '';
	$orderers = array(0=>array('id'=>'','text'=>TEXT_SELECT));
	if($mode != 'edit'){
		if($_GET['orderer_id'] && $_GET['orderer_id'] > 0){
			$orderers = array(0=>array('id'=>$_GET['orderer_id'],'text'=>yes_get_companies_name(xtc_db_input($_GET['orderer_id']))));
			$default_orderer = $_GET['orderer_id'];
		}
	}
	
	$dbf->fields['orderer_id']->set_options($orderers,$default_orderer);
	$dbf->fields['orderer_id']->set_params('onchange="ajax_update_dropdown(this,\'dbfdivfieldorderer_ships_id\',\'orderer_ships_id\',\'imports\',\'yes_get_all_ships_by_company\');"');
	$dbf->fields['orderer_id']->set_group_id('sub5');
	$dbf->fields['orderer_id']->set_required(1);
	$dbf->fields['orderer_id']->set_ajax_function('getCompaniesByLetters');
	// orderer_id ergibt sich aus
	$dbf->add_field('orderer_ships_id','int(11)',$extern_table=true);
	$dbf->fields['orderer_ships_id']->set_title(ENTRY_SHIP,11);
	$dbf->fields['orderer_ships_id']->set_group_id('sub5');
	$dbf->fields['orderer_ships_id']->set_options(yes_get_ships_by_company($default_orderer),'');
	$dbf->fields['orderer_ships_id']->set_required(1);
	
	$dbf->add_field('orderer_reference_number','varchar(64)',$extern_table=true);
	$dbf->fields['orderer_reference_number']->set_title(ENTRY_REFERENCE_NUMBER,12);
	$dbf->fields['orderer_reference_number']->set_required(1);
	$dbf->fields['orderer_reference_number']->set_group_id('sub5');
	
	$dbf->add_field('content','text',$extern_table=true);
	$dbf->fields['content']->set_title(ENTRY_CONTENT,14);
	$dbf->fields['content']->set_required(3);
	$dbf->fields['content']->set_group_id('sub5');
	
	$dbf->add_field('delivery_conditions','int(11)',$extern_table=true);
	$dbf->fields['delivery_conditions']->set_title(ENTRY_DELIVERY_CONDITIONS,16);
	$dbf->fields['delivery_conditions']->set_options(yes_get_delivery_conditions_import(),'');
	$dbf->fields['delivery_conditions']->set_group_id('sub5');
	
	$dbf->add_field('value_of_goods','float(7,2)',$extern_table=true);
	$dbf->fields['value_of_goods']->set_title(ENTRY_VALUE_OF_GOODS,18);
	$dbf->fields['value_of_goods']->set_group_id('sub5');
	$dbf->fields['value_of_goods']->set_comments(TEXT_CURRENCY_FIELD);


	$dbf->add_field('quantity','int(11)',$extern_table=true);
	$dbf->fields['quantity']->set_title(ENTRY_QUANTITY,0);
	$dbf->fields['quantity']->set_group_id('sub4');
	$dbf->fields['quantity']->set_required(1,'int');
	$dbf->fields['quantity']->set_default_value(1);
	$dbf->fields['quantity']->set_array(true);
	
	$dbf->add_field('collie_format',"int(1)",$extern_table=true);
	$dbf->fields['collie_format']->set_title(ENTRY_COLLIE_FORMAT,1);
	$dbf->fields['collie_format']->set_options(yes_get_collie_formats(),'normal');
	$dbf->fields['collie_format']->set_group_id('sub4');
	$dbf->fields['collie_format']->set_array(true);
	
	$dbf->add_field('collie_format_text',"text",$extern_table=true);
	$dbf->fields['collie_format_text']->set_title(ENTRY_COLLIE_FORMAT_TEXT,2);
	$dbf->fields['collie_format_text']->set_group_id('sub4');
	$dbf->fields['collie_format_text']->set_array(true);
	$dbf->fields['collie_format_text']->set_comments(ENTRY_COLLIE_FORMAT_TEXT_DESC);
	
	$dbf->add_field('weight','float',$extern_table=true);
	$dbf->fields['weight']->set_title(ENTRY_WEIGHT,3);
	$dbf->fields['weight']->set_group_id('sub4');
	$dbf->fields['weight']->set_array(true);
	
	$dbf->add_field('collie_content','text',$extern_table=true);
	$dbf->fields['collie_content']->set_title(ENTRY_CONTENT,6);
	$dbf->fields['collie_content']->set_group_id('sub4');
//	$dbf->fields['collie_content']->set_required(3);
	$dbf->fields['collie_content']->set_array(true);
	
	$dbf->add_field('duane_status','int(11)',$extern_table=true);
	$dbf->fields['duane_status']->set_title(ENTRY_DUANE_STATUS,7);
	$dbf->fields['duane_status']->set_group_id('sub4');
	$dbf->fields['duane_status']->set_options(yes_get_duane_statuses(),'');
	$dbf->fields['duane_status']->set_array(true);
	
	$dbf->add_field('duane_reminder_date','datetime',$extern_table=true);
	$dbf->fields['duane_reminder_date']->set_title(ENTRY_DUANE_STATUS_REMINDER,8);
	$dbf->fields['duane_reminder_date']->set_group_id('sub4');
	$dbf->fields['duane_reminder_date']->set_array(true);
	
	$dbf->add_field('height','int(11)',$extern_table=true);
	$dbf->fields['height']->set_title(ENTRY_HEIGHT,9);
	$dbf->fields['height']->set_group_id('sub4');
	$dbf->fields['height']->set_required(1,'float');
	$dbf->fields['height']->set_array(true);

	$dbf->add_field('width','int(11)',$extern_table=true);
	$dbf->fields['width']->set_title(ENTRY_WIDTH,10);
	$dbf->fields['width']->set_group_id('sub4');
	$dbf->fields['width']->set_required(1,'float');
	$dbf->fields['width']->set_array(true);
	
	$dbf->add_field('depth','int(11)',$extern_table=true);
	$dbf->fields['depth']->set_title(ENTRY_DEPTH,12);
	$dbf->fields['depth']->set_group_id('sub4');
	$dbf->fields['depth']->set_required(1,'float');
	$dbf->fields['depth']->set_array(true);
*/
}



if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
}

// POST DATA ABFANGEN BEIM SPEICHERN
$mode = ($_POST['action'] == 'edit' or $_POST['action'] == 'add') ? $_POST['action'] : $mode; 

$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_IMPORTS),$access='imports');
$smarty->assign('PAGE_MENU',$menu);
//$smarty->assign('SUBGROUP_SHIPPER',TEXT_SUBGROUP_SHIPPER);
$smarty->assign('SUBGROUP_IMPORT',TEXT_SUBGROUP_IMPORT);
//$smarty->assign('SUBGROUP_ORDERER',TEXT_SUBGROUP_ORDERER);
//$smarty->assign('SUBGROUP_AGENT',TEXT_SUBGROUP_AGENT);
$smarty->assign('SUBGROUP_COSTS',TEXT_SUBGROUP_COSTS);
//$smarty->assign('SUBGROUP_SHIP_COSTS',TEXT_SUBGROUP_SHIP_COSTS);
//$smarty->assign('SUBGROUP_COSTS_POSITIONS',TEXT_SUBGROUP_COSTS_POSITIONS);
//$smarty->assign('SUBGROUP_COLLIES',TEXT_SUBGROUP_COLLIES);
$smarty->assign('TEXT_ADD_ENTRY',TEXT_ADD_ENTRY);
$smarty->assign('TEXT_REMOVE_ENTRY',TEXT_REMOVE_ENTRY);

$result = $dbf->get_form($form_name = 'imports',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/import_formular.tpl',$smarty);

if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$sql_update_array = $dbf->get_sql_array($_POST);
			
			// pruefen ob waehrung geaendert
			$currency = $currencies->get_currency($_POST['currency']);
			if($import->data->currency != $currency->data->code){
				$sql_update_array['currency'] = $currency->data->code;
				$sql_update_array['currency_value'] = $currency->data->value;
			} 
			
			// ADRESSDATEN AUS DB
			$address_sql_array = $imports->get_companydata_from_db($_POST['shipper_id'],'shipper');
			$sql_update_array = array_merge($sql_update_array,$address_sql_array);
			$address_sql_array = $imports->get_companydata_from_db($_POST['transport_agent_id'],'transport_agent');
			$sql_update_array = array_merge($sql_update_array,$address_sql_array);
			$address_sql_array = $imports->get_companydata_from_db($_POST['haulage_service_id'],'haulage_service');
			$sql_update_array = array_merge($sql_update_array,$address_sql_array);
			
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_UPDATED,$_POST['import_number']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);

			$currency = $currencies->get_currency($_POST['currency']);
			$sql_insert_array['currency_value'] = $currency->data->value;
			$sql_insert_array['currency'] = $currency->data->code;
			
			
			// ADRESSDATEN AUS DB
			$address_sql_array = $imports->get_companydata_from_db($_POST['shipper_id'],'shipper');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			$address_sql_array = $imports->get_companydata_from_db($_POST['transport_agent_id'],'transport_agent');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			$address_sql_array = $imports->get_companydata_from_db($_POST['haulage_service_id'],'haulage_service');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			
			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_ADDED,$_POST['import_number']),$id);
			
			for($i=0;$i<sizeOf($_POST['costs']);$i++){
				$sql_insert_array = array(
					'imports_id'=>$id,
					'companies_id'=>$_POST['costs_companies_id'][$i],
					'costs_text'=>yes_get_companies_name($_POST['costs_companies_id'][$i]),
					'costs'=>$_POST['costs'][$i]
				);
				xtc_db_perform(TABLE_IMPORT_COSTS,$sql_insert_array);
			}

			/*
			// SCHIFF
			$sql_insert_array = array(
				'imports_id'=>$id,
				'orderer_ships_id'=>$_POST['orderer_ships_id'],
				'orderer_id'=>$_POST['orderer_id'],
				'date_added'=>'now()',
				'orderer_reference_number'=>$_POST['orderer_reference_number'],
				'content'=>$_POST['content'],
				'location'=>yes_get_location_name($_POST['location']),
				'delivery_conditions'=>$_POST['delivery_conditions'],
				'value_of_goods'=>$_POST['value_of_goods']
			);
			$address_sql_array = $imports->get_companydata_from_db($_POST['orderer_id'],'orderer');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			xtc_db_perform(TABLE_IMPORT_SHIPS,$sql_insert_array);
			$isID = $conn->Insert_ID();

			// SCHIFF KOLLIES
			for($i=0;$i<sizeOf($_POST['quantity']);$i++){
				$sql_insert_array = array(
					'import_ships_id'=>$isID,
					'quantity'=>$_POST['quantity'][$i],
					'weight'=>$_POST['weight'][$i],
					'height'=>$_POST['height'][$i],
					'width'=>$_POST['width'][$i],
					'depth'=>$_POST['depth'][$i],
					'collie_content'=>$_POST['collie_content'][$i],
					'format'=>$_POST['collie_format'][$i],
					'format_text'=>(($_POST['collie_format'][$i] == 'special')?$_POST['collie_format_text'][$i]:''),
					'duane_status_id'=>$_POST['duane_status'][$i],
					'duane_reminder_date'=>(($_POST['duane_status'][$i] == 3) ? $_POST['duane_reminder_date'][$i] : '')
				
				);
				xtc_db_perform(TABLE_IMPORT_SHIP_COLLIES,$sql_insert_array);
			}
			*/
			// SCHIFF KOSTEN
			for($i=0;$i<sizeOf($_POST['ship_costs_companies_id']);$i++){
				$sql_insert_array = array(
					'import_ships_id'=>$isID,
					'companies_id'=>$_POST['ship_costs_companies_id'][$i],
					'costs'=>$_POST['ship_costs'][$i],
					'costs_text'=>yes_get_companies_name($_POST['ship_costs_companies_id'][$i])
				);
				xtc_db_perform(TABLE_IMPORT_SHIP_COSTS,$sql_insert_array);
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
