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

<style>

	#file-label {
		padding-top: 1.2em;
	}

</style>

<script>

	function Delete()
	{
		document.cashimport.action += '&action=delete';
		document.cashimport.submit();
	}

	function Save()
	{
		document.cashimport.action += '&action=save';
		document.cashimport.submit();
	}

</script>

<form name="cashimport" id="cashimport" action="?m=cashimport" method="post"></form>
<input type="submit" class="hiddenbtn" form="cashimport">
<table class="lmsbox lms-ui-background-cycle">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 25%;">
		<col style="width: 42%;">
		<col style="width: 32%;">
		<col style="width: 1%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{trans("ID:")}
			</td>
			<td>
				{trans("Source")}
			</td>
			<td>
				{trans("Date:")}
			</td>
			<td>
				<strong>{trans("Value:")}</strong>
			</td>
			<td>
				{trans("Customer:")}
			</td>
			<td>
				{trans("Description:")}
			</td>
			<td>
				<strong>{trans("Link to:")}</strong>
			</td>
			<td class="text-right nobr">
				<strong>{trans("Total:")} {$listdata.total}</strong>
			</td>
		</tr>
	</thead>
    <tbody class="lms-ui-multi-check">
		{foreach $divisions as $division}
			{if $division.list}
				<tr>
					<td colspan="5" class="dark">
						{icon name="division"}
						<strong>
							{$division.name|escape}
							{if !empty($division.label) && !empty($division_names[$division.name]) && count($division_names[$division.name]) > 1}
								({$division.label|escape})
							{/if}
						</strong>
					</td>
					<td colspan="3" class="dark text-right">
						{button icon="csv" label="Download (CSV)" href="?m=cashimport&action=csv&division={$division.id}"}
					</td>
				</tr>
				{foreach $division.list as $item}
				<tr>
					<td>
						{$item.id|string_format:"%04d"}
					</td>
					<td>
						<strong>{$item.sourcename|escape}</strong>
					</td>
					<td class="nobr">
						{$item.date|date_format:"Y-m-d"}
					</td>
					<td class="text-right nobr">
						<strong>{$item.value|money_format}</strong>
					</td>
					<td class="text-left">
						{if $item.customer}
							{$item.customer}
						{elseif $item.customerid}
							{$cust = $item.customerid}
							{$customerlist.$cust.customername}
						{/if}
					 </td>
					<td class="text-left">
						{$item.description}
					</td>
					<td class="text-left lms-ui-multi-check-ignore">
						{customerlist form="cashimport" customers=$customerlist selected=$item.customerid
							selectname="custid[{$item.id}]" inputname="customer[{$item.id}]" form="cashimport" version=2}
					</td>
					<td class="text-right">
						<input type="checkbox" name="marks[{$item.id}]" value="{$item.id}"
							class="lms-ui-multi-check" form="cashimport">
					</td>
				</tr>
				{/foreach}
			{/if}
		{/foreach}
		{if !$listdata.total}
			<tr>
				<td class="empty-table" colspan="8">
					{trans("No cash operations to import.")}
				</td>
			</tr>
		{/if}
	</tbody>
	<tfoot>
		<tr class="lms-ui-button-panel">
			<td class="nobr" colspan="4">
				<label>
					<input type="checkbox" name="allbox" class="lms-ui-multi-check-all" value="1" form="cashimport">
					{trans("Check All")}
				</label>
			</td>
			<td class="text-center nobr" colspan="2">
				{button icon="save" label="Download all (TXT)" href="?m=cashimport&action=txt"}
			</td>
			<td class="text-right nobr" colspan="2">
				{button icon="delete" label="Delete" onclick="Delete();"}
				{button icon="save" label="Save" onclick="Save();"}
				{button id="import-button" icon="import" label="<!do>Import"}
			</td>
		</tr>
	</tfoot>
</table>

{if !empty($error)}
<table class="lmsbox">
	<colgroup>
		<col style="width: 100%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				<strong>{trans("Errors and Duplicated Entries")}:</strong>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<table class="lmsbox-inner lms-ui-background-cycle">
					<colgroup>
						<col style="width: 1%;">
						<col style="width: 99%;">
					</colgroup>
					<thead>
						<tr>
							<td>
								<strong>{trans("Line:")}</strong>
							</td>
							<td>
								<strong>{trans("Content:")}</strong>
							</td>
						</tr>
					</thead>
					<tbody>
						{foreach $error as $filename => $file}
							{if (empty($file.lines))}
								{continue}
							{/if}
							{$lines = $file.lines}
							<tr class="lms-ui-distinguished-row" >
								<td colspan="2">
									{t a=$filename}File $a:{/t}
								</td>
							</tr>
							{foreach $lines as $key => $item}
								<tr>
									<td class="nobr">
										{$key}. <img src="img/{if is_array($item)}clone{else}warningon{/if}.gif">
									</td>
									<td>
										{if is_array($item)}
											<table width="100%" class="red">
												<colgroup>
													<col style="width: 1%;">
													<col style="width: 99%;">
												</colgroup>
												{if $item.date}
													<tr>
														<td>
															{trans("Date:")}
														</td>
														<td>
															{$item.date|date_format:"Y-m-d"}
														</td>
													</tr>
												{/if}
												{if $item.customer}
													<tr>
														<td>
															{trans("Customer:")}
														</td>
														<td>
															{$item.customer}{if $item.customerid} ({$item.customerid|string_format:"%04d"}){/if}
														</td>
													</tr>
												{/if}
												{if $item.value}
													<tr>
														<td>
															{trans("Value:")}
														</td>
														<td>
															{$item.value|money_format}
														</td>
													</tr>
												{/if}
												{if $item.comment}
													<tr>
														<td>
															{trans("Description:")}
														</td>
														<td>
															{$item.comment}
														</td>
													</tr>
												{/if}
											</table>
										{else}
											{$item}
										{/if}
									</td>
								</tr>
							{/foreach}
						{/foreach}
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>
{/if}

<form name="parser" id="parser" enctype="multipart/form-data" method="post" action="?m=cashimportparser"></form>
<input type="submit" class="hiddenbtn" form="parser">
<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
    <thead>
		<tr>
			<td colspan="2">
				<strong>{trans("Import from files:")}</strong>
			</td>
		</tr>
    </thead>
    <tbody>
		<tr>
			<td class="nobr valign-top" id="file-label">
				{icon name="attachment"}
				<strong>{trans("Import files:")}</strong>
			</td>
			<td class="nobr">
				{fileupload id="files" fileupload=$fileupload form="parser"}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="cashsource"}
				<strong>{trans("Source:")}</strong>
			</td>
			<td class="nobr">
				<select name="source" {tip text="Select import source (optional)" trigger="source"} form="parser">
					<option value="0">{trans("— auto —")}</option>
					{foreach $sourcelist as $item}
						<option value="{$item.id}">{$item.name}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="lms-ui-box-buttons">
				{button icon="save" label="Submit" onclick="document.parser.submit();"}
			</td>
		</tr>
    </tbody>
</table>

<table class="lmsbox">
	<colgroup>
		<col style="width: 100%;">
	</colgroup>
    <thead>
		<tr>
			<td>
				{icon name="attachment"}
				<strong>{trans("Packages:")}</strong>
			</td>
		</tr>
    </thead>
    <tbody>
		<tr class="lms-ui-button-panel">
			<td>
				<table id="cashimport-packages-datatable" class="lms-ui-datatable lms-ui-datatable-column-search lms-ui-datatable-column-toggle"
					   data-order='[[ 0, "desc" ]]' data-page-length="25" data-state-save="true" style="display: none;"
					   data-empty-table-message="{trans("There are no packages in database.")}"
					   width="100%">
					<colgroup>
						<col style="width: 1%;">
						<col style="width: 96%;">
						<col style="width: 1%;">
						<col style="width: 1%;">
						<col style="width: 1%;">
					</colgroup>
					<thead>
						<tr>
							<th class="bold nobr">{trans("Date:")}</th>
							<th class="text-left nobr">{trans("Filename:")}</th>
							<th class="text-center nobr" data-searchable="true" data-select-value="true">{trans("User:")}</th>
							<th class="text-center nobr" data-searchable="false">{trans("Records:")}</th>
							<th data-orderable="false" data-searchable="false">&nbsp;</th>
						</tr>
					</thead>
					<tbody>
						{foreach $sourcefiles as $item}
						<tr>
							<td class="bold nobr">{$item.idate|date_format:"Y-m-d H:i"}</td>
							<td><a href="?m=balancelist&sourcefileid={$item.id}">{$item.name}</a></td>
							<td class="text-center nobr">{$item.username}</td>
							<td class="text-center">{$item.count|default:0}</td>
							<td class="text-right">
								{button type="link" class="delete-cashpackage" tip="Delete" icon="delete" href="?m=cashpackagedel&id={$item.id}"}
							</td>
						</tr>
						{/foreach}
					</tbody>
				</table>
			</td>
		</tr>
    </tbody>
</table>
<script>

	$(function() {
		$('.delete-cashpackage').click(function () {
			confirmDialog($t("Are you sure, you want to delete this package and related operations?"), this).done(function () {
				location.href = $(this).attr('href');
			});
			return false;
		});

		$('#import-button').one('click', function() {
			document.cashimport.submit();
		});
	});

</script>
{/block}

