<!---- 
    Author  : Ferry / Tanggal : 15-06-2015	
    Revisi  :
-->

<?php
	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";
	}
	//-------------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">
                        <span class="form-suspend">
                            <input type="checkbox" class="input" name="chk_suspended" id="chk_suspended" value="1" onclick="suspend(1);" <?= ($detail->TransStatus != 4)?'disabled':'' ?> <?php echo $cek_autority_delete;?> />
                            <span class="text" style="">Suspend</span>
                        </span>

                        <a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload();">Refresh</a>
                    </td>
                    
                    <td height="25" align="right" valign="middle">								
                        <span class="form-title"><?=$menu_label?></span>
                    </td>
                </tr>
            </table>		     	
        </div>			
    </div>
    <!-- end top view -->	

    <!-- center view -->
    <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" fit="true" style="padding:5px;">
            <?php if(in_array($detail->TransStatus,array(2,3,8,9))){ ?>
        	<div class="blanket"></div>
                <div class="status-box">
                    <b><?php 
                        if($detail->TransStatus == 3){echo 'Suspended';}
                        elseif($detail->TransStatus == 8){echo 'Item Suspended';}
                        elseif($detail->TransStatus == 9){echo 'Canceled by Dir.';}
                        elseif($detail->TransStatus == 2){echo 'Finished';}
                        else{echo 'Unknown';} 
                    ?></b>
                </div>
            <?php } ?>
            
            <form id="fm_bank_reconcile" name="fm_bank_reconcile" method="post">
                <input type="hidden" name="detail" id="detail"/>
                <input type="hidden" name="hdn_currency_code" id="hdn_currency_code" />
                
                <input type="hidden" name="hdn_new_balance" id="hdn_new_balance"/>
                <input type="hidden" name="hdn_calculated_balance" id="hdn_calculated_balance"/>
                <input type="hidden" name="hdn_out_of_balance" id="hdn_out_of_balance"/>
                <input type="hidden" name="hdn_total_clear" id="hdn_total_clear"/>

                <table width="100%" height="100%" cellpadding="0" cellspacing="0" border="0"> 
                    <tr>
                        <td width="43%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height="20">
                                    <td width="230"><b>Bank Account</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td>
                                        <select name="cbg_bank_account" id="cbg_bank_account" class="easyui-combogrid" style="width:250px;height:22px" required="true" editable="false" readonly
                                            data-options="
                                                panelWidth:400,
                                                value:'<?php echo $detail->BankReconcile;?>',
                                                idField:'AccountNumber',
                                                textField:'AccountName',
                                                url: '<?=base_url()?>list/bank_reconcile/ajax_bank_account',
                                                method: 'post',
                                                fitColumns: true,
                                                columns: [[
                                                    {field:'AccountNumber',title:'Account Bank Name',width:100,halign:'center',align:'center'},						
                                                    {field:'CurrencyCode',title:'Currency Name',width:100,halign:'center',align:'center'}						
                                                ]]                                                
                                            ">
                                        </select>
                                    </td>
                                </tr>
                                
                                <tr height='20'>
                                    <td width="230"><b>New Statement Balance</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td>
                                        <input type="text" name="txt_new_balance" id="txt_new_balance" class="easyui-numberbox" style="width:250px;text-align:right;" readonly
                                            data-options="min:1,precision:'2',groupSeparator:'.',decimalSeparator:','"                                    
                                        />
                                    </td>
                                </tr>
                                
                                <tr height='20'>
                                    <td width='230'><b>Calculated Statement Balance</b></td>
                                    <td width='10'><b>:</b></td>

                                    <td>
                                        <input type="text" name="txt_calculated_balance" id="txt_calculated_balance" class="easyui-numberbox" style="width:250px;text-align:right;" readonly
                                            data-options="min:1,precision:'2',groupSeparator:'.',decimalSeparator:','"                                    
                                        />
                                    </td>
                                </tr>

                                <tr height='20'>
                                    <td width='230'><b>Out of Balance</b></td>
                                    <td width='10'><b>:</b></td>

                                    <td>
                                        <input type="text" name="txt_out_of_balance" id="txt_out_of_balance" class="easyui-numberbox" style="width:250px;text-align:right;" readonly
                                            data-options="min:1,precision:'2',groupSeparator:'.',decimalSeparator:','"                                    
                                        />
                                    </td>
                                    
                                </tr>
                            </table>
                        </td>

                        <td width="50%" valign="top">
                            <table width="100%" cellpadding="0" cellspacing="1">
                                <tr height='20'>
                                    <td width="400" align="right" style="padding-right:15px;"><b>Reconcile Number</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td colspan="3">
                                        <input type="text" id="txt_bren_no" name="txt_bren_no" style="width:250px;text-align:center;" value="<?php echo $detail->BRenNumber; ?>" readonly />
                                    </td>
                                
                                </tr>
                        
                                <tr height='20'>
                                    <td colspan='2'>&nbsp;</td>
                                    <td width='100'><b>Reconcile Date</b></td>
                                    <td width='10'><b>:</b></td>

                                    <td align='right'>
                                        <input type="text" name="txt_bren_date" id="txt_bren_date" class="easyui-datebox validBackward" value="today" style="width:100px;height:20px" editable="false" required="required" /> 
                                    </td>
                                </tr>
                        
                                <tr tr height='20'>
                                    <td colspan="2">&nbsp;</td>
                                    <td width="140"><b>Last Reconcile Date</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td align="right" >
                                        <input type="text" name="txt_bren_last_date" id="txt_bren_last_date" class="easyui-datebox" value='<?= $detail->BRenLastDate ?>' style="width:100px;" readonly /> 
                                    </td>
                                </tr>

                                <tr tr height='20'>
                                    <td colspan="2">&nbsp;</td>
                                    <td width="140"><b>Currency</b></td>
                                    <td width="10"><b>:</b></td>

                                    <td>
                                        <span class="spn_currency" style="font-weight:bold">IDR</span>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

                <div class="easyui-tabs" data-options="tabHeight:18" style="margin-top:5px;">		
                    <div id="tab_item" title="Item">
                        <table id="dtg_item" class="easyui-edatagrid" title="" iconCls="icon-edit" singleSelect="true" rowNumbers="true" fitColumns="true" style="height:325px"
                            url="<?php echo base_url()?>list/bank_reconcile/ajax_item_for_edit"
                        >
                            <thead>
                                <tr>
                                    <th field='Trans_Date'    width='75'  data-options="align:'center',halign:'center'" formatter="formatDate">Date</th>
                                    <th field='Trans_Number'  width='195' data-options="align:'left',halign:'center'"   editor="{type:'text'}">Source No</th>
                                    <th field='Cheque_Number' width='100' data-options="align:'left',halign:'center'"   >Cheque No</th>
                                    <th field='Trans_Desc'    width='300' data-options="align:'left',halign:'center'"   >Description</th>
                                    <th field='Deposit'       width='100' data-options="align:'right',halign:'center'"  formatter="formatRp">Deposit</th>
                                    <th field='WithDrawal'    width='100' data-options="align:'right',halign:'center'"  formatter="formatRp">Withdrawal</th>
                                    <th field="Status"        width="40"  data-options="halign:'center',align:'center'" editor="{type:'checkbox',options:{on:'Yes',off:'No'}}">Status</th>
                                    <th field='Currency_Rate' width='60'  data-options="align:'center',halign:'center'" hidden>Currency Rate</th>
                                    <th field='Trans_Type'    width='60'  data-options="align:'center',halign:'center'" hidden>Trans Type</th>
                                    <th field='Modus_Status'  width='60'  data-options="align:'center',halign:'center'" editor="{type:'text'}" hidden>Modus Status</th>
                                    <th field='Modus_Type'    width='60'  data-options="align:'center',halign:'center'" editor="{type:'text'}" hidden>Modus Type</th>
                                    <th field='BRenDetailID'  width='60'  data-options="align:'center',halign:'center'" hidden>BRen Detail ID</th>
                                </tr>
                            </thead>
                        </table>
		    </div>
                </div>
                
                <table width="30%" cellpadding="0" cellspacing="1" style="padding-top:7px;" border='0' align='right'>
                    <tr height='20'>
                        <td width="120"><b>Total Clear</b></td>
                        <td width="10"><b>:</b></td>

                        <td align="right" width='170'>
                            <input type="text" id="txt_total_clear" name="txt_total_clear" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"
                            />
                        </td>
                    </tr>
                    
                    <tr height='20'>
                        <td width="160"><b>Total Outstanding</b></td>
                        <td width="10"><b>:</b></td>

                        <td align="right" width='170'>
                            <input type="text" id="txt_total_outstanding" name="txt_total_outstanding" class="easyui-numberbox" value="0" style="text-align:right;" readonly
                                data-options="min:0,precision:2,decimalSeparator:',',groupSeparator:'.'"
                            />
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
    
    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <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="approval();" <?= ($this->session->userdata("jobtitle_status") != 1 || substr($this->session->userdata('department'), -3) != '002' || (date('Y-m-d') < date('Y-m-d', strtotime($detail->BRenDate))) || !in_array($detail->TransStatus,array(4))) ? 'disabled' : ''?>>App. Mgr</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="update_data();" <?=($detail->TransStatus!=4)?'disabled':''?> <?php echo $cek_autority_save;?>>Save</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(3,9)) || $detail->AppLevel1Status==1)?'disabled':''?>><?=($this->session->userdata('jobtitle_status')==0)?'Cancel':'Close'?></a>
                </td>
            </tr>
        </table>
    </div>		
</div>

<script>
    $(document).ready(function(){
        $('#txt_total_clear').numberbox('setValue', '<?= $detail->TotalClear ?>');
        $('#txt_total_outstanding').numberbox('setValue', '<?= $detail->TotalOutStanding ?>');

        $('#dtg_item').datagrid('load',{
            BRenNumber  : '<?= $detail->BRenNumber ?>',
            BankAccount : '<?= $detail->BankReconcile ?>',
            TransStatus : '<?= $detail->TransStatus ?>'
        });
    
        $('#dtg_item').datagrid({
            onBeginEdit:function(rowIndex, rowData){
                editors                 = $('#dtg_item').datagrid('getEditors', rowIndex);
                trans_number_editor     = $(editors[0].target);
                status_editor           = $(editors[1].target);
                modus_status_editor     = $(editors[2].target);
                modus_type_editor       = $(editors[3].target);

                $(trans_number_editor).prop('readonly', true);

                $(trans_number_editor).click(function(){
                    var TransNo         = rowData.Trans_Number;            

                    trans_number        = TransNo.split('/');
                    trans_code          = trans_number[1];            
                    InitialCharTrans    = trans_code.substring(4,6);

                    load_transaction(InitialCharTrans, TransNo);
                });
                
                $(status_editor).click(function(){
                    modus_status = $(modus_status_editor).val();
                    
                    if($(this).is(':checked')){
                        if(modus_status == 0){
                            $(modus_type_editor).val('1');
                        }
                        else{
                            $(modus_type_editor).val('0');                            
                        }
                    }
                    else{
                        if(modus_status == 1){
                            $(modus_type_editor).val('3');
                        }
                        else{
                            $(modus_type_editor).val('0');                            
                        }
                    }
                    
                    sum_foote_note();                        
                });
            }
        });
        
        $("#fm_bank_reconcile").click(function() {
            $('#dtg_item').edatagrid('saveRow');
	});
    });

    function sum_foote_note(){
        $('#dtg_item').edatagrid('saveRow');
        item = $('#dtg_item').datagrid('getRows');

        total_clear         = 0;
        total_outstanding   = 0;

        $.each(item, function(index, value) {
            if(value.Status == 'Yes'){
                if(value.Deposit == 0){
                    total_clear         += parseFloat(value.WithDrawal);
                }
                else{
                    total_clear         += parseFloat(value.Deposit);
                }
            }
            else{
                if(value.Deposit == 0){
                    total_outstanding   += parseFloat(value.WithDrawal);
                }
                else{
                    total_outstanding   += parseFloat(value.Deposit);
                }
            }
        });

        $('#txt_total_clear').numberbox('setValue', total_clear);
        $('#txt_total_outstanding').numberbox('setValue', total_outstanding);
    }

    function load_transaction(InitialCharTrans, TransNo){
        var Tbl;
        var TabTitle;
        var LinkTo;

        switch(InitialCharTrans){
            case 'PP':
                Tbl 	= 'at_purchasepayment';
                TabTitle	= 'Purchase Payment';
                LinkTo      = 'list/purchases_payment';
                break;

            case 'SR':
                Tbl         = 'at_salesreceipt';
                TabTitle	= 'Sales Receipt';
                LinkTo	= 'list/sales_receipt';
                break;

            case 'CB':
                Tbl         = 'at_expensecashbon';
                TabTitle	= 'Expense Cash-Bon';
                LinkTo	= 'activities/purchases_expense_cashbon';
                break;

            case 'CP':
                Tbl 	= 'at_cashpayment';
                TabTitle	= 'Payment <= 250.000';
                LinkTo      = 'list/payment';
                break;

            case 'CD':
                Tbl         = 'at_cashdeposit';
                TabTitle	= 'Deposit';
                LinkTo	= 'activities/deposit';
                break;

            case 'SB':
                Tbl         = 'at_banksweep';
                TabTitle	= 'Sweep BANK';
                LinkTo	= 'activities/swift_bank';
                break;

            case 'AP':
                Tbl 	= 'at_afiliasidebt';
                TabTitle	= 'Piutang Afiliasi';
                LinkTo      = 'list/piutang_afiliasi';
                break;

            case 'AI':
                Tbl         = 'at_afiliasiinstallment';
                TabTitle	= 'Payment Afiliasi';
                LinkTo	= 'list/payment_afiliasi';
                break;

            case 'AE':
                Tbl         = 'at_employeedebt';
                TabTitle	= 'Piutang Karyawan';
                LinkTo	= 'activities/piutang_karyawan';
                break;

            case 'EI':
                Tbl         = 'at_employeeinstallment';
                TabTitle	= 'Payment Karyawan';
                LinkTo	= 'activities/payment_karyawan';
                break;

            case 'TC':
                Tbl         = 'at_transfercash';
                TabTitle	= 'Transfer To Petty Cash';
                LinkTo	= 'list/transaksi_antar_kas';
                break;

            default:
                Tbl         = '';
                TabTitle	= '';
                LinkTo	= '';
        }

        parent.tab_edit(TabTitle, LinkTo, 'icon-edit', 'edit', TransNo);
    }
    
    function update_data(){
        var item        = $('#dtg_item').datagrid('getRows');
        selected_status = false;

        $.each(item, function(index, value) {
            if(value.Status == 'Yes'){selected_status = true;}
        });

        if(selected_status == false){
            $.messager.alert('Warning','Tidak ada item yang dipilih untuk disimpan','warning');
        }
        else{
            detail = JSON.stringify(item);

            $("#detail").val(detail);
            edit_transaction('<?=$menu_link?>','#fm_bank_reconcile');
        }
    }
    
    function edit_transaction(url, form_id) {
	url = base_url + url;	
	$(form_id).form('submit',{
            url: url + '/update',
            onSubmit: function(){
                return $(this).form('validate');
            },
	
            success: function(result){
                var result = eval('('+result+')');

                if (result.status==true){
                    $.messager.alert('Success',result.message,'info',function(){						
                        location.reload();								
                    });
                }
                else{
                    $.messager.alert('Failed',result.message,'error',function(){						
                        location.reload();								
                    });
                }
            }
	});
    }

    function approval(){
        var url             = base_url + 'list/bank_reconcile/approval';
	var item            = $('#dtg_item').datagrid('getRows');
        
	var Detail          = JSON.stringify(item);
	var BRenNumber      = $("#txt_bren_no").val();

        $.post(url, {BRenNumber:BRenNumber, Detail:Detail}, function(result){
            if (result.status == true){
                $.messager.alert('Success',result.message,'info',function(){
                    location.reload();						                    
                });				
            } 
            else {
                $.messager.alert('Error',result.message,'error',function(){
                    location.reload();						                    
                });				
            }
        },'json');
    }

    function suspend(type){
	suspend_data_new('F', type, '<?=$menu_link?>', $('#txt_bren_no').val(), 'Bank Reconcile');
    }    
</script>