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

<script src="js/lms-ui-deadline-selection.js"></script>
<script>

	function saveheader() {
		if (document.setcustomer.customer)
			if (document.setcustomer.customer.value != 0)
				document.setcustomer.customerid.value = document.setcustomer.customer.value;

		$('#ingross').prop('disabled', false);
		$('#innet').prop('disabled', false);
		document.setcustomer.submit();
	}

</script>

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

<form name="setcustomer" id="setcustomer" method="POST" action="?m=invoiceedit&action=setcustomer">
	<input type="submit" class="hiddenbtn">
	<input type="hidden" name="invoice[template]" value="{$invoice.template}">
	<input type="hidden" name="invoice[doctype]" value="{$invoice.doctype}">
	{if $invoice.proforma}
		<input type="hidden" name="invoice[proforma]" value="{$invoice.proforma}">
	{/if}

	<table class="lmsbox">
		<colgroup>
			<col style="width: 1%;">
			<col style="width: 99%;">
		</colgroup>
		<thead>
			<tr>
				<td width="100%" colspan="2">
					<strong>{trans("Main Information:")}</strong>
				</td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td class="bold nobr">
					{trans("Invoice number:")}
				</td>
				<td class="nobr">
					{if isset($customer.id)}
						{assign var=customerid value=$customer.id}
					{else}
						{assign var=customerid value=$invoice.customerid}
					{/if}
					{numberplan_selection
						customer_selector = "#ownerid_input"
						cdate_selector    = "[name='invoice[cdate]']"
						numberplanlist    = $numberplanlist
						input_name        = "invoice[number]"
						input_value       = $invoice.number
						select_name       = "invoice[numberplanid]"
						selected          = $invoice.numberplanid
						number_trigger    = "number"
						plan_trigger      = "numberplanid"
						time              = $invoice.cdate
						customer_id       = $customerid
						planDocumentType  = $planDocumentType
					}
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Settlement date:")}
				</td>
				<td class="nobr">
					{if ConfigHelper::checkPrivilege("invoice_consent_date")}
						<input type="text" name="invoice[cdate]" value="{$invoice.cdate|date_format:"Y/m/d"}" size="12"
							placeholder="{trans("yyyy/mm/dd")}" data-cdate="{$invoice.cdate}"
							{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"}
						>
					{else}
						<input type="hidden" name="invoice[cdate]" value="{$invoice.cdate|date_format:"Y/m/d"}">
						{$invoice.cdate|date_format:"Y-m-d"}
					{/if}
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Sale date:")}
				</td>
				<td class="nobr">
					<input type="hidden" name="invoice[sdate]" value="{$invoice.sdate|date_format:"Y/m/d"}">
					{if ConfigHelper::checkPrivilege("invoice_sale_date")}
						<input type="text" name="invoice[sdate]" value="{$invoice.sdate|date_format:"Y/m/d"}" size="12"
							placeholder="{trans("yyyy/mm/dd")}"
							{tip class="lms-ui-date" text="Enter date of sale in YYYY/MM/DD format (empty field means current date) or click to select it from calendar" trigger="sdate"}
							{if !empty($invoice['copy-cdate'])} disabled{/if}>
					{else}
						{$invoice.sdate|date_format:"Y-m-d"}
					{/if}
					<label>
						<input type="checkbox" name="invoice[copy-cdate]" id="copy-cdate" value="1"
								{if !empty($invoice['copy-cdate'])} checked{/if}>
						{trans("copy consent date")}
					</label>
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Deadline:")}
				</td>
				<td class="nobr">
					{deadline_selection name="invoice[deadline]" id="deadline" cdate_selector="[name='invoice[cdate]']" value=$invoice.deadline}
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Payment type (cash/transfer/etc):")}
				</td>
				<td class="nobr">
					<select name="invoice[paytype]" {tip text="Select payment type" trigger="paytype"}>
						{foreach Localisation::arraySort(Utils::array_column($_PAYTYPES, 'label')) as $key => $item}
							<option value="{$key}"{if $invoice.paytype == $key} selected{/if}>{trans($item)}</option>
						{/foreach}
					</select>
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Document flags:")}
				</td>
				<td class="nobr">
					<label id="splitpayment" {tip text="translates into JPK MPP flag"}
							{if empty($suggested_flags.splitpayment)} style="display: none;"{/if}
					>
						<input type="checkbox" name="invoice[splitpayment]" value="1"
								{if !empty($suggested_flags.splitpayment) && $invoice.splitpayment} checked{/if}
						>
						{trans("split payment")}
					</label>
					<label {tip text="translates into JPK FP flag"}>
						<input type="checkbox" name="invoice[flags][{$smarty.const.DOC_FLAG_RECEIPT}]" value="1"
							id="receipt-flag"
							{if $invoice.flags[$smarty.const.DOC_FLAG_RECEIPT]} checked{/if}
							{if !empty($invoice.extid)} disabled{/if}
						>
						<span{if !empty($invoice.extid)} class="lms-ui-disabled"{/if}>
							{trans("<!invoice>receipt")}
						</span>
					</label>
					<label id="telecomservice" {tip text="translates into JPK EE flag"}
							{if empty($suggested_flags.telecomservice)} style="display: none;"{/if}
					>
						<input type="checkbox" name="invoice[flags][{$smarty.const.DOC_FLAG_TELECOM_SERVICE}]" value="1"
								{if $invoice.flags[$smarty.const.DOC_FLAG_TELECOM_SERVICE]} checked{/if}
						>
						{trans("<!invoice>telecommunication service")}
					</label>
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Accounting")}
				</td>
				<td class="nobr">
					<label>
						<input type="radio" id="ingross" name="invoice[netflag]" value="0"
								{if !$invoice.netflag} checked{/if} {if $contents} disabled{/if}
						>
						{trans("gross")}
					</label>
					<label>
						<input type="radio" id="innet" name="invoice[netflag]" value="1"
								{if $invoice.netflag} checked{/if} {if $contents} disabled{/if}
						>
						{trans("net")}
					</label>
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					{trans("Customer:")}
				</td>
				<td class="nobr">
					{if isset($customer.id)}
						{customerlist form="setcustomer" customers=$customers selected=$customer.id version=2
							selectname="customer" inputname="customerid" customOnChange="customer_change();"
							select_id="ownerid_select" input_id="ownerid_input" required=true}
					{else}
						{customerlist form="setcustomer" customers=$customers selected=$invoice.customerid version=2
							selectname="customer" inputname="customerid" customOnChange="customer_change();"
							select_id="ownerid_select" input_id="ownerid_input" required=true}
					{/if}
				</td>
			</tr>
			<tr>
				<td class="bold nobr">
					<label for="use_current_customer_data">{trans("Use current customer data:")}</label>
				</td>
				<td class="nobr">
					<input type="checkbox" id="use_current_customer_data" name="invoice[use_current_customer_data]" value="1"
						{if $invoice.use_current_customer_data} checked{/if}
						{tip text="Check it if you want to use current customer data"}
					>
				</td>
			</tr>
			<tr>
				<td class="bold">
					{trans("Currency:")}
				</td>
				<td>
					{currency_selection selected=$invoice.currency elementname="invoice[currency]"}
				</td>
			</tr>
			<tr>
				<td class="bold">
					{trans("Recipient address:")}
				</td>
				<td>
					{if $invoice.customerid && $invoice.recipient_address_id }
						{$selected_address_id = $invoice.recipient_address_id}
					{else}
						{$selected_address_id = null}
					{/if}
					{include file="customer/customeraddresses.html" id="customer_addresses" name="invoice[recipient_address_id]"
						trigger="address_id" selected_address_id=$selected_address_id preselection=false}
				</td>
			</tr>
			<tr>
				<td class="bold">
					{trans("Comment:")}
				</td>
				<td>
					<textarea name="invoice[comment]" rows="4" cols="50" {tip text="Enter invoice comment"}>{$invoice.comment}</textarea>
				</td>
			</tr>
			<tr>
				<td class="lms-ui-box-buttons" colspan="2">
					{button icon="save" label="Submit" onclick="saveheader();"}
				</td>
			</tr>
		</tbody>
	</table>
</form>

{include file="invoice/invoicecustomer.html"}
{include file="invoice/invoicecontents.html"}

<script>

	var recipient_addr;

	{if $invoice.customerid && isset($invoice.recipient_address)}
		recipient_addr = JSON.parse(Base64.decode('{$invoice.recipient_address}'));
	{/if}

	function updateDocumentFlags() {
		$.ajax({
			data: {
				updatedocumentflags: 1,
				customerid: $('#ownerid_input').val(),
				cdate: $('[name="invoice[cdate]"]').val(),
				value: $('#s-valuebrutto').val()
			},
			dataType: "json",
			success: function(result) {
				$('#splitpayment').toggle(result.splitpayment);
				if (!result.splitpayment) {
					$('#splitpayment :checkbox').prop('checked', false);
				}
				//$('#telecomservice').toggle(result.telecomservice);
				if (!result.telecomservice) {
					$('#telecomservice :checkbox').prop('checked', false);
				}
			}
		});
	}

	/*!
	 * \brief Update customer addresses list on select change.
	 */
	function customer_change() {
		getCustomerAddresses($("#ownerid_input").val(), function(addresses) {
			{if $invoice.customerid && isset($invoice.recipient_address)}
				var addr = {};
				addr[recipient_addr.address_id] = $.extend(recipient_addr, {
					default_address: true,
					location_address_type: "4"
				});
				$.each(addresses, function() {
					if (this.hasOwnProperty("default_address")) {
						delete this.default_address;
					}
				});
				addresses = $.extend(addr, addresses);
			{/if}

			setAddressList('#customer_addresses', addresses);
		});
		updateDocumentFlags();
	}

	initAdvancedSelectsTest('#customer_addresses');

	$(function() {
		$('form[name="setcustomer"] [name="invoice[number]"]').focus();

		$('[name="invoice[cdate]"]').change(function(e, init) {
			if (typeof(init) === 'undefined' || !init) {
				updateDocumentFlags();
			}
			if ($("#copy-cdate").prop('checked')) {
				var date = $(this).datepicker("getDate");
				$('input[type="text"][name="invoice[sdate]').datepicker("setDate", date);
				$('[type="hidden"][name="invoice[sdate]"]').val(sprintf("%04d/%02d/%02d", date.getFullYear(), date.getMonth() + 1, date.getDate()));
			}
		});

		$('#copy-cdate').change(function(e, init) {
			var checked = $(this).prop('checked');
			$('input[type="text"][name="invoice[sdate]"]').datepicker("option", "disabled", checked);
			if (checked) {
				$('[name="invoice[cdate]"]').trigger('change', typeof(init) !== 'undefined' && init);
			}
		}).trigger('change', true);

		{if ConfigHelper::checkConfig('invoices.receipt_option_warning')}
			$('#receipt-flag').change(function() {
				if ($(this).prop('checked')) {
					alertDialog($t("You select the receipt option, which may require issuing fiscal receipt!"), this);
				}
			});
		{/if}
	});

</script>
{/block}
