<?php

$cs = Yii::app()->getClientScript();

$baseUrl = Yii::app()->baseUrl;

?>


   <div id ="div_service_item_details">

    <div class="table-responsive">

        <table class="table table-hover table-bordered personal-task" id="short_closure_table">

            <tr>
                <th>No</th>  
                <th>Item</th>
                <th>Description</th>
                <th>Quantity</th>
                <th>Unit</th>
                <th>Rate</th>
                <th>Amount</th>
                <th>Received Qty</th>
                <th>Balance Qty</th>
                <th>Notes</th>
            </tr>

            <?php

        foreach ($modelOrderStockItem as $i => $modelOrder){
            ?>

                <tr>
                    <td>  <label><?php echo ($i+1); ?> </label></td>

                    <td>
                        <?php 
                          $modelOrder->stock_item_name = $modelOrder->product->part_number;
                        ?> 
                         <?php  echo $form->textField($modelOrder, "[" . $i . "]stock_item_name", array('class'=>'tooltipster go','readonly' => 'readonly')); ?>
                         <?php echo $form->hiddenField($modelOrder, "[" . $i . "]id"); ?>
                    </td>

                        <td>
                          <?php echo $form->textArea($modelOrder, "[" . $i . "]stock_item_description", array( 'class' => 'tooltipster desc go','readonly' => 'readonly'));  ?>
                         </td>

                        <td>
                            
                            <?php echo $form->textField($modelOrder, "[" . $i . "]stock_item_quantity", array('class' => 'qty go cell-center text-right','readonly' => 'readonly')); ?>

                           <td id="unit_<?php echo $i; ?>">
                             <?php 
                               ($modelOrder->stock_item_id > 0) ? $data = UnitOfMeasurement::getUnits($modelOrder->stock_item_id) : $data = array();
                             ?>
                             <?php echo $form->dropdownlist($modelOrder, "[" . $i . "]stock_item_unit",$data, array('class'=>'qty go','disabled' => true)); ?>
                        </td>
                        <td>
                              <?php echo $form->textField($modelOrder, "[" . $i . "]stock_item_rate", array('class' => 'qty go cell-center text-right','readonly' => 'readonly')); ?>
                           

                        </td>
                       
                        <td>
                                <?php echo $form->textField($modelOrder, "[" . $i . "]stock_item_amount", array('class' => 'qty go cell-center text-right','readonly' => 'readonly')); ?>
                              
                            <?php $stocktotal+=$modelOrder->stock_item_amount; ?>     
                              
                        </td>
                        <?php 
                            $existReceipt = PurchasingMaterialreceipt::model()->find(array('condition'=>'po_id='.$model->id,'order' => 'id desc'));
                            /* if(!empty($existReceipt)){
                                $con ='purchasing_materialreceipt_id ='.$existReceipt->id.' AND product_id='.$modelOrder->stock_item_id;
                                $remaining =PurchasingMaterialreceiptItem::model()->find(array('select'=>'remaining_quantity','condition'=>$con));
                                $modelOrder->remaining_qty = $remaining->remaining_quantity;
                                $modelOrder->received = number_format($modelOrder->stock_item_quantity -  $remaining->remaining_quantity, 2, '.', '');
                            }else{
                                 $modelOrder->remaining_qty = number_format($modelOrder->stock_item_quantity -  $remaining->received, 2, '.', '');
                            } */
                            if(!empty($existReceipt)){
                            	$con ='purchasing_materialreceipt_id ='.$existReceipt->id.' AND product_id='.$modelOrder->stock_item_id.' AND order_item_id='.$modelOrder->id;
                            	$remaining =PurchasingMaterialreceiptItem::model()->find(array('select'=>'remaining_quantity,received_quantity','condition'=>$con));
                            	$modelOrder->received = $remaining->received_quantity;
                            	$modelOrder->remaining_qty = number_format($modelOrder->stock_item_quantity -  $modelOrder->received, 2, '.', '');
                            }else{
                            	$modelOrder->remaining_qty = number_format($modelOrder->stock_item_quantity -  $remaining->received, 2, '.', '');
                            }
                        ?>
                        <td>
                            <?php echo $form->textField($modelOrder, "[" . $i . "]received", array('class' => 'qty go cell-center text-right','readonly' => 'readonly')); ?>
                        </td>
                        <td>
                              <?php echo $form->textField($modelOrder, "[" . $i . "]remaining_qty", array('class' => 'qty go cell-center text-right','readonly' => 'readonly')); ?>
                        </td>
                        <td>
                              <?php echo $form->textArea($modelOrder, "[" . $i . "]notes", array('class' => 'desc go en-text-area' )); ?>
                        </td>
                       
                       
                
                </tr>
        <?php }?>
        </table>
        <table class="table table-hover table-bordered table-nexted order_total">

            <tr class="bg-total">

                <td width="70%" class="text-right table_total_title">

                    <h2>Total</h2>

                </td>

                <td width="30%" class="text-right table_total">

                    <h2 class="total" class="text-right"><?php echo Yii::app()->numberFormatter->formatCurrency($stocktotal, " "); ?></h2>

                </td>

            </tr>

        </table>
         <?php $stockgrand_total = $stocktotal; ?>
        <table id="order_final_add_table" class="table table-hover table-bordered table-nexted">        

       <?php  foreach($modelOrderAdditions as $i => $modelOrderAdditions): ?>            

        <tr>               

             <td class="text-right td-in-6">

                 Additions        
             </td>   

                <td class="td-in-6">
                    <?php echo $form->dropDownList($modelOrderAdditions, "[" . $i . "]particular",MasFooterItems::getPurchaseAdditions(),array('class' => 'go','data-label'=>'Particulars','empty'=>'Select','disabled' => true));?>
                    <?php echo $form->hiddenField($modelOrderAdditions, "[" . $i . "]id");?>
                </td>

                <td>

                        <?php echo $form->textField($modelOrderAdditions, "[" . $i . "]particular_value", array('readonly' => 'readonly','class' =>'unit go')); ?>                    

                </td>

                <td>

                        <?php echo $form->textField($modelOrderAdditions, "[" . $i . "]calculated_value", array('readonly' => 'readonly', 'class' => 'value_rate additions_value go text-right')); ?>                    

                </td>
               <?php $stockgrand_total +=$modelOrderAdditions->calculated_value; ?>            

        </tr>       

 <?php endforeach; ?>   

    </table>    

    <table id="order_final_ded_table" class="table table-hover table-bordered  table-nexted">        

        <?php  foreach ($modelOrderDeductions as $i => $modelOrderDeductions): ?>           

        <tr>                
                    <td class="text-right td-in-6">

                            Deductions               

                

                    </td>

                    <td class="td-in-6">
                    <?php echo $form->dropDownList($modelOrderDeductions, "[" . $i . "]particular",MasFooterItems::getPurchaseDeductions(),array('class' => 'go','data-label'=>'Particulars','empty'=>'Select','disabled' => true));?>
                    <?php echo $form->hiddenField($modelOrderDeductions, "[" . $i . "]id");?>

                    </td>

                    <td>

                     <?php echo $form->textField($modelOrderDeductions, "[" . $i . "]particular_value", array('readonly' => 'readonly','class' =>'unit go')); ?>                    

                    </td>

                    <td>

                     <?php echo $form->textField($modelOrderDeductions, "[" . $i . "]calculated_value", array('readonly' => true, 'class' => 'value_rate deductions_value go text-right')); ?>                                    

                    </td>    
                       
                  <?php $stockgrand_total -=$modelOrderDeductions->calculated_value; ?>            

        </tr>        

            <?php endforeach; ?>    

    </table>    

   <table class="table table-hover table-bordered table-nexted order_grand_total">

            <tr class="summary_tr bg-total">

                <td width="70%" class="text-right table_total_title">

                    <h1>Grand Total</h1></td>

                <td width="30%" class="text-right table_total">

                    <h1 class="grandtotal" class="text-right">

                <?php echo Yii::app()->numberFormatter->formatCurrency($stockgrand_total, " "); ?>

                </h1></td>

            </tr>

        </table>
    </div>

         </div>
<script>

</script>
