<!---- 
    Author			: Daswan
    Created Date	: 14-12-2015
    Revision		: ) 
-->

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

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

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

    	<!-- center view -->
		<div id="form-body" data-options="region:'center',border:false">
            <div id="form-wrapper" class="easyui-panel"  style="width:1180px;height:526px;padding:5px;" fit='true'>
                <input type="hidden" name="" id="" value="" />
                
                <table width="100%" height="141">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                        <tr height="20">
                            <td width="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,
                                idField: 'AccountNumber',
                                textField: 'AccountName',
                                url: '<?= base_url().$menu_link; ?>/get_selling_account',               
                                columns: [[
                                     {field:'AccountNumber',title:'Account Number',width:130},
                                     {field:'AccountName',title:'Account Name',width:350}
                                ]],
                                fitColumns: true
                                ">
                                </select> 
                                
                            </td>
                            <td width="60">&nbsp;</td>
                        </tr>

                        <tr>
                            <td colspan="3" height="50"></td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                            <tr>
                            	<td height="20">&nbsp;</td>
                                <td width="731" align="center" valign="middle"></td>
                                <td width="150" align="center" valign="middle" bgcolor="#333399" ><span style="color:#fff;">Date</span></td>
                                
                            </tr>
                            <tr>
                            	<td height="20">&nbsp;</td>
                                <td align="center" valign="top">&nbsp;</td>
                                <td align="right" valign="top"><input name="txt_date" id="txt_date" type="text" value="<?php echo $date_now;?>" class="easyui-datebox validBackward" style="width:150px;height:20px" required editable="false" /></td>
                            </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" 
                            singleSelect="true" style="height:256px;"
                            data-options = "
                                onBeginEdit:function(rowIndex){
                                    /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                                    //$('.save-button').linkbutton('disable');
                                    /*=================================end============================================================*/
                                },
                                onEndEdit:function(indexRow,row,changes){
                                    /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                                    //$('.save-button').linkbutton('enable');
                                    /*=================================end============================================================*/
                                },
                               onDblClickCell: function(index,field,value){
                                    open_list_fixed_asset();
                               }
                            " 
                            >
                            <thead>
                            <tr>
                                <th field="AssetCode"   halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"           editor="{type:'Asset_Code',options:{required:true}}">AssetCode</th>
                                <th field="ItemCode"    halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"           editor="{options:{required:true}}">ItemCode</th>
                                <th field="Description" halign="center" align="left" valign="middle"    data-options="width:400,align:'left'"           editor="description">Item Description</th>
                                <th field="Qty"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"           editor="{type:'numberbox',options:{required:true,min:1,disabled:true}}">Qty</th>
                                <th field="AccumAmount"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"           editor="{type:'numberbox',options:{required:true,min:0.01,disabled:true}}" hidden="true">Accumulation Amount</th>
                                <th field="ItemAverage"         halign="center" align="left" valign="middle"    data-options="width:200,align:'left'"           editor="{type:'numberbox',options:{required:true,min:0.01,disabled:true}}" 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-add" plain="true" onclick="add_fixed_asset();">New</a>
                            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row();">Delete</a>
                        </div>                  
                    </div>
                    
                </div>
                <!-- End Item Grid -->      

                <table width="100%" cellpadding="0" cellspacing="0" style="margin-top:5px;">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1">
                            <tr>
                                <td width="100" rowspan="2" valign="top"><b>Description</b></td>
                                <td width="362" rowspan="2" valign="top"><textarea name="txa_description" cols="55.5" rows="4" style="resize:none;"></textarea></td>
                            </tr>
                            
                        </table>
                    </td>
                    <td width="50%" valign="top">
                         <table width="100%" cellpadding="0" cellspacing="1">
                           
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td width="144" align="right" valign="top" height="23"><b>Amount Average :&nbsp;</b></td>
                                <td width="144" align="right" valign="top">
                                    <input type="text" id="amount_average" name="amount_average" class="easyui-numberbox" value="0" data-options="min:0.00,precision:2,decimalSeparator:',',groupSeparator:'.'" style="text-align:right;width:200px" readonly />
                                    
                                </td>
                            </tr>
                        
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td width="144" align="right" valign="top" height="23"><b>Deposit To :&nbsp;</b></td>
                                 <td width="144" align="right" valign="top">
                                    <select name="cmb_bank" class="combo-grid" editable="true" id="cmb_bank" required="required"  style="width:200px;height:22px;" data-options="
                                        panelWidth: 400,
                                        idField: 'AccountNumber',
                                        textField: 'AccountName',
                                        url: '<?= base_url().$menu_link; ?>/get_bank_account',               
                                        columns: [[
                                         {field:'AccountNumber',title:'Account Number',width:130},
                                         {field:'AccountName',title:'Account Name',width:350}
                                        ]],
                                        fitColumns: true,
                                        onSelect: function(rowIndex, rowData){
                                            if(rowData.CurrencyCode == 'IDR'){
                                                
                                                $('#txt_rate').prop('readonly',true);
                                            }else{
                                                
                                                $('#txt_rate').prop('readonly',false);
                                            }

                                            get_master_rate(rowData.CurrencyCode);
                                            $('#txt_currency_code').val(rowData.CurrencyCode);
                                        }
                                        ">
                                    </select> 
                                    <input type="hidden" id="txt_currency_code" name="txt_currency_code">
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td width="144" align="right" valign="top" height="23"><b>Rate :&nbsp;</b></td>
                                <td width="144" align="right" valign="top">
                                    <input name="txt_rate" id="txt_rate" type="text" class="easyui-numberbox" style="width:200px;height:22px;text-align:right" formatter="formatRp" 
                                data-options="min:0.01,precision:2,decimalSeparator:',',groupSeparator:'.'" required >
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td width="144" align="right" valign="top" height="23"><b>Asset Price :&nbsp;</b></td>
                                <td width="144" align="right" valign="top">
                                    <input name="txt_asset_price" id="txt_asset_price" type="text" class="easyui-numberbox" style="width:200px;height:22px;text-align:right" formatter="formatRp" 
                                data-options="min:0.01,precision:2,decimalSeparator:',',groupSeparator:'.'" required >
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan="2" height="50"></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 save-button"   iconCls="icon-save"     onClick="save_me()" <?php echo $cek_autority_save;?>>Save</a>   
                        <a href="javascript:void(0)"    class="easyui-linkbutton form-button"               iconCls="icon-cancel"   onclick="location.reload();">Cancel</a> 
                    </td>
                 </tr>
            </table>                
        </div>
    </form>
</div>

<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">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" formatter="formatRp">ItemAverage</th>
                <th field="AccumAmount" width="150" halign="center" align="left" formatter="formatRp">Accummulation Amount</th>
                <th field="ItemAverageHidden" width="150" halign="center" align="left" hidden="true">ItemAverage</th>
                <th field="AccumAmountHidden" width="150" halign="center" align="left" hidden="true">Accummulation Amount</th>
                <th field="ItemCode" width="200" halign="center" align="left" hidden="true">ItemCode</th>
                <th field="ItemDesc" width="200" halign="center" align="left" >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 SavePrivillage =  '<?=$group_user->SavePrivillage;?>';

$(document).ready(function(){
    
     $("#fm_fixed_asset_selling").click(function(){
        if(form_click_aktiv > 0){
            $('#dg_fd').edatagrid('saveRow');
        }
    });

});

function add_fixed_asset(){

    var row         = $('#dg_fd').edatagrid('getRows');
    var count_row   = row.length;

    if(count_row < 1){
        $('#dg_fd').edatagrid('addRow');
    }else{
         $.messager.alert('Unseccessfull','Detail Transaksi tidak boleh lebih dari satu.','warning');
    }
    
}

function choose_fd(){

    var fd              = $('#dg_fixed_asset').datagrid('getChecked');

    $.each(fd, function(ind, value) {
        var ReceiptNumber   = value.ReceiptNumber;
        var ReceiptDate     = value.ReceiptDate;
        var AssetCode       = value.AssetCode;
        var ItemAverage     = value.ItemAverageHidden;
        var AccumAmount     = value.AccumAmountHidden;
        var ItemCode        = value.ItemCode;
        var ItemDesc        = value.ItemDesc;
        var ItemQty         = value.ItemQty;
        var FixedAssetType  = value.FixedAssetType;
        var ItemCategoryID  = value.ItemCategoryID;

        
        $('#dg_fd').edatagrid('updateRow',{
            index   : 0,
            row     :{  ItemCode           : ItemCode,
                        Description        : ItemDesc,
                        Qty                : ItemQty,
                        AccumAmount        : AccumAmount,
                        ItemAverage        : ItemAverage,
                        ReceiptNumber      : ReceiptNumber,
                        FixedAssetType     : FixedAssetType,
                        AssetCode          : AssetCode,
                        ItemCategoryID     : ItemCategoryID}
        });

        $('#dg_fd').edatagrid('saveRow');
        $("#amount_average").numberbox('setValue',ItemAverage);
        form_click_aktiv++;
        
    });
    modal_close('dialog-fixed-asset');
}

$.extend($.fn.datagrid.defaults.editors, {
    Asset_Code: {
        init: function(container, options){
            var input = $('<input type="text" class="open_list_fixed_asset" onClick="open_list_fixed_asset()" style="height:18px;" 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 open_list_fixed_asset() { 
   modal_open('List Fixed Asset','dialog-fixed-asset');
}

function remove_row(){

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

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

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

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

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

function save_me(){
    $('#dg_fd').edatagrid('saveRow'); 

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

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

    if(amount_average <= 0){
        $.messager.alert('Unseccessfull','Total Amount tidak boleh nol','error',function(){
            return false;
        });
    }else{
        var detail = JSON.stringify(row);
        $('#detail').val(detail);
        save_transaction('<?= $menu_link?>','#fm_fixed_asset_selling');
    }
}

</script>