<?php defined('BASEPATH') OR exit('No direct script access allowed');
$tdate = new DateTime($tDate);
//echo $tdate->format('d/m/Y');
$ddate = new DateTime($dueDate);
$shipping = $billing = array();
if (count($address)>1) {
	$billing = $address[0];
	$shipping = $address[1];
} else if ($address) {
	$shipping = $billing = $address[0];
}

$tNo_text 		= 'Invoice Number';
$tDate_text 	= 'Invoice Date';
$dueDate_text 	= 'Due Date';
$dueAmount_text = 'Balance Due';
$type_text = array(
	CRNOTE => 'Credit Note',
	//SALPMT => 'Receipt'
);
switch ($tType) {
	case SALRETAIL:
		$tNo_text 	= 'Retainer';
		$tDate_text = 'Retainer Date'; 
	break;
	case CRNOTE:
		$tNo_text 	= 'Credit Note';
		$dueDate_text 	= 'Credit Date';
		$dueAmount_text = 'Credits Remaining';
	break;
	case DELCHALLAN:
		$tNo_text 	= 'Delivery Challan';
		$dueDate_text 	= 'Challan Date';
	break;
	case EST:
		$tNo_text 	= 'Estimate Number';
		$tDate_text = 'Estimate Date'; 
	break;
	//case SALPMT:
		//$rec = 'receipt';
	//break;
}


 ?>
<div class="trans">
	<table border="0" cellspacing="0" cellpadding="0" style="width:100%">
		<tr>
			<td style="width:33%;text-align:left;padding-left:10px;"> 
			</td>
			<td style="width:34%;">
				<div align="center" class="name"><b><?php echo get_company('name');?></b></div>
				<div align="center"><?php echo get_company('address1');?></div>
					<?php if (get_company('address2')) {
						echo '<div align="center">'.get_company('address2').'</div>';
					} ?> 
				<div align="center"><?php echo get_company('city');?></div>
				<div align="center"><?php echo get_company('zip');?></div>
				<div align="center"><?php echo get_company('phone');?></div>
			</td>
			<td style="width:33%;">
			</td>
		</tr>
	</table><br>
</div>
<div class="rece">
	<h3><?php echo lang($tType.'_inv'); ?>&nbsp;Details</h3>
</div>
<div class="trans">
		<table border="0" cellspacing="0" cellpadding="0" style="width:100%">
			<tr>
				<td style="width:33%;vertical-align:top">
					<table style="width:33%">
						<tr>
							<td class="label"><?php echo $tNo_text;?></td>
							<td class="label"><?php echo '#'.$tNo;?></td>
						</tr>
					</table>
				</td>
				<td style="width:33%;">
					<table style="width:80%" align="center">
						<tr>
							<td class="label"><?php echo $tDate_text;?></td>
							<td><?php echo $tdate->format('d/m/Y');?></td>
						</tr>
					</table>		
				</td>
				<td style="width:33%;" valign="top">	
					<table border="0" cellspacing="0" cellpadding="0" style="width:100%;" align="right">
		
								<?php if ($dueDate) { ?>
							<tr>
								<td class="label"><?php echo $dueDate_text;?></td>
								<td>&nbsp;<?php echo $ddate->format('d/m/Y');?></td>
							</tr>
						<?php } ?>
					</table>				
				</td>
			</tr>
		</table>
		<div style="border-bottom:1px solid #eeeeee;"></div>
			
		<table border="0" cellspacing="0" cellpadding="0" style="width:100%">
			<tr>
				<td style="width:50%;" >
					<h4><?php echo lang('Billing Address'); ?> </h4>
					<?php if ($billing) { ?>
						<div><?php echo $billing['address1'];?></div>
						<?php if ($billing['address2']) {
							echo '<div>'.$billing['address2'].'</div>';
						} ?>
						<div><?php echo $billing['city'];?></div>
						<div><?php echo $billing['zip'];?></div>
						<div><?php echo $billing['phone'];?></div>
					<?php } ?>
				</td>
				<td style="width:50%;">
					<h4 align="right"><?php echo lang('Shipping Address');?> </h4>
					<?php if ($shipping) { ?>
						<div align="right"><?php echo $shipping['address1'];?></div>
						<?php if ($billing['address2']) {
							echo '<div>'.$shipping['address2'].'</div>';
						} ?>
						<div align="right" style="text-align:left;"><?php echo $shipping['city'];?></div>
						<div align="right" style="text-align:left;"><?php echo $shipping['zip'];?></div>
						<div align="right" style="text-align:left;"><?php echo $shipping['phone'];?></div>
					<?php } ?>
				</td>			
			</tr>
		</table>
			<hr style="border: 1px dashed black;" />
		<table border="1" cellspacing="0" cellpadding="0" style="width:100%">
			<tr>
				
					<!-- <div class="middlecol"> -->
						<table border="0" cellspacing="0" cellpadding="0" style="width:100%;"  class="middlecol">	
								
							<thead>
								<tr>
								<th style="width:50%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("ItemDescription");?></th>
								<th style="width:10%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("Tax");?></th>
								<th style="width:10%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("Qty");?></th>
								<th style="width:10%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("Rate");?></th>								
								<th style="width:10%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("Discount");?></th>
								<th style="width:10%;text-align:left;border-bottom: thin 1px dashed black;"><?php echo lang("Amount");?></th>
								</tr>	
								
							</thead>
								
							<tbody>
								<?php if (isset($details)) {
									$trans_items = array();
									$charges = array();
									foreach ($details as $i => $row) {
										$trans_items[$row['id']] = $row;
										if (empty($row["qty"])) {
											$charges[] = $row;
											continue;
										}
										echo '<tr >'
										.'<td style="text-align:center;">'.$row["itemName"].'</td>'
										.'<td style="text-align:left;">'.$row["taxName"].'</td>'
										.'<td style="text-align:left;">'.$row["qty"]."  <sub>".ucfirst(strtolower($row["unit"])).'</sub></td>'
										.'<td style="text-align:right;">'.$row["rate"].'</td>'
										.'<td style="text-align:right;">'.$row["discAmount"].'</td>'
										.'<td style="text-align:right;">'.$row["taxableAmount"].'</td>'
										.'</tr>';
									}
									if ($charges) {
										foreach ($charges as $i => $row) {
											echo '<tr><td colspan="2" style="border-bottom:none !important;"></td>'
											.'<td colspan="2" style="text-align:left;">'.$row["itemName"].'</td>'
											.'<td>'.$row["taxName"].'</td>'
											.'<td style="text-align:right">'.$row["taxableAmount"].'</td></tr>';
										}
									}
								} ?>
								
								<tr>
									<td colspan="2" style="border-bottom:none !important;"></td>
									<td class="label" colspan="3" style="text-align:right;"><?php echo lang("Sub Total"); ?></td>
									<td  class="label" style="text-align:right;"><?php echo $taxableAmount; ?></td>
								</tr>
								
								<?php if (isset($tax)) {
									$tot_tax = array();
									
									foreach ($tax as $i => $row) {
										$taxRate =  str_replace('.','',abs($row['taxRate']));//abs($taxRate);
										$tax_key = $row['taxTypeCode'].$taxRate;
										
										$tax_value = !empty($tot_tax[$tax_key])?($tot_tax[$tax_key]['taxValue']+$row['taxValue']):$row['taxValue'];
										// $tot_tax[$tax_key] = $row;
										$tot_tax[$tax_key]['taxTypeCode'] 	= $row['taxTypeCode'];
										$tot_tax[$tax_key]['taxRate'] 		= $row['taxRate'];
										$tot_tax[$tax_key]['taxValue'] 		= $tax_value;
										
										if (empty($tot_tax[$tax_key]['taxableAmount'])) {
											$tot_tax[$tax_key]['taxableAmount'] = $trans_items[$row['transItemID']]['taxableAmount'];
										} else {
											$tot_tax[$tax_key]['taxableAmount'] += $trans_items[$row['transItemID']]['taxableAmount'];
										}
										
									}
									$tax_name = $tax_value = '';
									foreach ($tot_tax as $i => $row) {
										 $tax_name .= $row['taxTypeCode'].' @ '.$row['taxRate'].'% on '.$row['taxableAmount']."<br><br>";
										 $tax_value .= $row['taxValue']."<br><br>"; 
									}

									echo '<tr><td colspan="2" valign="bottom" style="border-bottom:none !important;">';
									if ($notes) { ?>
										<?php echo lang("Notes") ?> <br><span style="color: #666666;margin-left: 20px;margin-top: 5px;"><?php echo $notes;?></span>
									<?php $notes = '';}
									echo '</td><td colspan="3" style="text-align:right;">'.trim($tax_name,'<br><br>').'</td><td style="text-align:right">'.trim($tax_value,'<br><br>').'</td></tr>';
									
								} ?>
								<tr>
									<td colspan="2" style="border-bottom:none !important;border-top: thin solid;border-color: #D3D3D3;"></td>
									<td class="label" colspan="3" style="text-align:right;border-top: thin solid;border-color: #D3D3D3;">Total </td>
									<td class="label" style="text-align:right;border-top: thin solid;border-color: #D3D3D3;"><?php echo $totAmount;?></td>
								</tr>
								<?php if (!empty($trans_payment)) {
									$payment_name = $payment_value = '';
									foreach ($trans_payment as $payment) { 
										$payment_name .= $type_text[$payment['tType']].' #'.$payment['tNo']."<br><br>";
										$payment_value .= $payment['allotAmt']."<br><br>"; 
									}
									echo '<tr><td colspan="2" valign="bottom" style="border-bottom:none !important;">';
									echo '</td><td colspan="3" style="text-align:right;">'.trim($payment_name,'<br><br>').'</td><td style="text-align:right">'.trim($payment_value,'<br><br>').'</td></tr>';
								} ?>
								<tr>
									<td colspan="2" style="border-bottom:none !important;"></td>
									<td class="label" colspan="3" style="text-align:right;"><?php echo $dueAmount_text; ?> </td>
									<td class="label" style="text-align:right"><?php echo $dueAmount;?></td>
								</tr>				
							</tbody>
						</table>
					<!-- </div> -->
				
			</tr>
			<hr style="border: 1px dashed black;" />
		</table>
	</div>
	<?php if ($notes) {?>
	<div class="notes">
			<div style="width:100%;margin-top:100px;text-align:center"><?php echo lang("Notes") ?><?php echo $notes;?></div>
	</div>
	<?php } ?>
	<div style="width:100%;margin-top:280px;text-align:right">
	<?php echo lang("Authorized Signature"); ?>
	</div>
	
	
<style>

.trans
{
 color:#666666;
 font-size:12px;
 width: 100%;
 min-height: 30%;
 margin: 0px;
}
table td {
	color:#000000;
}

table td.label {
	color:#000000;
	font-weight:bold;
}
table td h4, table td h5 {
	color:#000000;
	margin-top:0px;
	margin-bottom:5px;
}
.customer {
 width:40%; 
 height: 15px;
 background:#208ED3;
 color:#FFF;
 font-weight: bold;
 padding: 7px 17px 7px 7px;
 text-align: right;
 margin-left: -30px;
}

table.middlecol tr th, table.middlecol tr td {
	
}
.middlecol{
 color:#666666;
 width: 100%;
 height: 60%;
}
.middlecol th{
 text-align:center;
 font-weight:bold;
 vertical-align: middle;
 padding: 5px;
}
.middlecol tr.odd{
 background:#EAEAEA;
}
.middlecol tr.even{
 background:#FFFFFF;
}
.middlecol td{
 padding-right:10px;
 padding: 8px;
}
.tax_disable{
 display:none;
}
.notes{
	font-size: 11px;
	color: #000000;
    width: 100%;
}	
.rece{
text-align:center;
}
.rece{
	text-align:center;
}
.trans
{
	font-size:15px;
	width: 100%;
	min-height: 30%;
	margin: 0px;
}
/*.tab  {
	border-width: thin;
	border-spacing: 2px;
	border-style: none;
	border-color: black;
}*/
/*.tab tr td {
	margin-top:0px;
}*/
.name {
	font-size:20px;
}
.noBorder {
	border: 0;
}
</style>

