<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Robert8900
    $Date: 2018-8-13 下午18:14:49
    $Revision: 1.0  
    $Purpose: hlcm中途偿还
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" trace="true">
    <a:view package="leaf.ui.std" template="default">
        <a:link id="con_contract_et_calculate_link" url="${/request/@context_path}/modules/cont/CON701/con_contract_et_calculate.lsc"/>
        <a:link id="con_contract_et_save_link" model="cont.CON701.con_contract_et_save" modelaction="execute"/>
        <a:link id="con_contract_et_submit_link" model="cont.CON701.con_contract_et_submit" modelaction="execute"/>
        <a:link id="attachment_et_link" model="cont.CON701.con_contract_attachment_et" modelaction="update"/>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <a:link id="con_contract_status" model="cont.CON702.con_contract_status" modelaction="update"/>
        <div/>
        <script type="text/javascript"><![CDATA[div
            var save_flag = false;
            
            function Screen_exit() {
                $('${/parameter/@winId}').close();
            }
            
            function contractCreateScreen_calculate() {
                var record = $('contractQueryScreen_mainDs').getCurrentRecord(),
                    et_ds = $('early_terminationDs');
                var et_record = et_ds.getAt(0);
                var et_agreement_id;
                if (et_record.data.et_agreement_id){
                    et_agreement_id = et_record.data.et_agreement_id;
                }
                if (record.validateRecord(true)) {
                    var termination_date = Leaf.formatDate(record.get ('termination_date'));
                    var contract_id = record.get('contract_id');
                    //sql里面设定了 et_type = ET
                    var et_type = record.get('et_type');
                    //和后端匹配，控制一种结清方式
                    var et_profile = "PRI_BAL_STD";

                    Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                    Leaf.request({
                        url: $('con_contract_et_calculate_link').getUrl(),
                        para: {
                            termination_date: termination_date,
                            contract_id: contract_id,
                            et_agreement_id : et_agreement_id,
                            et_type: et_type,
                            et_profile: et_profile
                        },
                        success: function(args) {
                            var et_agreement_id = args.result.et_agreement_id;
                            et_ds.setQueryParameter('et_agreement_id', et_agreement_id);
                            et_ds.query();

                            $('cashflowPlanDs').setQueryParameter('et_agreement_id', et_agreement_id);
                            $('cashflowPlanDs').query();

                            document.getElementById("detail_flag_id").style.display = "";
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        failure: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        error: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        scope: this
                    });
                }
            }
            
            function et_save(value) {
                var et_ds = $('early_terminationDs'),
                    et_record = et_ds.getAt(0);
                var et_agreement_id = et_record.get('et_agreement_id');
                if (et_agreement_id) {
                    var deposit_deduction_flag = et_record.get('deposit_deduction_flag');
                    var deposit_by_agent = et_record.get('deposit_by_agent');
                    var ref_n04 = et_record.get('ref_n04');
                    var ref_n05 = et_record.get('ref_n05');

                    var remittance_date = et_record.get('remittance_date');
                    var remittance_amount = et_record.get('remittance_amount');

                    var reduce_period_interest = et_record.get('reduce_period_interest');
                    var reduce_residual_value = et_record.get('reduce_residual_value');

                    var deposit = et_record.get('deposit');
                    var third_party_deposit = et_record.get('third_party_deposit');
                    var note_s = et_record.get('note_s');
                    var cost_reduction = et_record.get('cost_reduction');
                    Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                    Leaf.request({
                        url: $('con_contract_et_save_link').getUrl(),
                        para: {
                            et_agreement_id: et_agreement_id,
                            deposit_deduction_flag : deposit_deduction_flag,
                            deposit_by_agent:deposit_by_agent,
                            ref_n04: ref_n04,
                            ref_n05: ref_n05,
                            reduce_residual_value:reduce_residual_value,
                            reduce_period_interest:reduce_period_interest,
                            deposit: deposit,
                            third_party_deposit:third_party_deposit,
                            note_s: note_s,
                            cost_reduction: cost_reduction,
                            remittance_date: remittance_date,
                            remittance_amount: remittance_amount
                        },
                        success: function(args) {
                            save_flag = true;
                            if (value == 'sub') {
                                et_submit();
                            } else if (value === 'calc') {
                                contractCreateScreen_calculate();
                            } else {
                                et_ds.setQueryParameter('et_agreement_id', et_agreement_id);
                                et_ds.query();
                                $('cashflowPlanDs').setQueryParameter('et_agreement_id', et_agreement_id);
                                $('cashflowPlanDs').query();
                                Leaf.SideBar.show({
                                    msg: '保存成功',
                                    duration: 2000
                                });
                            }
                             Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        failure: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        error: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        scope: this
                    });

                } else {
                    Leaf.showWarningMessage('提示', '请先测算！');
                }
            }

        function post(xml) {
            Leaf.request({
                url: '${/request/@context_path}/doodream/post/url',
                para: {
                    xml: xml
                },
                success: function(res) {
                    window.open(res.message, "", "width=940, height=650, scrollbars=yes, toolbar=no, location=no, status=no, menubar=no");
                    Leaf.SideBar.show({
                        msg: '${l:HLS.SUBMIT_SUCCESS}',
                        duration: 2000
                    });
                },
                failure: function() {
                    Leaf.SideBar.show({
                        msg: 'failure',
                        duration: 2000
                    });
                },
                error: function() {
                    Leaf.SideBar.show({
                        msg: 'error',
                        duration: 2000
                    });
                },
                scope: this
            });
        }

            //调用doodream 审批接口
        function et_submit() {
            var et_ds = $('early_terminationDs'),
                et_record = et_ds.getAt(0);
            var ref_n04 = et_record.get('ref_n04');
            var ref_n05 = et_record.get('ref_n05');
            var int_rate_display = et_record.get('int_rate_display');
            var win = Leaf.showConfirm('${l:HLS.PROMPT}', '确定要提交？', function() {
                Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                if (ref_n04 == 0 && ref_n05 == 0) {
                    Leaf.request({
                        url: $('con_contract_status').getUrl(),
                        para: {
                            et_agreement_id: et_record.get('et_agreement_id')
                        },
                        success: function (args) {
                            Leaf.showMessage('${l:PROMPT}', '提交成功');
                        },
                        failure: function () {

                        },
                        error: function () {

                        },
                        scope: this,
                        sync: true
                    });
                } else if (et_record) {
                    var et_agreement_id = et_record.get('et_agreement_id');
                    Leaf.request({
                        url: $('con_contract_et_submit_link').getUrl(),
                        para: {
                            et_agreement_id: et_agreement_id
                        },
                        success: function(args) { //
                            //post xml to doodream
                            post(args.result.xml);
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        failure: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        error: function() {
                            Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                        },
                        scope: this
                    });
                } else {
                    Leaf.showMessage('提示', '请先测算！');
                    Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
                }
            }, function() {
                Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
            });
            win.addListener('close', function() {
                Leaf.Masker.unmask($('${/parameter/@winId}').wrap);
            });
        }
            
            function et_submit_entrance() {
                et_save();
                var et_ds = $('early_terminationDs'),
                    et_record = et_ds.getAt(0);
                var et_agreement_id = et_record.get('et_agreement_id');
                Leaf.Masker.mask($('${/parameter/@winId}').wrap, '正在处理...');
                Leaf.request({
                    url: $('attachment_et_link').getUrl(),
                    para: {
                        table_name: 'CON_CONTRACT_ET_HD',
                        table_pk_value: et_agreement_id
                    },
                    success: function(res) {
                        var attachment_num = res.result.attachment_num;
                        if (attachment_num == 0) {
                            et_save('sub');
                        } else {
                            et_save('sub');
                        }
                    },
                    failure: function() {
                    },
                    error: function() {
                    },
                    scope: this
                });
            }
            
            function et_calc_entrance() {
                if (save_flag) {
                    et_save('calc');
                } else {
                    contractCreateScreen_calculate();
                }
            }

        ]]></script>
        <a:dataSets>
            <a:dataSet id="deposit_deduction_ds" lookupCode="YES_OR_NO"/>
            <a:dataSet id="early_terminationDs" autoCreate="true" model="cont.CON701.con_contract_et_hd">
                <a:fields>
                    <a:field name="deposit_deduction_flag"/>
                    <a:field name="deposit_deduction_flag_desc" displayField="code_value_name" options="deposit_deduction_ds" returnField="deposit_deduction_flag" valueField="code_value"/>
                    <a:field name="deposit_by_agent"/>
                    <a:field name="deposit_by_agent_n" displayField="code_value_name" options="deposit_deduction_ds" returnField="deposit_by_agent" valueField="code_value"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="cashflowPlanDs" autoPageSize="true" model="cont.CON701.contract_et_cashflow_ln">
                <a:fields>
                    <a:field name="due_date" datatype="date"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="Screen_exit" text="关闭"/>
                <a:gridButton click="et_calc_entrance" text="测算"/>
                <a:gridButton click="et_save" text="HLS.SAVE"/>
                <a:gridButton click="et_submit_entrance" text="提交"/>
            </a:screenTopToolbar>
            <a:form column="4" labelWidth="110" marginWidth="30" title="基础信息">
                <a:textField name="contract_number" bindTarget="contractQueryScreen_mainDs" prompt="合同编号" readOnly="true"/>
                <a:textField name="bp_name" bindTarget="contractQueryScreen_mainDs" prompt="承租人名称" readOnly="true"/>
                <a:textField name="bp_agent" bindTarget="contractQueryScreen_mainDs" prompt="代理商名称" readOnly="true"/>
                <a:datePicker name="termination_date" bindTarget="contractQueryScreen_mainDs" prompt="中途偿还基准日期" renderer="Leaf.formatDate"/>
            </a:form>
            <div id="detail_flag_id">
                <a:form title="结清信息">
                    <a:form column="4" labelWidth="110" marginWidth="30">
                        <a:numberField name="undue_principal" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期本金" readOnly="true"/>
                        <a:numberField name="ref_n01" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期本金" readOnly="true"/>
                        <a:numberField name="ref_n02" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期分期手续费" readOnly="true"/>
                        <a:numberField name="ref_n08" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="预付金额" readOnly="true"/>

                        <a:numberField name="ref_n03" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息" readOnly="true"/>
                        <a:numberField name="ref_n06" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="逾期利息" readOnly="true"/>
                        <a:numberField name="ref_n07" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="未到期分期手续费" readOnly="true"/>
                        <a:numberField name="et_total_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还结清总金额" readOnly="true"/>

                        <a:numberField name="et_fee" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费" readOnly="true"/>
                        <a:numberField name="penalty" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息" readOnly="true"/>
                        <a:numberField name="residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金" readOnly="true"/>
                        <a:numberField name="deferred_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="延期利息" readOnly="true"/>

                        <a:numberField name="ref_n05" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="中途偿还手续费减免金额"/>
                        <a:numberField name="ref_n04" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="罚息减免金额"/>
                        <a:numberField name="deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="客户保证金" readOnly="true"/>
                        <a:comboBox name="deposit_deduction_flag_desc" bindTarget="early_terminationDs" prompt="客户保证金是否抵扣"/>

                        <a:numberField name="reduce_period_interest" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="期间利息减免金额" />
                        <a:numberField name="reduce_residual_value" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="留购金减免金额" />
                        <a:numberField name="third_party_deposit" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="代理商代付保证金金额" readOnly="true"/>
                        <a:comboBox name="deposit_by_agent_n" bindTarget="early_terminationDs" prompt="代理商代付保证金是否抵扣" />

                        <a:datePicker name="remittance_date"  bindTarget="early_terminationDs"  prompt="汇款日期" readOnly="false" renderer="Leaf.formatDate"/>
                        <a:numberField name="remittance_amount" allowDecimals="true" allowFormat="true" bindTarget="early_terminationDs" decimalPrecision="2" prompt="汇款金额" readOnly="false"/>

                    </a:form>
                    <a:form column="1" labelWidth="110" marginWidth="30">
                        <a:textArea name="note_s" width="800" bindTarget="early_terminationDs"  prompt="申请原因说明/背景"/>
                        <a:textArea name="cost_reduction" width="800" bindTarget="early_terminationDs"  prompt="费用减免说明"/>
                    </a:form>
                    <a:tabPanel marginHeight="400" marginWidth="30">
                        <a:tabs>
                            <a:tab prompt="资金计划" width="110">
                                <a:grid bindTarget="cashflowPlanDs" marginHeight="430" marginWidth="100" navBar="true">
                                    <a:columns>
                                        <a:column name="times" prompt="期数" align="center"/>
                                        <a:column name="cf_item_n" prompt="现金流类型" align="center"/>
                                        <a:column name="due_date" prompt="预定支付日期" align="center" renderer="Leaf.formatDate"/>
                                        <a:column name="due_amount" align="right" prompt="应收金额" renderer="Leaf.formatMoney"/>
                                        <a:column name="received_amount" align="right" prompt="已收金额" renderer="Leaf.formatMoney"/>
                                        <a:column name="received_principal" align="right" prompt="已收本金" renderer="Leaf.formatMoney"/>
                                        <a:column name="received_interest" align="right" prompt="已收利息" renderer="Leaf.formatMoney"/>
                                        <a:column name="cf_status_n" prompt="现金流状态" align="center"/>
                                    </a:columns>
                                </a:grid>
                            </a:tab>
                        </a:tabs>
                    </a:tabPanel>
                </a:form>
            </div>
        </a:screenBody>
        <script type="text/javascript"><![CDATA[
            Leaf.onReady(init);
            
            function init() {
                var record = $('contractQueryScreen_mainDs').getCurrentRecord();
                record.getField('contract_number').setReadOnly(true);
                record.getField('contract_name').setReadOnly(true);
                record.getField('received_times').setReadOnly(true);
                document.getElementById("detail_flag_id").style.display = "none";
            }
        ]]></script>
    </a:view>
</a:screen>
