<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: qwm  
    $Date: 2013-5-22 上午11:10:19  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <!-- <a:model-query autoCount="false" defaultWhereClause=" contract_id=${/parameter/@contract_id}" fetchAll="true" model="csh.CSH501.csh_payment_req_selected" rootPath="payment_req_selected"/> -->
        <a:model-query autoCount="false" defaultWhereClause=" document_category=&apos;PAYMENT_REQ&apos; and  enabled_flag=&apos;Y&apos;" fetchAll="true" model="hls.HLS003.hls_document_type" rootPath="payment_document_type"/>
        <a:model-query autoCount="false" defaultWhereClause=" enabled_flag=&apos;Y&apos;" fetchAll="true" model="gld.gld_exchangerate_types" rootPath="exchangerate_types"/>
        <a:model-query autoCount="false" fetchAll="true" model="csh.CSH501.csh_payment_req_ln_query" rootPath="csh_payment_req_ln_query" trace="true"/>
    </a:init-procedure>
    <a:view>
        <div/>
        <a:link id="csh_payment_req_create" url="${/request/@context_path}/modules/csh/CSH501/csh_payment_req_update.lsc"/>
        <a:link id="csh_payment_req_selected" url="${/request/@context_path}/modules/csh/CSH501/csh_payment_req_selected.lview"/>
        <a:link id="csh_payment_req_amount_check" model="csh.CSH501.csh_payment_req_amount_check" modelaction="execute"/>
        <a:link id="csh_payment_update_link" url="${/request/@context_path}/modules/csh/CSH504/csh_payment_update.lview"/>
        <a:link id="csh_req_ddct_link" url="${/request/@context_path}/modules/csh/CSH504/csh_payment_req_ln_ddct.lview"/>
        <script type="text/javascript"><![CDATA[
            function onUpdate_payment_req_hd(dataSet, record, name, value, oldValue) {
                var line_ds = $('csh_payment_req_ln_ds');
                var lineRecords = line_ds.getAll();
                if (name == 'req_date') {
            
                    for (var i = 0;i < lineRecords.length;i++) {
                        lineRecords[i].set('apply_pay_date', value);
                    }
                } else if (name == 'bp_id') {
                    for (var j = 0;j < lineRecords.length;j++) {
                        lineRecords[j].set('bp_code', record.get('bp_code'));
                        lineRecords[j].set('bp_name', record.get('bp_name'));
                        lineRecords[j].set('bp_id', record.get('bp_id'));
                    }
            
                }
            }
            
            function onUpdate_payment_req_ln(dataSet, record, name, value, oldValue) {
            
                var header_ds = $('csh_payment_req_create_ds');
                var line_ds = $('csh_payment_req_ln_ds');
                var lineRecords = line_ds.getAll();
                var hrecord = header_ds.getAt(0);
                var currency_code;
            
                if (name == 'amount') {
                    var amount = 0;
                    for (var i = 0;i < lineRecords.length;i++) {
                        var lineRecord = lineRecords[i];
                        currency_code = lineRecord.get('currency_code');
                        if (record.get('currency_code') != currency_code) {
                            hrecord.set('amount', null);
                            hrecord.set('currency_code', null);
                            return;
                        } else {
                            current_amount = lineRecord.get('amount');
                            if (current_amount == 'NAN' || current_amount == 'undefined' || current_amount == null || current_amount == '') {
                                current_amount = 0;
                            }
                            amount = amount + current_amount;
                        }
                    }
                    hrecord.set('amount', amount);
                    hrecord.set('currency_code', currency_code);
            
                }
            }
            var payment_req_id;
            
            function csh501_payment_create(value) {
                if ($('csh_payment_req_create_ds').validate() && $('csh_payment_req_ln_ds').validate()) {
                    var headerds = $('csh_payment_req_create_ds');
                    var hrecord = headerds.getAt(0);
                    var headRecordData = hrecord.data;
                    var lineds = $('csh_payment_req_ln_ds');
                    var lrecord = lineds.getAll();
                    var datas = [];
                    for (var i = 0;i < lrecord.length;i++) {
                        var data = lrecord[i].data;
                        datas.push(data);
                    }
                    var req_date = hrecord.get('req_date');
                    req_date = Leaf.formatDate(req_date);
                    var queryParams = new Object();
                    queryParams['details'] = datas;
                    queryParams['document_type'] = hrecord.get('document_type');
                    queryParams['req_date'] = req_date;
                    queryParams['amount'] = hrecord.get('amount');
                    queryParams['currency_code'] = hrecord.get('currency_code');
                    queryParams['description'] = hrecord.get('description');
                    queryParams['bp_id'] = hrecord.get('bp_id');
                    queryParams['bp_category'] = hrecord.get('bp_category');
                    queryParams['apply_pay_date'] = hrecord.get('apply_pay_date');
                    queryParams['payment_req_id'] = payment_req_id;
                    Leaf.Masker.mask(Ext.getBody(), '${l:HLS.SAVING}');
                    Leaf.request({
                        url: $('csh_payment_req_create').getUrl(),
                        para: queryParams,
                        success: function(args) { //
            
                            payment_req_id = args.result.payment_req_id;
                            var r_record = args.result.details.record;
                            for (var i = 0;i < r_record.length;i++) {
            
                                lineds.getAt(i).set('payment_req_ln_id', r_record[i].payment_req_ln_id);
                            }
                            $('csh_payment_req_ln_ds').setQueryUrl('${/request/@context_path}/autocrud/csh.CSH504.csh_payment_req_ln_upd_query/query?payment_req_id=' + payment_req_id);
                            if (value == 'sub') {
                                csh501_payment_submit();
                            } else {
                                Leaf.SideBar.show({
                                    msg: '${l:HLS.SUBMIT_SUCCESS}',
                                    duration: 2000
                                });
                                Leaf.Masker.unmask(Ext.getBody());
                                $('csh_payment_req_create_ds').query();
                                $('csh_payment_req_ln_ds').query();
                            }
                        },
                        failure: function() {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        error: function() {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        scope: this
                    });
                }
            }
            
            /* function onUpdate_payment_req_success() {
             window.location.href = $('csh_payment_req_selected').getUrl();
             } */
            
            
            
            function csh501_payment_back() {
                history.go(-1);
            }
            
            function csh501_payment_reset() {
                $('csh_payment_req_create_ds').getAt(0).set('document_type', '');
            
            }
            
            function csh501_payment_submit() {
            
            
                Leaf.showConfirm('${l:HLS.PROMPT}', '${l:HLS.ARE_YOU_SURE_TO_SUBMIT}', function okFun() {
                    Leaf.Masker.mask(Ext.getBody(), '${l:HLS.SAVING}');
                    Leaf.request({
                        url: '${/request/@context_path}/autocrud/csh.CSH501.upd_csh_payment_sub_flag/execute',
                        para: {
                            payment_req_id: payment_req_id,
                            _status: 'execute'
                        },
                        success: function(res) {
                            Leaf.SideBar.show({
                                msg: '${l:HLS.SUBMIT_SUCCESS}',
                                duration: 2000
                            });
                            Leaf.Masker.unmask(Ext.getBody());
                            window.location.href = $('csh_payment_req_selected').getUrl();
                        },
                        failure: function() {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        error: function() {
                            Leaf.Masker.unmask(Ext.getBody());
                        },
                        scope: this
                    });
                }, Leaf.Masker.unmask(Ext.getBody()));
            
            }
            
            function bp_id_check(record, lrecords) {
                var flag = 'Y';
                for (var j = 0;j < lrecords.length;j++) {
                    if (j == 0) {
                        bp_id = lrecords[j].get('bp_id');
                    } else {
                        if (bp_id != lrecords[j].get('bp_id')) {
                            flag = 'N';
                            break;
                        }
                    }
                }
                if (flag == 'Y') {
                    record.set('bp_code', lrecords[0].get('bp_code'));
                    record.set('bp_name', lrecords[0].get('bp_name'));
                    record.set('bp_id', lrecords[0].get('bp_id'));
            
                } else {
                    record.set('bp_code', '');
                    record.set('bp_name', '');
                    record.set('bp_id', '');
            
                }
            }
            
            function load_payment_req_ln(ds_ln) {
            
                var ds = $('csh_payment_req_create_ds');
                var record = ds.getAt(0),
                    records = ds.getAll();
                var line_amount = 0;
                var lrecords = ds_ln.getAll();
                var flag = 'Y';
                for (var j = 0;j < lrecords.length;j++) {
                    if (j == 0) {
                        currency_code = lrecords[j].get('currency_code');
                        line_amount = lrecords[j].get('amount');
                    } else {
                        if (currency_code != lrecords[j].get('currency_code')) {
                            flag = 'N';
                            break;
                        } else {
                            line_amount = line_amount + lrecords[j].get('amount');
                        }
                    }
                }
                if (flag == 'Y') {
                    record.set('amount', line_amount);
                    record.set('currency_code', lrecords[0].get('currency_code'));
                } else {
                    record.set('amount', '');
                    record.set('currency_code', '');
                }
            
                bp_id_check(record, lrecords);
            
                for (var i = 0;i < lrecords.length;i++) {
                    lrecords[i].set('apply_pay_date', new Date());
                }
            
                var contract_list = [];
                for (var m = 0;m < lrecords.length;m++) {
                    contract_list.add(lrecords[m].get('contract_id'));
                }
                contract_list = contract_list.join(',');
                record.getField('bp_code').setLovPara('contract_id', contract_list);
                record.getField('bp_code').setLovPara('contract_bp_flag', 'Y');
            }
            
            
            function render_req_create_grid(value, record, name) {
                if (name == 'bp_code') {
                    record.getField('bp_code').setLovPara('contract_id', record.get('contract_id'));
                    record.getField('bp_code').setLovPara('contract_bp_flag', 'Y');
            
                }
                return value;
            }
            
            function csh501_payment_save_submit() {
                csh501_payment_create('sub');
            
            }
            
            function csh504_ddct_link(value, record, name) {
                var payment_req_ln_id = record.get('payment_req_ln_id');
                if (!record.isNew && payment_req_ln_id) {
                    return '<a href="javascript:csh504_open_ddct_link(\'' + record.id + '\',\'' + record.ds.id + '\')">' + '抵扣' + '</a>';
                }
            }
            
            function csh504_open_ddct_link(record_id, ds_id) {
                var record = $(ds_id).findById(record_id);
                var payment_req_ln_id = record.get('payment_req_ln_id');
                var apply_amount = record.get('amount');
                var contract_number = record.get('contract_number');
                var contract_id = record.get('contract_id');
                var win = new Leaf.Window({
                    id: 'prj_project_bp_parameter_window',
                    url: $('csh_req_ddct_link').getUrl(),
                    params: {
                        apply_amount: apply_amount,
                        payment_req_ln_id: payment_req_ln_id,
                        contract_number: contract_number,
                        contract_id: contract_id
                    },
                    title: '抵扣',
                    fullScreen: true
                });
                win.on('close', function() {
                    $('csh_payment_req_ln_ds').query();
                });
            }
            
            
            function csh501_print() {
                var records_l = $('csh_payment_req_ln_ds').getAll();
                if (records_l.length > 0) {
                    var ds = $('csh_payment_req_create_ds');
                    var record = $('csh_payment_req_create_ds').getAt(0);
                    //var url_1 = 'http://199.10.10.65:8180/reportapp/frameset?__report=reports/req/' + 'con_payment_req.rptdesign' + '&&__format=PDF';
                    var url_1 = '${/request/@context_path}/reports?__report=reports/req/new/' + 'con_payment_req.rptdesign' + '&&__format=pdf';
                    var url_1_param = '&&batch_id=' + ${/session/@session_id} + '&&payment_req_id=' + record.get('payment_req_id');
                    window.open(url_1 + url_1_param);
                }
            }
            
            //付款申请打印

            function csh501_payment_print() {
                var records_l = $('csh_payment_req_ln_ds').getAll();
                var record = $('csh_payment_req_create_ds').getAt(0);
                if (records_l.length > 0) {
 
                    if (record.get('payment_req_id')) {
                        Leaf.request({
                            url: '${/request/@context_path}/autocrud/csh.CSH504.csh_payment_print/execute',
                            para: {
                                payment_req_id: record.get('payment_req_id'),
                                _status: 'execute'
                            },
                            success: function(res) {
                                csh501_print();
            
                            },
                            sync: true,
                            failure: function() {
                                Leaf.Masker.unmask(Ext.getBody());
                            },
                            error: function() {
                                Leaf.Masker.unmask(Ext.getBody());
                            },
                            scope: this
                        }, Leaf.Masker.unmask(Ext.getBody()));
                    } else {
                        Leaf.showMessage('${HLS.PROMPT}', '请先保存!');
                    }
                }

            }
            
            
            function csh501_act_amount(value, record, name) {
                var amount = record.get('amount');
                var sum_ddct_amount = record.get('sum_ddct_amount');
                if (!sum_ddct_amount) {
                    sum_ddct_amount = 0;
                }
                var act_amount = amount - sum_ddct_amount;
                return Leaf.formatMoney(act_amount);
            }
            /* function csh501_on_submit_sucess(ds,res){

             var payment_req_id = ds.getAt(0).get('payment_req_id');
             $('csh_payment_req_ln_ds').setQueryUrl('${/request/@context_path}/autocrud/csh.CSH504.csh_payment_req_ln_upd_query/query?payment_req_id='+payment_req_id);
             } */
            
            function csh501_trx_lov_fun(lov) {
                var record = this.record;
                record.getField(lov.binder.name).setLovPara('bp_id', record.get('bp_id'));
            }
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_authority_list_validate.lview?document_category=CONTRACT&amp;function_code=CSH501"/>
        <a:dataSets>
            <!-- <a:dataSet id="csh_payment_req_ds" selectable="true">
                <a:datas dataSource="/model/payment_req_selected"/>
            </a:dataSet> -->
            <a:dataSet id="document_type_ds">
                <a:datas dataSource="/model/payment_document_type"/>
            </a:dataSet>
            <a:dataSet id="exchangerate_types_ds">
                <a:datas dataSource="/model/exchangerate_types"/>
            </a:dataSet>
            <a:dataSet id="csh_payment_req_create_ds" autoCreate="true" submitUrl="csh_payment_req_update.lsc">
                <a:fields>
                    <a:field name="payment_req_id"/>
                    <!-- <a:field name="tenant_bp_id" defaultValue="${/model/payment_req_selected/record/@tenant_bp_id}"/> -->
                    <a:field name="document_type_dis" displayField="description" options="document_type_ds" required="true" returnField="document_type" valueField="document_type"/>
                    <a:field name="business_type" defaultValue="PAYMENT"/>
                    <!-- <a:field name="document_type" displayField="rate_method_code_display" options="exchangerate_types_ds" returnField="rate_method_code" valueField="rate_method_code"/> -->
                    <a:field name="bp_code" lovGridHeight="300" lovHeight="500" lovLabelWidth="100" lovService="basic.hls_bp_master_v_for_lov" lovWidth="530" readOnly="false" required="false" title="付款对象">
                        <a:mapping>
                            <a:map from="bp_code" to="bp_code"/>
                            <a:map from="bp_name" to="bp_name"/>
                            <a:map from="bp_category" to="bp_category"/>
                            <a:map from="bp_id" to="bp_id"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="bp_name" readOnly="true"/>
                    <a:field name="amount" readOnly="true"/>
                    <a:field name="currency_code" readOnly="true"/>
                    <a:field name="req_date" required="true"/>
                    <a:field name="apply_pay_date" required="false"/>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="onUpdate_payment_req_hd"/>
                    <!-- <a:event name="submitsuccess" handler="csh501_on_submit_sucess" /> -->
                </a:events>
            </a:dataSet>
            <a:dataSet id="csh_payment_req_ln_ds" autoPageSize="true" autoQuery="true" bindName="sub" bindTarget="csh_payment_req_create_ds" queryUrl="${/request/@context_path}/autocrud/csh.CSH501.csh_payment_req_ln_query/query" selectable="false">
                <!-- <a:datas dataSource="/model/csh_payment_req_ln_query"/> -->
                <a:fields>
                    <a:field name="residual_amount"/>
                    <a:field name="apply_pay_date" required="true"/>
                    <a:field name="amount" required="true"/>
                    <a:field name="bp_code" lovGridHeight="300" lovHeight="500" lovLabelWidth="100" lovService="basic.hls_bp_master_v_for_lov" lovWidth="530" required="true" title="付款对象">
                        <a:mapping>
                            <a:map from="bp_code" to="bp_code"/>
                            <a:map from="bp_name" to="bp_name"/>
                            <a:map from="bp_id" to="bp_id"/>
                            <a:map from="bp_category" to="bp_category"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="prepayment_trx" lovGridHeight="280" lovHeight="500" lovService="csh.CSH504.csh_prepayment_transaction" lovWidth="500">
                        <a:mapping>
                            <a:map from="transaction_id" to="prepayment_trx_id"/>
                            <a:map from="transaction_num" to="prepayment_trx"/>
                        </a:mapping>
                    </a:field>
                    <a:field name="bp_bank_account_code" autoComplete="true" autoCompleteField="bank_account_num" lovGridHeight="350" lovHeight="550" lovLabelWidth="90" lovService="basic.hls_bp_master_bank_account_v_for_lov?enabled_flag=Y&amp;bp_enabled_flag=Y" lovWidth="580" title="CSH511.BANK_ACCOUNT_CODE">
                        <a:mapping>
                            <a:map from="bank_account_id" to="bp_bank_account_id"/>
                            <a:map from="bank_account_num" to="bp_bank_account_num"/>
                            <a:map from="bank_account_name" to="bp_bank_account_name"/>
                            <a:map from="bank_account_code" to="bp_bank_account_code"/>
                        </a:mapping>
                    </a:field>
                </a:fields>
                <a:events>
                    <a:event name="update" handler="onUpdate_payment_req_ln"/>
                    <a:event name="load" handler="load_payment_req_ln"/>
                    <!-- <a:event name="submitsuccess" handler="onUpdate_payment_req_success"/> -->
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="csh501_payment_create" text="HLS.SAVE"/>
                <a:gridButton click="csh501_payment_save_submit" text="CSH501.CSH_REQ_POST"/>
                <a:gridButton id="csh501_payment_reset" click="csh501_payment_reset" text="HLS.RESET"/>
                <a:gridButton id="csh501_payment_back" click="csh501_payment_back" text="HLS.BACK"/>
                <a:gridButton click="csh501_payment_print" text="付款申请打印"/>
            </a:screenTopToolbar>
            <a:form title="CSH501.CSH_REQ_CREATE" width="1250">
                <a:box column="4" labelWidth="120">
                    <a:datePicker name="req_date" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_REQ_DATE" width="150"/>
                    <a:comboBox name="document_type_dis" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_REQ_TYPE" typeCase="upper" width="150"/>
                    <!-- <a:datePicker name="apply_pay_date" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_REQ_PAY_DATE" width="150"/> -->
                    <a:lov name="bp_code" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_PAYMENT_OBJ" width="150">
                        <a:events>
                            <a:event name="focus" handler="function(object){on_std_focus(object,&apos;BP&apos;)}"/>
                        </a:events>
                    </a:lov>
                    <a:textField name="bp_name" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_PAYMENT_OBJ_NAME" width="150"/>
                </a:box>
                <a:box column="4" labelWidth="120">
                    <a:numberField name="amount" bindTarget="csh_payment_req_create_ds" prompt="CSH501.CSH_APPLIED_TOTAL_AMOUNT" renderer="Leaf.formatMoney" typeCase="upper" width="150"/>
                    <a:textField name="currency_code" bindTarget="csh_payment_req_create_ds" prompt="HLS.CURRENCY" width="150"/>
                    <a:textField name="description" bindTarget="csh_payment_req_create_ds" prompt="HLS.COMMENT" width="428"/>
                </a:box>
            </a:form>
            <a:grid id="csh_payment_req_create_grid_ds" bindTarget="csh_payment_req_ln_ds" marginHeight="300" marginWidth="30" navBar="true">
                <a:columns>
                    <a:column name="contract_number" lock="true" prompt="HLS.CONTRACT_NUMBER" width="100"/>
                    <a:column name="contract_name" lock="true" prompt="HLS.CONTRACT_NAME" width="150"/>
                    <a:column name="bp_code" editor="create_grid_bp_lov" prompt="CSH501.CSH_PAYMENT_OBJ" renderer="render_req_create_grid"/>
                    <a:column name="bp_name" prompt="CSH501.CSH_PAYMENT_OBJ_NAME" width="150"/>
                    <a:column name="amount" align="right" editor="numberField" prompt="CSH501.CSH_REQ_AMOUNT" renderer="Leaf.formatMoney"/>
                    <a:column name="prepayment_trx" editor="csh501_tr_lov" prompt="预付款"/>
                    <a:column name="ddct_link" align="center" prompt="CSH710.DEDUCTION_AMOUNT" renderer="csh504_ddct_link"/>
                    <a:column name="sum_ddct_amount" align="right" prompt="抵扣总额" renderer="Leaf.formatMoney"/>
                    <a:column name="act_amount" align="right" prompt="实际申请金额" renderer="csh501_act_amount"/>
                    <a:column name="apply_pay_date" editor="datepicker" prompt="CSH501.CSH_REQ_PAY_DATE" renderer="Leaf.formatDate"/>
                    <a:column name="bp_bank_account_code" editor="csh501_tr_lov" prompt="CSH511.BP_BANK_ACCOUNT_CODE"/>
                    <a:column name="project_name" prompt="HLS.PROJECT_NAME" width="150"/>
                    <a:column name="bp_tenant_name" prompt="HLS.TENANT_NAME" width="150"/>
                    <a:column name="cf_description" prompt="CSH501.CSH_PAYMENT_PRJ"/>
                    <a:column name="due_amount" prompt="CSH501.CSH_PAYMENT_AMOUNT" renderer="Leaf.formatMoney"/>
                    <a:column name="currency_code" prompt="HLS.CURRENCY"/>
                    <a:column name="due_date" prompt="CSH501.CSH_DUE_DATE"/>
                    <a:column name="residual_amount" align="right" prompt="CSH501.CSH_RESIDUAL_AMOUNT" renderer="Leaf.formatMoney"/>
                    <a:column name="applied_pay_amount" align="right" prompt="CSH501.CSH_APPLIED_PAYMENT_AMOUNT" renderer="Leaf.formatMoney"/>
                </a:columns>
                <a:editors>
                    <a:datePicker id="datepicker"/>
                    <a:numberField id="numberField" allowFormat="true" allowNegative="false" decimalPrecision="2"/>
                    <a:lov id="create_grid_bp_lov">
                        <a:events>
                            <a:event name="focus" handler="function(object){on_std_focus(object,&apos;BP&apos;)}"/>
                        </a:events>
                    </a:lov>
                    <a:lov id="csh501_tr_lov">
                        <a:events>
                            <a:event name="focus" handler="csh501_trx_lov_fun"/>
                        </a:events>
                    </a:lov>
                    <a:lov id="csh501_loved"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
        <script type="text/javascript"><![CDATA[
                var ds = $('csh_payment_req_create_ds');
                var record = ds.getAt(0);
                record.set('req_date', new Date());
                record.set('document_type_dis', '${/model/payment_document_type/record/@description}');
                record.set('document_type', '${/model/payment_document_type/record/@document_type}');
            ]]></script>
    </a:view>
</a:screen>