<?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_penalty_billing_method" rootPath="group_billing_method_list"/>
		<a:model-query model="basic.sys_invoice_kind_type_v" rootPath="acr512_invoice_kind_type"/>
    </a:init-procedure>
    <a:view>
        <a:link id="svcLink_create_invoice" url="${/request/@context_path}/modules/acr/ACR510/acr_invoice_penalty_create.lsc"/>
        <a:screen-include screen="modules/hls/hls_common_javascript.lview"/>
        <script><![CDATA[
        function createScreen_close()
        {
            var win = $('acr_invoice_penalty_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()
        {
            var win = $('acr_invoice_create_invoice');
            var ds=$('headDs');
            var message_flag =false;
            debugger
            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 received_amount=selectedDs[i].get('received_amount');
                var cf_billing_amount=selectedDs[i].get('cf_billing_amount')||0;
                var temp_billing_amount=minus(received_amount,cf_billing_amount);


                // if(billing_amount!=due_amount &&billing_amount!=principal &&billing_amount!=interest){
                //    $L.showErrorMessage("提示","本次开票金额需与应收金额,应收本金,应收利息中的某一金额一致!");
                //    return;
                // }
                if(billing_amount>temp_billing_amount){
                    $L.showErrorMessage("提示","开票金额填写有误!");
                    return;
                }
                //add by gzj 售后回租只能开普票
                var business_type= selectedDs[i].get('business_type');
                var kind_type=selectedDs[i].get('invoice_kind_type');
                if(business_type=='LEASEBACK'&&(kind_type!='PAPER_GENERAL'&&kind_type!='ELECTRONIC_GENERAL'&&kind_type!='RECEIPT')){
                    Leaf.showMessage("提示","售后回租只能开普通发票!");
                    return;
                }
                if(business_type=='LEASEBACK'&&(kind_type!='ELECTRONIC_GENERAL'&&kind_type!='RECEIPT')){
                    message_flag =true;
                    // Leaf.showMessage("提示","回租建议选择增值税电子普票!");
                }



            }

            var datas = ds.getJsonData();
            var message_info;
            if(message_flag==false){
                message_info= '是否确认开票?';
            }else {
                message_info= "回租建议选择增值税电子普票!是否确认开票?";
            }
            Leaf.showConfirm('${l:HLS.PROMPT}',message_info,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','保证金');
                }
            }
        }
		window['acr510_get_object_for_invoice'] = function(object) {
            debugger;
            var record = object.record;
            var name = object.binder.name;
            var url = '${/request/@context_path}/autocrud/basic.sys_invoice_kind_type_v/query';
            if(name=='invoice_kind_type_n'){
                var combobox_ds =$('invoiceKindDs');
                combobox_ds.setQueryUrl(url);
                combobox_ds.setQueryParameter('record_id', record.get('record_id'));
                combobox_ds.query();
            }


        };
        ]]></script>
        <a:dataSets>
             <!--<a:dataSet id="invoiceKindDs" lookupCode="ACR510_INVOICE_KIND"/>-->
            <a:dataSet id="invoiceKindDs">
                <a:datas dataSource="/model/acr512_invoice_kind_type"/>
            </a:dataSet>
            <a:dataSet id="headDs" autoCreate="true">
                <a:fields>
                    <a:field name="company_id" defaultValue="${/parameter/@company_id}"/>
                    <a:field name="group_billing_method" defaultValue="NO_GROUP"/>
                    <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_penalty_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="invoice_kind_type"/>
                    <a:field name="invoice_kind_type_n" displayField="value_name" options="invoiceKindDs" required="true" returnField="invoice_kind_type" valueField="value_code"/>
                    <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="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="invoice_kind_type_n" editorFunction="" editor="selectedDs_grid_editor_comb" width="150"/>
                                <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="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:column name="full_elec_invoice_flag_n" prompt="是否全电发票" align="center"/>
							</a:columns>
                            <a:editors>
                                <a:comboBox id="selectedDs_grid_editor_comb">
                                    <a:events>
                                        <a:event name="focus" handler="window[&apos;acr510_get_object_for_invoice&apos;]"/>
                                    </a:events>
                                </a:comboBox>
                                <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="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="transaction_date" renderer="Leaf.formatDate" width="80" prompt="收款日期"/>
                             <!--   <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>