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

	function saveReceipt(print) {
		var which = 0;
		$('form[name="page"] [name^="which"]:checked').each(function() {
			which += parseInt($(this).val());
		});

		document.additem.action = "?m=receiptadd&action=save&which=" + which;

		if (print == 1) {
			document.additem.action += "&print=1";
		}
		document.additem.submit();
	}

	function saveHeader() {
		if (document.setcustomer.customer && document.setcustomer.customer.value != 0) {
			document.setcustomer.customerid.value = document.setcustomer.customer.value;
		}

		$('[name="notification"]').attr("form", "setcustomer");
		document.setcustomer.submit();
	}

	function moveCash() {
		$('[name="notification"]').attr("form", "movecash");
		document.movecash.submit();
	}

	function addItemList() {
		$('[name="notification"]').attr("form", "additemlist");
		document.additemlist.submit();
	}

	function addItem() {
		$('[name="notification"]').attr("form", "additem");
		document.additem.action = "?m=receiptadd&action=additem";
		document.additem.target = "";
		document.additem.submit();
	}

</script>

<form name="movecash" id="movecash" method="post" action="?m=receiptadd&action=movecash">
</form>

<form name="additemlist" id="additemlist" method="post" action="?m=receiptadd&action=additemlist">
</form>

<form method="post" name="additem" id="additem">
	<input type="submit" class="hiddenbtn">
</form>

{$default_printpage = ConfigHelper::getConfig('receipts.default_printpage')}
<form name="setcustomer" id="setcustomer" method="POST"
	action="?m=receiptadd{if $receipt.type}&type={$receipt.type}{/if}&action={if $receipt.regid && !$error.regid}setcustomer{else}setreg{/if}">
	<input type="submit" class="hiddenbtn">
	<input type="hidden" name="receipt[divisionid]" value="{$receipt.divisionid}"
		{if $receipt.type && !isset($customer.id) && empty($receipt.customerid)} disabled{/if}>
	<input type="hidden" name="receipt[type]" value="{$receipt.type}">
	<input type="hidden" name="receipt[numberplanid]" value="{$receipt.numberplanid}">
<TABLE class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
    <THEAD>
	<TR>
		<TD WIDTH="100%" COLSPAN="2">
			<B>{trans("Main Information:")}</B>
		</TD>
	</TR>
    </THEAD>
    <TBODY>
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Cash Registry:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP id="cashregistries">
			{include file="receipt/receiptcashregistries.html" regid=$receipt.regid}
		</TD>
	</TR>
	{if !$receipt.type}
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Type:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP>
			<SELECT SIZE="1" NAME="type" {tip text="Select cash receipt type"}>
				<OPTION VALUE="in"{if $receipt.type=="in"} SELECTED{/if}>{trans("pay in")}</OPTION>
				<OPTION VALUE="out"{if $receipt.type=="out"} SELECTED{/if}>{trans("pay out")}</OPTION>
			</SELECT>
		</TD>
	</TR>
	{/if}
	{if $receipt.numberplanid != ""}
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Receipt number:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP>
			<INPUT TYPE="TEXT" NAME="receipt[number]" VALUE="{if $receipt.number}{$receipt.number}{/if}" SIZE="10" {tip text="Enter receipt number. WARNING! Changing this number can be DANGEROUS! (leave this field empty to obtain next number)" trigger="number"}{if ($rights & 256) != 256 } readonly{/if}>
		{if $receipt.extnumber || $receipt.extended}
			{trans("Additional number:")}
			<INPUT TYPE="TEXT" NAME="receipt[extnumber]" VALUE="{$receipt.extnumber}" SIZE="40" {tip text="Enter additional document number" trigger="extnumber"}>
		{/if}
		</TD>
	</TR>
	{/if}
	{if $receipt.cdate}
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Settlement date:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP>
			<INPUT TYPE="TEXT" NAME="receipt[cdate]" class="lms-ui-date" VALUE="{$receipt.cdate|date_format:"Y/m/d"}" SIZE="10" {tip 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 ($rights & 256) != 256}readonly{/if}>
			{if $receipt.cdatewarning}
			<INPUT TYPE="HIDDEN" NAME="receipt[cdatewarning]" VALUE="1">
			{/if}
		</TD>
	</TR>
	{/if}
	<tr>
		<td class="bold nobr">
			{trans("Currency:")}
		</td>
		<td class="nobr">
			{currency_selection elementname="receipt[currency]" selected=$receipt.currency locked=$receipt.currency_locked}
		</td>
	</tr>
	{if $receipt.type && $rights}
	<tr id="division-row">
		<td class="nobr">
			<label for="divisionid"{if isset($customer.id) || !empty($receipt.customerid)} class="lms-ui-disabled"{/if}>
				<strong>
					{trans("Division")}
				</strong>
			</label>
		</td>
		<td>
			{division_selection name="receipt[divisionid]" selected=$receipt.divisionid superuser="{if !empty($superuser)}1{/if}"
				id="division-selection"
				disabled=(isset($customer.id) || !empty($receipt.customerid))}
		</td>
	</tr>
	<tr>
		<td>
			<strong>{trans("Target")}</strong>
		</td>
		<td class="nobr">
			<table>
				<colgroup>
					<col style="width: 1%;">
					<col style="width: 99%;">
				</colgroup>
				{if ($rights & 2) == 2}
					<tr>
						<td class="nobr">
							<label>
								<input type="radio" name="receipt[o_type]" value="customer" id="radio_customer" checked>
								{trans("customer")}
							</label>
						</td>
						<td class="nobr">
							{if isset($customer.id)}
								{customerlist form="setcustomer" customers=$customerlist selected=$customer.id version=2
									selectname="customer" inputname="customerid" input_id="customerid"
									customOnChange="document.getElementById('radio_customer').checked=true;xajax_GetCashRegistriesXajax($('[name=customerid]').val(), $('[name=\'receipt[regid]\']').val())"}
							{else}
								{customerlist form="setcustomer" customers=$customerlist selected=$receipt.customerid version=2
									selectname="customer" inputname="customerid" input_id="customerid"
									customOnChange="document.getElementById('radio_customer').checked=true;xajax_GetCashRegistriesXajax($('[name=customerid]').val(), $('[name=\'receipt[regid]\']').val())"}
							{/if}
						</td>
					</tr>
				{/if}
				{if ($rights & 4) == 4}
					{if $cashregcount > 1 && $receipt.type=="out"}
						<tr>
							<td colspan="2" class="nobr">
								<label>
									<input type="radio" name="receipt[o_type]" value="move"
										{if $receipt.o_type=="move"} checked{/if}>
									{trans("assets move")}
								</label>
							</td>
						</tr>
					{/if}
				{/if}
				{if ($rights & 8) == 8}
					{if $receipt.type=="out"}
						<tr>
							<td class="nobr">
								<label>
									<input type="radio" name="receipt[o_type]" value="advance" id="radio_advance"
										{if $receipt.o_type == "advance"} checked{/if}>
									{trans("advance")}
								</label>
							</td>
							<td>
								<input type="text" name="receipt[adv_name]" value="{$receipt.adv_name}" size="70"
									{tip text="Enter recipient" trigger="adv_name"}
									onclick="document.getElementById('radio_advance').checked=true">
							</td>
						</tr>
					{/if}
				{/if}
				{if ($rights & 16) == 16}
					<tr>
						<td class="nobr">
							<label>
								<input type="radio" name="receipt[o_type]" value="other" id="radio_other"
									{if $receipt.o_type == "other"} checked{/if}>
								{trans("other")}
							</label>
						</td>
						<td>
							<input type="text" name="receipt[other_name]" value="{$receipt.other_name}" size="70"
								{if $receipt.type=="out"}{tip text="Enter recipient" trigger="o_name"}{else}{tip text="Enter payer" trigger="other_name"}{/if}
								onclick="document.getElementById('radio_other').checked=true">
						</td>
					</tr>
				{/if}
			</table>
		</td>
	</tr>
	{elseif $receipt.customerid}
		<INPUT type="hidden" name="customerid" value="{$receipt.customerid}">
	{/if}
	<TR>
		<TD WIDTH="100%" ALIGN="right" COLSPAN="2">
			<A HREF="javascript:saveHeader();">{trans("Select")} <IMG SRC="img/save.gif" ALT=""></A>
		</TD>
	</TR>
    </TBODY>
</TABLE>
</form>
{if $error.nocash}<h2 align="center"><span class="lms-ui-error">{$error.nocash}</span></h2>{/if}
{if $receipt.selected && $customer}
<P>
<TABLE class="lmsbox">
    <THEAD>
	<TR>
		<TD WIDTH="1%" nowrap>
			<IMG SRC="img/customer.gif" ALT=""> <B>{trans("Customer:")}</B>
		</TD>
		<TD WIDTH="1%" nowrap>
			<B>{$customer.customername}</B>
			&nbsp;&raquo;&nbsp; {if $customer.postoffice && $customer.postoffice != $customer.city && $customer.street}{$customer.city}, {/if}{$customer.address},
				{$customer.zip} {if $customer.postoffice}{$customer.postoffice}{else}{$customer.city}{/if}
			&nbsp;&raquo;&nbsp; {if $customer.balance < 0}<span class="red">{/if}{$customer.balance|money_format}{if $customer.balance < 0}</span>{/if}
		</TD>
		<TD WIDTH="98%" align="right">
			{if $customer.groups}<span class="red bold">
			({foreach from=$customer.groups item=item name=groups}{$item.name}{if $smarty.foreach.groups.iteration < count($customer.groups)}, {/if}{/foreach})
			</span>{/if}
		</TD>
	</TR>
    </THEAD>
</TABLE>
{if $customer.notes}
<BR>
<TABLE class="lmsbox">
    <THEAD>
	<TR>
		<TD width="1%" nowrap>
			<IMG SRC="img/note.gif" ALT=""> <B>{trans("Notes:")}</B>
		</TD>
		<TD>
		        <span class="red">{$customer.notes|replace:"\n":"<BR>"}</span>
		</TD>
	</TR>
    </THEAD>
</TABLE>
{/if}
{if $customer.docwarning}
<BR>
<TABLE class="lmsbox">
    <THEAD>
	<TR>
		<TD width="1%" nowrap>
			<IMG SRC="img/docum.gif" ALT=""> <B>{trans("Documents:")}</B>
		</TD>
		<TD>
		        <span class="red">{$customer.docwarning}</span>
		</TD>
	</TR>
    </THEAD>
</TABLE>
{/if}
{if $customer.nodeswarning}
<BR>
<TABLE class="lmsbox">
    <THEAD>
	<TR>
		<TD width="1%" nowrap>
			<IMG SRC="img/node.gif" ALT=""> <B>{trans("Nodes:")}</B>
		</TD>
		<TD>
		        <span class="red">{$customer.nodeswarning}</span>
		</TD>
	</TR>
    </THEAD>
</TABLE>
{/if}
{if $customer.nodegroupswarning}
<BR>
<TABLE class="lmsbox">
    <THEAD>
	<TR>
		<TD width="1%" nowrap>
			<IMG SRC="img/nodegroup.gif" ALT=""> <B>{trans("Groups:")}</B>
		</TD>
		<TD>
		        <span class="red">{$customer.nodegroupswarning}</span>
		</TD>
	</TR>
    </THEAD>
</TABLE>
{/if}
</P>
{/if}
{if $receipt.type=="out" && $receipt.o_type=="move" && $receipt.selected && $cashregcount > 1}
<BR>
<TABLE class="lmsbox">
	<COLGROUP>
		<COL style="width: 1%;">
		<COL style="width: 99%;">
	</COLGROUP>
    <THEAD>
	<TR>
		<TD colspan="2" class="bold">
			{trans("Moving assets:")}
		</TD>
	</TR>
    </THEAD>
    <TBODY>
	<TR>
		<TD class="nobr">
            <span class="bold">{trans("Value:")}</span>
			<INPUT TYPE="TEXT" NAME="value" form="movecash" VALUE="" SIZE="10" {tip text="Enter value"}>
			<br>
			<a href="javascript:GetCashRegistryBalance();">{trans("get source registry balance")}</a>
            <script>
                function GetCashRegistryBalance() {
                    xajax_GetCashRegistryBalance($('[name="receipt[regid]"]').val());
				}
            </script>
		</TD>
		<TD class="bold nobr valign-top">
			{trans("Cash Registry:")}
			<SELECT SIZE="1" NAME="registry" form="movecash" {tip text="Select cash registry"}>
				<OPTION VALUE="0">{trans("— select registry —")}</OPTION>
				{foreach from=$cashreglist item=c}
				{if $c.id != $receipt.regid}
				<OPTION VALUE="{$c.id}">{$c.name|truncate:40:"&hellip;":true} ({$c.id|string_format:"%04d"})</OPTION>
				{/if}
				{/foreach}
			</SELECT>
		</TD>
	</TR>
	<TR>
		<TD class="buttons" colspan="2">
			<A HREF="javascript:moveCash();">{trans("Save & Print")} <IMG SRC="img/save.gif" ALT=""></A>
		</TD>
	</TR>
    </TBODY>
</TABLE>
{/if}
{if $receipt.selected && $receipt.o_type!="move"}
<TABLE class="lmsbox">
    <THEAD>
	{if $receipt.o_type == "customer" && $receipt['currency'] == Localisation::getCurrentCurrency()}
	<TR>
		<TD colspan="4">
			<TABLE class="lmsbox">
                            <THEAD>
				<TR>
					<TD>
						<IMG src="img/money.gif" ALT="">
						<B><A name="balance">{trans("Last 10 not accounted invoices:")}</A></B>
					</TD>
				</TR>
                            </THEAD>
                            <TBODY>
				<TR>
					<TD width="100%">
						<TABLE class="lmsbox-inner">
                                                    <THEAD>
							<TR>
								<TD width="1%">{trans("Date:")}</TD>
								<TD width="97%">{trans("Invoice:")}</TD>
								<TD width="1%" align="right">{trans("Value:")}</TD>
								<TD width="1%">&nbsp;</TD>
							</TR>
                                                    </THEAD>
                                                    <TBODY>
							{cycle values="light,lucid" print=false}
							{foreach from=$invoicelist item=item}
							<TR class="highlight {cycle}"  >
								<TD onclick="checkElement('mark{$item.id}')" class="nobr">{$item.cdate|date_format:"Y-m-d"}</TD>
								<TD onclick="checkElement('mark{$item.id}')">{$item.number}</TD>
								<TD align="right" onclick="checkElement('mark{$item.id}')" class="nobr">
									{moneyf($item.value * $item.currencyvalue)}
									{if $item.currency != Localisation::getCurrentCurrency()}
										<br>({moneyf($item.value, $item.currency)})
									{/if}
								</TD>
								<TD align="right">
									<INPUT TYPE="checkbox" ID="mark{$item.id}" form="additemlist" NAME="marks[]" VALUE="{$item.id}" checked>
								</TD>
							</TR>
							{foreachelse}
							<tr>
								<td colspan="4" class="empty-table">
									{trans("That customer hasn't got not accounted invoices.")}
								</td>
							</tr>
							{/foreach}
							<TR>
								<TD colspan="2" nowrap>
									<label>
										<input type="checkbox" form="additemlist" name="allbox" onchange="CheckAll('additemlist', this)" value="1"  checked>
										{trans("Check All")}
									</label>
								</TD>
							        <TD ALIGN="right" colspan="2" nowrap>
									<A href="javascript:addItemList();">{trans("Add items")} <IMG src="img/save.gif" ALT=""></A>
								</TD>
							</TR>
                                                    </TBODY>
						</TABLE>
					</TD>
				</TR>
                            </TBODY>
			</TABLE>
		</TD>
        </TR>
	{/if}
	<TR>
		<TD WIDTH="1%">
			<B>{trans("No.")}</B>
		</TD>
		<TD WIDTH="97%" NOWRAP>
			{trans("Name of product, commodity or service")}
		</TD>
		<TD WIDTH="1%" NOWRAP ALIGN="RIGHT">
			{trans("Value")}
		</TD>
		<TD WIDTH="1%" NOWRAP></TD>
	</TR>
    </THEAD>
    <TBODY>
	{cycle values="light,lucid" print=false name="items"}
	{foreach from=$contents item=item}
	<TR class="highlight {cycle name="items"}"  >
		<TD WIDTH="1%">
			<B>{counter}.</B>
		</TD>
		<TD WIDTH="93%">
			{$item.description}
		</TD>
		<TD WIDTH="1%" NOWRAP ALIGN="RIGHT">
			{$item.value|string_format:"%01.2f"}<BR>
		</TD>
		<TD WIDTH="1%" NOWRAP>
			<A HREF="?m=receiptadd&action=deletepos&posuid={$item.posuid}"><IMG SRC="img/delete.gif" {tip text="Remove this item from list"}></A>
		</TD>
	</TR>
	{foreachelse}
	<tr>
		<td colspan="4" class="empty-table">
			{trans("Receipt have no items. Use form below for items addition.")}
		</td>
	</tr>
	{/foreach}
	{if $contents}
	<TR>
		<TD COLSPAN="2" WIDTH="98%" ALIGN="RIGHT">
			<B>{trans("Total:")}</B>
		</TD>
		<TD WIDTH="1%" NOWRAP ALIGN="RIGHT">
			{sum array=$contents column=value assign="total_value"}{$total_value|string_format:"%01.2f"}
		</TD>
		<TD WIDTH="1%"></TD>
	</TR>
	{/if}
    </TBODY>
	<tfoot>
		<tr>
			<td style="width: 1%;" class="nobr">
				<strong>{counter}.</strong>
			</td>
			<td style="width: 94%;">
				<input type="text" name="description" size="80" form="additem"
					{tip text="Enter item description"}
					{if ($rights & 8) != 8 && ($rights & 32) != 32} readonly{/if}>
			</td>
			<td>
				<input type="text" name="value" size="10" value="" form="additem"
					{tip text="Enter item value"}
					{if ($rights & 8) != 8 && ($rights & 32) != 32} readonly{/if}>
			</td>
			<td>
				<a href="javascript:addItem();">
					<img src="img/save.gif" {tip text="Add item"}>
				</a>
			</td>
		</tr>
		<tr>
			<td colspan="4" class="text-right">
				<label>
					<input type="checkbox" name="notification" value="1" form="additem"
						{if !empty($receipt.notification)} checked{/if}>
					{trans("Notify customer")}
				</label>
			</td>
		</tr>
		<tr style="border-top: none;">
			<td colspan="4" class="text-right">
				<label>
					<input type="checkbox" name="which[{$smarty.const.DOC_ENTITY_ORIGINAL}]"
						value="{$smarty.const.DOC_ENTITY_ORIGINAL}" form="additem"
						{if preg_match('/original/i', $default_printpage)} checked{/if}>
					{$_DOCENTITIES[$smarty.const.DOC_ENTITY_ORIGINAL]}
				</label>
				<label>
					<input type="checkbox" name="which[{$smarty.const.DOC_ENTITY_COPY}]"
						value="{$smarty.const.DOC_ENTITY_COPY}" form="additem"
						{if preg_match('/copy/i', $default_printpage)} checked{/if}>
					{$_DOCENTITIES[$smarty.const.DOC_ENTITY_COPY]}
				</label>
				<a href="?m=receiptlist&regid={$receipt.regid}">
					{trans("Cancel")}
					<img src="img/cancel.gif">
				</a>
				<a href="?m=receiptadd&action=init&type={$receipt.type}">
					{trans("Clear Contents")}
					<img src="img/delete.gif">
				</a>
				{if !$customer && $receipt.o_type != "other" && $receipt.o_type != "advance"}
					<a href="javascript:alertDialog($t('Customer not selected!'));">
				{elseif !$contents}
					<a href="javascript:alertDialog($t('Receipt have no items!'));">
				{else}
					<a href="javascript:saveReceipt(0);">
				{/if}
					{trans("Save")}
					<img src="img/save.gif">
				</a>
				{if !$customer && $receipt.o_type != "other" && $receipt.o_type != "advance"}
					<a href="javascript:alertDialog($t('Customer not selected!'));">
				{elseif !$contents}
					<a href="javascript:alertDialog($t('Receipt have no items!'));">
				{else}
					<a href="javascript:saveReceipt(1);">
				{/if}
					{trans("Save & Print")}
					<img src="img/print.gif">
				</a>
			</td>
		</tr>
	</tfoot>
</TABLE>
{else}
	<input type="hidden" name="notification" value="{if empty($receipt.notification)}0{else}1{/if}" form="additem">
{/if}
<script>

	$(function() {
		{if !$receipt.selected}
			$('form[name="setcustomer"] [name="receipt[regid]"]').focus();
		{/if}

		$("#customerid").change(function() {
			var customerId = $(this).val();
			var customerSelected = customerId.length > 0;

			$('[name="receipt[divisionid]"]').each(function() {
				if ($(this).is("select")) {
					$(this).prop("disabled", customerSelected);
					$("#division-row label").toggleClass("lms-ui-disabled", customerSelected);
				} else {
					$(this).prop("disabled", !customerSelected);
				}
			});
		});

		$("#division-selection").change(function() {
			$('[type="hidden"][name="receipt[divisionid]"]').val($(this).val());
		});
	});

</script>
{/block}
