<?php 

$mas_company_details = MasCompany::model()->findByPk(Yii::app()->user->master_user_company_id);
$condition_main= 't.quotes_id =' . $id;
$condition= 't.quotes_id =' . $id;
$total=0;
$modelQuotes = Quotes::model()->findByPk($id);

$modelQuotesItems = QuotesItem::model()->findAll(array('condition'=>$condition_main,'order' => 'id ASC'));
?>
<!DOCTYPE html>
<html>
<head>
 <title>Available Quantity</title>
<style>
body{padding:0px!important;font-size:12px;}
table{
  border-collapse: collapse;}
.item_table {
  border-collapse: collapse;
  font-size:12px!important;
  margin:0px;
}
table.item_table,.item_table th,.item_table td {
  border: 1px solid black; 
  font-family:'Times New Roman', Times, serif;
  padding:1px;
} 
@media print {
.start-newpage{page-break-before:always;display: block;}
}
</style>

</head>
<body style="font-family: "Times New Roman", Times, serif">

<div class="main" >
     <div class="col-6">
            <br>
            <?php
                $modelQuotation = Quotes::model()->findByPk($id);
                
                $arr_warehouse = MasInventoryWarehouse::model()->findAll(array('condition'=>'isActive="Y"'));
               
            ?>
                <div class="h3" style="margin-top: 0;margin-bottom: 0;">
                    <?php echo "Available Quantity"; ?>
                </div>
                <div class="input-block">
                    <b><?php echo $id;?></b>
                </div>
                <b><?php echo strtoupper($modelQuotation->customer->customer_name); ?></b>
                <br>    
                <?php if($modelQuotation->billing_address != '') {     ?>       
                <small><?php echo strtoupper(nl2br($modelQuotation->billing_address)); ?></small><br>
                    <?php } ?>                
                <?php if($modelQuotation->customer->customer_phone_no != '') { ?>
                <small>Phone : <?php echo strtoupper($modelQuotation->customer->customer_phone_no); ?></small><br>
                <?php } ?>
                <?php if($modelQuotation->customercontact->person_name != '') { ?>
                <br><small>Kind Attn:</small>
                <small><b><?php echo  strtoupper($modelQuotation->customercontact->person_name); ?></b></small><br>
                <?php } ?>
        </div>
        <div class="col-5 offset-1" style="color:#101010;">
           <?php $this->renderPartial("//common/header");?>
            </div>
<div  style="width:100%;float: left;margin-top: 10px;font-size:11px;font-family:'Times New Roman', Times, serif;"> 
    
<table id="" class="table">
            <thead>
                <tr>
                    <th class="text-center">No</th>
                    <th class="text-center">Part No</th>
                    <th class="text-center" style="width:30%">Description</th>
                    <th class="text-center" style="width:10%">Req.Qty</th>
                    <th class="text-center" style="width:10%">Avail.Qty</th>
                    <?php
                     $colspan=5;
                    foreach($arr_warehouse as $warehouse){
                    $colspan++;
                    ?>
                    <th class="text-center" style="width:10%"><?php echo $warehouse->warehouse_name;?></th>
                    <?php } ?>
                </tr>
            </thead>
            <tbody>
                    <?php
                   
                $m=1;$n=1;$tr_non_avail='';$tr_avail='';
                foreach ( $modelQuotesItems as $i =>  $each_item) { 
                    $avail_qty=0;
                    $part_number = $each_item->product->part_number;
                    $total_allocated_product = AllocatedMaterial::model()->find(array('select' => 'IFNULL(sum(quantity),0) as quantity','condition'=>'product_id="'.$each_item->product_id.'"')); 
                    $total_available_product=StockLedger::model()->find(array('select' => 'IFNULL(sum(quantity),0) as quantity','condition'=>'product_id="'.$each_item->product_id.'" AND stock_updated="Y"')); 
                    $avail_qty= $total_available_product->quantity - $total_allocated_product->quantity;
                    
                    
                    
                    if($each_item->product->inventory_item_type==1){
                    //$avail_qty=StockLedger::model()->with('warehouse')->find(array('select'=>'SUM(quantity) AS quantity','condition'=>'product_id="'.$each_item->product_id.'" and warehouse.warehouse_code!="DAM" AND available_status="A" AND stock_updated="Y"'))->quantity;
                        if($each_item->quantity>$avail_qty){
                            $tr_non_avail .='<tr style="background-color:#f0cfcf">
                                                <td>'.$m.'</td>
                                                <td>'.$part_number.'</td>
                                                <td>'.nl2br($each_item->description).'</td>
                                                <td align="right">'.(float) $each_item->quantity.'</td>
                                                <td align="right">'.(float) $avail_qty.'</td>';
                                                foreach($arr_warehouse as $warehouse){
                                                    $total_available_product_in_warehouse=StockLedger::model()->find(array('select' => 'IFNULL(sum(quantity),0) as quantity','condition'=>'product_id="'.$each_item->product_id.'" AND stock_updated="Y" AND warehouse_id='.$warehouse->id)); 
                                                    $tr_non_avail.='<td align="right">'.(float) $total_available_product_in_warehouse->quantity.'</td>';
                                                }
                            $tr_non_avail.='</tr>';
                            $m++;
                        }else{
                           $tr_avail .='<tr>
                                                <td>'.$n.'</td>
                                                <td>'.$part_number.'</td>
                                                <td>'.nl2br($each_item->description).'</td>
                                                <td align="right">'.(float) $each_item->quantity.'</td>
                                                <td align="right">'.(float) $avail_qty.'</td>';
                                                foreach($arr_warehouse as $warehouse){
                                                    $total_available_product_in_warehouse=StockLedger::model()->find(array('select' => 'IFNULL(sum(quantity),0) as quantity','condition'=>'product_id="'.$each_item->product_id.'" AND stock_updated="Y" AND warehouse_id='.$warehouse->id)); 
                                                    $tr_avail.='<td align="right">'.(float) $total_available_product_in_warehouse->quantity.'</td>';
                                                }
                            $tr_avail.='</tr>';
                            $n++; 
                        }
                    }
                }
                if($tr_non_avail!=''){ ?>
                <tr><td colspan="<?php echo $colspan;?>"><h3>Non Available Items</h3></td></tr>
                <?php echo $tr_non_avail;}
                if($tr_avail!=''){ ?>
                <tr><td colspan="<?php echo $colspan;?>"><h3> Available Items</h3></td></tr>
                <?php echo $tr_avail;}?>
            </tbody>
        </table>
 

 

 </div>

</div>


</div>
 

 <?php $this->renderPartial("//common/footer");?>

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

      }

</style> -->
 
 
</body>

</html>


<?php //exit;?>
