<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: niminmin
    $Date: 2020-6-24
    $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="cancel_rent_trans_penalty_change_link" model="cont.CON7000.create_rent_trans_penalty_change"
                modelaction="update"/>
        <a:link id="submit_rent_trans_penalty_change_link" model="cont.CON7000.rent_trans_penalty_submit"
                modelaction="update"/>
        <a:link id="update_rent_trans_penalty_link"
                url="${/request/@context_path}/modules/cont/CON7000/con_rent_trans_penalty_deal.lsc"/>
        <script type="text/javascript"><![CDATA[
        function con7000_back() {
            $('${/parameter/@winId}').close();
        }

        function con7000_save() {
            var hd_record = $('con_rent_trans_penalty_hd_ds').getAt(0),
                business_sum_penalty = hd_record.get('business_sum_penalty'),
                account_sum_penalty = hd_record.get('account_sum_penalty');
            var csh_recordAll = $('con7000_csh_write_off_ds').getSelected();
            var param = {};
            var saveData = [];
            param['contract_id'] = hd_record.get('contract_id');
            param['record_id'] = hd_record.get('record_id');
            param['description'] = hd_record.get('description');
            if (csh_recordAll.length < 1) {
                Leaf.showMessage('提示', '请勾选营业核销记录');
                return;
            }
            for (var i = 0; i < csh_recordAll.length; i++) {
                var csh_record;
                csh_record = csh_recordAll[i];
                csh_record.set('_status', 'insert');
                csh_record.set('record_id', hd_record.get('record_id'));
                saveData.push(csh_record.data);
            }
            param['details'] = saveData;
            Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
            Leaf.request({
                url: $('update_rent_trans_penalty_link').getUrl(),
                para: param,
                success: function (res) {
                    Leaf.Masker.unmask(Ext.getBody());
                    $('con7000_gld_write_off_ds').query();
                },
                failure: function () {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                error: function () {
                    Leaf.Masker.unmask(Ext.getBody());
                },
                scope: this
            });
        }

        function con7000_submit() {
            var hd_record = $('con_rent_trans_penalty_hd_ds').getAt(0);
            var gld_record = $('con7000_gld_write_off_ds').getAll();
            if (gld_record.length < 1) {
                Leaf.showMessage('提示', '罚息调整数据不能为空！');
                return;
            }
            var csh_recordSelected = $('con7000_csh_write_off_ds').getSelected();
            var csh_recordAll = $('con7000_csh_write_off_ds').getAll();
            var received_amount = 0;
            var uns_received_amount = 0;
            var count = 0;
            var msg = '您好，';
            if(csh_recordSelected.length){
                csh_recordSelected.forEach(function (v, i) {
                    received_amount = v.get('received_amount')||0;
                    csh_recordAll.forEach(function (v1, i1) {
                        uns_received_amount = v1.get('received_amount')||0;
                        if(received_amount > 0 && received_amount == uns_received_amount && v1.get('trans_pently_flag') == 'N'){
                           count = count + 1;
                        }
                    });
                    if(count > 0){
                       msg = msg + '营业核销' + received_amount + '元的违约金有' + count + '条记录；';
                    }
                    count = 0;
                });
                msg = msg + '您确认修改该笔违约金对应的会计核销记录吗?';
            }


            Leaf.showConfirm('${l:HLS.PROMPT}', msg, function () {
                Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
                Leaf.request({
                    url: $('submit_rent_trans_penalty_change_link').getUrl(),
                    para: {
                        contract_id: hd_record.get('contract_id'),
                        record_id: hd_record.get('record_id')
                    },
                    success: function (res) {
                        Leaf.Masker.unmask(Ext.getBody());
                        $('${/parameter/@winId}').close();
                    },
                    failure: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    error: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    scope: this
                });
            });
        }

        function con7000_cancel() {
            var hd_record = $('con_rent_trans_penalty_hd_ds').getAt(0);
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认取消变更？', function () {
                Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
                Leaf.request({
                    url: $('cancel_rent_trans_penalty_change_link').getUrl(),
                    para: {
                        contract_id: hd_record.get('contract_id'),
                        record_id: hd_record.get('record_id')
                    },
                    success: function (res) {
                        Leaf.Masker.unmask(Ext.getBody());
                        $('${/parameter/@winId}').close();
                    },
                    failure: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    error: function () {
                        Leaf.Masker.unmask(Ext.getBody());
                    },
                    scope: this
                });
            });
        }
        
        function con7000_csh_write_off_ds_load(ds) {
            var records = ds.getAll();
            if('${/parameter/@function}' == 'CON7001'){
                if(records.length){
                    records.forEach(function(v,i){
                        if(v.data.trans_pently_flag == 'Y'){
                            setTimeout(function() {
                                ds.select(v);
                            }, 1);
                        }
                    });
                }
            }
        }
        function change_background_color(record) {

           if (record.get('trans_pently_flag') == 'Y') {
               return 'background-color:#00ff00';
             }

        }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="con_rent_trans_penalty_hd_ds" autoPageSize="true" autoQuery="true"
                       model="cont.CON7001.con_rent_trans_penalty_modify_query"
                       queryUrl="${/request/@context_path}/autocrud/cont.CON7001.con_rent_trans_penalty_modify_query/query?record_id=${/parameter/@record_id}">
                <!--                <a:fields>-->
                <!--                    <a:field name="description" required="true"/>-->
                <!--                </a:fields>-->
            </a:dataSet>
            <a:dataSet id="con7000_csh_write_off_ds" fetchAll="true" autoQuery="true"
                       model="cont.CON7001.con_rent_trans_penalty_csh_write_off"
                       queryUrl="${/request/@context_path}/autocrud/cont.CON7001.con_rent_trans_penalty_csh_write_off/query?contract_id=${/parameter/@contract_id}&amp;record_id=${/parameter/@record_id}"
                       selectable="true">
                <a:events>
                    <a:event name="load" handler="con7000_csh_write_off_ds_load"/>
                </a:events>
            </a:dataSet>
            
            <a:dataSet id="con7000_gld_write_off_ds" fetchAll="true" autoQuery="true"
                       model="cont.CON7001.con_rent_trans_penalty_gld_write_off"
                       queryUrl="${/request/@context_path}/autocrud/cont.CON7001.con_rent_trans_penalty_gld_write_off/query?record_id=${/parameter/@record_id}"/>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton id="con7000_back_id" click="con7000_back" text="HLS.EXIT"/>
                <a:gridButton id="con7000_save_id" click="con7000_save" text="保存"/>
                <a:gridButton id="con7000_submit_id" click="con7000_submit" text="提交审批"/>
                <a:gridButton id="con7000_cancel_id" click="con7000_cancel" text="取消变更"/>
            </a:screenTopToolbar>
            <a:form labelWidth="100" marginWidth="200" title="合同信息">
                <a:box column="4" labelWidth="115">
                    <a:textField name="apply_number" prompt="申请编号" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                    <a:textField name="contract_number" prompt="合同编号" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                    <a:textField name="contract_name" prompt="合同名称" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                    <a:textField name="bp_name" prompt="承租人名称" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                </a:box>
                <a:box column="3" labelWidth="115">
                    <a:textField name="bp_agent" prompt="代理店" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                    <a:textField name="apply_date" prompt="申请时间" readOnly="true"
                                 bindTarget="con_rent_trans_penalty_hd_ds"/>
                    <a:textField name="applyer" prompt="申请人" readOnly="true" bindTarget="con_rent_trans_penalty_hd_ds"/>
                </a:box>
                <a:box column="1">
                    <a:textArea name="description" bindTarget="con_rent_trans_penalty_hd_ds" prompt="变更理由及说明"
                                marginWidth="300"/>
                </a:box>
            </a:form>
            <a:tabPanel marginHeight="150" marginWidth="170">
                <a:tabs>
                    <a:tab prompt="营业违约金核销明细" width="150">
                        <a:grid id="con7000_csh_write_off_grid" bindTarget="con7000_csh_write_off_ds" marginHeight="130"
                                marginWidth="200" navBar="true" rowRenderer="change_background_color">
                            <a:columns>
                                <a:column name="times" prompt="回数" width="70" align="center"/>
                                <a:column name="due_date" prompt="预定支付日" width="120" align="center"
                                          renderer="Leaf.formatDate"/>
                                <a:column name="due_amount" prompt="应收金额" width="100" align="right"/>
                                <a:column name="cf_item_n" prompt="应收项目" width="100" align="center"/>
                                <a:column name="transaction_type" prompt="收款类型" width="120" align="center"/>
                                <a:column name="transaction_date" prompt="实际收款日" width="120" align="center"/>
                                <a:column name="transaction_num" prompt="现金事务编号" width="120" align="center"/>
                                <a:column name="write_off_date" prompt="核销日期" width="120" align="center"/>
                                <a:column name="received_amount" prompt="实收金额" width="120" align="center"/>
                                <a:column name="unreceived_amount" prompt="未收金额" width="116" align="center"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
            <a:tabPanel marginHeight="150" marginWidth="170">
                <a:tabs>
                    <a:tab prompt="营业违-对应-会计核销明细" width="180">
                        <a:grid id="con7000_gld_write_off_grid" bindTarget="con7000_gld_write_off_ds" marginHeight="300"
                                marginWidth="200" navBar="true">
                            <a:columns>
                                <a:column name="gld_times" prompt="回数" width="120" align="center"/>
                                <a:column name="gld_cf_item_n" prompt="应收项目" width="340" align="center"/>
                                <a:column name="gld_write_off_amount" prompt="会计核销金额" width="200" align="center"/>
                            </a:columns>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
        </a:screenBody>
    </a:view>
</a:screen>
