<style type="text/css">

@media print {
  @page {
    margin-top: 4.2cm;
    margin-bottom: 4.4cm;
  }
}
</style>
<!DOCTYPE html>
<html>
    <?php 

        $crm_login_company_id       = Yii::app()->user->master_user_company_id;
        $privillage_modal           = new MasCompanySettings();
        $privillage_condition       = 'crm_company_id='.$crm_login_company_id;
        $privillage_count           = $privillage_modal->count(array('condition'=>$privillage_condition));
        $privillage_settings_module = $privillage_modal->find(array('condition'=>$privillage_condition));
        $master_permission        = $privillage_settings_module->permissions;
        $master_permission_array  = json_decode($master_permission, true);
        
        $bonus_quantity             = $privillage_settings_module->bonus_quantity;
    ?>

<head>
    <title>Quotes</title>

</head>

<body>  
    <!--mpdf
<htmlpageheader name="myHeader">
  <div class="">
    <img src="<?php echo  'images/logo/'.Yii::app()->user->pdf_company_logo; ?>"/> 
  </div>
</htmlpageheader>
<sethtmlpageheader name="myHeader" value="on" />        mpdf-->
    <div class="main">
        <div class="col-4">
            <?php
                $modelQuotation = Quotes::model()->findByPk($id);
               
            ?>
                <div class="input-block">
                    <b> QTN # : <?php echo $id;?></b>
                </div>
                <b><?php echo strtoupper($modelQuotation->customer->customer_name); ?></b>
               
        </div>
            <div class="col-4">
                <div class="h3" style="margin-top: 0;margin-bottom: 0;text-align: center;color: #0000b3;font-size: 18px;">
                    <?php echo 'PROFIT'; ?>
                </div>
            </div>
        <div class="col-4" style="color:#101010;">
            <table class="table" style="width:100%">
                <tr>
                    <td colspan="2">
                    <?php echo $modelQuotation->leadowner->fullname.' , '.$modelQuotation->leadowner->phno.' , '; ?>
                    <br>
                    <?php echo $modelQuotation->leadowner->email; ?>
                    </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>
                    <?php if(isset($master_permission_array['inventory_image_show_quotation']) && $type==2){ ?>
                    <th class="text-center">Image</th>
                    <?php } ?>
                    <th class="text-center" style="width:4%">Qty</th>
                     <th class="text-center">Unit</th>
                    <th class="text-center" style="width:8%">Unit Cost Price</th>
                    <th class="text-center" style="width:8%">Unit Selling Price</th>
                    <th class="text-center" style="width:8%">Total Discount</th>
                    <th class="text-center">Profit/Unit</th>
                    <th class="text-center">Total Profit</th>
                    <th class="text-center">Profit(%)</th>
                </tr>
            </thead>
            <tbody>
                
                    <?php
                $m=1;
                $total_profit = 0;
                $condition .= '1=1';
                $condition .= ' AND t.quotes_id ="' . $id.'"';
                $count_alternate_ids = QuotesItem::model()->count(array('condition'=>'alternate_id!="" AND t.quotes_id ="' . $id.'"'));
                if($count_alternate_ids>0){
                    $order_by='alternate_id ASC';
                }
                else{
                    $order_by='id ASC';
                }
                $model_Quotes = QuotesItem::model()->findAll(array('condition'=>$condition,'order' => $order_by));
                foreach ( $model_Quotes as $i =>  $model_Quotes) { ?>
                    <?php   ( $model_Quotes->tax_vat_percentage > 0 )  ? $model_Quotes->tax_vat_amount = $model_Quotes->tax_vat_amount : $model_Quotes->tax_vat_amount = '0.00'; ?>
                    <?php
                    if($model_Quotes->product->inventory_item_type == 3){//memo ?>
                              <tr style="background-color:  #d0f5c5 ; color: white;"><td colspan="11"><b><?php echo $model_Quotes->description;?></b></td></tr>
                     <?php   }
                    else
                    { ?>
                      
              
                        <tr style="<?php if($model_Quotes->checked==0){echo 'background-color:#f0cfcf';} ?>">
                        <td>
                            <?php 
                            if($count_alternate_ids>0){
                                echo $model_Quotes->alternate_id;
                            }
                            else
                            {
                                echo $m;
                            }
                            ?>
                        </td>
                        
                        <td>
                            <?php echo $model_Quotes->product->part_number;?>
                        </td>
                        <td><?php echo $model_Quotes->description;?></td>
                       <?php if(isset($master_permission_array['inventory_image_show_quotation']) && $type==2){ ?>
                        <td>
                          <?php if($model_Quotes->product->item_image != ''){ ?>
                           <img width="120" height="120" src="<?php echo 'uploads/inventoryimages/thumbs/'.$model_Quotes->product->item_image; ?>">
                           <?php }
                           elseif($model_Quotes->image_file_name != ''){ ?>
                           <img width="120" height="120" src="<?php echo 'uploads/quotationimage/'.$model_Quotes->image_file_name; ?>">
                           <?php }?>
                        </td>
                        <?php } ?>
                        <td class="text-right">
                            <?php echo ( $model_Quotes->product->inventory_item_type == 2 ) ? 'N/A' : (float) $model_Quotes->quantity;?>
                        </td>
                        <td>
                            <?php //echo ( $model_Quotes->product->inventory_item_type == 2 ) ? 'N/A' : $model_Quotes->quoteunit->name;
                             if($model_Quotes->product->inventory_item_type == 2){
                                $unit_name = 'N/A';
                            }
                            elseif($model_Quotes->product->inventory_item_type == 4){
                                $unit_name = $model_Quotes->dummyquoteunit->symbol;
                            }
                            else{
                                $unit_name = $model_Quotes->quoteunit->name;
                            }
                            
                            echo $unit_name; ?>
                            
                        </td>
                      
                         <?php $rate= ( $model_Quotes->quantity > 0 ) ? ($model_Quotes->amount+$model_Quotes->addition_amount)/$model_Quotes->quantity : 0 ;?> 
                        <?php if($model_Quotes->cost_rate==0){?>
                         <td class="text-right" bgcolor="yellow">
                             <?php }else{ ?>
                              <td class="text-right">
                                  <?php } ?>
                            <?php echo Yii::app()->numberFormatter->formatCurrency($model_Quotes->cost_rate," ");?>
                        </td>
                        <td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($rate," ");?>
                        </td>
                         <td class="text-right">
                            <?php 
                            $discount_per_item = ($model_Quotes->deduction_amount/$model_Quotes->quantity);
                            echo Yii::app()->numberFormatter->formatCurrency($model_Quotes->deduction_amount," ");
                            $unit_selling_price=$rate - $discount_per_item;
                            $unit_cost_price = $model_Quotes->cost_rate;
                            $profit = $unit_selling_price - $unit_cost_price;
                            $total_item_profit = $profit * $model_Quotes->quantity;
                            
                            $total_cost_price = $model_Quotes->quantity * $unit_cost_price;
                            
                            //$profit_percent = ($unit_cost_price>0)?($total_item_profit / ($model_Quotes->quantity * $unit_cost_price)):100;
                            $profit_in_percentage = ($total_cost_price > 0) ? round(($total_item_profit / $total_cost_price) * 100, 2) : 100;
                            ?>
                        </td>
						<td class="text-right">
                            <?php echo Yii::app()->numberFormatter->formatCurrency($profit," ");?>
                        </td>
                        <td class="text-right"><?php echo Yii::app()->numberFormatter->formatCurrency($total_item_profit," ");?></td>
                        <td class="text-right"><?php echo Yii::app()->numberFormatter->formatCurrency($profit_in_percentage," ");?></td>
                        
                </tr>
                
                    <?php
                            $m=$m+1;
                            $total_profit+=$total_item_profit;
                    }
        } ?>
                    <?php
                $col = "10";
                 ?>
               
                            <tr>
                                <td class="text-right total bc-bl" colspan="<?php echo $col; ?>"><b>Total</b></td>
                                <!--<td></td>-->
                                <?php $total += $tot_tax; ?>
                                <td class="text-right total bc-br"><b><?php echo Yii::app()->numberFormatter->formatCurrency($total_profit," ");?></b></td>
                            </tr>
                            <tr>
                                <td class="text-right" colspan="<?php echo $col+1; ?>"><b><?php echo MasCurrency::number_to_currency(number_format(($total_profit), 2, '.', ''), $modelQuotation->currency); ?></b></td>
                            </tr>
            </tbody>
        </table>
     
    </div>

 <br/>
        <?php
        if ($modelQuotation->remarks != "") {?>
            <h5><U>Supplier Details</U></h5>
                <div class="h5sub-details">
            <?php echo nl2br($modelQuotation->remarks); ?>
                </div>
            <?php } ?>

    <htmlpagefooter name="myfooter">

    <img src="<?php echo  'images/logo/pdf_logo_footer.png'; ?>"/>
        <div style="font-size: 12px; text-align: center; padding-top: 10px;">
            <div style="font-size: 9px; text-align: center;">
            <?php echo Yii::app()->user->footer_autogenerated; ?><?php
                  date_default_timezone_set("Asia/Dubai");
                 echo date('d/m/Y')." ".date("h:i:sa"); ?>
            </div>
            <div style="text-align:center;width:100%">Page {PAGENO} of {nb} </div>
        </div>
    </htmlpagefooter>

    <style type="text/css">
   div { page-break-inside:auto;
    page-break-inside:avoid; 
    page-break-after:auto;

      }

</style>

</body>

</html>
