<!DOCTYPE html>
<html>
    <head>
        <title>Order List</title>
        <style>
            .table_box {
                border-collapse:separate!important;
                margin-top: 10px;
                padding: 10px;
                font-size:16px!important;
                border: 1px solid #0066ff;
                border-radius:6px;
            }
                 div {
         page-break-inside: auto;
            page-break-after: auto;

        }
 /*     @page {
        margin-top: 3.3cm;
       
          margin-bottom:3.5cm;
        margin-left: .3cm;
        margin-right: .3cm;
        header: html_myHeader;
        header: html_otherpages;
        font-family:'Times New Roman';
        border:5px solid red;
          height: 280mm;

    }
*/
        </style>
    </head>

    <body>

        <div style="padding: 35px;">
           <div class="" >
    
            
        <div class="">
            <?php  $this->renderPartial("//common/header"); ?>
            <div class="col-4">
                <?php
                $model = Order::model()->findByPk($id);
                $year = date('y'); 
                $formattedId = "$id";
                ?>
                <div class="input-block">
                    <b><?php echo $formattedId; ?></b>
                </div>
                <b><?php echo strtoupper($model->customer->customer_name); ?></b><br>
                <?php if ($model->billing_address != '') { ?>
                    <small><?php echo strtoupper(nl2br($model->billing_address)); ?></small><br>
                <?php } ?>
                <?php if ($model->customer->customer_phone_no != '') { ?>
                    <small>Phone : <?php echo strtoupper($model->customer->customer_phone_no); ?></small><br>
                <?php } ?>
                <?php if ($model->customercontact->person_name != '') { ?>
                    <br><small>Kind Attn:</small>
                    <small><b><?php echo strtoupper($model->customercontact->person_name); ?></b></small><br>
                <?php } ?>
            </div>
            <div class="col-4">
            <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;font-size: 14px;">
                <?php echo "SALES ORDER"; ?>
            </div>    
            </div>
            <div class="col-4" style="color:#101010;">
                <table class="table" style="width:100%" style="font-size:10px;">
                    <tr>
                        <?php
                        $modelOrder = Order::model()->findByPk($id);
                        $model_lead = Lead::model()->find(array('condition' => 'file_id="' . $modelOrder->file_id . '"'));
                        ?>
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo $modelOrder->file_id; ?>
                        </td>
                    </tr>
                   
                    <tr>
                        <td>Customer ID</td>
                        <td>
                            <?php echo $modelOrder->customer_id; ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Customer TRN #</td>
                        <td>
                            <?php echo $modelOrder->customer->customer_trn_no; ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Order Date</td>
                        <td>
                            <?php echo date('d/m/Y', STRTOTIME($modelOrder->order_date)); ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Delivery Date</td>
                        <td>
                            <?php echo date('d/m/Y', STRTOTIME($modelOrder->delivery_date)); ?>
                        </td>
                    </tr>
                    <tr>
                        <td>LPO</td>
                        <td>
                            <?php echo $model->lpo; ?>
                        </td>
                    </tr>
                    <tr>
                        <td>Payment Terms</td>
                        <td>
                            <?php echo $modelOrder->paymentTerm->payment_terms; ?>
                        </td>
                    </tr>
                    <tr>
                        <td style="width:40%">Inco Terms</td>
                        <td>
                            <?php echo $modelOrder->incoterm->inco_name; ?>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?php echo $model->leadowner->first_name . ' ' . $model->leadowner->last_name . ' , ' . $model->leadowner->phno; ?>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <br>
        <div class="col-md-12">
            <table id="" class="table" style="font-size:10px;">
                <thead>
                    <tr>
                        <th >No</th>
                        <th class="text-center" >Part No</th>

                        <th class="text-center" >Description</th>
                        <?php
                        $colspqn = "8";
                        if (isset($master_permission_array['inventory_image_show_order'])) {
                            $colspqn = $colspqn + 1;
                            ?>
                            <th >Image</th>
                        <?php } ?>
                        <?php
                        $colspqn = "8";
                        if ($type =='img') {
                            $colspqn = $colspqn + 1;
                            ?>
                            <th >Image</th>
                        <?php } ?>
                        
                        <th  >Qty</th>
                        <?php
                        if ($bonus_quantity == "Y") {
                            $colspqn = $colspqn + 1;
                            ?>
                            <th >Bonus Qty</th>
                        <?php } ?>
                        <th class="text-center">Unit</th>
    <!--                    <th>Sys Rate</th>
                        <th >Disc.</th> -->

                        <th class="text-center">Rate</th>
                        <!--th class="text-center " >Amount</th>
                        < <th class="text-center">Tax(%)</th> -->
                        <!--th class="text-center" style="width:10%" >Tax(%)</th>
                        <th class="text-center bc-tr">Net Amt</th-->
                    <th class="text-center">Total</th>
                    <th class="text-center">Tax Amt</th>
                    <th class="text-center bc-tr">Net Amt</th>
                    </tr>
                </thead>
                <tbody>

                    <?php
                    $m = 1;
                    $condition .= '1=1';
                    $condition .= ' AND t.order_id ="' . $id . '"';
                    $total = 0;

                    $total_cbm = 0;
                    $total_weight = 0;
                    $totaldeduction=0;
                    $modelOrderItem = OrderItem::model()->findAll(array('condition' => $condition, 'order' => 'id ASC'));
                    foreach ($modelOrderItem as $i => $eachItem) {
                        
                         $totaldeduction += $eachItem->deduction_amount;

                        /* if($eachItem->product->inventory_type == '2'){
                          ?>
                          <tr style="background: red;">
                          <td colspan=<?php echo $colspqn; ?>><?php echo $eachItem->description;?></td>
                          </tr>

                          <?php
                          }
                          else */if ($eachItem->product_id == 999999) {//memo
                            ?>
                            
                            <tr style="background-color: #d0f5c5; color: white;">
                                
          
                                <td colspan="<?= $colspqn + 1 ?>"><b><?php echo $eachItem->description; ?></b></td></tr>
                            <?php
                        } else {
                            ?>
                            <tr>
                                <td>
                                    <?php echo $m; ?>
                                </td>
                                
                                <td>
                                    <?php echo $eachItem->product->part_number; ?>
                                </td>

                                <td>
                                    <?php echo nl2br($eachItem->description); ?>
                                </td>
                                <?php if (isset($master_permission_array['inventory_image_show_order'])) { ?>
                                    <td>
                                        <?php if ($eachItem->product->item_image != '') { ?>
                                            <img src="<?php echo Yii::app()->baseUrl . '/uploads/inventoryimages/' . $eachItem->product->item_image; ?>" style="max-width: 100px; height: auto;">
                                        <?php } ?>
                                    </td>
                                <?php } ?>
                                <?php if ($type == 'img') { ?>
                                    <td>
                                        <?php if ($eachItem->product->item_image != '') { ?>
                                            <img src="<?php echo Yii::app()->baseUrl . '/uploads/inventoryimages/' . $eachItem->product->item_image; ?>" style="max-width: 100px; height: auto;">
                                        <?php } ?>
                                    </td>
                                <?php } ?>
                                <td class="text-right"><?php echo (int)$eachItem->quantity; ?></td>

                                <?php if ($bonus_quantity == "Y") { ?>
                                    <td class="text-right">
                                        <td class="text-right"><?php echo (int)$eachItem->bonus_qty; ?></td>
                                    </td>
                                <?php } ?>
                                <td>
                                    <?php echo $eachItem->orderunit->name; ?>
                                </td>
                                       <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency($eachItem->rate, " "); ?>
                                </td>   
                                <td class="text-right">
                                <?php echo Yii::app()->numberFormatter->formatCurrency(($eachItem->rate * $eachItem->quantity), " "); ?>
                                </td>
                                <!--td class="text-right">
                                <?php //echo Yii::app()->numberFormatter->formatCurrency($eachItem->deduction_amount, " "); ?>
                                </td>-->
                                <?php
                                $after_discount = $eachItem->rate - $eachItem->deduction_amount;
                                $total_after_discount = $after_discount * $eachItem->quantity;
                                // $rate=($eachItem->amount+$eachItem->addition_amount)/$eachItem->quantity;
                                ?>
                                <!--td class="text-right">
                                    <?php //echo Yii::app()->numberFormatter->formatCurrency($after_discount, " "); ?>
                                </td->

                                <td>
                                    <?php
                                    
                                    //echo Yii::app()->numberFormatter->formatCurrency($total_after_discount, " ");
                                    ?>
                                </td->
                                 <!-- <td class="text-right"> -->
                                <!--// $eachItem->tax_vat_amount.' ('.$eachItem->tax_vat_percentage.'%)' -->
                                <?php
                                // $eachItem->tax_vat_percentage.'%'
                                ?>
                                <!-- </td> -->
                                <td class="text-right">
                                    <?php
                                    $taxamount = $eachItem->tax_vat_amount;
                                    //echo (int) $eachItem->tax_vat_percentage;
                                     echo Yii::app()->numberFormatter->formatCurrency($taxamount, " ");
                                    //echo $taxamount;
                                    ?>
                                </td>

                                <td class="text-right">
                                    <?php
                                    $net_amount = $eachItem->net_amount + $eachItem->tax_vat_amount;
                                    echo Yii::app()->numberFormatter->formatCurrency($net_amount, " ");
                                    ?>

                                </td>
                            </tr>

                            <?php
                            $m = $m + 1;
                            $total += $eachItem->amount;
                          $net_amount_tot+= $net_amount;
                          $total_after_discount_tot+= $total_after_discount;
                            if ($eachItem->orderunit->name == 'Carton') {

                                $item_cbm = $eachItem->product->cubic_meter;
                                $item_weight = $eachItem->product->weight;
                                $total_cbm += ($eachItem->quantity * $item_cbm);
                                $total_weight += ($eachItem->quantity * $item_weight);
                            }
                        }
                    }
                    ?>
                    <?php if ($modelOrder->total_tax_amount > 0) : ?>
                        <tr>
                            <td class="text-right" colspan=<?php echo $colspqn; ?>><b>Total</b></td>
                            <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                        </tr>
                        
                        <?php if($totaldeduction > 0){?>
    <tr>
        <td class="text-right" colspan="<?php echo $colspqn; ?>"><b>Discount </b></td>
        <!--<td></td>-->
        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($totaldeduction, " "); ?></b></td>

    </tr>
    <tr>
        <td class="text-right" colspan="<?php echo $colspqn; ?>"><b>Total After Discount </b></td>
        <!--<td></td>-->
        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total-$totaldeduction, " "); ?></b></td>

    </tr>
    <?php 
    $total-=$totaldeduction;
    } ?>
                        
                        
                        <tr>
                            <td class="text-right" colspan=<?php echo $colspqn; ?>><b>VAT</b></td>
                            <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($modelOrder->total_tax_amount, " "); ?></b></td>
                        </tr>
                    <?php endif; ?>

                    <?php
                    $Addition = OrderAddition::model()->findAll(array('condition' => 'order_id=' . $modelOrder->id . ' AND calculated_value>0'));
                    $Dedution = OrderDeduction::model()->findAll(array('condition' => 'order_id=' . $modelOrder->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 $colspqn; ?>>
                                    <?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 $colspqn; ?>>
                                    <?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 $colspqn; ?>><b>Grand Total</b></td>
                        <!--<td></td>-->
                        <?php $total += $modelOrder->total_tax_amount; ?>
                        <td class="text-right total bc-br"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total, " "); ?></b></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan=<?php echo $colspqn + 1; ?>>
                            <b>
                                <?php
                                $fcurrency = new MasCurrency();
                                ?>
                                <?php
                                if ($total > 0) {
                                    echo $fcurrency->number_to_currency(number_format(($total), 2, '.', ''), $modelOrder->currency_id);
                                } else {
                                    echo "FOC(Free Of Cost)";
                                }
                                ?>
                            </b>
                        </td>
                    </tr>

                </tbody>
            </table>

        </div>
        <!--
        <div class="box" style="width:100%">
                <h5>Please ensure that all items are quoted in Carton</h5>
                <div class="h5sub-details" style="font-size: 10px;">Total CBM : <b><?php //echo $total_cbm;      ?> </b>CBM</div>
                <div class="h5sub-details" style="font-size: 10px;">Total Weight : <b><?php // echo $total_weight;      ?> </b>KG</div>

        </div>-->

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

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


        <?php $this->renderPartial("//common/footer", array('company_id' => $company_id)); ?>
</div>


    </body>

</html>
