<?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:view>
        <a:link id="leg_legal_fee_create_link_id" model="leg.LEG450.leg_legal_fee_create" modelaction="batch_update"/>
        <!--  <a:link id="leg_legal_fee_delete_link_id" model="leg.LEG450.leg_legal_fee_create" modelaction="delete"/> -->
        <a:link id="leg_fee_create_link_id" url="${/request/@context_path}/modules/leg/LEG600/leg_fee_create.lsc"/>
        <a:link id="leg_legal_fee_delete_link_id" url="${/request/@context_path}/modules/leg/LEG450/leg_fee_dml.lsc"/>
        <script type="text/javascript"><![CDATA[
            //保存前调用
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function(ds, record) {
            
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
            
                var records = $(ds_id).getAll();
                for (var i = 0;i < records.length;i++) {
            
                    //if (records[i].dirty == true) {
                    var vat_amount = records[i].get('vat_amount') == undefined ? 0 : records[i].get('vat_amount');
                    var net_amount = records[i].get('net_amount') == undefined ? 0 : records[i].get('net_amount');
                    var amount = records[i].get('amount') == undefined ? 0 : records[i].get('amount');
                    if (amount <= 0 || vat_amount < 0 || net_amount < 0) {
                        Leaf.showMessage('${l:PROMPT}', '金额小于零不合法！');
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return false;
                    }
                    if (amount != vat_amount + net_amount) {
            
                        Leaf.showMessage('${l:PROMPT}', '含税金额不等于不含税金额加税额！');
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        return false;
                    }
                    // }
                }
                var check_flag = true;
                return check_flag;
            };
            
            window['${/parameter/@layout_code}_user_button3_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
            
                var records = $(ds_id).getSelected();
            
                for (var i = 0;i < records.length;i++) {
                    if (records[i].dirty == true) {
                        Leaf.showMessage('${l:PROMPT}', '请先保存再确认');
                        return;
                    }
                }
            
                if (!records.length) {
                    Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录');
                    return;
                }
            
            
            
                var param = new Object();
                var datas = [];
            
                if (records.length == 0) {
                    Leaf.showInfoMessage('信息', '请至少选择一行！', null, 200, 100);
                }
            
                for (var i = 0;i < records.length;i++) {
                    if (records[i].get('legal_fee_id')) {
            
                        data = {};
                        data['legal_fee_id'] = records[i].get('legal_fee_id');
                        data['_status'] = 'update';
                        datas.push(data);
            
                    }
                }
                param['details'] = datas;
                var url_l = $('leg_legal_fee_create_link_id').getUrl();
                Leaf.request({
                    url: url_l,
                    para: datas,
                    success: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $(ds_id).query();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    failure: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            
            };
            //新增
            window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
                var ds = $(ds_id);
                //
                $(ds_id).create();
                var record = $(ds_id).getCurrentRecord();
            
                record.getField('return_amount').setReadOnly(false);
            
            
            };
            
            //加载时调用(grid,table,gridBox)
            /*       window['${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function(ds, record, config_records, bp_seq) {
             var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
             var records = $(ds_id).getAll();
             if (!records.length) {
             return;
             }
             for (var i = 0;i < records.length;i++) {
             //已确认的不可编辑
             if (records[i].data['status'] == 'C') {
             records[i].getField('cf_item').setReadOnly(true);
             records[i].getField('cf_item_n').setReadOnly(true);
             records[i].getField('occur_date').setReadOnly(true);
             records[i].getField('amount').setReadOnly(true);
             records[i].getField('due_date').setReadOnly(true);
             records[i].getField('description').setReadOnly(true);
             records[i].getField('return_date').setReadOnly(true);
             records[i].getField('return_amount').setReadOnly(true);
             }
             }
             };
             */
            
            window['${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function(ds, record, config_records, bp_seq) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
                var records = $(ds_id).getAll();
            
                if (!records.length) {
                    return;
                }
            
                for (var i = 0;i < records.length;i++) {
                    //已确认的不可编辑
                    if (records[i].data['csh_payment_req_flag'] == 'Y' || records[i].data['contract_status'] == 'PENDING') {
            
                        records[i].getField('cf_item').setReadOnly(true);
                        records[i].getField('cf_item_n').setReadOnly(true);
                        records[i].getField('occur_date').setReadOnly(true);
                        records[i].getField('amount').setReadOnly(true);
                        records[i].getField('due_date').setReadOnly(true);
                        records[i].getField('description').setReadOnly(true);
                        records[i].getField('return_date').setReadOnly(true);
                        records[i].getField('return_amount').setReadOnly(true);
                    }
                }
            
            };
            
            
            //删除
            window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_fee');
            
                var records = $(ds_id).getSelected();
                var datas = [];
            
                if (records.length == 0) {
                    Leaf.showInfoMessage('信息', '请至少选择一行！', null, 200, 100);
                }
                //校验
                /*  for (var i = 0;i < records.length;i++) {
                 var status = records[i].get('status');
                 if (status == 'C') {
                 Leaf.showMessage("${l:PROMPT}", "已确认费用不能删除！");
                 return;
                 }
                 }
                 for (var i = 0;i < records.length;i++) {
                 $(ds_id).remove(records[i]);
                 } */
                for (var i = 0;i < records.length;i++) {
                    if (records[i].get('legal_fee_id')) {
            
                        data = {};
                        data['legal_fee_id'] = records[i].get('legal_fee_id');
                        data['_status'] = 'delete';
                        datas.push(data);
            
                    }
                }
            
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                var url_l = $('leg_legal_fee_delete_link_id').getUrl();
                Leaf.request({
                    url: url_l,
                    para: datas,
                    success: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $(ds_id).query();
                    },
                    error: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    failure: function() {
                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    },
                    scope: this
                });
            
            
            
            };
            
            function leg450_open_contract_win(record_id, name, type) {
            
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'leg_legal_letter');
                var legal_record = $(ds_id).getCurrentRecord();
                var url_id = 'leg_uploadFile_id';
                if (type == '1') {
                    url_id = 'leg_uploadFile_id';
                } else {
                    url_id = 'leg_downloadFile_id';
                }
            
                var 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:leg450_open_contract_win(\'' + record.id + '\',\'' + 'attachment' + '\',' + '2' + ')">' + '附件' + '</a>';
                }
            
                return value;
            };
            
            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_fee');
                if (ds.id == ds_id && name == 'tax_rate') {
                    var amount = parseFloat(record.get('amount'));
                    var tax_rate = parseFloat(value);
                    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_amount', amount * parseFloat(value).toFixed(2));
                        record.set('net_amount', amount - amount * parseFloat(value).toFixed(2));
                    } else {
                        record.set('vat_amount', '');
                        record.set('net_amount', amount);
                    }
                } else if (ds.id == ds_id && name == 'amount' && record.get('tax_rate')) {
                
                    var tax_rate = parseFloat(record.get('tax_rate'));
                    var amount = parseFloat(value);
                    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_amount', amount * parseFloat(tax_rate).toFixed(2));
                        record.set('net_amount', amount - amount * parseFloat(tax_rate).toFixed(2));
                    } else {
                        record.set('vat_amount', '');
                        record.set('net_amount', amount);
                    }
                }
            
            };
            
            
            
            //新增时调用(grid,table,gridBox)
            window['${/parameter/@layout_code}_on_layout_dynamic_grid_add'] = function(ds, record, config_records, bp_seq) {
                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'CON_CONTRACT');
                var con_record = $(ds_id).getCurrentRecord();
                record.set('contract_id', con_record.data['contract_id']);
                record.set('status_n', '新建');
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
