<?php
include('includes/application_top.php');
require_once(DIR_WS_CLASSES.'invoices.php');
require_once(DIR_WS_CLASSES.'imports.php');
require_once(DIR_WS_CLASSES.'class.dbform.php');
$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';

$invoices = new invoices;

$currencies = new currencies;

switch($_GET['action']){
	case 'add_import_costs_save':
		if(is_array($_POST['import_costs_id']) and sizeOf($_POST['import_costs_id'])>0){
			$id = xtc_db_input($_GET['id']);
			foreach($_POST['import_costs_id'] as $k=>$import_costs_id){
				$costs_text = $invoices->add_import_costs($id,$import_costs_id);
				if($costs_text != ''){
					$msg = sprintf(HISTORY_INVOICE_IMPORT_COST_ADDED,$costs_text);
					$YES_HISTORY->add('invoices',$msg,$id);
				}
			}
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
		}else{
			$messageStack->add_session(ERROR_NO_IMPORT_COSTS,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$_GET['id'].'&action=overview'));
		exit;
		break;
	case 'add_import_ship_costs_save':
		if(is_array($_POST['import_ship_costs_id']) and sizeOf($_POST['import_ship_costs_id'])>0){
			$id = xtc_db_input($_GET['id']);
			$import_ship_costs = new import_ship_costs;
			foreach($_POST['import_ship_costs_id'] as $k=>$import_ship_costs_id){
				$import_ship_cost = $import_ship_costs->get_import_ship_cost($import_ship_costs_id);
				$ships_id = $import_ship_cost->get_ships_id();
				$ship_name = yes_get_ships_name($ships_id); 
				$costs_text = $invoices->add_import_ship_costs($id,$import_ship_costs_id);
				if($costs_text != ''){
					$msg = sprintf(HISTORY_INVOICE_IMPORT_SHIP_COST_ADDED,$costs_text,$ship_name);
					$YES_HISTORY->add('invoices',$msg,$id);
				}
			}
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
		}else{
			$messageStack->add_session(ERROR_NO_IMPORT_SHIP_COSTS,'error');
		}
		xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$_GET['id'].'&action=overview'));
		exit;
		break;
	case 'add_save':
		if(xtc_not_null($_GET['iID'])){
			$imports = new imports;
			$import = $imports->get_import($_GET['iID']);
			$import_ships_id = (int)xtc_db_input($_POST['import_ships_id']);
			$import_ships = new import_ships();
			$import_ship = $import_ships->get_import_ship($import_ships_id);
			$invoice_company_id = $import_ship->data->orderer_id;
			$baptismal_name_array = array();
			$baptismal_name = yes_get_baptismal_ship_name( $import_ship->data->orderer_ships_id );
			if($baptismal_name != ''){
				$baptismal_name_array = array('baptismal_name'=>$baptismal_name);
			}
			$address_data = yes_get_company_address_data($invoice_company_id,$baptismal_name_array);
			$invoice_number = $invoices->get_next_invoice_number($source='invoice');
			$import_number = $import->data->import_number;

			$default_currencies_id = $currencies->get_currencies_id(DEFAULT_CURRENCY);
			$currency = $currencies->get_currency($default_currencies_id);

			$sql_insert_array = array(
				'imports_id'=>$_GET['iID'],
				'invoice_number'=>$invoice_number,
				'import_ships_id'=>$import_ships_id,
				'date_added'=>'now()',
				'status_id'=>0,
				'recipient_id'=>$address_data['companies_id'],
				'recipient_name'=>$address_data['name'],
				'recipient_street_address'=>$address_data['street_address'],
				'recipient_suburb'=>$address_data['suburb'],
				'recipient_postcode'=>$address_data['postcode'],
				'recipient_city'=>$address_data['city'],
				'recipient_countries_id'=>$address_data['countries_id'],
				'recipient_vat_id'=>$address_data['vat_id'],
				'baptismal_name'=>$address_data['baptismal_name'],
				'currency'=>DEFAULT_CURRENCY,
				'currency_value'=>$currency->data->value
			);
			xtc_db_perform(TABLE_INVOICES,$sql_insert_array);
			$id = $conn->Insert_ID();

			for($i=0;$i<sizeOf($import_ship->data->costs);$i++){
				$sql_insert_array = array(
					'invoices_id'=>$id,
					'import_ship_costs_id'=>$import_ship->data->costs[$i]['import_ship_costs_id'],
					'costs_text'=>$import_ship->data->costs[$i]['costs_text'],
					'costs'=>$import_ship->data->costs[$i]['costs'],
					'date_added'=>'now()',
				);
				xtc_db_perform(TABLE_INVOICE_IMPORT_SHIP_COSTS,$sql_insert_array);
			}
			
			$msg = sprintf(HISTORY_INVOICE_ADDED,$invoice_number, $import_number);
			$YES_HISTORY->add('invoices',$msg,$id);
			$messageStack->add_session($msg,'success');
			xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$id.'&action=overview'));
		}else{
			$messageStack->add_session(ERROR_NO_IMPORTS_ID,'error');
			xtc_redirect(xtc_href_link(FILENAME_INVOICES));
		}
		break;
	case 'add':
		include(FILENAME_INVOICES_CHOOSE_ADDRESS);
		exit;
	case 'add_import_costs':
		include(FILENAME_INVOICES_CHOOSE_IMPORT_COSTS);
		exit;
		break;
	case 'add_import_ship_costs':
		include(FILENAME_INVOICES_CHOOSE_IMPORT_SHIP_COSTS);
		exit;
		break;
}


$dbf = new dbform('invoices','invoices_id',$conn);

$dbf->add_hidden_field('imports_id',$iID,$extern_table = false);
// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$invoice = $invoices->get_invoice($dbf->select_id);
	$import_number = yes_get_import_number_from_imports_id($invoice->data->imports_id);
	$companies = array(0=>array('id'=>$invoice->data->recipient_id,'text'=>yes_get_companies_name($invoice->data->recipient_id)));
}else{
	// add haben wir hier nicht
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
	$companies = array(0=>array('id'=>'','text'=>TEXT_SELECT));
	if(xtc_not_null($_GET['cID'])){
		$companies_id = (int)xtc_db_input($_GET['cID']);
		$companies = array(0=>array('id'=>$companies_id,'text'=>yes_get_companies_name($companies_id)));
	}
}

$dbf->fields['recipient_id']->set_title(ENTRY_INVOICE_RECIPENT_COMPANY,0);
$dbf->fields['recipient_id']->set_options($companies,'');
$dbf->fields['recipient_id']->set_ajax_function('getCompaniesByLetters');


$dbf->fields['currency']->set_title(ENTRY_CURRENCY,2);
$dbf->fields['currency']->set_options(yes_get_currencies(),'');


// 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_INVOICES,'id='.$dbf->select_id.'&action=overview'),$access='invoices');
$smarty->assign('PAGE_MENU',$menu);

if(xtc_not_null($companies_id)){
	$heading_variable = 'HEADING_TITLE_COMPANYS';
	$heading_title_param = array('company'=>yes_get_companies_name($companies_id));
}else{
	$heading_title_param = array('invoice_number'=>$invoice->data->invoice_number,'ship_name'=>yes_get_ships_name($invoice->data->import_ships_id));
}

$result = $dbf->get_form($form_name = 'invoices',$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);
			
			// checken ob currency geaendert wurde
			$selected_currency = $currencies->get_currency($_POST['currency']);
			$sql_update_array['currency'] = $selected_currency->data->code; 
			if($invoice->data->currency != $sql_update_array['currency']){
				$sql_update_array['currency_value'] = $selected_currency->data->value;				
			}

			if($invoice->data->recipient_id != $_POST['recipient_id']){
				$address_sql_array = $invoices->get_companydata_from_db($_POST['recipient_id'],'recipient');
				$sql_update_array = xtc_array_merge($sql_update_array,$address_sql_array);
			}
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_UPDATED,$import_number),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$dbf->select_id.'&action=overview'));
						break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['date_added'] = 'now()';

			$address_data = yes_get_company_address_data($_POST['recipient_id']);
			$invoice_number = $invoices->get_next_invoice_number($source='invoice');
			$currency = $currencies->get_currency($_POST['currency']);

			$sql_insert_array['invoice_number'] = $invoice_number;
			$sql_insert_array['status_id'] = 0;
			$sql_insert_array['recipient_name'] = $address_data['name'];
			$sql_insert_array['recipient_street_address'] = $address_data['street_address'];
			$sql_insert_array['recipient_suburb'] = $address_data['suburb'];
			$sql_insert_array['recipient_postcode'] = $address_data['postcode'];
			$sql_insert_array['recipient_city'] = $address_data['city'];
			$sql_insert_array['recipient_countries_id'] = $address_data['countries_id'];
			$sql_insert_array['recipient_vat_id'] = $address_data['vat_id'];
			$sql_insert_array['currency'] = $currency->data->code;
			$sql_insert_array['currency_value'] = $currency->data->value;

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_COMPANY_ADDED,yes_get_companies_name($_POST['recipient_id'])),$id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_INVOICES,'id='.$id.'&action=overview'));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
