<?php

use app\libraries\CommonHelper;

$totalpayment = 0;
?>
<?php if ($isinvoiceinmerge): ?>
	<!-- PAYMENT for current receipt items -->
	<table style="font-size:6px;border-bottom: 1px dashed <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>;">
		<?php foreach ($items as $item):
//			log_message('debug','userid:'.$item['user_id'].';cash:'.$item['cash_amount'].';cheque:'.$item['cheque_amount']);
			?>
			<?php if ($item['user_id'] == $userId): //log_message('debug','userid:'.$item['user_id'].';cash:'.$item['cash_amount'].';cheque:'.$item['cheque_amount']);?>
			<?php if ($item['cash_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 現金</span></td>
					<td style="width: 15%; line-height: 120%;" align="center">
						<span><?php echo CommonHelper::money($item['cash_amount']); ?></span></td>
				</tr>
			<?php $totalpayment+=$item['cash_amount']; ?>
			<?php endif; ?>
			<?php if ($item['cheque_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 支票</span></td>				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span><?php echo $item['cheque_bank_name']; ?><?php echo $item['cheque_no']; ?></span></td>
					<td style="width: 15%; line-height: 120%;" align="center"><span><?php echo CommonHelper::money($item['cheque_amount']); ?></span></td>
				</tr>
				<?php $totalpayment+=$item['cheque_amount']; ?>
			<?php endif; ?>
			<?php if ($item['atm_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 80%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 入數紙</span></td>
					<td style="width: 15%; line-height: 120%;" align="center">
						<span><?php echo CommonHelper::money($item['atm_amount']); ?></span></td>
				</tr>
				<?php $totalpayment+=$item['atm_amount']; ?>
			<?php endif; ?>
		<?php endif; ?>
		<?php endforeach; ?>
	</table>
<?php else: ?>
	<!-- PAYMENT -->
	<table style="font-size:6px;border-bottom: 1px dashed <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>;">
		<?php foreach ($items as $item):
//		log_message('debug','cash:'.$item['cash_amount'].';cheque:'.$item['cheque_amount']);
			?>
			<?php $paydate = date('Y-m-d g:ia', $item['payment_date']);
			$totalpayment += floatval(trim($item['amount_paid'])); ?>
			<tr>
				<td colspan="2" style="width: 100%; line-height: 30%;"></td>
			</tr>
			<?php if ($item['payment_type_id'] == PaymentType::Cheque):$payment = CommonHelper::currency($item['cheque_amount']);
			$paymenttypename = '支票'; ?>
			<tr>
				<td style="width: 85%; line-height: 100%;" align="left">
					<span>付款方式：<?php echo $paydate; ?> <?php echo $paymenttypename; ?></span></td>
			</tr>
			<tr>
				<td style="width: 85%; line-height: 100%;" align="left">
					<span><?php echo $item['cheque_bank_name']; ?><?php echo $item['cheque_no']; ?></span></td>
				<td style="width: 15%; line-height: 120%;" align="center"><span><?php echo $payment; ?></span></td>
			</tr>
		<?php else: $paymenttypename = $item['payment_type_id'] == PaymentType::Cash ? '現金' : '入數紙';
			$payment = $item['payment_type_id'] == PaymentType::Cash ? CommonHelper::currency($item['cash_amount']) : CommonHelper::currency($item['atm_amount']); ?>
			<tr>
				<td style="width: 85%; line-height: 80%;" align="left">
					<span>付款方式：<?php echo $paydate; ?> <?php echo $paymenttypename; ?></span></td>
				<td style="width: 15%; line-height: 120%;" align="center"><span><?php echo $payment; ?></span></td>
			</tr>
		<?php endif; ?>
		<?php endforeach; ?>
	</table>
<?php endif; ?>
<!-- TOTAL AMOUNT -->
<table style="font-size:6px;border-bottom: 1px dashed <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>">
	<tr>
		<td style="width: 100%; line-height: 120%;"></td>
	</tr>
	<tr>
		<td style="width: 84%; line-height: 80%;" align="right"><span style="color: <?php echo $THEME_COLOR; ?>;">付款總額 TOTAL PAYMENT: </span>
		</td>
		<td style="width: 16%; line-height: 80%;" align="center">
			<table style="border-bottom: 1px solid <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>;">
				<tr>
					<td align="center"><span id="totalpayment" data-amount="<?=$totalpayment?>"
								style="line-height: 80%;"><?php echo CommonHelper::currency($totalpayment); ?></span></td>
				</tr>
				<tr>
					<td style="line-height: 50%"></td>
				</tr>
			</table>
		</td>
	</tr>
	<!-- Double line, each line will be illustrated by table border-bottom property-->
	<!-- Spaces between line can be adjusted by line-height -->
	<tr>
		<td style="width: 100%; line-height: 5%;" align="center"></td>
	</tr>
	<tr>
		<td style="width: 84%; line-height: 15%;"></td>
		<td style="width: 16%; line-height: 15%;">
			<table style="border-bottom: 1px solid <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>;">
				<tr>
					<td></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr><td colspan="2" style="line-height: 120%;">&nbsp;</td></tr>
<!--	<tr>-->
<!--		<td style="width: 85%; line-height: 120%;" align="left">-->
<!--			<span>之前已付款額：--><?php //echo CommonHelper::money($HISTORIC_PAYMENT); ?><!--</span></td>-->
<!--	</tr>-->
</table>

<?php if(!empty($mergeditems)):?>
	<!-- PAYMENT for merged receipt items -->
	<table style="font-size:6px;border-bottom: 1px dashed <?php echo $THEME_THEAD_COLOR_BORDER_BOTTOM; ?>;">
		<tr><td colspan="2">合併發票之前已繳款項明細：</td></tr>
		<?php foreach ($mergeditems as $item):
//			log_message('debug','userid:'.$item['user_id'].';cash:'.$item['cash_amount'].';cheque:'.$item['cheque_amount']);
			?>
			<?php if ($item['user_id'] == $userId): //log_message('debug','userid:'.$item['user_id'].';cash:'.$item['cash_amount'].';cheque:'.$item['cheque_amount']);?>
			<?php if ($item['cash_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 現金</span></td>
					<td style="width: 15%; line-height: 120%;" align="center">
						<span><?php echo CommonHelper::money($item['cash_amount']); ?></span></td>
				</tr>
				<?php //$totalpayment+=$item['cash_amount']; ?>
			<?php endif; ?>
			<?php if ($item['cheque_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 支票</span></td>				</tr>
				<tr>
					<td style="width: 85%; line-height: 100%;" align="left">
						<span><?php echo $item['cheque_bank_name']; ?><?php echo $item['cheque_no']; ?></span></td>
					<td style="width: 15%; line-height: 120%;" align="center"><span><?php echo CommonHelper::money($item['cheque_amount']); ?></span></td>
				</tr>
				<?php //$totalpayment+=$item['cheque_amount']; ?>
			<?php endif; ?>
			<?php if ($item['atm_amount'] > 0): ?>
				<tr>
					<td colspan="2" style="width: 100%; line-height: 30%;"></td>
				</tr>
				<tr>
					<td style="width: 85%; line-height: 80%;" align="left">
						<span>付款方式：<?php echo date('Y-m-d g:ia',$item['payment_date']); ?> 入數紙</span></td>
					<td style="width: 15%; line-height: 120%;" align="center">
						<span><?php echo CommonHelper::money($item['atm_amount']); ?></span></td>
				</tr>
				<?php //$totalpayment+=$item['atm_amount']; ?>
			<?php endif; ?>
		<?php endif; ?>
		<?php endforeach; ?>
	</table>
<?php endif;?>
