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

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

$dbf = new dbform('exports','exports_id',$conn);

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);
if(xtc_not_null($dbf->select_id)){
	$export = $exports->get_export($dbf->select_id);
	$mode = 'edit';
}else{
	$mode = 'add';
}

$dbf->fields['export_number']->set_title(ENTRY_EXPORT_NUMBER,1);
$dbf->fields['export_number']->set_default_value($exports->get_next_export_number());
$dbf->fields['export_number']->set_required(1);
$dbf->fields['export_number']->set_group_id('main');

if(xtc_not_null($dbf->select_id)){
	$recipients = array(0=>array('id'=>$export->data->recipient_id,'text'=>yes_get_companies_name($export->data->recipient_id)));
}else{
	$recipients = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}

$dbf->fields['recipient_id']->set_title(ENTRY_RECIPIENT,5);
$dbf->fields['recipient_id']->set_options($recipients,'');
$dbf->fields['recipient_id']->set_required(1);
$dbf->fields['recipient_id']->set_params('onchange="ajax_update_dropdown(this,\'dbfdivfieldrecipient_contact_id\',\'recipient_contact_id\',\'exports\',\'yes_get_company_customers\');"');
$dbf->fields['recipient_id']->set_group_id('main');
$dbf->fields['recipient_id']->set_ajax_function('getCompaniesByLetters');

$dbf->fields['recipient_contact_id']->set_title(ENTRY_RECIPIENT_CONTACT,10);
$dbf->fields['recipient_contact_id']->set_options(yes_get_company_customers(),'');
$dbf->fields['recipient_contact_id']->set_group_id('main');

if(xtc_not_null($dbf->select_id)){
	$agents = array(0=>array('id'=>$export->data->agent_id,'text'=>yes_get_companies_name($export->data->agent_id)));
}else{
	$agents = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}

$dbf->fields['agent_id']->set_title(ENTRY_AGENT,15);
$dbf->fields['agent_id']->set_options($agents,'');
$dbf->fields['agent_id']->set_params('onchange="ajax_update_dropdown(this,\'dbfdivfieldagent_contact_id\',\'agent_contact_id\',\'exports\',\'yes_get_company_customers\');"');
$dbf->fields['agent_id']->set_group_id('main');
$dbf->fields['agent_id']->set_ajax_function('getCompaniesByLetters');

$dbf->fields['agent_contact_id']->set_title(ENTRY_AGENT_CONTACT,20);
$dbf->fields['agent_contact_id']->set_options(yes_get_company_customers(),'');
$dbf->fields['agent_contact_id']->set_group_id('main');

$dbf->fields['transport_types_id']->set_title(ENTRY_TRANSPORT_TYPE,25);
if(xtc_not_null($dbf->select_id)){
    $dbf->fields['transport_types_id']->set_options(yes_get_transport_types(),$export->data->transport_types_id);
}else{
    $dbf->fields['transport_types_id']->set_options(yes_get_transport_types(),'none');
}
//$dbf->fields['transport_types_id']->set_params('onchange="_show_type_fields(this);"');
$dbf->fields['transport_types_id']->set_group_id('main');
$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'),
    array('target_field'=>'start_transport_flight_number','source_value'=>'1','action'=>'show'),
    array('target_field'=>'end_transport_flight_number','source_value'=>'1','action'=>'show'),
    array('target_field'=>'transport_company_id','source_value'=>'1','action'=>'hide'),
    array('target_field'=>'start_transport_location','source_value'=>'1','action'=>'hide'),
    array('target_field'=>'end_transport_location','source_value'=>'1','action'=>'hide')
));

$dbf->fields['transport_types_code']->set_title(ENTRY_TRANSPORT_TYPES_CODE,30);
$dbf->fields['transport_types_code']->set_comments(TEXT_TRANSPORT_TYPES_CODE);
$dbf->fields['transport_types_code']->set_group_id('main');
$dbf->fields['transport_types_code']->set_helper('getAwbNumbersFromAirlines');

$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,31);
$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,32);
$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');

$dbf->fields['start_transport_flight_number']->set_title(ENTRY_START_TRANSPORT_FLIGHT_NUMBER,33);
$dbf->fields['start_transport_flight_number']->set_group_id('main');
$dbf->fields['end_transport_flight_number']->set_title(ENTRY_END_TRANSPORT_FLIGHT_NUMBER,33);
$dbf->fields['end_transport_flight_number']->set_group_id('main');



if(xtc_not_null($dbf->select_id)){
	$transport_companies = array(0=>array('id'=>$export->data->transport_company_id,'text'=>yes_get_companies_name($export->data->transport_company_id)));
	$default_currency = $export->data->currency;
}else{
	$transport_companies = array(0=>array('id'=>'','text'=>TEXT_SELECT));
	$default_currency = yes_get_currencies_id_from_code(DEFAULT_CURRENCY);
}
$dbf->fields['transport_company_id']->set_title(ENTRY_TRANSPORT_COMPANY,35);
$dbf->fields['transport_company_id']->set_options($transport_companies,'');
$dbf->fields['transport_company_id']->set_group_id('main');
$dbf->fields['transport_company_id']->set_ajax_function('getCompaniesByLetters');

$dbf->fields['start_transport_location']->set_title(ENTRY_START_TRANSPORT_LOCATION,40);
// EXTRA PRUEFEN IN FORM TEMPLATE DA EIN/AUSBLENDBAR
//$dbf->fields['start_transport_location']->set_required(3);
$dbf->fields['start_transport_location']->set_group_id('main');

$dbf->fields['start_transport_date']->set_title(ENTRY_START_TRANSPORT_DATE,45);
$dbf->fields['start_transport_date']->set_group_id('main');

$dbf->fields['end_transport_location']->set_title(ENTRY_END_TRANSPORT_LOCATION,50);
// EXTRA PRUEFEN IN FORM TEMPLATE DA EIN/AUSBLENDBAR
//$dbf->fields['end_transport_location']->set_required(3);
$dbf->fields['end_transport_location']->set_group_id('main');

$dbf->fields['end_transport_date']->set_title(ENTRY_END_TRANSPORT_DATE,55);
$dbf->fields['end_transport_date']->set_group_id('main');

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

	// costs
if($mode == 'add'){
	
	$import_ships_id = '';
	if($_GET['iID']){
		if(is_array($_GET['iID'])){
			for($i=0;$i<sizeOf($_GET['iID']);$i++){
				$import_ships_id .= $_GET['iID'][$i].'_';
			}
			$single_import_ships_id = $_GET['iID'][0];
		}else{
			$single_import_ships_id = $_GET['iID'];
			$import_ships_id = $_GET['iID'];
		}
	}else{
		$single_import_ships_id = current(split('_',$_POST['import_ships_id']));
	}
	$import_ships = new import_ships;
	$import_ship = $import_ships->get_import_ship($single_import_ships_id);
	$ships_id = $import_ship->data->orderer_ships_id;
	if(strstr($import_ships_id,'_'))
		$import_ships_id = substr($import_ships_id,0,strlen($import_ships_id)-1);
	$dbf->add_hidden_field('import_ships_id',$import_ships_id,$extern_table=true);
	$dbf->fields['import_ships_id']->set_group_id('sub1');
		
	$ship_name = yes_get_ships_name($ships_id);
	$ship_company_id = yes_get_ship_companies_id_by_role($ships_id,'owner');
	$ship_owner_name = yes_get_companies_name($ship_company_id);
	$ship_invoice_recipients_id = yes_get_ship_companies_id_by_role($ships_id,'invoice');
	$dbf->add_hidden_field('ships_id',$ships_id,$extern_table=true);
	$dbf->fields['ships_id']->set_group_id('sub1');
	
	$dbf->add_field('contact_id','int(11)',$extern_table=true);
	$dbf->fields['contact_id']->set_title(ENTRY_CONTACT,0);
	$contact_id_options = array_merge(array(0=>array('id'=>'','text'=>TEXT_SELECT)),yes_get_company_customers($ship_company_id));
	$dbf->fields['contact_id']->set_options($contact_id_options,'');
	$dbf->fields['contact_id']->set_group_id('sub1');
	
	$dbf->add_field('eta','datetime',$extern_table=true);
	$dbf->fields['eta']->set_title(ENTRY_ETA,1);
	$dbf->fields['eta']->set_group_id('sub1');
	
	$dbf->add_field('ets','datetime',$extern_table=true);
	$dbf->fields['ets']->set_title(ENTRY_ETS,5);
	$dbf->fields['ets']->set_group_id('sub1');

	/*
	$vpe_array = array();
	$total_collie_qty = yes_get_sum_of_collies_from_import_ships( $_GET['iID'] );
	for($i=0;$i<=$total_collie_qty;$i++){
		$vpe_array[] = array('id'=>$i,'text'=>$i);
	}
	$dbf->add_field('vpe','int(11)',$extern_table=true);
	$dbf->fields['vpe']->set_title(ENTRY_VPE,10);
	$dbf->fields['vpe']->set_options($vpe_array,0);
	$dbf->fields['vpe']->set_params('onchange="_collie_handler(this);"');
	$dbf->fields['vpe']->set_group_id('sub1');
	 * 
	 */

	$invoice_recipients = array(0=>array('id'=>'','text'=>TEXT_SELECT));
	$invoice_recipients[] = array('id'=>$ship_invoice_recipients_id,'text'=>yes_get_company_name($ship_invoice_recipients_id));
	$dbf->add_field('invoice_recipient_id','int(11)',$extern_table=true);
	$dbf->fields['invoice_recipient_id']->set_title(ENTRY_INVOICE_RECIPIENT,15);
	$dbf->fields['invoice_recipient_id']->set_options($invoice_recipients,$ship_invoice_recipients_id);
	$dbf->fields['invoice_recipient_id']->set_group_id('sub1');
	$dbf->fields['invoice_recipient_id']->set_ajax_function('getCompaniesByLetters');



	$conditions_array = yes_get_delivery_conditions_import();
	$dbf->add_field('delivery_conditions','int(11)',$extern_table=true);
	$dbf->fields['delivery_conditions']->set_title(ENTRY_DELIVERY_CONDITIONS,20);
	$dbf->fields['delivery_conditions']->set_options($conditions_array,'');
	$dbf->fields['delivery_conditions']->set_group_id('sub1');


	// WIRD GAR NICHT GEBRAUCHT?
	/*
	$alloc_array = array('table'=>TABLE_EXPORT_COLLIES,'src'=>'exports_id','target'=>'exports_id');
	$dbf->add_field('width','int(11)',$extern_table=true);
	$dbf->fields['width']->set_title(ENTRY_WIDTH,8);
	$dbf->fields['width']->set_group_id('sub2');
//	$dbf->fields['width']->set_required(1,'int');
	$dbf->fields['width']->set_array(true);
	$dbf->fields['width']->set_array_allocation($alloc_array);
	
	$dbf->add_field('height','int(11)',$extern_table=true);
	$dbf->fields['height']->set_title(ENTRY_HEIGHT,10);
	$dbf->fields['height']->set_group_id('sub2');
//	$dbf->fields['height']->set_required(1,'int');
	$dbf->fields['height']->set_array(true);
	$dbf->fields['height']->set_array_allocation($alloc_array);
	
	$dbf->add_field('depth','int(11)',$extern_table=true);
	$dbf->fields['depth']->set_title(ENTRY_DEPTH,12);
	$dbf->fields['depth']->set_group_id('sub2');
//	$dbf->fields['depth']->set_required(1,'int');
	$dbf->fields['depth']->set_array(true);
	$dbf->fields['depth']->set_array_allocation($alloc_array);
	
	$dbf->add_field('units_id','int(11)',$extern_table=true);
	$dbf->fields['units_id']->set_title(ENTRY_UNITS,14);
	$dbf->fields['units_id']->set_group_id('sub2');
//	$dbf->fields['units_id']->set_required(1,'int');
	$dbf->fields['units_id']->set_options(yes_get_units(),'');
	$dbf->fields['units_id']->set_array(true);
	$dbf->fields['units_id']->set_array_allocation($alloc_array);
	 * 
	 */
	
	$alloc_array = array('table'=>TABLE_EXPORT_COSTS,'src'=>'exports_id','target'=>'exports_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');
	$dbf->fields['costs_companies_id']->set_options(array(0=>array('id'=>'','text'=>TEXT_SELECT) ),'');
	$dbf->fields['costs_companies_id']->set_array(true);
	$dbf->fields['costs_companies_id']->set_array_allocation($alloc_array);
	$dbf->fields['costs_companies_id']->set_ajax_function('getCompaniesByLetters');
	
	$dbf->add_field('costs','int(11)',$extern_table=true);
	$dbf->fields['costs']->set_title(ENTRY_COSTS,6);
	$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('SUBGROUP_SHIPPER',sprintf(TEXT_SUBGROUP_SHIPPER,$ship_name,$ship_owner_name));
	$smarty->assign('TEXT_ADD_ENTRY',TEXT_ADD_ENTRY);
	$smarty->assign('TEXT_REMOVE_ENTRY',TEXT_REMOVE_ENTRY);
}

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_EXPORTS),$access='exports');
$smarty->assign('PAGE_MENU',$menu);

$smarty->assign('SUBGROUP_MAIN',TEXT_SUBGROUP_MAIN);
$smarty->assign('SUBGROUP_COLLIES',TEXT_SUBGROUP_COLLIES);
$smarty->assign('SUBGROUP_COSTS',TEXT_SUBGROUP_COSTS);
$smarty->assign('TEXT_TRANSPORT_TYPES_CODE',TEXT_TRANSPORT_TYPES_CODE);

$result = $dbf->get_form($form_name = 'exports',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/export_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($export->data->currency != $currency->data->code){
				$sql_update_array['currency'] = $currency->data->code;
				$sql_update_array['currency_value'] = $currency->data->value;
			} 
			
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_UPDATED,$_POST['export_number']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			if($sql_insert_array['transport_types_id'] == 1){
				// LUFTFRACHT - AWB CHECK
				$airlines_id = (int)xtc_db_input($_POST['selectairline']);
				$awb_status = yes_get_awb_status($airlines_id,$sql_insert_array['transport_types_code']);
				switch($awb_status){
					case -1:
						$messageStack->add_session(MESSAGE_ERROR_CREATE_AWB_MISSING,'error');
						xtc_redirect(xtc_href_link(FILENAME_EXPORTS_EDIT,'iID='.$_POST['import_ships_id']));
						break;
					case 0:
						$sql_insert_array['transport_types_airlines_id'] = $airlines_id;
						break;
					case 1:
						$messageStack->add_session(MESSAGE_ERROR_CREATE_AWB_LOCKED,'error');
						xtc_redirect(xtc_href_link(FILENAME_EXPORTS_EDIT,'iID='.$_POST['import_ships_id']));
						break;
				}
			}


			
			$currency = $currencies->get_currency($_POST['currency']);
			$sql_insert_array['currency_value'] = $currency->data->value; 
			
			$sql_insert_array['date_added'] = 'now()';
			
			$address_sql_array = $exports->get_companydata_from_db($_POST['recipient_id'],'recipient');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			
			$address_sql_array = $exports->get_companydata_from_db($_POST['agent_id'],'agent');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			
			$address_sql_array = $exports->get_companydata_from_db($_POST['transport_company_id'],'transport_company');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			
			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();

			// EBEN GESPEICHERTE AWB NUMMER DEAKTIVIEREN
			if($sql_insert_array['transport_types_airlines_id']){
				yes_update_awb_status($sql_insert_array['transport_types_airlines_id'],$sql_insert_array['transport_types_code'],1);
			}

			
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_ADDED,$_POST['name']),$id);
			
			// costs
			for($i=0;$i<sizeOf($_POST['costs_companies_id']);$i++){
				$sql_insert_array = array(
					'exports_id'=>$id,
					'companies_id'=>$_POST['costs_companies_id'][$i],
					'costs'=>$_POST['costs'][$i],
					'costs_text'=>yes_get_companies_name($_POST['costs_companies_id'][$i])
				);
				xtc_db_perform(TABLE_EXPORT_COSTS,$sql_insert_array);
			}
			
			// export target - beim add ist es nur eins
			$sql_insert_array = array(
				'exports_id'=>$id,
				'ships_id'=>$_POST['ships_id'],
				'eta'=>sprintf('%s %s:%s:00',$_POST['eta'],$_POST['eta_hour'],$_POST['eta_minute']),
				'ets'=>sprintf('%s %s:%s:00',$_POST['ets'],$_POST['ets_hour'],$_POST['ets_minute']),
				'invoice_recipient_id'=>$_POST['invoice_recipient_id'],
				'delivery_conditions'=>$_POST['delivery_conditions'],
				'contact_id'=>$_POST['contact_id']
			);
			$address_sql_array = $exports->get_companydata_from_db($_POST['invoice_recipient_id'],'invoice_recipient');
			$sql_insert_array = array_merge($sql_insert_array,$address_sql_array);
			xtc_db_perform(TABLE_EXPORT_TARGETS,$sql_insert_array);
			$etID = $conn->Insert_ID();
			
			// export_target_collis
			/** INAKTIV
			for($i=0;$i<sizeOf($_POST['width']);$i++){
				$sql_insert_array = array(
					'export_targets_id'=>$etID,
					'width'=>$_POST['width'][$i],
					'height'=>$_POST['height'][$i],
					'depth'=>$_POST['depth'][$i],
					'imports_id'=>$import_ship->data->imports_id
				);
				xtc_db_perform(TABLE_EXPORT_TARGET_COLLIES,$sql_insert_array);
			}
			*/
			// export_target_import_ships
			$import_ships_array = split('_',$_POST['import_ships_id']);
			foreach($import_ships_array as $k=>$import_ships_id){
				$sql_insert_array = array(
					'export_targets_id'=>$etID,
					'import_ships_id'=>$import_ships_id
				);
				xtc_db_perform(TABLE_EXPORT_TARGET_IMPORT_SHIPS,$sql_insert_array);
				$import_ships->update_status($import_ships_id,1);
				$import_ship = $import_ships->get_import_ship($import_ships_id);
				$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_EXPORTED,yes_get_ships_name($_POST['ships_id']),$_POST['export_number']),$import_ship->data->imports_id);
				$imports_id = $import_ship->data->imports_id;
			}
			// PRUEFEN OB IMPORT NOCH OFFENE SCHIFFE ENTHAELT
			if($imports_id > 0){
				$imports = new imports;
				$import = $imports->get_import($imports_id);
				if($imports->check_update_status($imports_id)){
					$imports->update_status($imports_id,1);
					$YES_HISTORY->add('imports',HISTORY_IMPORT_EXPORTED,$imports_id);
				}
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
