<!DOCTYPE html>
<html>

<head>
    <title>Purchase 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;   
            } */
            
                             div {
         page-break-inside: auto;
            page-break-after: auto;

        }
 
        </style>
</head>

<body>

        <div style="padding: 35px;">
           <div class="" >
     
    <div class="">
     <?php  $this->renderPartial("//common/header");?>
        <div class="col-4">
            <br>
            <?php
                         $modelInvoice = PurchasingInvoice::model()->findByPk($id);
//                   $model_cust = Customers::model()->findByPk($modelInvoice->cust_id);
?>
                <!--<span style="color:#1e62a3;font-size: 18px;margin-bottom: 25px"><b><?php // echo $title." # ".$model->id;?></b></span><br>-->
                
                <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>
                
                <?php 
                  if(isset($modelInvoice->vendor_details->vendor_postal_code) && $modelInvoice->vendor_details->vendor_postal_code!=""){
                      ?>
                      <small><?php echo 'PO Box : '.$modelInvoice->vendor_details->vendor_postal_code.", ";?></small>
                      <?php
                  }
                if(!empty($modelInvoice->vendor_details->vendor_city)){?>
                   <small><?php echo  $modelInvoice->vendor_details->vendor_city.', '?></small> 
                   <?php
                }
                ?>
                <small><?php echo  $modelInvoice->vendor_details->country->country_name; ?></small><br>
                <small><?php echo  $modelInvoice->vendor_details->vendor_phone_no; ?></small><br>
                
                
        </div>
        <div class="col-4">
        <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
        <?php echo "PURCHASE INVOICE"; ?>
        </div>    
        </div>
        <div class="col-4" style="color:#101010;">
            <table class="table" style="width:100%;font-size:10px;">
                <tr>
                        <td style="width:40%;">File ID</td>
                        <td>
                            <?php echo $modelInvoice->file_id;?>
                        </td>
                </tr>
                <tr>
                        <td style="width:40%;">PO #</td>
                        <td>
                            <?php echo $modelInvoice->po_id;?>
                        </td>
                </tr>
                <tr>
                        <td style="width:40%;">MR #</td>
                        <td>
                            <?php echo $modelInvoice->materialreceived;?>
                        </td>
                </tr>
<!--                <tr>
                    <td>Vendor ID</td>
                    <td>
                        <?php //echo $modelInvoice->vendor_id;?>
                    </td>
                </tr>
                
                    <tr>
                        <td>Vendor TRN#</td>
                        <td><?php //echo $modelInvoice->vendor_details->vendor_trn_no;?></td>
                    </tr>-->
                <tr>
                    <td style="width:40%;">Invoice No</td>
                    <td>
                        <?php echo $modelInvoice->invoice_no;?>
                    </td>
                </tr>
                <tr>
                    <td style="width:40%;">Invoice Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelInvoice->invoice_date));?>
                    </td>
                </tr>
                <tr>
                    <td style="width:40%;">Currency</td>
                    <td>
                        <?php echo $modelInvoice->currencyname->currency_code;?>
                    </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 >Part No</th>
                    <th >Description</th>
                    <!--<th class="text-center">Brand</th>-->
                    <th >Qty</th>
                    <th >Unit</th>
                    <th >Rate</th>
                    <th >Tax %</th>
                    <th >Tax Amount</th>
                    <th >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 = PurchasingInvoiceItem::model()->findAll(array('condition'=>$condition));
                foreach ( $modelInvoiceItem as $i =>  $modelInvoiceItem) {
                    
//                    if($modelInvoiceItem->product->inventory_item_type == '2'){
//                        ?>
<!--                        <tr style="background: red;">
                            <td colspan="8">//<?php echo $modelInvoiceItem->description;?></td>
                        </tr>-->
                
                        //<?php
//                    }                    
//                    else
//                    { 
                  //  if($modelInvoiceItem->checked==1)
                  //  {
                ?>
                 <tr>
                        <td>
                            <?php echo $m;?>
                        </td>
                        <td>
                            <?php echo $modelInvoiceItem->product->part_number;?>
                        </td>
                        <td>
                            <?php echo $modelInvoiceItem->description;?>
                            <?php if($modelInvoiceItem->product->have_expiry_date=='Y'){
                                    $batch_no=StockLedger::getBatchNoWithExpDate($modelInvoiceItem->purchasinginvoice->materialreceived,$modelInvoiceItem->product_id,'PURCHASE');
                                ?>
							<hr style="width:100%;">
                            <br/>
                             Batch No:<?php echo $batch_no;?>
                            <?php } ?>
                            <?php if($modelInvoiceItem->product->have_serial_no=='Y'){
                                $sl_no=StockLedger::getSerialNo($modelInvoiceItem->purchasinginvoice->materialreceived,$modelInvoiceItem->product_id,'PURCHASE');
                                ?>
							<hr style="width:100%;">
                            <br/>
                             Serial No:<?php echo $sl_no;?>
                            <?php } ?>
                        </td>
                        <td class="text-right"><?php echo number_format($modelInvoiceItem->quantity); ?></td>
                        <td>
                            <?php echo $modelInvoiceItem->unitdetails->name;  //.'('.$modelInvoiceItem->unitdetails->factor.')'?>  
                        </td>
                        <?php $rate=$modelInvoiceItem->rate;?>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($rate," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo $modelInvoiceItem->tax_percentage;?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelInvoiceItem->tax_amount," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelInvoiceItem->net_amount," ");?>
                        </td>
                </tr>
                
                    <?php
                        $m=$m+1;
                        $total+=$modelInvoiceItem->net_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=PurchasingInvoiceAddition::model()->findAll(array('condition'=>'purchasing_invoice_id='.$id.' AND calculated_value>0'));
                    $Dedution=PurchasingInvoiceDeduction::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
      $fcurrency=new MasCurrency();
   ?>
                                <?php 
                                echo $fcurrency->number_to_currency(number_format(($total), 2, '.', ''), $modelInvoice->currency);
                                //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){
        ?>
        <br>
        <div class="box" style="width: 100%">
            <!--        <small>**VAT Charges as will be applicable</small><br><br>-->
            <h5>Payment Terms</h5>
            <div class="h5sub-details">
               <?php echo nl2br($modelInvoice->paymentTerm->payment_terms); ?>
            </div>
        </div>   <br>
    <?php } ?>
      
        <?php  if($modelInvoice->terms_and_conditions!='')
            {
            ?>
              <div class="box" style="width: 100%">
         
            <h5>Terms & Conditions</h5>
            <div class="h5sub-details">
              <?php echo $modelInvoice->terms_and_conditions; ?>
            </div>
        </div>   
        <br>
             <?php
            }
        ?>
            <?php if($modelInvoice->notes!='')
            {
            ?>
            <table style="width:100%; page-break-inside:avoid;font-size:10px;" class="table_box" >
           <tr>
                <td><h4>Notes</h4></td>
            </tr>
            <tr style="position:relative">
                <td><?php echo nl2br($modelInvoice->notes); ?></td>
			</tr>
			
        </table>   
        <br>
            <?php } ?>
    </div>
    <br/>


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

</html>
