{box title="Your subscriptions"}
{$show_speeds = ConfigHelper::checkConfig('userpanel.show_speeds')}
{$show_period = ConfigHelper::checkConfig('userpanel.show_period')}
{$show_discount = ConfigHelper::checkConfig('userpanel.show_discount')}
{$show_discounted_value = ConfigHelper::checkConfig('userpanel.show_discounted_value')}
{$show_invoice_flag = ConfigHelper::checkConfig('userpanel.show_invoice_flag')}
{$show_all_assignments = ConfigHelper::checkConfig('userpanel.show_all_assignments')}
{$speed_unit_type = ConfigHelper::getConfig('userpanel.speed_unit_type')}
{$speed_unit_aggregation_threshold = ConfigHelper::getConfig('userpanel.speed_unit_aggregation_threshold')}
{$count = 0}

<div class="table-responsive">
	<table class="table table-sm table-hover table-bordered table-striped table-condensed">
		<thead class="bg-secondary lms-userpanel-table-header">
			<th>{trans("Tariff")}</th>
			<th>{trans("Subscription")}</th>
			{if $show_discount}
			<th>{trans("Discount")}</th>
			{/if}
			<th>{trans("Accounting day")}</th>
			{if $show_speeds}
			<th>{trans("Downrate (ceil):")}</th>
			<th>{trans("Uprate (ceil):")}</th>
			{/if}
			{if $show_invoice_flag}
			<th>{trans("Invoice")}</th>
			{/if}
			{if $show_period}
			<th>{trans("Period")}</th>
			{/if}
		</thead>
		{$total_real_value = array()}
		{$total_real_disc_value = array()}
		{$total_real_downrate = 0}
		{$total_real_uprate = 0}
		{$total_real_downceil = 0}
		{$total_real_upceil = 0}
		{foreach $assignments as $assignment}
			{if ($assignment.tariffid || $assignment.liabilityid) && !$assignment.suspended
				&& ($show_all_assignments || ($assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)))}
				<tr>
					<td>{if ConfigHelper::checkConfig('userpanel.show_tariffname')}{$assignment.name}{/if}</td>
					{if $show_discounted_value}
						<td class="text-right text-nowrap">
							{moneyf($assignment.discounted_value, $assignment.currency)}
							{if !isset($total_real_value[$assignment.currency])}
								{$total_real_value[$assignment.currency] = 0}
							{/if}
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_value[$assignment.currency] = $total_real_value[$assignment.currency] + $assignment.discounted_value}
							{/if}
						</td>
					{else}
						<td class="text-right text-nowrap">
							{moneyf($assignment.real_value, $assignment.currency)}
							{if !isset($total_real_value[$assignment.currency])}
								{$total_real_value[$assignment.currency] = 0}
							{/if}
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_value[$assignment.currency] = $total_real_value[$assignment.currency] + $assignment.real_value}
							{/if}
						</td>
					{/if}
					{if $show_discount}
					<td class="text-right text-nowrap">
						{if $assignment.pdiscount > 0}
							{$assignment.pdiscount}%
						{else}
							{if $assignment.vdiscount > 0}
								{moneyf($assignment.vdiscount, $assignment.currency)}
							{else}
								-
							{/if}
						{/if}
						{if !isset($total_real_disc_value[$assignment.currency])}
							{$total_real_disc_value[$assignment.currency] = 0}
						{/if}
						{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
							{$total_real_disc_value[$assignment.currency] = $total_real_disc_value[$assignment.currency] + $assignment.real_disc_value}
						{/if}
					</td>
					{/if}
					<td class="text-right">{$assignment.payday}</td>
					{if $show_speeds}
					<td>
						{if empty($assignment.downrate)}
							-
						{else}
							{convert_to_units($assignment.downrate * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_downrate = $total_real_downrate + $assignment.downrate}
							{/if}
						{/if}
						({if empty($assignment.downceil)}
							-
						{else}
							{convert_to_units($assignment.downceil * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_downceil = $total_real_downceil + $assignment.downceil}
							{/if}
						{/if})
					</td>
					<td>
						{if empty($assignment.uprate)}
							-
						{else}
							{convert_to_units($assignment.uprate * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_uprate = $total_real_uprate + $assignment.uprate}
							{/if}
						{/if}
						({if empty($assignment.upceil)}
							-
						{else}
							{convert_to_units($assignment.upceil * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
							{if $assignment.datefrom <= $smarty.now && (empty($assignment.dateto) || $assignment.dateto >= $smarty.now)}
								{$total_real_upceil = $total_real_upceil + $assignment.upceil}
							{/if}
						{/if})
					</td>
					{/if}
					{if $show_invoice_flag}
					<td class="text-center">{if $assignment.invoice}{trans("yes")}{else}&nbsp;{/if}</td>
					{/if}
					{if $show_period}
					<td>
						{if $assignment.datefrom}{trans("from")} {$assignment.datefrom|date_format:"Y-m-d"}{/if}
						{if $assignment.dateto}{trans("to")} {$assignment.dateto|date_format:"Y-m-d"}{/if}
					</td>
					{/if}
				</tr>
				{$count = $count +1}
			{/if}
		{foreachelse}
			<tr>
				<td colspan="{if $show_speeds}{if $show_period}8{else}7{/if}{else}{if $show_period}6{else}5{/if}{/if}">
					{trans("You haven't got any assigned subscriptions.")}
				</td>
			</tr>
		{/foreach}
		{if $count > 1}
			<tr class="bg-primary text-white">
				<td>{trans("Total:")}</td>
				<td class="text-right">
					{foreach $total_real_value as $currency => $total_value}
						{if !$total_value@first}<br>{/if}
						{moneyf($total_value, $currency)}
					{/foreach}
				</td>
				{if $show_discount}
				<td class="text-right">
					({foreach $total_real_disc_value as $currency => $total_disc_value}{if !$total_disc_value@first}<br>{/if}{moneyf($total_disc_value, $currency)}{foreachelse}-{/foreach})
				</td>
				{/if}
				<td>&nbsp;</td>
				{if $show_speeds}
				<td>
					{if $total_real_downrate}
						{convert_to_units($total_real_downrate * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
					{else}
						-
					{/if}
					({if $total_real_downceil}
						{convert_to_units($total_real_downceil * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
					{else}
						-
					{/if})
				</td>
				<td>
					{if $total_real_uprate}
						{convert_to_units($total_real_uprate * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
					{else}
						-
					{/if}
					({if $total_real_upceil}
						{convert_to_units($total_real_upceil * $speed_unit_type, $speed_unit_aggregation_threshold, $speed_unit_type)}/s
					{else}
						-
					{/if})
				</td>
				{/if}
				{if $show_invoice_flag}
				<td>&nbsp;</td>
				{/if}
				{if $show_period}
				<td>&nbsp;</td>
				{/if}
			</tr>
		{/if}
	</table>
</div>
{/box}
