<!DOCTYPE html>
<html>
<head>
    <title>Invoice</title>
	<style type="text/css">
       .table td {
        border: 1px solid #1f67b2; 
    }
	.whole {
	margin:10px;
	padding:5px;
	.table {
	border-collapse: collapse !important;
	font-size: 12px;
	color: #101010;
	width: 100%;
}
.table thead th{
	background: #e4e3e3;
	color: #000;
}
.table th,
.table td {
	border: 1px solid #000 !important;
	text-align: left;
	padding: 3px 6px;
}
.table th.text-left,
.table td.text-left{
	text-align: left;
	text-align: left !important;
}
.table th.text-right,
.table td.text-right{
	text-align: right;
	text-align: right !important;
}
.table th.text-center,
.table td.text-center{
	text-align: center;
	text-align: center !important;
}
.table tbody tr:nth-child(even){
	background: #fff;
}
.table th.Table-Title,
.table td.Table-Title{
    padding:7px 6px !important;
    font-size:1.15rem;
    color:green;
    font-weight:500;
    text-transform:uppercase;
}
.table th.total,
.table td.total{
	background: #e4e3e3;
	color: #fff;
	font-size: 11px;
}
	}
</style>
</head>
<body>
    <div class="">
        <?php  $invoice = OutboundInvoice::model()->with('items')->findByPk($id); ?>
        <?php  $outbound = Outbound::model()->findByPk($invoice->outbound_id); ?>
		<div class="col-12">
					<div class="text-right">
				     <?php if($logo_display>0){?>
                 <img src="<?php echo 'images/pdf/'.$logo; ?>" style="width:32%;">
                 <?php }else{ ?>
                 <?php } ?>
                 <?php if($logo_display>0){?>
                 <br><br><br>
                 <?php } ?>
                </div>
				 <div class="text-left" style="padding-top:-135px;line-height:9px;font-size:9px;font-weight:600;">
                 <?php if($logo_display==0){?>
                 <br><br><br><br><br><br><br><br><br><br>
                 <?php } ?>
                 <?php if($logo_display> 0 ){?>
                        <?php echo $formatted_address; ?>
                <?php } ?>
                </div>
                 <hr style="width:100%;">
			<div class="col-12">
				<div class="text-left" style="font-size:12px;font-weight:bold;padding-top:0px;">
				<p><?php echo $invoice->customer->name; ?></p>
				</div>
                <div class="text-left" style="font-size:9px;font-weight:bold;padding-top:-0px;">
                    <?php echo nl2br($invoice->customer->billing_adds); ?><br>
                </div>
				<div class="text-right" style="font-size:9px;padding-top:-50px !important;">
				<p><b>INVOICE : <?php echo $invoice->invoice_no; ?></b><br>
				   <b>DATE : <?php echo date('d M Y', strtotime($invoice->invoice_date)); ?></b><br>
                   <b>REF# : <?php echo $invoice->id; ?></b></p>
				</div>
					<!-- <div class="text-left" style="font-size:9px;font-weight:bold;">
					 <p>BILL OF ENTRY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<?php //echo $outbound->bill_of_entries(); ?><br>
						MEASUREMENT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;<?php //echo $outbound->totalVolume; ?> CBM<br>
						TRN NO&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;--<br>
						PACKING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;EXPORT STANDARD PACKING</p>
					</div> -->
					<div class="h3" style="text-align:center;padding-bottom:5px;padding-top:-10px;">
						<u><?php echo "INVOICE"; ?></u>
					</div>
			</div>
		</div>
	</div>
	<div class="col-md-12">
        <table id="" class="table">
            <thead>
              
                <tr>
				    <th class="text-center" style="width:8%;">SL.<br>NO</th>
                    <th class="text-center" style="width:25%">CATEGORY</th>
                    <th class="text-center" style="width:16%;">MODEL</th>
                    <th class="text-center" style="width:13%;">ORIGIN</th>
                    <th class="text-center" style="width:13%;">QTY.<br>SETS.</th>
					<th class="text-center" style="width:17%;">U/PRICE<br><?php echo $invoice->currency(); ?></th>
					<th class="text-center" style="width:17%;">Amount<br><?php echo $invoice->currency(); ?></th>
                </tr>
            </thead>
            <tbody>
				  <?php 
                  $grand_total_qty= 0;
                  $grand_total_amount = 0;
 
                  $x =1;
                  $query = 'SELECT B.id, B.brand_name FROM nia_outbound_invoice_items OI LEFT JOIN nia_mas_brand B ON B.id = OI.brand_id WHERE OI.invoice_id='.$id.' AND B.brand_name IS NOT NULL GROUP BY OI.brand_id ORDER BY B.brand_name';
                  $outbound_brands = Yii::app()->db->createCommand($query)->queryAll();
                  foreach($outbound_brands as $i => $each){ ?>
                    <tr><td colspan="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><?php echo $each['brand_name'] ?></b></td></tr>
                    <?php 
                        $query_of_category = 'SELECT CA.category_name, CA.id AS category, GROUP_CONCAT(OI.id) AS item_ids FROM nia_outbound_invoice_items OI LEFT JOIN nia_mas_category CA ON OI.category_id = CA.id WHERE OI.invoice_id='.$id.' AND CA.category_name IS NOT NULL AND  OI.brand_id='.(int) $each['id'].' GROUP BY CA.id ORDER BY CA.category_name';
                        $outbound_categories = Yii::app()->db->createCommand($query_of_category)->queryAll();
                        foreach( $outbound_categories as $eachCat ) : 
                            $query_products = 'SELECT * FROM nia_outbound_invoice_items OI LEFT JOIN nia_outbound_invoice O ON O.id = OI.invoice_id WHERE OI.id IN ('.$eachCat['item_ids'].') ORDER BY OI.outb_part_number';
                            $query_products_count= 'SELECT COUNT(id) AS value FROM nia_outbound_invoice_items WHERE id IN ('.$eachCat['item_ids'].')';
                            $items = Yii::app()->db->createCommand($query_products)->queryAll();
                            $count = Yii::app()->db->createCommand($query_products_count)->queryScalar();
                            
                            $total_qty= 0;
                            $total_amount = 0;
                            
                            foreach( $items as $c => $eachItem ) : 

                                $total_qty +=$eachItem['outb_quantity'];
                                $total_amount +=$eachItem['outb_invoice_rate'];

                                $grand_total_qty +=$eachItem['outb_quantity'];
                                $grand_total_amount +=$eachItem['outb_invoice_rate'];
                    ?>
                        <tr>
                            <td style="text-align:center;font-size:11px;"><?php echo $x; ?></td>
                            <?php if( $c == 0 ) : ?>
                                <td style="text-align:left;font-size:11px;" rowspan="<?php echo $count ?>"><?php echo $eachCat['category_name'];?></td>
                            <?php endif; ?>
                            <td style="text-align:center;font-size:11px;"><?php echo $eachItem['outb_part_number']; ?></td>
                            <td style="text-align:center;font-size:11px;"><?php echo $eachItem['outb_contry_of_origin']; ?></td>
                            <td style="text-align:center;font-size:11px;"><?php echo (float) $eachItem['outb_quantity']; ?></td>
                            <td style="text-align:right;font-size:11px;"><?php echo Yii::app()->numberFormatter->formatCurrency($eachItem['outb_unit_price'],"");  ?></td>
                            <td style="text-align:right;font-size:11px;"><?php echo Yii::app()->numberFormatter->formatCurrency($eachItem['outb_invoice_rate'],""); ?></td>
                        </tr>
                    <?php   $x++;
                            endforeach; ?>
                        <tr bgcolor="#e3e4e4"><td colspan="4" class="text-right" style="background-color:#e4e3e3;"><b>Subtotal&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td style="text-align:center;font-size:11px;background-color:#e4e3e3;"><b><?php echo (float) $total_qty?></b></td><td style="background-color:#e4e3e3;"></td><td style="text-align:right;background-color:#e4e3e3;"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total_amount,"") ?></b></td></tr>
                <?php endforeach; ?>
                <?php 
                } ?>
				<tr>
				   <td height="20" colspan="4" valign="top" style="text-align:right;background-color:#e4e3e3;"><b>TOTAL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>FRIEGHT</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                   <td height="20" style="text-align:center;background-color:#e4e3e3;"><b><?php echo $grand_total_qty;?><br><?php echo $invoice->unit_label  ?></b></td>
                   <td height="20" colspan="3" style="background-color:#e4e3e3;text-align:right;"><div style="float:left;"><b><?php echo Yii::app()->numberFormatter->formatCurrency($grand_total_amount,"");?></b></div><div style="text-align:left;"><b><?php echo Yii::app()->numberFormatter->formatCurrency($invoice->freight_charge,"");?></b></div></td>
				</tr>
				<tr>
				   <td height="20" colspan="6" valign="top" style="background-color:#e4e3e3;"><b><?php echo $invoice->destination ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></td><td class="text-right" style="background-color:#e4e3e3;"><b><?php echo $invoice->currency(); ?>&nbsp;<?php echo Yii::app()->numberFormatter->formatCurrency(($grand_total_amount+$invoice->freight_charge),"");?></b></td>
				</tr>
            </tbody>
        </table>
		</div>
	  <div class="col-md-12">
	  <p style="font-size:9px;"> <strong><?php echo $invoice->currency(); ?> <?php  echo  Yii::app()->controller->generateWords(($grand_total_amount+$invoice->freight_charge))  ?><br>
	  <?php echo nl2br($invoice->invoice_note); ?><br><br>
	  Beneficiary bank details  : </strong></p>
	  </div>
	  <div style="font-size:9px;text-align:left;padding-top:-23px;padding-left:175px;">
		<?php echo nl2br($invoice->bank_details); ?>
	  </div>
	  <p style="font-size:9px;padding-top:-9px;"><strong>FOR <?php echo $company_name; ?></strong><br>_____________________</p>
  


    <htmlpagefooter name="myfooter" class="newfoot">

<!--<div style="text-align:center;width:100%;font-size:8px;">Genarated from Bluesky Logistics
<?php
      //date_default_timezone_set("Asia/Dubai");
     //echo date('d/m/Y')." ".date("h:i:s A"); ?>
</div>-->


<!--<div style="border-top: 1px dotted #000000; font-size: 12px; text-align: center; padding-top: 10px; background-color: #e4e3e3;">
 <?php if($logo_display>0){?>
<div style="text-align:center;width:100%">
    <b><?php echo $company_name; ?></b><br>
    <?php echo $company_address; ?>
</div>
<?php } ?>
<div style="text-align:center;width:100%">Page {PAGENO} of {nb} </div>
</div>-->
</htmlpagefooter>


    <style type="text/css">
   div { 
    page-break-inside:avoid;

      }


</style>

</body>

</html>
