#include <infocon.style>
#include "account.inc"

<future>
<page func=InfoCon title="Kontoführung">
<calendar_init -5>

<?
  define('LOOKUP_PERIOD', '2 years');
  $table = "account";
  $value = "value_eur";
  $add = '';
  $hidden = '';
  if ((strlen ($_GET['cur']) > 0) && ($_GET['cur'] == "dm")) {
    $table = "account_dm";
    $value = "value_dm";
    $hidden = '<input type=hidden name=currency value="dm">';
    $add = "&currency=dm";
  }

  if (!isset($_GET['blzkto']) && isset($_GET['id'])) {
    if ($table == 'account')
      $accounting = new Accounting($_GET['id']);
    else
      $accounting = new AccountingDM($_GET['id']);
    $row = $accounting->fetch();

    if ($row) {
      $date = explode (" ", $row->datum);
      $date = explode ("-", $date[0]);
      $date = sprintf ("%d.%d.%d", $date[2], $date[1], $date[0]);
      $blzkto = $row->blz_kto;
      $add .= '&blzkto='. $blzkto;
    }
  } else {
    $accounting = new Accounting(false);
    $blzkto = $_GET['blzkto'];
  }
  $account = new AccountName($blzkto);
?>

<style type="text/css">
label {
  display: block;
  font-weight: bold;
  font-size: 12px;
  width: 160px;
  text-align: left;
  padding-left: 5px;
  padding-top: 0px;
  padding-bottom: 0px;
  background-color: #1aa2fe;
  color: white;
}
input {
  width: 400px;
  margin-bottom: 10px;
  border: solid #1aa2fe;
  border-width: 1px;
}
select {
  display: block;
  width: 400px;
  margin-bottom: 10px;
  border: solid #1aa2fe;
  border-width: 1px;
}
textarea {
  width: 400px;
  border: 1px solid #1aa2fe;
  border-width: 1px;
  margin-top: -1px;
}
</style>

<h3 class=bar><?=$account->fetch()->name;?></h3>

<form method=post action="update.php">
<input class=none type="hidden" name="id" value="<? echo $_GET[id]; ?>">
<?
  echo ("<input class=\"none\" type=\"hidden\" name=\"blzkto\" value=\"$blzkto\">");
  echo $hidden;
?>

<label for=statement>Auszug</label>
<input id=statement name=statement size=15 maxlength=15 value="<?=$row->statement?>" tabindex=1>

<br class="none">
<label for=datum>Datum</label>
<input id=datum name=datum size=15 maxlength=10 value="<?=$date?>" tabindex=2>
<calendar "datum">

<br class="none">
<label for=category>Kategorie</label>
<select id=category name=category tabindex=3>
<option>
<?
  foreach ($accounting->distinctCategories($blzkto) as $cat)
    printf ("<option value=\"%s\"%s>%s", $cat->category, $cat->category == $row->category?" selected":"", $cat->category);

?></select> <input id=category name=newcategory size=20 maxlength=50 value="" tabindex=4>

<br class="none">
<label for=from_to>Abs./Empf.</label>
<select id=from_to name=from_to tabindex=5>
<option>
<?
  $condition = isset($_GET['id']) ? false : sprintf("datum >= now() - interval '%s'", LOOKUP_PERIOD);
  foreach ($accounting->distinctFromTo($blzkto, $condition) as $name)
    printf ("<option value=\"%s\"%s>%s", $name->from_to, $name->from_to == $row->from_to?" selected":"", $name->from_to);

?></select> <input id=from_to name=newfrom_to size=30 maxlength=120 value="" tabindex=6>

<br class="none">
<label for=value>Betrag</label>
<input id=value name=value size=40 maxlength=20 value="<? echo $row->$value; ?>" tabindex=7>

<br class="none">
<label for=descr>Verwendungszweck</label>
<textarea id=descr name=descr rows=5 cols=67 tabindex=8><? echo $row->descr; ?></textarea>

<p><center>
<? if (isset ($_GET[id])) { ?>
<input class=button type=submit value="Update" tabindex=9>
&nbsp;&nbsp;&nbsp;
<input class=button type=submit value="Delete" name=delete tabindex=9>
<? } else { ?>
<input class=button type=submit value="Insert" tabindex=9>
<? } ?>
</center><p>
</form>

<h3 class=bar>Hinweise zur Bearbeitung</h3>

<ul>

<li> Das Datum wird in der Form dd.mm.yyyy erwartet

<li> Wenn die Kategorie bzw. Absender/Empfänger noch nicht existieren,
     werden diese manuell im Textfeld eingegeben

<li> Betrag als 999.44 angeben.

<li> Die erste Zeile des Verwendungszwecks wird in der Übersicht angezeigt

</ul>

<protect><script type="text/javascript">
$(function(){
    $('#statement').focus();

    var from_to_values = {};
    $.invoke('Accounting/GetCategoryInfo', 'blzkto='+$('input[name="blzkto"]').val(), function(data){
	from_to_values = data.list;
    });

    $('<textarea id="descriptions" rows="10" cols="60" readonly />').insertBefore('h3.bar:last');
    $('ul').append($('<li> Die Taste <b>r</b> im Feld Betrag springt zur Kategorie zurück</li>'));
    if ($('input.button[value="Insert"]').length) {
	$('select#category').blur(function(e){
	    if ($(e.target).val().length) {
		$('input[name="newcategory"]').val('');
		$('select#from_to').focus();

		update_select('select#from_to', from_to_values[$('select#category').val()]);
	    } else {
		$('input[name="newcategory"]').focus();
	    }
	});
	$('select#from_to').blur(function(e){
	    if ($(e.target).val().length) {
		$('input[name="newfrom_to"]').val('');
		$('input#value').focus();

		if ($('select#category').val().length) {
		    var parms = {};
		    parms['blzkto'] = $('input[name="blzkto"]').val();
		    parms['category'] = $('select#category').val();
		    parms['from_to'] = $('select#from_to').val();
		    $.invoke('Accounting/GetItems', parms, function(data){
			if (data.use_value)
			    $('input#value').val(data.value);
			else
			    $('input#value').select().focus();
			$('textarea#descriptions').val('');
			for (var i=0; i < data.list.length; i++)
			    $('textarea#descriptions').val($('textarea#descriptions').val() + data.list[i] + '\n');
			if (data.prefix.length)
			    $('textarea#descr').val(data.prefix);
			$('input#value').select().focus();
		    });
		}
	    } else {
		$('input[name="newfrom_to"]').focus();
	    }
	});
	$('input#value').keypress(function(e){
	    if (e.key == 'r')
		return false;
	});
	$('input#value').keyup(function(e){
	    if (e.key == 'r')
		$('select#category').focus();
	});
	$('input.button[value="Insert"]').click(function(e){
	    if (!$('select#category').val().length && !$('input[name="newcategory"]').val().length) {
		show_error('Keine Kategorie ausgewählt', 3);
		return false;
	    }
	    if (!$('select#from_to').val().length && !$('input[name="newfrom_to"]').val().length) {
		show_error('Kein Absender/Empfänger ausgewählt', 3);
		return false;
	    }
	    if (!$('input#value').val().length) {
		show_error('Kein Betrag angegeben', 3);
		return false;
	    }
	    if (!$('textarea#descr').val().length) {
		show_error('Kein Verwendungszweck angegeben', 3);
		return false;
	    }

	    show_message('Buchung wird gespeichert.', 10);
	    $.invoke('Accounting/Insert', $('form').serialize(), function(data){
		hide_message();
		$('input#datum').selectRange(0,$('input#datum').val().lastIndexOf('.')).focus();
	    });

	    return false;
	});
    }
});
</script></protect>
</page>

# Local variables:
# mode: text
# mode: auto-fill
# end:
