<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: trd
    $Date: 2012-12-6 上午11:18:39
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
      </a:init-procedure>
    <a:view>
        <a:link id="hls_journal_import_create_link_id" url="${/request/@context_path}/modules/hls/HLS536/hls_journal_import_create.lview"/>
        <a:link id="welcome_link_id" url="${/request/@context_path}/welcome.lview"/>
        <a:link id="hls_journal_con_contract_update_link_id" url="${/request/@context_path}/modules/cont/CON500/con_contract_update.lview"/>
        <a:link id="con_contract_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code" modelaction="update"/>
        <a:screen-include screen="modules/hls/hls_common_javascript.lview"/>
        <a:link id="hls_journal_import_link" url="${/request/@context_path}/modules/hls/HLS536/hls_journal_import_upload.lview"/>
        <a:link id="hls_journal_other_import_link" url="${/request/@context_path}/modules/hls/HLS536/hls_journal_other_attach_upload.lview"/>
        <a:link id="hls_journal_submit_link_id" url="${/request/@context_path}/modules/hls/HLS536/batch_hls_journal_tmp.lsc"/>
        <a:link id="hls_journal_reverse_link_id" url="${/request/@context_path}/modules/hls/HLS536/batch_hls_journal_reverse.lsc"/>
        <a:link id="hls_journal_import_delete_link" model="hls.HLS536.hls_journal_import_delete"
                        modelaction="batch_update"/>
        <a:link id="hls_journal_import_reverse_link" model="hls.HLS536.hls_journal_import_head_id"
                                modelaction="batch_update"/>
        <a:link id="journal_attachment_uploadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <a:link id="journal_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="batch_attachment_tmp_id" url="${/request/@context_path}/modules/hls/HLS536/batch_attchment_tmp.lsc"/>
         <script><![CDATA[
         Leaf.onReady(function() {
                     if ('${/session/@user_id}'!=10418) {
                         $('reverse_btn_id').hide();
                         $('reverse_submit_btn_id').hide();
                     }
                 });
        //查询按钮
        function hls536_journal_query() {
            $('hls_journal_header_result_ds').query();
        }

        //重置按钮
        function hls536_journal_reset() {
            $('hls_journal_header_query').reset();
        }
        //退出按钮

        function hls536_journal_exit() {
            location.href = $('welcome_link_id').getUrl();
            parent.shMenu('show');
        }



       function hls536_journal_import(){
          new Leaf.Window({
               id: 'hls_journal_import_handle_winid',
               url: $('hls_journal_import_link').getUrl(),
               title: '凭证模板导入',
               width: 420,
               height: 320
                         });
          }

          //删除凭证
          function hls536_journal_delete(){
           var records = $('hls_journal_header_result_ds').getSelected();
             if(records.length == 0){
                  Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录！');
                  return ;
                   }
            var datas = [];
            for (var i = 0; i < records.length; i++) {
            if ((records[i].get('reversed_flag') == 'N'&&(records[i].get('wfl_status') == 'APPROVING' || records[i].get('wfl_status') == 'APPROVED'))||(records[i].get('reversed_flag') == 'R'&&(records[i].get('reverse_wfl_status') == 'APPROVING' || records[i].get('reverse_wfl_status') == 'APPROVED'))){
                  Leaf.showMessage('提示', '只有新建和拒绝状态的单据才能刪除！');
                      return;
                        }
                var obj = {};
                obj['journal_header_id'] = records[i].get('journal_header_id');
                obj['_status'] = 'delete';
                datas[i] = obj;
            }
             Leaf.request({
                url: $('hls_journal_import_delete_link').getUrl(),
                para: datas,
                success: function () {
                    Leaf.SideBar.show({
                        msg: '操作成功',
                        duration: 2000
                    });
                    $('hls_journal_header_result_ds').query();
                },
                scope: this
                                });
          }

        //  提交审批
        // 可以批量提交，批量提交审批的数据在一个工作流里面，有一个批次表 存 batch_id,这里要把批量提交的数据 update batch_id
        function hls536_journal_send() {
             var ds_id = 'hls_journal_header_result_ds';
            var records = $(ds_id).getSelected();
            if(records.length == 0){
                Leaf.showMessage('${l:PROMPT}', '未选择！');
                return ;
            }

            //审批中、审批通过的单据不可提交审批
            for (let i = 0; i < records.length; i++) {
                var record = records[i];
                if (record.get('wfl_status') == 'APPROVING' || record.get('wfl_status') == 'APPROVED') {
                    Leaf.showMessage('提示', '审批中、审批通过的凭证不可提交审批!');
                    return;
                }
                if (!Ext.isEmpty(record.get('error_message'))){
                   Leaf.showMessage('提示', '凭证导入存在报错信息，不允许提交审批!');
                   return;
                }
            }

            var journal_header_imp_list = [];

            var journal_type_list = [];
            //同一个模板的凭证才能批量提交
            for (let i = 0; i < records.length; i++) {
                var record = records[i];
                //凭证模板类型
                var journal_type = record.get('journal_type');
                record.set('_status','update')
                journal_header_imp_list.push(record.data);
                journal_type_list.push(journal_type);
            }
            //普通工资和 BPO工资 共用一个 工资工作流，所以可以一起提交审批
            var equal_flag = journal_type_list.every(val => val === journal_type_list[0]);
            var is_salaries = journal_type_list.every(val => val === 'GENERAL_SALARIES' || val === 'BPO_SALARIES');
            var check_flag = false;
            if ((!equal_flag && is_salaries) || equal_flag){
                check_flag = true;
            }
            if (!check_flag){
                Leaf.showMessage('${l:PROMPT}', '同一个模板的凭证才能批量提交！');
                return ;
            }
            var param = {};
            param['details'] = journal_header_imp_list;
            //判断要走哪个工作流
            if (!equal_flag && is_salaries){
                param['wfl_type'] = 'SALARIES';
            }
            if (equal_flag){
                var journal_type = records[0].get('journal_type');
                param['wfl_type'] = journal_type;
            }
            //当属于工资工作模板工作流的单条或多条凭证被提交时,wfl_type也要为 SALARIES
            if(is_salaries){
                param['wfl_type'] = 'SALARIES';
            }
            if ($(ds_id).validate()) {
                if(param['wfl_type']!='FINANCIAL_TEMPLATE' && param['wfl_type']!='FUND_POOL'){
                Leaf.showConfirm('${l:HLS.PROMPT}', '确认已无其它附件需要添加？', function () {
                    Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
                    Leaf.request({
                        url: $('hls_journal_submit_link_id').getUrl(),
                        para: param,
                        success: function (res) {
                            Leaf.Masker.unmask(Ext.getBody());
                            Leaf.showMessage('提示', '提交审批成功!');
                            $('hls_journal_header_result_ds').query();
                        },
                        failure: function () {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        error: function () {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        scope: this
                    });
                },function(){},300,100);
             }else{
             Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认提交审批？', function () {
             Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
             Leaf.request({
                 url: $('hls_journal_submit_link_id').getUrl(),
                 para: param,
                 success: function (res) {
                     Leaf.Masker.unmask(Ext.getBody());
                     Leaf.showMessage('提示', '提交审批成功!');
                     $('hls_journal_header_result_ds').query();
                 },
                 failure: function () {
                     Leaf.Masker.unmask(Ext.getBody());
                 },
                 error: function () {
                     Leaf.Masker.unmask(Ext.getBody());
                 },
                 scope: this
             });
         },function(){},300,100);
             }
           }
        }

       //生成反冲数据，生成后数据可编辑
       function hls536_journal_reverse(){
        var records = $('hls_journal_header_result_ds').getSelected();
        if(records.length == 0){
             Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录！');
             return ;
        }
         for (var i = 0; i < records.length; i++) {
         if (records[i].get('wfl_status') != 'APPROVED') {
          Leaf.showMessage('提示', '凭证没有审批通过不能进行反冲,请检查！');
          return;
            }
         if (records[i].get('reversed_flag') == 'R') {
          Leaf.showMessage('提示', '存在已经反冲的凭证,请检查！');
           return;
                     }
                  }

        var datas = [];
        for (var i = 0; i < records.length; i++) {
         var obj = {};
         obj['journal_header_id'] = records[i].get('journal_header_id');
         obj['_status'] = 'execute';
         datas[i] = obj;
         }
         Leaf.request({
         url: $('hls_journal_import_reverse_link').getUrl(),
         para: datas,
         success: function () {
             Leaf.SideBar.show({
                 msg: '操作成功',
                 duration: 2000
             });
             $('hls_journal_header_result_ds').query();
         },
         scope: this
                         });

    }
        //反冲提交流程
        function hls536_journal_reverse_submit(){
         var records = $('hls_journal_header_result_ds').getSelected();
         if(records.length == 0){
              Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录！');
              return ;
               }
         //凭证传输状态不为Y的不能进行反冲
         for (var i = 0; i < records.length; i++) {
          if (records[i].get('wfl_status') != 'APPROVED') {
              Leaf.showMessage('提示', '凭证没有审批通过不能提交反冲审批,请检查！');
               return;
                     }
          if (records[i].get('reversed_flag') != 'R') {
            Leaf.showMessage('提示', '非反冲数据不能提交反冲审批,请检查！');
             return;
                               }
          if (records[i].get('reversed_flag') == 'R' && (records[i].get('reverse_wfl_status') == 'APPROVING'||records[i].get('reverse_wfl_status') == 'APPROVED')) {
              Leaf.showMessage('提示', '反冲审批中或审批通过的单据不能提交审批,请检查！');
               return;
                 }
              }

            var journal_header_imp_list = [];

          for (var i = 0; i < records.length; i++) {
              var record = records[i];
              record.set('_status','update')
              journal_header_imp_list.push(record.data);
          }
         var param = {};
         param['details'] = journal_header_imp_list;
         Leaf.showConfirm('${l:HLS.PROMPT}', '确认已无其它附件需要添加？', function () {
              Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
               Leaf.request({
                  url: $('hls_journal_reverse_link_id').getUrl(),
                  para: param,
                  success: function (res) {
                      Leaf.Masker.unmask(Ext.getBody());
                      Leaf.showMessage('提示', '提交审批成功!');
                       $('hls_journal_header_result_ds').query();
                  },
                  failure: function () {
                      Leaf.Masker.unmask(Ext.getBody());
                  },
                  error: function () {
                      Leaf.Masker.unmask(Ext.getBody());
                  },
                  scope: this
              });
          }, null);


        }
          
        function hls536_link_dynamic(reocrd_id, journal_header_id) {
           //增加逻辑：审批中、审批通过的只读；新建、审批拒绝的可编辑
           var record = $('hls_journal_header_result_ds').findById(reocrd_id);
           if((record.get('reversed_flag')=='N' && record.get('wfl_status') !='APPROVING' && record.get('wfl_status') != 'APPROVED') ||(record.get('reversed_flag')=='R' && record.get('reverse_wfl_status') != 'APPROVING' && record.get('reverse_wfl_status') != 'APPROVED')){
            var maintain_type = 'UPDATE';
            var param = record.data;
            param['currency'] = param['currency_code'];
            param['function_code'] = 'HLS536';
            param['document_id'] = journal_header_id;
            param['document_category'] = 'JE';
            param['function_usage'] = 'MODIFY';
            param['maintain_type'] = maintain_type;
            param['url_title'] = '${l:HLS530.JOURNAL_LINE_MODIFY}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'hls_journal_import_create_link_id');
           }else{
           var maintain_type = 'READONLY';
            var param = record.data;
            param['currency'] = param['currency_code'];
            param['function_code'] = 'HLS536';
            param['document_id'] = journal_header_id;
            param['document_category'] = 'JE';
            param['function_usage'] = 'QUERY';
            param['query_only'] = 'Y';
            param['maintain_type'] = maintain_type;
            param['url_title'] = '${l:HLS530.JOURNAL_LINE_MODIFY}';
            hls_doc_get_layout_code('con_contract_get_layout_code_link_id', param, 'hls_journal_import_create_link_id');

            }
        }

        function hls536_journal_num_render(value, record, name) {
             return '<a href="javascript:hls536_link_dynamic(' + record.id + ',' + record.get('journal_header_id') + ');">' + value + '</a>';
        }

        function receipt_attachment_upload(val,rec,name){
            return '<a href=javascript:open_upload_window('+rec.get('journal_header_id')+')>凭证模板附件</a>';
                	}

        function open_upload_window(record_id){
            var url = $('journal_attachment_uploadFile_id').getUrl() + '?table_name=HLS_JOURNAL_HEADER_IMP&header_id=' + record_id;
            var win = new Leaf.Window({
                url: url,
                title: '${l:HLS.SUPPORTING_DOCUMENT}',
                id: 'attachment_uploadFile_id',
                width: 850,
                height: 400
            });
            win.on('close', function() {
                $('hls_journal_header_result_ds').query();
            });
        }

        function attachment_upload(val,rec,name){
        if(rec.get('wfl_status')=='NEW'||rec.get('wfl_status')=='REJECT'||rec.get('reverse_wfl_status')=='NEW'||rec.get('reverse_wfl_status')=='REJECT'){
        return '<a href=javascript:open_other_attachment_upload('+rec.get('journal_header_id')+')>其它附件</a>';
        }else{
        return '<a href=javascript:open_other_attachment_download('+rec.get('journal_header_id')+')>其它附件</a>';
        }
                }

     //其它附件上传
      function open_other_attachment_upload(record_id){
         var url = $('journal_uploadFile_id').getUrl() + '?table_name=HLS_JOURNAL_DETAIL_IMP&header_id=' + record_id;
           var win = new Leaf.Window({
            url: url,
            title: '${l:HLS.SUPPORTING_DOCUMENT}',
            id: 'other_attachment_uploadFile_id',
            width: 850,
            height: 400
        });
        win.on('close', function() {
            $('hls_journal_header_result_ds').query();
        });
    }

    //其它附件下载
    function open_other_attachment_download(record_id){
             var url = $('journal_attachment_uploadFile_id').getUrl() + '?table_name=HLS_JOURNAL_DETAIL_IMP&header_id=' + record_id;
               var win = new Leaf.Window({
                url: url,
                title: '${l:HLS.SUPPORTING_DOCUMENT}',
                id: 'other_attachment_uploadFile_id',
                width: 850,
                height: 400
            });
            win.on('close', function() {
                $('hls_journal_header_result_ds').query();
            });
        }

    function selected_attachment_upload(){
     var records = $('hls_journal_header_result_ds').getSelected();
    if(records.length == 0){
      Leaf.showMessage('${l:PROMPT}', '请至少选择一条记录进行附件上传！');
      return ;
      }
      for (var i = 0; i < records.length; i++) {
      if ((records[i].get('reversed_flag') == 'R' && (records[i].get('reverse_wfl_status') == 'APPROVING'||records[i].get('reverse_wfl_status') == 'APPROVED'))||
         (records[i].get('reversed_flag') == 'N' && (records[i].get('wfl_status') == 'APPROVING'||records[i].get('wfl_status') == 'APPROVED'))) {
        Leaf.showMessage('提示', '凭证状态为审批中或者审批通过，不能进行批量附件上传！');
         return;
               }
      }
      var param = {};
      var saveData = [];
      for (var i = 0; i < records.length; i++) {
       var record = records[i];
       saveData.push({
       'journal_header_id': record.get('journal_header_id'),
       '_status': 'insert'
       });
      }
    param['details'] = saveData;
    Leaf.request({
      url: $('batch_attachment_tmp_id').getUrl(),
      para: param,
      success: function () {
      new Leaf.Window({
      id: 'hls_journal_import_handle_winid',
      url: $('hls_journal_other_import_link').getUrl(),
      title: '附件导入',
      width: 420,
      height: 320
                });
      },
      scope: this
                      });

     }


         ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
        <a:dataSets>
            <a:dataSet id="hls536_wfl_status_ds" lookupCode="HLS536_WFL_STATUS"/>
            <a:dataSet id="hls536_reverse_wfl_status_ds" lookupCode="HLS536_WFL_STATUS_REVERSE"/>
               <a:dataSet id="hls_journal_header_query">
                <a:fields>
                    <a:field name="journal_num"/>
                    <a:field name="batch_number"/>
                    <a:field name="reverse_batch_number"/>
                    <a:field name="journal_date_from"/>
                    <a:field name="journal_date_to"/>
                    <a:field name="total_amount_dr_from"/>
                    <a:field name="total_amount_dr_to"/>
                    <a:field name="description"/>
                    <a:field name="wfl_status_n" displayField="code_value_name" defaultValue="新建" options="hls536_wfl_status_ds" returnField="wfl_status" valueField="code_value"/>
                    <a:field name="wfl_status" defaultValue="NEW"/>
                    <a:field name="reverse_wfl_status_n" displayField="code_value_name" options="hls536_reverse_wfl_status_ds" returnField="reverse_wfl_status" valueField="code_value"/>
                    <a:field name="reverse_wfl_status" />
                </a:fields>
             </a:dataSet>
            <a:dataSet id="hls_journal_header_result_ds" autoPageSize="true" autoQuery="true" model="hls.HLS536.hls_journal_import_header" queryDataSet="hls_journal_header_query"  selectable="true">
              </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:toolbarButton click="hls536_journal_exit" text="HLS.EXIT"/>
                <a:toolbarButton click="hls536_journal_reset" text="HLS.RESET"/>
                <a:toolbarButton click="hls536_journal_query" text="HLS.QUERY"/>
                <a:toolbarButton click="hls536_journal_import" text="导入凭证模板"/>
                <a:toolbarButton click="hls536_journal_delete" text="删除凭证"/>
                <a:toolbarButton click="hls536_journal_send" text="提交审批"/>
                <a:toolbarButton id="reverse_btn_id" click="hls536_journal_reverse" text="生成反冲"/>
                <a:toolbarButton id="reverse_submit_btn_id" click="hls536_journal_reverse_submit" text="提交反冲"/>
                <a:toolbarButton id="hls536_upload_id" click="selected_attachment_upload" text="批量附件上传"/>
               </a:screenTopToolbar>
            <a:form column="5" labelSeparator=" " labelWidth="100" marginWidth="40" title="HAP_QUERY_TITLE">
                <a:textField name="journal_num" bindTarget="hls_journal_header_query" prompt="凭证编号"/>
                <a:datePicker name="journal_date_from" bindTarget="hls_journal_header_query" prompt="凭证日期从"/>
                <a:datePicker name="journal_date_to" bindTarget="hls_journal_header_query" prompt="凭证日期到"/>
                <a:numberField name="total_amount_dr_from" allowFormat="true" bindTarget="hls_journal_header_query" prompt="金额从"/>
                <a:numberField name="total_amount_dr_to" allowFormat="true" bindTarget="hls_journal_header_query" prompt="金额到"/>
                <a:textField name="description" bindTarget="hls_journal_header_query" prompt="凭证摘要"/>
                <a:comboBox name="wfl_status_n" bindTarget="hls_journal_header_query" prompt="凭证导入状态"/>
                <a:comboBox name="reverse_wfl_status_n" bindTarget="hls_journal_header_query" prompt="凭证反冲状态"/>
                <a:textField name="batch_number" bindTarget="hls_journal_header_query" prompt="凭证导入批次号"/>
                <a:textField name="reverse_batch_number" bindTarget="hls_journal_header_query" prompt="反冲凭证批次号"/>
                  </a:form>
            <a:grid id="hls_journal_headers_grid" bindTarget="hls_journal_header_result_ds" marginHeight="250" marginWidth="40" navBar="true">
                <a:columns>
                    <a:column name="journal_num" lock="true" prompt="凭证编号" renderer="hls536_journal_num_render" width="120"/>
                    <a:column name="batch_number"  prompt="凭证导入批次号" width="120"/>
                    <a:column name="reverse_batch_number"  prompt="凭证反冲批次号" width="120"/>
                    <a:column name="js_company_short_name" prompt="记账公司" width="90"/>
                    <a:column name="period_name" align="center" prompt="期间" width="80"/>
                    <a:column name="journal_date" align="center" prompt="凭证日期" renderer="Leaf.formatDate" width="90"/>
                    <a:column name="currency_desc" align="center" prompt="币种" width="80"/>
                    <a:column name="total_amount_fuc_dr" align="right" prompt="金额" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="description" prompt="凭证摘要" width="220"/>
                    <a:column name="je_detail_count" prompt="凭证行数量" width="50"/>
                    <a:column name="wfl_status_n" prompt="凭证导入状态" width="100"/>
                    <a:column name="reverse_wfl_status_n" prompt="凭证反冲状态" width="100"/>
                    <a:column name="error_message" prompt="错误信息"  width="150"/>
                    <a:column name="attachment_file"  prompt="凭证模板附件" width="120" renderer="receipt_attachment_upload" align="center"/>
                    <a:column name="other_attachment_file"  prompt="其它附件" width="120" renderer="attachment_upload" align="center"/>
                   </a:columns>
                </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
