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

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

$dbf = new dbform('ships','ships_id',$conn);
// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$heading_title_param = yes_get_ships_name($dbf->select_id);
	$ships = new ships();
	$ship = $ships->get_ship($dbf->select_id);
	$baptismal_name_in_invoice_address = $ship->data->baptismal_name_in_invoice_address;

}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
	$baptismal_name_in_invoice_address = 0;
}

if($mode == 'add'){
	$dbf->add_field('name','varchar(128)',$extern_table=true);
	$dbf->fields['name']->set_title(ENTRY_NAME,0);
	$dbf->fields['name']->set_required($min_length=3,$validate_func = '');
}

$dbf->add_field('ship_to_company','int(11)',$extern_table = true);
$dbf->fields['ship_to_company']->set_title(ENTRY_OWNER,2);

$default_ship_to_company = ($_REQUEST['companies_id']) ? xtc_db_input($_REQUEST['companies_id']) : '';
$default = ($dbf->select_id) ? yes_get_ship_company_id($dbf->select_id) : $default_ship_to_company;
$default_array = array(0=>array('id'=>'','text'=>TEXT_NONE));
if($default != ''){
	$default_array = array(0=>array('id'=>$default,'text'=>yes_get_companies_name($default) ));
}

$dbf->fields['ship_to_company']->set_options( $default_array, $default );
$dbf->fields['ship_to_company']->set_ajax_function('getCompaniesByLetters');


$default_ship_to_invoice_company = ($_REQUEST['companies_id']) ? xtc_db_input($_REQUEST['companies_id']) : '';
$default = ($dbf->select_id) ? yes_get_ship_invoice_company_id($dbf->select_id) : $default_ship_to_invoice_company;
$default_array = array(0=>array('id'=>'','text'=>TEXT_NONE));
if($default != ''){
	$default_array = array(0=>array('id'=>$default,'text'=>yes_get_companies_name($default) ));
}
$dbf->add_field('ship_to_invoice_company','int(11)',$extern_table = true);
$dbf->fields['ship_to_invoice_company']->set_title(ENTRY_INVOICE_OWNER,4);
$dbf->fields['ship_to_invoice_company']->set_options( $default_array, $default );
$dbf->fields['ship_to_invoice_company']->set_ajax_function('getCompaniesByLetters');
$dbf->fields['invoice_address']->set_title(TEXT_INVOICE_ADDRESS,5);

$default_array = array(0=>array('id'=>'0','text'=>NO),1=>array('id'=>'1','text'=>YES));
$dbf->fields['baptismal_name_in_invoice_address']->set_title(TEXT_BAPTISMAL_NAME_IN_INVOICE_ADDRESS,6);
$dbf->fields['baptismal_name_in_invoice_address']->set_options( $default_array, $baptismal_name_in_invoice_address );


$dbf->fields['cost_location']->set_title(ENTRY_COST_LOCATION,7);

$dbf->fields['comments']->set_title(COMMENTS,8);


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

$result = $dbf->get_form($form_name = 'ships',$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['date_added']);
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('ships',sprintf(HISTORY_SHIP_UPDATED,$_POST['name']),$dbf->select_id);
			
			$conn->Execute(sprintf("DELETE FROM %s WHERE ships_id='%s'",TABLE_SHIPS_TO_COMPANIES,$dbf->select_id));
			if($_POST['ship_to_company']>0){
				$insert_sql_array = array('ships_id'=>$dbf->select_id,'companies_id'=>$_POST['ship_to_company'],'role'=>'owner');
				$result = $conn->AutoExecute(TABLE_SHIPS_TO_COMPANIES,$insert_sql_array,'INSERT');
			}
			
			if($_POST['ship_to_invoice_company']>0){
				$insert_sql_array = array('ships_id'=>$dbf->select_id,'companies_id'=>$_POST['ship_to_invoice_company'],'role'=>'invoice');
				$result = $conn->AutoExecute(TABLE_SHIPS_TO_COMPANIES,$insert_sql_array,'INSERT');
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_SHIPS));
			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('ships',sprintf(HISTORY_SHIP_ADDED,$_POST['name']),$id);
			
			$insert_sql_array = array(
				'name'=>$_POST['name'],
				'ships_id'=>$id,
				'is_default'=>1,
				'date_added'=>'now()'
			);
			xtc_db_perform(TABLE_SHIP_NAMES,$insert_sql_array);
			
			if($_POST['ship_to_company']>0){
				$insert_sql_array = array('ships_id'=>$id,'companies_id'=>$_POST['ship_to_company'],'role'=>'owner');
				xtc_db_perform(TABLE_SHIPS_TO_COMPANIES,$insert_sql_array);
			}
			
			if($_POST['ship_to_invoice_company']>0){
				$insert_sql_array = array('ships_id'=>$id,'companies_id'=>$_POST['ship_to_invoice_company'],'role'=>'invoice');
				xtc_db_perform(TABLE_SHIPS_TO_COMPANIES,$insert_sql_array);
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_SHIPS));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
