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

function _get_imports_id($id){
	return $id;
}

$imports = new imports();
switch($_GET['subaction']){
	case 'delete_ship':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$import_ships_id = xtc_db_input($_GET['isID']);
			$import_ships = new import_ships;
			$collies = $import_ships->get_collies($import_ships_id);
			foreach($collies as $k=>$v){
				$import_ships->delete_collie( $import_ships_id,$v['import_ship_collies_id'] );
			}

			$import_ship = $import_ships->get_import_ship($import_ships_id);
			$ships_id = $import_ship->data->orderer_ships_id;
			$import_ships->delete($import_ships_id);
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_DELETED,yes_get_ships_name($ships_id)),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$id));
		break;
	case 'delete_ship_collie':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$import_ships_id = xtc_db_input($_GET['isID']);
			$import_ship_collies_id = xtc_db_input($_GET['iscID']);
			$import_ships = new import_ships;
			$collies = $import_ships->get_collies($import_ships_id);
			foreach($collies as $k=>$v){
				if($v['import_ship_collies_id'] == $import_ship_collies_id){
					$collie_name = $v['collie_content'];
				}
			}
			$import_ships->delete_collie( $import_ships_id,$import_ship_collies_id );

			$import_ship = $import_ships->get_import_ship($import_ships_id);
			$ships_id = $import_ship->data->orderer_ships_id;
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_COLLIE_DELETED,$collie_name,yes_get_ships_name($ships_id)),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$id));
		break;
	case 'delete_ship_cost':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$import_ships_id = xtc_db_input($_GET['isID']);
			$import_ship_costs_id = xtc_db_input($_GET['iscID']);
			$import_ships = new import_ships;
			$costs = $import_ships->get_costs($import_ships_id);
			foreach($costs as $k=>$v){
				if($v['import_ship_costs_id'] == $import_ship_costs_id){
					$cost_name = $v['costs_text'];
				}
			}
			$import_ships->delete_costs( $import_ships_id,$import_ship_costs_id );
			
			$import_ship = $import_ships->get_import_ship($import_ships_id);
			$ships_id = $import_ship->data->orderer_ships_id; 
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_COST_DELETED,$cost_name,yes_get_ships_name($ships_id)),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'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']);
			$import = $imports->get_import($id);
			$costs = $imports->get_costs($id);
			foreach($costs as $k=>$v){
				if($v['import_costs_id'] == $costs_id){
					$costs_text = $v['costs_text'];
				}
			}
			$imports->delete_costs( $id,$costs_id );
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_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_IMPORTS,'action=overview&id='.$_GET['id']));
		break;
}

switch($_GET['action']){
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$import = $imports->get_import($id);
			$imports->delete( $id );
			$YES_HISTORY->add('system',sprintf(HISTORY_IMPORT_DELETED,$import->data->import_number));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_IMPORTS));
		break;
	case 'overview':
		include(FILENAME_IMPORTS_OVERVIEW);
		exit;
		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 imports_id,import_number,shipper_name,transport_agent_name,haulage_service_name,date_added FROM %s",$imports->get_tablename() ));
$pager->index_field = $imports->get_index();
$pager->where_filter = array('status',IMAGE_STATUS,array(0=>array('id'=>'0','text'=>yes_get_imports_status_name(0)),1=>array('id'=>'1','text'=>yes_get_imports_status_name(1))),$default=0);

$pager->get_row_selector = 'iID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_NUMBER,TABLE_HEADING_SHIPPER,TABLE_HEADING_TRANSPORT_AGENT,TABLE_HEADING_HAULAGE_SERVICE,TABLE_HEADING_SHIPPER_REFERENCE_NUMBER,TABLE_HEADING_ADDED);
$pager->sortOrders = array(
	'imports_id'=>TABLE_HEADING_ID,
	'import_code'=>TABLE_HEADING_NUMBER,
	'shipper_id'=>TABLE_HEADING_SHIPPER,
	'haulage_service_id'=>TABLE_HEADING_HAULAGE_SERVICE,
	'transport_agent_id'=>TABLE_HEADING_TRANSPORT_AGENT,
	'date_added'=>TABLE_HEADING_ADDED
);
$pager->gridHelper = array(
	'imports_id'=>'_get_imports_id',
	'date_added'=>'xtc_date_long'
);
$pager->searchFields = array(
	'import_number','shipper_reference_number'
);

$pager->orderbyDefault = array('imports_id DESC');

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());
$smarty->assign('WHERE_FILTER',$pager->getWhereFilter());
$smarty->assign('SEARCH',$pager->getSearchInput());

$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());
$smarty->assign('NEW_ENTRY_BUTTON',yes_draw_href_button(BUTTON_NEW_IMPORT,xtc_href_link(FILENAME_IMPORTS_EDIT),'imports_edit'));
echo $smarty->fetch(DIR_FS_ADMIN.'templates/pager.tpl');

include('includes/footer.php');
