<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:view>
        <script type="text/javascript"><![CDATA[
    	    
    	    function init(){
    	        var batch_status = '${/parameter/@batch_status}';
    	        if(batch_status=='SUBMIT'){
    	            $('csh_grid_add_bt').setVisible(false);
    	            $('csh_grid_delete_bt').setVisible(false);
    	            $('csh_grid_save_bt').setVisible(false);
    	            $('csh_write_off_grid').hideColumn('left_amount');
    	            $('csh_write_off_grid').hideColumn('left_principal');
    	            $('csh_write_off_grid').hideColumn('left_interest');
    	        }else{
    	            $('csh_grid_add_bt').setVisible(true);
    	            $('csh_grid_delete_bt').setVisible(true);
    	            $('csh_grid_save_bt').setVisible(true);
    	        }
    	    }
    	    
    	    function csh_write_off_add(){
    	        var record = $('csh_write_off_interface_ds').create();
                $('csh_write_off_grid').showEditorByRecord(record);
    	    }
    	    
    	    function csh_edit_function(record,name){
    	        var batch_status = '${/parameter/@batch_status}';
    	        if(batch_status=='SUBMIT'){
    	            return;
    	        }else{
    	            if(name == 'contract_number'){
    	                if(record.get('write_off_type_desc') == '收款核销债权'){ 
    	                    record.getField('contract_number').setRequired(true);
    	                	return 'csh_write_off_grid_lv';
    	                }else{
    	                    record.getField('contract_number').setRequired(false);
    	                	record.set('contract_number','');
    	                	record.set('write_off_times','');
    	                	record.set('write_off_cf_item');
    	                	record.set('write_off_cf_item_desc','');
    	                }
    	            }
    	            // if(name == 'write_off_times'){return 'csh_write_off_grid_nf';}
    	            // if(name == 'write_off_cf_item_desc'){return 'csh_write_off_grid_cb';}
    	            // if(name == 'write_off_amount'){return 'csh_write_off_grid_nf';}
    	            // if(name == 'due_date'){return 'csh_write_off_grid_dp';}
    	            if(name == 'write_off_type_desc'){return 'csh_write_off_grid_cb';}
    	            if(name == 'write_off_bp_name'){return 'csh_write_off_grid_lv';}
    	            if(name == 'write_off_amount' || name=='write_off_principal'||name=='write_off_interest'){return 'csh_write_off_grid_nf';}
    	        }
    	    }
    	    
    	    function summaryRenderer(datas,name){
    	        var sum = 0;
    	        var sum_2 = 0;
                for (var i = 0;i < datas.length;i++) {
                    record = datas[i];
                    if (name == "write_off_amount" ) {
                        var t_1 = record.get("write_off_amount" );
                        var  t_2 = parseFloat(t_1);
                        if (!isNaN(t_2)) {
                            sum += t_2;
                        }
                    }
                    // if (name == "unreceived_amount" ) {
                        // var n_1 = record.get("unreceived_amount" );
                        // var  n_2 = parseFloat(n_1);
                        // if (!isNaN(n_2)) {
                            // sum_2 += n_2;
                        // }
                    // }
                }
                if (name == "write_off_type_desc") {
                    return '<div align="right">合计:</div>';
                }
                // if (name == "unreceived_amount" ) {           
                    // return '<font color="red">' + Leaf.formatNumber(sum_2, 2) + '</font>';
                // }
                if (name == "write_off_amount" ) {           
                    return '<font color="red">' + Leaf.formatNumber(sum, 2) + '</font>';
                }
    	    }
    	    
    	    function csh533_wf_amount_validate(record,name,value){
    	        var batch_status = '${/parameter/@batch_status}';
    	        if(batch_status=='SUBMIT'){
    	            return true;
    	        }else{
    	            if (value > record.get('left_amount')){
    	                return '分配金额不能大于剩余金额';
    	            }
    	        }
    	        return true;
    	    }
    	    function csh533_update_fun(ds,record,name,value,oldValue){
    	        if(name=='write_off_amount'){
    	            var left_amount = record.get('left_amount');
    	            var left_principal = record.get('left_principal');
    	            var left_interest = record.get('left_interest');
    	            var x_amount;
    	            if (value){
    	                if (value<=left_interest){
    	                    record.set('write_off_interest',value);
    	                    record.set('write_off_principal',null);
    	                }else if(value>left_interest){
    	                    record.set('write_off_interest',left_interest);
    	                    x_amount = value - left_interest;
    	                    record.set('write_off_principal',x_amount);
    	                }
    	            }else{
    	               record.set('write_off_interest',null);
    	               record.set('write_off_principal',null); 
    	            }
    	        }
    	    }
    	]]></script>
        <a:dataSets>
            <a:dataSet id="write_off_cf_item_ds" autoQuery="true" model="csh.CSH531N.hls_cashflow_item_v"/>
            <a:dataSet id="write_off_type_ds" autoQuery="true" model="csh.CSH531N.write_off_type_cb"/>
            <a:dataSet id="csh_write_off_interface_ds" autoQuery="true" fetchAll="true" model="csh.CSH531N.csh_write_off_interface" queryUrl="${/request/@context_path}/autocrud/csh.CSH531N.csh_write_off_interface/query?batch_id=${/parameter/@batch_id}&amp;trx_interface_id=${/parameter/@trx_interface_id}" selectable="true">
                <a:fields>
                    <a:field name="contract_id"/>
                    <a:field name="company_id" defaultValue="${/parameter/@company_id}"/>
                    <a:field name="write_off_date" defaultValue="${/parameter/@csh_trx_date}"/>
                    <a:field name="write_off_period_name" defaultValue="${/parameter/@csh_trx_period_name}"/>
                    <a:field name="write_off_period_num" defaultValue="${/parameter/@csh_trx_period_num}"/>
                    <a:field name="check_flag" defaultValue="Y"/>
                    <a:field name="record_type" defaultValue="MANUAL"/>
                    <a:field name="write_off_times"/>
                    <a:field name="write_off_amount" validator="csh533_wf_amount_validate"/>
                    <a:field name="due_date"/>
                    <a:field name="trx_interface_id" defaultValue="${/parameter/@trx_interface_id}"/>
                    <!-- <a:field name="unreceived_amount"/> -->
                    <a:field name="contract_number" lovHeight="530" lovUrl="${/request/@context_path}/modules/csh/CSH531/csh_write_off_ref_contract.lview?csh_bp_bank_account_num=${/parameter/@csh_bp_bank_account_num}" lovWidth="770" title="合同选择">
                        <a:mapping>
                            <a:map from="contract_id" to="contract_id"/>
                            <a:map from="contract_number" to="contract_number"/>
                            <a:map from="contract_name" to="contract_name"/>
                            <a:map from="times" to="write_off_times"/>
                            <a:map from="cf_item" to="write_off_cf_item"/>
                            <a:map from="cf_item_desc" to="write_off_cf_item_desc"/>
                            <a:map from="left_amount" to="write_off_amount"/>
                            <a:map from="due_date" to="due_date"/>
                            <a:map from="cashflow_id" to="write_off_cashflow_id"/>
                            <a:map from="left_amount" to="left_amount"/>
                            <a:map from="left_principal" to="left_principal"/>
                            <a:map from="left_interest" to="left_interest"/>
                            <a:map from="left_principal" to="write_off_principal"/>
                            <a:map from="left_interest" to="write_off_interest"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="batch_id" defaultValue="${/parameter/@batch_id}"/>
                    <a:field name="write_off_type"/>
                    <a:field name="write_off_type_desc" displayField="code_value_name" options="write_off_type_ds" returnField="write_off_type" valueField="code_value"/>
                    <a:field name="write_off_bp_id"/>
                    <a:field name="write_off_bp_name" autoComplete="true" autoCompleteField="true" lovAutoQuery="true" lovGridHeight="300" lovHeight="450" lovService="csh.CSH531N.hls_bp_master" lovWidth="450" title="分配对象选择">
                        <a:mapping>
                            <a:map from="bp_id" to="write_off_bp_id"/>
                            <a:map from="bp_name" to="write_off_bp_name"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="write_off_cf_item"/>
                    <a:field name="write_off_cf_item_desc" displayField="cf_item_desc" options="write_off_cf_item_ds" returnField="write_off_cf_item" valueField="cf_item"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="csh533_update_fun"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:grid id="csh_write_off_grid" bindTarget="csh_write_off_interface_ds" marginHeight="200" marginWidth="400">
                <a:toolBar>
                    <a:button id="csh_grid_add_bt" click="csh_write_off_add" icon="${/request/@context_path}/images/add.gif" text="新增"/>
                    <a:button id="csh_grid_delete_bt" type="delete"/>
                    <a:button id="csh_grid_save_bt" type="save"/>
                </a:toolBar>
                <a:columns>
                    <a:column name="write_off_type_desc" editorFunction="csh_edit_function" footerRenderer="summaryRenderer" prompt="HLS.WRITE_OFF_TYPE" width="100"/>
                    <a:column name="write_off_bp_name" editorFunction="csh_edit_function" prompt="CSH531.WRITE_OFF_BP_NAME" width="100"/>
                    <a:column name="contract_number" editorFunction="csh_edit_function" prompt="HLS.CONTRACT_NUMBER" width="120"/>
                    <a:column name="write_off_times" editorFunction="csh_edit_function" prompt="HLS.TIMES" width="80"/>
                    <a:column name="write_off_cf_item_desc" editorFunction="csh_edit_function" prompt="CSH531.CF_ITEM" width="80"/>
                    <!-- <a:column name="unreceived_amount" align="right" footerRenderer="summaryRenderer" prompt="未收金额" renderer="Leaf.formatMoney" width="120"/> -->
                    <a:column name="left_amount" align="right" footerRenderer="summaryRenderer" prompt="剩余金额" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="left_principal" align="right" footerRenderer="summaryRenderer" prompt="剩余本金" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="left_interest" align="right" footerRenderer="summaryRenderer" prompt="剩余利息" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="write_off_amount" align="right" editorFunction="csh_edit_function" footerRenderer="summaryRenderer" prompt="CSH531.AMOUNT" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="write_off_principal" align="right" prompt="分配本金" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="write_off_interest" align="right" prompt="分配利息" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="due_date" editorFunction="csh_edit_function" prompt="CSH531.WRITE_OFF_DATE" renderer="Leaf.formatDate" width="80"/>
                </a:columns>
                <a:editors>
                    <a:textField id="csh_write_off_grid_tf"/>
                    <a:numberField id="csh_write_off_grid_nf"/>
                    <a:datePicker id="csh_write_off_grid_dp"/>
                    <a:comboBox id="csh_write_off_grid_cb"/>
                    <a:lov id="csh_write_off_grid_lv"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
        <script type="text/javascript"><![CDATA[
            Leaf.onReady(init);
        ]]></script>
    </a:view>
</a:screen>