<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true">
    <a:init-procedure>

    </a:init-procedure>
    <a:view>
        <a:link id="bmLink_acr_invoice_return" model="acr.ACR605.acr_invoice_claim_return" modelaction="update"/>
        <a:link id="bmLink_acr_invoice_claim_confirm" model="acr.ACR605.acr_invoice_claim_confirm" modelaction="update"/>
        <a:link id="acr_invoice_refuse_link_id" model="acr.ACR605.acr_invoice_claim_return" modelaction="batch_update"/>
        <a:link id="con_contract_modify_link" url="${/request/@context_path}/modules/cont/CON505/con_contract_modify.lview"/>
        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:link id="acr517_invoice_send_to_email_link" url="${/request/@context_path}/modules/acr/ACR516/import_invoice_to_email.lsc"/>

        <script type="text/javascript"><![CDATA[

        function document_number_render(value,record,name)
        {
            if (name == 'document_number' && value) {
                return '<a href="javascript:open_document_win(\'' + record.ds.id + '\',\'' + record.id + '\')">' + value + '</a>';
            }
            return value;
        }
        function open_document_win(ds_id, record_id) {
            var record = $(ds_id).findById(record_id);
            var param = record.data;
            param['function_code'] = 'ACR513';
            param['function_usage'] = 'QUERY';
            param['maintain_type'] = 'UPDATE';
            param['url_title'] = '${l:ACR.INVOICE_DETAIL}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'con_contract_modify_link', ds_id);
        }

        function acr517_invoice_query() {
            $('acr517_invoice_result_ds').query();
        }

        function acr517_invoice_reset() {
            $('acr517_invoice_query_ds').reset();
        }

        function acr517_invoice_confirm() {
          //  var records = $('acr517_invoice_result_ds').getSelected();

            // $('acr517_invoice_result_ds').setSubmitUrl('${/request/@context_path}/autocrud/acr.ACR605.acr_invoice_claim_confirm_new/batch_update');
            var records=$('acr517_invoice_result_ds').getSelected();
            debugger;
            var datas = [];
            var r_map = new Map();
            for (var i = 0;i < records.length;i++) {
                datas.push({
                    'invoice_hd_id':records[i].get('invoice_hd_id'),
                    '_status': 'update'
                })
                if(records[i].get('division') == '70'){
                    if(!r_map.has(records[i].get('bp_id_agent_level1'))){
                        var arr = new Array();
                        arr.add(records[i].get('invoice_hd_id'));
                        r_map.set(records[i].get('bp_id_agent_level1'),arr);
                    }else{
                        r_map.get(records[i].get('bp_id_agent_level1')).add(records[i].get('invoice_hd_id'));
                    }
                } else if(records[i].get('invoice_type') == 'ECT'){
                    if(!r_map.has(records[i].get('invoice_bp_id'))){
                        var arr = new Array();
                        arr.add(records[i].get('invoice_hd_id'));
                        r_map.set(records[i].get('invoice_bp_id'),arr);
                    }else{
                        r_map.get(records[i].get('invoice_bp_id')).add(records[i].get('invoice_hd_id'));
                    }
                }
            }
            var result_map = MapTOJson(r_map);
            $L.showConfirm("提示",'确认执行该操作?',function(){
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/acr.ACR605.acr_invoice_claim_confirm_new/batch_update',
                    para: datas,
                    success: function () {
                        Leaf.request({
                            url: $('acr517_invoice_send_to_email_link').getUrl(),
                            para: {
                                result_map : result_map
                            }
                        });
                        Leaf.SideBar.show({
                            msg: '操作成功',
                            duration: 2000
                        });
                        $('acr517_invoice_result_ds').query();
                    },
                    failure: function () {
                        Leaf.showWarningMessage('', '${l:PRJ509.DATA_NOT_BACK}', null, 200, 100);
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    error: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    scope: this
                });
            })

            // alert(record.get('invoice_hd_id'));
            // debugger;
           // var invoice_hd_id = record.get('invoice_hd_id');
            // Leaf.request({
            //     url: $("bmLink_acr_invoice_claim_confirm").getUrl(),
            //     para: {
            //         invoice_hd_id:invoice_hd_id
            //     },
            //     success:function(){
            //         $('acr517_invoice_result_ds').query();
            //         Leaf.SideBar.show({
            //             msg: '操作成功',
            //             duration: 2000
            //         });
            //     },
            //     sync:true,
            //     scope: this
            // });
        }

        function acr517_invoice_return() {
            var records=$('acr517_invoice_result_ds').getSelected();
            var datas = [];
            for (var i = 0;i < records.length;i++) {
                datas.push({
                    'invoice_hd_id':records[i].get('invoice_hd_id'),
                    '_status': 'update'
                })

            }
            $L.showConfirm("提示",'确认执行该操作?',function(){
                Leaf.Masker.mask(Ext.getBody(),'正在操作...');
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/acr.ACR605.acr_invoice_claim_return/batch_update',
                    para: datas,
                    success: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                        Leaf.SideBar.show({
                            msg: '操作成功',
                            duration: 2000
                        });
                        $('acr517_invoice_result_ds').query();
                    },
                    failure: function () {
                        Leaf.showWarningMessage('', '${l:PRJ509.DATA_NOT_BACK}', null, 200, 100);
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    error: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    scope: this
                });
            })

        }
      function submit_success_btn(){
         $('acr517_invoice_result_ds').query();
      }
        function MapTOJson(m) {
            var str = '{';
            var i = 1;
            m.forEach(function (item, key, mapObj) {
                if (mapObj.size == i) {
                    str += '"' + key + '":"' + item + '"';
                } else {
                    str += '"' + key + '":"' + item + '",';
                }
                i++;
            });
            str += '}';
            //console.log(str);
            return str;
        }
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
        <a:dataSets>
<!--            <a:dataSet id="acr517_invoice_kind_ds" lookupCode="ACR510_INVOICE_KIND"/>-->
            <a:dataSet id="acr517_invoice_kind_ds" lookupCode="ACR512_INVOICE_KIND_TYPE"/>
            <a:dataSet id="acr517_invoice_statu_ds" lookupCode="ACR510_AR_INVOICE_STATUS"/>
            <a:dataSet id="acr517_claim_status_ds" lookupCode="CLAIM_STATUS"/>
            <a:dataSet id="cf_item_options">
                <a:datas dataSource="/model/hls_cashflow_item_list"/>
            </a:dataSet>
<!--            <a:dataSet id="claim_status_ds" loadData="true" lookupCode="CLAIM_STATUS"/>-->
            <a:dataSet id="csh_transaction_flag_ds">
                <a:datas>
                    <a:record code_value="NOT" code_value_name="未收款"/>
                    <a:record code_value="FULL" code_value_name="完全收款"/>
                    <a:record code_value="PARTIAL" code_value_name="部分收款"/>
                </a:datas>
            </a:dataSet>

            <a:dataSet id="acr517_invoice_query_ds" autoCreate="true">
                <a:fields>
                    <a:field name="document_number"/>
                    <a:field name="invoice_apply_num"/>
                    <a:field name="bp_id_agent_level1"/>
<!--                    <a:field name="invoice_kind_desc" displayField="code_value_name" options="acr517_invoice_kind_ds"
                             returnField="invoice_kind" valueField="code_value"/>-->
                    <a:field name="invoice_kind_type"/>
                    <a:field name="invoice_kind_type_n" displayField="code_value_name" options="acr517_invoice_kind_ds"
                             returnField="invoice_kind_type" valueField="code_value"/>
                    <a:field name="invoice_status_desc" displayField="code_value_name" options="acr517_invoice_statu_ds" returnField="invoice_status" valueField="code_value"/>
                    <a:field name="total_amount"/>
                    <a:field name="csh_transaction_flag"/>
                    <a:field name="csh_transaction_flag_desc" options="csh_transaction_flag_ds"
                             displayField="code_value_name" returnField="csh_transaction_flag" valueField="code_value"/>
                    <a:field name="cf_item"/>
                    <a:field name="cf_item_n" options="cf_item_options" displayField="description" returnField="cf_item" valueField="cf_item"/>
<!--                    <a:field name="claim_status" options="claim_status_ds" displayField="code_value_name" valueField="code_value" returnField="claim_status"/>-->
                    <a:field name="cf_item_desc" autoComplete="true" lovGridHeight="350" lovHeight="500" lovLabelWidth="100" lovService="csh.CSH518.hls_cashflow_item_v_lov?cf_direction=INFLOW" lovWidth="520" title="现金流项目">
                        <a:mapping>
                            <a:map from="cf_item" to="cf_item"/>
                            <a:map from="cf_item_desc" to="cf_item_desc"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="claim_status_n" displayField="code_value_name" options="acr517_claim_status_ds"
                             returnField="claim_status" valueField="code_value"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="acr517_invoice_result_ds" autoQuery="true" fetchAll="false" autoPageSize="true"
                       model="acr.ACR605.acr_invoice_claim_confirm_new" queryDataSet="acr517_invoice_query_ds" selectable="true">
              <a:events>
                  <a:event name="submitsuccess" handler="submit_success_btn"/>
              </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:toolbarButton click="acr517_invoice_query" text="HLS.QUERY"/>
                <a:toolbarButton click="acr517_invoice_reset" text="HLS.RESET"/>
                <a:toolbarButton click="acr517_invoice_confirm" text="确认"/>
                <a:toolbarButton click="acr517_invoice_return" text="退回"/>
            </a:screenTopToolbar>
            <a:form marginWidth="30" padding="0" title="查询条件" column="4">
                <a:textField name="document_number" bindTarget="acr517_invoice_query_ds" prompt="单据编号" width="120"/>
                <a:textField name="contract_number" bindTarget="acr517_invoice_query_ds" prompt="合同编号" width="120"/>
                <a:textField name="contract_name" bindTarget="acr517_invoice_query_ds" prompt="合同名称" width="120"/>
<!--                <a:lov name="cf_item_desc" bindTarget="acr517_invoice_query_ds" prompt="现金流项目" width="135"/>-->
                <a:numberField name="total_amount_f" allowFormat="true" bindTarget="acr517_invoice_query_ds" prompt="开票金额含税额从" width="120"/>
                <a:numberField name="total_amount_t" allowFormat="true" bindTarget="acr517_invoice_query_ds" prompt="开票金额含税额到" width="120"/>
                <a:textField name="invoice_title" bindTarget="acr517_invoice_query_ds" prompt="发票抬头" width="120"/>
<!--                <a:comboBox name="invoice_kind_desc" bindTarget="acr517_invoice_query_ds" prompt="ACR.INVOICE_KIND" width="120"/>-->
                <a:comboBox name="invoice_kind_type_n" bindTarget="acr517_invoice_query_ds" prompt="发票种类" width="120"/>
<!--                <a:comboBox name="invoice_status_desc" bindTarget="acr517_invoice_query_ds" prompt="发票状态" width="135"/>-->
                <a:textField name="vat_invoice_code" bindTarget="acr517_invoice_query_ds" prompt="发票代码" width="120"/>
                <a:textField name="invoice_number" bindTarget="acr517_invoice_query_ds" prompt="发票号码" width="120"/>
                <a:comboBox name="claim_status_n" bindTarget="acr517_invoice_query_ds" prompt="认领状态" width="120"/>
                <a:comboBox name="csh_transaction_flag_desc" bindTarget="acr517_invoice_query_ds" prompt="收款标记"
                            width="120"/>
                <a:textField name="invoice_apply_num" bindTarget="acr517_invoice_query_ds" prompt="申请编号" width="120"/>
                <a:datePicker name="invoice_date_from" bindTarget="acr517_invoice_query_ds" prompt="发票日期从" width="120"/>
                <a:datePicker name="invoice_date_to" bindTarget="acr517_invoice_query_ds" prompt="发票日期到" width="120"/>
            </a:form>
            <a:grid id="acr516_invoice_grid_id" bindTarget="acr517_invoice_result_ds" marginHeight="225" marginWidth="30" navBar="true">
                <a:columns>
                    <a:column name="document_number" lock="true" prompt="HLS.DOCUMENT_NUMBER" width="120" renderer="document_number_render"/>
                    <a:column name="contract_number" autoAdjust="false" prompt="HLS.CONTRACT_NUMBER" showTitle="true" width="150"/>
                    <a:column name="contract_name" width="150" prompt="合同名称"/>
                    <a:column name="bp_agent_name" prompt="代理商" width="100"/>
<!--                    <a:column name="invoice_kind_desc" prompt="发票种类" width="100"/>-->
                    <a:column name="invoice_kind_type_n" prompt="发票种类" width="200"/>
                    <a:column name="invoice_number" width="120" prompt="发票号码"/>
                    <a:column name="vat_invoice_code" width="120" prompt="发票代码"/>
                    <a:column name="invoice_date" renderer="Leaf.formatDate" prompt="发票日期" width="100"/>
                    <a:column name="invoice_bp_code" prompt="开票客户编号" width="100"/>
<!--                    <a:column name="invoice_bp_name" autoAdjust="false" showTitle="true" prompt="开票客户名称" width="150"/>-->
                    <a:column name="invoice_title" autoAdjust="false" showTitle="true" prompt="发票抬头" width="150"/>
                    <a:column name="total_amount" align="right" renderer="Leaf.formatMoney"  prompt="发票金额" width="100"/>
                    <a:column name="tax_amount" align="right" renderer="Leaf.formatMoney" prompt="税额" width="100"/>
                    <a:column name="net_amount" align="right" renderer="Leaf.formatMoney" prompt="不含税金额" width="100"/>
<!--                    <a:column name="currency" prompt="币种" width="80"/>-->
                    <a:column name="created_by_name" autoAdjust="false" showTitle="true" prompt="创建人" width="150"/>
<!--                    <a:column name="invoice_status_desc" prompt="发票状态" width="100"/>-->
<!--                    <a:column name="vat_interface_status_desc"  prompt="金税状态" width="100"/>-->
                    <a:column name="accounting_date" renderer="Leaf.formatDate" prompt="期间序号" width="100"/>
<!--                    <a:column name="create_je_flag" prompt="凭证标志" width="60"/>-->
                    <a:column name="bp_tax_registry_num" prompt="纳税人识别号"/>
<!--                    <a:column name="ele_invoice_status_desc" prompt="电子发票状态"/>-->
<!--                    <a:column name="swno" prompt="电子发票流水号" width="100"/>-->
                    <a:column name="claim_status_n" prompt="认领状态" width="100"/>
                    <a:column name="invoice_apply_num" prompt="申请编号" width="120"/>
                    <a:column name="csh_transaction_flag_desc" prompt="收款标记" width="80"/>
                </a:columns>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>