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

<form name="setcustomer" id="setcustomer" method="POST" action="?m=noteedit&action=setcustomer">
	<input type="submit" class="hiddenbtn">
	<input type="hidden" name="note[id]" value="{$note.id}">
	<input type="hidden" name="note[number]" value="{$note.number}">
	<input type="hidden" name="note[numberplanid]" value="{$note.numberplanid}">
	<input type="hidden" name="note[template]" value="{$note.template}">
</form>

<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
	<thead>
		<tr>
			<td colspan="2">
				<strong>{trans("Main Information:")}</strong>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td class="nobr">
				<strong>{trans("Debit note number:")}</strong>
			</td>
			<td class="nobr">
				{if isset($customer.id)}
					{assign var=customerid value=$customer.id}
				{else}
					{assign var=customerid value=$note.customerid}
				{/if}
				{numberplan_selection
					form              = "setcustomer"
					customer_selector = "#customerid_input"
					cdate_selector    = "#cdate"
					numberplanlist    = $numberplanlist
					input_name        = "note[number]"
					input_value       = $note.number
					select_name       = "note[numberplanid]"
					selected          = $note.numberplanid
					number_trigger    = "number"
					plan_trigger      = "numberplanid"
					time              = $note.cdate
					customer_id       = $customerid
					planDocumentType  = $planDocumentType
				}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				<strong>{trans("Settlement date:")}</strong>
			</td>
			<td class="nobr">
				{if ConfigHelper::checkPrivilege("invoice_consent_date")}
					<input type="text" name="note[cdate]" id="cdate" value="{$note.cdate|date_format:"Y/m/d"}"
						size="10" form="setcustomer" placeholder="{trans("yyyy/mm/dd")}"
						{tip class="lms-ui-date" text="Enter date of settlement in YYYY/MM/DD format (empty field means current date) or click to select it from calendar" trigger="cdate"}
					>
					{if $note.cdatewarning}
						<input type="hidden" name="note[cdatewarning]" value="1" form="setcustomer">
					{/if}
				{else}
					<input type="hidden" name="note[cdate]" id="cdate" value="{$note.cdate|date_format:"Y/m/d"}"
						form="setcustomer"
					>
					{$note.cdate|date_format:"Y-m-d"}
				{/if}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				<strong>{trans("Deadline:")}</strong>
			</td>
			<td class="nobr">
				<input type="text" name="note[paytime]" value="{$note.paytime}"
					size="10" form="setcustomer" {tip text="Enter deadline in days (optional)"}
				>
				({($note.cdate + $note.paytime * 86400)|date_format:"Y-m-d"})
			</td>
		</tr>
		<tr>
			<td class="nobr">
				<strong>{trans("Payment type:")}</strong>
			</td>
			<td class="nobr">
				{paytypes elemname="note[paytype]" selected=$note.paytype form="setcustomer"}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				<strong>{trans("Customer:")}</strong>
			</td>
			<td class="nobr">
				{if isset($customer.id)}
					{customerlist form="setcustomer" customers=$customers selected=$customer.id version=2
						selectname="customer" input_id="customerid_input" inputname="customerid" required=true}
				{else}
					{customerlist form="setcustomer" customers=$customers selected=$note.customerid version=2
						selectname="customer" input_id="customerid_input" inputname="customerid" required=true}
				{/if}
			</td>
		</tr>
		<tr>
			<td>
				<strong>{trans("Currency:")}</strong>
			</td>
			<td>
				{currency_selection selected=$note.currency elementname="note[currency]" form="setcustomer"}
			</td>
		</tr>
		<tr>
			<td class="lms-ui-box-buttons" colspan="2">
				{button type="submit" icon="save" label="Submit" form="setcustomer" id="submit-button"}
			</td>
		</tr>
	</tbody>
</table>

{if $customer}
<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
	<thead>
		<tr>
			<td class="nobr">
				{icon name="customer"}
				<strong>{trans("Customer:")}</strong>
			</td>
			<td>
				<strong>{$customer.customername}</strong>
				{icon name="next"}
				{$customer.address} &nbsp; {$customer.zip} {$customer.city}
				{if $customer.balance < 0}
					<span class="red">
				{/if}
				{moneyf($customer.balance)}
				{if $customer.balance < 0}
					</span>
				{/if}
			</td>
		</tr>
	</thead>
</table>
{/if}

<form method="POST" id="additem" action="?m=noteedit&action=additem" name="additem">
	<input type="submit" class="hiddenbtn">
</form>

<table class="lmsbox lms-ui-background-cycle">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 96%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				<strong>{trans("No.")}</strong>
			</td>
			<td>
				{trans("Title:")}
			</td>
			<td class="text-right nobr">
				{trans("Type")}
			</td>
			<td class="text-right nobr">
				{trans("Value:")}
			</td>
			<td>
				&nbsp;
			</td>
		</tr>
	</thead>
	<tbody>
		{foreach $contents as $item}
			<tr class="highlight">
				<td>
					<strong>{counter}.</strong>
				</td>
				<td>
					{$item.description}
				</td>
				<td class="text-right">
					{if isset($_SERVICETYPES[$item.servicetype])}
						{$_SERVICETYPES[$item.servicetype]}
					{else}
						{trans("— none —")}
					{/if}
				</td>
				<td class="text-right nobr">
					{$item.value|string_format:"%01.2f"}
				</td>
				<td>
					{button type="link" icon="delete" tip="Remove this item from list"
						href="?m=noteedit&action=deletepos&posuid={$item.posuid}"}
				</td>
			</tr>
		{foreachelse}
			<tr>
				<td colspan="5" class="empty-table">
					{trans("Debit note have no items. Use form below for items addition.")}
				</td>
			</tr>
		{/foreach}
		{if $contents}
			<tr>
				<td colspan="3" class="text-right bold">
					{trans("Total:")}
				</td>
				<td class="text-right nobr">
					<strong>{sum array=$contents column=value string_format="%01.2f"}</strong>
				</td>
				<td>
					&nbsp;
				</td>
			</tr>
		{/if}
	</tbody>
	<tfoot>
		<tr>
			<td class="nobr">
				<strong>{counter}.</strong>
			</td>
			<td>
				<input type="text" name="description" size="80" form="additem"
					{tip text="Enter description" trigger="description"}>
			</td>
			<td class="text-right">
				<select name="servicetype" form="additem" {tip text="Select type" trigger="type"}>
					<option value="">{trans("— none —")}</option>
					{foreach $_SERVICETYPES as $servicetype => $label}
						<option value="{$servicetype}">{$label}</option>
					{/foreach}
				</select>
			</td>
			<td class="text-right">
				<input type="text" name="value" size="8" form="additem" {tip text="Enter value" trigger="value"}>
			</td>
			<td>
				{button type="link" icon="save" tip="Add item" id="additem-button"}
			</td>
		</tr>
		<tr class="lms-ui-button-panel">
			<td colspan="5" class="text-right">
				{button icon="cancel" label="Cancel" href="?m=notelist"}
				{if !$customer}
					{button icon="save" class="customer-alert" label="Save"}
					{button icon="print" class="customer-alert" label="Save & Print"}
				{elseif !$contents}
					{button icon="save" class="document-alert" label="Save"}
					{button icon="print" class="document-alert" label="Save & Print"}
				{else}
					{button icon="save" label="Save" href="?m=noteedit&action=save"}
					{button icon="print" label="Save & Print" id="print-button"}
				{/if}
			</td>
		</tr>
	</tfoot>
</table>
<script>

	$(function() {
		$('[name="note[number]"]').focus();

		$(document).on('change', '[name="note[numberplanid]"]', function() {
			$('[name="note[number]"]').val('');
		});

		$('#setcustomer').submit(function() {
			if ($(this).get(0).form.checkValidity()) {
				return true;
			}
			$('#submit-button').prop('disabled', false);
			return false;
		});

		$('#additem-button').click(function() {
			$('#additem').submit();
		});

		$('#print-button').click(function() {
			$('#additem').attr('action', '?m=noteedit&action=save&print=1').submit();
		});

		$('.customer-alert').click(function() {
			alertDialog($t("Customer not selected!"), this);
			return;
		});
		$('.document-alert').click(function() {
			alertDialog($t("Document has no items!"), this);
			return;
		});
	});

</script>
{/block}
