<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'awb_numbers.php');
require_once(DIR_WS_CLASSES.'airlines.php');

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

$aID = ($_GET['aID']) ? xtc_db_input($_GET['aID']) : xtc_db_input($_POST['airlines_id']); 
if( $aID == 0){
	$messageStack->add_session(MESSAGE_ERROR_NO_AIRLINE,'error');
	xtc_redirect(FILENAME_AIRLINES);
}
$airlines = new airlines;
$airline = $airlines->get_airline($aID);

$dbf = new dbform('awb_numbers','awb_numbers_id',$conn);

$dbf->add_field('number_from','varchar(7)',$extern_table=true);
$dbf->fields['number_from']->set_title(ENTRY_FROM,1);
$dbf->fields['number_from']->set_required($min_length=7,$validate_func = 'int');
$dbf->fields['number_from']->set_comments(ENTRY_NUMBER_CODE.': '.$airline->data->number_code.'/');

$dbf->add_field('number_from_check_digit','varchar(1)',$extern_table=true);
$dbf->fields['number_from_check_digit']->set_title(ENTRY_CHECK_DIGIT,2);
$dbf->fields['number_from_check_digit']->set_required($min_length=1,$validate_func = 'int');
$dbf->fields['number_from_check_digit']->set_style('width:25px;');

$dbf->add_field('number_to','varchar(7)',$extern_table=true);
$dbf->fields['number_to']->set_title(ENTRY_TO,4);
$dbf->fields['number_to']->set_required($min_length=7,$validate_func = 'int');
$dbf->fields['number_to']->set_comments(ENTRY_NUMBER_CODE.': '.$airline->data->number_code.'/');

$dbf->add_field('number_to_check_digit','varchar(1)',$extern_table=true);
$dbf->fields['number_to_check_digit']->set_title(ENTRY_CHECK_DIGIT,5);
$dbf->fields['number_to_check_digit']->set_required($min_length=1,$validate_func = 'int');
$dbf->fields['number_to_check_digit']->set_style('width:25px;');

$dbf->fields['date_receipt']->set_title(ENTRY_DATE_RECEIPT,6);
$dbf->fields['date_receipt']->set_default_value(date('Y-m-d H:i:s'));

$dbf->add_hidden_field('airlines_id',$aID,$extern_table=false);

$heading_title_param = $airline->data->name;

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

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
}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_AWB_NUMBERS),$access='awb_numbers');
$smarty->assign('PAGE_MENU',$menu);

$result = $dbf->get_form($form_name = 'awb_numbers',$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('awb_numbers',sprintf(HISTORY_AWB_NUMBER_UPDATED,$_POST['name']),$dbf->select_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_AWB_NUMBERS));
			break;
		case 'add':
			if(
				($_POST['number_from'] % 7 == $_POST['number_from_check_digit']) &&
				($_POST['number_to'] % 7 == $_POST['number_to_check_digit'])
			){
				$existing = 0;
				$saved = 0;
				for($i=$_POST['number_from'];$i<=$_POST['number_to'];$i++){
					$check_query = xtc_db_query(sprintf("SELECT awb_number FROM %s WHERE airlines_id='%s' AND awb_number='%s'",TABLE_AWB_NUMBERS,$aID,$i));
					if(!xtc_db_num_rows($check_query)){
						$insert_sql_array = array(
							'airlines_id'=>$aID,
							'awb_number'=>$i,
							'awb_number_check_digit'=>($i%7),
							'date_receipt'=>sprintf('%s %s:%s:00',$_POST['date_receipt'],$_POST['date_receipt_hour'],$_POST['date_receipt_minute'])
						);
						xtc_db_perform($dbf->table,$insert_sql_array);
						$saved++;
					}else{
						$existing++;
					}
				}
				$YES_HISTORY->add('airlines',sprintf(HISTORY_AWB_NUMBER_ADDED,$_POST['number_from'],$_POST['number_to']),$aID);
				$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
				xtc_redirect(xtc_href_link(FILENAME_AWB_NUMBERS,'aID='.$aID));
			}else{
				$messageStack->add_session(MESSAGE_ERROR_AWB_CHECK,'error');
				xtc_redirect(xtc_href_link(FILENAME_AWB_NUMBERS_EDIT,'aID='.$aID));
			}
			exit;

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

include('includes/footer.php');
