<br />
<div style="background:#eeeeee;padding:10px;">

<table border="0" cellpadding="2" cellspacing="1" style="border:1px solid #aaaaaa;background:#cccccc;width:930px;">
<form id="retail_sale_new_item" method="post" action="">
	<tr>
		<td style="text-align:left;background:#ff9900;width:300px;text-align:left;">
			<label>Artikel</label><br />
			<input type="hidden" id="item_search_id" value="0" /><input type="text" id="item_search" style="width:250px;" autocomplete="off" />
			<div id="item_search_results" class="search_results hidden" style="position:absolute;width:280px;"></div>

			<span id="item_search_hits"></span>

		</td>
		<td style="text-align:left;background:#ff9900;"><label>St&uuml;ck</label><br /><input style="width:50px;" type="text" id="new_item_amount" name="new_item_amount" value="" /></td>
		<td style="text-align:left;background:#ff9900;"><label>Gesamtpreis</label><br /><input style="width:70px;" type="text" id="new_item_price_sum" name="new_item_price_sum" value="" /> &euro;</td>
		<td style="text-align:center;background:#ff9900;" colspan="3"><input type="button" id="add_item" name="add_item" value="Artikel hinzufügen" /></td>
	</tr>
</form>	
</table>
<br />
<form method="post" action="" id="table_retail_sale">
<table border="0" cellpadding="2" cellspacing="1" style="border:1px solid #aaaaaa;background:#cccccc;width:930px;">
	<input type="hidden" id="field_retail_id" name="id" value="{$form_data->id}" />
	<tr>
		<td style="text-align:center;background:#dddddd;width:300px;font-weight:bold;">Artikel</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;">Menge</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;text-align:right;">VK Preis gesamt&nbsp;</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;text-align:right;">MwSt. &euro;&nbsp;</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;">MwSt. %</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;">&nbsp;</td>
	</tr>
	{foreach item=article from=$form_data->item_list}
	<input type="hidden" name="item_id[]" value="{$article->id}" />
	<tr style="background:#eeeeee;border: 1px solid #ffffff;">
		<td style="text-align:center;background:#ffffff;width:300px;text-align:left;">#{$article->item_number} {$article->item_name}</td>
		
		
		<td style="text-align:center;background:#ffffff;" class="field_amount">
			<div class="amount_edit" style="display:none;"><input type="text" name="item_amount[]" style="width:50px;text-align:center;border:0px;height:15px;" value="{$article->amount}" /></div>
			<div class="amount_show">{$article->amount}</div>
		</td>
		<td style="background:#ffffff;text-align:right;" class="field_price_sum">
			<div class="price_sum_edit" style="display:none;"><input type="text" style="width:80px;border:0px;height:15px;text-align:right;" name="item_price_sum[]" value="{$article->price_sum|number_format:"2":",":""}" /> &euro;&nbsp;</div>
			<div class="price_sum_show">{$article->price_sum|number_format:"2":",":"."} &euro;&nbsp;</div>
		</td>
		
		
		<td style="background:#ffffff;text-align:right;">{$article->item_tax_sum|number_format:"2":",":"."} &euro;&nbsp;</td>
		<td style="text-align:center;background:#ffffff;">{$article->item_tax}</td>
		<td style="text-align:center;background:#ffffff;">
			<a class="delete_item" rel="{$article->id}" href="#" title="L&ouml;schen">
				<img src="{$info->base_url}/themes/admin/media/images/delete.png" alt="L&ouml;schen" />
			</a>
		</td>
	</tr>
	{/foreach}
	<tr>
		<td style="text-align:center;background:#dddddd;width:300px;"></td>
		<td style="text-align:center;background:#dddddd;"></td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;text-align:right;">{$form_data->vk_sum|number_format:"2":",":"."} &euro;&nbsp;</td>
		<td style="text-align:center;background:#dddddd;font-weight:bold;text-align:right;">{$form_data->tax_sum|number_format:"2":",":"."} &euro;&nbsp;</td>
		<td style="text-align:center;background:#dddddd;"></td>
		<td style="text-align:center;background:#dddddd;"></td>
	</tr>
	<tr>
		<td colspan="6" style="text-align:center;background:#ffffff;width:300px;text-align:right;">
			
			<table border="0" cellpadding="2" cellspacing="2" style="border:1px solid #aaaaaa;">
				<tr style="background:#dddddd;">
					<td colspan="2" style="text-align:center;background:#dddddd;font-weight:bold;">Tagesumsatz</td>
				</tr>
				<tr style="background:#dddddd;">
					<td style="font-weight:bold;text-align:left;">Umsatz 19%:</td>
					<td>{$form_data->tax_19_sum|number_format:"2":",":"."} &euro;</td>
				</tr>
				<tr style="background:#dddddd;">
					<td style="font-weight:bold;text-align:left;">Umsatz 7%:</td>
					<td>{$form_data->tax_7_sum|number_format:"2":",":"."} &euro;</td>
				</tr>
				<tr style="background:#dddddd;">
					<td style="font-weight:bold;text-align:left;">EC-Cash Umsatz:</td>
					<td><input style="text-align:right;" type="text" id="ec_cash" name="ec_sum" value="{$form_data->ec_sum}"> &euro;</td>
				</tr>
				<tr style="background:#dddddd;">
					<td style="font-weight:bold;text-align:left;">Bar Umsatz:</td>
					<td>{$form_data->cash_sum|number_format:"2":",":"."} &euro;</td>
				</tr>
			</table>	

		</td>
	</tr>
</table>
<br />
<input type="button" id="save_form" value="Tagesumsatz speichern">
<input type="button" id="print_button" value="Tagesumsatz drucken" style="position:absolute;right:20px;">
</form>
<br />

<script>
$(document).ready(function() {
	// save ec sum
	$('#save_form').click(function() {

		var jqxhr;
		var form_data;
		form_data = $('#table_retail_sale').serialize();

		jqxhr = $.ajax({
			'url' : './index.php?admin_modul=admin_retail_sale_editor&action=update',
			'dataType' : 'JSON',
			'type' : 'POST',
			'data' : form_data
		});

		jqxhr.success(function(data) {
			if (data.status == 'success') {
				$('#admin_retail_sale_edit').submit();
			} else {
				alert('Error!');
			}
		});

	});
	
	// save new item
	$('#add_item').click(function() {
		var jqxhr;
		jqxhr = $.ajax({
			'url' : './index.php?admin_modul=admin_retail_sale_editor',
			'dataType' : 'JSON',
			'type' : 'POST',
			'data' : {
				'action' : 'add_item',
				'id' : $('#retail_id').val(),
				'item_id' : $('#item_search_id').val(),
				'amount' : $('#new_item_amount').val(),
				'price_sum' : $('#new_item_price_sum').val()
			}
		});

		jqxhr.success(function(data) {
			if (data.status == 'success') {
				$('#admin_retail_sale_edit').submit();
			} else {
				alert('Error!');
			}
		});

	});
		
	
	
	// item search code
	$('#item_search').live('keyup', function(e) {
		e.preventDefault();
		
		if($(this).val().length >= 3) {
			var jqxhr = $.ajax({
				'url': './index.php?admin_modul=admin_item&mod=json&action=get_by_string&data=' + encodeURIComponent($(this).val())
			});
			
			jqxhr.success(function(data) {
				try {
					var data = $.parseJSON(data);
					$('#item_search_hits').text(data.hits + " Ergebnisse");
					
					if (data.hits >= 1) {
						$('#item_search_results').removeClass('hidden');
					} else {
						$('#item_search_results').addClass('hidden');
					}
					
					$('#item_search_results').html('');
					$.each(data.data , function(index, value) {
						var outstring = value.name;
						if (value.manufacturer) {
							outstring += ', ' + value.manufacturer
						}
						outstring += ', ' + value.number;
						$('#item_search_results').append('<div class="item_search_selectable" rel="' + outstring + '" title="' + value.id + '">' + outstring + '</div>');
					});
				} catch(e) {
					bend.dialogs.confirmationDialog({
						confirmation: 'Ok',
						showDenial: false,
						heading: 'ERROR',
						message: 'item search code JSON Error'
					});
				}
			});
			
			jqxhr.error(function() {
				bend.dialogs.confirmationDialog({
					confirmation: 'Ok',
					showDenial: false,
					heading: 'ERROR',
					message: 'item search code URL Error'
				});
			});
		} else {
			$('#item_search_hits').text('');
			$('#item_search_results').html('');
			$('#item_search_results').addClass('hidden');
		}
	});
	
	// click on item results
	$('.item_search_selectable').live('click', function(e) {
		var itemId = $(this).attr('title');
		
		$('#item_search_hits').text('');
		$('#item_search_results').html('');
		$('#item_search_results').addClass('hidden');
		$('#item_search').val('');
		$('#item_search_id').val(itemId);
		item_name = $(this).attr('rel')
		$('#item_search').val(item_name);
		
	});
	
	// save new item
	$('.delete_item').click(function(e) {
		e.preventDefault();
		
		confirmed = confirm('Wollen Sie den Artikel wirklich löschen?');
		if(confirmed) {
			var jqxhr;
			jqxhr = $.ajax({
				'url' : './index.php?admin_modul=admin_retail_sale_editor',
				'dataType' : 'JSON',
				'type' : 'POST',
				'data' : {
					'id' : $('#retail_id').val(),
					'action' : 'delete_item',
					'item_id' : $(this).attr('rel')
				}
			});

			jqxhr.success(function(data) {
				if (data.status == 'success') {
					$('#admin_retail_sale_edit').submit();
				} else {
					alert('Error!');
				}
			});
		}

	});

	
	$('.field_amount, .field_price_sum').hover(
		function() {
			$(this).css({
				'border': '1px solid #999999'
			});
		},
		function() {
			$(this).css({
				'border': '0px solid #000000'
			});
		}
	);

	
	$('.field_amount').click(function(e) {
		e.preventDefault();
		
		$(this).css({
			'background-color': '#ff9900'
		});

		$('.amount_edit',this).css({
			'display': 'inline'
		});

		$('.amount_show',this).css({
			'display': 'none'
		});
	});
		
	$('.field_price_sum').click(function(e) {
		e.preventDefault();
		
		$(this).css({
			'background-color': '#ff9900'
		});

		$('.price_sum_edit',this).css({
			'display': 'inline'
		});

		$('.price_sum_show',this).css({
			'display': 'none'
		});
	});
	
	var retail_id = $('#field_retail_id').val();
	$('#retail_id').val(retail_id);
		
});
</script>





