<!DOCTYPE html>
<html>

<head>
    <title>PURCHASE ORDER</title>
</head>

<body>
<div class="border-red">   
    <div class="">
        <?php $this->renderPartial("//common/header"); ?>
        <div class="col-4">
            <br>
            <?php $modelOrder = PurchasingOrder::model()->findByPk($id); ?>
            <div class="input-block">
                <?php
                $year = date('y');
                $formattedId = "RAM{$year}-PO $id";
                ?>
                <b><?php echo $formattedId; ?></b>
                <br>


            </div>
            <b><?php echo $modelOrder->vendor_details->vendor_name; ?></b>
            <br>
            <small><?php echo $modelOrder->vendor_details->vendor_address; ?></small><br>

            <?php
            if (isset($modelOrder->vendor_details->vendor_postal_code) && $modelOrder->vendor_details->vendor_postal_code != "") {
                ?>
                <small><?php echo 'PO Box : ' . $modelOrder->vendor_details->vendor_postal_code . ", "; ?></small>
                <?php
            }
            /*if($modelOrder->vendor_details->vendor_postal_code!="" || $modelOrder->vendor_details->vendor_location!=""){
            ?>    
            <small>
            <?php echo 'PO Box : ';
            if(isset($modelOrder->vendor_details->vendor_postal_code) && $modelOrder->vendor_details->vendor_postal_code!=""){
                echo $modelOrder->vendor_details->vendor_postal_code;
            }
            if($modelOrder->vendor_details->vendor_postal_code!="" && $modelOrder->vendor_details->vendor_location!=""){
                echo ", ";
            }
            if(isset($modelOrder->vendor_details->vendor_location) && $modelOrder->vendor_details->vendor_location!=""){
                echo $modelOrder->vendor_details->vendor_location; 
            }
            ?></small><br>
            <?php 
            }*/
            if (!empty($modelOrder->vendor_details->vendor_city)) { ?>
                <small><?php echo $modelOrder->vendor_details->vendor_city . ', ' ?></small>
                <?php
            }
            ?>
            <small><?php echo $modelOrder->vendor_details->country->country_name; ?></small><br>
            <small><?php echo $modelOrder->vendor_details->vendor_phone_no; ?></small><br>

            <br>
            <small><?php
            if (isset($modelOrder->start_text)) {

                echo "Dear Sir,<br><br>" . nl2br($modelOrder->start_text);
            }
            ?>
            </small>

        </div>



        <div class="col-4">
            <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
                <?php echo "PURCHASE ORDER"; ?>
            </div>
        </div>
        <div class="col-4" style="color:#101010;">
            <table class="table" style="width:100%" style="font-size:10px;">
                <tr>
                    <td style="width:40%">File ID</td>
                    <td>
                        <?php echo $modelOrder->file_id; ?>
                    </td>
                </tr>

                <?php if ($modelOrder->vendor_details->vendor_trn_no != '') { ?>
                    <tr>
                        <td style="width:40%">Vendor TRN#</td>
                        <td><?php echo $modelOrder->vendor_details->vendor_trn_no; ?></td>
                    </tr>
                <?php } ?>
                <tr>
                    <td style="width:40%">Order Date</td>
                    <td><?php echo date("d/m/Y", strtotime($modelOrder->order_date)); ?></td>
                </tr>

                <tr>
                    <td style="width:40%">Required Date</td>
                    <td><?php echo date("d/m/Y", strtotime($modelOrder->required_date)); ?></td>
                </tr>
                <tr>
                    <td style="width:40%">Currency</td>
                    <td><?php echo $modelOrder->currencyname->currency_code; ?></td>
                </tr>
                <tr>
                    <td style="width:40%">Our TRN #</td>
                    <td><?php echo MasCompany::model()->find()->trn_no ?></td>
                </tr>
                <tr>
                    <?php
                    ?>
                    <td style="width:40%">Payment Terms</td>
                    <td><?php echo $modelOrder->paymentterms->payment_terms; ?></td>
                </tr>
                <!--            <tr>
                <td>Expected Delivery Date</td>
                <td><?php // echo date("d/m/Y", strtotime($modelOrder->expected_delivery_date)); ?></td>
            </tr>-->

                </tr>
            </table>
            <div class="input-block">
                <barcode code="<?php echo $id; ?>" type="C39" height="0.66" size="1.2" />
            </div>
        </div>
    </div>


    <br>
    <br>

    <div class="clearfix">
        <table id="tbl_order_table" class="table table-fixed table-primary dataTable" style="font-size:10px;">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Item</th>
                    <th>Description</th>
                    <th>Quantity</th>
                    <th>Unit</th>
                    <th>Rate</th>
                    <?php if ($modelOrder->taxtype->tax == 'Y') { ?>
                        <th>Tax %</th>
                        <th>Tax Amount</th>
                    <?php } ?>
                    <?php if ($modelOrder->completed == "C") { ?>
                        <th>Received Qty</th>
                        <th>Balance Qty</th>
                        <th>Notes</th>
                    <?php } ?>
                    <th>Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php

                $model_req_stock = PurchasingOrderStockItem::model()->findAll(array('condition' => 't.purchasing_order_id="' . $model->id . '"'));
                $total_tax_amount = 0;
                $total = 0;
                foreach ($model_req_stock as $i => $model) {
                    ?>
                    <tr>
                        <td><?php echo $i + 1; ?></td>
                        <td><?php echo $model->stock_item_name; ?></td>
                        <td><?php echo $model->stock_item_description; ?>
                            <?php if (!empty($model->product->bar_code) && $frm == 1) { ?>
                                <hr style="width:105%;">
                                <br />
                                <barcode code="<?php echo $model->product->bar_code; ?>" type="EAN13" height="0.66" size="1.2" />
                            <?php } ?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model->stock_item_quantity, ""); ?></td>
                        <td><?php echo $model->unitdetails->name; ?></td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model->stock_item_rate, ""); ?></td>
                        <?php if ($modelOrder->taxtype->tax == 'Y') { ?>
                            <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($model->tax_percentage, ""); ?></td>
                            <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($model->tax_amount, ""); ?></td>
                        <?php } ?>

                        <?php if ($modelOrder->completed == "C") { ?>
                            <td class="text-center">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($model->received, ""); ?></td>
                            <td class="text-center">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($model->remaining_qty, ""); ?></td>
                            <td><?php echo $model->notes; ?></td>
                        <?php } ?>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model->stock_item_amount, ""); ?></td>
                        <?php $total += $model->stock_item_amount;

                        $total_tax_amount += $model->tax_amount;
                } ?>
                </tr>
                <?php
                if ($modelOrder->taxtype->tax == 'Y') {
                    $col = 9;
                    $colx = 0;
                    if ($modelOrder->completed == "C") {
                        $colx = 6;
                        $col = 12;
                    }
                } else {
                    $col = 7;
                    $colx = 0;
                    if ($modelOrder->completed == "C") {
                        $colx = 4;
                        $col = 10;
                    }
                }
                ?>
                <tr>
                    <td class="text-right" colspan="<?php echo $col - 1; ?>"><b>Total </b></td>
                    <td class="text-right" colspan="<?php echo $colx; ?>">
                        <b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                </tr>
                <?php
                $Addition = PurchasingOrderAddition::model()->findAll(array('condition' => 'purchasing_order_id=' . $modelOrder->id . ' AND calculated_value>0'));
                $Dedution = PurchasingOrderDeduction::model()->findAll(array('condition' => 'purchasing_order_id=' . $modelOrder->id . ' AND calculated_value>0'));
                if (!empty($Addition)) { ?>
                    <?php foreach ($Addition as $each) { ?>
                        <tr>
                            <td class="text-right" colspan="<?php echo $col - 1; ?>">
                                <?php echo MasFooterItems::getFooterItemName($each->particular); ?></td>
                            <td class="text-right" colspan="<?php echo $colx; ?>">
                                <b><?php echo Yii::app()->numberFormatter->formatCurrency($each->calculated_value, " "); ?></b>
                            </td>
                        </tr>
                        <?php $total += $each->calculated_value;
                    }
                } ?>
                <?php if (!empty($Dedution)) { ?>
                    <?php foreach ($Dedution as $each) { ?>
                        <tr>
                            <td class="text-right" colspan="<?php echo $col - 1; ?>">
                                <?php echo MasFooterItems::getFooterItemName($each->particular); ?></td>
                            <td class="text-right" colspan="<?php echo $colx; ?>">
                                <b><?php echo Yii::app()->numberFormatter->formatCurrency($each->calculated_value, " "); ?></b>
                            </td>
                        </tr>
                        <?php $total -= $each->calculated_value;
                    }
                } ?>

                <tr>
                    <td class="text-right" colspan="<?php echo $col - 1; ?>"><b>VAT</b></td>
                    <td class="text-right" colspan="<?php echo $colx; ?>">
                        <b><?php echo Yii::app()->numberFormatter->formatCurrency($total_tax_amount, " "); ?></b></td>
                </tr>
                <tr>
                    <td class="text-right" colspan="<?php echo $col - 1; ?>"><b>Grand Total</b></td>
                    <td class="text-right" colspan="<?php echo $colx; ?>">
                        <b><?php echo Yii::app()->numberFormatter->formatCurrency($total + $total_tax_amount, " "); ?></b>
                    </td>
                </tr>
                <tr>
                    <td class="text-right" colspan="<?php echo $col; ?>"><b>
                            <?php
                            $fcurrency = new MasCurrency();
                            ?>
                            <?php
                            if ($total > 0) {
                                echo $fcurrency->number_to_currency(number_format(($total + $total_tax_amount), 2, '.', ''), $modelOrder->currency);
                                //echo $modelOrder->currencyname->currency . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($total)))->result) . ' only'; 
                            } else {
                                echo "FOC(Free Of Cost)";
                            }
                            ?>
                        </b>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>




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

    <?php if ($modelOrder->notes != '') {
        ?>
        <div class="box" style="width: 100%">
            <h5>Notes</h5>
            <div class="h5sub-details">
                <?php echo nl2br(htmlspecialchars($modelOrder->notes)); ?>
            </div>
        </div>
        <?php
    } ?>

    <?php //if($modelOrder->remarks!='')
    //{
    ?>
    <!-- <div class="box"  style="width: 100%">
            <h5>Remarks</h5>
            <div class="h5sub-details">
                <?php //echo nl2br ($modelOrder->remarks); ?>
            </div>
        </div> -->
    <?php
    // } ?>

    <?php $this->renderPartial("//common/footer"); ?>
       </div>
</body>

</html>