<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: hsx
    $Date: 2018年8月8日17:29:18
    $Revision: 1.0
    $Purpose:罚息减免新增
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" dynamiccreateenabled="true" trace="true">
    <a:view>
        <a:link id="${/parameter/@layout_code}_con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <script src="${/request/@context_path}/javascripts/jquery-1.6.4.min.js" type="text/javascript"/>
        <a:link id="contract_penalty_calc_link_id" model="cont.CON1020.con_contract_penalty_info_save" modelaction="batch_update"/>
        <a:link id="${/parameter/@layout_code}_con_contract_modify_link" url="${/request/@context_path}/modules/cont/CON1010/con_contract_modify.lview"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@tree_code}_con500_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@tree_code}_con500_cdd_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <script type="text/javascript"><![CDATA[
        jQuery.noConflict();
        //测算逻辑
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
            //锁屏
            var ds_id1 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            var record1 = $(ds_id1).getCurrentRecord();
            if($(ds_id1).validate()){
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_penalty_info_save/execute',
                    para: {
                        penalty_reduce_id: record1.get('penalty_reduce_id'),
                        base_date: record1.get('base_date'),
                        penalty_reduction: record1.get('penalty_reduction'),
                        reduce_amount_total: record1.get('reduce_amount_total'),
                        reduce_amount_total_ratio: record1.get('reduce_amount_total_ratio')
                    },
                    success: function (res) {
                        if (res.result.header_result == 'success') {
                            var ds_id2 = 'CON_PENALTY_REDUCTION_MAINTAIN_G_PENALTY_DETAIL_con_contract_cashflow_ds';
                            $(ds_id2).setQueryParameter('copy_contract_id',record1.get('copy_contract_id'));
                            $(ds_id2).setQueryParameter('copy_contract_id',record1.get('copy_contract_id'));
                            $(ds_id2).query();
                            debugger
                            var records2 = $(ds_id2).getAll();
                            if (records2 && records2.length != 0) {
                                var param_list = [];
                                var param = {};
                                for (var i = 0; i < records2.length; i++) {
                                    param['penalty_info_id'] = records2[i].get('penalty_info_id');
                                    param['penalty_reduce_amount'] = records2[i].get('penalty_reduce_amount');
                                    param['_status'] = 'update';
                                    param_list.push(param);
                                }
                                Leaf.request({
                                    url: $('contract_penalty_calc_link_id').getUrl(),
                                    para: param_list,
                                    success: function (res1) {
                                        var copy_contract_id = record1.get('copy_contract_id');
                                        var base_date = record1.get('base_date');
                                        Leaf.request({
                                            url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_penalty_calculate/update',
                                            para: {
                                                copy_contract_id: copy_contract_id,
                                                base_date: base_date
                                            },
                                            success: function (res2) {
                                                Leaf.SideBar.show({
                                                    msg: '测算成功',
                                                    duration: 2000
                                                });
                                                $('CON_PENALTY_REDUCTION_MAINTAIN_F_QUERY_con_contract_penalty_reduce_ds').query();
                                                $('CON_PENALTY_REDUCTION_MAINTAIN_F_CASHFLOW_DETAIL_con_contract_cashflow_ds').query();
                                                $('CON_PENALTY_REDUCTION_MAINTAIN_G_PENALTY_DETAIL_con_contract_cashflow_ds').query();
                                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                            },
                                            failure: function () {
                                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                            },
                                            error: function () {
                                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                            },
                                            scope: this
                                        });
                                    },
                                    failure: function () {
                                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    error: function () {
                                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    scope: this
                                });
                            } else {
                                var copy_contract_id = record1.get('copy_contract_id');
                                var base_date = record1.get('base_date');
                                Leaf.request({
                                    url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_penalty_calculate/update',
                                    para: {
                                        copy_contract_id: copy_contract_id,
                                        base_date: base_date
                                    },
                                    success: function (res3) {
                                        Leaf.SideBar.show({
                                            msg: '测算成功',
                                            duration: 2000
                                        });
                                        $('CON_PENALTY_REDUCTION_MAINTAIN_F_QUERY_con_contract_penalty_reduce_ds').query();
                                        $('CON_PENALTY_REDUCTION_MAINTAIN_F_CASHFLOW_DETAIL_con_contract_cashflow_ds').query();
                                        $('CON_PENALTY_REDUCTION_MAINTAIN_G_PENALTY_DETAIL_con_contract_cashflow_ds').query();
                                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    failure: function () {
                                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    error: function () {
                                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                    },
                                    scope: this
                                });
                            }

                        }
                    },
                    failure: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    error: function () {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            }


        };
        //分配罚息
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function () {
            //锁屏
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var ds_id1 = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_contract_penalty_reduce');
            var record1 = $(ds_id1).getCurrentRecord();

            if( record1.get('reduce_amount_total')<0){
                $L.showInfoMessage('提示','罚息减免总金额不能小于0');
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                return;
            }
            Leaf.request({
                url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_penalty_info_save/execute',
                para: {
                    penalty_reduce_id: record1.get('penalty_reduce_id'),
                    base_date: record1.get('base_date'),
                    penalty_reduction: record1.get('penalty_reduction'),
                    reduce_amount_total: record1.get('reduce_amount_total'),
                    reduce_amount_total_ratio: record1.get('reduce_amount_total_ratio')
                },
                success: function (res) {
                    if (res.result.header_result == 'success') {
                        var ds_id2 = 'CON_PENALTY_REDUCTION_MAINTAIN_G_PENALTY_DETAIL_con_contract_cashflow_ds';
                        $(ds_id2).setQueryParameter('copy_contract_id',record1.get('copy_contract_id'));
                        $(ds_id2).query();
                        var records2 = $(ds_id2).getAll();
                        if (records2 && records2 != 0) {
                            var param_list = [];
                            var param = {};
                            for (var i = 0; i < records2.length; i++) {
                                param['penalty_info_id'] = records2[i].get('penalty_info_id');
                                param['penalty_reduce_amount'] = records2[i].get('penalty_reduce_amount');
                                param['_status'] = 'update';
                                param_list.push(param);
                            }
                            Leaf.request({
                                url: $('contract_penalty_calc_link_id').getUrl(),
                                para: param_list,
                                success: function (res1) {
                                    var copy_contract_id = record1.get('copy_contract_id');
                                    var base_date = record1.get('base_date');
                                    Leaf.request({
                                        url: '${/request/@context_path}/autocrud/cont.CON1020.con_contract_penalty_calculate/execute',
                                        para: {
                                            copy_contract_id: copy_contract_id
                                        },
                                        success: function (res2) {
                                            Leaf.SideBar.show({
                                                msg: '测算成功',
                                                duration: 2000
                                            });
                                            $('CON_PENALTY_REDUCTION_MAINTAIN_F_QUERY_con_contract_penalty_reduce_ds').query();
                                            $('CON_PENALTY_REDUCTION_MAINTAIN_F_CASHFLOW_DETAIL_con_contract_cashflow_ds').query();
                                            $('CON_PENALTY_REDUCTION_MAINTAIN_G_PENALTY_DETAIL_con_contract_cashflow_ds').query();
                                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                        },
                                        failure: function () {
                                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                        },
                                        error: function () {
                                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                        },
                                        scope: this
                                    });
                                },
                                failure: function () {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                error: function () {
                                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                },
                                scope: this
                            });
                        } else {
                            Leaf.showMessage('提示', '该单据尚未进行测算,或该基准日下不存在罚息!');
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        }

                    }
                },
                failure: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                error: function () {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                },
                scope: this
            });
        };

        var update_falg = 'N';

        //更新时调用
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function (ds, record, name, value, old_value, bp_seq) {
            if (value != old_value) {
                if (update_falg != 'Y') {

                    if (name == 'reduce_amount_total') {

                      //  record.set('reduce_amount_total',value>record.get('unreceived_overdue_amount') ?record.get('unreceived_overdue_amount'):value);
                       if(value>record.get('unreceived_overdue_amount')){
                           $L.showErrorMessage('提示','减免金额不能大于最大减免金额!');
                        //   record.set('reduce_amount_total',record.get('unreceived_overdue_amount'));
                        //   $(ds).getAt(0).set('reduce_amount_total','');
                           return;
                       }
                        var ratio = parseFloat(Leaf.formatNumber(div(record.get('reduce_amount_total'), record.get('total_overdue_amount')), 4));
                      //  update_falg = 'Y';
                        record.set('reduce_amount_total_ratio', ratio);

                    }


                   /* if (name == 'reduce_amount_total_ratio') {
                        var amount = parseFloat(mul(record.get('reduce_amount_total_ratio'), record.get('total_overdue_amount')), 2);
                     //   update_falg = 'Y';
                        record.set('reduce_amount_total', amount);

                    }*/
                } else {
                    update_falg = 'N';
                }
            }
        };

        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function (ds, record) {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();

            var advance_agent = record.get('advance_agent');
            if (advance_agent == 2 || advance_agent == 3) {
                if (record.get('agent_advance_amount') == null) {
                    Leaf.showMessage('${l:PROMPT}', '代理商垫款金额不能为空');
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    return false;
                }
            }
            return true;
            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();

        };
        //add by lihh 20210323
        window['${/parameter/@layout_code}_con500_cdd_attachtment_upload'] = function(id, name, query_only) {
            var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
            if (record.get('check_id')) {
                var url;
                if (query_only == 'Y'|| '${/parameter/@function_usage}' == 'QUERY') {
                    url = $('${/parameter/@layout_code}${/parameter/@tree_code}_con500_cdd_downloadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                } else {
                    url = $('${/parameter/@layout_code}${/parameter/@tree_code}_con500_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                }
                var win = new Leaf.Window({
                    url: url,
                    title: '${l:HLS.SUPPORTING_DOCUMENT}',
                    id: '${/parameter/@layout_code}${/parameter/@tree_code}_con_cdd_uploadFile_screen_id',
                    width: 850,
                    height: 400
                });

                win.on('close', function() {
                    record.ds.query();
                });
            } else {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');
            }
        };

        //超链接渲染
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record, bp_seq) {
            var link_function = '';
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
            if (name == 'function_tips') {
                return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\');">违约金减免信息需要在测算后才展示</a>';
            }else if (name == 'attachment') {
                link_function = '${/parameter/@layout_code}_con500_cdd_attachtment_upload';
                return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</a>';
            } else if (name == 'attach_file_name') {
                if (value != null) {
                    var link = '${/request/@context_path}/atm_download.lsc?attachment_id=';
                    var str = value.split(';;');
                    var url = '';
                    for (var i = 0;i < str.length;i++) {
                        var temp = str[i].split('--');
                        if (!Leaf.isEmpty(temp[0])) {
                            url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                        }
                    }
                    return url;

                }
            }
        };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>