<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhuxianfei
    $Date: 2018年7月25日 下午3:35:07  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script" customizationEnabled="true" dynamiccreateenabled="true"
          trace="true">
    <a:init-procedure>
        <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="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <a:link id="${/parameter/@layout_code}${/parameter/@pre_layout}_invoice_cdd_uploadFile_id"
                url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="acp_invoice_update_link" model="acp.ACP517.acp_invoice_confirm"
                modelaction="batch_update"/>
        <a:link id="acp_invoice_check_link" model="acp.ACP517.acp_invoice_confirm"
                modelaction="execute"/>
        <a:link id="acp_invoice_check" url="${/request/@context_path}/modules/acp/ACP517/acp_invoice_check.lsc"/>
        <a:link id="acp_invoice_verification" model="acp.ACP517.acp_invoice_verification"
                modelaction="execute"/>
        <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 type="text/javascript"><![CDATA[
        Leaf.onReady(function(){
            var acp_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'acp_invoice_ln');
           if('${/parameter/@contract_id}'){
               $(acp_ds_id).setQueryParameter('contract_id','${/parameter/@contract_id}');
               $(acp_ds_id).query();
           }

        })

        Ext.ux.Lightbox.register('a[ref=img]', true);

        //超链接渲染
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record, bp_seq) {

            var link_function = '';
            var invoice_line_id = record.get('invoice_line_id');
            window['${/parameter/@bp_seq}${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
            if (name == 'attachment') {
                return '<a href="javascript:attachment_window_open(\'' + invoice_line_id + '\',\'' + record.id + '\',\'' + record.ds.id + '\');">' + '上传' + '</a>';
            } else if (name == 'attachment_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_suffix = temp[0].substr(temp[0].lastIndexOf('.') + 1).toUpperCase();
                            if (file_suffix == 'BMP' || file_suffix == 'JPG' || file_suffix == 'JPEG' || file_suffix == 'PNG' || file_suffix == 'GIF') {
                                url = url + '<a ref="img" href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            } else {
                                url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            }
                        }
                    }
                    return url;
                }
            }
        };

        function attachment_window_open(invoice_line_id, record_id, ds_id) {
            var record = $(ds_id).findById(record_id);
            if (record.isNew) {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存再进行附件上传!');
            }
            else {
                url = $('${/parameter/@layout_code}${/parameter/@pre_layout}_invoice_cdd_uploadFile_id').getUrl() + '?table_name=ACP_INVOICE_LN&header_id=' + invoice_line_id;
                var win = new Leaf.Window({
                    id: 'open_attachment_window',
                    url: url,
                    title: '附件上传',
                    height: 350,
                    width: 850
                });

                win.on('close', function () {
                    record.ds.query();
                });
            }
        }

        //确认
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
            var acp_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'acp_invoice_ln');
            var records = $(acp_ds_id).getSelected();
            if (records.length == 0) {
                Leaf.showMessage('${l:PROMPT}', '请至少选择一条数据!');
                return;
            }
            /*if (!records.some(function (value) {
                    return value.get('contract_id') === records[0].get('contract_id');
                })) {
                Leaf.showErrorMessage('${l:PROMPT}', '请选择同一条合同');
            }*/

            var flag = 0;
            for (var i = 0; i < records.length; i++) {
                if (records[i].get('contract_id') != records[0].get('contract_id')) {
                    flag = flag + 1;
                }
            }
            //一次只能对同一个合同进行确认
            if (flag > 0) {
                Leaf.showErrorMessage('${l:PROMPT}', '请选择同一条合同');
                return;
            }
            /*var param = {};
            var saveData = [];
            for (var i = 0; i < records.length; i++) {
                saveData.push({
                    'dcfl_invoice_header' : records[i].get('dcfl_invoice_header'),
                    'dcfl_selleraddresstel' : records[i].get('dcfl_selleraddresstel'),
                    'dcfl_sellerbankaccountnr' : records[i].get('dcfl_sellerbankaccountnr'),
                    'dcfl_tax_registration' : records[i].get('dcfl_tax_registration'),
                    '_status' : 'update'
                });
            }
            param['details'] = saveData;
            Leaf.request({
                url: $('acp_invoice_check').getUrl(),
                para: param,
                success: function ()  {*/
            var param_list = [];
            var param = {};
            var invoice_line_id_s = '';
            for (var i = 0; i < records.length; i++) {
                param = {};
                param['invoice_line_id'] = records[i].get('invoice_line_id');
                param['_status'] = 'update';
                param_list.push(param);
                if (i == 0) {
                    invoice_line_id_s = records[i].get('invoice_line_id');
                } else {
                    invoice_line_id_s = invoice_line_id_s + ',' + records[i].get('invoice_line_id');
                }
            }
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();

            Leaf.request({
                url: $('acp_invoice_check_link').getUrl(),
                para: {

                    invoice_line_id_s: invoice_line_id_s,
                    contract_id: records[0].get('contract_id')
                },
                success: function (res) {


                    if (res.result.check_flag == 1) {
                        Leaf.request({
                            url: $('acp_invoice_update_link').getUrl(),
                            para: param_list,
                            success: function (res) {
                                $(acp_ds_id).query();
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            error: function () {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            failure: function () {
                                window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            },
                            sync: true,
                            scope: this
                        })
                    } else {
                        Leaf.showMessage('${l:HLS.PROMPT}', '价税合计之和与该合同的租赁物价款金额不相等!');

                        window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    }

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


        }


        //更新时调用
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function (ds, record, name, value, old_value, bp_seq) {
            var acr_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'acp_invoice_ln');

            if (acr_ds == ds.id) {
                if (name == 'total_amount') {
                    var net_amount = record.get('net_amount') || 0;
                    var tax_amount = record.get('tax_amount') || 0;
                    var sum_amount = net_amount + tax_amount;
                    if (value != sum_amount) {
                        Leaf.showMessage('提示', '税价合计不等于金额与税额之和,请核对!');
                        return;
                    }
                }
            }
        };


        //保存前调用
        /*window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function (ds, record) {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var check_flag = true;
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'acp_invoice_ln');
            var records = $(ds_id).getAll();
            for(var i=0;i<=records.length;i++){
                var net_amount = records[i]et('net_amount') || 0;
                var tax_amount = records[i].get('tax_amount') || 0;
                var sum_amount = net_amount + tax_amount;
                var total_amount = records[i]et('total_amount') || 0
                if (total_amount != sum_amount) {
                    Leaf.showMessage('提示', '税价合计不等于金额与税额之和,请核对!');
                    check_flag = false;
                    break;
                }
            }
            return check_flag;
        };*/

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