<!DOCTYPE html>
<html>

<head>
    <title>TAX CREDIT NOTE</title>
</head>

<body>

    <div class="">
        <div class="col-6">
            <br>
            <?php  
            
            
                $modelCredit = Creditnote::model()->findByPk($id);
              ?>
               
                <div class="h3" style="margin-top: 0;margin-bottom: 0;">
                    <?php echo 'TAX CREDIT NOTE'; ?>
                </div>
                <div class="input-block">
                    <b><?php echo $id;?></b>
                </div>
                <b><?php echo strtoupper($modelCredit->customer->customer_name); ?></b>
                <br>    
                <?php if($modelCredit->customer->customer_address != '') {     ?>       
                <small><?php echo strtoupper($modelCredit->customer->customer_address); ?></small><br>
                <small><?php echo strtoupper('PO BOX : '.$modelCredit->customer->customer_postal_code.', '.$modelCredit->customer->customer_location); ?></small><br>
				    <small><?php echo strtoupper($modelCredit->customer->customer_city.', '.$modelCredit->customer->country->country_name); ?></small><br>					 
					 <?php } ?>                
                <?php if($modelCredit->customer->customer_phone_no != '') { ?>
                <small>Phone : <?php echo strtoupper($modelCredit->customer->customer_phone_no); ?></small><br>
                <?php } ?>
                 <br><br><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">
                    <b>Our TRN #<?php echo $settings->trn_no;?></b>
                </div>
                <?php } ?>
        </div>
        <div class="col-5 offset-1" style="color:#101010;">
            <div class="text-right">
                <img src="<?php echo 'images/logo/'.Yii::app()->user->pdf_company_logo; ?>" class="logo">
            </div>
            <br>
            <table class="table" style="width:100%">
                <!-- <tr>
                  
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo $modelCredit->file_id;?>
                        </td>
                </tr>
                <tr>
                    <td>Customer ID</td>
                    <td>
                        <?php echo $modelCredit->customer_id;?>
                    </td>
                </tr> -->
                <tr>
                    <td>Customer TRN #</td>
                    <td>
                        <?php echo $modelCredit->customer->customer_trn_no;?>
                    </td>
                </tr>
                <tr>
                    <td>Credit Note Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelCredit->credited_date));?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice No</td>
                    <td>
                    <?php echo $modelCredit->invoice_id;?>
                    </td>
                </tr>
                
                <tr>
                    <td>Invoice Amount</td>
                    <td align="right">
                        <?php echo $modelCredit->invoice->grand_total;?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelCredit->invoice->invoice_date));?>
                    </td>
                </tr>
                <tr>
                    <td>Currency</td>
                    <td>
                        <?php 
                        $modelCurrency = new MasCurrency();
                        $modelCurrency = $modelCurrency->getCurrencyDetails($modelCredit->currency);
                        echo $modelCurrency->currency_code;
                        if($modelCurrency->currency_code!="AED"){
                            echo "/".$modelQ->exchange_rate;
                        }
                        ?>
                    </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">Item</th>
                    <th class="text-center" style="width:30%">Description</th>
                    <th class="text-center" style="width:4%">Qty(P)</th>
                    <th class="text-center" style="width:4%">Qty(R)</th>
                    <th class="text-center">Unit</th>
                    <th class="text-center" style="width:8%">Rate</th>
                    <th class="text-center">Tax(%)</th>
                    <th class="text-center">Tax Amount</th>
                    <th class="text-center bc-tr">Amount</th>
                </tr>
            </thead>
            <tbody>
                
                    <?php  $modelCreditItems = CreditnoteItem::model()->findAll(array('condition'=>'creditnote_id = '.$id.' AND returned_quantity>0'));
                        $total = $total_vat = 0;
                        foreach ( $modelCreditItems as $i =>  $modelCreditItems) {     ?>
                        
                
                <tr>
                        <td>
                            <?php echo $i+1;?>
                        </td>
                        <td>
                            <?php echo $modelCreditItems->product->part_number;?>
                        </td>
                        <td>
                            <?php echo $modelCreditItems->description;?>
                        </td>

                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelCreditItems->quantity," ");?>
                        </td>
                         <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelCreditItems->returned_quantity," ");?>
                        </td>
                        <td>
                            <?php echo $modelCreditItems->product->unit->symbol;?>
                        </td>
                        <td class="text-right">
                            <?php echo $modelCreditItems->rate;?>
                        </td>
                       <td>
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelCreditItems->tax_vat_percentage," ");?>
                        </td>
                       <td>
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelCreditItems->tax_vat_amount," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelCreditItems->net_amount," ");?>
                        </td>
                        <?php $total+=$modelCreditItems->net_amount; 
                        $total_vat+=$modelCreditItems->tax_vat_amount;
                        ?>
                </tr>
                <?php } ?>
                  <tr>
                        <td class="text-right" colspan="9"><b>Net Total  </b></td>
                        <!--<td></td>-->
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total," ");?></b></td>
                </tr>
                   <?php
                    $condition='creditnote_id="'.$modelCredit->id.'" AND calculated_value>0';
                    $Addition=CreditnoteAddition::model()->findAll(array('condition'=>$condition));
                    $Dedution=CreditnoteDeduction::model()->findAll(array('condition'=>$condition));
                    if(!empty($Addition))
                    {
                        foreach ($Addition as $eachAddition)
                        {
                      ?>
                    <tr>
                        <?
                        $total+=$eachAddition->calculated_value;
         ?>
                            <td class="text-right" colspan="9">
                                <?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="9">
                                    <?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" colspan="9"><b>VAT Return Amount</b></td>
                            <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total_vat," ");?></b></td>
                    </tr>
                   <tr>
                                <td class="text-right" colspan="10"><b>VAT Amount in <?php echo $modelCurrency->currency_code;?>  <?php 
                                    $model_currency=MasCurrency::model()->findByPk($modelCredit->currency);
                                    $vat_base=$modelCurrency->exchange_rate*$total_vat;
                                echo "(".Yii::app()->numberFormatter->formatCurrency($vat_base," ")."), "; ?> <?php
                              echo MasCurrency::number_to_currency(number_format(($vat_base), 2, '.', ''), $modelCredit->currency);
                                //echo ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval(($vat_base))))->result) . ' only';  ?></b></td>
                            </tr>


                    <?php 
                    $grand_total=$total + $total_vat;
                    ?>
                    <tr>
                        <td class="text-right" colspan="9"><b>Grand Total</b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($grand_total," ");?></b></td>
                    </tr>
                    
                    <tr>
                                <td class="text-right" colspan="10"><b>
                                    <?php if($grand_total > 0){
                                        
                                    echo MasCurrency::number_to_currency(number_format(($grand_total), 2, '.', ''), $modelCredit->currency);
                                    //echo $modelCurrency->currency_code . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($grand_total)))->result) . ' only';
                                }
                                else{
                                    echo "FOC(Free Of Cost)";
                                } ?></b></td>
                            </tr>
       
            </tbody>
        </table>
   
 
    </div>
           <?php if($modelCredit->reason!='')
       {
       ?>
        <div class="box" style="width: 100%">
            <h5>Reason</h5>
            <div class="h5sub-details">
                <?php echo nl2br($modelCredit->reason); ?>
            </div>
        </div>
        <?php
       }
      ?>
            <?php if($modelCredit->remarks!='')
       {
       ?>
            <div class="box" style="width: 100%">
                <h5>Remarks</h5>
                <div class="h5sub-details">
                    <?php echo  nl2br($modelCredit->remarks); ?>
                </div>
            </div>
            <?php } ?>
             <?php if($modelCredit->notes!='')
       {
       ?>
            <div class="box" style="width: 100%">
                <h5>Notes </h5>
                <div class="h5sub-details">
                    <?php echo  nl2br($modelCredit->notes); ?>
                </div>
            </div>
            <?php } ?>

    <!-- <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> -->
    <!--  <h3></h3>
                <p><?php // echo $model_Company->company_address; ?>
                <?php // echo ', '.$model_Company->company_location; ?>
                <?php // echo ', '.$model_Company->country->country_name; ?></p> -->
    <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>
