{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
<script>

	$(function() {
		$('#customerselect').change(function() {
			$('#selectededit').text($(this).find('option:selected').length);
		});

		$('[name="addbalance[type]"]').change(function() {
			var that = this;
			$('#servicetype').prop('disabled', function() {
				return $(that).val() == '1';
			});
		}).change();
	});

	function del(id)
	{
		confirmDialog($t("Are you sure, you want to delete last operation?"), $('#delete-lastoperation')).done(function() {
			location.href = '?m=balancedel&id=' + id;
		});
		return false;
	}

</script>

<style>

	#selectededit {
		background-color: transparent;
		font-weight: bold;
	}

</style>

{$default_taxrate = ConfigHelper::getConfig('phpui.default_taxrate')}
{$default_taxlabel = ConfigHelper::getConfig('phpui.default_taxlabel')}

<H1>{$layout.pagetitle}</H1>

<form name="addbalance" id="addbalance" method="POST" action="?m=balanceadd">
</form>

<table width="100%" cellpadding="3">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
	<tr>
		<td class="valign-top">
			<table class="lmsbox">
				<thead>
					<tr>
						<td class="bold">
							{icon name="customer"}
							{trans("Customers:")}
						</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>
							{trans("Select customers which has to concern new payment or covenant operation or leave it not selected if you want to add network income/expense operation.")}<BR><BR>
							<select size="20" id="customerselect" name="addbalance[mcustomerid][]" multiple form="addbalance">
								{foreach $customers as $customer}
									<option value="{$customer.id}">{$customer.customername|trunescape:40} ({$customer.id|string_format:"%04d"})</option>
								{/foreach}
							</select>
							<br>
							{trans("Selected:")}
							<span id="selectededit">0</span>
						</td>
					</tr>
				</tbody>
			</table>
		</td>
		<td class="valign-top">
			<table class="lmsbox">
				<colgroup>
					<col style="width: 1%;">
					<col style="width: 1%;">
					<col style="width: 98%;">
				</colgroup>
				<thead>
					<tr>
						<td colspan="3" class="bold">
							{icon name="money"}
							{trans("Value and description:")}
						</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>
							{icon name="value"}
						</td>
						<td class="bold">
							{trans("Value:")}
						</td>
						<td class="nobr">
							<input type="text" name="addbalance[value]" value="{$value}" size="15"
								{tip text="Enter value of operation"} form="addbalance">
							{currency_selection selected=$currency elementname="addbalance[currency]" form="addbalance"}
						</td>
					</tr>
					<TR>
						<td>
							{icon name="tax"}
						</td>
						<td class="bold">
							{trans("Tax:")}
						</td>
						<td>
							<select size="1" name="addbalance[taxid]" {tip text="Select Tax rate"}
								form="addbalance">
								{foreach $taxeslist as $tax}
									<option value="{$tax.id}"
										{if $taxid && $tax.id == $taxid || !$taxid
											&& (isset($default_taxlabel) && $tax.label == $default_taxlabel
											|| !isset($default_taxlabel) && $tax.value == $default_taxrate)} selected{/if}
										>{$tax.label}</option>
								{/foreach}
							</select>
						</td>
					</TR>
					<TR>
						<td>
							{icon name="type"}
						</td>
						<td class="bold">
							{trans("Type:")}
						</td>
						<TD class="nobr">
							<label>
								<input type="radio" name="addbalance[type]" value="0" {tip text="Select type of operation"}
									form="addbalance">
								{trans("liability")}
							</label>
							<label>
								<input type="radio" name="addbalance[type]" value="1" {tip text="Select type of operation"}
									form="addbalance" checked>
								{trans("income/expense")}
							</label>
						</TD>
					</TR>
					<tr>
						<td>
							{icon name="money"}
						</td>
						<td class="bold nobr">
							{trans("Service type")}
						</td>
						<td>
							<select id="servicetype" name="addbalance[servicetype]" form="addbalance">
								<option value="">{trans("— none —")}</option>
								{foreach Localisation::arraySort($_SERVICETYPES) as $type => $label}
									<option value="{$type}"{if $servicetype == $type} selected{/if}>{$label}</option>
								{/foreach}
							</select>
						</td>
					</tr>
					<tr>
						<td>
							{icon name="calendar"}
						</td>
						<td class="bold">
							{trans("Date:")}
						</td>
						<td>
							<input type="text" name="addbalance[time]" value="{if $time}{$time|date_format:"Y/m/d H:i"}{/if}"
								size="15" maxlength="15" form="addbalance"
								{tip class="lms-ui-datetime" text="Enter date in 'yyyy/mm/dd hh:mm' format (empty field means current date) or click to choose date from calendar"}>
						</td>
					</tr>
					{if $sourcelist}
					<tr>
						<td>
							{icon name="cashsource"}
						</td>
						<td class="bold nobr">
							{trans("Source:")}
						</td>
						<td>
							<select name="addbalance[sourceid]" {tip text="Select import source (optional)" trigger="source"}
								form="addbalance">
								<option value="-1">{trans("<!cash-source>— default —")}</option>
								<option value="0">{trans("— none —")}</option>
								{foreach $sourcelist as $item}
									<option value="{$item.id}"{if $item.id == $sourceid} selected{/if}>{$item.name}</option>
								{/foreach}
							</select>
						</td>
					</tr>
					{/if}
					<tr>
						<td>
							<label for="notification">
								{icon name="notification"}
							</label>
						</td>
						<td>
							<label for="notification">
								<strong>{trans("Notify customer")}</strong>
							</label>
						</td>
						<td>
							<label>
								<input type="hidden" form="addbalance" name="addbalance[notification]" value="0">
								<input type="checkbox" form="addbalance" id="notification"
									name="addbalance[notification]" value="1"{if !empty($notification)} checked{/if}>
							</label>
						</td>
					</tr>
					<tr>
						<td>
							{icon name="info"}
						</td>
						<td class="bold">
							{trans("Description:")}
						</td>
						<td>
							<textarea name="addbalance[comment]" rows="5" cols="50" {tip text="Enter comment"}
								form="addbalance">{$comment|escape|replace:"\n":"<BR>"}</textarea>
						</td>
					</tr>
					<tr>
						<td class="lms-ui-box-buttons" colspan="3">
							{button icon="save" label="Submit" onclick="document.addbalance.submit();"}
						</td>
					</tr>
				</tbody>
			</table>
			{if $last}
			<table class="lmsbox" style="margin-top: 1em;">
				<colgroup>
					<col style="width: 1%;">
					<col style="width: 1%;">
					<col style="width: 98%;">
				</colgroup>
				<thead>
					<tr>
						<td colspan="3" class="bold">
							{icon name="money"}
							{trans("Last operation:")}
						</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>
							{icon name="value"}
						</td>
						<td class="bold">
							{trans("Value:")}
						</td>
						<td>
							{moneyf($last.value, $last.currency)}
						</td>
					</tr>
					{if $last.tax}
					<tr>
						<td>
							{icon name="tax"}
						</td>
						<td class="bold">
							{trans("Tax:")}
						</td>
						<td>
							{$last.tax}
						</td>
					</tr>
					{/if}
					<tr>
						<td>
							{icon name="calendar"}
						</td>
						<td class="bold">
							{trans("Date:")}
						</td>
						<td>
							{$last.time|date_format:"Y-m-d H:i"}
						</td>
					</tr>
					{if $last.customerid}
					<tr>
						<td>
							{icon name="customer"}
						</td>
						<td class="bold">
							{trans("Customer:")}
						</td>
						<td>
							<a href="?m=customerinfo&id={$last.customerid}">{$last.customername|escape}</a>
						</td>
					</tr>
					{/if}
					{if $last.sourcename}
					<tr>
						<td>
							{icon name="cashsource"}
						</td>
						<td class="bold">
							{trans("Source:")}
						</td>
						<td>
							{$last.sourcename}
						</td>
					</tr>
					{/if}
					{if $last.comment}
					<tr>
						<td>
							{icon name="info"}
						</td>
						<td class="bold">
							{trans("Description:")}
						</td>
						<td>
							<table width="100%" cellpadding="5">
								<tr>
									<td class="fall superlight">
										{$last.comment|escape|replace:"\n":"<BR>"}
									</td>
								</tr>
							</table>
						</td>
					</tr>
					{/if}
					<tr>
						<td class="lms-ui-box-buttons" colspan="3">
							{button icon="delete" label="Delete" id="delete-lastoperation" onclick="del({$last.id});"}
						</td>
					</tr>
				</tbody>
			</table>
			{/if}
		</td>
	</tr>
</table>
{/block}
