<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: gaoyang  
    $Date: 2013-10-3 上午10:23:10  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" trace="true">
    <a:init-procedure>
        <a:model-query fetchAll="true" model="gld.gld_currency_vl" rootPath="currency_path"/>
        <a:model-query fetchAll="true" model="gld.gld_exchangerate_type_lov" rootPath="exchangerate_type_path"/>
        <a:model-query fetchAll="true" model="csh.CSH511.csh_transaction_default_exchange_rate_type" rootPath="default_exchange_rate_type_path"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con_contract_inception_execute_link_id" model="cont.CON500.con_contract_inception_execute" modelaction="update"/>
        <a:link id="get_exchange_rate_link_id" model="basic.hls_get_exchange_rate" modelaction="update"/>
        <a:link id="sys_service_welcome_link" url="${/request/@context_path}/welcome.lview"/>
        <a:link id="con_contract_inception_link_id" url="${/request/@context_path}/modules/cont/CON503/con_contract_inception.lview"/>
        <script type="text/javascript"><![CDATA[
            var ajax_flag = 'N';
            
            function con503_con_contract_inception_quit() {
                location.href = $('sys_service_welcome_link').getUrl();
                parent.shMenu('show');
            }
            
            function on_con_contract_inception_sure() {
                lock_list_inception_window();
                var current_ds = $('con_contract_inception_ds');
                if (current_ds.validate()) {
                    var current_record = current_ds.getCurrentRecord();
                    var confirm_win = Leaf.showConfirm('${l:PROMPT}', '${l:HLS.CONTRACT_NUMBER}:' + current_record.get('contract_number') + ',${l:CON500.CONTRACT_INCEPTION_CONFIRM}', function() {
                        ajax_flag = 'Y';
                        Leaf.request({
                            url: $('con_contract_inception_execute_link_id').getUrl(),
                            para: current_record.data,
                            success: function(res) {
                                parent.Leaf.SideBar.enable = true;
                                parent.Leaf.SideBar.show({
                                    msg: '${l:HLS.SUBMIT_SUCCESS}',
                                    duration: 2000
                                });
                                window.location.href = $('con_contract_inception_link_id').getUrl();
                            },
                            error: function(res) {
                                unlock_list_inception_window();
                            },
                            faliure: function() {
                                unlock_list_inception_window();
                            },
                            scope: this
                        });
                    }, function(res) {
                        unlock_list_inception_window();
                    });
                    confirm_win.on('close', function() {
                        if (ajax_flag == 'N') {
                            unlock_list_inception_window();
                        }
                    });
                } else {
                    unlock_list_inception_window();
                }
            }
            
            function lock_list_inception_window() {
                Leaf.Masker.mask(document.documentElement, '${l:HLS.EXECUTING}');
            }
            
            function unlock_list_inception_window() {
                Leaf.Masker.unmask(document.documentElement);
            }
            
            function on_con_contract_inception_update(ds, record, name, value, oldvalue) {
                if (name == 'rate_method_code' || name == 'inception_of_lease') {
                    if (record.get('currency') != '${/model/default_exchange_rate_type_path/record/@functional_currency_code}') {
                        var rate_method_code = record.get('rate_method_code');
                        record.set('exchange_rate_quotation', '');
                        record.set('exchange_rate', '');
                        if (rate_method_code == 'PERIOD' || rate_method_code == 'DAILY' || rate_method_code == 'FIXED') {
                            record.getField('exchange_rate').setReadOnly(true);
                            record.set('exchange_rate', '');
                            getExchangeRate({
                                fromCur: '${/model/default_exchange_rate_type_path/record/@functional_currency_code}',
                                toCur: record.get('currency'),
                                exchangeRateType: record.get('exchange_rate_type'),
                                exchangeDate: record.get('inception_of_lease')
                            });
                        } else {
                            record.getField('exchange_rate').setReadOnly(false);
                            record.set('exchange_rate', '');
                        }
                    }
                } else if (name == 'currency') {
                    record.set('rate_method_code', '');
                    record.set('exchange_rate_quotation', '');
                    record.set('exchange_rate_type', '');
                    record.set('exchange_rate_type_display', '');
                    if (!Ext.isEmpty(value)) {
                        if (value == '${/model/default_exchange_rate_type_path/record/@functional_currency_code}') {
                            record.getField('base_rate_new').setRequired(false);
                            record.getField('base_rate_new').setReadOnly(true);
                            record.getField('exchange_rate_type_display').setRequired(false);
                            record.getField('exchange_rate_type_display').setReadOnly(true);
                            record.set('base_rate_new', '');
                            record.set('exchange_rate', 1);
                        } else {
                            record.set('exchange_rate', '');
                            record.getField('base_rate_new').setReadOnly(false);
                            record.getField('base_rate_new').setRequired(true);
                            record.getField('exchange_rate_type_display').setRequired(true);
                            record.getField('exchange_rate_type_display').setReadOnly(false);
                        }
                    }
                } else if (name == 'base_rate_new') {
                    if (!Ext.isEmpty(value) && (value <= 0 || value >= 1)) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.BASE_RATE_LIMIT}');
                        setTimeout(function() {
                            record.set('base_rate_new', '');
                        }, 1);
                    }
                }
            }
            
            function getExchangeRate(param) {
                Leaf.request({
                    url: $('get_exchange_rate_link_id').getUrl(),
                    para: {
                        from_currency: param.fromCur,
                        to_currency: param.toCur,
                        exchange_date: param.exchangeDate,
                        exchange_rate_type: param.exchangeRateType
                    },
                    success: setRateAndQuotation,
                    scope: this
                });
            }
            
            function setRateAndQuotation(res) {
                var record = $('con_contract_inception_ds').getAt(0);
                if (Ext.isDefined(res.result.exchange_rate)) {
                    record.set('exchange_rate', res.result.exchange_rate);
                }
                if (Ext.isDefined(res.result.exchange_rate_quotation)) {
                    record.set('exchange_rate_quotation', res.result.exchange_rate_quotation);
                } else {
                    record.set('exchange_rate_quotation', 'DIRECT QUOTATION');
                }
            }
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_authority_list_validate.lview?document_category=CONTRACT&amp;function_code=CON502"/>
        <a:dataSets>
            <a:dataSet id="currency_ds">
                <a:datas dataSource="/model/currency_path"/>
            </a:dataSet>
            <a:dataSet id="exchangerate_type_ds">
                <a:datas dataSource="/model/exchangerate_type_path"/>
            </a:dataSet>
            <a:dataSet id="con_contract_inception_ds" autoCreate="true">
                <a:fields>
                    <a:field name="contract_number" lovGridHeight="350" lovHeight="530" lovService="basic.con_contract_for_lov?document_type=PRJCON&amp;inception_flag=N" lovWidth="550" required="true" title="HLS.CONTRACT">
                        <a:mapping>
                            <a:map from="contract_id" to="contract_id"/>
                            <a:map from="contract_number" to="contract_number"/>
                            <a:map from="contract_name" to="contract_name"/>
                            <a:map from="currency" to="currency"/>
                            <a:map from="currency_desc" to="currency_desc"/>
                            <a:map from="base_rate" to="base_rate_new"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="contract_name" readOnly="true"/>
                    <a:field name="currency_desc" displayField="currency_name" options="currency_ds" readOnly="true" returnField="currency" valueField="currency_code"/>
                    <a:field name="currency"/>
                    <a:field name="inception_of_lease" required="true"/>
                    <a:field name="exchange_rate_type_display" displayField="type_name" options="exchangerate_type_ds" readOnly="true" returnField="exchange_rate_type" valueField="type_code">
                        <a:mapping>
                            <a:map from="type_code" to="exchange_rate_type"/>
                            <a:map from="rate_method_code" to="rate_method_code"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="exchange_rate_type"/>
                    <a:field name="rate_method_code"/>
                    <a:field name="exchange_rate" readOnly="true"/>
                    <a:field name="base_rate_new" readOnly="true"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="on_con_contract_inception_update"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="con503_con_contract_inception_quit" text="HLS.EXIT"/>
                <a:gridButton click="on_con_contract_inception_sure" text="HLS.CONTRACT_INCEPTION"/>
            </a:screenTopToolbar>
            <a:form column="2" labelWidth="120" marginWidth="800" title="HLS.CONTRACT_INCEPTION">
                <a:lov name="contract_number" bindTarget="con_contract_inception_ds" prompt="HLS.CONTRACT_NUMBER"/>
                <a:textField name="contract_name" bindTarget="con_contract_inception_ds" prompt="HLS.CONTRACT_NAME"/>
                <a:datePicker name="inception_of_lease" bindTarget="con_contract_inception_ds" prompt="HLS.INCEPTION_OF_LEASE"/>
                <a:comboBox name="currency_desc" bindTarget="con_contract_inception_ds" prompt="HLS.CURRENCY"/>
                <a:comboBox name="exchange_rate_type_display" bindTarget="con_contract_inception_ds" prompt="HLS.EXCHANGE_RATE_TYPE_DESC"/>
                <a:numberField name="exchange_rate" allowNegative="false" bindTarget="con_contract_inception_ds" decimalPrecision="-1" prompt="HLS.EXCHANGE_RATE"/>
                <a:percentField name="base_rate_new" bindTarget="con_contract_inception_ds" decimalPrecision="-1" prompt="HLS.BASE_RATE"/>
            </a:form>
        </a:screenBody>
    </a:view>
</a:screen>
