<?php 

$mas_company_details = MasCompany::model()->findByPk(Yii::app()->user->master_user_company_id);
$condition= 't.Order_id ="' . $id.'"';
$total=0;
$modelOrder = Order::model()->findByPk($id);
$modelOrderItems = OrderItem::model()->findAll(array('condition'=>$condition,'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 = Order::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>
                    <th class="text-center" style="width:10%">Unit</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 ( $modelOrderItems as $i =>  $each_item) { 
                    $avail_qty=0;
                    
                    // $avail_qty_new = $each_item->product->quantity;
                    // print_r($avail_qty_new);
                    $condition = 'type=1';
                    $condition .= ' AND id = '.$each_item->product_id;
                    $query = 'SELECT * FROM view_all_stock_items WHERE '.$condition.'';
                    $view_all_stock_items = Yii::app()->db->createCommand($query)->queryAll();

                    
                    $arr_product_UOM = new UnitOfMeasurement();
                    $condition1 = '1=1';
                    $condition1 .= ' AND product_id = '.$each_item->product_id;
                    $condition1 .= ' AND id = '.$each_item->unit_id;

                    $arr_product_UOM=$arr_product_UOM->findAll(array('condition'=>$condition1));

                    if(($arr_product_UOM[0]->factor>0 && $view_all_stock_items[0]['quantity'] >0)){
                        $quantity_avail = number_format(($view_all_stock_items[0]['quantity'] / $arr_product_UOM[0]->factor),2);

                    }else{
                        $quantity_avail ='';
                    }

                   
                    $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*unit_factor),0) as quantity','condition'=>'product_id="'.$each_item->product_id.'" AND stock_updated="Y"')); 
                    $avail_qty= $total_available_product->quantity - $total_allocated_product->quantity;
                    //$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;
                    $totalPicked_qty = Yii::app()->db->createCommand('SELECT SUM(DI.picked_qty) FROM  erp_delivery_item DI LEFT JOIN erp_delivery_note DN ON DN.id = DI.delivery_note_id  WHERE DN.order_id = '.$each_item->order_id.' AND  DI.order_item_id="'.$each_item->id.'" and DN.status!="CN"')->queryScalar();
                    $pickable_quantity=($each_item->quantity - $totalPicked_qty);
                    $totalPickedBonus_qty = Yii::app()->db->createCommand('SELECT SUM(DI.picked_bonus_qty) FROM  erp_delivery_item DI LEFT JOIN erp_delivery_note DN ON DN.id = DI.delivery_note_id  WHERE DN.order_id = '.$each_item->order_id.' AND  DI.order_item_id="'.$each_item->id.'" and DN.status!="CN"')->queryScalar();
                    $pickable_bonus_quantity= ($each_item->bonus_qty -$totalPickedBonus_qty);
                    if($pickable_quantity>0 && $each_item->product->inventory_item_type==1){
                        //$each_item->orderunit->factor>0?$avail_qty=$avail_qty/$each_item->orderunit->factor:'';
                        if($each_item->quantity>$quantity_avail){
                            $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) $quantity_avail.'</td>
                                                <td>'.$each_item->orderunit->name.'</td>';

                                                foreach($arr_warehouse as $warehouse){
                                                    $total_available_product_in_warehouse=StockLedger::model()->find(array('select' => 'IFNULL(sum(quantity*unit_factor),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) $quantity_avail.'</td>
                                                <td>'.$each_item->orderunit->name.'</td>';

                                                foreach($arr_warehouse as $warehouse){
                                                    $total_available_product_in_warehouse=StockLedger::model()->find(array('select' => 'IFNULL(sum(quantity*unit_factor),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;?>
