<!DOCTYPE html>
<html>

<head>
    <title>Invoice List</title>

</head>

<body>
    <div class="">
        <div class="col-6">
            <br>
            <?php
                         $modelQ1 = Invoice::model()->findByPk($id);
                         $modelDelivery = new DeliveryNote();
                         if($modelQ1->delivery_ids!=""){
                            $modelDelivery = DeliveryNote::model()->findByPk($modelQ1->delivery_ids);
                         }
//                   $model_cust = Customers::model()->findByPk($modelQ1->cust_id);
?>
                <!--<span style="color:#1e62a3;font-size: 18px;margin-bottom: 25px"><b><?php// echo $title." # ".$model->id;?></b></span><br>-->
                <div class="h3" style="margin-top: 0;margin-bottom: 0;">
                    <?php echo INVOICE ; ?>
                </div>
                <div class="input-block">
                <b><?php echo  $modelQ1->invoice_no; ?></b>
                </div>
               <b><?php echo strtoupper($modelQ1->customer->customer_name); ?></b><br>
                 <?php if($modelQ1->billing_address != '') {     ?>       
                <small><?php echo strtoupper(nl2br($modelQ1->billing_address)); ?></small><br>
                    <?php } ?>      
                <?php if($modelQ1->customer->customer_phone_no !='') { ?>        
                <small>Phone : <?php echo strtoupper($modelQ1->customer->customer_phone_no); ?></small><br>
                <?php } ?>
                <?php if($modelQ1->contact1->person_name != '') { ?>
                <br><small>Kind Attn:</small>
                <small><b><?php echo  strtoupper($modelQ1->contact1->person_name); ?></b></small><br>
                <?php } ?>
        </div>
        <div class="col-5 offset-1" style="color:#101010;">
            <div class="text-right">
                <!--img src="<?php //echo Yii::app()->baseUrl . '/images/logo/'.Yii::app()->user->pdf_company_logo; ?>" class="logo"-->
                <img src="<?php echo 'images/logo/'.Yii::app()->user->pdf_company_logo; ?>" class="logo">
            </div>
            <br>
            <table class="table" style="width:100%">
                <tr>
                    <?php
            $modelQ = Invoice::model()->findByPk($id);
            $model_lead=Lead::model()->find(array('condition'=>'file_id="'.$modelQ->file_id.'"'));
            ?>
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo $modelQ->file_id;?>
                        </td>
                </tr>
                <tr>
                    <td>Customer ID</td>
                    <td>
                        <?php echo $modelQ->company_id;?>
                    </td>
                </tr>
                  <?php if($modelQ1->customer->customer_trn_no !='') { ?>     
                 <tr>
                    <td>Customer TRN#</td>
                    <td>
                        <?php echo $modelQ1->customer->customer_trn_no;?>
                    </td>
                </tr>
                 <?php } ?>
                <?php if($modelQ1->customer->customer_shj_imp_code !='') { ?>     
                <tr>
                    <td>Customer Shj. Imp. Code</td>
                    <td>
                        <?php echo $modelQ1->customer->customer_shj_imp_code;?>
                    </td>
                </tr>
                 <?php } ?>
                <?php 
                 $delivery = new DeliveryNote();
                if($modelQ->delivery_ids!=""){
                $delivery = DeliveryNote::model()->findAll(array('condition' => 'id IN ('.$modelQ->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(',', $modelQ->delivery_ids));?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelQ->invoice_date));?>
                    </td>
                </tr>
                <tr>
                    <td>Currency</td>
                    <td>
                        <?php echo "AED";?>
                    </td>
                </tr>
                <?php
                if(isset($model_lead->leadowner->first_name) && $model_lead->leadowner->first_name!="") {
                    ?>
                <tr>
                    <td colspan="2">
                        <?php echo $model_lead->leadowner->first_name.' '.$model_lead->leadowner->last_name.' , '.$model_lead->leadowner->phno;?>
                    </td>
                </tr>
                <?php 
                }
                ?>
<!--                 <tr>
                    <td>Dly Date</td>
                    <td>
                        <?php //echo date('d/m/Y',STRTOTIME($modelDelivery->delivery_date));?>
                    </td>
                </tr>-->
                 <tr>
                    <td>LPO</td>
                    <td>
                        <?php echo $modelQ->order_details->lpo;?>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <br>
    <div class="col-md-12">
        <table id="" class="table">
            <thead>
                <tr>
                    <th class="text-center bc-tl">No</th>
                    <th class="text-center">Part No</th>
                    <th class="text-center" style="width:30%">Description</th>
                    <!--<th class="text-center">Brand</th>-->
                    <th class="text-center" style="width:4%">Qty</th>
                    <th class="text-center">Unit</th>
                    <th class="text-center">VAT</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';
                $condition .= ' AND t.invoice_id ="' . $id.'"';
                $total=$total_vat=0;
                $model_Quotes = InvoiceItem::model()->findAll(array('condition'=>$condition));
                foreach ( $model_Quotes as $i =>  $model_Quotes) {
                    
                    if($model_Quotes->product->inventory_type == '2'){
                        ?>
                        <tr style="background: red;">
                            <td colspan="7"><?php echo $model_Quotes->description;?></td>
                        </tr>
                
                        <?php
                    }                    
                    else
                    { 
                  //  if($model_Quotes->checked==1)
                  //  {
                ?>
                 <tr>
                        <td>
                            <?php echo $m;?>
                        </td>
                        <td>
                            <?php echo $model_Quotes->product->part_number;?>
                        </td>
                        <td>
                            <?php echo nl2br($model_Quotes->description);?>
                        </td>
<!--                        <td>
                            <?php // echo $model_Quotes->product->brand_detail->brand_class_name;?>
                        </td>-->
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model_Quotes->quantity," ");?>
                        </td>
                        <td>
                            <?php echo $model_Quotes->invoiceunit->name;?>
                        </td>
                         <td>
                            <?php echo $model_Quotes->tax_vat_amount.' ('.$model_Quotes->tax_vat_percentage.'%)';?>
                        </td>
                        <?php $rate=$model_Quotes->net_amount/$model_Quotes->quantity;?>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($rate," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model_Quotes->net_amount," ");?>
                        </td>
                </tr>
                
                    <?php
                        $m=$m+1;
                        $total+=$model_Quotes->net_amount;
                        $total_vat+=$model_Quotes->tax_vat_amount;
       // }
        } 
        }
        ?>
                    <tr>
                        <td class="text-right" colspan="7"><b>Net Total  </b></td>
                        <!--<td></td>-->
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total," ");?></b></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="7"><b>VAT </b></td>
                        <!--<td></td>-->
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total_vat," ");?></b></td>
                    </tr>
                    <tr>
                        <td class="text-right" colspan="8"><b>VAT Amount in AED  <?php 
                            $model_currency=MasCurrency::model()->findByPk($modelQ->currencyname->id);
                            $vat_base=$model_currency->exchange_rate*$total_vat;
                        echo Yii::app()->numberFormatter->formatCurrency($vat_base," "); ?>; <?php
                        echo MasCurrency::number_to_currency(number_format(($vat_base), 2, '.', ''), $modelQ1->currency);
                        //echo  "AED " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval(($vat_base))))->result) . ' only';  ?></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="7">
                                <?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="7">
                                    <?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="7"><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>
                                <td class="text-right" colspan="8"><b><?php 
                                if($total > 0){
                                    echo MasCurrency::number_to_currency(number_format(($total+$total_vat), 2, '.', ''), $modelQ1->currency);
                                    //echo $modelQ->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 
         /*echo '<pre>';
         print_r($modelInvoice);*/
         
         if($modelInvoice->payment_terms!=0)
       {
       ?>
            <h5><u>Payment Terms</u></h5>
            <div class="h5sub-details">
                <?php echo nl2br ($modelInvoice->paymentTerm->payment_terms); ?>
            </div>
        <?php } ?>
        <?php if($modelInvoice->terms_and_conditions!='')
       {
       ?>
            <h5><u>Terms & Conditions</u></h5>
            <div class="h5sub-details">
                <?php echo nl2br($modelInvoice->terms_and_conditions); ?>
            </div>
        <?php
       }
      ?>
            <?php if($modelInvoice->notes!='')
       {
       ?>

                <h5><u>Notes</u></h5>
                <div class="h5sub-details">
                    <?php echo  nl2br ($modelInvoice->notes); ?>
                </div>
            <?php } ?>
    
       <?php $model =  OwnerBankDetails::model()->find(array('condition' => 'currency_id='.$modelInvoice->currency)); 
          if(empty($model))
              $model =  OwnerBankDetails::model()->find(array('condition' => 'currency_id=23'));   
          
          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
$mas_company = new MasCompany();
$mas_company = $mas_company->findByPK(Yii::app()->user->master_user_company_id);
?>
          
<div class="box" style="width:100%">
<div style="position:relative">
    <div style="width: 100%; margin-top: 2px;"> 
        <h5><?php  echo $mas_company->company_name; ?></h5>
    </div><br/>
    <div style="width: 20%;height: 40px;"></div>    
</div>
</div>          
   


<div class="box" style="width:100%">
<div style="position:relative">
   <br/>

    <div style="float:left; width: 40%;text-align: center;float: right;">
         ___________________________________________ <br>
         <span style="text-align: center;">Prepared By / Signature</span>
    </div>
    <div style="width: 20%;"></div>
    
</div>
</div>



<!--<div class="box" style="width:100%">
<div style="position:relative">
    <div style="width: 100%; margin-top: 2px;"> <h5>Cheques to be drawn in favour of</h5></div><br>

    <div style="float:left; width: 40%;text-align: center;">
         ___________________________________________ <br>
         <span style="text-align: center;">Prepared By / Signature</span>
    </div>
    <div style="width: 20%;"></div>
    <div style="width: 40%;float: right;text-align: center;"> 
                 ___________________________________________  <br>

        <span style="text-align: center;">Sales invoice checked & Received By / Signature</span>
    </div>
</div>
</div>-->
 
       <htmlpagefooter name="myfooter">


    <div style="text-align:center;width:100%;font-size:10px"><?= Yii::app()->user->footer_autogenerated; ?>
        <?php
                  date_default_timezone_set("Asia/Dubai");
                 echo date('d/m/Y')." ".date("h:i:sa"); ?>
    </div>

    
        <div style="border-top: 1px dotted #000000; font-size: 12px; text-align: center; padding-top: 10px; background-color: #d7faeb;">
             <div style="text-align:center;width:100%">
                <b><?= Yii::app()->user->footer_line_1 ?></b><br>
                <?= Yii::app()->user->footer_line_2 ?>
            </div>
            <div style="text-align:center;width:100%">Page {PAGENO} of {nb} </div>
        </div>
    </htmlpagefooter>
</body>

</html>
