<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

// Details Layout Begins

	$paymentTypes = array(
		"SP" => "Receipt #",
		"SC" => "Credit Note #",
		"JV" => "Journal #",
		"PP" => "Payment #",
		"PD" => "Debit Note #"
	);

	$roundAmount = formatAmount($roundAmount, $currencyCode, '','',$currFraction);
	$totAmount = formatAmount($totAmount, $currencyCode, '','',$currFraction);
	$totTax = formatAmount($totTax, $currencyCode, '','',$currFraction);
	$tdsAmount = empty((float)$tdsAmount) ? "" : formatAmount($tdsAmount, $currencyCode, '','',$currFraction);
	$dueAmount = formatAmount($dueAmount, $currencyCode, '','',$currFraction);

	$taxableAmount = formatAmount($taxableAmount, $currencyCode, false, true,$currFraction);
	$excTaxableAmount = formatAmount($excTaxableAmount, $expCurrencyCode, false, true,$currFraction);

	if (!empty($company['invsign']))
	{
		if ($isPrint && !$signOnPrint) {
			$company['invsign'] = null;
		}

		if ((!$isPrint || $is_email) && !$signMailPdf) {
			$company['invsign'] = null;
		}
	}
?>
<div class="detailTable">
	<table align='left' cellspacing="0" cellpadding="0"><?php
		$totWid = 100;
		$cols = 4;
		$totWid -= 37;

		if ($isSlNoCol) {
			$totWid -= 6;
			$cols++;
		}
		if ($showTax)
		{
			$totWid -= 10;
			$cols++;
		}
		if ($hasHSN)
		{
			$totWid -= 10;
			$cols++;
		}
		if($hasDiscount)
		{
			$totWid -= 8;
			$cols++;
		}
		if ($isExport && $currencyCode != $expCurrencyCode)
		{
			$totWid -= 17;
			$cols++;
		}

		if ($isSlNoCol) {
			echo '<col style="width:6%">';
		}
		echo '<col style="width:' . $totWid . '%">';
		if ($showTax)
		{
			echo '<col style="width:10%">';
		}
		if ($hasHSN)
		{
			echo '<col style="width:10%">';
		}
		echo '<col style="width:10%">';
		echo '<col style="width:10%">';
		if($hasDiscount)
		{
			echo '<col style="width:8%">';
		}
		if ($isExport && $currencyCode != $expCurrencyCode)
		{
			echo '<col style="width:17%">';
		}
		echo '<col style="width:17%">';
		?>
		<thead class="bgcolor">
			<tr><?php
				$expCurLable = '';
				$curLable = '';

				if($isExport && $currencyCode != $expCurrencyCode) {
					$curLable = '<br><small>(' . $currencyCode . ')</small>';
					$expCurLable = '<br><small>(' . $expCurrencyCode . ')</small>';
				}

				if ($isSlNoCol)
				{
					echo '<th class="">'.mylang("SNo").'</th>';
				}
				echo '<th class="">'.mylang("ItemDescription").'</th>';
				if ($hasHSN)
				{
					echo '<th class="">'.mylang("HSN/SAC").'</th>';
				}

				if ($showTax)
				{
					echo '<th class="">'.mylang("Tax").'</th>';
				}
				echo '<th class="">'.mylang("Qty").'</th>';
				echo '<th class="ar">'.mylang("Rate") . ($isExport && $currencyCode != $expCurrencyCode ? $expCurLable : $curLable) . '</th>';

				if ($hasDiscount)
				{
					echo '<th class="ar">'.mylang("Discount").'</th>';
				}
				if ($isExport && $currencyCode != $expCurrencyCode)
				{
					echo '<th class="ar nbr">'.mylang("Amount") . $expCurLable . '</th>';
				}
				echo '<th class="ar">'.mylang("Amount") . $curLable . '</th>';
			?></tr>
		</thead>
		<tbody><?php
			foreach ($details as $key => $val)
			{
				//display printname if given
				if(!empty($val['printName']) && trim($val['printName'])){
					$val['itemName'] =  trim($val['printName']);
				}
				$val['qty'] = $val['qty'] > 0 ? number_format($val['qty']) : '';
				$val['rate'] = formatAmount($val['rate'], $currencyCode, false, true,$rateFraction);
				$val['discAmount'] = formatAmount($val['discAmount'], $currencyCode, false, true,$rateFraction);
				$val['taxableAmount'] = formatAmount($val['taxableAmount'], $currencyCode, false, true,$currFraction);
				$val['excTaxableAmount'] = formatAmount($val['excTaxableAmount'], $expCurrencyCode, false, true,$currFraction);

				if ($val['discPercentage']) {
					$val['discPercentage'] = round($val['discPercentage'], 2);
				}

			?><tr><?php
				if ($isSlNoCol)
				{
					?><td class="ac vam"><?php echo $key+1; ?></td><?php
				}
				?><td class=""><?php
					echo $val['itemName'];
					if ($isItemDesc)
					{
						?><br><small class="desc"><?php
							echo $val['itemDesc'];
						?></small><?php
					}
				?></td><?php
				if ($hasHSN)
				{
					echo '<td class="">' . $val['itemHS'] . '</td>';
				}
				if ($showTax)
				{
					echo '<td class="">' . $val['taxName'] . '</td>';
				}
				?>
				<td class="ar"><?php echo $val['qty']; ?><?php if (!empty($units)) { ?> <sub><small><?php echo $val['unit']; ?></small></sub></td><?php } ?>
				<td class="ar"><?php echo $val['rate']; ?></td><?php
				if($hasDiscount)
				{
					/*?><td class="ar"><?php echo $val['_discAmount']; ?><br><?php echo $val['discPercentage']; ?>%</td><?php*/
					?><td class="ar"><?php echo ($val['discPercentage'] ? $val['discPercentage'] . '%' : ''); ?></td><?php
				}
				if ($isExport && $currencyCode != $expCurrencyCode)
				{
					?><td class="ar"><?php echo $val['excTaxableAmount']; ?></td><?php
				}
				?><td class="ar"><?php echo $val['taxableAmount']; ?></td>
			</tr><?php
			}
		if (!empty($charges))
		{
			if ($isExport && $currencyCode != $expCurrencyCode)
			{
				$cols--;
			}
			foreach($charges as $key => $charge_item)
			{
				$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, false, true,$currFraction);
			?><tr>
				<td class="ar" colspan="<?php echo $cols-1; ?>"><?php echo $charge_item['itemName']; ?></td><?php
			if ($isExport && $currencyCode != $expCurrencyCode)
			{
				?><td class="ar"><?php echo $charge_item['rate']; ?></td><?php
			}
				?><td class="ar"><?php echo $charge_item['taxableAmount']; ?></td>
			</tr><?php
			}
		}
		?></tbody>
	</table>
<?php

	// Details Layout End
	// Footer Layout Begins

	$rowspan = 1;
	if ($charges) {
		// $rowspan += count($charges);
	}
	if ($tax) {
		$rowspan += count($tax);
	}
	if ($roundAmount) {
		$rowspan++;
	}
	if ($tdsPercentage) {
		$rowspan++;
	}

	$rowspan++; // Total Amt

	if ($trans_payment) {
		$rowspan += count($trans_payment);
	}
	if ($dueAmount) {
		$rowspan++;
	}

?>
	<table align="left" cellspacing="0" cellpadding="0" class="footTable nbh"><?php
	if (!($isExport && $currencyCode != $expCurrencyCode))
	{
		?><col style="width:50%;">
		<col style="width:30%;">
		<col style="width:20%;"><?php
	}
	else
	{
		?><col style="width:40%;">
		<col style="width:26%;">
		<col style="width:17%;">
		<col style="width:17%;"><?php
	}
		?><tbody>
			<tr class="">
				<td class="vat" rowspan="<?php echo $rowspan; ?>">
					<p class="nm">Amount (in words) :<br><?php
							if ($isExport && $currencyCode != $expCurrencyCode && $expCurrencyCode)
							{
								echo $expCurrencyCode . ' ' . $exp_amt_str . " only";
							}
							else
							{
								echo ' ' . $amt_str . " only";
							}
					?></p><br><?php
				if($isBankDetail && $bankAccId && $accType != 'R')
				{
					?><table class="accInfo noborder nopadding" border="0" cellspacing="0" cellpadding="0">
						<tbody>
							<tr>
								<td colspan="5">
									<p class="nm">Company's Bank Details</p>
								</td>
							</tr>
							<tr>
								<td>A/C No</td>
								<td>&nbsp;: <b><?php echo $accNumber; ?></b></td>
								<td>&nbsp;</td>
								<td>Bank</td>
								<td>&nbsp;: <b><?php echo $bankName; ?></b></td>
							</tr>
							<tr>
								<td>IFS Code</td>
								<td>&nbsp;: <b><?php echo $ifscRouteCode; ?></b></td>
								<td>&nbsp;</td>
								<td>Branch</td>
								<td>&nbsp;: <b><?php echo $branchName; ?></b></td>
							</tr>
						</tbody>
					</table><br><?php
				}
				if($notes)
				{
					?><pre class="nm"><?php echo $notes; ?></pre><?php
				}
				?></td>
				<td class="ar"><b><?php echo mylang("SubTotal")?></b></td><?php
				if ($isExport && $currencyCode != $expCurrencyCode)
				{
					?><td class="ar nbr"><b><?php echo $excTaxableAmount; ?></b></td><?php
				}
				?><td class="ar"><b><?php echo $taxableAmount; ?></b></td>
			</tr><?php
			/*
			if (!empty($charges))
			{
				foreach($charges as $key => $charge_item)
				{
					$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, false, true);
			?><tr>
				<td class="ar"><?php echo $charge_item['itemName']; ?></td><?php
			if ($isExport)
			{
				?><td class="ar"><?php echo $charge_item['rate']; ?></td><?php
			}
				?>
				<td class="ar"><?php echo $charge_item['taxableAmount']; ?></td>
			</tr><?php
				}
			}*/
			if (!empty($tax))
			{
				foreach ($tax as $key => $tax_item)
				{
			?><tr><?php
			
				if(empty($tax_item['isReverse']))
				{
					?><td class="ar"><?php echo $tax_item['taxTypeCode']; ?> @ <b><?php echo $tax_item['taxRate'] . ($tax_item['taxCalc'] == "Q" ? "/Unit" : "%") ; ?></b></td>
					<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $tax_item['taxValue']; ?></b></td><?php
				}
				else
				{
					?><td class="ar"><?php echo $tax_item['taxTypeCode']; ?> @ <b><?php echo $tax_item['taxRate'] . ($tax_item['taxCalc'] == "Q" ? "/Unit" : "%") ; ?></b></td>
					<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $tax_item['taxValue']; ?></b></td><?php
				}
			?></tr><?php
				}
			}

			if(!empty($roundAmount))
			{
			?><tr>
				<td class="ar">Round Off </td>
				<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $roundAmount; ?></b></td>
			</tr><?php
			}

			if(!empty($tdsPercentage) && $tdsPercentage != 0 && !empty($tdsAmount))
			{
			?><tr>
				<td class="ar">TDS @ <b><?php echo $tdsPercentage; ?>%</b></td>
				<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $tdsAmount; ?></b></td>
			</tr><?php
			}
			?><tr>
				<td class="ar"><b>Total</b></td>
				<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $totAmount; ?></b></td>
			</tr><?php
		if (!empty($trans_payment))
		{

			foreach ($trans_payment as $key => $tpi)
			{
			?><tr>
				<td class="ar"><?php echo $paymentTypes[$tpi['tType']] . $tpi['tNo']; ?></td>
				<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $tpi['allotAmt']; ?></b></td>
			</tr><?php
			}
		}
		if (!empty($dueAmount))
		{
			?><tr>
				<td class="ar"><?php echo $dueAmount_text; ?></td>
				<td class="ar" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="2"' : '' ?>><b><?php echo $dueAmount; ?></b></td>
			</tr><?php
		}
			?><tr>
				<td valign="top" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="3"' : 'colspan="2"' ?> class="vat ar bot">
					<small><?php echo mylang("E&O.E")?></small>
				</td>
			</tr>
			<tr>
				<td valign="bottom" class="vab bob"><?php
				if ($terms)
				{
					?><pre class="nm noside"><?php echo mylang("Terms")?> :<br><?php echo $terms; ?></pre><?php
				}
				?></td>
				<td valign="bottom" <?php echo ($isExport && $currencyCode != $expCurrencyCode) ? 'colspan="3"' : 'colspan="2"' ?> class="vab bob ar"><?php
				if (!empty($company['invsign']))
				{
					echo '<span class="sign" style="float:right;"><img src="' . $company['invsign'] . '" width="" height="" /><br>'.mylang("AuthorizedSignature").'</span>';
				}
				else
				{
					echo '<span class="" style="margin-top:40px;float:right;">'.mylang("AuthorizedSignature").'</span>';
				}
				?></td>
			</tr>
			<?php
				if(!empty($S_IRN) && !empty($S_IRN['SignedQRCode']))
				{
					echo '<tr>';
					echo '<td colspan="4">';

					$eInvoicesize = !empty($eInvoicesize) ? $eInvoicesize : 1;

					?><barcode code="<?php echo $S_IRN['SignedQRCode']; ?>" class="qr" size="<?php echo $eInvoicesize; ?>" text="0" disableborder="1" type="QR" /><?php
					echo '</td>';
					echo '</tr>';
				}
			?>
		</tbody>
	</table>
	<!-- small class="nm ac dib" style="width:100%;"><?php lang('COMPUTER_GENERATED_DOCUMENT'); ?></small -->
</div>
<style>
.detailTable {
	width: 100%;
	display: inline-block;
	margin-top: 10px;
}
.accInfo {
	width: auto;
}
table thead.bgcolor th {
	background-color: <?php echo $bgColor; ?>;
	color: <?php echo $foreColor; ?>;
	font-weight: normal;
}
</style>
