<!---- 
Author 	: Ladur
Tanggal : 12-08-2014	
Revisi 	:
-->
<?php
//---------Untuk privellege jika dia ada tombol save----------
	if($group_user->SavePrivillage == 1){
		$cek_autority_save  = "";
	}
	else{
		$cek_autority_save 	= "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" id="tab_transstatus">
            
            </table>		     	
        </div>			
    </div>
    <!-- End of top view -->
    
    <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" style="width:1180%;height:526px;padding:5px;" fit="true">
        	<form id="fm_payment" name="fm_payment" method="post">
				<input type="hidden" name="detail" id="detail"/>
                
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                        	<td width="150"><b>Paid From</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<select name="cmb_bank_account" id="cmb_bank_account" 
                                	class="combo-grid" editable="true"
                                	style="width:300px;height:24px;" 
                                    required
                                    data-options="
                                        panelWidth: 302,
                                        idField: 'AccountNumber',
                                        textField: 'AccountName',
                                        url: '<?= base_url().$menu_link; ?>/get_account_kas',               
                                        columns: [[                
                                            {field:'AccountNumber',title:'AccountNumber',width:100},  
                                            {field:'AccountName',title:'Account Name',width:190},
                                            {field:'CurrencyCode',title:'Currency Code',width:120}
                                        ]],
                                        fitColumns: true,
                                         onSelect: function(rowIndex, rowData){
                                            get_bank_balance(rowData.AccountNumber,rowData.CurrencyCode);  
                                        }
                                    "
                                >
                                </select>
                            </td>
                        </tr>
                         <tr height="20">
                            <td width="150" valign="top" style="padding-top:4px;"><b><?php echo label_grid('check_no');?></b></td>
                            <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                                <input type="text" name="cheque_number" id="cheque_number" style="width:298px">
                            </td>
                        </tr>
                        <tr height="20">
                        	<td width="150" valign="top" style="padding-top:4px;"><b>Memo</b></td>
                            <td width="10" valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<textarea class="textbox" name="txa_memo" id="txa_memo" rows="4" style="width:298px;resize:none;white-space:normal;" onclick=""></textarea>
                            </td>
                        </tr>
                        <tr height="20">
                        	<td width="150"><b>Amount</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input name="txt_amount" id="txt_amount" type="text" class="easyui-numberbox" style="width:298px;height:20px;text-align:right" formatter="formatRp" 
                                    data-options="
                                        min:0.01,precision:2,decimalSeparator:',',groupSeparator:'.',disabled:true,
                                        onChange:function(value){
                                            amount_value(value);
                                        }
                                    " required>
                                <!-- <a href="#" id="btn_calculate" class="easyui-linkbutton" data-options="iconCls:'icon-calculator',size:'small'" onclick="count_amount()"></a> -->
                            </td>
                        </tr>
                        
                        </table>
                    </td>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                        	<td>&nbsp;</td>
                            <td width="150" style="background:#333399;text-align:center;color:#FFF;"><b>Available Balance (<span class='bank_balance_currency'></span>)</b></td>
                            <td width="122" style="background:#333399;text-align:center;color:#FFF;">Cash Payment Date</td>
                        </tr>
                        <tr height="20">
                        	<td>&nbsp;</td>
                            <td>
                            	<input name="txt_bank_balance" id="txt_bank_balance" type="text" class="easyui-numberbox" style="width:160px;height:20px;text-align:right" formatter="formatRp" 
                                    data-options="
                                        precision:2,decimalSeparator:',',groupSeparator:'.',
                                        
                                    " readonly>
                            </td>
                            <td>
                            	<input type="text" name="txt_payment_date" id="txt_payment_date" value="<?php echo $date_now;?>" class="easyui-datebox validBackward" onClick="validBackward();" style="width:122px;height:20px" editable="false" required />
                            </td>
                        </tr>
                        <tr height="20">
                        	<td colspan="3" align="right" style="padding-top:10px;">
                            	<span class="blink">PERINGATAN :</span>
                                <p>Form ini hanya dapat digunakan untuk melakukan transaksi dengan </p>
                                <p>total invoice sama dengan total faktur yang lebih kecil atau sama dengan Rp.250.000,00</p>
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                </table>
                <!-- End of Form header -->
                
                <!-- Datagrid -->
                <table id="dtg_payment" class="easyui-edatagrid"
					 toolbar="#tb_dtg_payment" idField="id"
					 rownumbers="true" fitColumns="true" 
                     singleSelect="true" style="height:355px;"
                     data-options ="
                        onBeginEdit:function(rowIndex){
                            /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                            save_button_begendedit('<?=$group_user->SavePrivillage;?>');
                            /*=================================end============================================================*/
                        },
                        onEndEdit:function(indexRow,row,changes){
                            /*====================on beg in edit saat grid di klik maka tombol save disable===================*/
                            
                            save_button_endedit('<?=$group_user->SavePrivillage;?>');
                            /*=================================end============================================================*/
                            var subtotal = 0;
                            var item =  $('#dtg_payment').datagrid('getRows');
                            $.each(item, function( index,value) {
                                subtotal += parseFloat(value.Amount);
                            });

                            if(subtotal > 250000){
                                amount_value(subtotal);
                                var amount_edit = parseFloat(changes.Amount);
                                subtotal -= amount_edit;
                            }

                            $('#total_amount').html(subtotal);
                            $('#txt_amount').numberbox('setValue',subtotal);
                        }
                       
                     "
                >
                    <thead>
                    <tr>
                        <th align="center" valign="middle" data-options="field:'AccountName',width:400,align:'left'" editor="{type:'Payment_Account_Name',options:{required:true}}">Account Name</th>
                        <th align="center" valign="middle" formatter="formatRp" data-options="field:'Amount',width:120,align:'center',editor:{type:'numberbox',options:{min:0.01,max:250000,precision:2,decimalSeparator:',',required:true}}">Amount</th>
                        <th align="center" valign="middle" data-options="field:'Memo',width:400,align:'left',editor:'text'">Memo</th>
                        <th align="center" valign="middle" data-options="field:'DepartmentName',width:150,align:'left'" editor="{type:'Department_Account_Name',options:{required:true}}">Department</th>
                        <th align="center" valign="middle" hidden='true' data-options="field:'DepartmentAccountCode',width:150,align:'left'" editor="{type:'Department_Account_Code',options:{required:true}}">Department Code</th>
                    </tr>
                    </thead>
				</table>
                
                <!-- Datagrid Toolbar -->
				<div id="tb_dtg_payment">
					<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add_row()">New</a>
					<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">Delete</a>
				</div>
                <!-- End of Datagrid Toolbar -->
                <!-- End of Datagrid -->
                
                
            </form>
        </div>
    </div>
    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>&nbsp;</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="javascript:location.reload()">Cancel</a>	     
                </td>
             </tr>
         </table>
		</div>
</div>

<!--  Tagor Dialog Payment AccountName -->
<div id="dialog-payment-account" 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-buttons-account-payment">
    <form id="form_payment_account" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <tr>
                <td width="197" align="left" valign="middle"><b>Account Number</b></td>
                <td width="14"><b>:</b></td>

                <td width="625" colspan="2">
                    <input type="text" style="width:608px;height:20px" name="txt_account_number" id="txt_account_number" class="easyui-validatebox" placeholder="< Account Number >" onkeyup="search_payment_account_popup();" />
                </td>
            </tr>

            <tr>
                <td width="197" align="left" valign="middle"><b>Account Name</b></td>
                <td><b>:</b></td>

                <td colspan="2">
                    <input type="text" style="width:608px;height:20px" name="txt_account_name" id="txt_account_name" class="easyui-validatebox" placeholder="< Account Name >" onkeyup="search_payment_account_popup();" />
                </td>
            </tr>

            <tr>
                <td colspan="3">
                    <table id="tt_choose_payment" class="easyui-datagrid" 
                        url="<?= base_url().$menu_link; ?>/all_accountpembiayaan/<?=$this->session->userdata('company');?>" 
                        singleSelect="true" fitColumns="true" pagesize="150"
                        style="width:828px;height:380px" data-options = "border:false,view:scrollview,autoRowheight:false"
                    >
                        <thead>
                            <tr>
                                <th field="AccountNumber" width="150" sortable="true">Account Number</th>
                                <th field="AccountName" width="460" sortable="true">Name</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-account-payment" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_payment_250();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-payment-account');">Cancel</a>
</div>

<!-- Dialog Payment Department -->
<div id="dialog-payment-department" class="easyui-dialog" style="width:450px;height:523px;top:27px;background-color:#F8F8F8;padding:0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons-department-payment">
    <form id="form_payment_department" method="post" novalidate>
        <table width="100%" align="left" border="0">
            <tr>
                <td width="120" align="left" valign="middle"><b>Department Code</b></td>
                <td width="10"><b>:</b></td>
                
                <td colspan="2">
                    <input type="text" style="width:288px;height:20px" name="txt_department_code" id="txt_department_code" class="easyui-validatebox" placeholder="< Department Code >" onkeyup="search_payment_department_popup();" />
                </td>
            </tr>

            <tr>
                <td width="120" align="left" valign="middle"><b>Department Name</b></td>
                <td><b>:</b></td>

                <td colspan="2">
                    <input type="text" style="width:288px;height:20px" name="txt_department_name" id="txt_department_name" class="easyui-validatebox" placeholder="< Department Name >" onkeyup="search_payment_department_popup();" />
                </td>
            </tr>
            
            <tr>
                <td colspan="3">
                    <table id="tt_choose_department_payment" class="easyui-datagrid" 
                        url="<?php echo base_url(); ?>list/payment/all_department/<?php echo $this->session->userdata('company');?>" 
                        singleSelect="true" fitColumns="true" 
                        style="width:428px;height:388px;"
                    >
                        <thead>
                            <tr>
                                <th halign="center" width="170" field="DepartmentCode" sortable="true">Department Code</th>
                                <th halign="center" width="375" field="DepartmentName" sortable="true">Name</th>
                            </tr>
                        </thead>
                    </table>
                </td>
            </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-department-payment" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_department_payment();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-payment-department');">Cancel</a>
</div>
<!-- End Tagor -->

<!-- <div id="win" class="easyui-dialog" style="width:400px;height:80px;" closed="true" closable="false" draggable="false"
        data-options="resizable:true,modal:true">
        <div id="p" class="easyui-progressbar" style="width:300px;margin:10px 50px 10px 50px;"></div>
</div> -->

<script type="text/javascript">

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

$(document).ready(function(){

    load_page_new('<?=$menu_label;?>');

    $('#fm_payment').click(function(){
        $('#dtg_payment').edatagrid('saveRow');
    });

});
	
function show_journal(){
	var id_hidden	= $('#id_tojournal').val();
	var id 			= id_hidden.replace("/",".");
	var id2			= id.replace("/",".");
	parent.tab_edit('Journal Payment', '<?=$menu_link?>', 'icon-journal', 'to_journal', id2);
}

function amount_value(value){
	if(value > 250000){
		$.messager.alert('Info','Amount tidak lebih dari 250.000');
		//$('#txt_amount').numberbox('setValue', 250000);
	}
}

 function start(){
    var value = $('#p').progressbar('getValue');
   if (value < 500){
        value += Math.floor(Math.random() * 10);
        $('#p').progressbar('setValue', value);
        setTimeout(arguments.callee, 200);
    }
};
	
function count_amount(){
	var total_amount = 0;
	var selected	= $('#dtg_payment').datagrid('getSelected');
	var index		= $('#dtg_payment').datagrid('getRowIndex', selected);
	var editor 		= $('#dtg_payment').datagrid('getEditors', index);
	
	var EAccount	= $(editor[0].target);
	var EAmount		= $(editor[1].target);
	var EDepartment	= $(editor[3].target);
	
	if(EAccount.val() == '' || EAmount.numberbox('getValue') == '' || EDepartment.val() == ''){
		$.messager.alert('Info','Silahkan lengkapi data pada grid','error',function(){});
		return false;
	}
	else{
		$('#dtg_payment').edatagrid('saveRow');		
		var rows 	= $('#dtg_payment').datagrid('getRows');
		for(var i=0; i<rows.length; i++){
			total_amount += parseInt(rows[i].Amount);
		}
	
		$('#total_amount').html(total_amount);
		$('#txt_amount').numberbox('setValue',total_amount);
	}
}

	
function save_me(){

    $('#dtg_payment').edatagrid('saveRow');
    var cek = $('#dtg_payment').datagrid('getRows');
    err  = 0;
    
    var transaksi = "";
    
    $.each(cek, function( index, value ) {
        if(transaksi != value.AccountNumber){
            err++;
            transaksi = value.AccountNumber;
        }
    });
    
    var total_amount = 0;
    
    for(var i=0; i<cek.length; i++){
        if(isNaN(cek[i].Amount)){ cek[i].Amount = 0; }
        total_amount += parseFloat(cek[i].Amount);
    }
    $('#total_amount').html(total_amount);
    
    if(err == 0) {
        $.messager.alert('Unseccessfull','Harap di pilih satu Transaksi.','error',function(){
            $('#dtg_payment').datagrid('reload');
        });
    }
    else if(total_amount != $('#txt_amount').numberbox('getValue')){
        $.messager.alert('Unseccessfull','Total Payment tidak sama dengan Amount.','error',function(){
            $('#dtg_payment').datagrid('reload');
        });
    }
    else{
        if($('#txt_amount').numberbox('getValue') > 250000){
            $.messager.alert('Unseccessfull','Total Payment tidak lebih dari Rp. 250.000','error',function(){
                $('#dtg_payment').datagrid('reload');
            });
        }
        else{
                    
            $('#dtg_payment').edatagrid('saveRow');
            var i=$('#dtg_payment').datagrid('getRows');
            var detail = JSON.stringify(i);
            $("#detail").val(detail);
            save_transaction('<?=$menu_link?>','#fm_payment');        
        }
    }
}

function open_payment_250() { 
   modal_open('Account List','dialog-payment-account');
}

function open_department_payment() { 
   modal_open('Department','dialog-payment-department');
}

function choose_department_payment() {
    var selected_data = $('#tt_choose_department_payment').datagrid('getSelected');

    $('.Department_Account_Name').val(selected_data.DepartmentName);
    $('.Department_Account_Code').val(selected_data.DepartmentCode);

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


function choose_payment_250() {
    var selected_data = $('#tt_choose_payment').datagrid('getSelected');

    $('.Payment_Account_Name').val(selected_data.AccountName);
    modal_close('dialog-payment-account');
}

function remove_row(){
    $('#dtg_payment').edatagrid('saveRow');     
    var selectedrow         = $('#dtg_payment').datagrid('getSelected');
    var row_index           = $('#dtg_payment').datagrid('getRowIndex', selectedrow);
    
    $.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.AccountName + '"?',  
        function(r){
            if (r){

                $('#dtg_payment').datagrid('deleteRow', row_index);
                calculate_sub();
            }
        }
    );
}

function add_row(){
    $('#dtg_payment').edatagrid('addRow');
}

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

    var item        = $('#dtg_payment').datagrid('getRows');
    var sub_total   = 0;
    
    $.each(item, function(index, value) {
        if(value.Amount != undefined){
            sub_total+= parseFloat(value.Amount);
        }               
    });
    
    $('#txt_amount').numberbox('setValue', sub_total);
}

$.extend($.fn.datagrid.defaults.editors, {
    Payment_Account_Name: {
        init: function(container, options){
            var input = $('<input type="text" class="Payment_Account_Name" onClick="open_payment_250()" 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);
        }
    },
            
    Department_Account_Name: {
        init: function(container, options){
            var input = $('<input type="text" class="Department_Account_Name" onClick="open_department_payment()" 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);
        }
    },
            
    Department_Account_Code: {
        init: function(container, options){
            var input = $('<input type="text" class="Department_Account_Code" onClick="open_payment_department()" 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);
        }
    }
});
//**===== End Tagor ====

</script>