<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure>
        <a:model-query model="csh.CSH531N.sys_user_emp_email" rootPath="csh531_default_email"/>
    </a:init-procedure>
    <a:view>
        <a:link id="csh_transaction_detail_link" url="${/request/@context_path}/modules/csh/CSH533N/csh_receipt_write_off.lview"/>
        <a:link id="csh533_send_mail_link" model="csh.CSH531N.csh_mail_send" modelaction="execute"/>
        <a:link id="csh533_check_write_off" model="csh.CSH531N.csh_mail_send" modelaction="update"/>
        <a:link id="csh_write_off_upload_link" url="${/request/@context_path}/modules/csh/CSH533N/csh_write_off_import.lview"/>
        <a:link id="csh_create_receipt_link" url="${/request/@context_path}/modules/csh/CSH533N/csh_create_receipt_trx.lview"/>
        <a:link id="csh_calc_write_off_flag_link" model="csh.CSH531N.csh_transaction_interface" modelaction="execute"/>
        <a:link id="csh531_send_mail_to_col_link" model="csh.CSH531N.send_mail_to_collector" modelaction="execute"/>
        <a:link id="csh531_send_to_mail_to_link" model="csh.CSH531N.send_mail_to" modelaction="execute"/>
        <script type="text/javascript"><![CDATA[
            var g_batch_id = ${/parameter/@batch_id};
            
            function cshReceiptDetail(value, record, name) {
                var batch_id = record.get('batch_id');
                var trx_interface_id = record.get('trx_interface_id');
                var csh_bank_slip_num = record.get('csh_bank_slip_num');
                var company_id = record.get('company_id');
                var csh_trx_date = Leaf.formatDate(record.get('csh_trx_date'));
                var csh_trx_period_name = record.get('csh_trx_period_name');
                var csh_trx_period_num = record.get('csh_trx_period_num');
                var csh_bp_bank_account_num = record.get('csh_bp_bank_account_num');
                return '<a href="javascript:csh_detail_fun(' + batch_id + ',' + trx_interface_id + ',\'' + csh_bank_slip_num + '\',' + company_id + ',\'' + csh_trx_date + '\',\'' + csh_trx_period_name + '\',' + csh_trx_period_num + ',\'' + csh_bp_bank_account_num + '\')">${l:CSH533.WRITE_OFF_DETAIL}</a>';
            }
            
            function csh_detail_fun(batch_id, trx_interface_id, csh_bank_slip_num, company_id, csh_trx_date, csh_trx_period_name, csh_trx_period_num, csh_bp_bank_account_num) {
                //
                var win = new Leaf.Window({
                    id: 'csh_transaction_detail_window',
                    url: $('csh_transaction_detail_link').getUrl(),
                    params: {
                        batch_id: batch_id,
                        trx_interface_id: trx_interface_id,
                        batch_status: '${/parameter/@batch_status}',
                        csh_bank_slip_num: csh_bank_slip_num,
                        company_id: company_id,
                        csh_trx_date: csh_trx_date,
                        csh_trx_period_name: csh_trx_period_name,
                        csh_trx_period_num: csh_trx_period_num,
                        csh_bp_bank_account_num: csh_bp_bank_account_num
                    },
                    title: '${l:CSH531.WRITE_OFF_DETAIL}',
                    width: 1000,
                    height: 430
                });
                win.on('close', function() {
                    Leaf.request({
                        url: $('csh533_check_write_off').getUrl(),
                        para: {
                            batch_id: g_batch_id
                        },
                        success: function() {},
                        scope: this
                    });
                    $('csh_transaction_interface_ds').query();
                });
            }
            
            function csh_close_window() {
                // $('csh_receipt_detail_window').close();
            
                // if($('csh_receipt_detail_window')){
                // $('csh_receipt_detail_window').close();
                // }else{
                location.href = $('csh_create_receipt_link').getUrl();
                // }
            }
            
            function csh533_receipt_save() {
                Leaf.request({
                    url: $('csh_calc_write_off_flag_link').getUrl(),
                    para: {
                        batch_id: ${/parameter/@batch_id}
                    },
                    success: function() {},
                    scope: this
                });
                $('csh_transaction_interface_ds').submit();
            }
            
            function send_mail() {
                Leaf.request({
                    url: $('csh533_send_mail_link').getUrl(),
                    para: {
                        batch_id: '${/parameter/@batch_id}'
                    },
                    success: function() {
                        Leaf.showMessage('${l:PROMPT}', '发送成功!');
                    },
                    scope: this
                });
            }
            
            function csh533_send_mail() {
                Leaf.showConfirm('${l:PROMPT}', '是否将分配结果邮件发送到催款员?', function() {
                    send_mail();
                }, function() {});
            }
            
            function do_import() {
                new Leaf.Window({
                    id: 'csh_write_off_upload_window',
                    params: {
                        winid: 'csh_write_off_upload_window'
                    },
                    url: $('csh_write_off_upload_link').getUrl() + '?batch_id=${/parameter/@batch_id}',
                    title: '${l:HLS.IMPORT}',
                    width: 420,
                    height: 275
                });
            }
            
            function csh533_import_write_off() {
                Leaf.showConfirm('${l:PROMPT}', '导入分配将覆盖自动分配的数据,是否继续?', function() {
                    do_import();
                }, function() {});
            }
            
            function csh533_query() {
                $('csh_transaction_interface_ds').query();
            }
            
            function csh531_mail_send_to_col() {
                Leaf.showConfirm('${l:PROMPT}', '是否将分配结果邮件发送到催款员?', function() {
                    Leaf.request({
                        url: $('csh531_send_mail_to_col_link').getUrl(),
                        para: {
                            batch_id: '${/parameter/@batch_id}'
                        },
                        success: function() {
                            Leaf.showMessage('${l:PROMPT}', '发送成功!');
                        },
                        scope: this
                    });
                }, function() {});
            }
            
            function csh531_mail_send_mail_to() {
                if ($('csh_transaction_interface_mail_to_ds').validate()) {
                    Leaf.showConfirm('${l:PROMPT}', '是否将分配结果邮件发送到指定人员?', function() {
                        Leaf.request({
                            url: $('csh531_send_to_mail_to_link').getUrl(),
                            para: {
                                batch_id: '${/parameter/@batch_id}',
                                mail_to: $('csh_transaction_interface_mail_to_ds').getAt(0).get('mail_to')
                            },
                            success: function() {
                                Leaf.showMessage('${l:PROMPT}', '发送成功!');
                            },
                            scope: this
                        });
                    }, function() {});
                }
            }
            
            function summaryRenderer(datas, name) {
                var sum = 0;
                var sum_2 = 0;
                if (name == "write_off_amount") {
                    for (var i = 0;i < datas.length;i++) {
                        record = datas[i];
                        var t_1 = record.get("write_off_amount");
                        var t_2 = parseFloat(t_1);
                        if (!isNaN(t_2)) {
                            sum += t_2;
                        }
                    }
                }
                if (name == "ebank_result_success_amt") {
                    for (var j = 0;j < datas.length;j++) {
                        record = datas[j];
                        var n_1 = record.get("ebank_result_success_amt");
                        var n_2 = parseFloat(n_1);
                        if (!isNaN(n_2)) {
                            sum_2 += n_2;
                        }
                    }
                }
                if (name == "csh_bank_account_num") {
                    return '<div align="right">合计:</div>';
                }
                if (name == "ebank_result_success_amt") {
                    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_remove_write() {
                $('csh533_transaction_interface_gd').remove();
            }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="write_off_flag_ds" lookupCode="CSH531_CSH_INTERFACE_WRITE_OFF_FLAG"/>
            <a:dataSet id="lock_flag_ds" lookupCode="CSH531_LOCK_FLAG"/>
            <a:dataSet id="csh_transaction_interface_mail_to_ds" autoCreate="true">
                <a:fields>
                    <a:field name="mail_to" defaultValue="${/model/csh531_default_email/record/@email}" fetchRemote="false" lovGridHeight="320" lovHeight="500" lovService="csh.CSH531N.csh_mail_to_list" lovWidth="500" required="true">
                        <a:mapping>
                            <a:map from="email" to="mail_to"/>
                        </a:mapping>
                    </a:field>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="csh_receipt_detail_query" autoCreate="true">
                <a:fields>
                    <a:field name="csh_bank_slip_num"/>
                    <a:field name="csh_bank_account_num"/>
                    <a:field name="csh_trx_date"/>
                    <a:field name="csh_bp_bank_account_num"/>
                    <a:field name="csh_bp_bank_account_name"/>
                    <a:field name="batch_id" defaultValue="${/parameter/@batch_id}"/>
                    <a:field name="csh_description"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="csh_transaction_interface_ds" autoPageSize="true" autoQuery="true" model="csh.CSH531N.csh_transaction_interface" queryDataSet="csh_receipt_detail_query" selectable="true">
                <a:fields>
                    <a:field name="lock_flag"/>
                    <a:field name="lock_flag_desc" displayField="code_value_name" options="lock_flag_ds" returnField="lock_flag" valueField="code_value"/>
                    <a:field name="write_off_flag"/>
                    <a:field name="write_off_flag_desc" displayField="code_value_name" options="write_off_flag_ds" returnField="write_off_flag" valueField="code_value"/>
                </a:fields>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="csh_close_window" text="HLS.RETURN"/>
                <a:gridButton click="csh533_import_write_off" text="CSH531.IMPORT_WRITE_OFF"/>
                <a:gridButton click="csh533_receipt_save" text="HLS.SAVE"/>
                <a:gridButton click="csh533_remove_write" text="HLS.REMOVE"/>
                <a:gridButton click="csh533_query" text="HLS.QUERY"/>
                <a:gridButton click="csh531_mail_send_to_col" text="邮件发送催款员"/>
                <a:gridButton click="csh531_mail_send_mail_to" text="邮件发送到"/>
                <a:lov name="mail_to" bindTarget="csh_transaction_interface_mail_to_ds" prompt="接收人" style="margin-top:10px"/>
            </a:screenTopToolbar>
            <a:form column="2" title="查询条件">
                <a:textField name="csh_description" bindTarget="csh_receipt_detail_query" prompt="合同序号"/>
            </a:form>
            <a:grid id="csh533_transaction_interface_gd" bindTarget="csh_transaction_interface_ds" marginHeight="190" marginWidth="30" navBar="true">
                <a:columns>
                    <a:column name="csh_bank_account_num" footerRenderer="summaryRenderer" prompt="CSH531.ACCOUNT_NUM" width="150"/>
                    <a:column name="csh_trx_date" prompt="CSH531.TRX_DATE" renderer="Leaf.formatDate" width="80"/>
                    <a:column name="csh_bp_bank_account_num" prompt="CSH531.BANK_ACCOUNT_NUM" width="150"/>
                    <a:column name="csh_bp_bank_account_name" prompt="CSH531.BANK_ACCOUNT_NAME"/>
                    <a:column name="csh_trx_amount" align="right" editor="csh533_receipt_grid_nf" prompt="CSH531.TRX_AMOUNT" renderer="Leaf.formatMoney"/>
                    <a:column name="csh_currency_code" align="center" prompt="CSH531.CURRENCY_CODE" width="60"/>
                    <a:column name="csh_description" prompt="CSH531.DESCRIPTION" width="200"/>
                    <!--    <a:column name="lock_flag_desc" editor="csh_transfer_grid_cb" prompt="CSH531.LOCK_FLAG" width="80"/>
                    <a:column name="post_flag" align="center" prompt="CSH531.POST_FLAG" width="60"/> -->
                    <a:column name="ebank_success_status" align="center" prompt="CSH5313.EBANK_SUCCESS_STATUS" width="55"/>
                    <a:column name="ebank_result_status_desc" prompt="CSH533.EBANK_RESULT_STATUS" width="55"/>
                    <a:column name="ebank_result_acc_balance" align="right" prompt="CSH533.EBANK_RESULT_ACC_BALANCE" renderer="Leaf.formatMoney"/>
                    <a:column name="ebank_result_success_amt" align="right" footerRenderer="summaryRenderer" prompt="CSH533.EBANK_RESULT_SUCCESS_AMT" renderer="Leaf.formatMoney"/>
                    <a:column name="write_off_amount" align="right" footerRenderer="summaryRenderer" prompt="分配金额" renderer="Leaf.formatMoney"/>
                    <a:column name="lock_flag_desc" editor="csh_transfer_grid_cb" prompt="CSH531.LOCK_FLAG" width="60"/>
                    <a:column name="write_off_flag_desc" align="center" prompt="CSH531.WRITE_OFF_FLAG" width="60"/>
                    <a:column align="center" prompt="CSH533.WRITE_OFF_DETAIL" renderer="cshReceiptDetail" width="60"/>
                </a:columns>
                <a:editors>
                    <a:comboBox id="csh_transfer_grid_cb"/>
                    <a:numberField id="csh533_receipt_grid_nf"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>