<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: Calvin  
    $Date: 2017年12月11日17:29:18
    $Revision: 1.0  
    $Purpose:车辆处置详情界面 
-->
<a:screen xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" dynamiccreateenabled="true" trace="true">
    <a:init-procedure>
        <a:model-query defaultWhereClause="t1.contract_lease_item_id = ${/parameter/@contract_lease_item_id}" fetchAll="true" model="cont.CON6323.con_collection_calc" rootPath="con_car_fee_path"/>
        <s:server-script import="contract_print_path.js"><![CDATA[
           $ctx.parameter.file_path = con_print_path['con_print_path'];
           $ctx.parameter.tomcat_source = con_print_path['tomcat_source'];
        ]]></s:server-script>
    </a:init-procedure>
    <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"/>
        <a:link id="${/parameter/@layout_code}_car_modify_special_link" url="${/request/@context_path}/modules/prj/PRJ500N/prj_project_create.lview"/>
        <a:link id="${/parameter/@layout_code}_con_contract_modify_link" url="${/request/@context_path}/modules/cont/CON505/con_contract_modify.lview"/>
        <a:link id="${/parameter/@layout_code}_hls_bp_master_query_link" url="${/request/@context_path}/modules/hls/HLS214N/hls_bp_master_modify.lview"/>
        <a:link id="${/parameter/@layout_code}_con632_close_link_id" url="${/request/@context_path}/modules/cont/CON632/con_collection_detail.lview"/>
        <a:link id="${/parameter/@layout_code}_con632_callback_link_id" url="${/request/@context_path}/modules/cont/CON632/con_collection_callback.lview"/>
        <a:link id="${/parameter/@layout_code}_col_detail_link" url="${/request/@context_path}/modules/cont/CON632/con_collection_phone_detail.lview"/>
        <a:link id="${/parameter/@layout_code}_submit_approval_link" model="cont.CON6323.con_collection_management_wfl" modelaction="execute"/>
        <a:link id="${/parameter/@layout_code}_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="${/parameter/@layout_code}_cdd_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <link href="${/request/@context_path}/css/lightbox.css" rel="stylesheet" type="text/css"/>
        <script src="${/request/@context_path}/javascripts/lightbox.js" type="text/javascript"/>
        <script src="${/request/@context_path}/javascripts/jquery-1.6.4.min.js" type="text/javascript"/>
        <script type="text/javascript"><![CDATA[
            jQuery.noConflict();
            Ext.ux.Lightbox.register('a[ref=img]', true);
            //锁屏
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window'] = function() {
                var detail_mask;
                if ('${/parameter/@winid}') {
                    if (parent.$L.CmpManager.get('${/parameter/@winid}')) {
                        detail_mask = parent.$('${/parameter/@winid}').wrap;
                        parent.Leaf.Masker.mask(detail_mask, '${l:HLS.EXECUTING}');
                    } else {
                        detail_mask = $('${/parameter/@winid}').wrap;
                        Leaf.Masker.mask(detail_mask, '${l:HLS.EXECUTING}');
                    }
                } else {
                    detail_mask = Ext.getBody();
                    Leaf.Masker.mask(detail_mask, '${l:HLS.EXECUTING}');
                }
            };
            //解屏
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window'] = function() {
                var detail_mask;
                if ('${/parameter/@winid}') {
                    if (parent.$L.CmpManager.get('${/parameter/@winid}')) {
                        detail_mask = parent.$('${/parameter/@winid}').wrap;
                        parent.Leaf.Masker.unmask(detail_mask);
                    } else {
                        detail_mask = $('${/parameter/@winid}').wrap;
                        Leaf.Masker.unmask(detail_mask);
                    }
                } else {
                    detail_mask = Ext.getBody();
                    Leaf.Masker.unmask(detail_mask);
                }
                if (Ext.get(document.documentElement)) {
                    $L.Masker.unmask(Ext.get(document.documentElement));
                }
            };
            
            //更新时调用
            window['${/parameter/@layout_code}_on_layout_dynamic_update'] = function(ds, record, name, value, old_valuess) {

                var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_col_management_hd');
                if (ds.id == ds_id) {
                    // var v_princopal_difference, v_rent_difference, v_total_profit_loss;
                    //本金差异 = 处置设备融资额/支付表融资额*（支付表剩余本金-保证金）-实际处置价
                    //租金差异 = 处置设备融资额/支付表融资额*（支付表剩余租金-保证金）-实际处置价
                    //全部损益 = 租差+停车费+收车费+处置费(停车、维修、违章、过户、挂靠、评估)+(处置设备融资额/支付表融资额)*罚息
                    //以上为历史逻辑
            
                    //2018年03月02日15:04:16 新给计算逻辑
                    //本金差异 = 成交价格 - 收车费 - 停车费 - 营运费 - 维修费 - 违章费 - 过户费 -运输费 - 车辆保险 + 保证金 - 处置设备融资额/支付表融资额*支付表剩余本金
                    //租金差异 = 成交价格 - 收车费 - 停车费 - 营运费 - 维修费 - 违章费 - 过户费 -运输费 - 车辆保险 + 保证金 - 处置设备融资额/支付表融资额*支付表剩余租金
                    //全部损益 = 成交价格 - 收车费 - 停车费 - 营运费 - 维修费 - 违章费 - 过户费 -运输费 - 车辆保险 + 保证金 - 处置设备融资额/支付表融资额*（支付表剩余租金 + 支付表罚息）
            
            
                    // 收车费 CAR_COMMISSION
                    // 停车费 PARKING_FEE_O
                    // 营运费 OPERATION_COST
                    // 维修费 MAINTENANCE_COST
                    // 违章费 ILLEGAL_COST
                    // 过户费 CLOSING_COST
                    // 运输费 SHIPPING_CHARGE
                    // 车辆保险 CAR_INSURANCE
                    // 保证金 处置设备融资额/支付表融资额*保证金
                    var deal_price = record.get('deal_price') || 0; // 成交价格
                    var car_commission = record.get('car_commission') || 0; // 收车费 CAR_COMMISSION
                    var parking_fee_o = record.get('parking_fee_o') || 0; // 停车费 PARKING_FEE_O
                    var operation_cost = record.get('operation_cost') || 0;
                    var maintenance_cost = record.get('maintenance_cost') || 0;
                    var illegal_cost = record.get('illegal_cost') || 0;
                    var closing_cost = record.get('closing_cost') || 0;
                    var shipping_charge = record.get('shipping_charge') || 0;
                    var car_insurance = record.get('car_insurance') || 0;
            
                    var actual_management_price = record.get('actual_management_price') || 0; //实际处置价
                    var contract_lease_item_id = '${/model/con_car_fee_path/record/@contract_lease_item_id}'; //剩余本金
                    var over_principal = '${/model/con_car_fee_path/record/@over_principal}'; //剩余本金
                    var over_due_amount = '${/model/con_car_fee_path/record/@over_due_amount}'; //剩余租金
                    var interest = '${/model/con_car_fee_path/record/@interest}'; //支付表罚息
                    var sin_car_finance_amount = '${/model/con_car_fee_path/record/@sin_car_finance_amount}'; //处置设备融资额
                    var con_finance_amount = '${/model/con_car_fee_path/record/@con_finance_amount}'; //支付表融资额
                    var deposit = '${/model/con_car_fee_path/record/@deposit}'; //保证金

                    /* if (sin_car_finance_amount == 0) {
                     Leaf.showMessage('${l:HLS.PROMPT}', '处置设备融资额为0，请检查单据!');
                     return;
                     } else */

                        if (con_finance_amount == 0) {
                        Leaf.showMessage('${l:HLS.PROMPT}', '支付表融资额为0，请检查单据!');
                        return;
                    }
            
                    if (name == 'actual_management_price' || name == 'deal_price' || name == 'car_commission' || name == 'parking_fee_o' || name == 'operation_cost' || name == 'maintenance_cost' || name == 'illegal_cost' || name == 'closing_cost' || name == 'shipping_charge' || name == 'car_insurance') {
                        var principal_difference = 0;
                        var rent_difference = 0;
                        var total_profit_loss = 0;
                        // var a =  deal_price - car_commission - parking_fee_o - operation_cost - maintenance_cost - illegal_cost - closing_cost - shipping_charge - car_insurance;
                        // var b = ((sin_car_finance_amount / con_finance_amount) * deposit);
                        // var c = ((sin_car_finance_amount / con_finance_amount) * over_principal);
                        
                        principal_difference = deal_price - car_commission - parking_fee_o - operation_cost - maintenance_cost - illegal_cost - closing_cost - shipping_charge - car_insurance + ((sin_car_finance_amount / con_finance_amount) * deposit) - ((sin_car_finance_amount / con_finance_amount) * over_principal);
            
                        rent_difference = deal_price - car_commission - parking_fee_o - operation_cost - maintenance_cost - illegal_cost - closing_cost - shipping_charge - car_insurance + ((sin_car_finance_amount / con_finance_amount) *  deposit) - ((sin_car_finance_amount / con_finance_amount) * over_due_amount);
            
                        total_profit_loss = deal_price - car_commission - parking_fee_o - operation_cost - maintenance_cost - illegal_cost - closing_cost - shipping_charge - car_insurance + ((sin_car_finance_amount / con_finance_amount) * deposit) - ((sin_car_finance_amount / con_finance_amount )* (over_due_amount + interest));
            
                        record.set('principal_difference', principal_difference);
                        record.set('rent_difference', rent_difference);
                        record.set('total_profit_loss', total_profit_loss);
                    }
            
                    // if (name == "actual_management_price") {
                    // if (value) {
                    // record.set('principal_difference', record.get('principal_diff') - value);
                    // record.set('rent_difference', record.get('rent_diff') - value);
                    // }
                    // }
            
                    // if (name == 'callback_fee' || name == "parking_fee" || name == "actual_management_price") {
                    // if (record.get('callback_fee') && record.get('parking_fee') && record.get('actual_management_price')) {
                    // record.set('total_profit_loss', record.get('rent_difference') + record.get('callback_fee') + record.get('parking_fee') + record.get('management_amount') + record.get('car_price_ratio'));
                    // }
                    // }
                }
            };
            
            //提交审批
            window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
                //提交先保存
                var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                if (root_ds.validate()) {
                    window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
                    var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_col_management_hd');
                    var record = $(ds_id).getCurrentRecord();
            
                    Leaf.showConfirm('${l:HLS.PROMPT}', '确认提交？', function() {
                        window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
                        Leaf.request({
                            url: $('${/parameter/@layout_code}_submit_approval_link').getUrl(),
                            para: {
                                management_id: record.get('management_id')
                            },
                            success: function(res) {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                                $('${/parameter/@winid}').close();
                                Leaf.SideBar.show({
                                    msg: '操作成功',
                                    duration: 2000
                                });
                            },
                            failure: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function() {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            scope: this
                        });
                    });
                }
            };
            
            var isLoaded = 'N';
            //	ds_call_id 进入后无法自动刷新，设置定时刷新
            var check_isload = setInterval(function() {
                if (isLoaded == 'N') {
                    if (isLoaded == 'N') {
                        var ds_call_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_collection_call_hd');
                        if (ds_call_id) {
                            jQuery("#${/parameter/@layout_code}_G_PHONE_COLLECTION_con_collection_call_hd_layout_grid_id_wrap .nav-refresh").click();
                            jQuery("div").unbind(); //移除所有
                            isLoaded = 'Y';
                        }
                    }
                } else {
                    clearInterval(check_isload);
                }
            }, 1000);
            
            //查询时调用(grid,table,gridBox)
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_grid_query'] = function(ds, qpara, bp_seq) {
                var ds_check_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master');
                var ds_call_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_collection_call_hd');
                if (ds_check_id == ds.id && ds_check_id) {
                    $(ds_check_id).setQueryParameter('bp_id', '${/parameter/@bp_id}');
                } else if (ds.id == '${/parameter/@layout_code}_G_CAR_hls_bp_master_ds') {
                    $('${/parameter/@layout_code}_G_CAR_hls_bp_master_ds').setQueryParameter('bp_id', '${/parameter/@bp_id}');
                }
            
                if (ds_call_id == ds.id && ds_call_id) {
                    var col_number = '${/parameter/@col_number}';
                    col_number = col_number.replace('CC', '');
                    $(ds_call_id).setQueryParameter('col_id', col_number);
                }
            };
            
            function open_project_Window(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                var param = {};
                param['company_id'] = record.get('company_id');
                param['project_id'] = record.get('project_id');
                param['function_code'] = 'PRJ502D';
                param['function_usage'] = 'QUERY';
                param['bp_class'] = record.get('bp_class');
                param['maintain_type'] = 'QUERY';
                param['url_title'] = '${l:HLS.PROJECT_MAITAIN}';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_car_modify_special_link', ds_id, '${/parameter/@layout_code}');
            }
            
            function open_contract_win(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                var param = record.data;
                param['function_code'] = 'CON301';
                param['maintain_type'] = 'UPDATE';
                param['url_title'] = '${l:CON301.CONTRACT_DETAIL}';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_con_contract_modify_link', ds_id, '${/parameter/@layout_code}');
            }
            
            function open_bp_code_query_win(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                var param = record.data;
                param['function_code'] = 'CON501H';
                param['function_usage'] = 'QUERY';
                param['url_title'] = '${l:HLS212.BP_MASTER_QUERY}';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_hls_bp_master_query_link', ds_id);
            }
            
            function open_collection_win(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                var param = record.data;
            
                if (record.get('wfl_status') == 'NEW' || record.get('wfl_status') == 'REJECT') {
                    param['function_usage'] = 'MODIFY';
                } else {
                    param['function_usage'] = 'QUERY';
                }
                param['bp_id'] = record.get('bp_id');
                param['function_code'] = 'CON6322_M';
                // param['function_usage'] = 'MODIFY';
                param['function_type'] = 'VISIT';
                param['url_title'] = '催收详情';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_con632_callback_link_id', ds_id);
            }
            
            function open_col_detail_win(ds_id, record_id) {
                var record = $(ds_id).findById(record_id);
                var param = record.data;
                param['function_code'] = 'CON632_PDQ';
                param['function_usage'] = 'QUERY';
                param['maintain_type'] = 'READONLY';
                param['url_title'] = '催收详情';
                param['winid'] = 'col_detail_id';
                hls_doc_get_layout_code('${/parameter/@layout_code}_con_contract_get_layout_code_link_id', param, '${/parameter/@layout_code}_col_detail_link', ds_id, '${/parameter/@layout_code}');
            }
            
            function view_pdf(attachment_id) {
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/fnd.fnd_atm_attachment/query',
                    para: {
                        attachment_id: attachment_id
                    },
                    success: function(res) {
                        var path = res.result.record.file_path;
                        path = path.substr(path.indexOf('hls_attachment'));
                        var tomcat_source = '${/parameter/@tomcat_source}';
                        var source_path = 'http://' + window.location.host + '/' + tomcat_source + '/' + path;
                        var oWin = window.open(source_path);
                    },
                    scope: this
                });
            }
            
            function upload_file(id, name, query_only) {
                var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
                if (record.get('management_ln_id')) {
                    var url;
                    if (query_only == 'Y' || '${/parameter/@function_usage}' == 'QUERY' || '${/parameter/@maintain_type}' == 'READONLY') {
                        url = $('${/parameter/@layout_code}_cdd_downloadFile_id').getUrl() + '?table_name=CON_COLLECTION_MANAGEMENT&header_id=' + record.get('management_ln_id');
                    } else {
                        url = $('${/parameter/@layout_code}_cdd_uploadFile_id').getUrl() + '?table_name=CON_COLLECTION_MANAGEMENT&header_id=' + record.get('management_ln_id');
                    }
                    var win = new Leaf.Window({
                        url: url,
                        title: '${l:HLS.SUPPORTING_DOCUMENT}',
                        id: 'prj500_cdd_uploadFile_screen_id',
                        width: 850,
                        height: 400
                    });
            
                    win.on('close', function() {
                        record.ds.query();
                    });
                } else {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');
            
                }
            }
            
            //超链接渲染
            window['${/parameter/@layout_code}_dynamic_link_renderer'] = function(value, record, name, config_record) {
                window['${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
                if (name == 'project_number' && value) {
                    return '<a href="javascript:open_project_Window(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
                } else if (name == 'contract_number' && value) {
                    return '<a href="javascript:open_contract_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
                } else if (name == 'bp_code' && value) {
                    return '<a href="javascript:open_bp_code_query_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
                } else if (name == 'col_number' && value) {
                    return '<a href="javascript:open_collection_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
                } else if (name == 'col_detail' && !record.isNew) {
                    return '<a href="javascript:open_col_detail_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + config_record.get('prompt') + '</a>';
                } else if (name == 'attachment') {
                    link_function = 'upload_file';
                    if (record.get('attach_count') == 0 || !record.get('attach_count')) {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</a>';
                    } else {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '(' + record.get('attach_count') + ')' + '</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])) {
                                var file_name = temp[0].toUpperCase();
                                var file_suffix = temp[0].substr(temp[0].lastIndexOf('.') + 1).toUpperCase();
                                if (file_name.indexOf('.PDF') >= 0) {
                                    url = url + '<a href=javascript:view_pdf(\'' + temp[1] + '\')>' + temp[0] + '</a>' + ',';
                                } else if (file_suffix == 'BMP' || file_suffix == 'JPG' || file_suffix == 'JPEG' || file_suffix == 'PNG' || file_suffix == 'GIF') {
                                    url = url + '<a href=' + link + temp[1] + ' ref="img">' + temp[0] + '</a>' + ',';
                                } else {
                                    url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                                }
                            }
                        }
                        return url;
                    }
                }
                return value;
            };
            
            //modify by ck 点击审批按钮 自动保存
            
            function zjwfl5110_beforeapproveCheck(type) {
                if ('${/parameter/@instance_id}') {
                    if ('${/parameter/@maintain_type}' == 'MODIFY' || '${/parameter/@function_usage}' == 'UPDATE') {
                        var root_ds = $('${/parameter/@layout_code}_virtual_ds');
                        if (root_ds.validate()) {
                            window['${/parameter/@layout_code}_SAVE_LAYOUT_DYNAMIC_CLICK']();
                            return true;
                        } else {
                            return false;
                        }
                    } else if ('${/parameter/@maintain_type}' == 'READONLY' || '${/parameter/@function_usage}' == 'QUERY') {
                        return true;
                    } else {
                        return true;
                    }
                } else {
                    return true;
                }
            }
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
