<!---- 
    Author			: Hasan
    Created Date	: 03 Feb 2015
    Revision		: Fachri1) 
-->

	<?php
				//---------Untuk privellege jika dia ada tombol save----------
				if($group_user->SavePrivillage == 1){
					$cek_autority_save 		= "";
				}
				else{
					$cek_autority_save 		= "disabled";
				}
				
				//-------------end----------------------------
	?>	
	
<style>
table td{
	height: 20px;
}
</style>

<div class="easyui-layout" data-options="fit:true,border:false">
    <form id="fm_job_costing" name="fm_job_costing" method="post">
		<input type="hidden" name="detail" id="detail" />

        <!-- top view-->
        <div id="form-header" data-options="region:'north',border:false" >
            <div id="form-header-wrapper">
                <table cellpadding="0" cellspacing="0" width="100%" border="0">
                <tr>
                    <td align="left">
                        <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_add('New Job Costing','<?=$menu_link?>','icon-coa','add')">New</a>
						<a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
                    </td>					
        
                    <td height="25" align="right" valign="middle">
                        <span class="form-title" style="margin-left:7px;">
                            <?=$menu_label?>
                        </span>							
                    </td>
                </tr>
                </table>		     	
            </div>			
        </div>
        <!-- end top view -->	

    	<!-- center view -->
		<div id="form-body" data-options="region:'center',border:false">
            <div id="form-wrapper" class="easyui-panel"  style="width:1180px;height:526px;padding:5px;" fit='true'>
                <input type="hidden" name="" id="" value="" />
                
                <table width="100%" height="141">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                            <td width="100px"><b>Job Costing</b></td>
                            <td colspan="3">
                            	<b>:</b>
                                <select name="cmb_jcost" class="combo-grid" id="cmb_jcost" required="required" editable="true"  style="width:400px;height:22px;" data-options="
                                panelWidth: 400,
                                idField: 'AccountNumber',
                                textField: 'AccountName',
                                url: '<?php echo base_url(); ?><?php echo"list/job_costing"; ?>/get_persedian_bahan_baku',               
                                columns: [[
                                 {field:'AccountNumber',title:'Account Number',width:130},
                                 {field:'AccountName',title:'Account Name',width:350}
                                ]],
                                
                                fitColumns: true
                                ">
                                </select> 
                                
                            </td>
                            <td width="60"&nbsp;>&nbsp;</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td colspan="2">&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td colspan="2">&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr>
                        	<td height="20">&nbsp;</td>
                            <td width="731" align="center" valign="middle"></td>
                            <td width="150" align="center" valign="middle" bgcolor="#333399" ><span style="color:#fff;">Date</span></td>
                            <td width="250" align="center" valign="middle" bgcolor="#333399"><span style="color:#fff;">Department</span></td>
                        </tr>
                        <tr>
                        	<td height="20">&nbsp;</td>
                            <td align="center" valign="top">&nbsp;</td>
                            <td align="right" valign="top"><input name="txt_date" id="txt_date" type="text" value="<?php echo $date_now;?>" class="easyui-datebox validBackward" style="width:150px;height:20px" required editable="false" /></td>
                            <td align="left" valign="top">
                            	<select name="cmb_department" id="cmb_department" required
                                	style="width:250px;height:20px;" 
                                    class="combo-grid" 
                                    data-options="
                                    	idField:'DepartmentCode', 
                                        textField:'DepartmentName', 
                                        url:'<?=base_url()?>custom/get_data/department',
                                        columns: [[
                                         {field:'DepartmentName',title:'Department Name',width:224}
                                        ]]
                                    " 
                                />
                            </td>
                        </tr>
                        <tr>
                            <td height="20">&nbsp;</td>
                            <td width="731" height="20" align="center" valign="middle">&nbsp;</td>
                            <td width="150" align="center" valign="middle">&nbsp;</td>
                            <td width="250" align="center" valign="middle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td height="20" align="left" valign="top">&nbsp;</td>
                            <td align="right" valign="top">&nbsp;</td>
                            <td align="right" valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td width="192" align="center" valign="middle">&nbsp;</td>
                            <td height="20" width="731" align="center" valign="middle">&nbsp;</td>
                            <td width="150" align="center" valign="middle">&nbsp;</td>
                            <td width="250" align="center" valign="middle">&nbsp;</td>
                        </tr>		
                        <tr>
                            <td align="left" valign="top">&nbsp;</td>
                            <td height="20" align="left" valign="top">&nbsp;</td>
                            <td height="20" align="left" valign="top">&nbsp;</td>
                            <td align="right" valign="top">&nbsp;</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        </table>
                    </td>
            	</tr>											
                </table>	

                <!--Item Grid -->
                <div class="easyui-tabs" data-options="tabHeight:18">
                    <div title="Items">
                        <table toolbar="#tb_dg_item" id="dg_job" class="easyui-edatagrid" idField="id"
                            rownumbers="true" fitColumns="true" 
                            singleSelect="true" style="height:256px;" 
                            >
                            <thead>
                            <tr>
                                <th field="ItemCode" 	halign="center"	align="left" valign="middle"	data-options="width:400,align:'left'" 			editor="{type:'item_inventory',options:{required:true}}">Item</th>
                                <th field="Description" halign="center" align="left" valign="middle"	data-options="width:700,align:'left'" 			editor="{type:'text',options:{required:true}}">Item Description</th>
                                <th field="Qty" 		halign="center" align="left" valign="middle"	data-options="width:200,align:'left'" 			editor="{type:'numberbox',options:{precision:2,decimalSeparator:','}}" formatter="formatRp">Qty</th>
                                <th field="ItemUnit" 	halign="center" align="left" valign="middle"	data-options="width:200,align:'left'" 			editor="{type:'text',options:{required:true}}" >Unit</th>
                                <th field="ItemAverage" halign="center" align="left" valign="middle"	data-options="width:150,align:'left'"  			editor="{type:'numberbox',options:{precision:2,decimalSeparator:','}}" formatter="formatRp" >Unit Price</th>
                                <th field="DateJob" 		halign="center" align="left" valign="middle"	data-options="width:150,align:'left'" 			editor="{type:'datebox',options:{editable:false}}" formatter="formatDate">Date</th>
                                <th field="Amount_item" halign="center" align="left" valign="middle"	data-options="width:150,align:'left'" 			editor="{type:'numberbox',options:{decimalSeparator:',',
                                formatter:function(v){
                                	return v;
                                },
                                parser:function(s){
                                	var v = parseFloat(s);
                                    return v;
                                }
                                }}" hidden>Amount</th>
                                <th field="ItemKategori" halign="center" align="left" valign="middle"	data-options="width:150,align:'left'" 			editor="{type:'text'}" 		hidden>Kategori</th>
                                <th field="ItemType" 	halign="center" align="left" valign="middle"	data-options="width:150,align:'left'" 			editor="{type:'text'}"  	hidden>Item Type</th>
                                <th field="ItemStatus" 	halign="center" align="left" valign="middle"	data-options="width:150,align:'left'" 			editor="{type:'text'}"  	hidden>Item Status</th>
                            	<th field="ItemAverage_Real" halign="center" align="left" valign="middle"	data-options="width:150,align:'left'"  		editor="{type:'numberbox',options:{decimalSeparator:',',
                                formatter:function(v){
                                	return v;
                                },
                                parser:function(s){
                                	var v = parseFloat(s);
                                    return v;
                                }
                                }}" hidden>Real Hpp</th>
                            </tr>
                            </thead>
                        </table>	
                        <div id="tb_dg_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add_jobcos();">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row();">Delete</a>
                        </div>					
                    </div>
                    
                </div>
                <!-- End Item Grid -->		

                <table width="100%" cellpadding="0" cellspacing="0" style="margin-top:5px;">
                <tr>
                    <td width="100" rowspan="2" valign="top"><b>Description</b></td>
                    <td width="362" rowspan="2" valign="top"><textarea name="txa_description" cols="55.5" rows="4" style="resize:none;"></textarea></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="301" align="right" valign="top" height="23"><b>Total Job Cost :&nbsp;</b></td>
                    <td width="144" align="right" valign="top">
                        <input type="text" id="sub_total" name="sub_total" class="easyui-numberbox" value="0" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;" readonly />
                        <input type="text" id="sub_total_koma" name="sub_total_koma" class="easyui-numberbox" value="0"  hidden
                        data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                        formatter:function(v){
                            return v;
                        },
                        parser:function(s){
                            var v = parseFloat(s);
                            return v;
                        }
                        " 
                         style="text-align:right;" readonly />
                    </td>
                </tr>
                </table>			
            </div>
        </div>
        <div id="form-footer" data-options="region:'south',border:false">
			<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
				 <tr>
				   <td align="left">
						
				   </td>
					<td align="right">     
                    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-document" onclick="show_legend();" plain="true">Legend</a>                         
						 <a href="javascript:void(0)" 	class="easyui-linkbutton" 							iconCls="icon-help" 	onclick="" plain="true">Help</a>								
                        <a href="javascript:void(0)" 	class="easyui-linkbutton form-button save-button" 	iconCls="icon-save" 	onClick="save_me()" <?php echo $cek_autority_save;?>>Save</a> 	
                        <a href="javascript:void(0)" 	class="easyui-linkbutton form-button" 				iconCls="icon-cancel" 	onclick="location.reload();">Cancel</a>	
					</td>
				 </tr>
			</table>				
		</div>
    </form>
    
</div>

<!-- Job Costing pop -->
<div id="dialog-job-cos" class="easyui-dialog" style="width:850px;height:523px;top:27px;background-color:#F8F8F8;padding:0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#job_cos">
    <div id="tab_purchase_req" class="easyui-tabs" style="width:835px;height:485px; background-color:#F8F8F8;" data-options="fit:true,plain:true">
    	<div title="Item" style="padding:10px" >
        	<table width="100%">
            	<tr>
                <td width="310"><b>Item Category</b></td>
                <td><b>:</b></td>

                <td>
                    <select name="cmb_ItemCategoryID" id="cmb_ItemCategoryID" class="easyui-combobox" style="width:502px;" editable="true" 
                        data-options="
                            value:'All',
                            valueField:'ItemCategoryID',
                            textField:'ItemCategoryName',
                            url:'<?=base_url()?>custom/get_data/itemcategory',
                            onSelect: function(rec){
                                var id = rec.ItemCategoryID;
                                search_all_item();
                            }
                        ">
                    </select>
                </td>
            </tr>

            <tr>
                <td><b><span class="header_code">Item Code</span></b></td>
                <td><b>:</b></td>
                
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_ItemCode" id="txt_ItemCode" class="easyui-validatebox" placeholder="Item Code" onkeyup="search_all_item();" />
                </td>
            </tr>
            
            <tr>
                <td><b><span class="header_name">Item Name</span></b></td>
                <td><b>:</b></td>
                
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_ItemDesc" id="txt_ItemDesc" class="easyui-validatebox" placeholder="Item Name" onkeyup="search_all_item();" />
                </td>
            </tr>
            </table>
            <table id="grid_pop_up" class="easyui-datagrid" singleSelect="true"  rownumbers="true" style="width:812px;height:313px"
               url="<?Php echo base_url()?>custom/get_data/search_all_item"
                pageSize="50" 
                data-options = "border:true,view:scrollview,autoRowheight:false"
            >
                <thead>
                    <tr bgcolor="">
                        <th halign="center" field="ItemCode" 		width="150" sortable="true">Item Code</th>
                        <th halign="center" field="ItemDesc" 		width="300" sortable="true">Name</th>
                        <th halign="center" field="ItemCategoryName" width="192" sortable="true" >Item Category Name</th>
                        <th halign="center" field="ItemUnit" 		width="70" sortable="true" 		hidden>Item Unit</th>
                        <th halign="center" field="ItemAverage" 	width="100%" sortable="true" 	hidden>Item Average</th>
                        <th halign="center" field="IsLock" 			width="90" sortable="true" 		hidden>Status</th>
                        <th halign="center" field="ItemCategoryID" 	width="200" sortable="true" 	hidden>ItemCategoryID</th>
                        <th halign="center" field="ItemQty" 		width="96" sortable="true" 	formatter="formatRp">Item Qty</th>
                        <th halign="center" field="ItemLockId" 		width="200" sortable="true" 	hidden>Item Lock Id</th>
                    </tr>
                </thead>
            </table>
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_job_costing_ok();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-job-cos');">Cancel</a>
            </div>
            
        </div>
        <div title="Services / Expenses" style="padding:10px">
        	<table width="100%">
            	<tr>
                <td width="310"><b>Item Category</b></td>
                <td><b>:</b></td>

                <td>
                    <select name="cmb_ItemCategoryID" id="cmb_ItemCategoryID" class="easyui-combobox" style="width:512px;" editable="false" disabled="disabled"
                            data-options="
                            valueField:'ItemCategoryID',
                            textField:'ItemCategoryName',
                            url:'',
                            onSelect: function(rec){
                                var id = rec.ItemCategoryID;
                                search_servis_item();
                            }
                        ">
                    </select>
                </td>
            </tr>

            <tr>
                <td><b><span class="header_code">Item Code</span></b></td>
                <td><b>:</b></td>
                
                <td>
                    <input type="text" style="width:510px;height:20px" name="txt_ItemCode_Servis" id="txt_ItemCode_Servis" placeholder="Item Code" class="easyui-validatebox" onkeyup="search_servis_item();" />
                </td>
            </tr>
            
            <tr>
                <td><b><span class="header_name">Item Desc</span></b></td>
                <td><b>:</b></td>
                
                <td>
                    <input type="text" style="width:510px;height:20px" name="txt_ItemDesc_Servis" id="txt_ItemDesc_Servis" placeholder="Item Desc" class="easyui-validatebox" onkeyup="search_servis_item();" />
                </td>
            </tr>
            </table>
            <table id="tt_choose_servis" class="easyui-datagrid" singleSelect="true" remoteSort="false" pagesize="150" fitColumns="true" style="width:812px;height:313px" 
                data-options = "border:true,view:scrollview,autoRowheight:false"
                url="<?=base_url()?>custom/get_data/search_service"                   
            >                        
                <thead>
                    <tr>
                        <th field="ItemCode"        width="293" sortable="true" halign='center'><span class="header_code">Item Code</span></th>
                        <th field="ItemDesc"        width="430" sortable="true" halign='center'><span class="header_name">Description</span></th>
                        <th field="ItemCategoryID"  width="200" sortable="true" halign='center' hidden><span class="header_name">Item Category</span></th>
                        <th field="ItemCategoryName"  width="200" sortable="true" halign='center' ><span class="header_name">Category Name</span></th>
                    </tr>
                </thead>
            </table>
            <!-- Dialog Button for Dialog servis-->
            <div style="border:none; margin-top:10px; float:right;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_expense();">&nbsp; OK &nbsp; &nbsp;</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-job-cos');">Cancel</a>
            </div>
        </div>
    </div>
</div>

<!-- Dialog Button -->

<?php $this->load->view('v_custom_pop_up');?>
<script type="text/javascript">
var SavePrivillage 	= '<?php echo $group_user->SavePrivillage;?>';
var LoopIndexGrid   = 0;
var index_tab 		= 0;



$(document).ready(function(){
	$('#sub_total').numberbox('setValue',sub_total);
		$('#tab_purchase_req').tabs({
	    	onSelect:function(titel,index){
	    		index_tab = index;
	    	}
	    });
		
		$('#dg_job').edatagrid({
			onBeginEdit:function(rowIndex){
				editors = $('#dg_job').edatagrid('getEditors',rowIndex);
				
				ItemCode		= $(editors[0].target);
				ItemDesc		= $(editors[1].target);
				qty_editor		= $(editors[2].target);
				ItemUnit		= $(editors[3].target);
				hpp_editor		= $(editors[4].target);
				DateJob			= $(editors[5].target);
				
				Amount_item		= $(editors[6].target);
				
				ItemType		= $(editors[8].target);
				ItemStatus		= $(editors[9].target);
				RealHPP			= $(editors[10].target);
				
				var sub_total   = 0;
				
				DateJob.datebox('setValue', " ");
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				save_button_begendedit(SavePrivillage);
				/*=================================end============================================================*/
				ItemDesc.prop('readonly', true);
				ItemUnit.prop('readonly', true);
				/*
					Check apalah dia Inventory apa expense
				*/
				if(ItemType.val() == 0){
					hpp_editor.prop('readonly', true);
				}
				else{
					hpp_editor.prop('readonly', false);
				}
				
				$(ItemCode).click(function(){
					open_job_cos(rowIndex);
				}).mouseover(function(){
					$(this).css('cursor','pointer');
				});
			
				qty_editor.numberbox({
					onChange:function(){
						qty 	 = $(qty_editor).numberbox('getValue');
						hpp 	 = $(hpp_editor).numberbox('getValue');
					
						amount = parseFloat(qty) * parseFloat(hpp);
						$(Amount_item).numberbox('setValue',amount);
					}
				});
				
				hpp_editor.numberbox({
					onChange:function(){
						qty 	 = $(qty_editor).numberbox('getValue');
						hpp 	 = $(hpp_editor).numberbox('getValue');
						hpp_real = $(RealHPP).numberbox('setValue', hpp);
						
						
						amount = parseFloat(qty) * parseFloat(hpp);
						$(Amount_item).numberbox('setValue',amount);
					}
				});
			},
			onEndEdit:function(rowIndex, rowData, changes){
				var item        = $('#dg_job').datagrid('getRows');
                var sub_total   = 0;
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				save_button_endedit(SavePrivillage);
				/*=================================end============================================================*/
				
                $.each(item, function( index, value ) {
                    if(value.Amount_item != undefined){
                        sub_total+= parseFloat(value.Qty * value.ItemAverage_Real);
                    }				
                });

                $('#sub_total').numberbox('setValue',sub_total);
				$('#sub_total_koma').numberbox('setValue',sub_total);
				
			}
		});
		
		$('#fm_job_costing').click(function(){
			$('#dg_job').edatagrid('saveRow');
		});
		
		datagrid_item_type("dg_job");//untuk warna grid
});

function save_me(){
	$('#dg_job').edatagrid('saveRow');
	var i=$('#dg_job').datagrid('getRows');
	var detail = JSON.stringify(i);
	$("#detail").val(detail);
	save_transaction('<?php echo $menu_link?>','#fm_job_costing');
}

function open_job_cos(indexRow) { 
	LoopIndexGrid = indexRow;
   modal_open('Inventory','dialog-job-cos');
}

/*==================================jika di klik tombol ok di pop up================================*/
function choose_job_costing_ok(){
	var selected_data=$('#grid_pop_up').datagrid('getSelected');
	
	if(selected_data.ItemLockId == 1){
		$.messager.alert('Unseccessfull','Item sedang proses Adjustment','error',function(){	
			$('#grid_pop_up').datagrid('reload'); 
		});
	}
	else{
		/*===================panggil function chose job cos dibawah========================*/
		choose_job_costing();
		/*==============================end================================================*/
	}
}
/*============================end===================================================================*/

function choose_job_costing() {
    var selected_data=$('#grid_pop_up').datagrid('getSelected');	
	
    flag = true;
    item = $('#dg_job').datagrid('getRows');

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','info',function(){	
                $('#grid_pop_up').datagrid('reload'); 	
            });
   
            flag = false;
        }
    });
	
    if(flag != false){
        var row 				= $('#dg_job').datagrid('getRows');
		//$('#dg_job').datagrid('endEdit', (row.length - 1));
		$('#dg_job').edatagrid('saveRow');
		
		$('#dg_job').datagrid('updateRow',{
			index: LoopIndexGrid,
			row: {
				ItemCode       : selected_data.ItemCode,
				Description       : selected_data.ItemDesc,
				ItemUnit : selected_data.ItemUnit,
				ItemAverage : selected_data.ItemAverage,
				ItemAverage_Real : selected_data.ItemAverage,
				ItemKategori : selected_data.ItemCategoryID,
				ItemType : selected_data.ItemStatus,
				ItemStatus : selected_data.ItemStatus,
				//Amount_item : 0
			}
		});
		$('#dg_job').datagrid('beginEdit', LoopIndexGrid);
		//$('#dg_job').datagrid('refreshRow', LoopIndexGrid);
		
		modal_close('dialog-job-cos');
    }
}

function choose_expense(){
	var selected_data=$('#tt_choose_servis').datagrid('getSelected');
	flag = true;
	
	item = $('#dg_job').datagrid('getRows');

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','info',function(){	
                $('#tt_choose_servis').datagrid('reload'); 	
            });
   
            flag = false;
        }
    });
	
	if(flag != false){
		var row 				= $('#dg_job').datagrid('getRows');
		//$('#dg_job').datagrid('endEdit', (row.length - 1));
		
		$('#dg_job').edatagrid('saveRow');
		
		$('#dg_job').datagrid('updateRow',{
			index: LoopIndexGrid,
			row: {
				ItemCode       : selected_data.ItemCode,
				Description       : selected_data.ItemDesc,
				ItemUnit : selected_data.ItemUnit,
				ItemAverage : 0,
				ItemAverage_Real : 0,
				ItemKategori : selected_data.ItemCategoryID,
				ItemType : 1,
				ItemStatus : 2,
				//Amount_item : 0
			}
		});
		
		$('#dg_job').datagrid('beginEdit', LoopIndexGrid);
		
		modal_close('dialog-job-cos');
	}
	
}

function add_jobcos(){
	if($('#cmb_jcost').combobox('getValue') == ""){
		$.messager.alert('Unseccessfull','Silahkan pilih Job Costing Account terlebih dahulu','info',function(){	
		});
	}
	else if($('#cmb_department').combobox('getValue') == ""){
		$.messager.alert('Unseccessfull','Silahkan pilih Department terlebih dahulu','info',function(){	
		});
	}
	else{
		var row 				= $('#dg_job').datagrid('getRows');
		$('#dg_job').datagrid('endEdit', (row.length - 1));
		
		$('#dg_job').edatagrid('addRow',{
			//index: 1,
			row: {
				ItemAverage     : 0,
				ItemAverage_Real     : 0,
				Qty     		: 0
			}
		});
		$('#dg_job').datagrid('beginEdit', (row.length - 1));
	}
}

function remove_row(){
	var selectedrow     = $('#dg_job').datagrid('getSelected');
	var row_index       = $('#dg_job').datagrid('getRowIndex', selectedrow);
	
	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ItemCode + '"?',  
		function(r){
			if (r){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
				$('#dg_job').datagrid('deleteRow', row_index);
				calculate_sub();
			}
		}
	);
}

function calculate_sub(){
	$('#dg_job').edatagrid('saveRow');

	var item        = $('#dg_job').datagrid('getRows');
	var sub_total   = 0;
	
	$.each(item, function(index, value) {
		if(value.Amount_item != undefined){
			sub_total+= parseFloat(value.Qty * value.ItemAverage_Real);
		}				
	});
	
	$('#sub_total').numberbox('setValue', sub_total);
	$('#sub_total_koma').numberbox('setValue',sub_total);
}
</script>