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

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

$invoice_import_ship_costs = new invoice_import_ship_costs;

$dbf = new dbform('invoice_import_ship_costs','invoice_import_ship_costs_id',$conn);

$dbf->fields['costs_text']->set_title(ENTRY_COSTS_POSITION,0);
$dbf->fields['costs_text']->set_required($min_length=3,$validate_func = '');
$dbf->fields['costs']->set_title(ENTRY_COSTS,2);
$dbf->fields['costs']->set_required(1,'float');
$dbf->fields['costs']->set_comments(TEXT_CURRENCY_FIELD);

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$invoice_import_ship_cost = $invoice_import_ship_costs->get_invoice_import_ship_cost($dbf->select_id);
	$invoices_id = $invoice_import_ship_cost->data->invoices_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);
	$import_ship_cost_smarty_array = $import_ship_cost->get_smarty_data_array();
	$import_ship_cost_smarty_array['import_number'] = yes_get_import_number_from_imports_id($import_ship_cost->get_imports_id());
	$import_ship_cost_smarty_array['currency'] = $import_ship_cost->get_currency();
	$snippet_smarty = new yesSmarty;
	$snippet_smarty->assign('import_ship_cost_info',$import_ship_cost_smarty_array);
	$smarty->assign('info_snippet',$snippet_smarty->fetch(DIR_FS_ADMIN.'templates/info_snippet_import_ship_cost.html'));
}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_INVOICE_IMPORT_SHIP_COSTS),$access='invoice_import_ship_costs');
$smarty->assign('PAGE_MENU',$menu);

$result = $dbf->get_form($form_name = 'invoice_import_ship_costs',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);

if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$sql_update_array = $dbf->get_sql_array($_POST);
			unset($sql_update_array['costs']); // extra handling
			$check = $invoice_import_ship_cost->check_update_costs_value($import_ship_cost->data->costs,$_POST['costs']);
			if($check){
				xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");
				if($invoice_import_ship_cost->data->costs <> $_POST['costs']){
					$invoices = new invoices;
					$invoices->update_import_ship_costs($invoice_import_ship_cost->data->invoices_id,$invoice_import_ship_cost->data->import_ship_costs_id,(float)$_POST['costs']);
				}
				$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_IMPORT_SHIP_COST_UPDATED,$_POST['costs_text'],$invoice_import_ship_cost->data->ship_name),$invoices_id);
				$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
				xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$invoices_id.'&action=overview'));
			}else{
				$messageStack->add_session(sprintf(ERROR_COSTS_NOT_VALID,$_POST['costs']),'error');
				xtc_redirect(xtc_href_link(FILENAME_INVOICES_IMPORT_SHIP_COSTS_EDIT,'id='.$_GET['id']));
			}
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['date_added'] = 'now()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('invoice_import_ship_costs',sprintf(HISTORY_INVOICE_IMPORT_SHIP_COST_ADDED,$_POST['name']),$id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_INVOICE_IMPORT_SHIP_COSTS));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
