<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: ZLF 
    $Date: 2014-11-18 上午11:07:38  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure><![CDATA[
	]]></a:init-procedure>
    <a:view>
        <a:link id="attachment_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <script type="text/javascript"><![CDATA[
            function change_reg_save() {
                var record = $('con_contract_change_req_head_ds').getCurrentRecord();
                if (!record.get('description')) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '变更原因不能为空!');
                    return;
                }
                Leaf.Masker.mask($('con_contrac_change_req_detail_window').wrap, '${HLS.EXECUTING}');
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/cont.CON747.con_contract_penatly_req_header_save/update',
                    para: {
                        change_req_id: record.get('change_req_id'),
                        contract_id: record.get('contract_id'),
                        change_reason: record.get('description')
                    },
                    success: function(res) {
                        var records = $('con_contract_change_req_line_ds').getAll();
                        //
                        var datas = [];
                        for (var i = 0;i < records.length;i++) {
                            var obj = {};
                            obj['change_req_id'] = records[i].get('change_req_id');
                            obj['change_penalty_amount'] = records[i].get('change_penalty_amount');
                            obj['cashflow_id'] = records[i].get('cashflow_id');
                            obj['derate_amount'] = records[i].get('derate_amount');
                            obj['_status'] = 'update';
                            datas[i] = obj;
                        }
                        //
                        Leaf.request({
                            url: '${/request/@context_path}/autocrud/cont.CON748.con_contract_change_req_detail/batch_update',
                            para: datas,
                            //sync: true,
                            success: function() {
                                Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                                Leaf.SideBar.show({
                                    msg: '保存成功',
                                    duration: 2000
                                });
                            },
                            failure: function() {
            
                                Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                            },
                            error: function() {
                                alert(error);
                                Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                            }
                        });
                    },
                    failure: function() {
                        alert(failure);
                        Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                    },
                    error: function() {
                        alert(error);
                        Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                    }
                });
            }
            
            function req_detail_approve() {
                Leaf.showConfirm('${l:HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', con_change_req_approve, function() {
                    return;
                });
            }
            
            function con_change_req_approve() {
                change_reg_save();
                var record = $('con_contract_change_req_head_ds').getCurrentRecord();
                //
                Leaf.request({
                    url: '${/request/@context_path}/autocrud/cont.CON747.con_contract_penatly_submit/execute',
                    para: {
                        change_req_id: record.get('change_req_id')
                    },
                    sync: true,
                    success: function(res) {
                        Leaf.SideBar.show({
                            msg: '提交成功',
                            duration: 2000
                        });
                        Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                        $('con_contrac_change_req_detail_window').close();
                    },
                    failure: function() {
                        Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                    },
                    error: function() {
                        Leaf.Masker.unmask($('con_contrac_change_req_detail_window').wrap);
                    }
            
            
                });
            }
            
            function attachment_uploadFile() {
                var record = $('con_contract_change_req_head_ds').getCurrentRecord();
                //
                if (!record.get('change_req_id')) {
                    Leaf.showMessage('${l:HLS.PROMPT}', '请先保存！');
                    return;
                }
                var url = $('attachment_uploadFile_id').getUrl() + '?table_name=CON_CONTRACT_CHANGE_REQ&header_id=' + record.get('change_req_id');
                var win = new Leaf.Window({
                    url: url,
                    title: '${l:HLS.SUPPORTING_DOCUMENT}',
                    id: 'uploadFile_id',
                    width: 850,
                    height: 400
                });
                win.on('close', function() {});
            }
            
            function req_detail_close() {
                $('con_contrac_change_req_detail_window').close();
            }
            
            function con_change_req_line_ds_update(ds, record, name, value, oldvalue) {
                if (name == 'derate_amount') {
                    var due_amount = record.get('due_amount');
                    var change_penalty_amount = due_amount - value;
                    if (change_penalty_amount < 0) {
                        Leaf.showConfirm('${l:HLS.PROMPT}', '减免后的应收金额小于零,请确认!', function() {
                            record.set('change_penalty_amount', change_penalty_amount);
                            return;
                        }, function() {
                            record.set('derate_amount', oldvalue);
                            return;
                        });
                    } else {
                        record.set('change_penalty_amount', change_penalty_amount);
                    }
                }
            }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="contract_change_req_status_ds_2" lookupCode="CON_CHANGE_REQ_STATUS"/>
            <a:dataSet id="con_contract_change_req_head_ds" autoQuery="true" model="cont.CON748.con_contract_change_req" queryUrl="${/request/@context_path}/autocrud/cont.CON748.con_contract_change_req/query?change_req_id=${/parameter/@change_req_id}">
                <a:fields>
                    <a:field name="status_desc" displayField="code_value_name" options="contract_change_req_status_ds_2" returnField="req_status" valueField="code_value"/>
                    <a:field name="description" required="true"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="con_contract_change_req_line_ds" autoQuery="true" model="cont.CON748.con_contract_change_req_detail" queryUrl="${/request/@context_path}/autocrud/cont.CON748.con_contract_change_req_detail/query?change_req_id=${/parameter/@change_req_id}" selectable="true">
                <a:events>
                    <a:event name="update" handler="con_change_req_line_ds_update"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="req_detail_close" text="HLS.BACK"/>
                <a:gridButton click="change_reg_save" text="HLS.SAVE"/>
                <a:gridButton click="req_detail_approve" text="HLS.SUBMIT"/>
                <a:gridButton click="attachment_uploadFile" text="附件上传"/>
            </a:screenTopToolbar>
            <a:form marginWidth="50" title="查询条件">
                <a:hBox>
                    <a:textField name="change_req_number" bindTarget="con_contract_change_req_head_ds" prompt="申请编号" readOnly="true"/>
                    <a:datePicker name="req_date" bindTarget="con_contract_change_req_head_ds" prompt="申请日期从" readOnly="true"/>
                    <a:textField name="contract_name" bindTarget="con_contract_change_req_head_ds" prompt="合同名称" readOnly="true"/>
                    <a:textField name="contract_number" bindTarget="con_contract_change_req_head_ds" prompt="合同编号" readOnly="true"/>
                    <a:comboBox name="req_status_desc" bindTarget="con_contract_change_req_head_ds" prompt="申请状态" readOnly="true"/>
                </a:hBox>
                <a:hBox>
                    <a:textArea name="description" bindTarget="con_contract_change_req_head_ds" prompt="变更原因" width="380"/>
                </a:hBox>
            </a:form>
            <a:grid id="con_contract_change_req_line_grid" bindTarget="con_contract_change_req_line_ds" marginHeight="250" marginWidth="50" navBar="true">
                <a:columns>
                    <a:column name="cf_item_desc" align="center" prompt="HLS.CF_ITEM_DESC" width="120"/>
                    <a:column name="times" align="center" prompt="HLS.TIMES" width="100"/>
                    <a:column name="rental_amount" align="right" prompt="CON305.ACC_AMOUNT" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="due_date" align="center" prompt="HLS.DUE_DATE" renderer="Leaf.formatDate" width="120"/>
                    <a:column name="overdue_max_days" align="right" prompt="CON747.OVERDUE_MAX_DAYS" width="120"/>
                    <a:column name="due_amount" align="right" prompt="CON305.ACC_AMOUNT" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="received_amount" align="right" prompt="已收金额" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="unreceived_amount" align="right" prompt="未收金额" renderer="Leaf.formatMoney" width="120"/>
                    <a:column name="derate_amount" align="right" editor="line_nf" prompt="减免金额" renderer="Leaf.formatMoney" width="150"/>
                    <a:column name="change_penalty_amount" align="right" prompt="减免后应收金额" renderer="Leaf.formatMoney" width="120"/>
                </a:columns>
                <a:editors>
                    <a:numberField id="line_nf" allowDecimals="true" allowFormat="true" allowNegative="false"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
