<!DOCTYPE html>
<html>

    <head>
        <title>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>
    </head>
    <?php
    $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);
    $warehouse_id = $modelInvoice->warehouse_id;
    ?>

    <body>
         <div class="border-red" style="border:7px solid red;padding: 5px;height: 100%;">
        <div class="" >
            <?php
            $this->renderPartial('//common/header', array('warehouse_id' => $warehouse_id));

//$this->renderPartial("//common/header");
            ?>
            <div class="col-4">

                <?php
                $modelInvoice = Invoice::model()->findByPk($id);
                $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>
                <?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" style="font-size:10px;">
                        Our TRN# <b><?php echo $settings->trn_no; ?></b>
                    </div>
                <?php } ?>
            </div>
            <div class="col-3">
                <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
                    <?php echo "INVOICE"; ?>
                    <br><br>
                </div>
                <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
                </div>
            </div>
            <div class="col-5" style="color:#101010;">
                <table class="table" style="width:100%" style="font-size:10px;">
                    <tr>
                        <td >Customer TRN #</td>
                        <td>
                            <?php
                            $modelInvoice = Invoice::model()->findByPk($id);
                            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>Payment Terms</td>
                        <td>
                            <?php echo $modelInvoice->paymentTerm->payment_terms; ?>
                        </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" style="font-size:10px;">
                <thead>
                    <tr>
                        <th class="text-center bc-tl">No</th>

                        <th class="text-center" style="width:30%">Description</th>
                        <th class="text-center" style="width:4%">Qty</th>
                        <?php if ($bonus_quantity == "Y") { ?>
                            <th class="text-center" style="width:4%">Bonus Qty</th>
                        <?php } ?>
                        <th class="text-center">Unit</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';
                    $col = '5';
                    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';
                    }
                    $model_Invoice = InvoiceItem::model()->findAll(array('condition' => $condition, 'order' => $order_by));
                    foreach ($model_Invoice as $i => $model_Invoice) {

                        if ($model_Invoice->product->inventory_item_type == '2') {
                            ?>
                            <tr style="background: #E3F2FD;">
                                <td colspan="<?php echo $col + 1; ?>"><?php echo $model_Invoice->description; ?></td>
                            </tr>

                            <?php
                        } else {
                            //  if($model_Invoice->checked==1)
                            //  {
                            ?>
                            <tr>
                                <td>
                                    <?php echo $m; ?>
                                </td>

                                <td>
                                    <?php echo nl2br($model_Invoice->description); ?>
                                </td>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($model_Invoice->quantity, " "); ?>
                                </td>
                                <?php if ($bonus_quantity == "Y") { ?>
                                    <td class="text-right">
                                        <?php echo Yii::app()->numberFormatter->formatCurrency($model_Invoice->bonus_qty, " "); ?>
                                    </td>
                                <?php } ?>
                                <td>
                                    <?php echo $model_Invoice->product->unit->unit_name; ?>
                                </td>
                                <?php $rate = $model_Invoice->net_amount / $model_Invoice->quantity; ?>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($rate, " "); ?>
                                </td>

                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($model_Invoice->net_amount, " "); ?>
                                </td>
                            </tr>

                            <?php
                            $m = $m + 1;
                            $total += $model_Invoice->net_amount;
                            $total_vat += $model_Invoice->tax_vat_amount;
                            // }
                        }
                    }
                    ?>
                    <tr>
                        <td class="text-right" colspan="<?php echo $col; ?>"><b>Net Total </b></td>
                        <!--<td></td>-->
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></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 FooterItems::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 FooterItems::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 </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>
                        <?php
                        $fcurrency = new MasCurrency();
                        ?>
                        <td class="text-right" colspan="<?php echo $col + 1; ?>"><b><?php
                                if ($total > 0) {
                                    echo $fcurrency->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>
                    <!--          <tr>
           <td class="text-right" colspan="5"><b><?php //echo ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($total)))->result) . ' only';
                                ?></b></td>
                        </tr>-->
                </tbody>
            </table>
        </div>
        <?php
        $model = OwnerBankDetails::model()->find();
        if (!empty($model)) {
            ?>
            <h5><u>Bank Details</u></h5>
            <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>
        <?php } ?>
        <?php $this->renderPartial('//common/footer', array('warehouse_id_pass' => $warehouse_id, 'company_id' => $company_id));
        ?>

 </div>
    </body>

</html>