<!DOCTYPE html>
<html>


    <?php
    if (isset(Yii::app()->user->master_user_company_id) && (int) Yii::app()->user->master_user_company_id > 0) {
        $company_id = Yii::app()->user->master_user_company_id;
    }


    // $mpdf->use_kwt = true;

    $modelDirectInvoice = DirectInvoice::model()->findByPk($id);
    $modelInvoice = Invoice::model()->find(array('condition' => 'order_id=' . $id . ' AND is_direct_invoice="Y"'));
    $margintop = "10 cm";
    if (strlen($modelInvoice->customer->customer_name) > 25) {
        $margintop = "11 cm";
    }
    ?>
    <head>
        <title>INVOICE_<?php echo $id; ?>".pdf"</title>
        
    </head>
    <?php
    if (isset(Yii::app()->user->pdf_company_logo)) {
        $logo_name = Yii::app()->user->pdf_company_logo;
    } else {
        $logo_name = "pdf_logo.png";
    }
    $model_Delivery = DeliveryNote::model()->findByPk($id);
    ?>
    <body style="font-family: 'Times New Roman' Times serif">
        <div class="main" >
            <?php $this->renderPartial("//common/header");?>
                    <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 $modelDirectInvoice->id; ?></b>
                        </div>
                        <b><?php echo ($modelInvoice->customer->customer_name); ?></b><br>
                        <?php if ($modelInvoice->customer->customer_address != '') { ?>
                            <small><?php echo strtoupper(nl2br($modelInvoice->customer->customer_address)); ?></small><br>
                        <?php } ?>
                        <?php if ($modelInvoice->customer->customer_phone_no != '') { ?>
                            <small>Phone : <?php echo strtoupper($modelInvoice->customer->customer_phone_no); ?></small><br>
                        <?php } ?>
                        <small>Customer TRN :</small>
                        <small><b>
                                <?php echo $modelInvoice->customer->customer_trn_no; ?></b></small>
                        <?php if ($modelInvoice->contact->person_name != '') { ?>
                            <br><small>Kind Attn:</small>
                            <small><b><?php echo strtoupper($modelInvoice->contact->person_name); ?></b></small>
                        <?php } ?>
                    </div>
                    <div class="col-3">
                        <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;color: #000000;font-size: 14px;">
                            <?php echo "PROFORMA INVOICE"; ?>
                        </div>
                        <div style="width:100%;font-family:'Times New Roman', Times, serif;font-style:italic; margin-bottom:2cm; text-align:center;"> ---- Page {PAGENO} ----</div>
                        <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
                            <?php
                            if ($barcode == 'Y') {
                                ?>
                                <barcode code="<?php echo $modelDirectInvoice->id; ?>" type="EAN13" height="0.66"  size="1.2"/>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
                    <div class="col-5" style="color:#101010;">
                        <?php $settings = MasCompany::model()->findByPk($company_id); ?>
                        <?php if ($settings->trn_no != '') { ?>
                            <div class="input-block"  style="font-size:13px; margin-left: 80px; ">
                                Our TRN# <b><?php echo $settings->trn_no; ?></b>
                            </div>
                        <?php } ?>
                        <table class="table" style="width:100%"  style="font-size:13px;">
                            <tr>
                                <td>Invoice Date</td>
                                <td>
                                    <?php echo date('d/m/Y', STRTOTIME($modelDirectInvoice->invoice_date)); ?>
                                </td>
                            </tr>
                            <tr>
                                <td>Delivery Date</td>
                                <td>
                                    <?php echo '___ / ___ / _________'; ?>
                                </td>
                            </tr>
                            <tr>
                                <td>Delivery Note #</td>

                                <td>
                                    <?php echo implode(PHP_EOL, explode(',', $modelInvoice->delivery_ids)); ?>
                                </td>
                            </tr>
                            <tr>
                                <td>Currency</td>
                                <td>
                                    <?php echo $modelDirectInvoice->currency_details->currency_code; ?>
                                </td>
                            </tr>
                            <tr>
                                <td>Payment Terms</td>
                                <td>
                                    <?php echo $modelDirectInvoice->paymentTerm->payment_terms; ?>
                                </td>
                            </tr>
                        </table>
                    </div>
            </htmlpageheader>
            <sethtmlpageheader name="firstpage" value="off" show-this-page="1" />
            <!-- <sethtmlpageheader name="otherpages" value="on" /> -->
            <div>
               <!--  <div class="header" style="width:100%;float: left; padding-bottom:10px;">
                    <img src="<?php echo 'images/logo/' . $logo_name; ?>"/>
                </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 $modelDirectInvoice->id; ?></b>
                    </div>
                    <b><?php echo ($modelInvoice->customer->customer_name); ?></b><br>
                    <?php if ($modelInvoice->customer->customer_address != '') { ?>
                        <small><?php echo strtoupper(nl2br($modelInvoice->customer->customer_address)); ?></small><br>
                    <?php } ?>
                    <?php if ($modelInvoice->customer->customer_phone_no != '') { ?>
                        <small>Phone : <?php echo strtoupper($modelInvoice->customer->customer_phone_no); ?></small><br>
                    <?php } ?>
                    <small>Customer TRN :</small>
                    <small><b>
                            <?php echo $modelInvoice->customer->customer_trn_no; ?></b></small>
                    <?php if ($modelInvoice->contact->person_name != '') { ?>
                        <br><small>Kind Attn:</small>
                        <small><b><?php echo strtoupper($modelInvoice->contact->person_name); ?></b></small>
                    <?php } ?> -->
                </div>
                <!-- <div class="col-3">
                    <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;color: #000000;font-size: 14px;">
                        <?php echo "PROFORMA INVOICE"; ?>
                    </div>
                    <div style="width:100%;font-family:'Times New Roman', Times, serif;font-style:italic; margin-bottom:2cm;text-align:center;"> ---- Page 1 ----</div>
                    <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
                        <?php
                        if ($barcode == 'Y') {
                            ?>
                            <barcode code="<?php echo $modelDirectInvoice->id; ?>" type="EAN13" height="0.66"  size="1.2"/>
                            <?php
                        }
                        ?>
                    </div>
                </div>
                <div class="col-5" style="color:#101010;">
                    <?php $settings = MasCompany::model()->findByPk($company_id); ?>
                    <?php if ($settings->trn_no != '') { ?>
                        <div class="input-block"  style="font-size:13px; margin-left: 80px; ">
                            Our TRN# <b><?php echo $settings->trn_no; ?></b>
                        </div>
                    <?php } ?>
                    <table class="table" style="width:100%"  style="font-size:13px;">

                        <tr>
                            <td>Invoice Date</td>
                            <td>
                                <?php echo date('d/m/Y', STRTOTIME($modelDirectInvoice->invoice_date)); ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Delivery Date</td>
                            <td>
                                <?php echo '___ / ___ / _________'; ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Delivery Note #</td>
                            <td>
                                <?php echo implode(PHP_EOL, explode(',', $modelInvoice->delivery_ids)); ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Currency</td>
                            <td>
                                <?php echo $modelDirectInvoice->currency_details->currency_code; ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Payment Terms</td>
                            <td>
                                <?php echo $modelDirectInvoice->paymentTerm->payment_terms; ?>
                            </td>
                        </tr>
                        <?php
                        if (isset($modelInvoice->leadowner->first_name) && $modelInvoice->leadowner->first_name != "") {
                            ?>
                            <tr>
                                <td colspan="2"><b>Created by:&nbsp;</b>
                                    <?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>
        </div>
        <br>
        <div class="col-md-12">
            <table id="" class="table"  style="font-size:13px;margin-top: -10px;">
                <thead>
                    <tr>
                        <th class="text-center bc-tl">No</th>
                        <th class="text-center" style="width:25%">Part No</th>
                    <!--<th class="text-center" style="width:20%">Description</th>-->
                        <th class="text-center">Capacity</th>
                        <th class="text-center">Packing</th>
                        <th class="text-center">Qty</th>
                        <th class="text-center">Unit</th>
                        <th class="text-center" style="width:10%">Rate</th>
                        <th class="text-center">Total</th>
                        <th class="text-center" style="width:10%">Tax Amt</th>
                        <th class="text-center bc-tr">Net Amt</th>
                    </tr>
                </thead>
                <tbody>

                    <?php
                    $m = 1;
                    $condition .= '1=1';
                    $condition .= ' AND t.invoice_id ="' . $id . '"';
                    $total = 0;
                    $tax_total = 0;
                    $modelDirectInvoiceItem = DirectInvoiceItem::model()->findAll(array('condition' => $condition));
                    foreach ($modelDirectInvoiceItem as $i => $modelDirectInvoiceItem) {
                        ?>
                        <tr>
                            <td>
                                <?php echo $m; ?>
                            </td>
                            <td>
                                <?php echo $modelDirectInvoiceItem->product->part_number; ?>
                            </td>
                            <!--<td>
                            <?php //echo $modelDirectInvoiceItem->description; ?>
                            </td>-->
                            <td>
                                <?php echo $modelDirectInvoiceItem->product->capacity; ?>
                            </td>
                            <td>
                                <?php echo $modelDirectInvoiceItem->product->packing; ?>
                            </td>
                            <td class="text-right">
                                <?php echo (float) $modelDirectInvoiceItem->quantity; ?>
                            </td>
                            <td>
                                <?php echo $modelDirectInvoiceItem->uomdetails->name;  //.'('.$modelDirectInvoiceItem->unitdetails->factor.')'?>
                            </td>
                            <?php $rate = $modelDirectInvoiceItem->rate; ?>
                            <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($rate, " "); ?>
                            </td>
                            <?php $_total = $modelDirectInvoiceItem->rate * $modelDirectInvoiceItem->quantity; ?>
                            <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($_total, " "); ?>
                            </td>
                            <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($modelDirectInvoiceItem->tax_amount, " "); ?>
                            </td>

                            <td class="text-right">
                                <?php
                                echo
                                $line_net_total = $modelDirectInvoiceItem->amount + $modelDirectInvoiceItem->tax_amount;
                                Yii::app()->numberFormatter->formatCurrency($line_net_total, " ");
                                ?>
                            </td>
                        </tr>

                        <?php
                        $m = $m + 1;
                        $total += $modelDirectInvoiceItem->amount;
                        $tax_total += $modelDirectInvoiceItem->tax_amount;
                        if ($modelDirectInvoiceItem->uomdetails->name == 'CTN') {

                            $item_cbm = $modelDirectInvoiceItem->product->cubic_meter;
                            $item_weight = $modelDirectInvoiceItem->product->weight;
                            $total_cbm += ($modelDirectInvoiceItem->quantity * $item_cbm);
                            $total_weight += ($modelDirectInvoiceItem->quantity * $item_weight);
                            $total_qty = $modelDirectInvoiceItem->quantity;
                            $tot_cartoons += $total_qty;
                        }
                    }
                    ?>
                    <tr>
                        <td class="text-right" colspan="9"><b>Total  </b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                    </tr>
                    <?php
                    $Addition = DirectInvoiceAddition::model()->findAll(array('condition' => 'invoice_id=' . $id . ' AND calculated_value>0'));
                    $Dedution = DirectInvoiceDeduction::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="10">
                                    <?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="10">
                                    <?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="9"><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($modelDirectInvoice->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="9"><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="10"><b><?php 
                           $MasCurrency=new MasCurrency();
                        echo$MasCurrency->number_to_currency(number_format(($total), 2, '.', ''), $modelDirectInvoice->currency_id); //echo $modelDirectInvoice->currencyname->currency_code . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($total)))->result) . ' only';       ?></b></td>
                    </tr>
                </tbody>
            </table>
        </div>
        <br>
        <!--
        <div style="width: 100%;">
            <div style="width: 48%; height: 80px; float: left; border: 1px solid #1f67b2; padding: 5px;">
                <h5>Please ensure that all items are quoted in Carton</h5>
                <div class="h5sub-details" style="font-size: 13px;">Total CBM : <b>
                        <?php //echo $total_cbm; ?> </b>CBM</div>
                <div class="h5sub-details" style="font-size: 13px;">Total Weight : <b><?php //echo $total_weight; ?> </b>KG</div>

                <div class="h5sub-details" style="font-size: 13px;">No. of Cartoons :

                    <b><?php //echo $tot_cartoons; ?> </b> </div>
            </div>
            <div style="width: 48%; height: 80px; float: right; padding: 5px; border: 1px solid #1f67b2;">
                <h5></h5>
                <div  style="font-size: 13px;">Received Amount : <b>______________ </b></div><br>
                <div  style="font-size: 13px;">Balance :&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>______________ </b></div>
            </div>
        </div>-->

        <?php
        if ($modelDirectInvoice->terms_and_conditions != '') {
            ?>
            <div class="box" style="width:100%">
                <h5>Terms & Conditions</h5>
                <div class="h5sub-details">
                    <?php echo nl2br($modelDirectInvoice->terms_and_conditions); ?>
                </div>
            </div>
            <?php
        }
        ?>
        <?php /* if($modelInvoice->notes!='')
          {
          ?>

          <div class="box" style="width:100%">
          <h5>Notes</h5>
          <div class="h5sub-details">
          <?php echo  nl2br ($modelInvoice->notes); ?>
          </div>
          </div>
          <?php } */ ?>


        <div style="width: 100%; height: 80px; border: 1px solid #1f67b2; margin-top: 10px;padding: 1px;">
            <div style="float:left; width: 40%;text-align: center;">
                <br>
                <br>
                ___________________________________________ <br>
                <span style="text-align: center;"><small>Order by/Contact Number</small></span>
            </div>
            <div style="width: 20%;"></div>
            <div style="width: 40%;float: right;text-align: center;">
                <img src="images/signature.jpg">  <br>
                <span style="text-align: center;"><small>Authorized Signature</small></span>
            </div>
        </div>

        <?php
        $condition = 't.isActive="Y" and (t.file_id = "' . $modelInvoice->file_id . '" AND t.file_id > 0)';
        $modelDocument = Document::model()->findAll(array('condition' => $condition));
        if (count($modelDocument) > 0) {
            ?>

            <?php
            foreach ($modelDocument as $eachDocument) {
                ?>
                <div class="start-newpage"  style="page-break-before:always">
                    <img src="uploads/documents/<?php echo $eachDocument['document_name']; ?>">
                </div>
                <?php
            }
            ?>


        <?php } ?>


        <?php
        $modelDirectInvoice = DirectInvoice::model()->findByPk($id);
        $created_datetime = $modelDirectInvoice->created_datetime;
        ?>
        <?php //$this->renderPartial("//common/footer",array('company_id'=>$company_id,'created_datetime'=>$created_datetime,'from'=>'direct')); ?>
     <?php $this->renderPartial("//common/footer");?>

</body>

</html>



