<!---- 
    Author			: Hasan
    Created Date	: 03 Feb 2015
    Revision		: Fachri 
-->
<style>
table td{
	height: 20px;
}
</style>

<?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    = "disabled";
            }
			
			//-------------end----------------------------

            $FDNumber   = $detail->FDNumber;
            $FDNumber   = str_replace('/', 'slash', $FDNumber);
?>	
<div class="easyui-layout" data-options="fit:true,border:false">
    <form id="fm_fixed_asset_dispose" name="fm_fixed_asset_dispose" method="post">
		<input type="hidden" name="detail" id="detail" />
		<input type="hidden" name="id" id="id" value="<?= $detail->FDNumber; ?>"/>
        
        <!-- 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">
                        <input type="checkbox" class="easyui-checkbox" name="chk_suspended" id="chk_suspended" style="margin-top:0px;margin-right:1px;" onclick="suspend_transaction(1,'<?= $menu_link;?>',$('#txt_fd_no').val(),'<?= $menu_label;?>')" <?=(!in_array($detail->TransStatus,array(4,5,6))) ? 'disabled' : ''?> <?=(($detail->TransStatus==3)?'checked':'')?> <?php echo $cek_autority_delete;?>/>
                    	<span style="display:inline-block;margin-top:2px;<?=(($detail->TransStatus==3)?'color:gray;':'color:black')?>">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?>', '<?= $menu_link; ?>', 'icon-print', 'to_preview_journal/<?= $detail->FDNumber ?>')" <?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 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">
        <?php if($detail->TransStatus == 2){ ?>
            <div class="blanket"></div>
            <div class="paid-box"><b>Finished</b></div>
        <?php }else if($detail->TransStatus==1){ ?>
             <div class="blanket"></div>
             <div class="suspend-box"><b>On Progress</b></div>
		<?php }else if($detail->TransStatus==3){ ?>
             <div class="blanket"></div>
             <div class="suspend-box"><b>Suspended</b></div>
        <?php
        }
        ?>
            <div id="form-wrapper" class="easyui-panel"  style="width:1180px;height:526px;padding:5px;" fit='true'>
               
                <input type="hidden" name="branch_hidden" id="branch_hidden" value="<?php echo $detail->BranchCode; ?>"/>
                <table width="100%" height="141">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                            <td width="150"><b>Gain / Loss Account</b></td>
                            <td colspan="3">
                                <b>:</b>
                                <select name="cmb_gl" class="combo-grid" editable="true" id="cmb_gl" required="required"  style="width:400px;height:22px;" data-options="
                                    panelWidth: 400,
                                    value     : '<?= $detail->GLAccount?>',
                                    idField: 'AccountNumber',
                                    textField: 'AccountName',
                                    url: '<?= base_url().$menu_link; ?>/get_gl_account',               
                                    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="250" align="center" valign="middle" bgcolor="#333399" ><span style="color:#fff;">Dispose Fixed Asset No.</span></td>
                                <td width="150" align="center" valign="middle" bgcolor="#333399" ><span style="color:#fff;">Date</span></td>
                                
                            </tr>
                            <tr>
                                <td height="20">&nbsp;</td>
                                <td align="center" valign="top">&nbsp;</td>
                                <td align="left" valign="top"><input name="txt_fd_no" id="txt_fd_no" type="text" value="<?= $detail->FDNumber;?>" style="width:250px; height:18px" required editable="false" readonly /></td>
                                <td align="right" valign="top"><input name="txt_date" id="txt_date" type="text" value="<?= $detail->FDDate;?>" class="easyui-datebox validBackward" style="width:150px" required editable="false" readonly/></td>
                                <input type="hidden" id="detail_id" name="detail_id">
                            </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_fd" class="easyui-edatagrid" idField="id"
                            rownumbers="true" fitColumns="true" url="<?= base_url().$menu_link;?>/get_detail_fixed_asset/<?=$FDNumber;?>"
                            singleSelect="true" style="height:256px;" 
                            data-options = "
                                
                            " 
                            >
                            <thead>
                            <tr>
                                <th field="AssetCode"   halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"  >AssetCode</th>
                                <th field="ItemCode"    halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"  >ItemCode</th>
                                <th field="Description" halign="center" align="left" valign="middle"    data-options="width:700,align:'left'"   >Item Description</th>
                                <th field="Qty"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"   >Qty</th>
                                <th field="AccumAmount"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"  hidden="true">Accumulation Amount</th>
                                <th field="ItemAverage"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"  hidden="true">ItemAverage</th>
                                <th field="ReceiptNumber" data-options="width:200,align:'left'" hidden="true">ReceiptNumber</th>
                                <th field="FixedAssetType" data-options="width:200,align:'left'" hidden="true">FixedAssetType</th>
                                <th field="ItemCategoryID" width="200" halign="center" align="left" hidden="true">ItemCategoryID</th>
                                
                            </tr>
                            </thead>
                        </table> 
                        <div id="tb_dg_item">
                            <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;" <?= in_array($detail->TransStatus,array(0,1,2,3)) ? 'readonly' : '' ?>><?php echo $detail->FDDesc; ?></textarea></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="301" align="right" valign="top" height="23"><b>Total Amount :&nbsp;</b></td>
                    <td width="144" align="right" valign="top">
                        <input type="text" id="TotalAmount" name="TotalAmount" class="easyui-numberbox" data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;" value="<?= $detail->TotalAmount?>" 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-help" onclick="" plain="true">Help</a>								
                        <a href="javascript:void(0)" class="easyui-linkbutton form-button app-button" iconCls="icon-ok" onclick="app_dir()" <?=($this->session->userdata('jobtitle_status') != 0 || $detail->TransStatus != 5 || (date('Y-m-d') < date('Y-m-d', strtotime($detail->FDDate))))?'disabled':''?> >App. Dir</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,9)))?'':'disabled'?> style="<?= $cek_autority_reverse;?>" >Reverse</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onClick="update_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="parent.tab_edit('Print Dispose Fixed Asset', '<?php echo $menu_link; ?>', 'icon-print', 'to_print_detail','<?php echo $detail->FDNumber;?>')" <?=(in_array($detail->TransStatus,array(4,5,6,1,2,3,9)))?'':'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,5,6)))?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>	
					</td>
				 </tr>
			</table>				
		</div>
    </form>
</div>

<div id="dialog-fixed-asset" class="easyui-dialog" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons" style="width:840px;height:auto;top:27px;background-color:#F8F8F8;padding:5px;">     
     <table id="dg_fixed_asset" class="easyui-datagrid" 
        url="<?= base_url().$menu_link;?>/get_fixed_asset"     
        remoteSort="false"
        pageSize="150"
        fitColumns="true" 
        rowNumbers="true"
        singleSelect="true"
        style="width:812px;height:400px" 
        data-options = "border:true,view:scrollview,autoRowheight:false"
    >                      
        <thead>
            <tr>
                <th field="ck" checkbox="true"></th>
                <th field="ReceiptNumber" width="250" halign='center' align='center' sortable="true" hidden="true">Receipt No.</th>
                <th field="ReceiptDate" width="130" halign='center' align='center' sortable="true" formatter="formatDate">Receipt Date</th>
                <th field="AssetCode" width="150" halign='center' align='center' sortable="true">AssetCode</th>
                <th field="ItemAverage" width="150" halign="center" align="left" hidden="true">ItemAverage</th>
                <th field="AccumAmount" width="150" halign="center" align="left" hidden="true">AccumAmount</th>
                <th field="ItemCode" width="200" halign="center" align="left" hidden="true">ItemCode</th>
                <th field="ItemDesc" width="200" halign="center" align="left" hidden="true">ItemDesc</th>
                <th field="ItemQty" width="200" halign="center" align="left" hidden="true">ItemQty</th>
                <th field="FixedAssetType" width="200" halign="center" align="left" hidden="true">FixedAssetType</th>
                <th field="ItemCategoryID" width="200" halign="center" align="left" hidden="true">ItemCategoryID</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_fd();">&nbsp; OK &nbsp; &nbsp;</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-fixed-asset');">Cancel</a>
    </div>
                     
</div>


<script type="text/javascript">

var form_click_aktiv = 0;
var ind_fd           = -1;
var arr_fixed        = [];

var SavePrivillage =  '<?=$group_user->SavePrivillage;?>';

$(document).ready(function(){

    $("#fm_fixed_asset_dispose").click(function(){
        $('#dg_fd').edatagrid('saveRow');
    });
});

function add_fixed_asset(){
    $('#dg_fd').edatagrid('addRow');

    ind_fd = -1;

    var row        = $('#dg_fd').edatagrid('getRows');
    $.each(row, function(ind, val) {
           ind_fd++;
    });
}


function update_data(){

    var created_by  = '<?= $detail->CreatedBy;?>';
    var login_as    = "<?= $this->session->userdata('username');?>";

    $('#dg_fd').edatagrid('saveRow'); 

    var row         = $('#dg_fd').datagrid('getRows');
    var totalAmount = 0;

    $.each(row, function(ind, value) {
        totalAmount += value.Amount;
    });

    if(totalAmount <= 0){
        $.messager.alert('Unseccessfull','Total Amount tidak boleh nol','error',function(){
            return false;
        });
    }else{
        var login_as        = "<?= $this->session->userdata('username');?>";
        var created_by      = "<?= $detail->CreatedBy;?>";
        var depart_by       = "<?= $detail->DepartmentCode;?>";
        var depart_ses      = "<?= $this->session->userdata('department');?>";
        var jobtitle_status = "<?= $this->session->userdata('jobtitle_status');?>";

        var no_transaksi    = $('#txt_fd_no').val();
        var status          = '<?= $detail->TransStatus;?>';

        checking_update(no_transaksi,status,function(check){
        
            if(check.status == true){

                var detail = JSON.stringify(row);
                $('#detail').val(detail);
                edit_data('<?= $menu_link?>','#fm_fixed_asset_dispose',created_by,login_as,depart_by,depart_ses,jobtitle_status);

            }else{
                $.messager.alert('Unseccessfull',check.message,'error',function(){
                    location.reload();
                });
            }

        });
    }
}

function app_dir(){
    var no_transaksi    = $('#txt_fd_no').val();

    checking_approval(no_transaksi,function(check){
        
        if(check.status == false){
            approved_dir('<?=$menu_link?>','#fm_fixed_asset_dispose');
        }else{
            $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function app_reverse(){

    var no_transaksi    = $('#txt_fd_no').val();

    checking_reverse(no_transaksi,function(check){
        
        if(check.status == true){
            reversed_data('F','<?=$menu_link?>',no_transaksi, '<?=$menu_label?>');
        }else{
           $.messager.alert('Unseccessfull',check.message,'error',function(){
                location.reload();
            });
        }
    });
}

function remove_row(){

    $('#dg_fd').edatagrid('saveRow'); 

    var countRow        = $('#dg_fd').datagrid('getRows');

    var selectedrow     = $('#dg_fd').datagrid('getSelected');
    var row_index       = $('#dg_fd').datagrid('getRowIndex', selectedrow);

    if(countRow.length > 1){
    
        $.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');
                    $('.app-button').linkbutton('disable');
                    /*=================================end============================================================*/
                    arr_fixed.push(selectedrow.AssetCode);
                    $('#detail_id').val(JSON.stringify(arr_fixed));
                    $('#dg_fd').datagrid('deleteRow', row_index);
                    form_click_aktiv--;
                    ind_fd--;
                    calculate_sub();
                }
            }
        );
    }else{
        $.messager.alert('warning','Anda tidak dapat menghapus list fixed asset, minimum list fixed asset adalah satu (1).','error');
    }
}

function calculate_sub(){
    var row         = $('#dg_fd').edatagrid('getRows');
    var totalAmount = 0;

    $.each(row, function(ind, value) {
        totalAmount += parseFloat(value.ItemAverage);
    });

    $("#TotalAmount").numberbox('setValue',totalAmount);
}


function open_list_fixed_asset() { 
   modal_open('List Fixed Asset','dialog-fixed-asset');
}

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?>', $('#txt_fd_no').val(), '<?=$menu_label;?>', url, created_by, login_as, job_title);
}


</script>