<?xml version="1.0" encoding="UTF-8"?>
<!--
  create by nyl  2014-12-3 
  for contract context create
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure>
        <a:model-query model="cus.CUS100.cus_default_values" rootPath="cus_default_value"/>
    </a:init-procedure>
    <a:view>
        <a:link id="con643_submit_link" model="cont.CON640.submit_rpt" modelaction="execute"/>
        <a:link id="con643_get_control_id_link" model="db.rpt_report_control_pkg.checkin_rpt" modelaction="execute"/>
        <a:link id="con643_refresh_link" model="db.rpt_report_control_pkg.refresh_rpt" modelaction="execute"/>
        <!-- <a:link id="con640_ccr_detail_link" url="${/request/@context_path}/modules/cont/CON640/con640_ccr_detail.lview"/> -->
        <script type="text/javascript"><![CDATA[
        	function eg_rpt100_status_find(control_id){
                Leaf.request({
                    url: $('con643_refresh_link').getUrl(),
                    para: {'control_id':control_id},
                    success: function(res) {
                    if (res.result.job_stauts == 'FINISH') {
                            var result_ds = $('con640_result_ds');
                            var record = $('con640_head_ds').getCurrentRecord();
                            record.set('status', res.result.job_stauts);
                            result_ds.setQueryParameter('control_id', control_id);
                            result_ds.query();
                        } else {
                            eg_rpt100_head_query();
                        }
                    },
                    failure: function() {
                        con640_unlock_current_window();
                    },
                    error: function() {
                        con640_unlock_current_window();
                    },
                    scope: this
                });
			}
			
			function con640_lock_current_window() {
                Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
            }
			
			function con640_unlock_current_window() {
                Leaf.Masker.unmask(Ext.getBody());
            }
			
			function con640_load(ds) {
                result_load_flag = 'Y';
                unlock_window_after_load();
            }
        	
        	function con640_query() {
                var record = $('con640_status_ds').getCurrentRecord();
                if (record && record.get('calc_date')) {
                    eg_rpt100_head_query();
                } else {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请输入计算日期！');
                    return;
                }
            }
			
			
			function on_con640_head_ds_load(ds) {
                head_load_flag = 'Y';
                if (from_update_flag == 'N') {
                    var record = $('con640_head_ds').getCurrentRecord();
                    var control_id = null,
                        status = null;
                    if (record) {
                        control_id = record.get('control_id');
                        status = record.get('status');
                    }
            
                    if (status == 'FINISH') {
                        var result_ds = $('con640_result_ds');
                        record.set('status', status);
                        result_ds.setQueryParameter('control_id', control_id);
                        result_ds.query();
                    } else {
                        eg_rpt100_status_find(control_id);
                    }
                } else {
                    from_update_flag = 'N';
                    unlock_window_after_load();
                }
            }
			
			
			function con640_submit(){
			    var record = $('con640_head_ds').getCurrentRecord(),
                    status_record = $('con640_status_ds').getCurrentRecord();
                var control_id = record.get('control_id');
                var calc_date = status_record.get('calc_date');
			    if (status_record && calc_date) {
			    Leaf.showConfirm('${l:PROMPT}', '计算将清除上次运行结果，并且执行时间较长，是否确认提交？', function() {
			        $('con640_result_ds').removeAll();
	                Leaf.request({
	                    url: $('con643_submit_link').getUrl(),
	                    para: {'control_id':control_id,
	                        	'check_company_id' : ${/session/@company_id},
	                          'calc_date':calc_date},
	                    success: function(res) {
	                        eg_rpt100_head_query();
	                    },
	                    failure: function() {
	                        con640_unlock_current_window();
	                    },
	                    error: function() {
	                        con640_unlock_current_window();
	                    },
	                    scope: this
	                });  
			    });
			    
			} else {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请输入计算日期！');
                    return;
                }
            }
			
			function eg_rpt100_head_query(){
                $('con640_head_ds').query();
			}
			function con640_red_fun(value,record,name){
			    return '<font style="color:red;bold:true">'+value+'</font>';
			}
				
			function con640_excel(){
			    $('con640_result_grid_ds')._export('xls','excel_report');
			}
			
			function unlock_window_after_load() {
                if (head_load_flag == 'Y' && result_load_flag == 'Y') {
                    head_load_flag = 'N';
                    result_load_flag = 'N';
                    con640_unlock_current_window();
                }
            }
            
            var head_load_flag = 'N';
            var result_load_flag = 'N';
            var from_update_flag = 'N';
			
			function con640_update(ds,record,name,value,oldvalue){
			    if (value){
			        //
			        var date = Leaf.formatDate(value);
			        date = date.substring(0,7).replace(/-/g,"");
			        con640_lock_current_window();
			        Leaf.request({
	                    url: $('con643_get_control_id_link').getUrl(),
	                    para: {'report_name':'CON640',
	                        	'check_company_id' : 0,
	                           // 'check_date': null,
	                           'check_period':date},
	                    success: function(res) {
	                        from_update_flag = 'Y';
                            var head_ds = $('con640_head_ds');
                            var result_ds = $('con640_result_ds');
                            if (res.result.control_id) {
                                head_ds.setQueryParameter('control_id', res.result.control_id);
                                result_ds.setQueryParameter('control_id', res.result.control_id);
                                head_ds.query();
                                result_ds.query();
                                } else {
                                con640_unlock_current_window();
                            }
	                    },
	                    failure: function() {
	                        con640_unlock_current_window();
	                    },
	                    error: function() {
	                        con640_unlock_current_window();
	                    },
	                    scope: this
	                	});
				    }
			}
			
			// function con506_open_ccr_detail_win(record_id,ds_id){
                // // var record = $('con640_result_ds').findById(record_id);
                // // var url = $('con640_ccr_detail_link').getUrl() + '?contract_id=' + id;
                // var record = $('con640_result_ds').getCurrentRecord();
                // var contract_id = record.get('contract_id');
               // new Leaf.Window({
        	        // id:'con506_ccr_detail_win',
        	        // title:'明细',
        	        // url:$('con640_ccr_detail_link').getUrl(),
        	        // params:{contract_id:contract_id},
        	        // width:980,
        	        // height:480
        	    // });
            // }
			
			// function render_doc_monitorDs_grid(value, record, name) {
			    // // var contract_id = record.get('contract_id');
			    // return '<a href="javascript:con506_open_ccr_detail_win(' + record.get('contract_id') + ');">' + '变更明细' + '</a>';
			// }
			
			function con640_reset() {
                $('con640_query_ds').reset();
            }

        ]]></script>
        <a:dataSets>
            <a:dataSet id="company_ds" autoQuery="true" model="cus.CUS323.fnd_companies"/>
            <a:dataSet id="division_name_ds" loadData="true" model="basic.hls_division_for_lov"/>
            <a:dataSet id="con640_head_ds" model="cus.CUS312.cus_rpt_report_control">
                <a:events>
                    <a:event name="load" handler="on_con640_head_ds_load"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="con640_status_ds" autoCreate="true">
                <a:fields>
                    <a:field name="calc_date"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="con640_update"/>
                </a:events>
            </a:dataSet>
            <a:dataSet id="con640_query_ds">
                <a:fields>
                    <a:field name="contract_number" autoComplete="true" lovGridHeight="300" lovHeight="450" lovService="cus.CUS323.con_cf_aging_buckets_contract_for_lov" lovWidth="500" title="HLS.CONTRACT_NUMBER">
                        <a:mapping>
                            <a:map from="contract_number" to="contract_number"/>
                            <a:map from="contract_name" to="contract_name"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="company_id_n" displayField="company_short_name" options="company_ds" returnField="company_id" valueField="company_id"/>
                    <a:field name="division_n" displayField="description" options="division_name_ds" returnField="division" valueField="division"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="con640_result_ds" autoPageSize="true" autoQuery="true" model="cont.CON640.cus_rpt_con640_o_lv" queryDataSet="con640_query_ds">
                <a:events>
                    <a:event name="load" handler="con640_load"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:toolbarButton click="con640_query" text="查询"/>
                <a:toolbarButton click="con640_submit" text="计算"/>
                <a:toolbarButton click="con640_reset" text="重置"/>
                <a:toolbarButton click="con640_excel" text="导出"/>
            </a:screenTopToolbar>
            <a:form column="4" marginWidth="100" title="数据刷新状态">
                <a:datePicker name="calc_date" bindTarget="con640_status_ds" prompt="计算时间"/>
                <a:label name="last_submit_date" bindTarget="con640_head_ds" prompt="最后刷新时间" renderer="con640_red_fun"/>
                <a:label name="last_submitted_by" bindTarget="con640_head_ds" prompt="最后刷新人" renderer="con640_red_fun"/>
                <a:label name="status" bindTarget="con640_head_ds" prompt="状态" renderer="con640_red_fun"/>
            </a:form>
            <a:form column="4" marginWidth="100" title="查询条件">
                <a:lov name="contract_number" bindTarget="con640_query_ds" prompt="合同序号"/>
                <a:textField name="contract_name" bindTarget="con640_query_ds" prompt="合同名称"/>
                <a:textField name="search_term_1" bindTarget="con640_query_ds" prompt="合同号"/>
                <a:comboBox name="company_id_n" bindTarget="con640_query_ds" prompt="公司"/>
                <a:comboBox name="division_n" bindTarget="con640_query_ds" prompt="产品线"/>
                <a:textField name="serial_number" bindTarget="con640_query_ds" prompt="设备编号"/>
                <a:datePicker name="due_date_from" bindTarget="con640_query_ds" prompt="应收日期从"/>
                <a:datePicker name="due_date_to" bindTarget="con640_query_ds" prompt="应收日期到"/>
            </a:form>
            <a:grid id="con640_result_grid_ds" bindTarget="con640_result_ds" marginHeight="280" marginWidth="80" navBar="true">
                <a:columns>
                    <a:column name="contract_number" align="left" lock="true" prompt="合同序号" width="100"/>
                    <a:column name="search_term_1" align="left" lock="true" prompt="合同号" width="100"/>
                    <a:column name="bp_id_tenant_n" align="left" lock="true" prompt="承租人" width="100"/>
                    <a:column name="division_n" align="center" prompt="产品线" width="60"/>
                    <a:column name="specification" align="center" prompt="机型" width="100"/>
                    <a:column name="serial_number" align="center" prompt="设备编号" width="100"/>
                    <a:column name="direct_debit_bank_id_n" align="center" prompt="付款方式" width="100"/>
                    <a:column name="dd_bank_account_num" align="left" prompt="扣款卡号" width="100"/>
                    <a:column name="with_holding_bank_name" align="left" prompt="代扣账户名" width="100"/>
                    <a:column name="inception_of_lease" align="right" prompt="起租日" renderer="Leaf.formatDate" width="100"/>
                    <a:column name="cf_end_date" align="right" prompt="到期日" renderer="Leaf.formatDate" width="100"/>
                    <a:column name="due_amount_all" align="right" prompt="月还款金额(计划)" width="100"/>
                    <a:column name="due_amount_current" align="right" prompt="本月应收租金" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="due_amount_old" align="right" prompt="陈欠租金" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="due_amount_now" align="right" prompt="当前应收租金" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="penalty_1" align="right" prompt="第一期罚息" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="penalty_sum" align="right" prompt="总罚息" renderer="Leaf.formatMoney" width="100"/>
                    <!-- <a:column name="lease_mgt_fee" align="right" prompt="管理费" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="lease_charge" align="right" prompt="手续费" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="deposit" align="right" prompt="保证金" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="insurance_fee" align="right" prompt="保险费" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="notarial_fee" align="right" prompt="公证费" renderer="Leaf.formatMoney" width="100"/> -->
                    <a:column name="machine_law_fee" align="right" prompt="拖车费用-律师函" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="contract_status_n" align="right" prompt="合同状态" width="100"/>
                    <!-- <a:column name="ccr_detail" align="center" prompt="变更明细"  renderer="render_doc_monitorDs_grid" width="100"/> -->
                    <a:column name="legal_status_n" align="right" prompt="法务状态" width="100"/>
                    <a:column name="collector_user_id_n" align="right" prompt="客户服务专员" width="100"/>
                    <a:column name="due_amount_month" align="right" prompt="本月已收租金" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="ar_description" align="right" prompt="备注" width="100"/>
                    <a:column name="due_date" align="right" prompt="应收日期" renderer="Leaf.formatDate" width="100"/>
                    <a:column name="company_id_n" align="right" prompt="出租人" width="100"/>
                    <a:column name="third_lease_mgt_fee" align="right" prompt="第三方管理费" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="contract_change_counter_fee" align="right" prompt="合同变更手续费" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="legal_fee" align="right" prompt="法律费用" renderer="Leaf.formatMoney" width="100"/>
                    <a:column name="reschedule_flag" align="center" prompt="是否支付日调整" width="92"/>
                    <a:column name="et_flag" align="center" prompt="是否提前结清" width="92"/>
                    <a:column name="tenant_change_flag" align="center" prompt="是否合同转让" width="92"/>
                    <a:column name="contract_cancel_flag" align="center" prompt="是否合同撤销" width="92"/>
                    <a:column name="cashflow_change_flag" align="center" prompt="是否租金计划变更" width="105"/>
                    <a:column name="bank_acc_change_flag" align="center" prompt="是否银行卡号变更" width="92"/>
                    <a:column name="lease_item_change_flag" align="center" prompt="是否租赁物变更" width="92"/>
                    <a:column name="mortgage_change_flag" align="center" prompt="是否抵押物变更" width="92"/>
                    <a:column name="guarantor_change_flag" align="center" prompt="是否担保人变更" width="92"/>
                </a:columns>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
