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

// 'isLogo, color, isDueDate, isSlNoCol, isItemDesc, isHsnCol, isBankDetail, isCustSign, isPageNo, isFullPage, isPrintDate, footerText, copiesToPrint, printType, topMargin, rightMargin, bottomMargin, leftMargin'
$dueDate_text = (!empty($label) && !empty($label['ValidTill'])) ? $label['ValidTill'] :(!empty($dueDate_text)? $dueDate_text : "DueDate") ;
$rel_tNo_text =  (!empty($label) && !empty($label['OrderNo'])) ? $label['OrderNo'] : (!empty($rel_tNo_text) ? $rel_tNo_text : '') ;
$rel_tDate_text =  (!empty($label) && !empty($label['OrderDate'])) ? $label['OrderDate'] : (!empty($rel_tDate_text) ? $rel_tDate_text : '') ;
$pdf_type = (!empty($label) && !empty($label[$pdf_type])) ? $label[$pdf_type] : (!empty($pdf_type) ? $pdf_type : '') ;
	if (!isset($isCompact))
	{
		$isCompact = false;
	}
	if($tType == 'JO'){
		$transQr = false;
	}

	$addressView = !empty($addressView) ? $addressView : 'export/address/salinv';
	$detailsView = !empty($detailsView) ? $detailsView : 'export/details/salinv';
	if($tType == 'SE'){
		$detailsView.="_est";
	}

	$detailsView = !empty($sections) ? "section/export/details/salinv" : $detailsView;

	$custFieldsView = !empty($custFieldsView) ? $custFieldsView : 'export/customfields';
	$exportDetailsView = !empty($exportDetailsView) ? $exportDetailsView : 'export/exportdetails';
	$letterPadHeader = 'export/letterpadHeader';
	$headColor = (empty($is_email) ? $bgColor : $color);
	if($tType == 'PI') {
		$billing['headColor'] = $headColor;
	}
	$billing['fontname'] = !empty($fontname) ? $fontname : '';
	$shipping['fontname'] = !empty($fontname) ? $fontname : '';
	$isQuote = false;
	
	if ($tType == 'SE' || $tType == 'SO' || $isExportInvoice)
	{
		$isQuote = true;
		//$pdf_type = '';
	}
	if ($isPrint && $isExport)
	{
		$isEstimate = true;
	}

	if($isPrint && $printcolor == 'transparent')
	{
		$foreColor = "#000000";
	}
	if (!empty($is_email) && $color != 'transparent')
	{
		$foreColor = "#ffffff";
	}
	
	$hasHSN = ($hasHSN && $isHsnCol) ? 1 : 0;

	if ($isShipping && $tType != 'PE' && (!$sameAsBill || empty($hidShipAdr)))
	{
		$isShipping = true;
	}
	else
	{
		$isShipping = false;
	}
	
	$pdff_type = $pdf_type;
	//For Normal Print clicks hide the pdf type
	if(!empty($isOriginal))
	{
		$pdff_type = '';
	}
	if($bgColor == 'transparent')
	{
		$showVerticalLine= false;
	}
	else
	{
		$showVerticalLine = true;
	}
	$fontname = (!empty($fontname) && $fontname != 'null') ? $fontname : ""; 
	$this->view('export/css', array(
		"fontname" => $fontname
	));
	
	if($tType == 'PC' || $tType == 'PD' || $tType == 'PE')
	{
		$company['logo'] = '';
	}

	if($dueDate == '0000-00-00')
	{
		$dueDate = '';
	}

	if (isset($download) && $download == -1)
	{
		echo '<bookmark title="'.$tNo.'" level="0" ></bookmark>';
	}
	if (!empty($isAllPageHeader))
	{
		echo '<htmlpageheader name="'.$pdf_type.'">';
	}
	if(!empty($isOriginal))
	{
		$printColors = '';
	}
	if(!empty($printColors))
	{
		$headColor = $printColors;
	}
	if(!empty($isSlip) && $pdf_type == 'Extra Copy') {
		$pdff_type = '';
	}

?>

	<div class="trans" >
		<a name="<?php echo $tNo; ?>"></a>
		<table border="0" cellspacing="0" cellpadding="0" class="noborder nopadding vat"><?php
			/*if (!empty($company['logo']))
			{
				if (empty($isPrint))
				{	
					echo '<col style="width: 15%">';
				}
				else {
					echo '<col style="width: 25%">';
				}
				echo '<col style="width: 45%">';
			}
			else
			{
				echo '<col style="width: 70%">';
			}*/
			?><!--<col style="width: 30%">-->
			<tbody>	<?php $colspan = ($tType == 'SH' && $isLogo == 2) ? '2' : '2' ?><?php
			
				if($isLogo == 2)
				{
					if($paper == 'A5')
					{
						$img_height = '75px';
					}
					else
					{
						$img_height = '120px';
					}
					
					echo '<tr><td  colspan ="'.$colspan.'"  width="100%"><div class="" ><img style="width: 100%;display: inline-block;" src="' . $company['fullHeaderImg'] . '" /></div></td></tr>';
					if($tType == 'PI')
					{
						echo '<tr><td colspan="'.$colspan.'"><hr></td></tr>';
					}
				}

				if($tType == 'PC' ||  $tType == 'PD')
				{
					echo '<tr><td colspan="' . $colspan . '">';
					$this->view($letterPadHeader); 
					echo '</td><tr>';
					echo '<tr><td colspan="' . $colspan . '"><hr></td></tr>';
				}

?>				<tr><?php
			
				if($isLogo == 1 || $isLogo == 0 || ($tType == 'PI' && $isLogo == 2))
				{
					if($tType != 'PI')
					{
						if (!empty($company['logo']))
						{
							echo '<td class="vat" style="display:block;height:auto;max-height: 120px;" width="25%"><div class="imgDiv fltlt" ><img src="' . $company['logo'] . '" /></div></td>';
						}
					}
					
					?><td <?php !empty($company['logo']) ? 'width="40%"' : 'width="25%"' ?>><?php
						if ($tType == 'PI' || $tType=='PC' || $tType == 'PD')
						{ 
							$contact_name = !empty($contact['company']) ? $contact['company'] : (!empty($contact['contactPerson']) ? $contact['contactPerson'] : (!empty($conName) ? $conName : (!empty($contact['name']) ?($contact['name']) : "")));
							$billing['name'] = $contact_name;
							if($contact_name || !empty($conName))
							{
								echo '<span>Supplier / Vendor</span>';
								$this->subview($addressView, $billing);
								
							}
							$isShowAddress = false;
							if($tType == 'PI' || $tType == 'PC' || $tType == 'PD')
							{
								if(!empty($conTaxNo))
								{
									echo '<p class="nm">'.mylang("GSTIN").' : '. $conTaxNo.'</p>' ;
								}
								echo '<p class="nm">'.mylang("SOS").' : '.$pos.'</p>' ;
							}
						}
						else if ($tType == 'PE')
						{
							echo $company["name"] . "<br>";

							if(!empty($company["city"]))
							{
								echo $company["city"];
								if($company["zip"])
								{
									echo ' - ' . $company["zip"];
								}
							}
						}
						else
						{
							$this->subview($addressView, $company);
						}
					?></td><?php
					if($tType == 'PI')
					{
						if($isLogo == 0)
						{
						?><td width="30%"><?php
						
							echo mylang("Issued");
							$this->subview($addressView, $company);
					
						?></td><?php
						}
					}
?>					<td class="vat" <?php echo empty($tNoBarcode) ? 'width="40%"' : 'width="45%"' ?>><?php include(APPPATH . "views/export/meta.php"); ?></td>
				
<?php		}
			else if($isLogo == 2)
			{
				?><td width="35%" colspan="<?php echo $colspan; ?>"><?php
				if($isLogo == 0)
				{
					echo mylang("Issued");
					$this->subview($addressView, $company);
				}
				?></td>
				</tr>
<?php
			}
				
				if (!empty($tType!='PI') && !empty($tType!='PC') && !empty($tType != 'PD') && !empty($company['countryCode'])  && $company['countryCode'] == 'IN')
				{
					$classCodes = array(
						'EXPWOP' => lang('EXPWOP'),
						'EXPWP' => lang('EXPWP'),
						'SEZWP' => lang('SEZWP'),
						'SEZWOP' => lang('SEZWOP')
					);
					$_tmp_classCode_content = '';
					if (!$isQuote && !empty($classCode) && !empty($classCodes[$classCode]))
					{
						$_tmp_classCode_content .= $classCodes[$classCode];
					}
					if(($tType == "SI"|| $tType == "SR") && (empty($classCode) || empty($classCodes[$classCode])) && $isComposition) {
						$_tmp_classCode_content .= "COMPOSITION TAXABLE PERSON, NOT ELIGIBLE TO COLLECT TAX ON SUPPLIES";
					}
					//For delivery challan with internal branch transfer
					if(($tType =="SH" || $tType =="TD" || $tType =="GR" || $tType =="TR") &&   !empty($contact) && !empty($contact['csType']) && ($contact['csType'] == 'B'|| $contact['csType'] == 'L')) {
						$_tmp_classCode_content .= "Stock Transfer / Branch Transfer - Not for Sale";
					}

			?><tr class="vam">
				<td <?php
					if (!$isQuote && $tType != DELCHALLAN && !empty($classCode) && !empty($classCodes[$classCode]))
					{
						echo 'class="bot ac vam bob" style="padding:5px;border-top:0.5px solid #000000;border-bottom:0.5px solid #000000;"';
					} else if($tType == "SI" && (empty($classCode) || empty($classCodes[$classCode])) && $isComposition) {

						echo 'class="bot ac vam bob" style="padding:5px;border-top:0.5px solid #000000;border-bottom:0.5px solid #000000;"';
					}
					else
					{
						echo 'class="bot ac vam" style="border-bottom:0.5px solid #000000"';
					}
				?> colspan="<?php echo !empty($company['logo']) ? 3 : 2; ?>"><?php
					echo $_tmp_classCode_content;

				?></td>
			</tr><?php
			}else{
				if(!empty($tType =='PO')){
					echo '<tr>';
					?><td colspan="<?php echo !empty($company['logo']) ? 3 : 2; ?>" class="bot ac vam" style="border-bottom:0.5px solid #000000">';
					<?php echo '</td></tr>';
				}
			}
		?></tbody>
		</table>
	</div><?php
	
	if(!empty($tType) && $tType == 'PO' && !empty($categoryCode) && $categoryCode == 'TL'){?>
		<table cellspacing="0" cellpadding="0" style="width:100%;border-collapse: collapse;border-top: none !important;" class="inv_details wpw  bot ft">
			<thead class="vam">
				<tr>
					<?php
					//if transaction conId as current bId then its received transfer Order
					if($conId  == $this->bId){
						if(!empty($billing)){?>
						<td style="width: 50%;text-align: left;"><?php echo "From Location : ".'<b>'.$billing['name'].'</b>';
							echo '<br>';
							$this->subview($addressView, $billing);	
							?>
						</td>
						<?php } ?>
						<?php if(!empty($company)){?>
						<td style="width: 50%;text-align: left;"><?php echo "To Location : ".'<b>'. $conName .'</b>'; 
							echo '<br>';
							//$this->subview($addressView, $company);	
						?> </td>
						<?php } 
					}?>
					<?php if($conId  != $this->bId){
						if(!empty($company['company'])){?>
						<td style="width: 50%;text-align: left;"><?php echo "From Location : ".'<b>'. $company['company'].'</b>'; 
								echo '<br>';
								$this->subview($addressView, $company);	
						?></td>
						<?php } ?>
						
						<?php if(!empty($conName)){?>
						<td style="width: 50%;text-align: left;"><?php echo "To Location :".'<b>'.$conName .'</b>';
							echo '<br>';
							$this->subview($addressView, $billing);	
						 ?> </td>
						<?php } 
					}?>
					
				</tr>
			</thead>
		</table>
	<?php }

if (!in_array($tType, array("PI", "PC", "PD")) && (empty($categoryCode) || ($categoryCode != 'TL' || $tType != 'PO' || $tType != 'PQ')))
{
	?><div class="trans"><?php

	if (!empty($billing['name']) || !empty($shipping['name']))
	{
		?><table border="0" cellspacing="0" cellpadding="0" class="noborder vat" style="padding-top:5px;padding-bottom:5px;<?php echo !empty($isAllPageHeader) ? 'border-bottom: 0.5px solid black;':''; ?>"><?php
/*
		$cols = 2;

		if (!empty($transQr) && !empty($qrTransImg))
		{
			$cols++;
		}

		if (!empty($isShipping) && $tType != 'PO')
		{
			
		}
*/
?>			<tbody>
				<tr>
					<td <?php echo !empty($showVerticalLine) ? 'style="padding:5px;border-left: 4px solid '.$headColor.'"' : '' ?>>
						<div>
<?php
						if ($tType == 'SH' && $isLogo == 2)
						{
							echo mylang("Issued");
							$this->subview($addressView, $billing);
						}
						else
						{
							//Name for billing address (if company name exists company name else if contact person name exists then contact person name else display name
							$contact_name = !empty($contact['company']) ? $contact['company'] : (!empty($contact['contactPerson']) ? $contact['contactPerson'] : (!empty($conName) ? $conName : (!empty($contact['name']) ?($contact['name']) : "")));
							$billing['name'] = $contact_name;
							if($contact_name || !empty($conName))
							{
								?><p class="fw nm"><?php
								echo ($tType == 'PI' || $tType == 'GR') ? mylang("Supplier") : mylang("Issued");
								?></p><?php
							}	
							if($tType == 'PO' || $tType == 'PQ') 
							{
								
								$this->subview($addressView, $billing);
							}
							if ($contact_name && $tType != 'PO' && $tType != 'PQ')   
							{
							?>
							<?php if(is_english($contact_name) && empty($fontname))
								 {?>
									 <b class="fw nm <?php echo !empty($showVerticalLine) ? 'tc' :'as'; ?>"><?php echo $contact_name; ?></b><?php
								} 
								else{
									echo "<span class='.!empty($showVerticalLine) ? 'tc' : 'as' .'>".$contact_name ."</span>";
								}
							}
							else if (!empty($conName) && $tType != 'PO' && $tType != 'PQ')
							{
							?>
							<?php if(is_english($contact_name) && empty($fontname)) 
								{?>
									<b class="fw nm <?php echo !empty($showVerticalLine) ? 'tc' :'as'; ?>"><?php echo $conName; ?></b><?php									}else{
										echo "<span class='.!empty($showVerticalLine) ? 'tc' : 'as' .'>".$conName ."</span>";
								}
						
							}
							if(!$expCurrencyCode)
							{
								if(!empty($conTaxNo) )
								{
									?><p class="nm"><?php echo mylang("GSTIN"); ?> : <?php echo $conTaxNo; ?></p><?php
								}
								else if(!empty($contact['taxNo']))
								{
									?><p class="nm"><?php echo mylang("GSTIN"); ?> : <?php echo $contact['taxNo']; ?></p><?php
								}
								if(!empty($contact['perTax']))
								{
									?><p class="nm"><?php echo mylang("PAN"); ?> : <?php echo $contact['perTax']; ?></p><?php
								}
								
								if(!empty($pos) && $tType!='PE' && $tType!='PI')
								{
									?><p class="nm"><?php echo mylang("POS");?> : <?php echo $pos; ?></p><?php
								}
								if($tType=='PI')
								{
									?><p class="nm"><?php echo mylang("SOS");?> : <?php echo $pos; ?></p><?php
								}
								if(!empty($contact['config']) && !empty($contact['config']['isRegisteredMSME']))
								{
									?><p class="nm"><?php echo mylang("MSME") .' '. $contact['config']['msmet'] ;?> : <?php echo $contact['config']['msmen']; ?></p><?php
								}
							}
							if($expCurrencyCode && $shipping && !empty($shipping['country']))
							{
								echo '<p class="nm">'.$billing['country'].'</p>';		
							}

							if (!empty($contact['contactPersonMobile']))
							{
								$billing['mobile'] = $contact['contactPersonMobile'];
								if(empty($isShipping)) {
									$shipping['mobile'] = $contact['contactPersonMobile'];
								}
							}
					
						}?></div>
					</td>
<?php
				if($tType == 'PO' && !empty($po_billaddr))
				{
					$company['name'] = $company['compName'];
					$company['gstin'] = $company['pan'] = $company['addInfo'] = '';
					?><td><?php
					
						echo mylang("BillableTo");

						$this->subview($addressView, $company);
				
					?></td><?php
				}
				if ($tType == 'SH' && $isLogo == 2)
				{
					if ($isShipping)
					{
						echo '<td class="nbr">Shipping Address : <br>';
						$this->subview($addressView, $shipping);
						echo '</td>';
					}

					$headColor = "transparent";
					?>
				<?php
				}
				if ($tType == 'SH' && $isLogo == 2)
				{
					$headColor = "transparent";
					?><td class="vat" <?php echo !empty($isShipping) ? 'width="33.3%"' : 'width="50%"' ?>><?php include(APPPATH . "views/export/meta.php"); ?>
					</td>
				<?php
				}
				else if(!empty($isShowAddress) && !empty($billing['countryCode']))
				{
					if ($tType != 'PO' && $tType != 'PQ')
					{
?>					<td  <?php echo !empty($isShipping) ? 'width="33%"' : 'width="50%"' ?>  <?php echo !empty($showVerticalLine) ? 'style="padding:5px;border-left: 4px solid ' . $headColor . '"' : '' ?>><div><?php
						$addr = ($isAllServices || $isShipping || $isQuote) ? mylang("BillingAddress") : mylang("Billing&Shipping Address");

						if ($tType == 'PO')
						{
							$addr = "Supplier";
						}
						if ($tType != 'PO')
						{
							echo '<span class="">' . $addr . '</span>';
						}
						if ($tType == 'SH')
						{
							$billing["name"] = $conName;
						}
						if ($tType != 'PO' )
						{
							    if ($tType == 'GR') {
								$billing = $company;
							    } else {
							// Name for billing address (if company name exists company name else if contact person name exists then contact person name else display name
							$contact_name = !empty($contact['company']) ? $contact['company'] : (!empty($contact['contactPerson']) ? $contact['contactPerson'] : (!empty($conName) ? $conName : (!empty($contact['name']) ? ($contact['name']) : "")));
							$billing["name"] = !empty($contact_name) ? $contact_name : $conName;
							}
							$this->subview($addressView, $billing);
						}
					?></div></td><?php
					}

					if ($isShipping && !$isAllServices)
					{
?>						<td <?php echo !empty($showVerticalLine) ? 'style="padding:5px;border-left: 4px solid '.$headColor.'"' : '' ?>><?php echo $tType != 'PO' && $tType != 'PQ' ? mylang("ShippingAddress") : 'Dispatch To'; ?>: <br>
						<?php 
							//Name for shipping address (if company name exists company name else if contact person name exists then contact person name else display name
							if ($tType != 'PO' && $tType != 'PQ')
							{
								$contact_name = !empty($contact['company']) ? $contact['company'] : (!empty($contact['contactPerson']) ? $contact['contactPerson'] : (!empty($conName) ? $conName :  (!empty($contact['name']) ? $contact['name'] : "")));
								//Edited shipping name directly in grid
								if($shipping["name"] != $contact_name)
								{
									$contact_name  = $shipping["name"];
								}
								$shipping["name"] = !empty($contact_name) ? $contact_name : $conName;
							}
							if($tType == 'PQ'){
								$shipping['name'] = $company['compName'];
								$shipping['gstin'] = $shipping['pan'] = $shipping['addInfo'] = '';
							}


							$this->subview($addressView, $shipping);
?>
						</td>
<?php
					}
				}
?>
			<!--<td class="vat"><?php
					//include(APPPATH . "views/export/meta.php");
				?></td>-->
<?php
				if (!empty($transQr) && !empty($qrTransImg))
				{
					echo '<td style="border:0px;width: auto;">';
					?><img src="<?php echo $qrTransImg; ?>" style="width:4cm;height:4cm;" class="" /><?php
					echo '</td>';
				}
?>				</tr>
			</tbody>
		</table><?php
	} ?>
	</div>
	<!-- /page_header -->
	<?php
}
else
{
	// echo '</page_header>';
}

if (!empty($isAllPageHeader))
{
	
	echo '</htmlpageheader><sethtmlpageheader name="'.$pdf_type.'" value="on" show-this-page="1" page="all" />';
}
?><div class="trans"><?php
		if (!empty($export_details[0]['value']) && !$isExportInvoice && ($isExport && $currencyCode != $expCurrencyCode))
		{
			$this->view($exportDetailsView);
		}
		if(!empty($fields))
		{ 
			$this->view($custFieldsView);
		}
?></div><?php
/*
	if ($isPrint)
	{
		// For Print Date : [[date_d]]/[[date_m]]/[[date_y]] [[date_h]]:[[date_i]]:[[date_s]]
		// <small class="ar">[[date_d]]/[[date_m]]/[[date_y]] [[date_h]]:[[date_i]]:[[date_s]]</small>
		//echo '<page_footer><p><small class="al">page [[page_cu]]/[[page_nb]]</small></p></page_footer>';
	}
*/
	$this->view($detailsView, array(
		"useSimpleTaxCol" => isset($useSimpleTaxCol) ? $useSimpleTaxCol : 1,
		'showVerticalLine' => 1,
		'isShowFor' => '1',
		'hideAmount' => ($tType == 'SH' && isset($isAmount) && !$isAmount) ? true : false
		// 'sysAttrAsCol' => true,
		// "hideAllAmount" => 1,
	));

	if(!empty($isack))
	{
		$totLessCharges = 0;
		if (!empty($lessCharges))
		{
			foreach($lessCharges as $key => $charge_item)
			{
				$totLessCharges += abs($charge_item['itemTotal']);
			}
		}
?>
<br>
<table align="left" cellspacing="0" cellpadding="0" class="noborder vat footTable" autosize="1" style="page-break-inside: avoid;border-top: 1px dotted #000;">	
	<tbody>
		<tr>
			<td><?php
				$company['email'] = '';
				$company['gstin'] = '';
				$company['name'] = lang("companyName");
				$this->subview($addressView, $company);
?>			</td>
			<td><?php
				$billing['email'] = '';
				$billing['gstin'] = '';
				$this->subview($addressView, $billing);
?>			</td>
			<td  class="al">Inv No : <?php
			echo $tNo . '&nbsp;&nbsp;' . $tDate;
			if (!empty($ref))
			{
				echo '<br> Ref : ' . $ref;
			}
?><br>
			<b style="font-size: 14px;">Total &nbsp;:&nbsp;</b>
<?php
			echo '<b style="font-size: 14px;">';
			echo formatAmount($totAmount + $totLessCharges, $isExport ? $expCurrencyCode : $currencyCode, "-", $isExport ? true : false,$currFraction);
			echo '</b><br>';
			echo mylang("ForcompanyName");
			?></td>
		</tr>
	</tbody>
</table><?php
	}
?>
<style>
body {
<?php
	if(!empty($isWatermark))
	{
		echo 'background: url("' . $waterImg . '") no-repeat 0 0;';
		if ($isWatermark && $isWatermark == 1)
		{
			echo 'background-position: top;';
		}
		if ($isWatermark && $isWatermark == 2)
		{
			echo 'background-position: center;';
		}
		if ($isWatermark && $isWatermark == 3)
		{
			echo 'background-position: bottom;';
		}
		if (!empty($isAllPageHeader))
		{
			echo 'header: html_'.$pdf_type.'';
		}
	}
?>
}
.invMeta {
	width: auto;
}
<?php
	if(!empty($isPrint))
	{
?>
.invMeta td {
	padding:0px !important;
	/*white-space: nowrap;*/
}
<?php
	}
?>
</style>
