<!DOCTYPE html>
<html>

<head>
    <title>TAX DEBIT NOTE</title>
</head>

        <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:2cm;*/
          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>

<body>
  <?php
$modelCompanySettings = new MasCompanySettings();
$modelCompanySettings = $modelCompanySettings->find(array('condition'=>'crm_company_id='.(int) Yii::app()->user->master_user_company_id));   
if(!empty($modelCompanySettings->print_template_logo)){
    $pdf_logo = $modelCompanySettings->print_template_logo;
}
else{
    $pdf_logo = Yii::app()->user->company_logo;
}


if($modelCompanySettings->print_template_id=='R'){
    $print_template_style="width:30%;float:right";
}
elseif($modelCompanySettings->print_template_id=='M'){
    $print_template_style="width:100%;float:left";
}
else{
    $print_template_style ="width:30%;float:left";
}

?>
        <div style="padding: 35px;">
           <div class="" >
        <htmlpageheader name="firstpage" style="display:none">
</htmlpageheader>

<htmlpageheader name="otherpages" style="display:none">
      <div style="position: fixed; top: -8; left: 0; right: 0; bottom:0;
              border: 7px solid red;
              
              background-size: cover;
              z-index: -100;height:100%;width:98%;"> 

    <?php //$this->renderPartial('//common/header'); ?>
     <div class="header" style="width:100%;padding-bottom:0;display:flex;margin-top:5px;padding-right:5px;">
        <img style="<?php echo $print_template_style;?>;" src="<?php echo  'uploads/logo/'.$pdf_logo; ?>"/>
    </div>
<br>
   </div>
</htmlpageheader>     
    <div class="">
       <?php // $this->renderPartial("//common/header");?>
        <div class="col-6">
            
            <?php  
            
                $modelDebit = Purchasingdebitnote::model()->findByPk($id);
                $invoice_type = $modelDebit->invoice->invoice_type;
                $invoice_no = $modelDebit->invoice->invoice_no;
                $debit_note_no = $modelDebit->debit_note_no;
                $invoice_type_name="";
                    if((int) $invoice_type > 0){
                        $modelInvoiceType = new MasInvoiceType();
                        $arr_invoice_type_details = $modelInvoiceType->findByPk((int) $invoice_type);
                        $inv_code      = $arr_invoice_type_details->inv_code;
                        //$invoice_no=$inv_code.$invoice_no;
                        //$debit_note_no=$inv_code.$debit_note_no;
                        $invoice_type_name = $arr_invoice_type_details->inv_name;
                    }
                $modelPurchasingInvoice = PurchasingInvoice::model()->findByPk($modelDebit->purchasing_invoice_id);

              ?>
               
                <div class="h3" style="margin-top: 0;margin-bottom: 0;">
                    <?php echo 'TAX DEBIT NOTE'; ?>
                </div>
                <div class="input-block">
                    <b><?php echo $debit_note_no;?></b>
                </div>
                <b><?php echo strtoupper($modelDebit->vendor_details->vendor_name); ?></b>
                <br>    
                <?php if($modelDebit->vendor_details->vendor_address != '') {     ?>       
                <small><?php echo strtoupper($modelDebit->vendor_details->vendor_address); ?></small><br>
                <small><?php echo strtoupper('PO BOX : '.$modelDebit->vendor_details->vendor_postal_code.', '.$modelDebit->vendor_details->vendor_location); ?></small><br>
				    <small><?php echo strtoupper($modelDebit->vendor_details->vendor_city.', '.$modelDebit->vendor_details->country->country_name); ?></small><br>					 
					 <?php } ?>                
                <?php if($modelDebit->vendor_details->vendor_phone_no != '') { ?>
                <small>Phone : <?php echo strtoupper($modelDebit->vendor_details->vendor_phone_no); ?></small><br>
                <?php } ?>
                <?php //if($modelDebit->vendorcontact->person_name != '') { ?>
<!--                <br><small>Kind Attn:</small>
                <small><b><?php //echo  strtoupper($modelDebit->vendorcontact->person_name); ?></b></small><br>-->
                <?php //} ?>
        </div>
        <div class="col-5 offset-1" style="color:#101010;">
            
            <table class="table" style="width:100%">
                <tr>
                  
                        <td class="bc-tl">File ID</td>
                        <td>
                            <?php echo $modelDebit->file_id;?>
                        </td>
                </tr>
                <tr>
                    <td>Vendor ID</td>
                    <td>
                        <?php echo $modelDebit->vendor_id;?>
                    </td>
                </tr>
                <tr>
                    <td>REF#</td>
                    <td>
                        <?php echo $id;?>
                    </td>
                </tr>
                <tr>
                    <td>Purchase Return Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelDebit->debited_date));?>
                    </td>
                </tr>
                <?php if($modelDebit->Isdebitnote_or_goodsreturn=='N'){
                    ?>
                <tr>
                    <td>Debit Note Amount</td>
                    <td>
                        <?php echo $modelDebit->debited_amount;?>
                    </td>
                </tr>
                <?php } ?>
                
                <tr>
                    <td>Invoice No</td>
                    <td>
                        <?php echo $invoice_no;?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice Amount</td>
                    <td align="left">
                        <?php echo Yii::app()->numberFormatter->formatCurrency($modelDebit->invoice->grand_total,"");?>
                    </td>
                </tr>
                <tr>
                    <td>Invoice Date</td>
                    <td>
                        <?php echo date('d/m/Y',STRTOTIME($modelDebit->invoice->invoice_date));?>
                    </td>
                </tr>
                <tr>
                    <td>Currency</td>
                    <td>
                        <?php 
                        $modelCurrency = new MasCurrency();
                        $modelCurrency = $modelCurrency->getCurrencyDetails($modelDebit->currency);
                        echo $modelCurrency->currency_code;
                        if($modelCurrency->currency_code!="AED"){
                            echo "/".$modelCurrency->exchange_rate;
                        }
                        ?>
                    </td>
                </tr>
                
                 <tr>
                    <td>Invoice Type</td>
                    <td>
                        <?php echo $invoice_type_name;?>
                    </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" >Description</th>
                    <th class="text-center" >Qty(P)</th>
                    <th class="text-center" >Qty(R)</th>
                    <th class="text-center">Unit</th>
                    <th class="text-center">Rate</th>
                    <th class="text-center bc-tr">Amount</th>
                </tr>
            </thead>
            <?php if($modelDebit->Isdebitnote_or_goodsreturn=='Y'){
                ?>
            <tbody>
                
                    <?php  $modelDebitItems = PurchasingdebitnoteItem::model()->findAll(array('condition'=>'purchasing_debitnote_id = '.$id.' '));
                        $total = $total_vat =0;
                        foreach ( $modelDebitItems as $i =>  $modelDebitItems) {     ?>
             
                
                <tr>
                        <td>
                            <?php echo $i+1;?>
                        </td>
                        <td>
                            <?php echo $modelDebitItems->product->part_number;?>
                        </td>
                        <td>
                            <?php echo $modelDebitItems->description;?>
                        </td>

                        <td class="text-right"><?php echo number_format($modelDebitItems->purchased_quantity); ?></td>
                        <td class="text-right"><?php echo number_format($modelDebitItems->returned_quantity); ?></td>
                        
                        <td>
                            <?php echo $modelDebitItems->product->unit->symbol;?>
                        </td>
                       
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelDebitItems->rate," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($modelDebitItems->net_amount," ");?>
                        </td>
                        <?php $total+=$modelDebitItems->net_amount; 
                        $total_vat+=$modelDebitItems->tax_vat_amount;?>
                </tr>
                <?php } ?>
                <tr>
                        <td class="text-right" colspan="7"><b>Net Total</b></td>
                        <td class="text-right"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total," ");?></b></td>
                </tr>
                
                <?php
                    $condition='purchasing_debit_note_id="'.$modelDebit->id.'" AND calculated_value>0';
                    $Addition=PurchasingDebitNoteAddition::model()->findAll(array('condition'=>$condition));
                    $Dedution=PurchasingDebitNoteDeduction::model()->findAll(array('condition'=>$condition));
                    if(!empty($Addition))
                    {
                        foreach ($Addition as $eachAddition)
                        {
                      ?>
                    <tr>
                        <?
                        $total+=$eachAddition->calculated_value;
         ?>
                            <td class="text-right" colspan="7">
                                <?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="7">
                                    <?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="7"><b>VAT</b></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 <?php echo $modelCurrency->currency_code;?>  <?php 
                                    $model_currency=MasCurrency::model()->findByPk($modelDebit->currency);
                                    $vat_base=$modelCurrency->exchange_rate*$total_vat;
                                echo "(".Yii::app()->numberFormatter->formatCurrency($vat_base," ")."), "; ?> <?php
                              
                                echo ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval(($vat_base))))->result) . ' only';  ?></b></td>
                    </tr-->
                    <?php 
                    $grand_total=$total + $modelDebit->total_tax_amount;
                    ?>
                    <tr>
                        <td class="text-right" colspan="7"><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="8"><b>
                                    <?php if($grand_total > 0){
                                        $Mascurr=new MasCurrency();
                                         echo $Mascurr->number_to_currency(number_format(($grand_total), 2, '.', ''), $modelDebit->currency);
                                    //echo $modelCurrency->currency . " " . ucfirst($this->widget('ext.NumtoWord.NumtoWord', array('num' => floatval($grand_total)))->result) . ' only';
                                }
                                else{
                                    echo "FOC(Free Of Cost)";
                                } ?></b></td>
                            </tr>
            </tbody>
            <?php 
            }
            else
            {
                ?>
                 <tr>
                        <td class="text-center" colspan="7">
                            No item returned
                        </td>
                </tr>        
                <?php                 
                
            }
            ?>
            
        </table>
   
    </div>
          <?php if($modelDebit->terms_and_conditions!='')
       {
       ?>
    <div class="box" style="width: auto">
            <h5>Terms And Conditions</h5>
            <div class="h5sub-details">
                <?php echo nl2br($modelDebit->terms_and_conditions);?>
            </div>
        </div>
        <?php
       }
      ?>
        <?php if($modelDebit->reason!='')
       {
       ?>
    <div class="box" style="width: auto">
            <h5>Reason</h5>
            <div class="h5sub-details">
                <?php echo nl2br($modelDebit->reason); ?>
            </div>
        </div>
        <?php
       }
      ?>
            <?php if($modelDebit->remarks!='')
       {
       ?>
              <div class="box" style="width: auto">
                <h5>Remarks</h5>
                <div class="h5sub-details">
                    <?php echo  nl2br($modelDebit->remarks); ?>
                </div>
            </div>
            <?php } ?>
             <?php if($modelDebit->notes!='')
       {
       ?>
              <div class="box" style="width: auto">
                <h5>Notes</h5>
                <div class="h5sub-details">
                    <?php echo nl2br($modelDebit->notes); ?>
                </div>
            </div>
            <?php } ?>
  
   <!--  <br/>
    <br/>
    <br/>
    <br/> -->
<!--     <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> -->
<?php $this->renderPartial("//common/footer");?>
  </div>
</body>

</html>
