<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author:  Yimeng
    $Date: 2015-05-06 下午01:58:31  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true" trace="true">
    <a:init-procedure/>
    <a:view>
        <a:link id="con733_prj_secify_approver_link" url="${/request/@context_path}/modules/prj/PRJ501N/cus_hls_specify_approver.lview"/>
        <a:link id="con733_submit_link" model="cont.CON733.con_debt_exemption_submit" modelaction="execute"/>
        <a:link id="con733_cancel_link" model="cont.CON733.con_debt_change_cancel" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_con733_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="${/parameter/@layout_code}_con733_contract_modify_link" url="${/request/@context_path}/modules/cont/CON301/con_contract_query_n.lview"/>
        <a:link id="${/parameter/@layout_code}_con_contract_change_link" url="${/request/@context_path}/modules/cont/CON731/hls_con_contract_change_detail.lview"/>
        <a:link id="con_contract_payment_details_link" url="${/request/@context_path}/modules/cont/CON305/con_contract_payment_details.lview"/>
        <script type="text/javascript"><![CDATA[
            function open_contract_win(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                // $(ds_id).find(record_id);
                var param = record.data;
                param['function_code'] = 'CON301R';
                param['function_usage'] = 'QUERY';
                param['maintain_type'] = 'QUERY';

                param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con733_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_con733_contract_modify_link', ds_id,'${/parameter/@layout_code}');
            }
            window['${/parameter/@layout_code}_balance_detail'] = function(record_id, ds_id) {
               /*  var record = window['${/parameter/@layout_code}_hls_link_render_record'][record_id + '---' + name]; */
               var record = $(ds_id).findById(record_id);
                var contract_id = record.get('contract_id');
                new Leaf.Window({
                    title: '合同收款明细(CON_BALANCE_RECIEVED)',
                    id: 'con_contract_payment_details_link_id',
                    url: $('con_contract_payment_details_link').getUrl(),
                    params: {
                        'contract_id': contract_id,
                        'function_code': 'CON315',
                        'layout_code': 'CON_BALANCE_RECIEVED'
                    },
                    fullScreen: true
                });
            };
        window['${/parameter/@layout_code}_open_change_detail'] = function(record_id, ds_id) {
                var record = $(ds_id).findById(record_id);
                var param = record.data;
                param['change_req_id'] = record.get('contract_id');
                param['contract_id'] = record.get('contract_id');
                param['function_code'] = 'CON732D_WFL';
                if (record.get('business_type') == 'ET') {
                    param['layout_code'] = 'CCR_ET';
                } else {
                    param['layout_code'] = 'CCR_UNIVERSAL';
                }
                param['function_usage'] = 'QUERY';
                param['maintain_type'] = 'READONLY';
                param['show_history_flag'] = 'Y';
                param['url_title'] = '变更申请-支付期调整 ';
                param['winid'] = '${/parameter/@layout_code}_con_contract_change_link_winid';
                var win = new Leaf.Window({
                    id: '${/parameter/@layout_code}_con_contract_change_link_winid',
                    params: param,
                    url: $('${/parameter/@layout_code}_con_contract_change_link').getUrl(),
                    title: '变更申请-支付期调整',
                    fullScreen: true,
                    draggable: true,
                    closeable: false
                });
            };  
            
            window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
                if (name == 'contract_number' && value) {
                    return '<a href="javascript:open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
                }else if (name == 'change_detail') {
                    link_function = '${/parameter/@layout_code}_open_change_detail';
                    return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + record.ds.id + '\');"><font color="red">' + config_record.get('prompt') + '</font></a>';
                } else if (name == 'balance_link') {
                    link_function = '${/parameter/@layout_code}_balance_detail';
                    return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + record.ds.id + '\');"><font color="red">' + config_record.get('prompt') + '</font></a>';
            
                }
                return value;
            };
            
            //保存前调用
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function(ds, record) {
                window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                var debt_lines_cf_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req_cf');
                var debt_lines_cf_ds = $(debt_lines_cf_dsid);
                var line_records = debt_lines_cf_ds.getAll();
                var flag = 0;
                for (i = 0;i < line_records.length;i++) {
                    exemption_amount = line_records[i].get('exemption_amount');
                    due_amount = line_records[i].get('due_amount');
                    if (exemption_amount > due_amount) {
                        line_records[i].set('exemption_amount', '');
                        flag = 1;
                    }
                }
                if (flag == 1) {
                    Leaf.showMessage('${l:PROMPT}', '免除金额不可大于应收金额！');
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    return;
                } else {
                    con733_set_amount();
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
            };
            
             //更新时
            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'], 'con_debt_exemption_req_cf');
                if (ds.id == ds_id && name == 'exemption_amount') {
              		var cf_type = record.get('cf_type');
              		if (cf_type =='200'){
              		    var exemption_amount = parseFloat(record.get('exemption_amount'));
	                    var tax_rate = parseFloat(record.get('tax_rate')== undefined ? 0 : record.get('tax_rate'));
	                    var tax_rate_field = record.getField('tax_rate');
	                    var net_exemption_amount = (exemption_amount/(1+tax_rate)).toFixed(2);
	            
	                    if (value) {
	                        if (tax_rate < 0 || tax_rate > 1) {
	            
	                            Leaf.showMessage('提示', '税率应在0到1之间！', null, 350);
	                            record.set('vat_exemption_amount', null);
	                            record.set('net_exemption_amount', null);
	                            return;
	                        }
	                        record.set('vat_exemption_amount', exemption_amount - net_exemption_amount);
	                        record.set('net_exemption_amount', net_exemption_amount);
	                    } else {
	                        record.set('vat_exemption_amount', '');
	                        record.set('net_exemption_amount', exemption_amount);
	                    }
              		}
                    
                }
            };
            
            // //保存submitsuccess调用
            // window['${/parameter/@layout_code}_on_layout_dynamic_submitsuccess'] = function(ds, record, res, bp_seq) {
                //
                // alert('jj');
                // var debt_head_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                // var debt_lines_cf_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req_cf');
                // var debt_lines_cf_ds = $(debt_lines_cf_dsid);
                // var debt_head_ds = $(debt_head_dsid);
                // var exemption_amount;
                // var cf_type;
                // var total_amount = 0;
                // var total_amt_legal = 0;
                // var head_record = debt_head_ds.getCurrentRecord();
                // var line_records = debt_lines_cf_ds.getAll();
            
                // for (i = 0;i < line_records.length;i++) {
                    // exemption_amount = line_records[i].get('exemption_amount');
                    // cf_type = line_records[i].get('cf_type');
                    // if (!exemption_amount) {
                        // exemption_amount = 0;
                    // }
                    // if (cf_type == '9') {
                        // total_amount = total_amount + parseFloat(exemption_amount);
                    // } else if (cf_type == '200') {
                        // // alert(total_amt_legal);
                        // total_amt_legal = total_amt_legal + parseFloat(exemption_amount);
                    // }
                // }
            
                // head_record.set('alloc_exemption_amount', total_amount);
                // head_record.set('alloc_exemption_amount_legal', total_amt_legal);
                // debt_head_ds.query();
                // window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            // };
            
            function con733_set_amount (){
                var debt_head_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                var debt_lines_cf_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req_cf');
                var debt_lines_cf_ds = $(debt_lines_cf_dsid);
                var debt_head_ds = $(debt_head_dsid);
                var exemption_amount;
                var cf_type;
                var total_amount = 0;
                var total_amt_legal = 0;
                var head_record = debt_head_ds.getCurrentRecord();
                var line_records = debt_lines_cf_ds.getAll();
            
                for (i = 0;i < line_records.length;i++) {
                    exemption_amount = line_records[i].get('exemption_amount');
                    cf_type = line_records[i].get('cf_type');
                    if (!exemption_amount) {
                        exemption_amount = 0;
                    }
                    if (cf_type == '9') {
                        total_amount = total_amount + parseFloat(exemption_amount);
                    } else if (cf_type == '200') {
                        // alert(total_amt_legal);
                        total_amt_legal = total_amt_legal + parseFloat(exemption_amount);
                    }
                }
            
                head_record.set('alloc_exemption_amount', total_amount);
                head_record.set('alloc_exemption_amount_legal', total_amt_legal);
                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
            }
            
            //自动分配
            window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function() {

                var debt_head_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                var debt_lines_cf_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req_cf');
                var debt_lines_cf_ds = $(debt_lines_cf_dsid);
                var debt_head_ds = $(debt_head_dsid);
            
                 
                var due_amount_h;
                var cf_type_h;
                var exemption_amount_h;
                var total_amount;
                var total_amt_legal;
                var head_record = debt_head_ds.getCurrentRecord();
                var line_records = debt_lines_cf_ds.getAll();
                var total_exemption_amount = head_record.get('total_exemption_amount');
                var total_exemption_amt_legal = head_record.get('total_exemption_amt_legal');
                var total_line_amount = head_record.get('total_line_amount');
                var total_line_amt_legal = head_record.get('total_line_amt_legal');
                if (total_exemption_amount > total_line_amount) {
                    Leaf.showMessage('${l:PROMPT}', '罚息减免金额大于总金额');
                }
                if (total_exemption_amt_legal > total_line_amt_legal) {
                    Leaf.showMessage('${l:PROMPT}', '法律减免金额大于总金额');
                }
                for (i = 0;i < line_records.length;i++) {
                    line_records[i].set('exemption_amount', '');
                }
                var amount = total_exemption_amount;
                var amt_legal = total_exemption_amt_legal;
                for (k = 0;amount > 0;k++) {
                    due_amount_h = line_records[k].get('due_amount');
                    net_amount_h = line_records[k].get('net_due_amount');
                    vat_amount_h = line_records[k].get('vat_due_amount');
                    cf_type_h = line_records[k].get('cf_type');
                    if (cf_type_h == '9') {
                        if (due_amount_h <= amount) {
                            line_records[k].set('exemption_amount', due_amount_h);
                            amount = amount - due_amount_h;
                            amount = amount.toFixed(2);
                        } else {
                            line_records[k].set('exemption_amount', amount);
                            amount = amount - due_amount_h;
                            amount = amount.toFixed(2);
                        }
                    }
                }
                /* for(j=0; amt_legal>0 ;j++){
                due_amount_h = line_records[j].get('due_amount');
                cf_type_h = line_records[j].get('cf_type');
                if (cf_type_h == '200'){
                if (due_amount_h <= amt_legal){
                line_records[j].set('exemption_amount',due_amount_h);
                amt_legal = amt_legal-due_amount_h;
                amt_legal = amt_legal.toFixed(2);
                }else{
                line_records[j].set('exemption_amount',amt_legal);
                amt_legal = amt_legal-due_amount_h;
                amt_legal = amt_legal.toFixed(2);
                }
                }
                } */
            
            };
            
           
            //加载时调用(grid,table,gridBox)
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_grid_load'] = function(ds, record, config_records, bp_seq) {
                var debt_head_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                var debt_lines_cf_dsid = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req_cf');
                var debt_lines_cf_ds = $(debt_lines_cf_dsid);
                var debt_head_ds = $(debt_head_dsid);
            
                var due_amount;
                var cf_type;
                var total_amount = 0;
                var total_amt_legal = 0;
                var head_record = debt_head_ds.getCurrentRecord();
                var line_records = debt_lines_cf_ds.getAll();
            
                for (i = 0;i < line_records.length;i++) {
                    due_amount = line_records[i].get('due_amount');
                    cf_type = line_records[i].get('cf_type');
                    if (!due_amount) {
                        due_amount = 0;
                    }
                    if (cf_type == '9') {
                        total_amount = total_amount + due_amount;
                    } else if (cf_type == '200') {
                        total_amt_legal = total_amt_legal + due_amount;
                    }
                }
                head_record.set('total_line_amount', total_amount);
                head_record.set('total_line_amt_legal', total_amt_legal);
            
            };
            
            //提交审批
            window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
                var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                var req_record = $(req_ds_id).getAt(0);
                if ($(req_ds_id).validate()) {
                    if (req_record.dirty) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '${l:HLS.EXECUTE_AFTER_SAVE}');
                        return;
                    }
                    Leaf.showConfirm('${l:HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', function() {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        Leaf.request({
                            url: $('con733_submit_link').getUrl(),
                            para: {
                                change_req_id: ${/parameter/@change_req_id}
                            },
                            success: function(res) {
                                window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                $('${/parameter/@winid}').close();
                            },
                            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_button1_layout_dynamic_click'] = function() {
                var req_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_debt_exemption_req');
                var req_record = $(req_ds_id).getAt(0);
            
                window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                Leaf.request({
                    url: $('con733_cancel_link').getUrl(),
                    para: {
                        change_req_id: ${/parameter/@change_req_id}
                    },
                    success: function(res) {
                        window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        $('${/parameter/@winid}').close();
                    },
                    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
                });
            };
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
