<?xml version="1.0" encoding="UTF-8"?>
<!--
  create by nyl  2014-12-3 
  for contract context create
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.function_code = ${/parameter/@function_code}" model="cus.CUS100.cus_default_function_name" rootPath="default_function_name"/>
        <a:model-query fetchAll="true" model="leg.LEG600.leg_default_law_firm" rootPath="default_law_firm"/>
        <a:model-query fetchAll="true" model="leg.LEG600.get_sysdate" rootPath="sysdate"/>
        <a:model-query fetchAll="true" model="leg.LEG100.get_default_country" rootPath="get_default_country"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con638_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="leg_legal_create_cashflow_link_id" model="cont.CON610.leg_legal_create_cashflow" modelaction="batch_update"/>
        <a:link id="leg_letter_batch_send_link_id" model="cont.CON611.leg_letter_batch_send" modelaction="batch_update"/>
        <a:link id="leg_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="leg_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <a:link id="leg400_print_link_id" url="${/request/@context_path}/modules/leg/LEG400/leg_content_print_detail.lview"/>
        <script type="text/javascript"><![CDATA[
            function leg600_open_contract_win(ds_id, record_id, name, type) {
            
            
                var legal_record;
            
                var url_id = 'leg_uploadFile_id';
                if (type == '1') {
                    ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                    legal_record = $(ds_id).getCurrentRecord();
                    url_id = 'leg_uploadFile_id';
                } else {
                    legal_record = $(ds_id).findById(record_id);
                    url_id = 'leg_downloadFile_id';
                }
                var url;
                //
                if (type == 1) {
                    url = $(url_id).getUrl() + '?table_name=LEG_LEGAL_LETTER&header_id=' + '${/parameter/@legal_letter_id}';
                } else {
                    url = $(url_id).getUrl() + '?table_name=LEG_LEGAL_LETTER&header_id=' + legal_record.data['legal_letter_id'];
                }
                new Leaf.Window({
                    url: url,
                    title: '附件',
                    id: 'leg_legal_letter_id',
                    width: 850,
                    height: 400
                });
            }
            
            window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
                if (name == 'attachment') {
                    return '<a href="javascript:leg600_open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\',\'' + 'attachment' + '\',' + '2' + ')">' + '附件' + '</a>';
                }
            
                return value;
            };
            
            function confirm_send_flag(send_flag) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds_id) {
                    if(!$(ds_id).validate(true)){
                        return;
                    }
                    var records = $(ds_id).getSelected();
                    if (records.length == 0) {
                        Leaf.showMessage('提示', '请选择数据！', null, 350);
                        return;
                    }
                    var param = new Object();
                    var datas = [];
                    for (var i = 0;i < records.length;i++) {
                        var obj = {};
            
                        obj['contract_id'] = records[i].get('contract_id');
                        obj['legal_letter_id'] = records[i].get('legal_letter_id');
                        obj['send_flag'] = send_flag;
            
                        obj['_status'] = 'update';
                        if (records[i].dirty == true) {
                            Leaf.showMessage('提示', '请先保存！', null, 350);
                            return;
                        }
                        datas[i] = obj;
                    }
            
                    window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.request({
                        url: $('leg_letter_batch_send_link_id').getUrl(),
                        para: datas,
                        success: function(res) {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            $(ds_id).query();
                        },
                        failure: function() {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        error: function() {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        scope: this
                    });
                }
            
            }
            
            //确认发送
            window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
                var send_flag = 'Y';
                confirm_send_flag(send_flag);
            };
            
            //确认不发送
            window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
                var send_flag = 'N';
                confirm_send_flag(send_flag);
            };
            
            //更新时
            window['${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_value, bp_seq) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds.id == ds_id && name == 'tax_rate' && value) {
                    var fee = parseFloat(record.get('fee'));
                    var tax_rate = parseFloat(value);
                    var net_fee = (fee/(1+tax_rate)).toFixed(2);
                    var tax_rate_field = record.getField('tax_rate');
                    if (value) {
                        if (tax_rate < 0 || tax_rate > 1) {
            
                            Leaf.showMessage('提示', '税率应在0到1之间！', null, 350);
                            record.set('tax_rate', null);
                            tax_rate_field.setValue(null);
                            record.set('vat_fee', null);
                            record.set('net_fee', null);
                            return;
                        }
                        record.set('vat_fee',fee - net_fee);
                        record.set('net_fee', net_fee);
                    } else {
                        record.set('vat_fee', '');
                        record.set('net_fee', fee);
                    }
                }
                if (ds.id == ds_id && name == 'fee' && record.get('tax_rate')) {
         
                    var tax_rate = parseFloat(record.get('tax_rate')== undefined ? 0 : record.get('tax_rate'));
                    var fee = parseFloat(value);
                      var net_fee = (fee/(1+tax_rate)).toFixed(2);
                    var tax_rate_field = record.getField('tax_rate');
                    if (value) {
                        if (tax_rate < 0 || tax_rate > 1) {
            
                            Leaf.showMessage('提示', '税率应在0到1之间！', null, 350);
                            record.set('tax_rate', null);
                            tax_rate_field.setValue(null);
                            record.set('vat_fee', null);
                            record.set('net_fee', null);
            
                            return;
                        }
                        record.set('vat_fee', fee  - net_fee);
                        record.set('net_fee', net_fee);
                    } else {
                        record.set('vat_fee', '');
                        record.set('net_fee', fee);
                    }
                }
            
            };
            
            //生成律师函
            /*   window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
             var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
             if (ds_id) {
             var records = $(ds_id).getSelected();
             if (records.length != 1) {
             Leaf.showMessage('提示', '请选择单条数据！', null, 350);
             return;
             }
             var param = new Object();
             param['legal_letter_id'] = records[0].get('legal_letter_id');
             param['contract_id'] = records[0].get('contract_id');
             param['winid'] = 'leg_print_detail_winid';
             param['function'] = 'LEG600D';
             new Leaf.Window({
             id: 'leg_print_detail_winid',
             params: param,
             url: $('leg400_print_link_id').getUrl(),
             title: '法务文本打印',
             width: 900,
             height: 450
             });
             }
             }; */
            //创建费用现金流
            window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                if (ds_id ) {
                    if(!$(ds_id).validate(true)){
                        return;
                    }
                    var records = $(ds_id).getSelected();
                    if (records.length == 0) {
                        Leaf.showMessage('提示', '请选择数据！', null, 350);
                        return;
                    }
                    var param = new Object();
                    var datas = [];
                    for (var i = 0;i < records.length;i++) {
                        if (records[i].dirty == true) {
                            Leaf.showMessage('提示', '请先保存！', null, 350);
                            return;
                        }
                        var obj = {};
                        obj['contract_id'] = records[i].get('contract_id');
                        obj['legal_letter_id'] = records[i].get('legal_letter_id');
            
                        obj['_status'] = 'update';
                        datas[i] = obj;
                    }
            
            
                    window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.request({
                        url: $('leg_legal_create_cashflow_link_id').getUrl(),
                        para: datas,
                        success: function(res) {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        failure: function() {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        error: function() {
                            window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        scope: this
                    });
            
                }
            };
            //查询时调用(grid,table,gridBox)
            window['${/parameter/@layout_code}_on_layout_dynamic_grid_query'] = function(ds, qpara, bp_seq) {
            
                var letter_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
            
                if (ds.id == letter_ds_id && letter_ds_id) {
                    aut_authority_list_validate_query(ds, qpara);
                }
            
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_authority_list_validate.lview?document_category=CONTRACT&amp;function_code=CON611"/>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
