<!---- 
    Author 	: Fachri
    Tanggal : 	
    Revisi 	:
-->

	<?php
				//---------Untuk privellege jika dia ada tombol save----------
				if($group_user->SavePrivillage == 1){
					$cek_autority_save 		= "";
				}
				else{
					$cek_autority_save 		= "disabled";
				}
				
				if($group_user->DeletePrivillage == 1){
					$cek_autority_delete 		= "";
				}
				else{
					$cek_autority_delete 		= "disabled";
				}
				
				if($group_user->JournalPrivillage == 1){
					$cek_autority_journal 		= "";
				}
				else{
					$cek_autority_journal 		= "disabled";
				}
				
				//-------------end----------------------------
	?>	
	
<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- top view-->
	<div id="form-header" data-options="region:'north',border:false" >
		 <div id="form-header-wrapper">
				<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
					<tr>
						<td align="left" valign="middle">							
							<a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="location.reload()">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"><?=$menu_label?></span>
						</td>
					</tr>
				</table>		     	
		 </div>			
	</div>
	<!-- end top view -->	
	<!-- center view -->
	<div id="form-body-grey" data-options="region:'center',border:false">
		<div id="form-wrapper" class="easyui-panel"  style="width:1180%;padding:5px;" fit="true">
			<form id="fm_roll_over" name="fm_roll_over" method="post">
            <input type="hidden" id="detail" name="detail"/>
            <input type="hidden" id="Status_Tab" name="Status_Tab" value="0"/>
                <table width="1358" height="188">
                <tr>
                    <td width="160" style="width:160px;"><b>Batch No.</b></td>
                    <td width="556">
                     <select name="cmb_roll_over" class="combo-grid" id="cmb_roll_over" required="required" style="width:254px;height:20px" 
                            data-options="
                                
                                panelWidth: 254,
                                idField: 'JCostNumber',
                                textField: 'JCostNumber',
                                
                                url: '<?php echo base_url(); ?><?php echo"list/roll_over"; ?>/get_no_job_cos',
                                columns: [[
                                {
                                    field:'JCostNumber',title:'Batch Number',width:190}
                                ]],
                                fitColumns: true
                            ">
                    </select>
                       
                    </td>
                    <td width="604" rowspan="7" valign="top">
                    <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                        	<td width="441" height="20">&nbsp;</td>
                            <td width="679" ></td>
                            <td width="157" style="background:#333399;text-align:center;color:#FFF;">Roll Over Date</td>
                        </tr>
                        <tr height="20">
                        	<td>&nbsp;</td>
                            <td>
                            	
                            </td>
                            <td>
                            	<input type="text" id="txt_rolloverdate" value='<?php echo $date_now;?>' name="txt_rolloverdate" class="easyui-datebox validBackward" style="width:155px;" editable='false' required="required"/>
                            </td>
                        </tr>
                      </table>
                  </td>
                </tr>
                <tr>
                    <td><b>Job Cost Date</b></td>
                    <td><input type="text" id="txt_date" name="txt_date" readonly value="" style="width:251px;background-color:#E6E6E6;" /></td>
                  </tr>
                <tr>
                    <td><b>Job Cost Account</b></td>
                    <td>
                        <input type="text" id="txt_jobcostaccountno" name="txt_jobcostaccountno" readonly value="" style="width:151px;background-color:#E6E6E6;" /> <input type="text" id="txt_jobcostaccountnodesc" name="txt_jobcostaccountnodesc" readonly value="" style="width:251px;background-color:#E6E6E6;" />
                   	</td>
                  </tr>
                <tr>
                    <td valign="top"><b>Description</b></td>
                    <td >
                    	<textarea name="txt_jobcostdesc" cols="55.5" rows="4" ></textarea>
                    </td>
                  </tr>
                <tr>
                    <td height="30"><b>Total Job Cost</b></td>
                    <td>
                        <input type="text" name="txt_totaljobcost" id="txt_totaljobcost" class="easyui-numberbox" style="text-align:right;" 
                                                    data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" readonly/>
                        
                        <input type="text" name="txt_totaljobcost_no_koma" id="txt_totaljobcost_no_koma" class="easyui-numberbox" style="text-align:right;" hidden
                        data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                        formatter:function(v){
                            return v;
                        },
                        parser:function(s){
                            var v = parseFloat(s);
                            return v;
                        }
                        "  readonly/>
                                                    
                        <input type="text" name="txt_total_allocated" id="txt_total_allocated" class="easyui-numberbox" style="text-align:right;" hidden
                                                     readonly/>
                        <input type="text" name="txt_amount_allocated" id="txt_amount_allocated" class="easyui-numberbox" style="text-align:right;" hidden
                        data-options="min:0,decimalSeparator:',',groupSeparator:'.',
                        formatter:function(v){
                            return v;
                        },
                        parser:function(s){
                            var v = parseFloat(s);
                            return v;
                        }
                        "  readonly/>
                    </td>
                  </tr>
                </table>
                <!-- Datagrid -->
          <div class="easyui-tabs" data-options="tabHeight:18" id="tab_roll_over">
                    <div title="To Account">
					<table id="dg_rol_over" class="easyui-edatagrid"
                             toolbar="#tb_dg_rol_over" idField="id"
                             rownumbers="true" fitColumns="true" 
                             singleSelect="true" style="height:280px;"
                        >
                            <thead>
                            <tr>
                                <th align="center" valign="middle" data-options="field:'AccountNo',width:100,align:'left'" editor="{type:'AccountNo',options:{required:true,disabled:true}}">Account No</th>
                                <th align="center" valign="middle" data-options="minValue:0,field:'AccountName',width:120,align:'center',editor:{type:'AccountName'}">Account Name</th>
                                <th align="center" valign="middle" data-options="field:'DepartmentName',width:150,align:'left'" editor="{type:'DepartmentName',options:{required:true,disabled:true}}">Department</th>
                                <th align="center" valign="middle" data-options="field:'DepartmentCode',width:150,align:'left'" editor="{type:'DepartmentCode'}" hidden='true'>Department Code</th>
                            </tr>
                            </thead>
                      </table>
                        <!-- Datagrid Toolbar -->
                        <div id="tb_dg_rol_over">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="new_grid_for_to_akun();">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row_akun();">Delete</a>
                        </div>
                
</div>
                  <div title="To Items">
                  	<table id="dg_rol_over_item" class="easyui-edatagrid"
                             toolbar="#tb_dg_rol_over_item" idField="id"
                             rownumbers="true" fitColumns="true" 
                             singleSelect="true" style="height:280px;"
                        >
                            <thead>
                            <tr>
                                <th field="ItemCode"         width="50"  data-options="align:'left',halign:'center'"   editor="{type:'ItemCode',options:{disabled:true,required:true}}">Item</th>
                                <th field="ItemDesc"         width="130" data-options="align:'left',halign:'center'"   editor="{type:'ItemDesc',options:{disabled:true}}">Description</th>
                                <th field="ItemQty"          width="30"  data-options="align:'center',halign:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true,min:1}}" formatter="formatRp">Qty</th>
                                <th field="ItemUnit"         width="25"  data-options="align:'center',halign:'center'" editor="{type:'ItemUnit'}">Unit</th>
                                <th field="Allocated"        width="50"  data-options="align:'center',halign:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true,min:1}}" formatter="formatRp">Allocated Value</th>
                                <th field="HPPNew"        	 width="50"  data-options="align:'center',halign:'center'" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',required:true,min:0,disabled:true}}" formatter="formatRp">Amount</th>
                             	<th field="ItemCategoryID"   width="50"  data-options="align:'left',halign:'center'"   editor="{type:'ItemCategoryID',options:{disabled:true}}" hidden>ItemCategoryID</th>
                                <th field="HPPNew_Los"       width="50"  data-options="align:'center',halign:'center'" editor="{type:'numberbox',options:{decimalSeparator:',',
                                formatter:function(v){
                                	return v;
                                },
                                parser:function(s){
                                	var v = parseFloat(s);
                                    return v;
                                }
                                }}" hidden>Amount No Precision</th>
                            </tr>
                            </thead>
</table>
                        
                        <!-- Datagrid Toolbar -->
                        <div id="tb_dg_rol_over_item">
                            <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="new_grid()">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row_item();">Delete</a>
                        </div>
                  </div>
				</div>
            </form>
		</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_data();" <?php echo $cek_autority_save;?>>Save</a> 
                         <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="parent.tab_close('tt')">Cancel</a>
					</td>
				 </tr>
			</table>				
		</div>	
</div>

<!-- dialog select expense  cashbon list-->
<!-- Job Costing pop -->
<div id="dialog-akun-roll-over" 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="#dialog-roll-over-akun">
    <form id="form_roll_over_akun" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <tr>
                <td width="308"><b>Account Number</b></td>
                <td width="10"><b>:</b></td>
                
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_AccountNumber" id="txt_AccountNumber" class="easyui-validatebox" placeholder="Account Number" onkeyup="search_akun();" />
                </td>
            </tr>
            
            <tr>
                <td><b>Account Name</b></td>
                <td><b>:</b></td>
            
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_AccountName" id="txt_AccountName" class="easyui-validatebox" placeholder="Account Name" onkeyup="search_akun();" />
                </td>
            </tr>
            
            <tr>
                <td colspan="3">
                    <table id="grid_akun" class="easyui-datagrid" singleSelect="true"  rownumbers="true" style="width:828px;height:385px;"
                       url="<?php echo base_url(); ?>list/roll_over/get_akun_rol_over" 
                        pageSize="50" 
                        data-options = "border:true,view:scrollview,autoRowheight:false"
                    >
                        <thead>
                            <tr bgcolor="">
                                <th halign="center" field="AccountNumber" 		width="150" sortable="true">Account Number</th>
                                <th halign="center" field="AccountName" 		width="600" sortable="true">Account Name</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-roll-over-akun" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_akun();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-akun-roll-over')">Close</a>
</div>

<!-- dialog select department-->
<div id="dialog-data-department" 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="#dialog-button-department">
    <form id="form_department" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <!--<tr>
                <td width="308"><b>Department Code</b></td>
                <td width="10"><b>:</b></td>
                
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_DepartmentCode" id="txt_DepartmentCode" class="easyui-validatebox" placeholder="Department Code" onkeyup="search_all_item();" />
                </td>
            </tr>
            
            <tr>
                <td><b>Department Number</b></td>
                <td><b>:</b></td>
            
                <td>
                    <input type="text" style="width:500px;height:20px" name="txt_DepartmentName" id="txt_DepartmentName" class="easyui-validatebox" placeholder="Department Number" onkeyup="search_all_item();" />
                </td>
            </tr>-->
            
            <tr>
                <td colspan="3">
                    <table id="dg_department" class="easyui-datagrid" singleSelect="true"  rownumbers="true" style="width:828px;height:440px;"
                       url="<?php echo base_url(); ?>list/roll_over/department/<?php echo $this->session->userdata('company');?>" 
                        pageSize="50" 
                        data-options = "border:true,view:scrollview,autoRowheight:false"
                    >
                        <thead>
                            <tr bgcolor="">
                                <th halign="center" field="DepartmentCode" 		width="150" sortable="true">Department Code</th>
                                <th halign="center" field="DepartmentName" 		width="620" sortable="true">Department Name</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-button-department" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_department();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-data-department')">Close</a>
</div>

<!-- ---------------------------------------rol over pop -------------------------------------------------------------------------------------------------->
<div id="dialog-item-rol-over" 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="#roll_over">
    <form id="form_roll_over" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <!-----------tabel item kategori dari sini------------------------------------------------------------------------------->
            <tr>
                <td width="308"><b>Item Category</b></td>
                <td width="10"><b>:</b></td>

                <td>
                    <select name="cmb_ItemCategoryID" id="cmb_ItemCategoryID" class="easyui-combobox" style="width:502px;" editable="false" 
                        data-options="
                            valueField:'ItemCategoryID',
                            textField:'ItemCategoryName',
                            url:'<?=base_url()?>custom/get_data/itemcategory_no_asset',
                            onSelect: function(rec){
                                var id = rec.ItemCategoryID;
                                search_all_item();
                            }
                        ">
                    </select>
                </td>
            </tr>
			<!----------------------------end---------------------------------------------------------------------------------------->
            <tr>
                <td><b>Item Code</b></td>
                <td width="10"><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>Item Name</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>
            
            <tr>
                <td colspan="3">
                    <table id="grid_pop_up" class="easyui-datagrid" singleSelect="true"  rownumbers="true" style="width:828px;height:360px;"
                       url="<?Php echo base_url()?>list/roll_over/search_roll_over"
                        pageSize="150" 
                        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="400" sortable="true">Name</th>
                                <th halign="center" field="ItemCategoryName" 	width="200" sortable="true">Item Category Name</th>
                                
                                <th halign="center" field="ItemUnit" 		width="70" sortable="true" 		hidden>Item Unit</th>
                                <th halign="center" field="ItemCategoryID" 	width="200" sortable="true" 	hidden>ItemCategoryID</th>
                                <th halign="center" field="ItemQty" 		width="200" sortable="true" 	hidden>Item Qty</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="roll_over">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_item();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-item-rol-over');">Cancel</a>
</div>
<!--------------------------------------------------end---------------------------------------------------------------------------------------------------->
<?php $this->load->view('v_custom_pop_up');?>
<script>
$(document).ready(function(){
	$('#tab_roll_over').tabs({
		border:true,
		onSelect:function(title){
			if(title == "To Account"){
				$('#dg_rol_over').datagrid({
					onBeginEdit:function(rowIndex){
						editors = $('#dg_rol_over').datagrid('getEditors',rowIndex);
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('disable');
						/*=================================end============================================================*/
					},
					onEndEdit:function(){
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('enable');
						/*=================================end============================================================*/
					}
				});
				
				$('#fm_roll_over').click(function(){
					$('#dg_rol_over').edatagrid('saveRow');
				});
				
				$('#Status_Tab').val(0);
			}
			else{
				$('#dg_rol_over_item').datagrid({
					onBeginEdit:function(rowIndex){
						editors 		= $('#dg_rol_over_item').datagrid('getEditors',rowIndex);
						var n4 			= $(editors[4].target);
						var n5 			= $(editors[5].target); 
						var n7 			= $(editors[7].target); 
						
						var total_job_cost 	= $('#txt_totaljobcost_no_koma').numberbox('getValue');
						//n5.numberbox('setValue',0);
						n4.numberbox({
							onChange:function(){
								if(n4.numberbox('getValue') > 100){
									$.messager.alert('Unseccessfull','Maaf Allocated value tidak boleh melebihi 100 %','error',function(){	
										n4.numberbox('setValue', 0);
									});
								}
								else{
									total_amount = parseFloat(total_job_cost) * parseFloat(n4.numberbox('getValue')) / 100;
									n5.numberbox('setValue', total_amount);
									n7.numberbox('setValue', total_amount);
								}
								
							}
						});
			
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('disable');
						/*=================================end============================================================*/
					},
					onEndEdit:function(){
						var item        = $('#dg_rol_over_item').datagrid('getRows');
                		var sub_total   = 0;
						var total_amount = 0; 
						$.each(item, function( index, value ) {
							if(value.Allocated != undefined){
								sub_total+= parseFloat(value.Allocated);
								total_amount+= parseFloat(value.HPPNew_Los);
							}				
						});
		
						$('#txt_total_allocated').numberbox('setValue',sub_total);
						$('#txt_amount_allocated').numberbox('setValue',total_amount);
						
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('enable');
						/*=================================end============================================================*/
					}
				});
				
				$('#fm_roll_over').click(function(){
					$('#dg_rol_over_item').edatagrid('saveRow');
				});
				$('#Status_Tab').val(1);
			}
		}
	});
	
	var BranchCode = '<?php echo $this->session->userdata("branch_status");?>';
	if(BranchCode == 0){
		$('#tab_roll_over').tabs('enableTab', 'To Items');
	   	$('#tab_roll_over').tabs('enableTab', 'To Account');
	}
	else{
		$('#tab_roll_over').tabs('disableTab', 'To Items');
	   	$('#tab_roll_over').tabs('enableTab', 'To Account');
	}
	
	if($('#Status_Tab').val() == 0){
		$('#dg_rol_over').datagrid({
			onBeginEdit:function(rowIndex){
				editors = $('#dg_rol_over').datagrid('getEditors',rowIndex);
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('disable');
				/*=================================end============================================================*/
			},
			onEndEdit:function(){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
			}
		});
		
		$('#fm_roll_over').click(function(){
			$('#dg_rol_over').edatagrid('saveRow');
		});
	}
	else{
		/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
		$('.save-button').linkbutton('disable');
		/*=================================end============================================================*/
		
		$('#fm_roll_over').click(function(){
			$('#dg_rol_over_item').edatagrid('saveRow');
		});
	}
	
});

$('#cmb_roll_over').combogrid({
	onSelect: function(rowIndex, rowData){	
		$('#txt_jobcostaccountno').val(rowData.JCostAccount); 
		$('#txt_jobcostaccountnodesc').val(rowData.AccountName); 
		$('#txt_date').val(rowData.JCostDate); 
		$('#txt_jobcostdesc').val(rowData.JCostDesc);
		$('#txt_totaljobcost').numberbox('setValue',rowData.JCostSubTotal);
		$('#txt_totaljobcost_no_koma').numberbox('setValue',rowData.JCostSubTotal);
		
		if($('#Status_Tab').val() == 0){
			$('#dg_rol_over').datagrid('loadData', {'total':0,'rows':[]});
			$('#tab_roll_over').tabs('enableTab', 'To Items');
		}
		else{
			$('#dg_rol_over_item').datagrid('loadData', {'total':0,'rows':[]});
			$('#tab_roll_over').tabs('enableTab', 'To Account');
		}
	}
});
function open_accoun(){
	modal_open('Select Account','dialog-akun-roll-over');
}

function open_department(){
	modal_open('Select Department','dialog-data-department');
}

function open_item(){
	modal_open('Select Item','dialog-item-rol-over');
}

function choose_akun(){
	var selected_data = $('#grid_akun').datagrid('getSelected');
	$('.AccountName').val(selected_data.AccountName);
	$('.AccountNo').val(selected_data.AccountNumber);

	modal_close('dialog-akun-roll-over');
	
	/*=================================saat button ok di klik maka langsung disable to item di tab=====================*/
	$('#tab_roll_over').tabs('disableTab', 'To Items');
	/*======================================end========================================================================*/
}

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

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','error',function(){	
                $('#grid_pop_up').datagrid('reload'); 	
            });
   
            flag = false;
        }
    });
	
	if(flag != false){
		$('.ItemCode').val(selected_data.ItemCode);
		$('.ItemDesc').val(selected_data.ItemDesc);
		$('.ItemUnit').val(selected_data.ItemUnit);
		$('.ItemCategoryID').val(selected_data.ItemCategoryID);
	
		modal_close('dialog-item-rol-over');
	}
	/*=================================saat button ok di klik maka langsung disable to item di tab=====================*/
	$('#tab_roll_over').tabs('disableTab', 'To Account');
	/*======================================end========================================================================*/
}

function choose_department(){
	var selected_data = $('#dg_department').datagrid('getSelected');
	$('.DepartmentName').val(selected_data.DepartmentName);
	$('.DepartmentCode').val(selected_data.DepartmentCode);

	modal_close('dialog-data-department');
}

function save_data(){
	if($('#Status_Tab').val() == 0){
		var sub_total   = 0;
		flag = true;
		item = $('#dg_rol_over').datagrid('getRows');
	
		$.each(item, function(index,value){
			if(value.DepartmentName == ""){
				$.messager.alert('Unseccessfull','form tidak lengkap, silahkan Lengkapi form anda','error',function(){	
					 $('#dg_rol_over').datagrid('reload'); 	
				});
	   
				flag = false;
			}
			
			if(value.AccountNo == ""){
				$.messager.alert('Unseccessfull','form tidak lengkap, silahkan Lengkapi form anda','error',function(){	
					 $('#dg_rol_over').datagrid('reload'); 	
				});
	   
				flag = false;
			}
		});
		
		if(flag != false){
			$('#dg_rol_over').edatagrid('saveRow');	
			var j			= $('#dg_rol_over').datagrid('getRows');
			var rol_over 	= JSON.stringify(j);
			$("#detail").val(rol_over);
			save_transaction('<?php echo $menu_link?>','#fm_roll_over');
		}
	}
	else{
		if($('#txt_total_allocated').numberbox('getValue') == 100){
			flag = true;
			item = $('#dg_rol_over_item').datagrid('getRows');
		
			$.each(item, function(index,value){
				if(value.ItemCode == ""){
					$.messager.alert('Unseccessfull','form tidak lengkap, silahkan Lengkapi form anda','error',function(){	
						 $('#dg_rol_over_item').datagrid('reload'); 	
					});
		   
					flag = false;
				}
				
				if(value.ItemQty == ""){
					$.messager.alert('Unseccessfull','form tidak lengkap, silahkan Lengkapi form anda','error',function(){	
						 $('#dg_rol_over_item').datagrid('reload'); 	
					});
		   
					flag = false;
				}
			});
			if(flag != false){
				$('#dg_rol_over_item').edatagrid('saveRow');	
				var j			= $('#dg_rol_over_item').datagrid('getRows');
				var rol_over 	= JSON.stringify(j);
				$("#detail").val(rol_over);
				save_transaction('<?php echo $menu_link?>','#fm_roll_over');
			}
		}
		else{
			$.messager.alert('error','Allocated Value total tidak 100%, silahkan cek kembali form anda, .','error',function(){	
                $('#dg_rol_over_item').datagrid('reload'); 	
            });
		}
	}
}

$.extend($.fn.datagrid.defaults.editors, {
	ItemCategoryID: {
		init: function(container, options){
			var input = $('<input type="text" class="ItemCategoryID" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	ItemUnit: {
		init: function(container, options){
			var input = $('<input type="text" class="ItemUnit" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	ItemCode: {
		init: function(container, options){
			var input = $('<input type="text" class="ItemCode" onClick="open_item()" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	ItemDesc: {
		init: function(container, options){
			var input = $('<input type="text" class="ItemDesc" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	AccountName: {
		init: function(container, options){
			var input = $('<input type="text" class="AccountName" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	AccountNo: {
		init: function(container, options){
			var input = $('<input type="text" class="AccountNo" onClick="open_accoun()" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	DepartmentName: {
		init: function(container, options){
			var input = $('<input type="text" class="DepartmentName" onClick="open_department()" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	DepartmentCode: {
		init: function(container, options){
			var input = $('<input type="text" class="DepartmentCode" readonly />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	}
});

/*
	function ini jika tab berada di to akun dan ingin menambahkan lebih dari 1 row langsung di blok
*/
function new_grid_for_to_akun(){
	var item = $('#dg_rol_over').edatagrid('getData').total;
	if($('#txt_totaljobcost').numberbox('getValue') == ""){
		$.messager.alert('Warning','Silahkan pilih terlebih dahulu Batch No','error');
	}
	else if(item == 0){
		$('#dg_rol_over').edatagrid('addRow');
	}
	else{
		$.messager.alert('Warning','Anda tidak dapat menambah data akun, maximum akun adalah satu (1)','error');
	}
}
/*==============================================end==================================================*/

/*
	function ini saat di klik tombol delete, row langsung ke delete dan tab to item enable lagi
*/
function remove_row_akun(){
	var selectedrow     = $('#dg_rol_over').datagrid('getSelected');
	var row_index       = $('#dg_rol_over').datagrid('getRowIndex', selectedrow);
	
	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus row ini',  
		function(r){
			$('#dg_rol_over').edatagrid('saveRow');	
			if (r){
				$('#tab_roll_over').tabs('enableTab', 'To Items');
				
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
				
				$('#dg_rol_over').datagrid('deleteRow', row_index);
				
			}
		}
	);
}
/*==========================================end================================================*/

/*
	function ini saat di klik tombol delete, row langsung ke delete dan tab to akun enable lagi
*/
function remove_row_item(){
	var selectedrow     = $('#dg_rol_over_item').datagrid('getSelected');
	var row_index       = $('#dg_rol_over_item').datagrid('getRowIndex', selectedrow);
	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.ItemCode + '"?',  
		function(r){
			$('#dg_rol_over_item').edatagrid('saveRow');	
			if (r){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
				var item = $('#dg_rol_over_item').datagrid('getData').total;
				if(item == 1){
					/*=================================saat button ok di klik maka langsung disable to item di tab=====================*/
					$('#tab_roll_over').tabs('enableTab', 'To Account');
					/*======================================end========================================================================*/
				}
				$('#dg_rol_over_item').datagrid('deleteRow', row_index);
				calculate_sub();
			}
		}
	);
}
/*==========================================end================================================*/

function calculate_sub(){
	$('#dg_rol_over_item').edatagrid('saveRow');
	
	var item        = $('#dg_rol_over_item').datagrid('getRows');
	var sub_total   = 0;
	var total_allocated = 0;

	$.each(item, function(index, value) {
		total_allocated	+= parseFloat(value.Allocated);
		sub_total		+= parseFloat(value.HPPNew);				
	});
	$('#txt_total_allocated').numberbox('setValue',total_allocated);
	$('#txt_amount_allocated').numberbox('setValue', sub_total);
}

function new_grid(){
	if($('#txt_totaljobcost').numberbox('getValue') == ""){
		$.messager.alert('Warning','Silahkan pilih terlebih dahulu Batch No','error');
	}
	else{
		$('#dg_rol_over_item').edatagrid('addRow')
	}
}
</script>