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

// Details Layout Begins

	if (!isset($sysAttrAsCol))
	{
		$sysAttrAsCol = false;
	}

	if ($includeDiscount)
	{
		$hasDiscount = false;
	}

	if (!empty($hideAllAmount))
	{
		$hideAmount = true;
		$showTax = false;
		$hideRate = true;
	}

	$paymentTypes = array(
		"SP" => "Receipt #",
		"SC" => "Credit Note #",
		"JV" => "Journal #",
		"PP" => "Payment #",
		"PD" => "Debit Note #",
		"AO" => "Opening Balance #",
		"PC" => "Sup.Credit Note #"
	);

	$isExpNonnGst = $classCode == "XPNG" ? true : false;
	$hideQty = false;
	if ($isExpNonnGst)
	{
		$hideRate = true;
		$hideQty = true;
	}

	$align = array("al", "ac", "ar");

	$isHsnBreakup = empty($isTaxPanel) ? false : true;

	///$borderColor = empty($borderColor) ? '#000000' : $borderColor;
	if($tType == 'SH') {
		//$isBankDetail = false;
		$notes = $terms = '';
	}
	$headColor = (empty($is_email) ? $bgColor : $color);
	$roundAmount = !empty($roundAmount) ? (float)$roundAmount : 0;
	// $roundAmount = formatAmount($roundAmount, $currencyCode, '');
	$dueAmount = abs($dueAmount);
	// $dueAmount = ($dueAmount != $totAmount) ? $dueAmount : 0;

	if ($isReverse)
	{
		$totAmount -= $totTax;
	}

	if(!empty($discountTotal))
	{
		if ($includeDiscount)
		{
			$taxableAmount = $taxableAmount + $discountTotal;
		}
		$discountTotal = formatAmount($discountTotal, $currencyCode, "-", true);
	}

	$f_dueAmount = formatAmount(($dueAmount != $totAmount) ? $dueAmount : 0, $currencyCode, '');
	$f_totAmount = formatAmount($totAmount, $currencyCode);

	if (empty($dueAmount))
	{
		$outstanding = '';
	}
	if (!empty($outstanding))
	{
		$oldDue = $outstanding - $dueAmount;
		$oldDue = ($oldDue > 0) ? formatAmount($oldDue, $currencyCode) : '';
		$newDue = ($outstanding > 0) ? formatAmount($outstanding, $currencyCode) : '';
	}

	// $totTax = formatAmount($totTax, $currencyCode);
	$tdsAmount = formatAmount($tdsAmount, $currencyCode);

	$taxableAmount = formatAmount($taxableAmount, $currencyCode, false, true);
	$tottaxAmount = formatAmount($tottaxAmount, $currencyCode, false, true);
	$forexTaxableAmount = formatAmount($forexTaxableAmount, $expCurrencyCode, false, true);
	$totForexAmt = formatAmount($totForexAmt, $expCurrencyCode, false, true);
	$f_forexDueAmount = $forexDueAmount != 0 ? formatAmount($forexDueAmount, $expCurrencyCode, false, true) : null;


	$isPrint = $export_type ? true : false;
	$discol = 0;
	/*if (!empty($company['invsign']))
	{
		if ($isPrint && !$signOnPrint) {
			$company['invsign'] = null;
		}

		if ((!$isPrint || $is_email) && !$signMailPdf) {
			$company['invsign'] = null;
		}
	}*/
	if (!empty($company['invsign']))
	{
		//For PDF(1),Display sign only in the mail Pdf
		if (!empty($download) && $download == 1 && $signOnPrint == 1) {
			$company['invsign'] = null;
		}
	}
	
	if ($tType == 'PO') {
		$isCustSign = false;
	}

	$foreColor = "#000000";

	if(!empty($bgColor))
	{
		if($bgColor == 'transparent')
		{
			$showVerticalLine= false;
		}
	}
	else
	{
		$showVerticalLine = true;
	}

	$gridFields = array();
	if (!empty($fields))
	{
		foreach ($fields as $row)
		{
			if (($row['tType'] == 'IG' || $row['tType'] == 'RG' || $row['tType'] == 'DG' || $row['tType'] == 'EG' || $row['tType'] == 'PG') && !empty($row['isPrint'])
			)
			{
				$row['fValue'] = json_decode($row['fValue'], true);
				$gridFields[] = $row;
			}
		}
	}

	if (empty($gstin) || $isComposition || $classCode == "BOS")
	{
		$showTax = false;
	}
	$taxlenth = 0;
	if($showTax && !empty($taxTypes))
	{
		$taxlenth = count($taxTypes);
	}
	$totQty = 0;
	$totFreeQty = 0;
	if(!empty($printColors)) {
		$headColor = $printColors;
	}

	// style="height:11.3cm;padding-top:1cm;padding-left: 0.5cm;padding-right: 0.5cm;"
?>
	<div class="gridtable">
	<table align='left' cellspacing="0" cellpadding="0" class="noborder grid" style="width:100%;padding-left: 0.5cm;padding-right: 0.5cm;"><?php
		$expCurLable = '';
		$curLable = '';
		$bothCur = '';

		$curLable = '<br><small>(' . $currencyCode . ')</small>';
		if($isExport) {
			$expCurLable = '<br><small>(' . $expCurrencyCode . ')</small>';
			$bothCur .= '<br><p class="curr nm"><small class="">(' . $expCurrencyCode . ')</small>';
		} else {
			$bothCur .= '<br>';
		}
		$bothCur .= '<small class="">(' . $currencyCode . ')</small></p>';

		$totWid = 100;
		$cols = 1;
		$chargesCol1 = 0;
		$chargesCol2 = 0;
		$subTotalCols = 0;
		$subTotalCols1 = 0;
		$qtyTotalCols = 0 ;

		$colTag = '';
		$headTag = '<tr>' . PHP_EOL;
		$bodyTag = '<tr class="tbody">' . PHP_EOL;
		$salChargeTag = '<tr>' . PHP_EOL;

		// Row Number
		if (!empty($isSlNoCol)) {
			//$colTag .= '<col style="width:6%">' . PHP_EOL;
			$headTag .= '<th style="width:35px; background-color:'.$headColor.'" class="first">'.lang("SNo").'</th>' . PHP_EOL;
			$bodyTag .= '<td class="ac">{rowNum}</td>' . PHP_EOL;

			//- $totWid -= 6;
			$cols++;
			$chargesCol1++;
			$subTotalCols++;
			$qtyTotalCols++;
		}

		if (!empty($firstFieldGrid))
		{
			//$colTag .= '<col style="width:20%">' . PHP_EOL;
			$headTag .= '<th style="border-bottom:0.5px solid #000000;border-right:0.5px solid #000000;background-color:'.$headColor.'" class="">' . $firstFieldGrid['fName'] . '</th>' . PHP_EOL;
			$bodyTag .= '<td style="white-space:nowrap;" class="{cf_class}">{cf_' . $firstFieldGrid['fId'] . '}</td>' . PHP_EOL;

			$cols++;
			$chargesCol1++;
			$subTotalCols++;
			$qtyTotalCols++;

			//- $totWid -= 20;
		}

		// Name & Desc
		//$colTag .= '<col style="width:{nameWid}%">' . PHP_EOL;
		$headTag .= '<th style="width:{nameWid}%;background-color:'.$headColor.'" class="">'.lang('ItemDescription');
		if (!$sysAttrAsCol && !empty($sys_attrs_grp))
		{
			$headTag.='<table align="left" cellspacing="0" cellpadding="0" width="100%" class="noborder"><thead><tr>';
			foreach($sys_attrs_grp as $k=>$v)
			{
				$headTag .= '<th width="33.3%" class="' . $align[$k] . '"><small>' . $v["name"] .'</small></th>';
			}
			$headTag .= '</tr></thead></table>';
		}
		$headTag .= '</th>' . PHP_EOL;

		$desc = '';
		if (!empty($isItemDesc))
		{
			$desc = '<br><span class="fnt-11 desc nm pretxt">{itemDesc}</span>';
		}

		if (!$sysAttrAsCol && !empty($sys_attrs_grp))
		{
			$desc .= "{sys_attrs}";
		}

		$desc .= '<p class="desc nm pretxt"><small>{attrs}</small></p>';

		// width="{nameWid}%"
		$bodyTag .= '<td  style="width:9cm;" class="">{itemName}' . $desc . '</td>' . PHP_EOL;
		$cols++;
		$chargesCol1++;
		$subTotalCols++;
		$qtyTotalCols++;

		if ($sysAttrAsCol && !empty($sys_attrs_grp))
		{
			foreach($sys_attrs_grp as $_sys_attr)
			{
				$headTag .= '<th class="">' . $_sys_attr["name"] . '</th>';
				$bodyTag .= '<td class="">{attr_' . $_sys_attr["attrGrpId"] . '}</td>' . PHP_EOL;

				$cols++;
				$chargesCol1++;
				$subTotalCols++;
				$qtyTotalCols++;
			}
		}
/*
		if ($sysAttrAsCol && !empty($sys_attrs_grp))
		{
			foreach($sys_attrs_grp as $k=>$attr_name)
			{
				$headTag .= '<th width="5%" class="">' . $attr_name . '</th>';
				$bodyTag .= '<td class="">{' . $attr_name . '}</td>' . PHP_EOL;

				$cols++;
				$chargesCol1++;
				$subTotalCols++;
			}
		}
*/
		// SKU Code
		if (!empty($isSkuCol) && $isSkuCol && $isSkuCol == 2)
		{
			//$colTag .= '<col style="width:10%">' . PHP_EOL;
			$headTag .= '<th style="background-color:'.$headColor.'" class="">'.lang("SKU").'</th>' . PHP_EOL;
			$bodyTag .= '<td class="">{SKU}</td>' . PHP_EOL;

			//- $totWid -= 10;
			$cols++;
			$chargesCol1++;
			$subTotalCols++;
			$qtyTotalCols++;
		}

		// HSN/SAC Code
		if ($isHsnCol)
		{
			//$colTag .= '<col style="width:10%">' . PHP_EOL;
			$headTag .= '<th style="width:70px;background-color:'.$headColor.'" class="">'.lang("HSN/SAC").'</th>' . PHP_EOL;
			$bodyTag .= '<td class="">{itemHS}</td>' . PHP_EOL;

			//- $totWid -= 10;
			$cols++;
			$chargesCol1++;
			$subTotalCols++;
			$qtyTotalCols++;
		}

		$isGridFields = false;
		// Custom Fields Grid
		if (!empty($gridFields))
		{
			foreach($gridFields as $val)
			{
				if (!empty($val['fValue']))
				{
					$isGridFields = true;
				}

				if ($isGridFields)
				{
					break;
				}
			}

			if (!$isGridFields)
			{
				$gridFields = array();
			}
			else
			{
				/*
				$cnt = count($gridFields);
				$wid = 10 * $cnt;
				if ($cnt > 2)
				{
					$wid = 10;
				}
				*/

				foreach($gridFields as $val)
				{
					// $colTag .= '<col style="width:' . ($wid / $cnt) . '%">' . PHP_EOL;
					$headTag .= '<th style="white-space:nowrap;background-color:'.$headColor.'" class="">' . $val['fName'] . '</th>' . PHP_EOL;
					$bodyTag .= '<td class=" ' . ($val['fType'] == 'number' ? 'ar' : '') . '{cf_class}">{cf_' . $val['fId'] . '}</td>' . PHP_EOL;

					$cols++;
					$chargesCol1++;
					$qtyTotalCols++;
				}

				//- $totWid -= $wid;
			}
		}

		// Quantity, Unit Name
		//$colTag .= '<col style="width:7%">' . PHP_EOL;

		if (empty($hideQty) && !empty($isqty))
		{
			$headTag .= '<th style="width:3cm;background-color:'.$headColor.';" class="ac">'.lang("Qty");// 
			if (!$isCompact)
			{
				$headTag .= (!empty($commonUnit) ? '<br/><small><small>' . $commonUnit . '</small></small>' : '');
			}
			$headTag .= '</th>' . PHP_EOL;

			$bodyTag .= '<td nowrap="nowrap" style="width:3cm;padding-right:0.5cm;" class="ac">{qty}</td>' . PHP_EOL;
			// . (!$isCompact && empty($commonUnit) ? '<small><small>{unit}</small></small>' : '')
			//- $totWid -= 7;
			$cols++;
			$chargesCol1++;
			$subTotalCols1++;
		}

		if (empty($hideQty) && !empty($isFreeQty))
		{
			// Free Qty
			//$colTag .= '<col style="width:12%">' . PHP_EOL;
			$headTag .= '<th style="" class="ar">' . lang("Free Qty");
			if (!$isCompact)
			{
				$headTag .= (!empty($commonUnit) ? '<br/><small><small>' . $commonUnit . '</small></small>' : '');
			}
			$headTag .= '</th>' . PHP_EOL;
			$bodyTag .= '<td class="ar">{freeQty}' . (!$isCompact && empty($commonUnit) ? '<br><small><small>{unit}</small></small>' : '') . '</td>' . PHP_EOL;
			// $totWid -= 12;
			$cols++;
			$chargesCol1++;
			$subTotalCols1++;
		}

		if (!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp)
		{
			// MRP
			//$colTag .= '<col style="width:12%">' . PHP_EOL;
			$headTag .= '<th style="" class="ar">' . lang("MRP") . $curLable . '</th>' . PHP_EOL;
			$bodyTag .= '<td class="ar">{mrp}</td>' . PHP_EOL;
			// $totWid -= 12;
			$cols++;
			$chargesCol1++;
			$subTotalCols1++;
		}

		if (!empty($hasSaleRates) && $tType == PURINV)
		{
			// Sales Rate
			//$colTag .= '<col style="width:12%">' . PHP_EOL;
			$headTag .= '<th class="ar">'.lang("Sale Rate") . '</th>' . PHP_EOL;
			$bodyTag .= '<td class="ar">{saleRate}</td>' . PHP_EOL;
			// $totWid -= 12;
			$cols++;
			$chargesCol1++;
			$subTotalCols1++;
			$qtyTotalCols++;
		}
		

		if (empty($hideRate) )
		{
			// Rate Per Unit
			//$colTag .= '<col style="width:12%">' . PHP_EOL;
			$headTag .= '<th style="width:3cm;background-color:'.$headColor.'" class="ar">'.lang("Rate") . ($isExport ? $expCurLable : '') . '</th>' . PHP_EOL;
			if ($includeTax)
			{
				$bodyTag .= '<td nowrap="nowrap" style="width:3cm;padding-right:0.5cm;" class="ar">{rateWithTax}' . '</td>' . PHP_EOL;
			}
			else
			{
				$bodyTag .= '<td nowrap="nowrap" style="width:3cm;padding-right:0.5cm;" class="ar">{rate}' . '</td>' . PHP_EOL;
			}
			//- $totWid -= 12;
			$cols++;
			$chargesCol1++;
			$subTotalCols1++;
		}

		// Discount
		if($hasDiscount)
		{
			//$colTag .= '<col style="width:10%">' . PHP_EOL;
			$headTag .= '<th style="background-color:'.$headColor.'" class="ar" >'.lang("Discount") . ($isExport ? $expCurLable : '') . '</th>' . PHP_EOL;
			if ($isCompact)
			{
				$bodyTag .= '<td class="ar">{discount}</td>' . PHP_EOL;
			}
			else
			{
				$bodyTag .= '<td class="ar">{' . ($isExport ? 'excDiscAmount' : 'discAmount') . '}<br><small>{discPercentage}</small></td>' . PHP_EOL;
			}

			//- $totWid -= 10;
			$cols++;
			$discol = $cols;

			$chargesCol1++;
			$subTotalCols1++;
		}

		$salChargeTag .= '<td class="ar {chargeClass}" colspan="' . $chargesCol1 . '">{itemName}</td>' . PHP_EOL;

		// Tax Name
		if ($isTaxDetailCol == 1)
		{
			if ($showTax && !empty($taxTypes))
			{
				//$colTag .= '<col style="width:10%">';
				$headTag .= '<th style="" class="ac">'.lang("Tax").'</th>';
				$bodyTag .= '<td class="ar">{taxName}</td>';

				$salChargeTag .= '<td class="ar {chargeClass}">{taxName}</td>' . PHP_EOL;

				// $totWid -= 10;
				$cols++;
				$subTotalCols1++;
			}
			else
			{
				$showTax = false;
			}
		}

		if (empty($hideAmount))
		{
			if ($isEstimate && $isExport)
			{
			//	$colTag .= '<col style="width:17%">' . PHP_EOL;
				$headTag .= '<th class="ar" style="background-color:'.$headColor.'">'.lang("Amount"). $expCurLable . '</th>' . PHP_EOL;
				$bodyTag .= '<td nowrap="nowrap" class="ar">{forexTaxableAmount}</td>' . PHP_EOL;

				$salChargeTag .= '<td nowrap="nowrap" class="ar {chargeClass}">{taxableAmount}</td>' . PHP_EOL;
				//$totWid -= 15;
				$cols++;
			}
			else
			{
				// Taxable Amount in foreign currency
				if ($isExport)
				{
					//$colTag .= '<col style="width:15%">' . PHP_EOL;
					$headTag .= '<th class="ar" style="background-color:'.$headColor.'">'.lang("Amount") . $expCurLable . '</th>' . PHP_EOL; // $bothCur
					$bodyTag .= '<td nowrap="nowrap" class="ar">{forexTaxableAmount}</td>' . PHP_EOL;

					$salChargeTag .= '<td class="ar {chargeClass}">{rate}</td>' . PHP_EOL;

					// $totWid -= 15;
					$cols++;
				}

				// Taxable Amount
				//$colTag .= '<col style="width:17%">' . PHP_EOL;
				$headTag .= '<th class="ar" style="width:6cm;background-color:'.$headColor.'">';
				$headTag .= (!$showTax || $isTaxDetailCol == 1 || $includeTax) ? (lang("Amount"). '<br>') : (lang("Taxable Amount").'<br>');
				$headTag .= ($isExport ? substr($curLable, 4) : '') . '</th>' . PHP_EOL;

				if ($includeTax && (!$isTaxDetailCol || $isTaxDetailCol == 1))
				{
					$bodyTag .= '<td nowrap="nowrap" style="width:3.5cm;padding-right:1cm;" class="ar">{itemTotal}</td>' . PHP_EOL;
					$salChargeTag .= '<td class="ar {chargeClass}">{itemTotal}</td>' . PHP_EOL;
				}
				else
				{
					$bodyTag .= '<td nowrap="nowrap" style="width:3.5cm;padding-right:1cm;" class="ar">{taxableAmount}</td>' . PHP_EOL;
					$salChargeTag .= '<td class="ar {chargeClass}">{taxableAmount}eryt</td>' . PHP_EOL;
				}
				//- $totWid -= 15;
				$cols++;
			}
		}

		if ($isTaxDetailCol == 2)
		{
			if ($showTax && !empty($taxTypes))
			{
				foreach($taxTypes as $taxType) {
				
				//	$colTag .= '<col style="width:8%">' . PHP_EOL;
					$headTag .= '<th style="" class="">' . $taxType . $curLable . '</th>' . PHP_EOL;
					if ($isCompact)
					{
						$bodyTag .= '<td class="ar fnt-small"><small>{' . $taxType . 'Rate}</small>&nbsp;&nbsp;{' . $taxType . 'Value}</td>' . PHP_EOL;
					}
					else
					{
						$bodyTag .= '<td class="ar fnt-small">{' . $taxType . 'Value}<br><small>{' . $taxType . 'Rate}</small></td>' . PHP_EOL;
					}

					$salChargeTag .= '<td class="ar fnt-small {chargeClass}">{' . $taxType . 'Value}<br><small>{' . $taxType . 'Rate}</small></td>' . PHP_EOL;

					//- $totWid -= 10;
					$cols++;
					$subTotalCols1++;
				}

				// Item Total
				$headTag .= '<th style="" class="ar">'.lang("Amount") . $curLable . '</th>' . PHP_EOL;
				$bodyTag .= '<td nowrap="nowrap" class="ar">{itemTotal}</td>' . PHP_EOL;
				$salChargeTag .= '<td nowrap="nowrap" class="ar {chargeClass}">{itemTotal}</td>' . PHP_EOL;
				//- $totWid -= 15;
				$cols++;
				$subTotalCols1++;
			}
		}

		$headTag .= '</tr>' . PHP_EOL;
		$bodyTag .= '</tr>' . PHP_EOL;
		$salChargeTag .= '</tr>' . PHP_EOL;

		if (!$showTax)
		{
			$chargesCol1 += $chargesCol2;
		}

		echo $colTag;

		//- $bodyTag = str_replace('{nameWid}', $totWid ? $totWid : 20, $bodyTag);
		// echo str_replace('{nameWid}', $totWid, $bodyTag);
		if ($isExport)
		{
			$nameWid = ($cols <= 5 ? 70 : ($cols <= 9 ? (strlen($forexTaxableAmount) <= 9 ? 50 : 40) : 30));
			if (strlen($forexTaxableAmount) > 10)
			{
				$nameWid -= 10;
			}
		}
		else
		{
			$nameWid = ($cols <= 5 ? 70 : ($cols <= 9 ? (strlen($taxableAmount) <= 9 ? 50 : 40) : 30));
		}

		$nameWid = "5.2cm";
		$headTag = str_replace('{nameWid}', $nameWid, $headTag);

		?><!--<thead  class="bgcolor vam" ><?php echo $headTag; ?></thead>-->
		<tbody><?php
			$unitArr = array();
			if (!empty($details))
			{
				// $discountTotal = 0;
				$subTotal = 0;
				$cnt = count($details);

				foreach ($details as $key => $val)
				{
					
					if (!empty($isSkuCol) && $isSkuCol && $isSkuCol == 1)
					{
						if(empty($val['SKU'])) {
							$val['SKU'] = '';
						}
						if(!empty($val['SKU'])) {
							$val['itemName'] = $val['SKU'] . " - " . $val['itemName'];
						}
						
					}
					if (empty($val["taxId"]))
					{
						$val["taxName"] = "GST 0%";
					}
					$taxName = true;
					if((empty($val["taxName"]) && empty($val["taxId"])) || $val["taxName"]=="GST 0%")
					{
						$taxName = false;
					}

					if (!empty($val['unit']))
					{
						$unitArr[] = $val['unit'];
						$val['qty'] = !empty($val['qty']) && $val['qty'] > 0 ? floatval($val['qty']) : 0;
						if(!empty($val['qty']) && $details[0]['unit'] == $val['unit'])
						{
							$totQty += $val['qty'];
							if (!empty($val['freeQty']))
							{
								$totFreeQty += $val['freeQty'];
							}
						}
					}
					//print_R($details[0]['unit']);exit;
					/*
					if(!empty($val['discAmount']) && is_numeric($val['discAmount']))
					{
						$discountTotal += $val['discAmount'];
					}
					*/

					if (!empty($val['discPercentage']))
					{
						$val['discPercentage'] = round($val['discPercentage'], 2);
						$val['discPercentage'] = empty(floatval($val['discPercentage'])) ? "" : ($val['discPercentage'] . "%");
					}

					if ($includeDiscount)
					{
						$val['discAmount'] = "";
						$val['excDiscAmount'] = "";
						$val['taxableAmount'] = $val['qty'] * $val['rate'];

						$subTotal += $val['taxableAmount'];
					}
					else
					{
						$val['discAmount'] = !empty($val['discAmount']) ? formatAmount($val['discAmount'], $currencyCode, "-", true) : 0;
						$val['excDiscAmount'] = formatAmount(!empty($val['excDiscAmount']) ? $val['excDiscAmount'] : 0, $expCurrencyCode, "-", true);
						
					}

					if ($val['discType'] == "%")
					{
						$val['discount'] = $val['discPercentage'];
					}
					else
					{
						$val['discount'] = $isExport ? $val['excDiscAmount'] : $val['discAmount'];
					}

					if ($includeDiscount)
					{
						if ($includeTax)
						{
							$val['itemTotal'] += $val["_discAmount"];
						}
						else
						{
							$val['taxableAmount'] += $val["_discAmount"];
						}
					}

					$val['qty'] = empty($val['qty']) ? "-" : $val['qty'];
					if ($isTaxDetailCol == 2 && $showTax)//  && !empty($taxTypes)
					{
						$val['rate'] = round($val['taxableAmount'] / $val['qty'], 2);
					}
					$val['rate'] = formatAmount(!empty($val['rate']) ? $val['rate'] : 0, $isExport ? $expCurrencyCode : $currencyCode, "-", true);

					$val['taxableAmount'] = formatAmount($val['taxableAmount'], $currencyCode, "-", true);
					$val['forexTaxableAmount'] = formatAmount($val['forexTaxableAmount'], $expCurrencyCode, "-", true);
					$val["itemTotal"] = formatAmount($val['itemTotal'], $currencyCode, "-", true);
					$val["mrp"] = formatAmount(!empty($val['mrp']) ? $val['mrp'] : 0, $currencyCode, "-", true);

					if (!empty($val["itemDesc"]))
					{
						$val["itemDesc"] = str_replace(array("\r\n", "\r", "\n"), "<br>", $val["itemDesc"]);
					}
					
					$attrs = "";
					if (!empty($sys_attrs_grp))
					{
						foreach($sys_attrs_grp as $_sys_attr)
						{
							if (!empty($val['sys_attrs'][$_sys_attr["attrGrpId"]]))
							{
								$attrs .= $_sys_attr["name"] . ' : ' . $val['sys_attrs'][$_sys_attr["attrGrpId"]] . PHP_EOL;
							}
						}
					}
					$val["attrs"] = $attrs . (empty($val["attrs"]) ? "" : $val["attrs"]);
/*
					if (!empty($val['sys_attrs']))
						$sys_attrs	= "";
						$idx=0;
						foreach($val['sys_attrs'] as $k=>$sys)
						{
							$val['attr_' . $k] = $sys ? $sys : "-";
							if (!empty($sys))
							{
								$sys_attrs .= $sys;
							}
							$idx++;
						}

						$val['sys_attrs'] = $sys_attrs;
					}
*/
					/*
					if (!empty($val['sys_attrs']))
					{
						$sys_attrs	='<table align="left" cellspacing="0" cellpadding="0" width="100%" class="noborder"><tbody><tr>';
						$idx=0;
						foreach($val['sys_attrs'] as $k=>$sys)
						{
							$val['attr_' . $k] = $sys ? $sys : "-";
							if (!empty($sys))
							{
								$sys_attrs .= '<td class="' . $align[$idx] . '" style="width: 33.3%"><small>' . ($sys ? $sys : '-') . '</small></td>';
							}
							$idx++;
						}
						$sys_attrs.='</tr></tbody></table>';
						$val['sys_attrs'] = $sys_attrs;
					}
					*/

					if (!empty($firstFieldGrid) && !empty($firstFieldGrid['fValue'][$val['id']]))
					{
						$val['cf_' . $firstFieldGrid['fId']] = $firstFieldGrid['fValue'][$val['id']];
					}

					foreach($gridFields as $k => $gf)
					{
						if (empty($gf['fValue'][$val['id']]))
						{
							continue;
						}

						$val['cf_' . $gf['fId']] = $gf['fValue'][$val['id']];

						if ($gf['isTotCol'])
						{
							if (empty($gf['total']))
							{
								$gf['total'] = 0;
							}
							$gf['cf_class'] = "al";
							if (is_numeric($gf['fValue'][$val['id']]))
							{
								$gf['total'] += floatval($gf['fValue'][$val['id']]);
								$val['cf_class'] = "ar";
							}

							$gridFields[$k] = $gf;
						}
					}

					$val['rowNum'] = $key+1;

					echo $this->parser->parse_str($bodyTag, $val, true);

					if ($isPrint && $isFullPage && $key == $cnt-1)
					{
						/*
						if(!empty($paper) && $paper != 'A5')
						{
							echo "{{EMPTY";
							echo $this->parser->parse_str($bodyTag, array("itemName" => '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" width="10px" height="##TransImgHeight##mm" />'), true);
							echo "EMPTY}}";
						}
						*/
						// echo $this->parser->parse_str($bodyTag, array("itemName" => '<div style="display:inline-block;width:20px;height:##TransImgHeight##mm;"></div>'), true);
					}
				}
			}
			//To find if the quantity is same for all
			$unitArrCnt = count(array_unique($unitArr));
			
			if (!empty($charges))
			{
				foreach($charges as $key => $charge_item)
				{
					$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true);
					if ($key > 0)
					{
						$charge_item["chargeClass"] = "nbt";
					}
					echo $this->parser->parse_str($salChargeTag, $charge_item, true);
				}
			}
?>
		</tbody>
	</table>
	</div>
<?php
	$isFullPage = false;

	// Details Layout End
	// Footer Layout Begins

	$rowspan = 1;
	/*
	if ($charges) {
		// $rowspan += count($charges);
	}
	*/
	if (!empty($tax) && (!$isExport || !$isEstimate)) {
		$rowspan += count($tax);
	}
	if ($roundAmount) {
		$rowspan++;
	}
	if (!empty($tdsPercentage)) {
		$rowspan++;
	}

	$rowspan++; // Total Amt

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


if (empty($hideAmount))
{
	?>
<htmlpagefooter name="last_page_footer">
<div class="last_page_footer" style="height:6cm;">
<table align="left" border="0" cellspacing="0" cellpadding="0" class="footTable vat nbh2 wpw noborder" style="width:100%;padding-left:0.5cm;padding-right:0.5cm;clear:both;table-layout: fixed;"><?php
	if (!$isExport || $isEstimate)
	{
		$col_wd = $col_wd1 = 'style="width:50%;"';
	}
	else
	{
		$col_wd = 'style="width:58%;"';
		$col_wd1 = 'style="width:42%;"';
	}
?>
	<!-- <thead>
		<tr>
			<th class="nb np"></th>
			<th class="nb np"></th>
		</tr>
	</thead> -->
	<tbody>
		<tr>
			<td style="width:10.2cm;" class="wrp">
				<!--<div class="nm amtinwords" style="white-space: pre-line; word-wrap: break-word !important;">Amount (in words) :<br><?php
					if ($isExport && $expCurrencyCode)
					{
						echo $expCurrencyCode . ' ' . $exp_amt_str . " only";
					}
					else
					{
						// $currencyCode
						echo $amt_str . " only";
					}
				?></div><br>--><?php
				if($tType == 'SI' && !empty($isBankDetail) && !empty($bankAccId) && !empty($accType))
				{
					?>
					<table class="bank accInfo noborder nopadding nwrap" border="0" cellspacing="0" cellpadding="0" autosize="1">
						<tbody>
							<tr>
								<td class="wrp" colspan="2">
									<p class="bank title nm">Our Bank Details</p>
								</td>
							</tr>
							<tr>
								<td class="accNumber title wrp" style="width:25%">Account No</td>
								<td class="accNumber value wrp">&nbsp;: <?php echo $accNumber; ?></td>
							</tr>
							<tr>
								<td class="accName title wrp">Account Name</td>
								<td class="accName value wrp">&nbsp;: <?php echo $bankAccName; ?></td>
							</tr>
							<tr>
								<td class="bankName title wrp">Bank</td>
								<td class="bankName value wrp">&nbsp;: <?php echo $bankName; ?></td>
							</tr>
							<tr>
								<td class="branch title wrp">Branch</td>
								<td class="branch value wrp">&nbsp;: <?php echo $branchName; ?></td>
							</tr>
							<tr>
								<td class="ifsc title wrp">IFSC Code</td>
								<td class="ifsc value wrp">&nbsp;: <?php echo $ifscRouteCode; ?></td>
							</tr>
							
							<?php
					if ($isExport)
					{
						if ($swiftCode) {
							?><tr>
								<td class="swift title wrp">Swift Code</td>
								<td class="swift value wrp">&nbsp;: <b><?php echo $swiftCode; ?></b></td>
							</tr><?php	
						}
						if (!empty($corrBank))
						{
							?><tr>
								<td class="corrBank title wrp">Correspondent Bank</td>
								<td class="corrBank value wrp">&nbsp;: <b><?php echo $corrBank; ?></b></td>
							</tr><?php
						}
						if ($adCode) {
							?><tr>
								<td class="dealer title wrp">AD Code</td>
								<td class="dealer value wrp">&nbsp;: <b><?php echo $adCode; ?></b></td>
							</tr><?php
						}
					}
						?></tbody>
					</table></div><br><?php
				}
				else if ($tType == 'SR')
				{
					if ($paymentName)
					{
						echo "Payment Mode : " . $paymentName . "<br/>";
					}
					if ($paymentMode != 1 && !empty($accName))
					{
						echo "Payment Account : " . $accName . "<br/>";
					}
					if ($ref)
					{
						echo "Reference No : " . $ref . "<br/>";
					}
				}
				/*
				if($notes)
				{
					?><span class="nm notes pretxt"><?php echo $notes; ?></span><br><br><br><?php
				}
				*/
			?>
			</td>
			<td style="width:10.4cm;padding-right:0.5cm;" class="np wrp">
				<table align="left" cellspacing="0" cellpadding="0" class="noborder">
					<tbody>
				<tr>
					<td class="ar subTotal title bob" style="font-size:17px;" colspan="2"><?php

					if ($includeTax)
					{
						echo '<small>(Tax Included)</small>&nbsp;&nbsp;';
					}

					?>Sub Total</td>
					<td class="ar" style="font-size:17px;"><?php echo $taxableAmount; ?></td>
				</tr>
					<?php
					if ($includeDiscount && $discountTotal)
					{
?>
						<tr>
							<td class="ar discountTotal title wrp" colspan="2" style="font-size: 12px;">
								<small><i>less</i></small> Discount <?php echo empty($discountPercent) ? "" : ((int)$discountPercent . "%"); ?>
							</td>
							<td class="ar discountTotal value wrp" style="font-size: 12px;"><?php echo $discountTotal; ?></td>
						</tr>
<?php
					}

					if ( ($isTaxDetailCol == 0 || $isTaxDetailCol == 1) && !empty($tax) && (!$isExport || !$isEstimate))
					{
						if ($showTaxBreakup)
						{
							// Tax to be paid on Reverse Charge
							foreach ($tax as $key => $tax_item)
							{
					?><tr class="<?php echo empty($isReverse) ? '' : 'reversecharge'; echo $includeTax ? 'includetax' : ''; ?>">
						<td class="ar tax title wrp np" colspan="2" style="font-size: 12px;"><?php
							if ($isReverse)
							{
								echo '<span class="asterisk">*</span>&nbsp;&nbsp;';
							}
							echo $tax_item['taxTypeCode'] . ' ' . round($tax_item['taxRate'], 2) ; 
							if ($tax_item['taxCalc'] == "Q")
							{
								echo '/Unit';
							}
							else
							{
								echo '% <small>on ' . (empty($tax_item['taxableAmt']) ? '' : formatAmount($tax_item['taxableAmt'], $currencyCode, '', true)). '</small>';
							}
						?></td>
						<td class="ar tax value wrp np" style="font-size: 12px;"><?php
							// echo $includeTax ? '' : '<b>';
							echo formatAmount($tax_item['taxValue'], $currencyCode, "-", true);
							// echo $includeTax ? '' : '</b>';
						?></td>
					</tr><?php
							}
						}
						else if (!empty($totTaxByCode))
						{
							foreach ($totTaxByCode as $key => $val)
							{
					?><tr class="<?php echo empty($isReverse) ? '' : 'reversecharge'; ?>">
						<td class="ar tax title wrp" colspan="2"><?php
							if ($isReverse)
							{
								echo '<span class="asterisk">*</span>&nbsp;&nbsp;';
							}
							echo $key;
						?></td>
						<td class="ar tax value "><b><?php echo formatAmount($val, $currencyCode, "-", true); ?></b></td>
					</tr><?php
							}
						}
					}

					if (($isExport && $classCode == 'EXPWOP') || $classCode == 'SEZWOP')
					{
						echo '<tr class="">';
						echo '<td class="ar tax title wrp" colspan="2">';
						echo 'IGST @ 0.00% on ' . $taxableAmount;
						echo '</td>';
						echo '<td class="ar tax value wrp"><b>0.00</b></td>';
						echo '</tr>';
					}

					if($roundAmount)
					{
					?><tr>
						<td class="ar round title wrp" style="font-size:12px;" colspan="2">Round Off </td>
						<td class="ar round value wrp" style="font-size:12px;"><?php echo $roundAmount; ?></td>
					</tr><?php
					}

					if(!empty($tdsPercentage) && $tdsPercentage != 0)
					{
					?><tr>
						<td class="ar tds title wrp">TDS @ <b><?php echo $tdsPercentage; ?>%</b></td>
						<td class="ar tds value wrp" colspan="2"><b><?php echo $tdsAmount; ?></b></td>
					</tr><?php
					}
					?><tr>
						<td class="ar total value vam wrp npr" style="font-size: 18px;" colspan="3">GRAND TOTAL&nbsp;&nbsp;&nbsp;<?php
						// Export invoices
						// !$isQuote &&
						if ($isExport)
						{
							// $forexTaxableAmount
							echo $forexTaxableAmount . '<br/><small><small>' . $expCurrencyCode . '</small></small>';
						}
						else
						{
							echo $f_totAmount;
						}
						?></td><?php
			/*
			?>
						<td class="total ar title vam" colspan="2"><?php
							if ($isReverse)
							{
								?><small class="fltlt"><span class="asterisk">*</span>&nbsp;&nbsp;to be paid on Reverse Charge</small>&nbsp;&nbsp;&nbsp;<?php
							}
							?><b class="fltrt">Total</b>
						</td><?php

						if ($isQuote && $isExport)
						{
							echo '<td class="ar total value vam"><b>' . $forexTaxableAmount . '</b><br/><small><small>' . $expCurrencyCode . '</small></small></td>';
						}
						else
						{
							if ($isExport)
							{
								// echo '<td class="ar totForexAmt value export vab"><b>' . $totForexAmt . '</b></td>';
							}
							if (!$isExport || !$isEstimate)
							{
								echo '<td class="ar total value vam" ><b>' . $totAmount . '</b></td>';
							}
						}
*/
						?></tr><?php
						$trans_payment = null;
						if (!empty($trans_payment))
						{
							$class = '';
							$writeOff = $forexWriteOff = 0;
							foreach ($trans_payment as $key => $tpi)
							{
								$class = ($key == 0) ? 'bot' : (($key == count($trans_payment)-1) ? 'bob' : '');
								if (!empty($tpi['writeOff']))
								{
									$writeOff += $tpi['writeOff'];
								}
								if (!empty($tpi['forexWriteOff']))
								{
									$forexWriteOff += $tpi['forexWriteOff'];
								}
							?><tr class="">
								<td class="ar payment title wrp <?php echo $class; ?>" <?php echo $isExport ? '' : 'colspan="2"'; ?> >(-) <?php
								echo $paymentTypes[$tpi['tType']] . $tpi['tNo'].'  ';?><?php echo '<small>'?><?php echo date_format(date_create($tpi['tDate']),"d/m/Y"); ?> <?php echo '</small>'?></td><?php
								if ($isExport)
								{
									echo '<td colspan="2" class="ar forexAmount value export wrp ' . $class . '">';
									echo '<small>' . $expCurrencyCode . '</small>&nbsp;&nbsp;';
									echo $tpi['forexAmount'];
									echo '</td>';
								}
								if (!$isExport && !$isEstimate)
								{
									echo '<td class="ar payment value wrp ' . $class . '">' . formatAmount($tpi['allotAmt'] + $tpi['tdsAmt'], $currencyCode, "-", true) . '</td>';
								}
							?></tr><?php
								if ($writeOff)
								{
									echo '<tr><td class="ar payment title wrp" '.($isExport ? '' : 'colspan="2"').'>Write-Off</td>';
									if ($isExport) {
										echo '<td class="ar forexAmount value export wrp ">' . $forexWriteOff. '</td>';
									}
									echo '<td colspan="2" class="ar payment value wrp" '.($isExport ? 'colspan="2"':'').'>'.formatAmount($writeOff, $currencyCode, "-", true).'</td></tr>';
								}
							}
						}
						if (!empty($f_dueAmount) && empty($outstanding))
						{
							?><tr>
								<td class="ar dueamt title wrp" <?php echo $isExport ? '' : 'colspan="2"'; ?>><?php echo $dueAmount_text; ?></td><?php
							if ($isExport)
							{
								echo '<td colspan="2" class="ar forexDueAmount value wrp">';
								if ($f_forexDueAmount)
								{
									echo '<small>' . $expCurrencyCode . '</small>&nbsp;&nbsp;';
									echo '<b>' . $f_forexDueAmount . '</b>';
								}
								else
								{
									echo "-";
								}
								echo '</td>';
							}
							else
							{
								echo '<td class="ar dueamt value wrp"><b>';
								echo (!$isEstimate) ? $f_dueAmount : '-';
								echo '</b></td>';
							}
?>
							</tr><?php
						}
						if (!empty($outstanding))
						{
							if (!empty($oldDue))
							{
							?><tr>
								<td class="ar title wrp" colspan="2"><?php echo lang("Existing Balance")?></td>
								<td class="ar value wrp"><?php echo $oldDue; ?></td>
							</tr>
							<?php
							}
							if (!empty($newDue))
							{
							?>
							<tr>
								<td class="ar title wrp" style="border-top:1px solid;" colspan="2"><?php echo lang("Total Balance")?></td>
								<td class="ar value wrp" style="border-top:1px solid;"><b><?php echo $newDue; ?></b></td>
							</tr><?php
							}
						}
						/*
						// if (empty($isFullPage) && empty($isHsnBreakup))
						if (empty($isHsnBreakup))
						{
						?><tr>
							<td valign="top" colspan="3" class="vat ar bot eoe nb">
								<small>E & O.E</small>
							</td>
						</tr><?php
						}
						*/
					?><!--<tr>
						<td valign="top" colspan="3" class="vat ar eoe nb">
							<p class="nm ar"><small><?php echo lang("E & O.E")?></small></p>
						</td>
					</tr>-->
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>
</div>
</htmlpagefooter>
<sethtmlpagefooter name="last_page_footer" value="1" />
<?php
}
	// if (!empty($isFullPage) || !empty($isHsnBreakup)) {
	/*
	if (!empty($isHsnBreakup)) {
		echo '<p class="nm ar"><small>E & O.E</small></p>';
	}
	*/

	if (!empty($taxTypes) && !empty($isHsnBreakup))
	{
	//	$this->view("export/hsn");
	}

		if (!empty($isFullPage))
		{
			// echo '<end_last_page end_height="30mm"><page_footer>';
		}

		// echo (!empty($isFullPage) || !empty($isHsnBreakup)) ? 'nb' : 'nbt';

	$eOe = "";
	/*
	if (empty($isHsnBreakup)) {
		$eOe = '<p class="nm ar"><small>E & O.E</small></p>';
	}
	*/
?>
<?php if($tType != PURINV){ ?>
<!--<table align="left" cellspacing="0" cellpadding="0" class="footTable vat <?php echo (!empty($isHsnBreakup)) ? 'nb' : 'nbt'; ?> wpw" style="page-break-inside: avoid">--><!-- autosize="1" -->
	<!--<col style="width:50%;">
	<col style="width:50%;">-->
	<!--<tbody><?php
	if ($terms)
	{
		?><tr>
			<td valign="bottom" colspan="2" class="vab terms" ><?php
				?><div class="noside " style="display:inline-block;"><span class="nm pretxt"><?php echo lang("Terms")?> :<br><?php echo nl2br($terms); ?></span></div>
			</td>
		</tr><?php
	}

		if ((!$is_email || !empty($company['invsign'] || !empty($is_pdf))) && !empty(trim(lang("For"))))
		{
			$clr = ($headColor == 'transparent') ? 'black' : $headColor;
		?><tr>
			<td class="ar bgColor" colspan="2" style="color:<?php echo $clr; ?>" ><?php echo lang("For"); ?> <?php echo  lang("companyName"); ?></td>
		</tr><?php
		}
		?><tr><?php
			echo '<td valign="bottom" class="vab nbr custsign bgColor" >';
			if ((!$is_email  ||!empty($is_pdf)) && !empty($isCustSign))
			{
				$clr = ($headColor == 'transparent') ? 'black' : $headColor;
				echo '<span class="" style="margin-top:40px;color:'.$clr.'" >' . ($tType == DELCHALLAN ? lang("Received") : lang("Customer Signature")) . '</span>';
			}
			echo '</td>';
			
			echo '<td valign="bottom" class="vab ar authsign bgColor" style="color:'.$headColor.'" >';
			//echo $eOe;
			if ($tType != PURINV && !empty(trim(lang("For")))) {
				if (!empty($company['invsign']) && (!empty($signOnPrint)))// $isPrint
				{
					$clr = ($headColor == 'transparent') ? 'black' : $headColor;
					echo '<span class="sign" style="float:right;"><img src="' . $company['invsign'] . '" width="" height="" /style="margin-top:0px;color:'.$clr.'" ><br>';
					echo '<span style="color:black;">'.lang("Authorized Signature").'</span>';
				}
				else if (!$is_email || !empty($company['invsign'] || !empty($is_pdf)))
				{
					$clr = ($headColor == 'transparent') ? 'black' : $headColor;
					echo '<br><br><br><span class="" style="margin-top:40px;float:right;color:'.$clr.'">'.lang("Authorized Signature").'</span>';
				}
			}
			echo '</td>';
		?></tr>
	</tbody>
</table><?php
}
//$email_color = empty($color) ? '#208ED3' : $color;
//$headColor = (empty($is_email) ? $bgColor : $email_color);
/*if(count(explode(",",$export_type)) > 1) {
		$headColor = $colorr;
	}*/
	

		if ( !empty($footerText) && !in_array($tType, array(PURINV, PURCRNOTE, PURDBNOTE, PURADVPMT, EXPRETAIL, PURPMT)))
		{
			echo '<p class="footerTxt nm pretxt">' . $footerText . '</p>';
		}
/*
		if (!empty($isFullPage))
		{
			// echo '</page_footer></end_last_page>';
		}

		if (!$isQuote)
		{
			echo '<page_footer><p class="genby nm">' . lang('COMPUTER_GENERATED_DOCUMENT') . '</p></page_footer>';
		}
*/
		if (!empty($waterMark))
		{
			echo '<page_footer><p class="watermark nm">' . $waterMark . '</p></page_footer>';
		}
?>
</div>-->
<style>
.detailTable {
	width: 100%;
	display: inline-block;
	margin-top: 10px;
}
table.grid tbody tr.tbody td {
	border-bottom: 1px solid #555;
}
table.grid tbody tr.tbody td {
	font-size: 17px;
}
.accInfo {
	width: auto;
}
<?php if(!empty($isvertical)) { ?>
.subtot td {
	border-top:1px solid black;
}
<?php } ?>
table, table td, table th {
	border-color:<?php echo 'black' ?>;/*$headColor == "transparent"?'#000000':$borderColor*/
}
<?php if(empty($isvertical)) { ?>
.subtot td {
	border-top: none;
	border-bottom: none;
		
}
<?php } ?>

table thead.bgcolor th {
	background-color: <?php echo $headColor; ?>;
	color: <?php echo $foreColor; ?>;
}
p.curr small {
	width: 50%;
	text-align: center;
	display: inline-block;
}
.tc {
	color: <?php echo $headColor; ?>;
}
.vl {
 	border-left: 4px solid <?php echo $headColor; ?>;
	padding-left:5px;
}
.ft{
	border-top:1px solid black;
}

.as {
	color:black;
}
.disc{
	font-style: italic;
	color: grey;
	font-weight: bold;
	padding-right: 15px;
}
.bob {
	border-bottom:1px solid grey;
}
.wrp {	
	white-space: nowrap;
}
<?php if(empty($isPrint)) { ?>
<?php if(empty($isvertical)) { ?>
.subTotal,.total {
	border-bottom:1px solid #000000;
}
<?php } ?>
<?php if(empty($isvertical)) { ?>
.first {
	border-right:0.5px solid #000000;
	border-left:1px solid #000000;
}
<?php } ?>
<?php } ?>
<?php if(empty($isPrint)) { ?>
.total {
	border-top:none;
}
<?php } ?>
.bob {
	border-bottom:1px solid grey;
}

</style>
