<!DOCTYPE html>
<html>


<?php 
        $mpdf->use_kwt = true;
        $crm_login_company_id       = Yii::app()->user->master_user_company_id;
        $privillage_modal           = new MasCompanySettings();
        $privillage_condition       = 'crm_company_id='.$crm_login_company_id;
        $privillage_settings_module = $privillage_modal->find(array('condition'=>$privillage_condition));        
        $bonus_quantity             = $privillage_settings_module->bonus_quantity;

        $modelInvoice = Invoice::model()->findByPk($id);
    ?>
    <head>
        <title>TAX INVOICE</title>
        <style>
            .table_box {
                border-collapse:separate!important;
                margin-top: 10px;                      
                padding: 10px;
                font-size:16px!important;
                border: 1px solid #0066ff;
                border-radius:6px;   
            }
            
        </style></style>
    </head>
<body>
    <div class="">
    <div class="header" style="width:100%;float: left;padding-bottom:0;">
        <img src="<?php echo  'images/logo/'.Yii::app()->user->pdf_company_logo; ?>"/>
    </div>
        <div class="col-4">
            <?php
                        
                         $invoice_type = $modelInvoice->invoice_type;
                         $invoice_no = $modelInvoice->invoice_no;
                          $invoice_type_name="";
		                    if((int) $invoice_type > 0){
		                        $modelInvoiceType = new MasInvoiceType();
		                        $arr_invoice_type_details = $modelInvoiceType->findByPk((int) $invoice_type);
		                        $inv_code      = $arr_invoice_type_details->inv_code;
		                        
		                        $invoice_no=$inv_code.'-'.$invoice_no;
		                        $invoice_type_name = $arr_invoice_type_details->inv_name;
		                    }
?>
                <div class="input-block">
                    <b><?php echo $modelInvoice->id;?></b>
                </div>
               <b><?php echo strtoupper($modelInvoice->customer->customer_name); ?></b><br>
                 <?php if($modelInvoice->billing_address != '') {     ?>       
                <small><?php echo strtoupper(nl2br($modelInvoice->billing_address)); ?></small><br>
                    <?php } ?>      
                <?php if($modelInvoice->customer->customer_phone_no !='') { ?>        
                <small>Phone : <?php echo strtoupper($modelInvoice->customer->customer_phone_no); ?></small><br>
                <?php } ?>
                <?php if($modelInvoice->contact->person_name != '') { ?>
                <br><small>Kind Attn:</small>
                <small><b><?php echo  strtoupper($modelInvoice->contact->person_name); ?></b></small><br>
                <?php } ?>
                <br><br><br>
                <?php $settings = MasCompany::model()->findByPk(Yii::app()->user->master_user_company_id);  ?>
                <?php if($settings->trn_no != ''){  ?>
                <div class="input-block" style="background-color:white">
                    Our TRN# <b><?php echo $settings->trn_no;?></b>
                </div>
                <?php } ?>
        </div>
        <div class="col-4">
        <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;color: #0000b3;font-size: 18px;">
        <?php echo "TAX INVOICE"; ?>
            <br><br>
        </div>   
             <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
            <?php
             if($barcode == 'Y'){
            ?>
            <barcode code="<?php echo $modelInvoice->id;?>" type="EAN13" height="0.66"  size="1.2"/>
            <?php
             }
            ?>
             </div>
        </div>
        <div class="col-4" style="color:#101010;">
            <table class="table" style="width:100%">
                <!-- <tr>
                    <?php
            $modelInvoice = Invoice::model()->findByPk($id);
            $model_lead=Lead::model()->find(array('condition'=>'file_id="'.$modelInvoice->file_id.'"'));
            ?>
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo $modelInvoice->file_id;?>
                        </td>
                <tr>
                    <td>Customer ID</td>
                    <td>
                        <?php echo $modelInvoice->customer_id;?>
                    </td>
                </tr> -->
                <tr>
                    <td>Type</td>
                    <td>
                        <?php echo 'Sales';?>
                    </td>
                </tr>     
                 <tr>
                    <td>Customer TRN #</td>
                    <td>
                        <?php echo $modelInvoice->customer->customer_trn_no;?>
                    </td>
                </tr>
                <?php 
                 $delivery = new DeliveryNote();
                if($modelInvoice->delivery_ids!=""){
                $delivery = DeliveryNote::model()->findAll(array('condition' => 'id IN ('.$modelInvoice->delivery_ids.')'));
                }
                $dates = array_map(function($obj) {return date('d/m/Y',STRTOTIME($obj->delivery_date)); },$delivery);
              
                ?>
               <tr>
                    <td>Delivery Date</td>
                    <td>
                        <?php echo implode(PHP_EOL,$dates);?>
                    </td>
                </tr>
                  <tr>
                    <td>Delivery Note #</td>
                   
                    <td>
                        <?php echo implode(PHP_EOL,explode(',', $modelInvoice->delivery_ids));?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelInvoice->invoice_date));?>
                    </td>
                </tr>
                <tr>
                    <td>Currency</td>
                    <td>
                        <?php 
                        $modelCurrency = new MasCurrency();
                        $modelCurrency = $modelCurrency->getCurrencyDetails($modelInvoice->currency);
                        echo $modelCurrency->currency_code;
                        if($modelCurrency->currency_code!="AED"){
                            echo "/".$modelInvoice->exchange_rate;
                        }
                        ?>
                    </td>
                </tr> 
                  <tr>
                    <td>LPO</td>
                    <td>
                        <?php echo $modelInvoice->order_details->lpo;?>
                    </td>
                </tr>
                <?php
                if(isset($modelInvoice->leadowner->first_name) && $modelInvoice->leadowner->first_name!="") {
                    ?>
                <tr>
                    <td colspan="2">
                        <?php echo $modelInvoice->leadowner->first_name.' '.$modelInvoice->leadowner->last_name;
                        if(isset($modelInvoice->leadowner->phno) && $modelInvoice->leadowner->phno!=""){
                            echo ' , '.$modelInvoice->leadowner->phno;
                        }?>
                    </td>
                </tr>
                <?php 
                }
                ?>

               
            </table>
        </div>
    </div>
    <br>
    <div class="col-md-12">
        <table id="" class="table">
            <thead>
                <tr>
                    <th class="text-center bc-tl">No</th>
                    <th class="text-center">Part No</th>
                    <th class="text-center" style="width:30%">Description</th>
                    <th class="text-center" style="width:4%">Qty</th>
                    <th class="text-center">Unit</th>
                    <th class="text-center" style="width:8%">Rate</th>
                    <th class="text-center">Tax(%)</th>
                    <th class="text-center">Tax Amount</th>
                    <th class="text-center">Discount</th>
                    <th class="text-center bc-tr">Net Amount</th>
                </tr>
            </thead>
            <tbody>
               
                    <?php
                $m=1;
                $condition .= '1=1';
                $col='9';
                if($bonus_quantity=="Y"){
                    $col = $col+1;
                }
                $condition .= ' AND t.invoice_id ="' . $id.'"';
                $total=$total_vat=0;
                 $count_alternate_ids = InvoiceItem::model()->count(array('condition'=>'alternate_id!="" AND t.invoice_id ="' . $id.'"'));
                if($count_alternate_ids>0){
                    $order_by='alternate_id ASC';
                }
                else{
                    $order_by='id ASC';
                }
                $modelInvoiceItem = InvoiceItem::model()->findAll(array('condition'=>$condition,'order'=>$order_by));
                foreach ( $modelInvoiceItem as $i =>  $modelInvoiceItem) {
                    
                    $batch_no='';$sl_no='';
                ?>
                 <tr>
                        <td>
                             <?php 
                            if($count_alternate_ids>0){
                                echo $modelInvoiceItem->alternate_id;
                            }
                            else
                            {
                                echo $m;
                            }
                            ?>
                        </td>
                        <td>
                            <?php echo $modelInvoiceItem->product->part_number;?>
                            <hr style="width:105%;">
                            <br/>
                             <?php if($barcode == 'Y'){?>
                             <barcode code="<?php echo $modelInvoiceItem->product->bar_code;?>" type="EAN13" height="0.66"/>
                             <?php }?>
                        </td>
                        <td>
                            <?php echo nl2br($modelInvoiceItem->description);?>
                            <?php if(!empty($modelInvoiceItem->product->bar_code)){?>
							<hr style="width:105%;">
                            <br/>
                             Bar Code: <?php echo $modelInvoiceItem->product->bar_code;?>
                            <?php } ?>
                            <?php if($modelInvoiceItem->product->have_expiry_date=='Y'){
                                    $batch_no=StockLedger::getBatchNoWithExpDate($modelInvoice->delivery_ids,$modelInvoiceItem->product_id,'DELIVERY_NOTE');
                                ?>
							<hr style="width:105%;">
                            <br/>
                             Batch No: <?php echo $batch_no;?>
                            <?php } ?>
                            <?php if($modelInvoiceItem->product->have_serial_no=='Y'){
                                $sl_no=StockLedger::getSerialNo($modelInvoice->delivery_ids,$modelInvoiceItem->product_id,'DELIVERY_NOTE');
                                ?>
							<hr style="width:105%;">
                            <br/>
                             Serial No: <?php echo $sl_no;?>
                            <?php } ?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelInvoiceItem->quantity," ");?>
                        </td>
                        <td>
                            <?php echo $modelInvoiceItem->product->unit->symbol;?>
                        </td>
                        <?php $rate=$modelInvoiceItem->rate;?>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency((($modelInvoiceItem->net_amount+$modelInvoiceItem->deduction_amount)/$modelInvoiceItem->quantity)," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo $modelInvoiceItem->tax_vat_percentage;?>
                        </td>
                        <td class="text-right">
                            <?php echo $modelInvoiceItem->tax_vat_amount;?>
                        </td>
                         <td class="text-right">
                            <?php echo $modelInvoiceItem->deduction_amount;?>
                        </td>
                        <td class="text-right">
                            <?php 
                            $net_amount = $modelInvoiceItem->net_amount + $modelInvoiceItem->tax_vat_amount;
                            echo Yii::app()->numberFormatter->formatCurrency($net_amount," ");?>
                        </td>
                </tr>
                
                    <?php
                        $m=$m+1;
                        $total+=$modelInvoiceItem->net_amount;
                        $total_vat+=$modelInvoiceItem->tax_vat_amount;
         
        }
        ?>
                    <tr>
                        <td class="text-right"  colspan="<?php echo $col; ?>"><b>Grand Total Without VAT</b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total," ");?></b></td>
                    </tr>
                    <tr>
                        <td class="text-right"  colspan="<?php echo $col; ?>"><b>VAT </b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total_vat," ");?></b></td>
                    </tr>
                <?php
                    $Addition=InvoiceAddition::model()->findAll(array('condition'=>'invoice_id='.$id.' AND calculated_value>0'));
                    $Dedution=InvoiceDeduction::model()->findAll(array('condition'=>'invoice_id='.$id.' AND calculated_value>0'));
                    if(!empty($Addition))
                    {
                        foreach ($Addition as $eachAddition)
                        {
                      ?>
                    <tr>
                        <?php
                        $total+=$eachAddition->calculated_value;
         ?>
                            <td class="text-right"  colspan="<?php echo $col; ?>">
                                    <?php
                        echo MasFooterItems::getFooterItemName($eachAddition->particular);
                        ?>
                            </td>
                            <td class="text-right">+
                                <?php echo Yii::app()->numberFormatter->formatCurrency($eachAddition->calculated_value," ");?>
                            </td>
                    </tr>
                    <?php
                    }
                    }
                    if(!empty($Dedution))
                    {
                        foreach ($Dedution as $eachDed)
                        {
                      ?>
                        <tr>
                            <?php
                        $total-=$eachDed->calculated_value;
         ?>
                                <td class="text-right"  colspan="<?php echo $col; ?>">
                                    <?php
                        echo MasFooterItems::getFooterItemName($eachDed->particular);
                        ?>
                                </td>
                                <td class="text-right">-
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($eachDed->calculated_value," ");?>
                                </td>
                        </tr>
                        <?php
                    }
                    }
                    ?>
                            <tr>
                                <td class="text-right total bc-bl"  colspan="<?php echo $col; ?>"><b>Grand Total With VAT </b></td>
                                <!--<td></td>-->
                                <td class="text-right total bc-br"><b><?php echo Yii::app()->numberFormatter->formatCurrency(($total+$total_vat)," ");?></b></td>
                            </tr>
                            <tr>
                                <td class="text-right" colspan="<?php echo $col+1; ?>"><b><?php 
                                if($total > 0){
                                    echo MasCurrency::number_to_currency(number_format(($total+$total_vat), 2, '.', ''), $modelInvoice->currency);
                                    //echo $modelInvoice->currencyname->currency_code . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval(($total+$total_vat))))->result) . ' only'; 
                                }
                                else{
                                    echo "FOC(Free Of Cost)";
                                }
?></b></td>
                            </tr>
            </tbody>
        </table>
    </div>

         <?php 
         /*echo '<pre>';
         print_r($modelInvoice);*/
         
         if($modelInvoice->payment_terms!=0)
       {
       ?>
         <table style="width:100%; page-break-inside:avoid;border:none;" class="table_box" >
            <tr>
                <td>
                    <h5 style="color: #003366;"><u>Payment Terms</u></h5><br/>
                    <div class="h5sub-details">
                        <?php echo nl2br ($modelInvoice->paymentTerm->payment_terms); ?>
                    </div>
                </td>
            </tr>
        </table>
        <?php } ?>
        <?php if($modelInvoice->terms_and_conditions!='')
       {
       ?>
        <table style="width:100%; page-break-inside:avoid;border:none;" class="table_box" >
            <tr>
                <td>
                    <h5 style="color: #003366;"><u>Terms & Conditions</u></h5><br/>
                    <div class="h5sub-details">
                        <?php echo $modelInvoice->terms_and_conditions; ?>
                    </div>
                </td>
            </tr>
        </table>
        <?php
       }
      ?>
            <?php if($modelInvoice->notes!='')
       {
       ?>
        <table style="width:100%; page-break-inside:avoid;border:none;" class="table_box" >
            <tr>
                <td>
                    <h5 style="color: #003366;"><u>Notes</u></h5><br/>
                    <div class="h5sub-details">
                        <?php echo nl2br ($modelInvoice->notes); ?>
                    </div>
                </td>
            </tr>
        </table>
            <?php } ?>
    
       <?php $model =  OwnerBankDetails::model()->find(array('condition' => 'currency_id='.$modelInvoice->currency)); 
          if(empty($model))
              $model =  OwnerBankDetails::model()->find(array('condition' => 'currency_id=23'));   
          if(!empty($model)){
    ?>
    
    <table style="width:100%; page-break-inside:avoid;border:none;" class="table_box" >
            <tr>
                <td>
                    <h5 style="color: #003366;"><u>Bank Details</u></h5><br/>
                    <div class="h5sub-details">
                    <?= 'Account Name : '.$model->bank_account_name ?><br>
                    <?= 'Account No   : '.$model->bank_ac_no ?><br>
                    <?= 'Bank Name    : '.$model->bank->bank_name ?><br>
                    <?= 'Bank Branch  : '.$model->bank_branch ?><br>
                    <?= 'IBAN No      : '.$model->bank_iban ?><br>
                    <?= 'Swift No     : '.$model->bank_swift ?>
                </div>
                </td>
            </tr>
        </table>
          <?php } ?>
   
        <table style="width:100%; page-break-inside:avoid;margin-bottom: 21rem;border:none;" class="table_box" >
            <tr>
                <td colspan="3">
                    <div class="row" style="margin-top: 1rem; margin-left: 2px;"><h5 style="color: #003366;">Acknowledge Receipt : _________________________________________________________________________________________________________</h5></div>
                <br><br><br></td>
            </tr>
            <tr style="position:relative">
                <td style="float:left; width: 40%;text-align: center;">
                    ___________________________________________ <br>
			                   <span style="text-align: center;"><small>Name</small></span>
			    </td>
			    <td style="width: 20%;"></td>
			    <td style="width: 40%;float: right;text-align: center;"> 
			         ___________________________________________  <br>
			          
			                  <span style="text-align: center;"><small>Signature</small></span>
			    </td>
            </tr>
        </table>
<!-- <div class="box" style="width: 100%; margin-bottom: 21rem;"> 
			<div class="col-12">
			    
			</div><br><br>
			
			
			<div style="position:relative">
			    
			    <div style="float:left; width: 40%;text-align: center;"> 
			         ___________________________________________ <br>
			                   <span style="text-align: center;"><small>Name</small></span>
			    </div>
			    <div style="width: 20%;"></div>
			    <div style="width: 40%;float: right;text-align: center;"> 
			         ___________________________________________  <br>
			          
			                  <span style="text-align: center;"><small>Signature</small></span>
			    </div>
			</div>
			</div>-->




 <htmlpagefooter name="myfooter">

<!--            <div style="text-align:center;width:100%;font-size:10px"><?php echo Yii::app()->user->footer_autogenerated; ?>
        <?php
                  /*date_default_timezone_set("Asia/Dubai");
                 echo date('d/m/Y')." ".date("h:i:sa");*/ ?>
    </div>-->

 <img src="<?php echo  'images/logo/pdf_logo_footer.png'; ?>"/>
<!--        <div style="border-top: 1px dotted #000000; font-size: 12px; text-align: center; padding-top: 10px; background-color: #d7faeb;">
            <div style="text-align:center;width:100%">
                <b><?php //echo Yii::app()->user->footer_line_1; ?></b><br>
                <?php //echo Yii::app()->user->footer_line_2; ?>
            </div>
            <div style="text-align:center;width:100%">Page {PAGENO} of {nb} </div>
        </div>-->
    </htmlpagefooter>
</body>

</html>
