<?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;
}
/*$details[] =  array
        (
			'id' => '58',
            'itemName' => 'Freight',
			'taxName' => '',
			'discAmount' => 0, 
            'rate' => 99,
            'qty' => 0,
            'unit' => 'NOS',
            'taxableAmount' => 99.00,
            'taxId' => 2, 
            'itemTotal' => 110.88,
            'accId' => 28
        );
$details[] =  array
        (
			'id' => '58',
            'itemName' => 'Insurance',
			'taxName' => '',
			'discAmount' => 0, 
            'rate' => 99,
            'qty' => 0,
            'unit' => 'NOS',
            'taxableAmount' => 99.00,
            'taxId' => 2, 
            'itemTotal' => 110.88,
            'accId' => 28
        );*/
?>
																		
<page footer="page">
	<div class="trans">
		<table border="0" cellspacing="0" cellpadding="0" style="width:100%">
			<tr>
				<td style="text-align:left;padding-left:10px;"> 
					<div class="customer"><?php echo lang($tType.'_inv'); ?> </div>
					<h2><?php echo $pdf_type;?></h2>
				</td>
				<td style="width:25%;vertical-align:top">
					<table style="width:80%">
						<tr>
							<td class="label"><?php echo $tNo_text;?></td>
							<td class="label"><?php echo '#'.$tNo;?></td>
						</tr>
						<tr>
							<td class="label"><?php echo $tDate_text;?></td>
							<td><?php echo $tdate->format('d/m/Y');?></td>
						</tr>
						<?php if ($dueDate) { ?>
							<tr>
								<td class="label"><?php echo $dueDate_text;?></td>
								<td><?php echo $ddate->format('d/m/Y');?></td>
							</tr>
						<?php } ?>
						<?php if ($ref) { ?>
							<tr>
								<td class="label"><?php echo 'Reference No';?></td>
								<td><?php echo $ref;?></td>
							</tr>
						<?php } ?>
					</table>
				</td>
				<td style="width:33%;">
					<h4><?php echo get_company('name');?> </h4>
					<div><?php echo get_company('address1');?></div>
					<?php if (get_company('address2')) {
						echo '<div>'.get_company('address2').'</div>';
					} ?> 
					<div><?php echo get_company('city');?></div>
					<div><?php echo get_company('zip');?></div>
					<div><?php echo get_company('phone');?></div>
					<?php $com_config = get_company('config'); ?> 
					<table border="0" cellspacing="0" cellpadding="0" style="width:100%;">
						<tbody>
							<tr>
								<td style="width:25%">GSTIN</td>
								<td><?php echo !empty($com_config['gstin'])?$com_config['gstin']:'-'; ?></td>
							</tr>
							<tr>
								<td>PAN</td>
								<td><?php echo !empty($com_config['pan'])?$com_config['pan']:'-'; ?></td>
							</tr>
						</tbody>
					</table>					
				</td>
			</tr>
			<tr><td style="padding:20px 0pc 20px 0px;" colspan="3">
				<div style="border-bottom:1px solid #eeeeee;"></div>
			</td></tr>
			<tr>
				<td style="width:33%;" valign="top">					
					<h4><?php echo !empty($contact['name']) ?$contact['name']:'' ;?></h4>
					<table border="0" cellspacing="0" cellpadding="0" style="width:100%;">
						<tbody>
							<tr>
								<td style="width:25%">GSTIN</td>
								<td><?php echo !empty($contact['taxNo'])?$contact['taxNo']:'-'; ?></td>
							</tr>
							<tr>
								<td>PAN</td>
								<td><?php echo !empty($contact['pan'])?$contact['pan']:'-'; ?></td>
							</tr>
							<tr>
								<td>POS</td>
								<td><?php echo !empty($pos)?$pos:'-'; ?></td>
							</tr>
						
						</tbody>
					</table>
				</td>
				<td style="width:33%;">
					<h4><?php echo '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:33%;">
					<h4><?php echo 'Shipping Address';?> </h4>
					<?php if ($shipping) { ?>
						<div><?php echo $shipping['address1'];?></div>
						<?php if ($billing['address2']) {
							echo '<div>'.$shipping['address2'].'</div>';
						} ?>
						<div><?php echo $shipping['city'];?></div>
						<div><?php echo $shipping['zip'];?></div>
						<div><?php echo $shipping['phone'];?></div>
					<?php } ?>
				</td>			
			</tr>
			<tr>
				<td colspan="3" style="padding-top:20px;" >
					<!-- <div class="middlecol"> -->
						<table border="0" cellspacing="0" cellpadding="0" style="width:100%;border-color:#EEEEEE;" class="middlecol">
							<thead>
								<tr>
								<th style="width:45%;border-left:1px solid #999999;">Item Description</th>
								<th style="width:10%">Tax</th>
								<th style="width:10%">Qty</th>
								<th style="width:10%">Rate</th>								
								<th style="width:10%">Discount</th>
								<th style="width:10%">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="border-left:1px solid #999999;border-right:none !important;">'.$row["itemName"].'</td>'
										.'<td style="border-right:none !important;">'.$row["taxName"].'</td>'
										.'<td style="text-align:center;border-right:none !important;">'.$row["qty"]."  <sub>".ucfirst(strtolower($row["unit"])).'</sub></td>'
										.'<td style="text-align:right;border-right:none !important;">'.$row["rate"].'</td>'
										.'<td style="text-align:right;border-right:none !important;">'.$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;border-right:none !important;">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) { ?>
										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;border-right:none !important;">'.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;"></td>
									<td class="label" colspan="3" style="text-align:right;border-right:none !important;">Total </td>
									<td class="label" style="text-align:right"><?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;border-right:none !important;"><?php echo $dueAmount_text; ?> </td>
									<td class="label" style="text-align:right"><?php echo $dueAmount;?></td>
								</tr>				
							</tbody>
						</table>
					<!-- </div> -->
				</td>
			</tr>
		</table>
	</div>
	<?php if ($notes) {?>
	<div class="notes">
			<div>Notes</div>
			<div style="color: #666666;padding-left: 10px;padding-top: 5px;width: 100%;"><?php echo $notes;?></div>
	</div>
	<?php } ?>
	<div style="width:100%;margin-top:100px;text-align:right">
		Authorized Signature
	</div>
	
		
	
	<!-- <page_footer>
		<table align="center" border="0" cellspacing="0" cellpadding="0" style="width:100%;">
						<tr>
							<td style="text-align:right;width:50%;">
							</td>
							<td style="text-align:right;width:50%;">
								Authorized Signature
							</td>
						</tr>
		</table>
	</page_footer> -->
	<!--<div>
    <?php if($type === 'SALPMT') { ?>
    <img src='passIcon.jpg'>Pass
    <?php } else { ?>
    <img src='failIcon.jpg'>Fail
    <?php } ?>
	</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 {
	border-right:1px solid #999999;
	border-bottom:1px solid #999999;
}
.middlecol{
 color:#666666;
 width: 100%;
 height: 60%;
}
.middlecol th{
 background:#208ED3;
 text-align:center;
 color:#FFF;
 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%;
}	
</style>
</page>
