<?php defined('BASEPATH') OR exit('No direct script access allowed');
//Label declaration
//**********************
$tNo_text = (!empty($label) && !empty($label['No'])) ? $label['No'] :lang("PaymentNo");
$tDate_text =  (!empty($label) && !empty($label['Date'])) ? $label['Date'] :lang("PaymentDate"); 
$custFieldsView = !empty($custFieldsView) ? $custFieldsView : 'export/customfields';
//*****************



	$addressView = 'export/address/salinv';
	$detailsView = 'export/details/salinv2';
	$letterPadHeader = 'export/letterpadHeader';
	$arr = array(
		'SE' => 'Quote',
		'SC' => 'Sales Credit note',
		'SD' => 'Sales Debit note',
		'SR' => 'Cash invoice',
		'SI' => 'Tax Invoice'
	);
	$transUrl = array(
		'PQ' => 'quotes',
		'PO' => 'estimate',
		'PI' => 'bills',
		'EI' => 'expenses',
		'PE' => 'expenses',
		"GR" => 'receiptnote',
		'PC' => 'creditnote',
		'PD' => 'debitnote',
		'PP' => 'payments',
		'AO' => 'opening',
		'SC' => 'creditnote',
		'SI' => 'invoice'
	);
	if(!empty($sigOnPrint)) {
		$isSign = 1;
	}
	$totAmount =formatAmount($totAmount, $currencyCode, "-", true,$currFraction);
	$headColor = (empty($is_email) ? $bgColor : $color);
	if($tType == 'SI' && in_array($classCode, array('EXPWP', 'EXPWOP', 'DEEM', 'EXPBO')))
	{
		$arr['SI'] = 'Export Invoice';
	}
	$transType = !empty($arr[$tType]) ? $arr[$tType] : lang($tType);

	$fontname = (!empty($fontname) && $fontname != 'null')  ? $fontname : ""; 
	$this->view('export/css', array(
		"fontname" => $fontname
	));
	$isTds = false;
	$isDue = false;
	$isWriteOff = false;
	$istot = false;
	if(!empty($payments)) {
		foreach($payments as $key => $val)
		{
			if(!empty($val['totAmount'])){
				$istot = true;
			}
			if(!empty($val['dueDate']) && $val['dueDate'] == '0000-00-00') {
				$val['dueDate'] = '';
			}
			else {
				$val['dueDate'] = date("d/m/Y", strtotime($val['dueDate']));
			}
			if(!empty($val['relDate']) && $val['relDate'] == '0000-00-00') {
				$val['relDate'] = '';
			}
			else if (!empty($val['relDate']))
			{
				$val['relDate'] = date("d/m/Y", strtotime($val['relDate']));
			}
			$val['tdsAmount'] = strval($val['tdsAmount']);
			if($val['isTds']){
				$isTds = true;
			}
			if(!empty($val['writeOff']) && $val['writeOff'] != '0.00'){
				$isWriteOff = true;
			}
			if($val['dueAmount'] != 0) {
				$isDue = true;
			}
			if($val['dueAmount'] < 0) {
				$val['dueAmount'] = -($val['dueAmount']);
			}
			$val['dueAmount'] =  formatAmount($val['dueAmount'], $currencyCode, "-", true,$currFraction);
			$payments[$key] = $val;
		} 
	}
	else {
		$isDue = false;
		$payments[0]['allotAmt'] = $totAmount;
		$payments[0]['dueAmount'] ='';
		$payments[0]['tdsAmount'] =  '';
		$payments[0]['dueDate'] =  '';
		$payments[0]['tNo'] =  '';
		$payments[0]['totAmount'] =  '';
		$payments[0]['dueDate'] = '';
	}
	//print_R($isTds);exit;
?><page footer="page">
<?php 
	if (isset($download) && $download==-1){
		echo '<bookmark title="'.$tNo.'" level="0" ></bookmark>';
	} ?>
	<div>
		<div class="ac"><?php
			$this->subview($letterPadHeader);
		?><?php if(!empty($paper) && $paper !='A5'){ echo '<br>'; }?>
			<h2 class="highlght margin"><?php echo $titleText; ?></h2>
		</div>
	</div>
	<table class="noborder vat nopadding" cellspacing="0" cellpadding="0" style="width:100%" align="right">
		<!--<col style="width:50%" />
		<col style=<?php echo empty($isPrint) ? "width:15%" : "width:25%" ?> />-->
		<tbody>
			<tr>
				<?php if (isset($billing)) { ?>
				<td  style="text-align:left"  width="60%">Paid To : <?php
					$this->subview($addressView, $billing);
				?></td>
				<?php } ?>
				<td style="text-align:left" width="40%">
					<span><?php echo $tNo_text;?> <span style="margin-left:7px;">: <b><?php echo $tNo; ?></b></span></span><br>
					<?php if(!empty($tDate)) { ?><?php echo $tDate_text;?> <b><?php echo $tDate; ?></b><?php } ?>
					<?php if(!empty($paymentName) && empty($ispaymode)) { ?><br><span><?php echo mylang("PaymentMode");?> : <?php echo $paymentName; ?></span><?php } ?>
					<?php if(!empty($ref)) { ?><br><span style=" width: 200px; word-break: break-all;white-space: normal;"><?php echo mylang("Reference");?> : <?php echo $ref; ?></span><?php } ?>
					
				</td>
			</tr>
		</tbody>
	</table><?php if(!empty($paper) && $paper != 'A5'){ echo "<br>"; } ?>
	<table class="noborder vat nopadding" cellspacing="0" cellpadding="0" style="width:25%;">
		<!--<col style=<?php echo empty($isPrint) ? "width:7%" : "width:14%" ?> />
		<col style="width:50%" />-->
		
		<!--<tbody>
			<tr>
				<td  width="10%">Payment Mode</td>
				<td width="15%" style="text-align:left;">:  <?php echo $paymentName; ?></td>
				
			</tr>
			
			<tr>
				<?php if (!empty($ref)){ ?>
				<td width="10%" >Reference</td>
				<td width="15%">: <?php echo $ref; ?></td>
				<?php } ?>
			</tr>
			
		</tbody>-->
	</table><?php if(!empty($paper) && $paper != 'A5'){ echo "<br><br>"; } ?>
	<br>
	<?php if(!empty($fields))
		{ 
			$this->view($custFieldsView);
		}
	?>
	<table cellspacing="0" cellpadding="0" style="width:100%; table-layout:fixed;" class="det">
		<thead>
			<tr>
				<th style="width:35%;"><?php echo mylang("TransactionDescription"); ?></th>
				<?php if($istot) { ?><th style="width:15%;" class="ar"><?php echo mylang("Amount"); ?></th><?php } ?>
				<?php if($isTds) { ?><th style="width:10%;" class="ar"><?php echo mylang("TDS"); ?></th><?php } ?>
				<?php if($isWriteOff) { ?><th style="width:10%;" class="ar"><?php echo 'Write off'; ?></th><?php } ?>
				<?php if($isDue) { ?><th style="width:15%;" class="ar"><?php echo mylang("DueAmount"); ?></th><?php } ?>
				<th style="width:15%;" class="ar"><?php echo mylang("Payment"); ?></th>
			</tr>
		</thead>
		<tbody>
		<?php foreach($payments as $val) { ?>
			<tr>
				<td>
					<?php
						if(!empty($val['tNo'])){
							if((!empty($isPrint) || $isPrint == 1) || !empty($val['relNumber'])) {
								echo 'Bill# ' . $val['tNo']; 
							} else {
								if($val['tType'] == 'JV'){
									echo 'Voucher# ' . $val['tNo']; 
								}else{
									$ttype = ($val['tType'] == 'SC' || $val['tType'] == 'SI') ? "sales" : "purchase";
									echo '<a style="color:#4183c4;text-decoration:none;" href="'.base_url().'index.php/'.$ttype.'/'.$transUrl[$val['tType']].'/'.$val['allotTransId'].'" target="_blank">'.ucfirst($transUrl[$val['tType']]).'#'.$val['tNo'].'</a>';
								}
							}
						}
						if($notes && empty($val['tNo'])){ echo $notes; }
						if(!empty($val['relNumber'])) {
							echo '<br>';
							if(!empty($isPrint) || $isPrint == 1) {
								echo 'Sup Inv# <b>'.$val['relNumber'].'</b>&nbsp;&nbsp;';
							} else {
								echo 'Sup Inv# <a style="color:#4183c4;text-decoration:none;" href="'.base_url().'index.php/purchase/'.$transUrl[$val['tType']].'/'.$val['allotTransId'].'" target="_blank"><b>'.$val['relNumber'].'</b></a>';
							}
							if(!empty($val['relDate'])) { echo $val['relDate']; }
						}
					?>
				</td>
				<?php if($istot) { ?><td class="ar"><?php echo $val['totAmount']; ?></td><?php } ?>
				<?php if($isTds) { ?><td class="ar"><?php echo $val['tdsAmount']; ?></td><?php } ?>
				<?php if($isWriteOff) { ?><td class="ar"><?php echo $val['writeOff']; ?></td><?php } ?>	
				<?php if($isDue) { ?><td class="ar"><?php echo $val['dueAmount']; ?></td><?php } ?>
				<td class="ar"><?php echo $val['allotAmt']; ?></td>
			</tr>
		<?php } ?>
		</tbody>
	</table>
	<?php if($istot){ ?>
		<table cellspacing="0" cellpadding="0" style="width:100%; table-layout:fixed;" class="nb np det">
			<tr>
				<td style="width:35%;" class="ar"><b><?php echo mylang("TOTAL");?></b></td>
				<?php if($istot) { ?><td style="width:15%;" class="ar"><b><?php echo $totAmount; ?></b></td><?php } ?>
				<?php if($isTds) { ?><td style="width:10%;" class="ar"><b><?php echo  !empty($tdstotal) ?   $tdstotal : ''; ?></b></td><?php } ?>
				<?php if($isWriteOff) { ?><td style="width:10%;" class="ar"><b><?php echo !empty($writeofftotal) ?  $writeofftotal : ''; ?></b></td><?php } ?>
				<?php if($isDue) { ?><td style="width:15%;" class="ar"></td><?php } ?>
				<td style="width:15%;" class="ar"><b><?php echo !empty($allotAmttotal) ?  $allotAmttotal : ''; ?></b></td>
			</tr>
		</table>
	<?php } ?>

	<?php if(!empty($balance)){
			 $caption =  ($balance && $balance < 0 ) ?  ' Cr' : '';
			if($balance < 0) {
				$balance = -($balance);
			}
			$balance =  formatAmount($balance, $currencyCode, "-", true,$currFraction);
		}
	?>
	<?php if($incOldBal){?>
	<table cellspacing="0" cellpadding="0"  style="width:100%; table-layout:fixed;"  class="nb np det"                                                                               >
		<!--<col style="width:15%;" />
		<col <?php echo $isTds ? 'style="width:30%;"' :  'style="width:45%;"' ?>/>
		<?php if($isTds) { ?>		<col style="width:15%;" /> <?php } ?>
		<col style="width:20%;" />
		<col style="width:20%;" />
		-->
			<tr>
				<!--<td width="15%"></td>-->
				<td <?php echo $isTds ? 'width=30%' :  'width=45%' ?> class="ar"><b><?php echo mylang("Balance");?></b></td>
				<?php if($isTds) { ?><td class="ar" width="15%"></td><?php } ?>
				<td></td>
				<td class="ar" <?php echo $isDue ? 'width=20%' : "width=40%" ?>><b><?php if($isDue) { echo $balance . $caption ; }?></b></td>
				<td class="ar" width="20%"><b><?php if(!$isDue) { echo (!empty($balance) ? $balance : '-') .(!empty($caption) ? $caption : '');} ?></b></td>
				
			</tr>	
			<!--<tr>
				<td class="al" <?php echo $isTds ? 'colspan="4"' : 'colspan="3"' ?>><p class="nm amtinwords">Amount In words : (<?php echo $currencyCode; ?>) <?php  echo $amt_str . " only"; ?></p></td>
			</tr>-->
		
	</table>
	<?php }?>
	<!--/******Payment modes display as table - Setting (Payment mode breakup) ****/-->
	<?php if(!empty($ispaymode)){?>
	<table cellspacing="0" cellpadding="0" style="width:100%;" class="det">
		<thead>
			<tr>
				<td><b>Paid from A/C</b></td>
				<td><b>Payment mode</b></td>
				<td><b>Reference</b></td>
				<td class="ar"><b>Amount Paid</b></td>
			</tr>
		</thead>
		<tbody>
			<?php foreach($payModes as $pay){ 
			 $pay['pAmount'] =	formatAmount($pay['pAmount'] , $currencyCode, "-", true,$currFraction);
				?>
			<tr>
				<td><?php echo $pay['pAccName'];?></td>
				<td><?php echo $pay['pModeName'];?></td>
				<td><?php echo (!empty($pay['pRef']) ?$pay['pRef'] : "-");?></td>
				<td class="ar"><?php echo $pay['pAmount'];?></td>
			</tr>
			<?php } ?>
		</tbody>
	</table>
	<?php } ?>
	<br>
	<table align="left" cellspacing="0" cellpadding="0" class="footTable vat noBorder" autosize="1" style="page-break-inside: avoid">
	<!--<col style="width:50%;">
	<col style="width:50%;">-->
	<tbody>
	<tr>
		<td><?php echo "Amount paid : ";?> <?php echo $amt_str . " only"; ?></td>
	</tr><?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 mylang("Terms");?> :<br><?php echo nl2br($terms); ?></span></div>
			</td>
		</tr><?php
	}
	if(!empty($notes) || !empty($isTransDesc)){
				echo '<tr><td><span class="nm notes pretxt">'.  $notes .'</span></td></tr>';
	}
	
		if ((!$is_email || !empty($company['invsign'] || !empty($is_pdf))) && !empty(trim(lang("For"))))
		{
			$clr = ($headColor == 'transparent') ? 'black' : $headColor;
		if(!empty($isSign)) { ?><tr>

			<td class="ar bgColor" colspan="2" style="color:<?php echo $clr; ?>" ><?php echo mylang("ForcompanyName"); ?></td>
		</tr><?php } 
		}
		?><tr><?php
			$wid = '';
			if($paper == 'A5') {
				$wid = '20mm';
			}
			$hgt = '';
			if(empty($isSign)) {
				$hgt = '100px';
			}
			echo '<td valign="bottom" class="vab nbr custsign bgColor" style="height: '.$hgt.'">';
			if ((!$is_email  ||!empty($is_pdf)) && !empty($isCustSign))
			{
				$clr = ($headColor == 'transparent') ? 'black' : $headColor;
				
				echo '<span class="" style="margin-top:40px;color:'.$clr.'" >' . (($tType == DELCHALLAN) ? mylang("Received") : 'Received By') . '</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"><img src="' . $company['invsign'] . '" width="'.$wid.'" height="" /style="margin-top:0px;color:'.$clr.'" /></span><br>';
					if(!empty($isSign)) {
						echo '<span style="color:'.$clr.'">'.mylang("AuthorizedSignature").'</span>';
					}
				}
				else if (!empty($isSign) && (!$is_email || !empty($company['invsign'] || !empty($is_pdf))))
				{
					$clr = ($headColor == 'transparent') ? 'black' : $headColor;
					echo '<br><br><span class="" style="margin-top:40px;float:right;color:'.$clr.'">'.mylang("AuthorizedSignature").'</span>';
				}
			}
			echo '</td>';
		?></tr>
	</tbody>
</table>
</page>
<?php
		if(!empty($isfooterimg) && !empty($footerImg))
		{
					if($paper == 'A5')
					{
						$img_height = '75px';
					}
					else
					{
						$img_height = '120px';
					}
					
					echo '<table align="left" cellspacing="0" cellpadding="0" class="footTable 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 ( !empty($footerText))
		{
			echo '<p class="footerTxt nm pretxt">' . nl2br($footerText) . '</p>';
		}
?>
<style>
	.highlght {
		background-color: <?php echo $bgColor; ?>;
		color: <?php echo $foreColor; ?>;
    		padding: 6px;
		padding-top:<?php echo !empty($isPrint) ? '3px' : ''; ?>;
	    	font-weight: bold;
	    	font-size: 19px;
	}
	td {
		white-space: nowrap;
	}
	.margin {
		margin-top: 0px;
   		margin-bottom: 6px;
	}
</style>
