#include <infocon.style>

<future>
<page func=InfoCon title="Buchhaltung">

<style type="text/css">
span.in { color: green; }
span.out { color: red; }
span.in:hover, span.out:hover { background: yellow; border: 1px solid #ccc; }
</style>

<h3>Erstattungen / Offene Buchungen</h3>

<form id="open_items">
Titel <input type="text" id="title" name="title" value="" size="20" title="Optionaler Titel der Liste"><br>
<?php
    $sales = new Sales();
    foreach ($sales->getOpenItems(false) as $row) {
	$date = substr ($row->date,6,2) . "." . substr ($row->date,4,2) . "." . substr ($row->date,0,4);
	printf('<input name="nr[]" type="checkbox" value="%d" price="%d">&nbsp;', $row->nr, $row->price);
	printf('<span class="%s">%d&nbsp;&nbsp;%s %s (%.2f&nbsp;&euro;)</span><br>',
	       $row->price < 0 ? 'out' : 'in',
	       $row->nr, $date, $row->description, $row->price / 100);
    }
?>
Zwischensumme <input type="text" id="subtotal" value="" data-value="" size="5" style="background: #ececec;text-align:right;" readonly> &euro;
&nbsp;&nbsp;&nbsp;<input type="text" id="subtotal_tr" value="" size="5" style="background: #ececec;text-align:right;" readonly> &euro;
<p><input type="submit" class="button" value="Erstatten" onclick="return send_request()"> <input type="reset" class="button" value="Reset"><p>
</form>

</page>
<protect>
<script type="text/javascript">
function cb_change()
{
    var subtotal = $('input#subtotal').attr('data-value');
    if (typeof subtotal == 'string' && !subtotal.length) subtotal = 0;
    else subtotal = parseInt(subtotal, 10);

    if ($(this).prop('checked'))
	subtotal += parseInt($(this).attr('price'));
    else
	subtotal -= parseInt($(this).attr('price'));

    $('input#subtotal').attr('data-value', subtotal);
    if (subtotal == 0) {
	$('input#subtotal').val('');
	$('input#subtotal_tr').val('');
    } else {
	$('input#subtotal').val((subtotal / 100).toFixed(2));
	$('input#subtotal_tr').val((subtotal * -1 / 100).toFixed(2).replace('.', ','));
    }
}

function send_request()
{
    $.invoke('Sales/Subtotal', $('form#open_items').serialize(), function(data){
	show_message('Mail sent');
	$('form#open_items')[0].reset();
    });

    return false;
}

$(function(){
	$('form#open_items input[type="checkbox"]').click(cb_change);
	setTimeout(function(){$('form#open_items')[0].reset();}, 500);
});
</script>
</protect>
