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

	$isHorizontal	= (!empty($isvertical) && $isvertical == 2) ? true : false;
	$isvertical		= (!empty($isvertical) && $isvertical == 1) ? true : false;
	$isSlNoCol		= (!empty($isSlNoCol)) ? $isSlNoCol : false;
	$isItemDesc		= (!empty($isItemDesc)) ? $isItemDesc : false;
	$totqty = 0;
	$isAmount = !isset($isAmount) ? 1 : $isAmount;
	$price = !isset($price) ? 0 : $price;
	$units = !isset($units) ? 0 : $units;
	$itemsecline = !empty($itemsecline) ? true : false;
	$isdiscount = !isset($isdiscount) ? 0 : $isdiscount;
	$isBankDetail = !isset($isBankDetail) ? 0 : $isBankDetail; 
	$totalitems = 0;
	$totalsavings = 0;
	$totalmrp = 0;
	$showreceivedAmt = false;
	$_paper = explode(",", $paper);
	$paper = empty($_paper[0]) ? "A4" : $_paper[0];
	$showbalance = false;
	$hasTax = false;
	$addressView = 'export/address/salinv';
	if($tType == 'JO' && empty($price)){
		$isAmount = false;
	}
	//For retail invoice prepare bank details for display
	if(!empty($banks) && !empty($bankid)){
	$bank = '';
		foreach ($banks as $key => $val)
		{
			if($val['accId'] == $bankid){
				$bank = $val ;
				if(!empty($val['upiId'])){
				$upi['name'] = $val['accName'];
				$upi['id'] = $val['upiId'];
				}
			}	
		}
	}
	$transUrl = array(
		'SE' => 'estimate',
		'SO' => 'order',
		'SI' => 'invoice',
		'SR' => 'invoice',
		'SH' => 'deliverychallan',
		'SC' => 'creditnote',
		'SD' => 'debitnote',
		'SP' => 'receipts',
	);
	
	$paymentTypes = array(
		"SP" => "Receipt #",
		"SC" => "Credit Note #",
		"JV" => "Journal #",
		"SI" => "Sales #",
		"PI" => "Purchase #",
		"PP" => "Payment #",
		"PD" => "Debit Note #",
		"AO" => "Opening Balance #",
		"PC" => "Sup.Credit Note #"
	);
	$cnt = 0;
	$gridFields = array();
	
	$f_dueAmount = formatAmount(($dueAmount != $totAmount) ? $dueAmount : 0, $currencyCode, '',$currFraction);
	if (!empty($fields))
	{
		$gridFieldTypes = array("IG", "RG", "DG", "EG", "PG", "XG");
		foreach ($fields as $row)
		{
			if (in_array($row['tType'], $gridFieldTypes) && !empty($row['isPrint']))
			{
				$row['fValue'] = json_decode($row['fValue'], true);
				$gridFields[] = $row;
			}
		}
	}
	$isGridFields = false;
	if (!empty($gridFields))
	{
		foreach($gridFields as $val)
		{
			if (!empty($val['fValue']))
			{
				$isGridFields = true;
			}

			if ($isGridFields)
			{
				
				break;
			}
			
		}
		if($isGridFields && $itemsecline){
			$cnt = count($gridFields); 
		}
	}
	if (!empty($tax) && !$isComposition)
	{
		$hasTax = true;
	}

	if (empty($dueAmount))
	{
		//$outstanding = '';
	}
	if (!empty($outstanding))
	{
		$oldDue = $outstanding - $dueAmount;
		// no need to subtract taxableamount , if receipt maid for invoice it may cause old due issue.
		$oldDue = ($oldDue > 0) ? formatAmount($oldDue, $currencyCode,'','',$currFraction) : '';
		$newDue = $outstanding > 0 ? formatAmount($outstanding, $currencyCode,'','',$currFraction) : '';
	}
	if(!empty($recAmt) && ($recAmt + 0) > 0){
		$showreceivedAmt = true;
	}
	if(!empty($balAmt) && ($balAmt - 0) > 0){
		$showbalance = true;
	}

	/*
	if (!empty($tax) && $company["gstin"] && !$company["isCompositionScheme"])
	{
		$hasTax = true;
	}
	*/
	$currency_fraction = !empty($currency_fraction) ? $currency_fraction : 2;

	$colspan1 = 1;
	$colspan2 = 0;
	if ($itemsecline)
	{
		$colspan1++;	// Amount column

		if ($isSlNoCol && ($paper=='3' ||  $paper =='4'))
		{
			$colspan2++;
		}

		if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
		{
			$colspan1++;
		}

		if(!empty($price))
		{
			$colspan1++;
		}

		$colspan2 = 'colspan="' . $colspan2 . '"';
	}

	$colspan1 = 'colspan="' . $colspan1 . '"';

	if($tType != 'SP' && $tType !='PP')
	{
?><table class="noborder vam itemdetails" cellspacing="0"><?php

	$col = (empty($isAmount) && empty($price)) ? 'colspan="2"' : '';

	?><thead>
		<tr class="dashed"><?php
/*
			if($isSlNoCol && ($paper=='3' ||  $paper =='4'))
			{
				?><th class="al"><?php echo lang("SNo"); ?></th><?php
			}
*/
			echo '<th class="al">' . mylang("Item") . '</th>';

			if ($isGridFields && $itemsecline)
			{
				foreach($gridFields as $val)
				{
					echo '<th class="al">' . $val['fName'] . '</th>';
				}
			}

			echo '<th class="ar">' . mylang("Qty") . '</th>';
			if(!empty($units) && $units && $units == 2)
			{
				echo '<th class="al">' . mylang("Units") . '</th>';
			}

			if($price && ($itemsecline || $paper=='3' || $paper =='4'))
			{
				echo '<th class="ar">' . mylang("Rate") . '</th>';
			}

			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				echo '<th class="ar mrp">' . mylang("MRP") . '</td>';
			}

			//Discount column
			if(!empty($isdiscount) && $hasDiscount)
			{
		     	echo '<th class="ar">' . mylang("Disc") . '</th>';
			}

			if($isAmount)
			{
				?><th class="ar"><?php echo  mylang("Amt"); ?></th><?php
			}

		?></tr>
	</thead>
	<tbody class="details"><?php

		$unit = !empty($details[0]['unit']) ? $details[0]['unit'] : '';
		$totDiscount = 0;
		$printname = false;
		foreach ($details as $key => $val)
		{
			if(!empty($val['itemType']) && $val['itemType'] == 'S' && $val['unit'] == ''){
						$val["qty"] = "-";
						$val["rate"] = "";
						$val["rateWithTax"] = "";
						$val["unit"] = "";
			} 
			/*
					If 2 decimal selected & for customer input 
						5, it will display as 5
						5.1 , it will display as 5.10
						5.13 , it will display as 5.13
						5.112 , it will display as 5.112
						5.1124 , it will display as 5.112

						If 3 decimal selected & for customer input 
						5, it will display as 5
						5.1 , it will display as 5.100
						5.13 , it will display as 5.130
						5.112 , it will display as 5.112
						5.1124 , it will display as 5.112 (since in db we are having 3 digits only)
					*/
					if(!empty($val['qty'])  && $val['qty'] !== '-'){
						if(strpos( $val['qty'], '.' ) === false){
							$val['qty'] = round($val['qty'],3);
						}else{
							$decimal_places = strlen(substr(strrchr($val['qty'], "."), 1));
							if($decimal_places >=3){
								$val['qty'] = round($val['qty'],3);
							}else {
								$decimal_range  = (!empty($qtyFraction) && $qtyFraction == 3) ? "%.3f" : "%.2f";
								$val['qty'] = sprintf($decimal_range,round($val['qty'],3));
							}
						}
					}else{
						$val['qty'] = "-";
					}	
			//display printname if given
			if(!empty($val['printName']) && trim($val['printName'])){
				$printname = true;
				$val['itemName'] =  trim($val['printName']);
			}
			if (!empty($units) && $units && $units == 3)
			{
				if(!empty($val['rel_units']) && empty($val['relUnitType']))
				{
					foreach($val['rel_units'] as $_key => $_val)
					{
						if($val['unit']  != $_val['name'])
						{
							if(!empty($val['qty']) && !empty($_val['convFactor']))
							{
								$val['unit'] = $val['unit'] .'<br>(' . number_format(($val['qty'] / $_val['convFactor']),2) .$_val['name'] .')'; 
							}
						}
					}
				}
						
				if(!empty($val['rel_units']) && !empty($val['relUnitType']))
				{
					foreach($val['rel_units'] as $_key => $_val)
					{
						if($val['relUnitName']  != $_val['name'])
						{	
							if(!empty($val['qty']) && !empty($val['convFactor']))
							{
								if(empty($_val['type']))
								{
									$val['rel_qty'] = ($val['qty'] * $val['convFactor']);
									$val['unit'] = $val['unit'] .'<br>(' . ($val['rel_qty']) .$_val['name'] .')'; 
								}
								if(!empty($_val['type']) && !empty($val['rel_qty']))
								{
									$val['unit'] = $val['unit'] .'<br>(' . number_format(($val['rel_qty'] / $_val['convFactor']),2) .$_val['name'] .')'; 
								}
							}
						}
					}
				}
			}
			?><tr class="item det <?php echo $val["id"]; ?>">
<?php
/*
				if ($isSlNoCol && ($paper=='3' ||  $paper =='4'))
				{
					echo '<td class="vam">' . ($key + 1) . '</td>';
				}
*/

				echo '<td ' . $colspan1 . ' class="itemName">';

				if ($isSlNoCol && ($paper=='3' ||  $paper =='4'))
				{
					echo ($key + 1) . '. ';
				}

				echo $val['itemName'];

				if ($colspan2)
				{
					echo '</tr>';
					echo '<tr class="row2">';
					echo '<td ' . $colspan2 . '>';
				}

				$itemAttr = array();
			if(!empty($val['sys_attrs']) && !empty($sys_attrs_grp))
			{
				$sysAttri = '';
				$i = 0;
				$lenarr = count($val['sys_attrs']);

				foreach ($val['sys_attrs'] as $value)
				{
					$sysAttri .= $sys_attrs_grp[$i]['name'] . ' : ' . $value;	
					$i = $i + 1;
					if($lenarr > $i)
					{
						$sysAttri .= '<br>';
					}
				}

				if(!empty($sysAttri))
				{
					/* ?><br><small style="font-size: 10px !important;"><?php */
					$itemAttr[] =  '<small>' . $sysAttri . '</small>';
					/* ?></small><?php */
				}
			}

			if(!empty($val['attrs']))
			{
				$itemAttr[] = '<br><small>' . $val['attrs'] . '</small>';
				/* ?><br><small style="font-size: 10px !important;" ><?php echo $val['attrs']; ?></small><?php */
			}

			if ($isItemDesc && !empty($val['itemDesc']))
			{
				$itemAttr[] = '<br><small>' . $val['itemDesc'] . '</small>';
				/* ?><br><small style="font-size: 10px !important;" ><?php echo $val['itemDesc']; ?></small><?php */
			}
			if (!empty($val['cfList']))
			{
				$itemAttr[] = '<br><small>' . $val['cfList'] . '</small>';
				/* ?><br><small style="font-size: 10px !important;" ><?php echo $val['itemDesc']; ?></small><?php */
			}

			if ($isHsnCol && !empty($val['itemHS']))
			{
				$itemAttr[] = '<div><small>' . $val['itemHS'] . '</small></div>';
				/*
				?><br><small style="font-size: 10px !important;"><span style ="font-size: 10px !important;font-style: italic;"><?php echo 'HSN: '. $val['itemHS'];
				?></span></small><?php
				*/
			}
			if(!empty($itemAttr)){
				foreach($itemAttr as $row) {
					echo $row;
				}
			}
			?></td>
			<?php
				if ($isGridFields && $itemsecline)
				{
					foreach($gridFields as $k => $gf)
					{
						if (empty($gf['fValue'][$val['id']]))
						{
							continue;
						}

						$val['cf_' . $gf['fId']] = $gf['fValue'][$val['id']];
						echo '<td class="al">'.  $val['cf_' . $gf['fId']] .'</td>';
					}
				}
				/*
					If 2 decimal selected & for customer input 
						5, it will display as 5
						5.1 , it will display as 5.10
						5.13 , it will display as 5.13
						5.112 , it will display as 5.112
						5.1124 , it will display as 5.112

						If 3 decimal selected & for customer input 
						5, it will display as 5
						5.1 , it will display as 5.100
						5.13 , it will display as 5.130
						5.112 , it will display as 5.112
						5.1124 , it will display as 5.112 (since in db we are having 3 digits only)
					*/
					if(!empty($val['qty']) && $val['qty'] !== '-'){
						if(strpos( $val['qty'], '.' ) === false){
							$val['qty'] = round((float)$val['qty'],3);
						}else{
							$decimal_places = strlen(substr(strrchr($val['qty'], "."), 1));
							if($decimal_places >=3){
								$val['qty'] = round($val['qty'],3);
							}else {
								$decimal_range  = (!empty($qtyFraction) && $qtyFraction == 3) ? "%.3f" : "%.2f";
								$val['qty'] = sprintf($decimal_range,round($val['qty'],3));
							}
						}
					}else{
						$val['qty'] = "-";
					}
				?><td <?php echo $col; ?> class="ar qty"><?php
					echo $val['qty'];

					if (!empty($val['unit']))
					{
				?>&nbsp;<small><small><?php
					if($units && $units == 1)
					{
						$unitArr[] = $val['unit'];
						echo '<span style="font-weight:normal;font-size:9px;">' . $val['unit'] . '</span>'; // strtolower($val['unit']) == "nos" ? "" : 
					}
					if($units && $units == 3)
					{
						echo '<span style="font-weight:normal;font-size:9px;">' . $val['unit'] . '</span>'; // strtolower($val['unit']) == "nos" ? "" : 
					}
						?></small></small><?php
					}
?>
					</td>
<?php				if(!empty($units) && $units && $units == 2)
					{
						?><td class="al rate"><?php echo $val['unit']; ?></td>
<?php				}

			if(!empty($price) && ($itemsecline || $paper=='3' ||  $paper =='4'))
			{
				$val['rateWithTax'] = formatAmount(!empty($val['rateWithTax']) ? $val['rateWithTax'] : 0, $isExport ? $expCurrencyCode : $currencyCode, "-", true,$rateFraction);
				$val['rate'] = formatAmount(!empty($val['rate']) ? $val['rate'] : 0, $isExport ? $expCurrencyCode : $currencyCode, "-", true,$rateFraction);
				?><td class="ar rate"><?php echo $includeTax ? $val['rateWithTax'] : $val['rate']; ?></td><?php
			}

			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				$strSalModCodes = get_company('salModCodes');
				$salModCodes = explode(',',$strSalModCodes);
				if(in_array('MC',$salModCodes))
				{
					$val["mrp"] = formatAmount(!empty($val['mrp']) ? $val['mrp'] : 0,$isExport ? $expCurrencyCode : $currencyCode, "-", true,$currFraction);
					if(!empty($val['itemcurrencyCode']) && !empty($val['mrp']))
					{
						if($tType == 'SI' && (( $isExport && $expCurrencyCode != $val['itemcurrencyCode']) ||  ( !$isExport && $val['itemcurrencyCode']!= get_company("currency"))))
						{
							$val['mrp'] = $val['chargesAmount'] .'<br><small style="font-style: italic;color: grey;font-weight: bold;">' .$val['mrp'].'&nbsp;'.$val['itemcurrencyCode'] .'</small>';
						}
						else if($tType == 'SI' &&  $isExport && $expCurrencyCode == $val['itemcurrencyCode'] )
						{
							$val['mrp'] =  $val['mrp'] .'<small style="font-style: italic;color: grey;font-weight: bold;">' .'&nbsp;'.$val['itemcurrencyCode'] .'</small>';
						}
						else if($tType == 'PI' || ($tType == 'SI' && $val['itemcurrencyCode']== get_company("currency")))
						{
							$val['mrp'] =  $val['mrp'] .'<small style="font-style: italic;color: grey;font-weight: bold;">' .'&nbsp;'.$val['itemcurrencyCode'] .'</small>';
						}
					}
				}
				$val["mrp"] = formatAmount(!empty($val['mrp']) ? $val['mrp'] : 0,$isExport ? $expCurrencyCode : $currencyCode, "-", true,$currFraction);
				?><td class="ar mrp"><?php echo $val['mrp']; ?></td><?php
			}


			if(!empty($isdiscount) && $hasDiscount)
			{

				if (!empty($val['discPercentage']))
				{
					$val['discPercentage'] = round($val['discPercentage'], 2);
					$val['discPercentage'] = empty(floatval($val['discPercentage'])) ? "" : ($val['discPercentage'] . "%");
				}
				if ($isdiscount == 2)
				{
					$val['discount'] = $val['discPercentage'];
				}
				else
				{
					$val['excDiscAmount'] =  !empty($val['discAmount']) ? $val['discAmount']: "";
					$val['discount'] = $isExport ? $val['excDiscAmount'] : $val['discItem'];
					
				}

				if (empty($val['discount']))
				{
					$val['discount'] = "-";
				}
?>			<td class="ar discAmount"><?php echo $val['discount']; ?></td><?php
			}

			if($isAmount)
			{
				?><td class="ar taxableAmount itemTotal"><?php
				if (!empty($val['_discAmount']))
				{
					$val['discAmount'] = $val['_discAmount'];
				}

				if(!empty($val['discItem']))
				{
					$totDiscount = $totDiscount + $val['discItem'];	
				}
				
				if (!empty($val['discAmount']))
				{
					echo number_format(($includeTax ? $val["_itemTotal"] : (!empty($val['_taxableAmount'])  ? $val['_taxableAmount'] : 0)), $rateFraction);
				}
				else
				{

					echo number_format(($includeTax ? $val["itemTotal"] : $val['taxableAmount']), $rateFraction);
				}
		?></td><?php }
		}
		if (!empty($charges))
		{
			$colspan = 2;

			if (($price) && ($paper=='3' ||  $paper =='4'))
			{
				$colspan++;
			}

			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				$colspan++;
			}
			if(!empty($isdiscount) && $hasDiscount)
			{
				$colspan++;
			}
			if(!empty($units)  && $units == 2 && ($paper=='3' ||  $paper =='4')){
				$colspan++;
			}

			$colspan = "colspan=\"" . $colspan . "\"";
			foreach($charges as $key => $charge_item)
			{
				if ($charge_item["unit"] == "ABT"){
					$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true,$rateFraction);
					echo '<tr>';
					echo '<td class="ar" ' . $colspan. ' >'.$charge_item['itemName'].'</td>';
					echo '<td class="ar">'. $charge_item['taxableAmount'].'</td>';
					echo '</tr>';
				}
			}
	}
	?></tr><?php 
		$unitArr = !empty($unitArr) ? array_map("strtoupper", $unitArr) : '';  
		$unitArrCnt = !empty($unitArr) ? count(array_unique($unitArr)) : '';
		/******Multiple units(two distinct units ) total qty calculation ******/
		if(!empty($unitArr)){ 
				$multiple_units = array_values(array_unique($unitArr));
		}
		if(!empty($unitArrCnt) && ($unitArrCnt > 0) && ($unitArrCnt <= 2) && !empty($multiple_units) ){
			$_totQty = array();
			foreach ($details as $key => $val){
				foreach ($multiple_units as $_val){
					$val['unit'] = strtoupper($val['unit']);
					if(!isset($_totQty[$_val])){
						$_totQty[$_val] = 0;
					}
					if(!empty($val['unit']) && $val['unit'] == $_val){
						$_totQty[$_val] += $val['qty'];
					}
				}
					
			}
		}

		if(!empty($details))
		{
			$totalamt= 0;
			foreach ($details as $key => $val)
			{
				if ($includeTax)
				{
					$totalamt += $val['itemTotal'];
				}
				else
				{
					$totalamt += $val['taxableAmount'];
				}
				$totalitems = count($details); 
				$totqty = $totqty + (float)$val['qty'];
				$val['qty'] = $val['qty'] > 0 ? (float)$val['qty'] : '';
				if (!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
				{
					if (!empty($val['mrp'])&& $val['mrp'] > 0 && $val['qty'] && $val['qty'] > 0)
					{
						$totalsavings += (($val['mrp'] - $val['rateWithTax'])  * $val['qty']);
					}

					if (!empty($val['discAmount']))
					{
						$totalsavings += $val['discAmount'];
					}
				}

				$details[$key] = $val;
			}
		}

		$issubtotal = true;
		//If Subtotal=total & no tax, hide Subtotal
		if ($taxableAmount == $totalamt && empty($charges) && !$hasTax && !$includeDiscount && empty($totDisc))
		{
			$issubtotal = false;
		}

			
			if ($issubtotal)
			{
		?><tr  class="subtotal">
			<?php
				/*
				if($isSlNoCol && ($paper=='3' ||  $paper =='4'))
				{
					echo '<td></td>';
				}
				*/
			?>
			<td  style="font-weight: normal;" colspan ="<?php echo $cnt ? $cnt+1 : '';?>"><?php 
				if(!$isSlNoCol && ($paper=='3' ||  $paper =='4'))
				{
					echo '<small> Total Items - '. $totalitems.'</small><br>';				
				}
				else
				{
					echo ($includeTax && $hasTax) ? (mylang("SubTotal")."(inc.Tax) ") :  mylang("SubTotal");
				}
			?> &nbsp;</td>
				<!--<?php echo lang("Sub Total"); ?>--></td>
<?php
			
			?><td  class="<?php echo (empty($units)|| (!empty($units) && ($unitArrCnt == 1 && count($details) > 0))) ? 'ar' :'ac'?>" style ="white-space: nowrap !important;border-top: 1px dotted black;"><?php 
				
				if(!empty($units) && ($units == 1) &&  ($unitArrCnt == 1 && count($details) > 0)) { ?>
					 <?php echo $totqty;?>	&nbsp;<small><small style="font-size :9px !important;"><?php echo $unit; ?></small></small><?php } ?>
				<?php if(!empty($units) && ($units == 1) && !empty($unitArrCnt) && ($unitArrCnt == 2)&& count($details) > 0) { 
				
					foreach($_totQty as $_ky => $_vl){
				?>
					
						<?php echo $_vl; ?>&nbsp;<small><small style="font-size :9px !important;"><?php echo $_ky; ?></small></small><br><?php
						
					}
				 } ?>
			</td>
			<?php if($units && ($units == 2) && ( $paper=='3' ||  $paper =='4'))
			{
				echo '<td style="border-top: 1px dotted black;"></td>';
			}
			?>
			<?php 
			if($price && ($itemsecline || $paper=='3' ||  $paper =='4'))
			{
				echo '<td style="border-top: 1px dotted black;"></td>';
			}
				
			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				echo '<td style="border-top: 1px dotted black;"></td>';
			}
			
			?>
			<?php if($hasDiscount && !empty($totDiscount) && !empty($isdiscount)){?>
				<td style="border-top: 1px dotted black;" class="ar"><?php echo number_format($totDiscount,$currFraction); ?></td>
			<?php }
			if ($isAmount){ ?>
			<td class="ar taxableAmount" style="border-top: 1px dotted black;"><?php
				// $totalamt= 0;
				if (!empty($charges))
				{
					foreach($charges as $key => $charge_item)
					{
						if ($charge_item["unit"] == "ABT")
						{
							$totalamt += (float)$charge_item['taxableAmount'];
						}
					}
					$taxableAmount = $totalamt;
				}
				if(sal_mod_active(POINTS_MOD)  && !empty($points) && !empty($points['pOut_value'])){
					$taxableAmount = $taxableAmount + $points['pOut_value'];
				}
			//$taxableAmount = formatAmount($taxableAmount, $currencyCode, "-", true);
				if (!empty($totDisc))
				{
					if ($includeTax)
					{
						echo number_format($totAmount + $totDisc,$currFraction);
					}
					else
					{
						echo number_format($taxableAmount + $totDisc,$currFraction);
					}
				}
				else
				{
					echo number_format($taxableAmount, $currFraction);
				}
			}
			?></td>
		</tr>
		
		<?php }?>
		<?php if ($isAmount){ ?>
				<?php	
					//$addbeforetaxtot = 0;
					if (!empty($charges))
					{
						$colspan = 2;
		
						if (($price) && ($paper=='3' ||  $paper =='4'))
						{
							$colspan++;
						}
		
						if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
						{
							$colspan++;
						}
						if(!empty($units)  && $units == 2 && ($paper=='3' ||  $paper =='4')){
							$colspan++;
						}
						if(!empty($isdiscount) && $hasDiscount)
						{
								$colspan++;
						}
						$colspan = "colspan=\"" . $colspan . "\"";
						foreach($charges as $key => $charge_item)
						{
							
							if ($charge_item["qty"] < 0)
							{
								$lessCharges[] = $charge_item;
								continue;
							}

							if ($charge_item["unit"] != "ABT" && $charge_item["unit"] != "AAL" && $charge_item["unit"] != "LAL")
							{
								echo '<tr>';
								echo '<td class="ar" ' . $colspan. ' >'.$charge_item['itemName'].'</td>';
								if ($charge_item["unit"] == "LBT")
								{
									echo '<td class="ar">' . formatAmount($charge_item['itemTotal'], $currencyCode, "-", true,$rateFraction) . '</td>';
								}
								else
								{
									echo '<td class="ar">' . formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true,$rateFraction) . '</td>';
								}
								
								echo '</tr>';
							}
						}
					}
?>	</tbody>
</table>

<table class="noborder vam itemdetails">
<tbody><?php

	$colspan = 2;

	if (($price) && ($paper=='3' ||  $paper =='4'))
	{
		$colspan++;
	}

	if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
	{
		$colspan++;
	}

	if ($isSlNoCol && ($paper=='3' ||  $paper =='4')){
		$colspan++;
	}

	$colspan = "colspan=\"" . $colspan . "\"";
	if ($includeDiscount &&  !empty($totDisc))
	{
		?>
		<tr class="totDiscount">
			<td <?php echo $colspan; ?> class="ar">(-) Discount</td>
			<td class="ar"><?php echo number_format($totDisc, $rateFraction); ?></td>
		</tr>
		<?php
	}
	/*$lessCharges = array();
			if (!empty($charges))
			{
				$colspan = 1;

				if (($price) && ($paper=='3' ||  $paper =='4'))
				{
					$colspan++;
				}

				if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
				{
					$colspan++;
				}
				if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
					$colspan++;
				}

				$colspan = "colspan=\"" . $colspan . "\"";
				foreach($charges as $key => $charge_item)
				{
					if ($charge_item["qty"] < 0)
					{
						//$lessCharges[] = $charge_item;
						continue;
					}

					$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true);
					echo '<tr>';
					echo '<td class="ar" ' . $colspan. ' >'.$charge_item['itemName'].'</td>';
					echo '<td class="ar">'. $charge_item['taxableAmount'].'</td>';
					echo '</tr>';
				}
			}
	*/
	if ($hasTax)
	{
			
		foreach ($tax as $key => $val)
		{
			if(!empty($val['taxTypeCode']) && ($val['taxTypeCode'] != "EXEMPTED"))
			{

		?><tr class="tax small">
			<td <?php echo $colspan; ?> class="ar nph"><?php

			echo $val['taxName'] . (!empty($val['taxRate']) ? (' ' . round($val['taxRate'], 2) . ($val['taxCalc'] == "Q" ? "/Unit" : "%")) : "");

			if($paper=='3' ||  $paper =='4')
			{
				echo empty($val['taxableAmt']) ? '' : (' <small>on ' . formatAmount($val['taxableAmt'], $currencyCode, '', true,$currFraction) . '</small>');
			}

		?></td>
			<td class="ar nph"><?php echo empty((float)$val['tax_value']) ? '-' : number_format($val['tax_value'],$currFraction); ?></td>
		</tr><?php
			}
		}
	}

	if($roundAmount && $roundAmount != '0.00')
	{
?>		<tr>
			<td <?php echo $colspan; ?>>Round Off</td>
			<td class="ar" ><?php echo formatAmount($roundAmount, $currencyCode, '', true,$currFraction); ?></td>
		</tr><?php
	}
?>
</tbody>
</table>
<table class="noborder vam itemdetails" cellspacing="0">
<tbody>
	<tr class="totAmountCurr dashed1 <?php echo empty($outstanding) ? 'dashed2' : ''; ?>">
		<td><b style="white-space: pre;"><?php echo mylang("TOTAL"); ?></b>
		<?php if(empty($issubtotal)){
			if(!empty($units) && ($units == 1) &&  ($unitArrCnt == 1 && count($details) > 0)) { ?>
					 - <?php echo $totqty;?>	&nbsp;<small><small style="font-size :9px !important;"><?php echo $unit; ?></small></small><?php } ?>
				<?php if(!empty($units) && ($units == 1) && !empty($unitArrCnt) && ($unitArrCnt == 2)&& count($details) > 0) { 
					echo " - ";
					foreach($_totQty as $_ky => $_vl){
				?>
					
						<?php echo $_vl; ?>&nbsp;<small><small style="font-size :9px !important;"><?php echo $_ky; ?></small></small><?php
						
					}
				 } 
		}?>
	</td>
<?php
		if(($price) && ($paper=='3' ||  $paper=='4'))
		{
?>		<td  class="<?php echo !empty($units) ? 'ar' :'ar'?>"><?php echo $hasTax ? "" : ""; ?></td><?php
		}

		if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
		{
			echo '<td></td>';
		}
		if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
			//echo '<td></td>';
		}
		
?>

		<td class="ar amt" colspan="4"><?php echo (sal_mod_active(POINTS_MOD)  && !empty($points) && !empty($points['pOut_value'])) ? $totpointsAmount : '<b><big>'.$totAmountCurr.'</big></b>' ; ?></td>
	</tr>
</tbody>
</table>
<table class="noborder vam itemdetails">
<tbody>
	
<?php if (!empty($lessCharges))
		{
			
			foreach($lessCharges as $key => $charge_item)
			{
				$colspan = 2;

				if (($price) && ($paper=='3' ||  $paper =='4'))
				{
					$colspan++;
				}

				if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
				{
					$colspan++;
				}
				if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
					$colspan++;
				}

				$colspan = "colspan=\"" . $colspan . "\"";

				$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true,$currFraction);
				?><tr class="">
					<td class="ar charges title wrp" <?php echo $colspan; ?>><i>(less) </i><?php
						echo $charge_item["itemName"];
						if (!empty($charge_item["conName"]))
						{
							echo ' <small>(' . $charge_item["conName"] . ')</small>';
						}
					?></td>
					<td class="ar charges value wrp"><b><?php
						echo formatAmount(abs($charge_item['itemTotal']), $currencyCode, "-", true,$currFraction);
					?></b></td>
				</tr><?php
			}
		}
?>
<?php if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4')){
		if(!empty($totalsavings)){ ?>
			<tr class=" dashed2"><td colspan="6" class="ac"> Total savings Rs: <?php echo  formatAmount($totalsavings, $currencyCode, "-", true,$currFraction);?></td></tr>
	<?php } ?>
<?php } ?>
<?php
	if (!empty($charges))
	{
		$totLessCharges = 0;
		foreach($charges as $key => $charge_item)
		{
			if ($charge_item["unit"] == "AAL" || $charge_item["unit"] == "LAL")
			{
				if( $charge_item["unit"] == "AAL" ){
					$totLessCharges += abs($charge_item['itemTotal']);
				}
				if( $charge_item["unit"] == "LAL" ){
					$totLessCharges -= abs($charge_item['itemTotal']);
				}
				$charge_item['taxableAmount'] = formatAmount($charge_item['taxableAmount'], $currencyCode, "-", true,$rateFraction);
			?>
				<tr class="balance">
					<td <?php
							$colspan = 3;
							if (($price) && ($paper=='3' || $paper=='4'))
							{
								$colspan++;
							}
							if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
							{
								$colspan++;
							}
							$colspan = "colspan=\"" . $colspan . "\"";
							echo $colspan;
							 ?> 
						class="npt npb"><?php echo $charge_item['itemName']; ?>
					</td>
					<td class="ar npt npb"><?php echo $charge_item['taxableAmount']; ?></td>	
				</tr>
				<tr class="balance totAmountCurr">
					<td <?php
							$colspan = 3;
							if (($price) && ($paper=='3' || $paper=='4'))
							{
								$colspan++;
							}
							if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
							{
								$colspan++;
							}
							$colspan = "colspan=\"" . $colspan . "\"";
							echo $colspan;
							 ?> 
						class="npt npb finaltotal "><b><?php echo "Final Total" ?></b>
					</td>
					<td class="ar npt npb amt"><?php echo '<b><big>'.formatAmount($totAmount +$totLessCharges, $currencyCode, "-", true,$currFraction).'</big></b>'; ?></td>	
				</tr>
		<?php 	}
		}
	}
?>			
<?php
	//Receipt ,balance due ,existing balance
			if (!empty($trans_payment)&& !empty($incOldBal) && (($incOldBal == 1) ||($incOldBal == 2)))
						{
							$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  style="font-size: 11px; white-space: nowrap !important;" class="ar payment title wrp <?php echo $class; ?>" <?php echo ($isExport ? '' : (!empty($price) ? 'colspan="4"' : 'colspan="3"')); ?> >(-) <?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 && $currencyCode != $expCurrencyCode)
								{
									echo '<td colspan="2" class="ar forexAmount value export wrp ' . $class . '">';
									echo '<small style="font-size:12px;">' . $expCurrencyCode . '</small>&nbsp;&nbsp;';
									echo $tpi['forexAmount'];
									echo '</td>';
								}
								if (!$isExport || ($isExport && $currencyCode == $expCurrencyCode))
								{
									echo '<td class="ar payment value wrp ' . $class . '">' . formatAmount($tpi['allotAmt'] + $tpi['tdsAmt'], $currencyCode, "-", true,$currFraction) . '</td>';
								}
							?></tr><?php
								if ($writeOff)
								{
									echo '<tr><td class="ar payment title wrp" '.($isExport ? '' : 'colspan="2"').'>Write-Off</td>';
									if ($isExport && $currencyCode != $expCurrencyCode) {
										echo '<td class="ar forexAmount value export wrp ">' . $forexWriteOff. '</td>';
									}
									echo '<td colspan="2" class="ar payment value wrp" '.($isExport && $currencyCode != $expCurrencyCode ? 'colspan="2"':'').'>'.formatAmount($writeOff, $currencyCode, "-", true,$currFraction).'</td></tr>';
								}
							}
						}
			if (!empty($f_dueAmount) && empty($outstanding) && !empty($incOldBal) && (($incOldBal == 1) ||($incOldBal == 2)))
						{
							?><tr>
								<td style= "font-size: 11px;" class="ar dueamt title wrp" <?php echo $isExport ? '' : 'colspan="2"'; ?>><?php echo $dueAmount_text; ?></td><?php
							if ($isExport && $currencyCode != $expCurrencyCode)
						{
							// echo '<td class="ar total value vam" colspan="3"><b>Total</b>:&nbsp;&nbsp;&nbsp;<b>' . $excTaxableAmount . '</b><br/><small><small style="font-size:12px;">' . $expCurrencyCode . '</small></small></td>';
							echo '<td colspan="2" class="ar forexDueAmount value wrp">';
							echo '<b>' . $excTaxableAmount . '</b><br/><small><small style="font-size:12px;">' . $expCurrencyCode . '</small></small>';
							echo '</td>';
						}
						else
						{
							// echo '<td class="ar total value vam" colspan="3" ><b>Total</b>:&nbsp;&nbsp;&nbsp;<b>' . $totAmount . '</b></td>';
							echo '<td class="ar dueamt value wrp"><b>';
							echo '<b>' . $totAmount . '</b>';
							echo '</b></td>';
						}
?>
							</tr><?php
						}
						?>
						<?php 
		if(sal_mod_active(POINTS_MOD)  && !empty($points) && !empty($points['pOut_value'])){		
?>
		<tr class="balance">
			<td <?php
					$colspan = 3;
					if (($price) && ($paper=='3' || $paper=='4'))
					{
						$colspan++;
					}
					if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
					{
						$colspan++;
					}
					$colspan = "colspan=\"" . $colspan . "\"";
					echo $colspan;
					 ?> 
				class="npt npb">
				<?php if ($tType == 'SR') { ?>
					(-) Redeem Points (<?php echo $points['pOut']; ?>) :
				<?php } elseif ($tType == 'SC') { ?>
					(+) Return Points (<?php echo $points['pIn']; ?>) :
				<?php } ?>
				</td>
			<td class="ar npt npb"><?php echo $points['pOut_value']; ?></td>	
			</tr>
			<?php } if(sal_mod_active(POINTS_MOD)  && !empty($points) && !empty($points['pOut_value'])){ ?>
			<tr class="dashed1 dashed2">
				<td <?php
						$colspan = 3;
						if (($price) && ($paper=='3' || $paper=='4'))
						{
							$colspan++;
						}
						if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
						{
							$colspan++;
						}
						$colspan = "colspan=\"" . $colspan . "\"";
						echo $colspan; ?> class="npt npb" style="white-space: nowrap !important;"><h2>Grand Total :</h2></td>
				<td  class="ar npt npb"><h2><?php echo formatAmount($totAmount, $currencyCode, '','',$currFraction); ?></h2></td>		
			</tr>
			<?php
			} 
			if(sal_mod_active(POINTS_MOD)  && !empty($available_points)){?>
				<tr>
					<td <?php
						$colspan = 4;
						if (($price) && ($paper=='3' || $paper=='4'))
						{
							$colspan++;
						}
						if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
						{
							$colspan++;
						}
						$colspan = "colspan=\"" . $colspan . "\"";
						echo $colspan; ?>  class="npt npb">
						<?php
							if(empty($available_points['pIn']) && !empty($availpoavailable_pointsints['pOpen'])) {
									echo mylang("AvailablePoints")." (Bal) : ".$available_points['pOpen'];
							}?>
							<?php if(!empty($available_points['pIn']) && empty($available_points['pOpen'])) {
									echo mylang("AvailablePoints")." (Added) : ".$available_points['pIn'];
							}?>
							<?php if(!empty($available_points['pOpen']) && !empty($available_points['pIn'])) {
								echo  mylang("AvailablePoints"). "(Bal : ".$available_points['pOpen']. " + Added : ".$available_points['pIn'] ." )  :  " .$availpoints['pOpen'];
							}?>
					</td>
				</tr>
			
	<?php	} ?>
	<?php 					
	if (!empty($outstanding) && !empty($incOldBal) && ($incOldBal == 2))
	{
		if (!empty($oldDue))
		{
?>
	<tr class="oldDue">
		<td style="white-space: nowrap !important;"><b><?php echo mylang("ExistingBalance") ?></b></td>
<?php	if(($price) && ($paper=='3' ||  $paper=='4'))
		{
			echo '<td class="ac"></td>';
		}

		if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
		{
			echo '<td></td>';
		}
		if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
			echo '<td></td>';
		}

?>		<td class="ar" colspan="2"><b><?php echo $oldDue; ?></b></td>
	</tr>
	
<?php
		}
		if (!empty($newDue))
		{
?>
	<tr class="newDue dashed1 dashed2">
		
		<td><b><?php echo mylang("TotalBalance") ?></b></td>
<?php
		if(($price) && ($paper=='3' ||  $paper=='4'))
		{
			echo '<td class="ac"></td>';
		}

		if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
		{
			echo '<td></td>';
		}
		 if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
			echo '<td></td>';
		}
?>		<td class="ar" colspan="2"><b><?php echo $newDue; ?></b></td>
	</tr>
<?php
		}
	}
?>
	

	<?php
	
		if ($showreceivedAmt)
		{
	?>
		<tr class="totAmountCurr">
			<td class="npt npb"><?php echo lang("Cash") ?></td>
			<td class="ar npt npb"></td>
<?php
			if(($price) && ($paper=='3' ||  $paper =='4'))
			{
				echo '<td class="npt npb"></td>';
			}

			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				echo '<td class="npt npb"></td>';
			}
			 if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
				echo '<td></td>';
			}
?>			<td class="ar npt npb"><?php echo $totAmountCurr; ?></td>	
		</tr>
		<tr class="recAmtCurr">
			<td class="npt npb"><?php echo lang("Cash tendered") ?>:</td>
			<td class="ar npt npb"></td>
<?php
			if(($price) && ($paper=='3' ||  $paper =='4'))
			{
				echo '<td class="npt npb"></td>';
			}

			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				echo '<td class="npt npb"></td>';
			}
			 if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
				echo '<td class="npt npb"></td>';
			}
?>			<td class="ar npt npb"><?php echo $recAmtCurr; ?></td>	
		</tr>
<?php
		if ($showbalance)
		{
?>
			<tr class="balance dashed2">
				<td class="npt npb">Balance :</td>
				<td class="ar npt npb"></td>
<?php
				if(($price) && ($paper=='3' ||  $paper =='4'))
				{
					echo '<td class="npt npb"></td>';
				}

				if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
				{
					echo '<td class="npt npb"></td>';
				}
				if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
					echo '<td class="npt npb"></td>';
				}
?>
				<td class="ar npt npb"><?php echo $balAmtCurr; ?></td>	
			</tr>
<?php
			}
		}
	}
?>
</tbody>
</table>
<table class="noborder vam itemdetails">
<tbody>
	<tr class="e&oe">
		<td <?php
			$colspan = 3;
			if (($price) && ($paper=='3' || $paper=='4'))
			{
				$colspan++;
			}
			if(!empty($mrpInPurSal) && !empty($isMrpCol) && $hasMrp && ($paper=='3' ||  $paper =='4'))
			{
				$colspan++;
			}
			if($isSlNoCol && ($paper=='3' ||  $paper =='4')){
				$colspan++;
			}

			$colspan = "colspan=\"" . $colspan . "\"";
			echo $colspan; ?> class="ar"><small><?php echo mylang("E&O.E")?></small></td>
	</tr>
</tbody>
</table>
<?php
//Bank details display for "RETAIL INVOICE"
//********************************************
 if(!empty($isBankDetail) && ($tType == 'SR' && !empty($bank)))
{ 
?><div>
			<table class="bank accInfo noborder nopadding nwrap" border="0" cellspacing="0" cellpadding="0" <?php echo !empty($showVerticalLine) ? 'style="width:100%;padding:5px;border-left: 4px solid '.$headColor.';page-break-inside: avoid"' : 'style="page-break-inside: avoid"' ?> autosize="1">
					<tbody>
					<?php if(!empty($isBankDetail) && $isBankDetail !=4){ ?>

								<tr>
									<td class="wrp" colspan="2" style="padding: 0px !important;">
										<p class="bank title nm">
										<?php if(empty($fontname)){ ?><?php echo mylang("OurBankDetails")?>
								<?php } else{?>
									<b><?php echo mylang("OurBankDetails")?></b>
									<?php } ?>
										</p>
									</td>
								</tr>
								<tr>
									<td class="accNumber title wrp vat" ><?php echo mylang("AccountNo"); ?></td>
									<td class="accNumber value wrp ">&nbsp;: <b><?php echo $bank['accNumber']; ?></b></td>
									
								</tr>
								<tr>
									<td class="accName title wrp vat"><?php echo mylang("AccountName");?></td>
									<td class="accName value wrp">&nbsp;: <b><?php echo $bank['accName']; ?></b></td>
								</tr>
								<tr>
									<td class="ifsc title wrp vat"><?php echo mylang("IFSCCode"); ?></td>
									<td class="ifsc value wrp">&nbsp;: <b><?php echo $bank['ifscRouteCode']; ?></b></td>
								</tr>
								<tr>
									<td class="bankName title wrp vat"><?php echo mylang("Bank"); ?></td>
									<td class="bankName value wrp">&nbsp;: <b><?php echo $bank['bankName']; ?></b></td>
								</tr>
								<tr>
									<td class="branch title wrp vat"><?php echo mylang("Branch"); ?></td>
									<td class="branch value wrp">&nbsp;: <b><?php echo $bank['branchName']; ?></b></td>
								</tr>
							
							<?php
								//if(!empty($company['countryCode']) && $company['countryCode'] != 'IN'){
								if($isExport){
									if ($swiftCode) {
										?><tr>
											<td class="swift title wrp vat"><?php echo mylang("SwiftCode"); ?></td>
											<td class="swift value wrp">&nbsp;: <b><?php echo $bank['swiftCode']; ?></b></td>
										</tr><?php
									}
									if (!empty($corrBank))
									{
										?><tr>
											<td class="corrBank title wrp"><?php echo mylang("CorrespondentBank"); ?></td>
											<td class="corrBank value">&nbsp;: <b><?php echo $bank['corrBank']; ?></b></td>
										</tr><?php
									}
									if ($adCode) {
										?><tr>
											<td class="dealer title wrp"><?php echo mylang("ADCode"); ?></td>
											<td class="dealer value wrp">&nbsp;: <b><?php echo $bank['adCode']; ?></b></td>
										</tr><?php
									}
								}
						}
				?>
				<?php  
				if(!empty($upi) && !empty($isBankDetail) && $isBankDetail == 3){?>
					<tr>
						<td class= "nb" style="padding: 0px !important;"><?php echo mylang("UPI");?></td>
						<td class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $upi['id']; ?></b></td>
					</tr>	
				<?php } ?>
				<tr>
					<?php if (!empty($upi)) { 
									if(!empty($isBankDetail) && $isBankDetail == 2){
										if ($tType == 'SR') {
											$val = !empty($totAmount) ? $totAmount : '';
										} elseif (!empty($dueAmount)) {
											$val = $dueAmount;
										} else {
											$val = '';
										}
									}else{
												$val = '';
									}	
									if(!empty($upi) && !empty($isBankDetail) && $isBankDetail == 4){
										if ($tType == 'SR') {
											$val = !empty($totAmount) ? $totAmount : '';
										} elseif (!empty($dueAmount)) {
											$val = $dueAmount;
										} else {
											$val = '';
										}
									}
						
					?>
						
						<td colspan="2" style="text-align: center;" ><br>
							<barcode code="<?php echo getUpiLink($upi["name"], $upi["id"], $val , $tNo); ?>" class="qr" size="1" text="0" disableborder="1" type="QR" />
						</td>
					<?php } ?>
				</tr>
			</tbody>
		</table>
</div><br>
<?php
}
?>
<?php if(!empty($isBankDetail) && !empty($accType) && $tType != 'SR' ){
		if(!empty($upiId)) {
			$upi['name'] = $bankAccName;
			$upi['id'] = $upiId;
		}
?>
<div>
	<table class="noborder vam itemdetails" cellspacing="0"  cellpadding="0">	
		<tbody>
		<?php if(!empty($isBankDetail) && $isBankDetail !=4){ ?>
			<tr>
				<td class="wrp" colspan="2" style="padding: 0px !important;">
					<p class="bank title nm">
						<?php if(empty($fontname)){ ?>
								<?php echo mylang("OurBankDetails")?>
						<?php } else{?>
								<b><?php echo mylang("OurBankDetails")?></b>
						<?php } ?>
					</p>
				</td>
			</tr>
			<tr>
				<td class= "nb" style="padding: 0px !important;"><?php echo mylang("AccountNo"); ?></td>
				<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $accNumber; ?></b></td>
			</tr>
			<tr>
				<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("AccountName");?></td>
				<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $bankAccName; ?></b></td>
			</tr>
			<tr>
				<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("IFSCCode"); ?></td>
				<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $ifscRouteCode; ?></b></td>
			</tr>
			<tr>
				<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("Bank"); ?></td>
				<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $bankName; ?></b></td>
			</tr>
			
			<tr>
				<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("Branch"); ?></td>
				<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $branchName; ?></b></td>
			</tr>
			<?php  
			if(!empty($upi) && !empty($isBankDetail) && $isBankDetail == 3){?>
			<tr>
				<td class= "nb" style="padding: 0px !important;"><?php echo mylang("UPI");?></td>
				<td class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $upi['id']; ?></b></td>
			</tr>	
			<?php }		
							?>
			<?php if(!empty($company['countryCode']) && $company['countryCode'] != 'IN'){
						if (!empty($swiftCode)) {
							?><tr>
								<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("SwiftCode"); ?></td>
								<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $swiftCode; ?></b></td>
							</tr><?php
						}
						if (!empty($corrBank))
						{
							?><tr>
								<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("CorrespondentBank"); ?></td>
								<td  class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $corrBank; ?></b></td>
							</tr><?php
						}
						if (!empty($adCode)) {
							?><tr>
								<td  class= "nb" style="padding: 0px !important;"><?php echo mylang("ADCode"); ?></td>
								<td class= "nb" style="padding: 0px !important;">&nbsp;: <b><?php echo $adCode; ?></b></td>
							</tr><?php
						}
					}
			?>
			<?php }?>
			<tr>
			<?php if (!empty($upi)) { 
						if(!empty($isBankDetail) && $isBankDetail == 2){
								if ($tType == 'SR') {
									$val = !empty($totAmount) ? $totAmount : '';
								} elseif (!empty($dueAmount)) {
									$val = $dueAmount;
								} else {
									$val = '';
								}
						}else{
								$val = '';
						}
						if(!empty($upi) && !empty($isBankDetail) && $isBankDetail == 4){
							if ($tType == 'SR') {
								$val = !empty($totAmount) ? $totAmount : '';
							} elseif (!empty($dueAmount)) {
								$val = $dueAmount;
							} else {
								$val = '';
							}
						}
			
			?>
				
				<td colspan="2" style="text-align: center;" ><br>
					<barcode code="<?php echo getUpiLink($upi["name"], $upi["id"], $val , $tNo); ?>" class="qr" size="1" text="0" disableborder="1" type="QR" />
				</td>
			<?php } ?>
			</tr>
		</tbody>
	</table>
</div>
<br>
<?php }?>
<table class="noborder vam itemdetails">
<tbody>
<?php

	if (!empty($tNoBarcode))
	{
		?><tr class="tNoBarcode">
			<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac" style="padding:0 -5px 0 0">
				<barcode code="<?php echo $tNo; ?>" class="barcode" size="" height="0.6" text="0" type="C128A" />
			</td>
		</tr><?php
	}
	if( $tType == "SI" && $isComposition && $classCode == "REG") {
		?><tr class="isComposition">
			<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac" style="padding:0 -5px 0 0">
				<small style="font-size : 9px !important"> <?php echo "COMPOSITION TAXABLE PERSON, NOT ELIGIBLE TO COLLECT TAX ON SUPPLIES"; ?></small>
			</td>
		</tr><?php
	}
	if($notes){?>
		<tr  class="footerText">
			<td  <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="al"><?php echo mylang("Notes")?>:&nbsp;<?php echo $notes; ?></td><br>
		</tr>
	<?php }
	if ($terms)
	{
		?><tr  class="footerText">
			<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="al" ><?php
				?><?php echo mylang("Terms")?> : &nbsp;<?php echo nl2br($terms); ?>
			</td>
		</tr><?php
	}

			if(!empty($S_IRN) && !empty($S_IRN['SignedQRCode']))
			{
				$eInvoicesize = !empty($eInvoicesize) ? $eInvoicesize : 1;

?>			<tr class="qr">
				<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac">
					<barcode code="<?php echo $S_IRN['SignedQRCode']; ?>" class="qr" size="<?php echo $eInvoicesize; ?>" text="0" disableborder="1" type="QR" />
				</td>
			</tr>
<?php
			}


	if (!empty($isPreparedBy) && !empty($updatedBy)) 
	{ 

		?><tr  class="footerText">
			<td ><?php echo '<span class="" style="margin-top:40px;color:black" ><br>' . $updatedBy . ' <br> '.  mylang("PreparedBy") . '</span>'; ?>
			</td>
		</tr><?php
	
		
	}
	if (!empty($transQr) && !empty($qrTransImg))
	{
		?><tr class="transQr">
			<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac" style="padding:0 -5px 0 0">
				<img src="<?php echo $qrTransImg; ?>" style="width:4cm;height:4cm;" class="" />
			</td>
		</tr><?php
	}
	if(!empty($footerText))
	{
		?><tr  class="footerText">

<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac">
				<?php echo nl2br($footerText); ?>
			</td>
		</tr><?php
	}
	
	if (!empty($waterMark))
	{
		?><tr class="waterMark"><td colspan="4" class="np ac"><p class="nm watermark"><?php echo $waterMark; ?></p></td></tr><?php
	}

	if (!empty($isPrintDate))
	{
		$config = get_company("config");
		?><tr class="printDate">
			<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="np">
			<small style="letter-spacing: 1px;">Printed on : <?php echo date('d/m/Y h:i A', gmt_to_local(now(), $config["timeZone"])); ?></small>
		</td>
		</tr><?php
	}
?>
	</tbody>
</table><?php
}

if($tType =='SP' && $tType !='PP' )
{
?><table class="noborder vam itemdetails" cellspacing="0" cellpadding="0" align="center">
		<tbody>
			<tr class="address">
				<td><?php echo mylang("AmountReceivedfrom");?></td>
				<td><?php
					$billing = isset($billing)?$billing:array();
					// $conTaxNo
					if(!empty($conTaxNo))
					{
						$billing["gstin"] = $conTaxNo;
					}
					else if(!empty($contact['taxNo']))
					{
						$billing["gstin"] = $contact['taxNo'];
					}

					$this->subview($addressView, $billing);
				?></td>
			</tr>
<?php
			if($isExport)
			{
?>			<tr class="totForexAmt">
				<td><?php echo mylang("AmountReceived")?> (<?php echo $expCurrencyCode ?>)</td>
				<td><b><?php echo $totForexAmt ?></b> (<?php echo $exp_amt_str . " only"; ?>)</td>
			</tr>
<?php		}

			if(!$isExport || !$is_email)
			{
?>				<tr class="totAmount">
					<td><?php echo mylang("AmountReceived")?> (<?php echo $currencyCode; ?>)</td>
					<td><b><?php echo $totAmount; ?></b> (<?php echo $amt_str . " only"; ?>)</td>
				</tr>
			<?php		}
				if((!$isExport || !$is_email) && !empty($isrcptbalance) && !empty($balance) && $balance >0) {
					$balance = formatAmount($balance, $isExport ? $expCurrencyCode : $currencyCode,'','', $currFraction);
			?>
				<tr>
					<td><?php echo mylang("OutstandingBalance")?></td>
					<td><?php echo $balance; ?></td>
				</tr>
			<?php }

			if (!empty($paymentName) && $paymentName != "Other")
			{
?>				<tr class="paymentName">
					<td><?php echo mylang("PaymentMode"); ?></td>
					<td><?php echo $paymentName; ?></td>
				</tr><?php
			}
			if (!empty($ref))
			{
?>			<tr class="ref">
				<td><?php echo mylang("Reference");?></td>
				<td><?php echo $ref; ?></td>
			</tr><?php
			}
			if (!empty($notes))
			{
?>			<tr class="notes">
				<td><?php echo mylang("Remarks");?></td>
				<td> <?php echo $notes; ?></td>
			</tr><?php
			}
			if (!empty($payments) && ( $tType == 'SP' || empty($isPrint) || $isPrint != 1) ){ ?>
			<tr>
				<td><?php echo "Linked Transactions";?></td>
				<td> <?php $linkednotes = "<span>";
					foreach ($payments as $val)
					{
						if(!empty($val['tType']) && $val['tType'] != "AO"){
						$linkednotes.= '<a style="color:'.(!empty($bgColor) && $bgColor != 'transparent' ? $bgColor : '#4183c4').';text-decoration: none;padding: 2px 2px;" href="'.base_url().'index.php/sales/'.$transUrl[$val['tType']].'/'.$val['allotTransId'].'" target="_blank"><b>'. $val['tNo'].'</b></a><span>,</span>';
						}
					}
					$linkednotes .= "</span>";
					echo $linkednotes; ?></td>
			</tr><?php } ?>
			<tr>
				<td class="ac bot" colspan="2" width="100%" style="border-top:1px solid black"><?php echo mylang("Thanks");?></td>
			</tr>
			<?php
			if(!empty($footerText))
			{
?>			<tr  class=" dashed1 footerText">

				<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac">
				<?php echo nl2br($footerText); ?>
				</td>
			</tr><?php
			}
?>		</tbody>
	</table><br>
	
	<table class="noborder vat">
		<tr>
			<?php if (!$is_email || (!empty($company['invsign']) && !empty($signOnPrint)))
			{
			?>
			<td width="50%"  style="text-align:left"><?php
				if (!$is_email && !empty($isCustSign))
				{
					echo '<span class="" style="margin-top:40px;">'.mylang("CustomerSignature").'</span>';
				}
			?></td>
			<td width="50%" style="text-align:right"><?php
				echo '<div class="ar">'.mylang("ForcompanyName"). '</span></div>';
				echo '<br><span class="sign" style="float:right;">';
				// (!$isPrint || $is_email ||
				if (!empty($company['invsign']) && !empty($signOnPrint))
				{
					echo '<img src="' . $company['invsign'] . '" width="" height="" />';
				}
				else if (!empty($company['invsign']))
				{
					echo '<br><br>';
				}
				echo '<br>'.mylang("AuthorizedSignature").'</span>';
			?></td>
			<?php } ?>
		</tr>
	</table> 
<?php } ?>
<?php 
	if(!empty($isfooterimg) && !empty($footerImg))
	{		
		$img_height = '30px';
		echo '<table align="left" cellspacing="0" cellpadding="0" class="footTable noborder nbt wpw" autosize="1" style="page-break-inside: avoid"><tbody><tr><td colspan="2"  width="100%"><div class="" ><img style="height:auto; width: 100%;display: inline-block;" src="' . $footerImg . '" /></div></td></tr></tbody></table>';
	}


if($tType =='PP' && $tType !='SP' )
{		if(!empty($balance)){
			 $caption =  ($balance && $balance < 0 ) ?  'Cr' : '';
			if($balance < 0) {
				$balance = -($balance);
			}
			$balance =  formatAmount($balance, $currencyCode, "-", true,$currFraction);
		}
?><table class="noborder vam itemdetails" cellspacing="0" cellpadding="0" align="center">
		<tbody>
			<tr class="address">
				<td>Paid To</td>
				<td><?php
					// $conTaxNo
					if(!empty($conTaxNo))
					{
						$billing["gstin"] = $conTaxNo;
					}
					else if(!empty($contact['taxNo']))
					{
						$billing["gstin"] = $contact['taxNo'];
					}

					$this->subview($addressView, $billing);
				?></td>
			</tr>
<?php
			if($isExport)
			{
?>			<tr class="totForexAmt">
				<td><?php echo mylang("Amount")?> (<?php echo $expCurrencyCode ?>)</td>
				<td><b><?php echo $totForexAmt ?></b> (<?php echo $exp_amt_str . " only"; ?>)</td>
			</tr>
<?php		}

			if(!$isExport || !$is_email)
			{
?>				<tr class="totAmount">
					<td><?php echo mylang("Amount")?> (<?php echo $currencyCode; ?>)</td>
					<td><b><?php echo $totAmount; ?></b> (<?php echo $amt_str . " only"; ?>)</td>
				</tr>
<?php		}

			if (!empty($paymentName) && $paymentName != "Other")
			{
?>				<tr class="paymentName">
					<td><?php echo mylang("PaymentMode"); ?></td>
					<td><?php echo $paymentName; ?></td>
				</tr><?php
			}
			if (!empty($ref))
			{
?>			<tr class="ref">
				<td><?php echo mylang("Reference");?></td>
				<td><?php echo $ref; ?></td>
			</tr><?php
			}
			if (!empty($incOldBal))
			{
?>			<tr class="balance">
				<td><?php echo mylang("Balance");?></td>
				<td><?php echo (!empty($balance) ? $balance : '-') .(!empty($caption) ? $caption : ''); ?></td>
			</tr><?php
			}
			if (!empty($notes))
			{
?>			<tr class="notes">
				<td><?php echo mylang("Remarks");?></td>
				<td> <?php echo $notes; ?></td>
			</tr><?php
			}

			if(!empty($footerText))
			{
?>			<tr  class=" dashed1 footerText">

				<td <?php echo ($paper=='3' || $paper=='4') ? 'colspan="4"' : 'colspan="3"';?> class="ac">
				<?php echo nl2br($footerText); ?>
				</td>
			</tr><?php
			}

?>		</tbody>
	</table><br>
	
	<table class="noborder vat">
		<tr>
			<?php if (!$is_email || (!empty($company['invsign']) && !empty($signOnPrint)))
			{
			?>
			<td width="50%"  style="text-align:left"><?php
				if (!$is_email && !empty($isCustSign))
				{
					echo '<span class="" style="margin-top:40px;">'.mylang("CustomerSignature").'</span>';
				}
			?></td>
			<td width="50%" style="text-align:right"><?php
				echo '<div class="ar">'.mylang("ForcompanyName"). '</span></div>';
				echo '<br><span class="sign" style="float:right;">';
				// (!$isPrint || $is_email ||
				if (!empty($company['invsign']) && !empty($signOnPrint))
				{
					echo '<img src="' . $company['invsign'] . '" width="" height="" />';
				}
				else if (!empty($company['invsign']))
				{
					echo '<br><br>';
				}
				echo '<br>'.mylang("AuthorizedSignature").'</span>';
			?></td>
			<?php } ?>
		</tr>
	</table> 
<?php } ?>

<style>
	<?php if(empty($printname)){ ?>
	table tbody.details tr td{
		font-weight:  bold !important;
	}
	table tr.det td {
		padding-bottom: 3px !important;
	}
	<?php } ?>
</style>
