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

$invoices = new invoices();
switch($_GET['subaction']){
	case 'delete_import_cost':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$invoice_import_costs_id = xtc_db_input($_GET['iicID']);
			$invoice_import_costs = new invoice_import_costs;
			$invoice_import_cost = $invoice_import_costs->get_invoice_import_cost($invoice_import_costs_id);
			$invoices->delete_import_costs($id,$invoice_import_costs_id);
						
			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_IMPORT_COST_DELETED,$invoice_import_cost->data->costs_text),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES,'action=overview&id='.$id));
		break;
	case 'delete_import_ship_cost':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$invoice_import_ship_costs_id = xtc_db_input($_GET['iiscID']);
			$invoice_import_ship_costs = new invoice_import_ship_costs;
			$invoice_import_ship_cost = $invoice_import_ship_costs->get_invoice_import_ship_cost($invoice_import_ship_costs_id);
			$invoices->delete_import_ship_costs($id,$invoice_import_ship_costs_id);

			$import_ship_costs = new import_ship_costs;
			$import_ship_cost = $import_ship_costs->get_import_ship_cost($invoice_import_ship_cost->data->import_ship_costs_id);
			$ships_id = $import_ship_cost->get_ships_id();
			$ship_name = yes_get_ships_name($ships_id); 

			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_IMPORT_SHIP_COST_DELETED,$invoice_import_ship_cost->data->costs_text,$ship_name),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES,'action=overview&id='.$id));
		break;
	case 'delete_entry':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$invoice_entries_id = xtc_db_input($_GET['entries_id']);
			$invoice_entries = new invoice_entries;
			$invoice_entry = $invoice_entries->get_invoice_entry($invoice_entries_id);
			$invoices->delete_entries($id,$invoice_entries_id);
						
			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_ENTRY_DELETED,sprintf($invoice_entry->data->entry_text,0,50)),$id);
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES,'action=overview&id='.$id));
		break;
}
switch($_GET['action']){
	case 'overview':
		include(FILENAME_INVOICES_OVERVIEW);
		exit;
		break;
	case 'delete':
		if(xtc_not_null($_GET['id'])){
			$id = xtc_db_input($_GET['id']);
			$invoice = $invoices->get_invoice($id);
			$invoices->delete( $id );

			$YES_HISTORY->add('system',sprintf(HISTORY_INVOICE_DELETED,$invoice->data->invoice_number,$invoice->data->imports_id));
			$messageStack->add_session(MESSAGE_SUCCESS_DELETE,'success');
		}else{
			$messageStack->add_session(MESSAGE_ERROR_DELETE,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES));
		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 invoices_id,invoice_number,recipient_name,recipient_city,recipient_countries_id,date_added FROM %s",$invoices->get_tablename() ));
$pager->index_field = $invoices->get_index();

$pager->get_row_selector = 'iID';
$pager->gridHeader = array(TABLE_HEADING_ID,TABLE_HEADING_NUMBER,TABLE_HEADING_COMPANY,TABLE_HEADING_CITY,TABLE_HEADING_COUNTRY,TABLE_HEADING_ADDED);
$pager->gridHelper = array(
	'recipient_countries_id'=>'xtc_get_country_name'
);
$pager->sortOrders = array(
	'invoices_id'=>TABLE_HEADING_ID,
	'invoice_number'=>TABLE_HEADING_NUMBER,
	'name'=>TABLE_HEADING_COMPANY,
'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 />';
//echo '<br />'.yes_draw_href_button(BUTTON_NEW_INVOICE,xtc_href_link(FILENAME_INVOICES_EDIT),'invoices_edit');

include('includes/footer.php');
