<?php 
$model_settings         = new MasCompanySettings();
$crm_login_company_id   = Yii::app()->user->master_user_company_id;        
$model_settings         = $model_settings->find(array('condition'=>'crm_company_id='.$crm_login_company_id));  
?><!DOCTYPE html>
<html>

<head>
    <title>Sales Return</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;

        }

        </style>

<body>

            <div style="padding: 35px;">
           <div class="" >
      
    <div class="">
    <?php  $this->renderPartial("//common/header");?>
        <div class="col-4">
            <?php  
            
                $modelCredit = Creditnote::model()->findByPk($id);
                

              ?>
                <div class="input-block">
                    <b><?php echo  $modelCredit->creditnote_no;?></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 } ?>
        </div>
        <div class="col-4">
        <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;">
        <?php echo "SALES RETURN"; ?>
        </div>
        </div>
        <div class="col-4" style="color:#101010;">
            <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>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 Yii::app()->numberFormatter->formatCurrency($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>
                 <tr>
                    <td>Sales Man</td>
                    <td>
                        <?php 
                            echo $modelCredit->createdby->fullname;
                        ?>
                    </td>
                </tr>
                 <!-- <tr>
                    <td>Branch</td>
                    <td>
                         <?php 
                          //  echo $modelCredit->warehouse->warehouse_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" style="width:15%">Item</th>
                    <th class="text-center" style="width:35%">Description</th>
                    <th class="text-center">Qty(P)</th>
                    <?php 
                    $col=6;
                    if($bonus_quantity=="Y"){
                        $col=$col+2;
                        ?>
                     <th class="text-center">Bonus Qty(P)</th>                     
                    <?php } ?>
                    <th class="text-center">Qty(R)</th>
                    <?php if($bonus_quantity=="Y"){ ?>
                     <th class="text-center">Bonus Qty(R)</th>                     
                    <?php } ?>
                    <th class="text-center">Unit</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 or returned_bonus_qty>0)'));
                        $total = $total_vat = 0;
                        foreach ( $modelCreditItems as $i =>  $modelCreditItems) {  
                            $batch_no='';$sl_no='';?>
                <tr>
                        <td>
                            <?php echo $i+1;?>
                        </td>
                        <td>
                            <?php echo $modelCreditItems->product->part_number;?>
                        </td>
                        <td>
                            <?php echo $modelCreditItems->description;?>
                            <?php if($modelCreditItems->product->have_expiry_date=='Y'){
                                $batch_no=StockLedger::getBatchNo($id,$modelCreditItems->product_id,'CREDIT_NOTE');
                                ?>
                            <br/>
                             Batch No:<?php echo $batch_no;?>
                            <?php } ?>
                            <?php if($modelCreditItems->product->have_serial_no=='Y'){
                                $sl_no=StockLedger::getSerialNo($id,$modelCreditItems->product_id,'CREDIT_NOTE');
                                ?>
                            <br/>
                             Serial No:<?php echo $sl_no;?>
                            <?php } ?>
                        </td>

                        <td class="text-right"><?php echo number_format($modelCreditItems->quantity); ?></td>
                        <?php if($bonus_quantity=="Y"){ ?>
                            <!-- <td class="text-right">
                                <?php //echo number_format($modelCreditItems->bonus_qty," ");?>
                            </td> -->
                            <td class="text-right"><?php echo number_format($modelCreditItems->bonus_qty); ?></td>
                        <?php } ?>
                         <!-- <td class="text-right">
                            <?php //echo number_format($modelCreditItems->returned_quantity," ");?>
                        </td> -->
                            <td class="text-right"><?php echo number_format($modelCreditItems->returned_quantity); ?></td>
                        <?php if($bonus_quantity=="Y"){ ?>
                            <!-- <td class="text-right">
                                <?php //echo number_format($modelCreditItems->returned_bonus_qty," ");?>
                            </td> -->
                            <td class="text-right"><?php echo number_format($modelCreditItems->returned_bonus_qty); ?></td>
                        <?php } ?>
                        <td>
                            <?php echo $modelCreditItems->product->unit->symbol;?>
                        </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="<?php echo $col; ?>"><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>
                        <?php
                        $total+=$eachAddition->calculated_value;  ?>
                            <td class="text-right" colspan="<?php echo $col; ?>">
                                <?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 $col; ?>">
                                    <?php echo MasFooterItems::getFooterItemName($eachDed->particular); ?>
                                </td>
                                <td class="text-right">
                                    <?php echo Yii::app()->numberFormatter->formatCurrency($eachDed->calculated_value," ");?>
                                </td>
                        </tr>
                        <?php
                    }
                    }
                    ?>
                    <?php $grand_total=$total + $total_vat; ?>
                    <tr>
                        <td class="text-right" colspan="<?php echo $col; ?>"><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="<?php echo $col; ?>"><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="<?php echo $col+1; ?>"><b>
                                    <?php if($grand_total > 0){
                                         $modelCurrency = new MasCurrency();
                                        echo $modelCurrency->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->terms_and_conditions!='')
       {
       ?>
            <div class="box" style="width: 100%">
                <h5>Terms And Conditions</h5>
                <div class="h5sub-details">
                    <?php echo  nl2br($modelCredit->terms_and_conditions); ?>
                </div>
            </div>
            <?php } ?>
           <?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 } ?>

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

</html>
