<?php
include('includes/application_top.php');
require_once(DIR_WS_CLASSES.'imports.php');
require_once(DIR_WS_CLASSES.'import_ships.php');

include(DIR_WS_CLASSES.'class.dbform.php');
$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';

$dbf = new dbform('import_ships','import_ships_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_ships = new import_ships();
	$import_ship = $import_ships->get_import_ship($dbf->select_id);
	$imports_id = $import_ship->data->imports_id;
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
	$imports_id = ($_GET['iID']) ? xtc_db_input($_GET['iID']) : xtc_db_input($_POST['imports_id']);
	$dbf->add_hidden_field('imports_id',$imports_id);
}
$imports = new imports;
$exclude_ships_array = $imports->get_saved_ship_id_array($imports_id);

if(xtc_not_null($dbf->select_id)){
	$default_array = array(
		0=>array(
			'id'=>yes_get_location_id_by_code($import_ship->data->location),
			$import_ship->data->location,'text'=>$import_ship->data->location
		)
	);
}

// in  imports_edit
//$dbf->fields['location']->set_title(ENTRY_LOCATION_TYPE,2);
//$dbf->fields['location']->set_options($default_array,'');
//$dbf->fields['location']->set_required(1);
//$dbf->fields['location']->set_ajax_function('getAirportsCodeByLetters');

if($mode == 'add'){
	$dbf->fields['orderer_id']->set_title(ENTRY_ORDERER,10);
	$dbf->fields['orderer_id']->set_options(array(0=>array('id'=>'','text'=>TEXT_SELECT)),'');
	$dbf->fields['orderer_id']->set_params('onchange="ajax_update_dropdown(this,\'dbfdivfieldorderer_ships_id\',\'orderer_ships_id\',\'import_ships\',\'yes_get_available_ships_id_for_new_import_ship|imports_id\');"');
	$dbf->fields['orderer_id']->set_required(1);
	$dbf->fields['orderer_id']->set_ajax_function('getCompaniesByLetters');
	$dbf->fields['orderer_id']->set_group_id('main');
	
	// orderer_id ergibt sich aus
	$default_orderer_id = ($mode == 'edit') ? $import_ship->data->orderer_id : 0;  
	$dbf->fields['orderer_ships_id']->set_title(ENTRY_SHIP,11);
	$dbf->fields['orderer_ships_id']->set_options(yes_get_available_ships_id_for_new_import_ship($default_orderer_id,$imports_id),'');
	$dbf->fields['orderer_ships_id']->set_required(1);
	$dbf->fields['orderer_ships_id']->set_group_id('main');
}else{
	$dbf->fields['orderer_id']->set_title(ENTRY_ORDERER,10);
	$dbf->fields['orderer_id']->type = 'hidden';
	$dbf->fields['orderer_id']->set_view_hidden_field(true);
	$dbf->fields['orderer_id']->set_comments(yes_get_companies_name($import_ship->data->orderer_id));
	$dbf->fields['orderer_id']->set_group_id('main');
	
	$dbf->fields['orderer_ships_id']->set_title(ENTRY_SHIP,11);
	$dbf->fields['orderer_ships_id']->type = 'hidden';
	$dbf->fields['orderer_ships_id']->set_view_hidden_field(true);
	$dbf->fields['orderer_ships_id']->set_comments(yes_get_ships_name($import_ship->data->orderer_ships_id));
	$dbf->fields['orderer_ships_id']->set_group_id('main');
}

$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('main');

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

$dbf->fields['content']->set_title(ENTRY_CONTENT,15);
$dbf->fields['content']->set_group_id('main');

$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('main');

$dbf->fields['value_of_goods']->set_title(ENTRY_VALUE_OF_GOODS,18);
$dbf->fields['value_of_goods']->set_required(1,'float');
$dbf->fields['value_of_goods']->set_comments(TEXT_CURRENCY_FIELD);
$dbf->fields['value_of_goods']->set_group_id('main');

// wird bei imports_edit gesetzt
//$dbf->fields['currencies_id']->set_title(ENTRY_CURRENCY,20);
//$dbf->fields['currencies_id']->set_options(yes_get_currencies(),'');

	$alloc_array = array('table'=>TABLE_IMPORT_SHIP_COLLIES,'src'=>'import_ships_id','target'=>'import_ships_id');
	$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->fields['quantity']->set_array_allocation($alloc_array);

	$dbf->add_field('format',"int(1)",$extern_table=true);
	$dbf->fields['format']->set_title(ENTRY_COLLIE_FORMAT,1);
	$dbf->fields['format']->set_options(yes_get_collie_formats(),'normal');
	$dbf->fields['format']->set_group_id('sub4');
	$dbf->fields['format']->set_array(true);
	$dbf->fields['format']->set_array_allocation($alloc_array);

	$dbf->add_field('format_text',"text",$extern_table=true);
	$dbf->fields['format_text']->set_title(ENTRY_COLLIE_FORMAT_TEXT,2);
	$dbf->fields['format_text']->set_group_id('sub4');
	$dbf->fields['format_text']->set_array(true);
	$dbf->fields['format_text']->set_comments(ENTRY_COLLIE_FORMAT_TEXT_DESC);
	$dbf->fields['format_text']->set_array_allocation($alloc_array);

	$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->fields['weight']->set_array_allocation($alloc_array);

	// GELOESCHT
	//$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_id','int(11)',$extern_table=true);
	$dbf->fields['duane_status_id']->set_title(ENTRY_DUANE_STATUS,7);
	$dbf->fields['duane_status_id']->set_group_id('sub4');
	$dbf->fields['duane_status_id']->set_options(yes_get_duane_statuses(),'');
	$dbf->fields['duane_status_id']->set_array(true);
	$dbf->fields['duane_status_id']->set_array_allocation($alloc_array);

	$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->fields['duane_reminder_date']->set_array_allocation($alloc_array);

	$dbf->add_field('documents_present','int(1)',$extern_table=true);
	$dbf->fields['documents_present']->set_title(ENTRY_DOCUMENTS_PRESENT,10);
	$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('sub4');
	$dbf->fields['documents_present']->set_array_allocation($alloc_array);

	$dbf->add_field('gassed','int(1)',$extern_table=true);
	$dbf->fields['gassed']->set_title(TEXT_GASSED,15);
	$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['gassed']->set_group_id('sub4');
	$dbf->fields['gassed']->set_array_allocation($alloc_array);

	$dbf->add_field('height','int(11)',$extern_table=true);
	$dbf->fields['height']->set_title(ENTRY_HEIGHT,20);
	$dbf->fields['height']->set_group_id('sub4');
	$dbf->fields['height']->set_required(1,'float');
	$dbf->fields['height']->set_array(true);
	$dbf->fields['height']->set_array_allocation($alloc_array);

	$dbf->add_field('width','int(11)',$extern_table=true);
	$dbf->fields['width']->set_title(ENTRY_WIDTH,30);
	$dbf->fields['width']->set_group_id('sub4');
	$dbf->fields['width']->set_required(1,'float');
	$dbf->fields['width']->set_array(true);
	$dbf->fields['width']->set_array_allocation($alloc_array);

	$dbf->add_field('depth','int(11)',$extern_table=true);
	$dbf->fields['depth']->set_title(ENTRY_DEPTH,40);
	$dbf->fields['depth']->set_group_id('sub4');
	$dbf->fields['depth']->set_required(1,'float');
	$dbf->fields['depth']->set_array(true);
	$dbf->fields['depth']->set_array_allocation($alloc_array);

$smarty->assign('SUBGROUP_COLLIES',TEXT_SUBGROUP_COLLIES);

	$alloc_array = array('table'=>TABLE_IMPORT_SHIP_COLLIES,'src'=>'import_ships_id','target'=>'import_ships_id');
	$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->fields['quantity']->set_array_allocation($alloc_array);

$smarty->assign('SUBGROUP_COSTS',TEXT_SUBGROUP_COSTS);

	$alloc_array = array('table'=>TABLE_IMPORT_SHIP_COSTS,'src'=>'import_ships_id','target'=>'import_ships_id');
	$dbf->add_field('companies_id','int(11)',$extern_table=true);
	$dbf->fields['companies_id']->set_title(ENTRY_COSTS_POSITION,1);
	$dbf->fields['companies_id']->set_group_id('sub3');
	$dbf->fields['companies_id']->set_options(array(0=>array('id'=>'','text'=>TEXT_SELECT)),'');
	$dbf->fields['companies_id']->set_ajax_function('getCompaniesByLetters');
	$dbf->fields['companies_id']->set_array(true);
	$dbf->fields['companies_id']->set_array_allocation($alloc_array);

	$dbf->add_field('costs','int(11)',$extern_table=true);
	$dbf->fields['costs']->set_title(ENTRY_COSTS,10);
	$dbf->fields['costs']->set_required(1,'float');
	$dbf->fields['costs']->set_comments(TEXT_CURRENCY_FIELD);
	$dbf->fields['costs']->set_group_id('sub3');
	$dbf->fields['costs']->set_array(true);
	$dbf->fields['costs']->set_array_allocation($alloc_array);









$smarty->assign('TEXT_ADD_ENTRY',TEXT_ADD_ENTRY);
$smarty->assign('TEXT_REMOVE_ENTRY',TEXT_REMOVE_ENTRY);

// 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,'action=overview&id='.$imports_id),$access='import_ships');
$smarty->assign('PAGE_MENU',$menu);
$heading_title_param = yes_get_import_number_from_imports_id($imports_id);

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


if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$sql_update_array = $dbf->get_sql_array($_POST);
			$address_sql_array = $imports->get_companydata_from_db($_POST['orderer_id'],'orderer');
			$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_SHIP_UPDATED,yes_get_ships_name($_POST['orderer_ships_id'])),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$imports_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['date_added'] = 'now()';
			$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($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			// SCHIFF KOLLIES
			for($i=0;$i<sizeOf($_POST['quantity']);$i++){
				$sql_insert_array = array(
					'import_ships_id'=>$id,
					'quantity'=>$_POST['quantity'][$i],
					'weight'=>$_POST['weight'][$i],
					'height'=>$_POST['height'][$i],
					'width'=>$_POST['width'][$i],
					'depth'=>$_POST['depth'][$i],
					'format'=>$_POST['format'][$i],
					'format_text'=>(($_POST['format'][$i] == 'special')?$_POST['format_text'][$i]:''),
					'duane_status_id'=>$_POST['duane_status_id'][$i],
					'duane_reminder_date'=>(($_POST['duane_status'][$i] == 3) ? $_POST['duane_reminder_date'][$i] : ''),
					'documents_present'=>$_POST['documents_present'][$i],
					'gassed'=>$_POST['gassed'][$i]

				);
				xtc_db_perform(TABLE_IMPORT_SHIP_COLLIES,$sql_insert_array);
			}

			// SCHIFF KOSTEN
			for($i=0;$i<sizeOf($_POST['costs']);$i++){
				$sql_insert_array = array(
					'import_ships_id'=>$id,
					'companies_id'=>$_POST['companies_id'][$i],
					'costs'=>$_POST['costs'][$i],
					'costs_text'=>yes_get_companies_name($_POST['companies_id'][$i])
				);
				xtc_db_perform(TABLE_IMPORT_SHIP_COSTS,$sql_insert_array);
			}

			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_ADDED,yes_get_ships_name($_POST['orderer_ships_id'])),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$imports_id));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
