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

$exports = new exports();
switch($_GET['subaction']){
	case 'delete_collie':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$etID = xtc_db_input($_GET['etID']);
			$collie_id = xtc_db_input($_GET['etcID']);
			$export_targets = new export_targets;
			$export_target = $export_targets->get_export_target($etID);

			$export_targets->delete_collie( $etID,$collie_id );
			$ship_name = yes_get_ships_name($export_target->data->ships_id);
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_COLLIE_DELETED,$ship_name),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$id));
		break;
	case 'delete_target':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$etID = xtc_db_input($_GET['etID']);
			$imports = new imports;
			$exports = new exports;
			$export = $exports->get_export($id);
			$export_number = $export->data->export_number;
			$export_targets = new export_targets;
			$export_target = $export_targets->get_export_target($etID);

			$ship_name = yes_get_ships_name($export_target->data->ships_id);
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_COLLIE_DELETED,$ship_name),$id);

			$imports_to_check = array();
			for($j=0;$j<sizeOf($export_target->data->imports);$j++){
				$is = $export_target->data->imports[$j];
				$import_ships = new import_ships;
				$import_ships->update_status($is['import_ships_id'],0);
				$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_REMOVE_FROM_EXPORT,$ship_name,$export_number),$is['imports_id']);
				$imports_to_check[$is['imports_id']] = true;
			}
			
			// NUN NOCH CHECKEN OB DIE BETROFFENEN IMPORTE STATUS 1 HABEN - WENN JA, AUF NULL SETZEN
			if(sizeOf($imports_to_check)>0){
				foreach($imports_to_check as $imports_id=>$val){
					$import = $imports->get_import($imports_id);
					if($import->data->status == 1){
						$imports->update_status($imports_id,0);
					}
				}
			}
			$export_targets->delete( $etID );
			
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$id));
		break;
	case 'delete_import_ship':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$export = $exports->get_export($id);
			$etID = xtc_db_input($_GET['etID']);
			$export_target_import_ships_id = xtc_db_input($_GET['etiID']);
			$export_targets = new export_targets;
			$export_target = $export_targets->get_export_target($etID);
			
			$export_target_import_ships = new export_target_import_ships;
			$export_target_import_ship = $export_target_import_ships->get_export_target_import_ship($export_target_import_ships_id);
			$import_ships_id = $export_target_import_ship->data->import_ships_id;
			
			$ship_name = yes_get_ships_name($export_target->data->ships_id);
			$import_number = yes_get_import_number_from_import_ships_id($import_ships_id);
			$export_target_import_ships->delete( $export_target_import_ships_id );
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_IMPORT_SHIP_DELETED,$import_number,$ship_name),$id);

			$import_ships = new import_ships;
			$import_ship = $import_ships->get_import_ship($import_ships_id);
			$imports_id = $import_ship->data->imports_id;
			
			
			// STATUS IMPORT SETZEN
			$imports = new imports;
			$imports->update_status($imports_id,0);
			
			// STATUS IMPORT_SHIP SETZEN
			$import_ships = new import_ships;
			$import_ships->update_status($import_ships_id,0);
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_REMOVE_FROM_EXPORT,$ship_name,$export->data->export_number),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$id));
		break;
	case 'delete_costs':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$costs_id = xtc_db_input($_GET['costs_id']);
			$export = $exports->get_export($id);
			$costs = $exports->get_costs($id);
			foreach($costs as $k=>$v){
				if($v['export_costs_id'] == $costs_id){
					$costs_text = $v['costs_text'];
				}
			}
			$exports->delete_costs( $id,$costs_id );
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_COSTS_DELETED,$costs_text),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$_GET['id']));
		break;
}

switch($_GET['action']){
	case 'overview':
		include('exports_overview.php');
		exit;
		break;
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$export = $exports->get_export($id);
			$imports = new imports;
			// UPDATE DIE ENTHALTENEN IMPORT-SCHIFFE
			$import_ships = new import_ships;
			$export_targets = new export_targets;
			$imports_to_check = array();
			for($i=0;$i<sizeOf($export->data->targets);$i++){
				$et = $export->data->targets[$i];
				for($j=0;$j<sizeOf($et['imports']);$j++){
					$is = $et['imports'][$j];
					$import_ships = new import_ships;
					$import_ships->update_status($is['import_ships_id'],0);
					$ship_name = yes_get_ships_name($et['ships_id']);
					$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_REMOVE_FROM_EXPORT,$ship_name,$export->data->export_number),$is['imports_id']);
					$imports_to_check[$is['imports_id']] = true;
				}
			}
			
			// NUN NOCH CHECKEN OB DIE BETROFFENEN IMPORTE STATUS 1 HABEN - WENN JA, AUF NULL SETZEN
			if(sizeOf($imports_to_check)>0){
				foreach($imports_to_check as $imports_id=>$val){
					$import = $imports->get_import($imports_id);
					if($import->data->status == 1){
						$imports->update_status($imports_id,0);
					}
				}
			}
			$exports->delete( $id );
			
			$YES_HISTORY->add('system',sprintf(HISTORY_EXPORT_DELETED,$export->data->export_number));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_EXPORTS));
		break;
}

$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';


include(DIR_WS_CLASSES.'class.adodb-pager.php');

$pager = new ADODB_Pager($conn,sprintf("SELECT exports_id,recipient_name,agent_name,transport_types_id,date_added FROM %s",$exports->get_tablename() ));
$pager->index_field = $exports->get_index();
$pager->where_filter = array('status',IMAGE_STATUS,array(0=>array('id'=>'0','text'=>yes_get_exports_status_name(0)),1=>array('id'=>'1','text'=>yes_get_exports_status_name(1))),$default=0);

$pager->get_row_selector = 'eID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_RECIPIENT,TABLE_HEADING_AGENT,TABLE_HEADING_TRANSPORT_TYPE,TABLE_HEADING_ADDED);
$pager->gridHelper = array(
	'transport_types_id'=>'yes_get_transport_type_name'
);
$pager->sortOrders = array(
	'exports_id'=>TABLE_HEADING_ID,
	'recipient_name'=>TABLE_HEADING_RECIPIENT,
	'agent_name'=>TABLE_HEADING_AGENT,
	'date_added'=>TABLE_HEADING_ADDED
);

include(DIR_WS_INCLUDES.'header.php');

$pager->Render($rows_per_page=MAX_DISPLAY_SEARCH_RESULTS_ADMIN);

$selected_item = $pager->selected_row;

$grid_smarty = $smarty;
$smarty->assign('pager_tab_file',$pager->pager_tab_file);
$smarty->assign('HEADER',$pager->getHeader());
$smarty->assign('SORTING',$pager->getSortingInput());

$grid_smarty->assign('grid',$pager->getGrid());
$grid = $grid_smarty->fetch(DIR_FS_ADMIN.'templates/pager_grid.tpl');

$smarty->assign('GRID',$grid);
$smarty->assign('FOOTER',$pager->getFooter());
echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');

echo '<br />'.yes_draw_href_button(BUTTON_NEW_EXPORT,xtc_href_link(FILENAME_EXPORTS_EDIT),'exports_edit');

include('includes/footer.php');
