<!---- 
    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";
			}

            if($group_user->ReversePrivillage == 1){
                $cek_autority_reverse  = "";
            }
            else{
                $cek_autority_reverse    = "display:none";
            }
			
			//-------------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" width="30%">							
							<input type="checkbox" class="easyui-checkbox" name="chk_suspended" id="chk_suspended" style="margin-top:0px;margin-right:1px;" onClick="suspend(1)" <?php if($detail->TransStatus == 3){ echo"checked"; } ?> <?=(!in_array($detail->TransStatus,array(4,6)) || $detail->AppLevel1Status == 1) ? 'disabled' : ''?> <?php echo $cek_autority_delete;?>/>
                    		<span style="display:inline-block;margin-top:2px;<?=(($detail->AppLevel1Status!=4)?'color:gray;':'')?>"><?=(($detail->TransStatus!=3)?"Suspend":"Suspend")?> </span>
                        	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-journal" plain="true"  id="btn_journal" 
								onclick="parent.tab_add('Preview Journal <?=$menu_label?>', '<?php echo $menu_link; ?>', 'icon-print', 'to_preview_journal/<?php echo $detail->ROvrNumber ?>')" <?php echo $cek_autority_journal;?>>Journal
							</a>
                            <a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
							
						</td>	
                         <td align="center" width="40%"><span id="approval_information">
							<?php
                            if($detail->TransStatus==4){
                                echo get_status_approval_header($detail->TransStatus);
                            }
                            ?>
                            </span>
                        </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">
        <?php if(in_array($detail->TransStatus,array(1,2,3,9))){ ?>
                	<div class="blanket"></div>
                	<div class="status-box"><b><?php if($detail->TransStatus==2){ echo 'Finished'; }elseif($detail->TransStatus==3){ echo 'Suspended'; }elseif($detail->TransStatus==1){ echo 'On Progres'; }elseif($detail->TransStatus==9){ echo 'Canceled by Dir.'; }else{ echo 'Unknown'; } ?></b></div>
            	<?php } ?>
			<form id="fm_roll_over" name="fm_roll_over" method="post">
            <input type="hidden" id="detail" name="detail"/>
            <input type="hidden" name="detail_id" id="detail_id">
            <input type="hidden" id="Status_Tab" name="Status_Tab" value="<?php echo $detail->ROvrStatus;?>"/>
            <input type="hidden" id="id" name="id" value="<?php echo $detail->ROvrNumber;?>"/>
            <input type="hidden" id="JCostNumber" name="JCostNumber" value="<?php echo $detail->JCostNumber;?>"/>
            
                <table width="1350" height="188">
                <tr>
                    <td style="width:160px;"><b>Batch No.</b></td>
                    <td>
                     <select name="cmb_roll_over" class="easyui-combogrid" id="cmb_roll_over" required="required" style="width:254px;height:20px" readonly
                            data-options="
                            	value:'<?php echo $detail->JCostNumber;?>',
                                editable:false,
                                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="553" rowspan="7" valign="top">
                    <table width="102%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                        	<td width="180">&nbsp;</td>
                            <td width="185" style="background:#333399;text-align:center;color:#FFF;">Roll Over Number</td>
                            <td width="155" style="background:#333399;text-align:center;color:#FFF;">Roll Over Date</td>
                        </tr>
                        <tr height="20">
                        	<td>&nbsp;</td>
                            <td>
                            	<input type="text" name="txt_rol_over_no" id="txt_rol_over_no" class="easyui-validatebox" style="width:222px;background-color:#E3E2E2;text-align:center;" required="required" readonly value="<?php echo $detail->ROvrNumber;?>"/>
                            </td>
                            <td>
                            	<input type="text" id="txt_rolloverdate" value="<?php echo $detail->ROvrDate;?>" name="txt_rolloverdate" class="easyui-datebox validBackward" style="width:155px;" editable='false' required="required" readonly="readonly"/>
                            </td>
                        </tr>
                      </table>
                  </td>
                </tr>
                <tr>
                    <td><b>Job Cost Date</b></td>
                    <td><input type="text" id="txt_date" name="txt_date" readonly style="width:251px;background-color:#E6E6E6;" value="<?php echo date("d-m-Y", strtotime($detail->JCostDate));?>"/></td>
                  </tr>
                <tr>
                    <td><b>Job Cost Account</b></td>
                    <td>
                        <input type="text" id="txt_jobcostaccountno" name="txt_jobcostaccountno" readonly style="width:151px;background-color:#E6E6E6;" value="<?php echo $detail->ROvrAccountNumber;?>"/> <input type="text" id="txt_jobcostaccountnodesc" name="txt_jobcostaccountnodesc" readonly style="width:251px;background-color:#E6E6E6;" value="<?php echo $detail->AccountName;?>"/>
                   	</td>
                  </tr>
                <tr>
                    <td valign="top"><b>Description</b></td>
                    <td >
                    	<textarea name="txt_jobcostdesc" cols="55.5" rows="4" ><?php echo $detail->ROvrDesc;?></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;" value="<?php echo $detail->JCostSubTotal;?>"
                                                    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;" value="<?php echo $detail->JCostSubTotal;?>" 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;" value="<?php echo $detail->ROvrTotalAmount;?>" 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-<?=(in_array($detail->TransStatus,array(3,1,2)))?'datagrid':'edatagrid'?>"
                             toolbar="#tb_dg_rol_over" idField="id"
                             rownumbers="true" fitColumns="true" 
                             url="<?php echo base_url(); ?>list/roll_over/get_edit_grid/<?php echo $detail->ROvrNumber;?>/<?php echo $detail->ROvrStatus;?>" 
                             singleSelect="true" style="height:280px;"
                        >
                            <thead>
                            <tr>
                                <th align="center" valign="middle" data-options="field:'AccountNumber',width:100,align:'left'" editor="{type:'AccountNo',options:{required:true}}">Account No</th>
                                <th align="center" valign="middle" data-options="minValue:0,field:'AccountName',width:120,align:'center',editor:{type:'AccountName',options:{required:true}}">Account Name</th>
                                <th align="center" valign="middle" data-options="field:'DepartmentName',width:150,align:'left'" editor="{type:'DepartmentName',options:{required:true}}">Department</th>
                                <th align="center" valign="middle" data-options="field:'DepartmentCode',width:150,align:'left'" editor="{type:'DepartmentCode',options:{required:true}}" hidden='true'>Department Code</th>
                                <th align="center" valign="middle" data-options="field:'ROvrDetailID',width:150,align:'left'" hidden>ROvr Detail ID</th>
                                <th align="center" valign="middle" data-options="field:'RowStatus',width:150,align:'left'" hidden>Row Status</th>
                            </tr>
                            </thead>
                        </table>
                        <!-- Datagrid Toolbar -->
                        <div id="tb_dg_rol_over">
                            <!--<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg_rol_over').edatagrid('addRow')">New</a>-->
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">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;"
                             url="<?php echo base_url(); ?>list/roll_over/get_edit_grid/<?php echo $detail->ROvrNumber;?>/<?php echo $detail->ROvrStatus;?>" 
                        >
                            <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',options:{disabled:true}}">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:{required:true,disabled:true}}" hidden>ItemCategoryID</th>
                                <th field="ROvrDetailID"   	width="50"  data-options="align:'left',halign:'center'"   hidden>ROvr Detail ID</th>
                                <th align="center" valign="middle" data-options="field:'RowStatus',width:150,align:'left'" hidden>Row Status</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="javascript:$('#dg_rol_over_item').edatagrid('addRow')">New</a>-->
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">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 app-button" iconCls="icon-ok" onclick="approval_roll();" <?= (($this->session->userdata("jobtitle_status") != 1 || !in_array($detail->TransStatus, array(4,6))) || (date('Y-m-d') < date('Y-m-d', strtotime($detail->ROvrNumber))))?'disabled':'' ?>>App. Mgr</a>
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-reload" onclick="app_reverse()" <?=(in_array($detail->TransStatus,array(1,2)))?'':'disabled'?> style="<?= $cek_autority_reverse;?>" >Reverse</a>
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="save_data();"  <?=(in_array($detail->TransStatus,array(0,1,2,3)))?'disabled':''?> <?php echo $cek_autority_save;?>>Save</a> 								
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="print_detail()" <?=(in_array($detail->TransStatus,array(0,1,2,3,9,8)))?'':'disabled'?>>Preview & Print</a>   
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="<?=($this->session->userdata('jobtitle_status')==0)?'suspend(2)':'parent.tab_close(\'tt\')'?>" <?=(!in_array($detail->TransStatus,array(0,4,6)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>
                </td>
             </tr>
        </table>				
    </div>		
</div>

<!-- dialog select expense  cashbon list-->
<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/ajax/<?php echo $this->session->userdata('company');?>" 
                        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="620" 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',
                            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/job_costing/search_job_cos"
                        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="620" sortable="true">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="200" sortable="true" 	hidden>Item Qty</th>
                                <th halign="center" field="ItemLockId" 		width="200" sortable="true" 	hidden>Item Lock Id</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>
/*========================deklar variabel array for remove===================*/
var array_item = [];
/*==============================end=========================================*/
$(document).ready(function(){
	$('#tab_roll_over').tabs({
		border:true,
		onSelect:function(title){
			if(title == "To Account"){
				$('#Status_Tab').val(0);
			}
			else{
				$('#Status_Tab').val(1);
			}
		}
	});
	
	/*====================================jika dia branch maka disable item===========================*/
	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');
	}
	/*===========================end=================================================================*/
	
	/*====================================jika dia branch maka disable item===========================*/

	var ROvrStatus = '<?php echo $detail->ROvrStatus;?>';
	if(ROvrStatus == 0){
		$('#tab_roll_over').tabs('disableTab', 'To Items');
	   	$('#tab_roll_over').tabs('enableTab', 'To Account');
		$('#tab_roll_over').tabs('select', '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');
				$('.app-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{
		$('#tab_roll_over').tabs('enableTab', 'To Items');
	   	$('#tab_roll_over').tabs('disableTab', 'To Account');
		$('#tab_roll_over').tabs('select', 'To Items');
		
		$('#dg_rol_over_item').datagrid({
			onLoadSuccess: 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);
					}				
				});

				$('#txt_total_allocated').numberbox('setValue',sub_total);
				$('#txt_amount_allocated').numberbox('setValue',total_amount);
			},
			onBeginEdit:function(rowIndex){
				editors = $('#dg_rol_over_item').datagrid('getEditors',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');
				
				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');
				$('.app-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');
		});
	}
	/*====================================end=======================================================*/
});

$('#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);
	}
});
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(){
	var url             = '<?= base_url();?>custom/custom/checking_update';
	var no_transaksi    = $('#id').val();
	var trans_status 	= '<?=$detail->TransStatus?>';
	
	var created_by 		= '<?php echo $detail->CreatedBy;?>';
	var login_as 			= '<?php echo $this->session->userdata('username');?>';
	var job_title 			= '<?php echo $this->session->userdata('jobtitle_status');?>'; 
	
	if($('#Status_Tab').val() == 0){
		$('#dg_rol_over').edatagrid('saveRow');	
		var j			= $('#dg_rol_over').datagrid('getRows');
		var rol_over 	= JSON.stringify(j);
		$("#detail").val(rol_over);
		edit_transaction_check('<?php echo $menu_link;?>', '#fm_roll_over', no_transaksi, url, trans_status,created_by, login_as, job_title);
	}
	else{
		if($('#txt_total_allocated').numberbox('getValue') == 100){
			flag = true;
			item = $('#dg_rol_over_item').datagrid('getRows');
			
			$.each(item, function(index,value){
				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){
				if(parseFloat($('#txt_amount_allocated').numberbox('getValue')) == parseFloat($('#txt_totaljobcost_no_koma').numberbox('getValue'))){
					$('#dg_rol_over_item').edatagrid('saveRow');	
					var j			= $('#dg_rol_over_item').datagrid('getRows');
					var rol_over 	= JSON.stringify(j);
					$("#detail").val(rol_over);
					edit_transaction_check('<?php echo $menu_link;?>', '#fm_roll_over', no_transaksi, url, trans_status,created_by, login_as, job_title);
				}
				else{
					$.messager.alert('error','Total amount Roll Over tidak sama dengan total Job Costing','error',function(){	
                	
            		});
				}
			}
		}
		else{
			$.messager.alert('error','Allocated Value total tidak 100%, silahkan cek kembali form anda, .','error',function(){	
                	
            });
		}
	}
	
	
}

function remove_row(){
	if($('#Status_Tab').val() == 0){
		var item = $('#dg_rol_over').datagrid('getData').total;
	
		if(item <= 1){
			$.messager.alert('Warning','Anda tidak dapat menghapus list item, minimum list item adalah satu (1)','error');
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('enable');
			/*=================================end============================================================*/
			$('#dg_rol_over').edatagrid('saveRow');	
		}
		else {
			var selectedrow     = $('#dg_rol_over').datagrid('getSelected');
			var row_index       = $('#dg_rol_over').datagrid('getRowIndex', selectedrow);
		
			$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus akun "' + selectedrow.AccountNumber + '"',  
				function(r){
					if (r){
						array_item.push(selectedrow.ROvrDetailID);
						$("#detail_id").val(JSON.stringify(array_item));
						$('#dg_rol_over').datagrid('deleteRow', row_index);
						calculate_sub();	
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('enable');
						/*=================================end============================================================*/	
					}
				}
			);		
		}
	}
	else{
		var item = $('#dg_rol_over_item').datagrid('getData').total;
	
		if(item <= 1){
			$.messager.alert('Warning','Anda tidak dapat menghapus list item, minimum list item adalah satu (1)','error');
			/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
			$('.save-button').linkbutton('enable');
			/*=================================end============================================================*/
			$('#dg_rol_over_item').edatagrid('saveRow');	
		}
		else {
			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){
					if (r){
						array_item.push(selectedrow.ROvrDetailID);
						$("#detail_id").val(JSON.stringify(array_item));
						$('#dg_rol_over_item').datagrid('deleteRow', row_index);
						calculate_sub();
						/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
						$('.save-button').linkbutton('enable');
						/*=================================end============================================================*/		
					}
				}
			);		
		}
	}
}

function suspend(type){
	var created_by 		= '<?php echo $detail->CreatedBy;?>';
	var login_as 		= '<?php echo$this->session->userdata('username');?>';
	var job_title 		= '<?php echo $this->session->userdata('jobtitle_status');?>';
	var url             = '<?= base_url();?>custom/custom/checking_suspend';
	
	suspend_check_trans('F', type, '<?=$menu_link?>', $('#id').val(), 'Roll Over', url, created_by, login_as, job_title);
}

$.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" 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" 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 approval_roll() {
	var url_check           = '<?= base_url();?>custom/custom/checking_approval';
	var no_transaksi    	= $('#id').val();
	
	checking_approval(no_transaksi,function(check){
		if(check.status == false){
			app_mgr();
		}else{
			$.messager.alert('Unseccessfull',check.message,'error',function(){
				location.reload();
			});
		}
	});
}

function app_mgr(){
	if($('#Status_Tab').val() == 0){
		$('#dg_rol_over').edatagrid('saveRow');	
		var j			= $('#dg_rol_over').datagrid('getRows');
		var rol_over 	= JSON.stringify(j);
		$("#detail").val(rol_over);
		custom_data('<?=$menu_link?>/approval_mgr','#fm_roll_over');
	}
	else{
		if(parseFloat($('#txt_amount_allocated').numberbox('getValue')) == parseFloat($('#txt_totaljobcost_no_koma').numberbox('getValue'))){
			$('#dg_rol_over_item').edatagrid('saveRow');	
			var j			= $('#dg_rol_over_item').datagrid('getRows');
			var rol_over 	= JSON.stringify(j);
			$("#detail").val(rol_over);
			custom_data('<?=$menu_link?>/approval_mgr','#fm_roll_over');
		}
		else{
			$.messager.alert('error','Total Job Costing tidak sama dengan Amount, silahkan cek kembali form anda','error',function(){	
                $('#dg_rol_over_item').datagrid('reload'); 	
            });
		}
	}
}

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

	var item        = $('#dg_rol_over_item').datagrid('getRows');
	var sub_total   = 0;
	var total_amount = 0; 
	$.each(item, function( index, value ) {
		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);
	
}

function print_detail(){
	if($('#Status_Tab').val() == 0){
		parent.tab_edit('Print Roll Over', '<?php echo $menu_link; ?>', 'icon-print', 'to_print_detail_akun','<?php echo $detail->ROvrNumber;?>')
	}
	else{
		parent.tab_edit('Print Roll Over', '<?php echo $menu_link; ?>', 'icon-print', 'to_print_detail','<?php echo $detail->ROvrNumber;?>')
	}
	
}

function app_reverse(){
    var no_transaksi    = $('#id').val();

    checking_reverse(no_transaksi,function(check){
        
        if(check.status == true){
            reverse_data_by_detail('<?php echo $menu_link?>','#fm_roll_over');        }else{
           $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}
</script>