<!DOCTYPE html>
<html>

    <head>
        <title>Direct Purchase Invoice</title>
    </head>

    <body>
        
                 <div class="">
        <div class="col-6">
            <br>
          <?php
                         $modelInvoice = DirectPurchaseInvoice::model()->findByPk($id);
                         $modelPurchaseInvoice = PurchasingInvoice::model()->find(array('condition'=>'po_id='.$id.' AND purchase_invoice="DP"'));
                        ?>  
                <div class="h3" style="margin-top: 0;margin-bottom: 0;">
                    <?php echo "PURCHASE INVOICE"; ?>
                </div>
               <div class="input-block">
                 <b><?php echo $modelInvoice->id;?></b>
                </div>
                <b><?php echo $modelInvoice->vendor_details->vendor_name; ?></b>
                <br/>
                <small><?php echo  $modelInvoice->vendor_details->vendor_address; ?></small><br/>
                <small><?php echo  'PO Box : '.$modelInvoice->vendor_details->vendor_postal_code.', '.$modelInvoice->vendor_details->vendor_location; ?></small><br/>
                <small><?php echo  $modelInvoice->vendor_details->vendor_city.', '.$modelInvoice->vendor_details->country->country_name; ?></small><br/>
                <small><?php echo  $modelInvoice->vendor_details->vendor_phone_no; ?></small><br/>
                
        </div>
        <div class="col-5 offset-1" style="color:#101010;">
            <div class="text-right">
                <!--img src="<?php //echo Yii::app()->baseUrl . '/images/logo/'.Yii::app()->user->pdf_company_logo; ?>" class="logo"-->
                <img src="<?php echo 'images/logo/'.Yii::app()->user->pdf_company_logo; ?>" class="logo">
            </div>
            <br>
            <table class="table" style="width:100%">
  <tr>
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo  (!empty($modelPurchaseInvoice->file_id))?$modelPurchaseInvoice->file_id:"Nil";?>
                        </td>
                    </tr>
                    <tr>
                        <td class="bc-tl">PO #</td>
                        <td>Nil</td>
                    </tr>
                    <tr>
                        <td class="bc-tl">MR #</td>
                        <td>Nil</td>
                    </tr>
                    <tr>
                        <td>Vendor Invoice No</td>
                        <td>
                            <?php echo $modelInvoice->invoice_no; ?>
                        </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 echo $modelInvoice->currencyname->currency_code . '/' . $modelInvoice->exchange_rate; ?>
                        </td>
                    </tr>
                    </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">Tax %</th>
                        <th class="text-center">Tax Amount</th>
                        <th class="text-center" style="width:8%">Rate</th>
                        <th class="text-center bc-tr">Net Amount</th>
                    </tr>
                </thead>
                <tbody>

                    <?php
                    $m = 1;
                    $condition .= '1=1';
                    $condition .= ' AND t.purchasing_invoice_id ="' . $id . '"';
                    $total = 0;
                    $tax_total = 0;
                    $modelInvoiceItem = DirectPurchaseInvoiceItem::model()->findAll(array('condition' => $condition));
                    foreach ($modelInvoiceItem as $i => $modelInvoiceItem) {

                            ?>
                            <tr>
                                <td>
        <?php echo $m; ?>
                                </td>
                                <td>
        <?php echo $modelInvoiceItem->product->oem_number; ?>
                                </td>
                                <td>
        <?php echo $modelInvoiceItem->description; ?>
                                </td>
                                <td class="text-right">
                                    <?php echo (float) $modelInvoiceItem->quantity; ?>
                                </td>
                                <td>
                                    <?php echo $modelInvoiceItem->unit;  //.'('.$modelInvoiceItem->unitdetails->factor.')'?>  
                                </td>
                                <td class="text-right">
                                    <?php echo (int) $modelInvoiceItem->tax_percentage; ?>
                                </td>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($modelInvoiceItem->tax_amount, " "); ?>
                                </td>
                                <?php $rate = $modelInvoiceItem->rate; ?>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($rate, " "); ?>
                                </td>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($modelInvoiceItem->amount, " "); ?>
                                </td>
                            </tr>

                            <?php
                            $m = $m + 1;
                            $total+=$modelInvoiceItem->amount;
                            $tax_total+=$modelInvoiceItem->tax_amount;
                            
                    }
                    ?>
                    <tr>
                        <td class="text-right" colspan="8"><b>Total  </b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                    </tr>
                    <?php
                    $Addition = DirectPurchaseInvoiceAddition::model()->findAll(array('condition' => 'purchasing_invoice_id=' . $id . ' AND calculated_value>0'));
                    $Dedution = DirectPurchaseInvoiceDeduction::model()->findAll(array('condition' => 'purchasing_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="8">
                                <?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>
                            <?
                            $total-=$eachDed->calculated_value;
                            ?>
                                <td class="text-right" colspan="8">
                                <?php
                                echo MasFooterItems::getFooterItemName($eachDed->particular);
                                ?>
                                </td>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($eachDed->calculated_value, " "); ?>
                                </td>
                            </tr>
                                    <?php
                                }
                            }
                            $total+=$tax_total;
                            ?>
                    <tr>
                        <td class="text-right" colspan="8"><b>VAT  </b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($tax_total, " "); ?></b></td>
                    </tr>  
                    <!--tr>
                       <td class="text-right" colspan="9"><b>VAT Amount in AED  <?php /* $model_currency=MasCurrency::model()->findByPk($modelInvoice->currencyname->id);
                      $vat_base=$model_currency->exchange_rate*$tax_total;
                      echo Yii::app()->numberFormatter->formatCurrency($vat_base," "); ?>; <?php

                      echo  "AED " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval(($vat_base))))->result) . ' only'; */ ?></b></td>
                   </tr-->
                    <tr>
                        <td class="text-right total bc-bl" colspan="8"><b>Grand Total  </b></td>
                        <td class="text-right total bc-br"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="9"><b><?php echo MasCurrency::number_to_currency(number_format(($total), 2, '.', ''), $modelInvoice->currency_id); //echo $modelInvoice->currencyname->currency_code . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($total)))->result) . ' only';  ?></b></td>
                    </tr>
                </tbody>
            </table>
<?php
if ($modelInvoice->payment_terms != 0) {
    ?>
                <div class="box" style="width:100%">
                    <h5>Payment Terms</h5>
                    <div class="h5sub-details">
                <?php echo nl2br($modelInvoice->paymentTerm->payment_terms); ?>
                    </div>
                </div>
<?php } ?>
        </div>
        <br/>
<htmlpagefooter name="firstfooter">
        <div style="text-align:right;width:100%;"><i>Contd...</i></div> 
        <div style="text-align:center;width:100%;"><img src="<?php echo 'images/'.Yii::app()->user->company_footer_logo; ?>" /></div> 
    </htmlpagefooter>
    <sethtmlpagefooter name="firstfooter" page="all" value="on" />

    <htmlpagefooter name="lastfooter">
        <div style="text-align:right;width:100%;">Contd...</div> 

        <div style="text-align:center;width:100%;"><img src="<?php echo 'images/'.Yii::app()->user->company_footer_logo; ?>" /></div> 
    </htmlpagefooter>
    <sethtmlpagefooter name="lastfooter" value="off" />

    <htmlpagefooter name="myfooter">

        <div style="text-align:center;width:100%;"><img src="<?php echo 'images/'.Yii::app()->user->company_footer_logo; ?>" /></div> 
    </htmlpagefooter>
    <sethtmlpagefooter name="myfooter" page="all" value="on" />   

</body>

</html>
