<?xml version="1.0" encoding="UTF-8"?>
<!--
	&author:DJ
	$date:2013/05/08
	$purpose:发票创建
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <a:model-query model="basic.sys_session_info" rootPath="default_value_record"/>
        <a:model-query model="acr.ACR510.acr_invoice_group_billing_method" rootPath="group_billing_method_list"/>
    </a:init-procedure>
    <a:view>
        <a:link id="svcLink_create_invoice" url="${/request/@context_path}/modules/acr/ACR510/acr_invoice_create.lsc"/>
        <a:screen-include screen="modules/hls/hls_common_javascript.lview"/>
        <script><![CDATA[
        function createScreen_close()
        {
            var win = $('acr_invoice_create_invoice');
            win.close();
        }
        function colum_caculateTotal(data, name) {
            var total = 0;
            var length = data.length;
            var value = 0;
            for (var i = 0;i < length;i++) {
                if (Ext.isEmpty(data[i].get(name))) {
                    value = 0;
                } else {
                    value = data[i].get(name);
                }
                total = plus(total, value);
            }
            return '<font color=red>' + Leaf.formatMoney(total) + '</font>';
        }

        function sum_foot(data, name) {
            if (name == 'billing_amount') {
                return colum_caculateTotal(data, name);
            }

        }

        function createScreen_create()
        {
            debugger;
            var win = $('acr_invoice_create_invoice');
            var ds=$('headDs');
            if(!ds.validate(true))
            {
                Leaf.Masker.unmask(Ext.getBody());
                return;
            }
            //判断本次开票金额是否为租金,本金,和利息之中某一个
            var selectedDs=$('selectedDs').getAll();
            for(var i=0;i<selectedDs.length;i++){
                var billing_amount=selectedDs[i].get('billing_amount');
                var due_amount=selectedDs[i].get('due_amount');
                var principal=selectedDs[i].get('principal');
                var interest=selectedDs[i].get('interest');
                var cf_billing_amount=selectedDs[i].get('cf_billing_amount');
                var received_amount=selectedDs[i].get('received_amount');


                // if(billing_amount!=due_amount &&billing_amount!=principal &&billing_amount!=interest){
                //    $L.showErrorMessage("提示","本次开票金额需与应收金额,应收本金,应收利息中的某一金额一致!");
                //    return;
                // }
                if(billing_amount==cf_billing_amount){
                    $L.showErrorMessage("提示","该金额已经开票过!");
                    return;
                }

                if(received_amount<plus(cf_billing_amount,billing_amount)&&selectedDs[i].get('cf_item')==1&&selectedDs[i].get('cf_status')=='BLOCK'&&selectedDs[i].get('contract_status')=='REPURING'){
                    $L.showErrorMessage("提示","本次开票金额+已开票金额不能超过已核销金额!");
                    return;
                }



            }

            var datas = ds.getJsonData();
            Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认开票?',function()
            {
                Leaf.Masker.mask(Ext.getBody(), '${l:HLS.EXECUTING}');
                Leaf.request({
                    url: $('svcLink_create_invoice').getUrl(),
                    para: datas,
                    success: function(res) {
                        Leaf.Masker.unmask(Ext.getBody());
                        createScreen_close();
                        contractDs_grid_query();
                    },
                    failure: function() {
                        Leaf.Masker.unmask(Ext.getBody());

                    },
                    error: function() {
                        Leaf.Masker.unmask(Ext.getBody());

                    },
                    scope: this
                });
            },function (){
                Leaf.Masker.unmask(Ext.getBody());

            });

        }

        function unSelect_selectedDs(ds,record){
            record.dirty = false;
        }

        function onSelect_selectedDs(ds,record){
            record.dirty = true;
        }

        function onLoad_selectedDs(ds)
        {
            ds.selectAll.defer(5,ds);
            var records=ds.getAll();
            for (var i=0;i<records.length;i++) {
                if (records[i].get('invoice_kind') && records[i].get('invoice_kind')=='RECEIPT' && records[i].get('cf_item') && records[i].get('cf_item')=='51') {
                    records[i].set('product_name','保证金');
                }
            }
        }
        ]]></script>
        <style><![CDATA[
        .item-radio-option {
            margin-right: 50px;
        }
        ]]></style>
        <a:dataSets>
            <a:dataSet id="invoiceKindDs" lookupCode="ACR510_INVOICE_KIND"/>
            <a:dataSet id="headDs" autoCreate="true">
                <a:fields>
                    <a:field name="company_id" defaultValue="${/parameter/@company_id}"/>
                    <a:field name="group_billing_method" defaultValue="GROUP_BY_TIMES"/>
                    <a:field name="accounting_date" defaultValue="${/model/default_value_record/record/@today}"/>
                    <a:field name="invoice_date" defaultValue="${/model/default_value_record/record/@today}" required="true"/>
                </a:fields>
            </a:dataSet>

            <a:dataSet id="selectedDs" autoQuery="true" bindName="line_info" bindTarget="headDs" fetchAll="true" model="acr.ACR510.acr_invoice_create" selectable="true">
                <a:fields>
                    <a:field name="billing_amount" required="true"/>
                    <a:field name="product_name" required="true"/>
                    <a:field name="invoice_kind"/>
                    <a:field name="invoice_kind_desc" displayField="code_value_name" options="invoiceKindDs" required="true" returnField="invoice_kind" valueField="code_value"/>
                    <a:field name="billing_object"/>
                    <a:field name="billing_object_name" prompt="开票对象名称"/>

                </a:fields>
                <a:events>
                    <a:event name="load" handler="onLoad_selectedDs"/>
                    <a:event name="select" handler="onSelect_selectedDs"/>
                    <a:event name="unselect" handler="unSelect_selectedDs"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:gridButton click="createScreen_close" text="HLS.CLOSE"/>
                <a:gridButton click="createScreen_create" text="HLS.CREATE"/>
            </a:screenTopToolbar>
            <a:form column="3" labelWidth="120">
                <a:datePicker name="invoice_date" bindTarget="headDs" prompt="开票申请日期"/>
                <a:datePicker name="accounting_date" bindTarget="headDs" prompt="HLS.ACCOUNT_DATE"/>
            </a:form>
            <a:tabPanel marginHeight="220" marginWidth="30">
                <a:tabs>
                    <a:tab prompt="ACR510.TAB.BILLING_INFO" width="100">
                        <a:grid id="selectedDs_grid" bindTarget="selectedDs" marginHeight="270" marginWidth="60">
                            <a:columns>
                                <a:column name="contract_number" width="150"/>
                                <a:column name="times" align="right" width="40"/>
                                <a:column name="cf_item_desc"/>
                                <a:column name="product_name" editor="selectedDs_grid_editor_tf" width="150"/>
                                <a:column name="invoice_kind_desc" editor="selectedDs_grid_editor_comb"/>
                                <a:column name="billing_object_name" width="150"/>

                                <a:column name="currency_desc" align="center"/>
                                <a:column name="due_amount" align="right" renderer="Leaf.formatMoney"/>
                                <a:column name="principal" align="right" renderer="Leaf.formatMoney"/>
                                <a:column name="interest" align="right" renderer="Leaf.formatMoney"/>
                                <a:column name="received_amount" align="right" renderer="Leaf.formatMoney"/>
                                <a:column name="cf_billing_amount" align="right" renderer="Leaf.formatMoney"/>
                                <a:column name="billing_amount" align="right" editor="selectedDs_grid_editor_nf" renderer="Leaf.formatMoney" footerRenderer="sum_foot"/>
                            </a:columns>
                            <a:editors>
                                <a:comboBox id="selectedDs_grid_editor_comb"/>
                                <a:textField id="selectedDs_grid_editor_tf"/>
                                <a:numberField id="selectedDs_grid_editor_nf"/>
                            </a:editors>
                        </a:grid>
                    </a:tab>
                    <a:tab prompt="ACR510.TAB.ASSIST_INFO" width="100">
                        <a:grid id="selectedDs_grid_2" bindTarget="selectedDs" marginHeight="270" marginWidth="60">
                            <a:columns>
                                <a:column name="contract_number" width="150"/>
                                <a:column name="times" align="right" width="40"/>
                                <a:column name="cf_item_desc"/>
                                <a:column name="contract_name" width="200"/>
                                <a:column name="project_number" width="150" prompt="报单编号"/>
<!--                                <a:column name="project_name" width="200" prompt="报单名称"/>-->
                                <a:column name="last_received_date" renderer="Leaf.formatDate" width="80"/>
                             <!--   <a:column name="exchange_rate" align="right"/>
                                <a:column name="exchange_rate_type_desc"/>-->
                            </a:columns>
                        </a:grid>
                    </a:tab>
                </a:tabs>
            </a:tabPanel>
            <a:fieldSet title="ACR.GROUP_BILLING_METHOD">
                <a:radio name="group_billing_method" bindTarget="headDs" labelField="code_value_name" options="/model/group_billing_method_list" prompt="" radioSeparator="" valueField="code_value" width="600"/>
            </a:fieldSet>
        </a:screenBody>
    </a:view>
</a:screen>