{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
<H1>{$layout.pagetitle}</H1>
<SCRIPT>
	function savereceipt(print)
	{
		var which = 0;
		$('form[name="additem"] [name^="which"]:checked').each(function() {
			which += parseInt($(this).val());
		});

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

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

	function additem()
	{
		document.additem.action = "?m=receiptedit&action=additem";
		document.additem.target = "";
		document.additem.submit();
	}

	function saveheader()
	{
		if (document.setcustomer.customer)
			if (document.setcustomer.customer.value != 0)
				document.setcustomer.customerid.value = document.setcustomer.customer.value;
		document.setcustomer.submit();
	}
</SCRIPT>
{$default_printpage = ConfigHelper::getConfig('receipts.default_printpage')}
<form name="setcustomer" id="setcustomer" method="POST" action="?m=receiptedit&action=setcustomer">
<INPUT type="submit" class="hiddenbtn">
<INPUT type="hidden" name="receipt[type]" value="{$receipt.type}">
<INPUT type="hidden" name="receipt[numberplanid]" value="{$receipt.numberplanid}">
<TABLE class="lmsbox">
    <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>
			<SELECT SIZE="1" NAME="receipt[regid]" {tip text="Select cash registry" trigger="regid"}>
				{foreach from=$cashreglist item=c}
				<OPTION VALUE="{$c.id}"{if $c.id == $receipt.regid} SELECTED{/if}>{$c.name|truncate:40:"&hellip;":true} ({$c.id|string_format:"%04d"})</OPTION>
				{/foreach}
			</SELECT>
		</TD>
	</TR>
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Receipt number:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP>
			<INPUT TYPE="TEXT" NAME="receipt[number]" VALUE="{$receipt.number}" SIZE="10" {tip text="Enter receipt number. WARNING! Changing this number can be DANGEROUS!" trigger="number"}>
			{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>
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{trans("Settlement date:")}</B>
		</TD>
		<TD style="width: 99%;" class="nobr">
			<INPUT TYPE="TEXT" NAME="receipt[cdate]" VALUE="{$receipt.cdate|date_format:"Y/m/d"}" SIZE="10" 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 $receipt.cdatewarning}
			<INPUT TYPE="HIDDEN" NAME="receipt[cdatewarning]" VALUE="1">
			{/if}
		</TD>
	</TR>
	<tr>
		<td class="bold nobr">
			{trans("Currency:")}
		</td>
		<td class="nobr">
			{currency_selection elementname="receipt[currency]" selected=$receipt.currency}
		</td>
	</tr>
	<TR>
		<TD WIDTH="1%">
			<B>{trans("Target:")}</B>
		</TD>
		<TD WIDTH="99%" NOWRAP>
		    <TABLE>
			<TR>
				<TD style="width: 1%;" class="nobr">
					<label>
						<INPUT type="radio" name="receipt[o_type]" value="customer" id="radio_customer" checked>
						{trans("customer")}:
					</label>
				</TD>
				<TD style="width: 99%;">
					{if isset($customer.id)}
						{customerlist form="setcustomer" customers=$customerlist selected=$customer.id version=2
							selectname="customer" inputname="customerid"
							customOnChange="document.getElementById('radio_customer').checked=true;"}
					{else}
					{customerlist form="setcustomer" customers=$customerlist selected=$receipt.customerid version=2
						selectname="customer" inputname="customerid"
						customOnChange="document.getElementById('radio_customer').checked=true;"}
					{/if}
				</TD>
			</TR>
			<TR>
			    <TD width="1%" nowrap>
				<INPUT TYPE="radio" NAME="receipt[o_type]" VALUE="advance" id="radio_advance" {if $receipt.o_type=="advance"} checked{/if}> <label for="radio_advance">{trans("advance")}:</label>
			    </TD>
			    <TD width="99%">
				<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>
			<TR>
			    <TD width="1%" nowrap>
				<INPUT TYPE="radio" NAME="receipt[o_type]" VALUE="other" id="radio_other" {if $receipt.o_type=="other"} checked{/if}> <label for="radio_other">{trans("other")}:</label>
			    </TD>
			    <TD width="99%">
				<INPUT TYPE="text" NAME="receipt[other_name]" VALUE="{$receipt.other_name}" SIZE="70" {if $receipt.type=="out"}{tip text="Enter recipient" trigger="other_name"}{else}{tip text="Enter payer" trigger="other_name"}{/if} onclick="document.getElementById('radio_other').checked=true">
			    </TD>
			</TR>
		    </TABLE>
		</TD>
	</TR>
	<TR>
		<TD WIDTH="100%" ALIGN="right" COLSPAN="2">
			<A HREF="javascript: saveheader();">{trans("Submit")} <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; {$customer.address} &nbsp; {$customer.zip} {$customer.city}
		        &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}
<P>
<TABLE class="lmsbox">
    <THEAD>
	<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=receiptedit&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>
	<FORM METHOD="POST" {*ACTION="?m=receiptedit&action=additem"*} NAME="additem">
	<INPUT type="submit" class="hiddenbtn">
	<TR>
		<TD WIDTH="1%" NOWRAP>
			<B>{counter}.</B>
		</TD>
		<TD WIDTH="94%">
			<INPUT TYPE="text" NAME="description" SIZE="80" {tip text="Enter item description"}>
		</TD>
		<TD>
			<INPUT TYPE="text" NAME="value" SIZE="10" {tip text="Enter item value"}>
		</TD>
		<TD>
			<A HREF="javascript:additem(); "><IMG SRC="img/save.gif" ALT="" {tip text="Add item"}></A>
		</TD>
	</TR>
	<TR>
		<TD COLSPAN="4" CLASS="{if $covenantlist}flr{else}fbottomu{/if}" ALIGN="RIGHT">
			<label>
				<INPUT type="checkbox" name="which[{$smarty.const.DOC_ENTITY_ORIGINAL}]"
						value="{$smarty.const.DOC_ENTITY_ORIGINAL}"
						{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}"
						{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" alt=""></A>&nbsp;
			{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" alt=""></A>&nbsp;

			{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/save.gif" alt=""></A>
		</TD>
	</TR>
	</FORM>
    </TFOOT>
</TABLE>
</P>
<script>

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

</script>
{/block}
