{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}

{* check DIRECTION *}
{assign var="direction_check" value="{if $listdata.direction == "desc"}asc{else}desc{/if}"}

{* check ORDER is set *}
{function order_check category=''}
	{if $listdata.direction == "asc" && $listdata.order == $category},desc{/if}
{/function}

<!--// $Id$ //-->
<h1>{$layout.pagetitle}</h1>

<style>

	.billing-table tr td,th {
		padding-right: 15px;
	}

	.billing-table tr td:last-child, th:last-child {
		padding-right: 0px;
	}

	#voip-billing-filter {
		display: flex;
		align-items: center;
	}

	#voip-billing-filter > :not(:first-child) {
		margin-left: 0.4em;
	}

	#choosefilter .lms-ui-customer-select-container {
		display: inline-flex;
	}

</style>

<form method="post" action="?m={$layout.module}" name="choosefilter" id="choosefilter">
	<input type="hidden" name="m" value="voipaccountbillinglist">
<form>

<table class="lmsbox billing-table lms-ui-background-cycle">
    <colgroup>
        <col style="width: 1%;">
        <col style="width: 1%;">
        <col style="width: 96%;">
        <col style="width: 1%;" span="8">
        {assign var="number_of_table_columns" value="12"}
    </colgroup>
    <thead>
        <tr {tip text="Click on column name to change sorting order"}>
            <th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=begintime{order_check category="begintime"}">{trans("Date")}</a>
                {if $listdata.order == "begintime"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

            <th colspan="2" nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=caller_name{order_check category="caller_name"}">{trans("Customer:")}</a>
                {if $listdata.order == "caller_name"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}

				<a href="?m=voipaccountbillinglist&amp;o=callee_name{order_check category="callee_name"}">{trans("Customer:")}</a>
                {if $listdata.order == "callee_name"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>
            <th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=caller{order_check category="caller"}">{trans("Caller:")}</a>
                {if $listdata.order == "caller"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>
            <th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=callee{order_check category="callee"}">{trans("Callee:")}</a>
                {if $listdata.order == "callee"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

			<th nowrap>
                <a href="?m=voipaccountbillinglist&o=totaltime{order_check category="totaltime"}">{trans("<!voip>Total time")}</a>
                {if $listdata.order == "totaltime"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

			<th nowrap>
                <a href="?m=voipaccountbillinglist&o=billedtime{order_check category="billedtime"}">{trans("<!voip>Billed time")}</a>
                {if $listdata.order == "billedtime"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

			<th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=direction{order_check category="direction"}">{trans("<!voip>Direction")}</a>
                {if $listdata.order == "direction"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

			<th nowrap>
				<a href="?m=voipaccountbillinglist&o=type{order_check category="type"}">{trans("<!voip>Type")}</a>
				{if $listdata.order == "type"}
					<img src="img/{$direction_check}_order.gif" alt="">
				{/if}
			</th>

			<th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=status{order_check category="status"}">{trans("<!voip>Status")}</a>
                {if $listdata.order == "status"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

			<th nowrap>
                <a href="?m=voipaccountbillinglist&amp;o=price{order_check category="price"}">{trans("Price:")}</a>
                {if $listdata.order == "price"}
					<img src="img/{$direction_check}_order.gif" alt="">
                {/if}
            </th>

            <th align="right" class="nobr">
                {t a=$total}Total: $a{/t}
            </th>
        </tr>
		<tr>
			<td colspan="{$number_of_table_columns}">
				<div id="voip-billing-filter">
					{customerlist
						form="choosefilter"
						customers=$voipownerlist.name
						selected=$listdata.fvownerid
						version=2
						inputname="fvownerid"
						customOnChange="CustomerChanged($(this).val());"
					}

					<span>{trans("VoIP account")}</span>
					{include file="voipaccount/voipaccounts.html" form="choosefilter"}

					<span class="lms-ui-date-period-container">
						<span>{trans("Period")}</span>
						{date_period_preset from="#frangefrom" to="#frangeto"}
						<span>{trans("from")}</span>
						<input
							type="text"
							name="frangefrom"
							id="frangefrom"
							form="choosefilter"
							size="10"
							maxlength="20"
							value="{if $listdata.frangefrom > 0}{$listdata.frangefrom|date_format:"Y/m/d"}{/if}"
							{tip class="lms-ui-date" text="Enter date in YYYY/MM/DD format (empty field means no limit) or click to choose it from calendar"}>
						<span>{trans("to")}</span>
						<input
							type="text"
							name="frangeto"
							id="frangeto"
							form="choosefilter"
							size="10"
							maxlength="20"
							value="{if $listdata.frangeto > 0}{$listdata.frangeto|date_format:"Y/m/d"}{/if}"
							{tip class="lms-ui-date" text="Enter date in YYYY/MM/DD format (empty field means no limit) or click to choose it from calendar"}>&nbsp;
					</span>

					<span>{trans("<!voip>Direction")}</span>
					<select size="1" name="fdirection" form="choosefilter">
						<option value="">{trans("— all —")}</option>
						<option value="{$smarty.const.BILLING_RECORD_DIRECTION_INCOMING}"
							{if $listdata.fdirection == $smarty.const.BILLING_RECORD_DIRECTION_INCOMING} selected{/if}>{trans("<!voip>incoming")}</option>
						<option value="{$smarty.const.BILLING_RECORD_DIRECTION_OUTGOING}"
							{if $listdata.fdirection == $smarty.const.BILLING_RECORD_DIRECTION_OUTGOING} selected{/if}>{trans("<!voip>outgoing")}</option>
					</select>

					<span>{trans("<!voip>Type")}</span>
					<select size="1" name="ftype" form="choosefilter">
						<option value="">{trans("— all —")}</option>
						<option value="{$smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}"
							{if is_numeric($listdata.ftype) && $listdata.ftype == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL} selected{/if}>{trans("<!voip>voice call")}</option>
						<option value="{$smarty.const.BILLING_RECORD_TYPE_VIDEO_CALL}"
							{if is_numeric($listdata.ftype) && $listdata.ftype == $smarty.const.BILLING_RECORD_TYPE_VIDEO_CALL} selected{/if}>{trans("<!voip>video call")}</option>
						<option value="{$smarty.const.BILLING_RECORD_TYPE_SMS}"
							{if $listdata.ftype == $smarty.const.BILLING_RECORD_TYPE_SMS} selected{/if}>{trans("<!voip>SMS")}</option>
						<option value="{$smarty.const.BILLING_RECORD_TYPE_MMS}"
							{if $listdata.ftype == $smarty.const.BILLING_RECORD_TYPE_MMS} selected{/if}>{trans("<!voip>MMS")}</option>
						<option value="{$smarty.const.BILLING_RECORD_TYPE_DATA_TRANSFER}"
							{if $listdata.ftype == $smarty.const.BILLING_RECORD_TYPE_DATA_TRANSFER} selected{/if}>{trans("<!voip>data transfer")}</option>
					</select>

					<span>{trans("<!voip>Status")}</span>
					<select size="1" name="fstatus"	form="choosefilter">
						<option value="">{trans("— all —")}</option>
						<option value="{$smarty.const.BILLING_RECORD_STATUS_ANSWERED}"
							{if $listdata.fstatus == $smarty.const.BILLING_RECORD_STATUS_ANSWERED} selected{/if}>{trans("answered")}</option>
						<option value="{$smarty.const.BILLING_RECORD_STATUS_NO_ANSWER}"
							{if $listdata.fstatus == $smarty.const.BILLING_RECORD_STATUS_NO_ANSWER} selected{/if}>{trans("no answer")}</option>
						<option value="{$smarty.const.BILLING_RECORD_STATUS_BUSY}"
							{if $listdata.fstatus == $smarty.const.BILLING_RECORD_STATUS_BUSY} selected{/if}>{trans("<!voip>busy")}</option>
						<option value="{$smarty.const.BILLING_RECORD_STATUS_SERVER_FAILED}"
							{if $listdata.fstatus == $smarty.const.BILLING_RECORD_STATUS_SERVER_FAILED} selected{/if}>{trans("server error")}</option>
						<option value="{$smarty.const.BILLING_RECORD_STATUS_UNKNOWN}"
								{if $listdata.fstatus == $smarty.const.BILLING_RECORD_STATUS_UNKNOWN} selected{/if}>{trans("unknown")}</option>
					</select>
					{button type="link" icon="next" onclick="$('#choosefilter').submit()"}
				</div>
			</td>
		</tr>
        {if $pagination->getTotal() != 0}
		<TR>
			<TD class="lms-ui-pagination" colspan="{$number_of_table_columns}">
				{include file="pagination.html"}
			</TD>
		</TR>
		{/if}
    </thead>
    <tbody>
        {foreach $billings as $record}
			<tr class="highlight">

				<td nowrap>
					{$record.begintime|date_format:"Y-m-d H:i:s"}
				</td>

				<td nowrap>
					{if !empty($record.callerownerid)}
						<a href="?m=customerinfo&id={$record.callerownerid}"
							class="lms-ui-hint-rollover"
							data-url="?m=customerinfoshort&id={$record.callerownerid}">
							{ucfirst($record.caller_lastname|lower)} {ucfirst($record.caller_name)}
						</a>
					{else}
						{trans("Customer from outside")}
					{/if}
				</td>

				<td nowrap>
					{trans("to")}

					{if !empty($record.calleeownerid)}
						<a href="?m=customerinfo&id={$record.calleeownerid}"
							class="lms-ui-hint-rollover"
							data-url="?m=customerinfoshort&id={$record.calleeownerid}">
							{ucfirst($record.callee_lastname|lower)} {ucfirst($record.callee_name)}
						</a>
					{else}
						{trans("customer from outside")}
					{/if}
				</td>

				<td>
					{if !empty($record.callerownerid)}
						<a href="?m=voipaccountinfo&amp;id={$record.callervoipaccountid}">{$record.caller}</a>
					{else}
						{$record.caller}
					{/if}
				</td>

				<td>
					{if !empty($record.calleeownerid)}
						<a href="?m=voipaccountinfo&amp;id={$record.calleevoipaccountid}">{$record.callee}</a>
					{elseif !empty($record.callee)}
						{$record.callee}
					{else}
						-
					{/if}
				</td>

				<td>
					{if $record.type == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}
						{$record.totaltime|duration_format}
					{else}
						-
					{/if}
				</td>

				<td>
					{if $record.type == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}
						{$record.billedtime|duration_format}
					{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_DATA_TRANSFER}
						{$record.billedtime|size_format}
					{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_SMS}
						{$record.billedtime}
					{else}
						-
					{/if}
				</td>

				<td colspan="2" class="nobr">
					{if $record.type == $smarty.const.BILLING_RECORD_TYPE_DATA_TRANSFER}
						{trans("<!voip>data transfer")}
					{elseif $record.direction == $smarty.const.BILLING_RECORD_DIRECTION_OUTGOING}
						{if $record.type == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}
							{trans("<!voip>outgoing call")}
						{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_SMS}
							{trans("<!voip>outgoing SMS")}
						{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_MMS}
							{trans("<!voip>outgoing MMS")}
						{/if}
					{elseif $record.direction == $smarty.const.BILLING_RECORD_DIRECTION_INCOMING}
						{if $record.type == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}
							{trans("<!voip>incoming call")}
						{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_SMS}
							{trans("<!voip>incoming SMS")}
						{elseif $record.type == $smarty.const.BILLING_RECORD_TYPE_MMS}
							{trans("<!voip>incoming MMS")}
						{/if}
					{/if}
				</td>

				<td nowrap>
					{if $record.status == $smarty.const.BILLING_RECORD_STATUS_BUSY}
						{trans("<!voip>busy")}
					{elseif $record.status == $smarty.const.BILLING_RECORD_STATUS_ANSWERED}
						{trans("answered")}
					{elseif $record.status == $smarty.const.BILLING_RECORD_STATUS_NO_ANSWER}
						{trans("no answer")}
					{elseif $record.status == $smarty.const.BILLING_RECORD_STATUS_SERVER_FAILED}
						{trans("server error")}
					{elseif $record.status == $smarty.const.BILLING_RECORD_STATUS_UNKNOWN}
						{trans("unknown")}
					{/if}
				</td>

				<td nowrap>
					{$record.price|money_format}
				</td>

				<td align="right" class="nobr">
					{if $record.billedtime && ($record.caller_flags || $record.callee_flags)}
						<a href="?m=voipcallrecording&id={$record.id}&download=1"><img src="img/save.gif"></a>
						{documentview id="{$record.id}"
						              type="audio/mp3"
						              name="{trans("ID:")} {$record.id}, {trans("Date:")} {$record.begintime|date_format:"Y-m-d H:i:s"}"
	                                  url="?m=voipcallrecording&id={$record.id}"
	                                  text="<span class=\"ui-icon ui-icon-volume-on\"></span>"}
					{/if}
					<a href="?m=voipaccountbillinginfo&amp;id={$record.id}">
						<img src="img/info.gif" alt="{trans("Info")}" title="{trans("Details")}">
					</a>
				</td>
			</tr>
        {foreachelse}
			<tr>
				<td class="empty-table" colspan="{$number_of_table_columns}">
					{trans("No CDR records found in database.")}
				</td>
			</tr>
        {/foreach}
    </tbody>
    <tfoot>
        {if $pagination->getTotal() != 0}
		<TR>
			<TD class="lms-ui-pagination" colspan="{$number_of_table_columns}">
				{include file="pagination.html"}
			</TD>
		</TR>
		{/if}

		{$sum = 0}
		{$call_time = 0}
		{$talk_time = 0}
		{$total_transfer = 0}
		{$billed_transfer = 0}
		{$smses = 0}
		{$mmses = 0}

		{foreach $billings as $b}
			{$sum = $sum + $b.price}
			{if $b.type == $smarty.const.BILLING_RECORD_TYPE_VOICE_CALL}
				{$call_time = $call_time + $b.totaltime}
				{$call_talk = $call_talk + $b.billedtime}
			{elseif $b.type == $smarty.const.BILLING_RECORD_TYPE_DATA_TRANSFER}
				{$total_transfer = $total_transfer + $b.totaltime}
			{elseif $b.type == $smarty.const.BILLING_RECORD_TYPE_SMS}
				{$smses = $smses + $b.totaltime}
			{elseif $b.type == $smarty.const.BILLING_RECORD_TYPE_MMS}
				{$mmses = $mmses + $b.totaltime}
			{/if}
		{/foreach}

        <tr>
            <td class="text-right bold" colspan="{$number_of_table_columns-7}">
				{trans("<!voip>Time on page")}<br>
				{trans("<!voip>Time")}
				{if $stats.total_transfer}
					<br>
					{trans("<!voip>Transfer on page")}
					<br>
					{trans("<!voip>Transfer")}
				{/if}
				{if $stats.smses}
					<br>
					{trans("<!voip>Number of SMS on page")}
					<br>
					{trans("<!voip>Number of SMS")}
				{/if}
				{if $stats.mmses}
					<br>
					{trans("<!voip>Number of MMS on page")}
					<br>
					{trans("<!voip>Number of MMS")}
				{/if}
            </td>
            <td>
				{$call_time|duration_format}
				<br>
				{$stats.totaltime|duration_format}
				{if $stats.total_transfer}
					<br>
					—
					<br>
					—
				{/if}
				{if $stats.smses}
					<br>
					—
					<br>
					—
				{/if}
				{if $stats.mmses}
					<br>
					—
					<br>
					—
				{/if}
            </td>

            <td>
				{$call_talk|duration_format}
				<br>
				{$stats.billedtime|duration_format}
				{if $stats.total_transfer}
					<br>
					{$total_transfer|size_format}
					<br>
					{$stats.total_transfer|size_format}
				{/if}
				{if $stats.smses}
					<br>
					{$smses}
					<br>
					{$stats.smses}
				{/if}
				{if $stats.mmses}
					<br>
					{$mmses}
					<br>
					{$stats.mmses}
				{/if}
            </td>
            <td colspan="4" class="text-right">
                {$sum|money_format}<br>
                {$stats.price|money_format}
            </td>
            <td class="bold text-center">
                {if $total}{$total}{else}0{/if}<br>
                {intval($stats.cnt)}
            </td>
        </tr>
    </tfoot>
</table>

<script>
	function CustomerChanged(str) {
		$.ajax({
			url: '?m=voipaccountbillinglist',
			type: "POST",
			dataType: "json",
			data: { str : str },
			success:
				function(response) {
					var selectbox = $('#customernumberselect');
					selectbox.replaceWith( response );
				},
		});
	}
</script>

{/block}
