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

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

$invoices = new invoices;

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

// 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));
}

$dbf->fields['recipient_name']->set_title(ENTRY_NAME,0);
$dbf->fields['recipient_name']->set_required(2);

$dbf->fields['recipient_street_address']->set_title(ENTRY_STREET_ADDRESS,10);
$dbf->fields['recipient_street_address']->set_required(2);

$dbf->fields['recipient_suburb']->set_title(ENTRY_SUBURB,20);

$dbf->fields['recipient_postcode']->set_title(ENTRY_POST_CODE,30);

$dbf->fields['recipient_city']->set_title(ENTRY_CITY,40);
$dbf->fields['recipient_city']->set_required(2);

$dbf->fields['recipient_countries_id']->set_title(ENTRY_COUNTRY,50);
$dbf->fields['recipient_countries_id']->set_options(xtc_get_countries(),'');

$dbf->fields['recipient_vat_id']->set_title(ENTRY_VAT_ID,60);

$dbf->fields['baptismal_name']->set_title(ENTRY_BAPTISMAL_NAME,70);


// 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);

$heading_title_param = $invoice->data->invoice_number;

$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);
			
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('invoices',HISTORY_INVOICE_RECIPIENT_UPDATED,$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()';

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('invoices',sprintf(HISTORY_INVOICE_ADDED,$import_number),$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');
