<?php

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

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

?>



    <div class="table-responsive">

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

            <tr>

                <th>No</th>  
                <th>Item</th>
                <th>Description</th>
                <th>Quantity</th>
                <th>Rate</th>
                <th>Unit</th>
                <th>Amount</th>

                <th class="text-center">
                    <a class="btn btn-sm btn-theme " href="javascript:void(0);" onclick="addTableRow($('#nonstock_item_table'));"><i class="fa fa-plus"></i></a></th>

            </tr>
            
                <?php foreach($modelEnquiryNonStockItem as $i=>$modelEnquiry){?>
                <tr>
                    
                    <td>  <label><?php echo ($i+1); ?> </label></td>
                    
                    <?php if($modelEnquiry->non_stock_item_name!=''){
                                 $non_stock_item_name = $modelEnquiry->non_stock_item_name;
                        
                      }else{
                       $non_stock_item_name = $modelRequisitionNonStockItem->non_stock_item_name;
                     }
                     ?> 
                    
                    
                    <td><?php echo $form->textField($modelEnquiry, "[" . $i . "]non_stock_item_name",array('data-label'=>'Item Name','class'=>'tooltipster go')); ?></td>
                
                <?php echo $form->hiddenField($modelEnquiry, "[" . $i . "]id"); ?>
                       <?php if($modelEnquiry->non_stock_item_description!=''){
                                 $non_stock_item_description = $modelEnquiry->non_stock_item_description;
                        
                      }else{
                       $non_stock_item_description = $modelRequisitionNonStockItem->non_stock_item_description;
                     }
                     ?> 
                    
                    
                    <td><?php echo $form->textArea($modelEnquiry, "[" . $i . "]non_stock_item_description",array('data-label'=>'Description','class'=>'go')); ?></td>
                
                  <?php if($modelEnquiry->non_stock_item_quantity!=''){
                                 $non_stock_item_quantity = $modelEnquiry->non_stock_item_quantity;
                        
                      }else{
                       $non_stock_item_quantity = $modelRequisitionNonStockItem->non_stock_item_quantity;
                     }
                     ?> 
                  
                
                
                    <td><?php echo $form->textField($modelEnquiry, "[" . $i . "]non_stock_item_quantity", array('class' => 'qty go cell-center text-right','onkeypress' => 'return isNumberKey(event)','onKeyup'=>'validatequantity(this.id);')); ?></td>

                    <td>
                              <?php echo $form->textField($modelEnquiry, "[" . $i . "]non_stock_item_rate", array('class' => 'qty go cell-center text-right', 'onkeypress' => 'return isNumberKey(event)','onkeyup' => 'calculatenonstockValues($("#nonstock_item_table"));')); ?>
                              <?php echo $form->error($modelEnquiry, "[" . $i . "]non_stock_item_rate"); ?>

                        </td>
                    
                     <?php if($modelEnquiry->non_stock_item_unit!=''){
                                 $non_stock_item_unit = $modelEnquiry->non_stock_item_unit;
                        
                      }else{
                       $non_stock_item_unit = $modelRequisitionNonStockItem->non_stock_item_unit;
                     }
                     ?> 
                    <td><?php echo $form->textField($modelEnquiry, "[" . $i . "]non_stock_item_unit", array('data-label'=>'Unit','class'=>'go')); ?></td>
                    <td>
                            <?php echo $form->textField($modelEnquiry, "[" . $i . "]non_stock_item_amount", array('class' => 'qty go cell-center text-right', 'onkeypress' => 'return isNumberKey(event)','onKeyup'=>'validate1(this.id);')); ?>
                            <?php echo $form->error($modelEnquiry, "[" . $i . "]non_stock_item_amount"); ?>
                            <?php $total+=$modelEnquiry->non_stock_item_amount; ?>     
                              
                     </td>
                    <td class="text-center nw" width="1%">
                          <?php echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('class'=>'btn btn-sm btn-danger','onclick' => 'removeTable($(\'#nonstock_item_table\'),this,this.id)', 'title' => 'Remove this item','id'=>'requisition_'.$i)); ?>
                 </td>
                </tr>
                <?php }?>
            
        </table>
        <table class="table table-hover table-bordered table-nexted nonstock_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($total, " "); ?></h2>

                </td>

            </tr>

        </table>
         <?php $grand_total = $total; ?>
        <table id="nonstock_final_add_table" class="table table-hover table-bordered table-nexted">        

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

        <tr>               

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

                 Additions                    

                <a class="btn btn-sm btn-theme " href="javascript:void(0);" onclick="addTableRow($('#nonorder_final_add_table'));"><i class="fa fa-plus"></i></a>

                </td>   

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

                <td>

                        <?php echo $form->textField($modelOrderAdditions, "[" . $i . "]particular_value", array('onkeyup' => 'calculatenonstockValues($("#nonstock_item_table"));', 'onkeypress' => 'return isNumberKey(event)', 'class' => 'value_rate go text-right', 'placeholder' => 'value')); ?>                    

                </td>

                <td>

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

                </td>

                <td width="0">                    

                    <?php echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('class'=>'btn btn-sm btn-danger','onclick' => 'removeExtrasRow($("#nonorder_final_add_table"), this)')); ?>                    

                </td>                

                <?php $grand_total +=$modelOrderAdditions->calculated_value; ?>            

        </tr>       

 <?php endforeach; ?>   

    </table>    

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

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

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

                            Deductions               

                <a class="btn btn-sm btn-theme " href="javascript:void(0);" onclick="addTableRow($('#nonorder_final_ded_table'));"><i class="fa fa-plus"></i></a>             

                    </td>

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

                    </td>

                    <td>

                     <?php echo $form->textField($modelOrderDeductions, "[" . $i . "]particular_value", array('onkeyup' => 'calculatenonstockValues($("#nonstock_item_table"));', 'onkeypress' => 'return isNumberKey(event)', 'class' => 'value_rate go text-right', 'placeholder' => 'value')); ?>                    

                    </td>

                    <td>

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

                    </td>    
                    <td width="0">
                    <?php echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('class'=>'btn btn-sm btn-danger','onclick' => 'removeExtrasRow($("#nonorder_final_ded_table"), this)')); ?>                    

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

        </tr>        

            <?php endforeach; ?>    

    </table>    

   <table class="table table-hover table-bordered table-nexted nonstock_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($grand_total, " "); ?>

                </h1></td>

            </tr>

        </table>
    </div>
<?php   
$cs = Yii::app()->getClientScript();
$cs->registerScript("removeTable ", "function removeTable(jQtable,evt,field_id) {
        var arr_fields= field_id.split('_');
        
        var row_field=arr_fields[1];
        var id=$('#PurchasingEnquiryItem_'+row_field+'_id').val();
           formIdStarter   =   jQtable.attr('id').split(/_(.+)?/)[0];
                var num_rows    =   jQtable.find('tr:gt(0)').length;
                if(num_rows>1){
                    var tr  =   $(evt).parent().parent();
                    if(isRowEmpty(tr)) { 
                        if(id!=''){
                                jQuery.ajax({

                                        'type': 'post',
                                        'dataType': 'json',
                                        'url': '" . Yii::app()->createUrl('enquiry/DeleteEnquiryItem') . "',
                                        'data': {'id':id}  ,
                                        'cache': false,
                                        'success': function(data) {
                                            if(data.status=='success'){
                                                $(evt).parents('tr').remove();
                                                var i=0;
                                                jQtable.find('tr:gt(0)').each(function() { $(this).find('input,textarea,.errorMessage').each(function()
                                                {
                                                old_id   =   $(this).attr('id');
                                                new_id   =   old_id.replace(/\d+/,i);
                                                $(this).attr('id',new_id);
                                                old_name =   $(this).attr('name');
                                                if(old_name!==undefined) {
                                                new_name =   old_name.replace(/\d+/,i);
                                                $(this).attr('name',new_name);} });
                                                $(this).find('td:first label').html(++i); }); 
                                                 t8.success('Material Request Deleted','success');
                                                 $.pnotify_remove_all();
                                            }else{
                                              t8.error('Deletion Not Possible, History Exist','error');
                                              
                                            }                         
                                        }
                                });
                            }else{
                                $(evt).parents('tr').remove();
                                var i=0;
                                jQtable.find('tr:gt(0)').each(function() { $(this).find('input,textarea,.errorMessage').each(function()
                                {
                                old_id   =   $(this).attr('id');
                                new_id   =   old_id.replace(/\d+/,i);
                                $(this).attr('id',new_id);
                                old_name =   $(this).attr('name');
                                if(old_name!==undefined) {
                                new_name =   old_name.replace(/\d+/,i);
                                $(this).attr('name',new_name);} });
                                $(this).find('td:first label').html(++i); }); 
                            }
                            

                    }else {
                        alert('Atleast one item needed here','Alert');
                    }
            }     
    };", CClientScript::POS_HEAD);
?>

<?php
$cs = Yii::app()->getClientScript();
$cs->registerScript("validatequantity", "function validatequantity(field_id){
        var arr_fields= field_id.split('_');
        
        var row_field=arr_fields[1];
        var id=$('#PurchasingEnquiryNonStockItem_'+row_field+'_non_stock_item_quantity').val();
        //alert(id);
        if($('#PurchasingEnquiryNonStockItem_'+row_field+'_non_stock_item_quantity').val()<=0)
        {
            alert('Plse enter valid quantity');
            return false;
         }
        else
        {
         return true;
        }
      };", CClientScript::POS_HEAD);    
?>
<?php $cs->registerScript("calculatenonstockValues", " function calculatenonstockValues(jQtable,frm) {
        $('.quotation-helper,.purchase-helper,.stock-helper').each(function(){ $(this).hide(); $(this).find('table').remove();});
            formIdStarter   =   jQtable.attr('id').split(/_(.+)?/)[0];
            var count,qty,rate,amount,addition_value,deduction_value,net;
            var other_additions = 0;
            var other_deductions = 0;
            var total   =   0;
            var grand_total =   0;
            jQtable.find('tr:gt(0)').each(function() {
                net = 0;
                qty =   $(this).find('[id $=_stock_item_quantity]').val();
                rate =   $(this).find('[id $=_non_stock_item_rate]').val();
                amount  =   (Number(qty)*Number(rate)).toFixed(2);
                net =   (Number(amount)).toFixed(2);
                if(net != 0){
                    total   =   Number(total)+Number(net);
                }
                else{
                    total   =   Number(total);
                }
                $(this).find('[id $=_amount]').val(Number(amount).toFixed(2));
                $(this).find('[id $=_addition_amount]').val(Number(addition_value).toFixed(2));
                $(this).find('[id $=_deduction_amount]').val(Number(deduction_value).toFixed(2));
                $(this).find('[id $=_net_amount]').val(Number(net).toFixed(2));
            });
            other_additions    =   calculateExtra($('#'+formIdStarter+'_final_add_table'),total);
            other_deductions  =    calculateExtra($('#'+formIdStarter+'_final_ded_table'),total);

                grand_total =   Number(total)   +   Number(other_additions);
                grand_total -=  Number(other_deductions);
                $('.'+formIdStarter+'_total .total').html(Number(total).toFixed(2));


            $('.'+formIdStarter+'_grand_total .grandtotal').text(Number(grand_total).toFixed(2));
            $('#grnd_total_id').val(Number(grand_total).toFixed(2));
        }", CClientScript::POS_HEAD); ?>        

