<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: qianming  
    $Date: 2015-7-21 下午2:02:45  
    $Revision: 1.0  
    $Purpose: 
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure>
        <a:model-query model="basic.sys_session_info" rootPath="default_value_record"/>
    </a:init-procedure>
    <a:view>
        <a:link id="acp_invoice_save_selected_link_id" url="${/request/@context_path}/modules/csh/CSH501/acp_invoice_save_selected.lsc"/>
        <a:link id="svcLink_create_invoice" url="${/request/@context_path}/modules/csh/CSH501/acp_invoice_create.lsc"/>
        <script type="text/javascript"><![CDATA[
            function lock_window(){
                Leaf.Masker.mask($('acp_invoice_contract_v_win_id').wrap,'正在执行......');
            }
            function unlock_window(){
                Leaf.Masker.unmask($('acp_invoice_contract_v_win_id').wrap);
            }
        	function acp_invoice_contract_exit(){
        	    $('acp_invoice_contract_v_win_id').close();
        	}
        	function acp_invoice_contract_confirm(){
        	    var c_record = $('acp_invoice_contract_v_result_ds').getAt(0);
        	    var f_record = $('acp_invoice_contract_cf_v_result_ds').getAt(0);
        	    if($('acp_invoice_contract_v_result_ds').validate()){
        	        lock_window();
			        Leaf.request({
		                url: $('acp_invoice_save_selected_link_id').getUrl(),
		                para: {
		                    cashflow_id:f_record.get('cashflow_id'),
		                    billing_object_id:c_record.get('billing_object_id'),
		                    billing_object_name:c_record.get('billing_object_name'),
		                    invoice_title:c_record.get('invoice_title'),
		                    invoice_bp_address_phone_num:c_record.get('invoice_bp_address_phone_num'),
		                    invoice_bp_bank_account:c_record.get('invoice_bp_bank_account'),
		                    object_tax_registry_num:c_record.get('object_tax_registry_num'),
		                    description:c_record.get('description'),
		                    currency:f_record.get('currency'),
		                    exchange_rate_type:f_record.get('exchange_rate_type'),
		                    exchange_rate_quotation:f_record.get('exchange_rate_quotation'),
		                    exchange_rate:f_record.get('exchange_rate')
		                },
		                success: function(res) {
		                    var val;
		                    if(!!c_record.get('object_tax_registry_num')){
		                        val = c_record.get('object_tax_registry_num');
		                    }else{
		                        val = null;
		                    }
		                    $('selectedDs').query();
		                    $('acp_invoice_contract_create_btn_id').enable();
		                    $('acp_invoice_contract_confirm_btn_id').disable();
		                    document.getElementById('bill_div_id').style.display = '';
		                    unlock_window();
		                },
		                failure: function() {
		                    unlock_window();
		                },
		                error: function() {
		                    unlock_window();
		                },
		                scope: this
	            	});
			    }
			    
        	}
        	function acp_invoice_contract_create(){
        	    if(!$('headDs').validate(true)){
        	        return;
        	    }
			    var datas = $('headDs').getJsonData();
			    var recs = $('selectedDs').getAll();
			    for(var i = 0;i<recs.length;i++){
			        if(recs[i].get('invoice_kind')=='0' && Ext.isEmpty($('acp_invoice_contract_v_result_ds').getAt(0).get('object_tax_registry_num'))){
			           	Leaf.showMessage('提示','<font color="red">销方识别号</font>为空,无法开<font color="red">增值税专用发票</font>,请维护商业伙伴<font color="red">税务登记号</font>(即销方识别号)',null,350);
			           	return;
			        }
			    }
			    Leaf.showConfirm('${l:HLS.PROMPT}', '是否确认录入?',function(){
            	    lock_window();
            	    Leaf.request({
		                url: $('svcLink_create_invoice').getUrl(),
		                para: datas,
		                success: function(res) {
		                    unlock_window();
		                    acp_invoice_contract_exit();
		                },
		                failure: function() {
		                    unlock_window();
		                },
		                error: function() {
		                    unlock_window();
		                },
		                scope: this
		            });
            	});
        	}
        	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);
			}
			Leaf.onReady(function (){
			    $('acp_invoice_contract_create_btn_id').disable();
			});
		]]></script>
        <a:dataSets>
            <a:dataSet id="acp_invoice_contract_v_result_ds" autoCreate="true" autoQuery="true" model="csh.CSH501.acp_invoice_contract_v" queryUrl="${/request/@context_path}/autocrud/csh.CSH501.acp_invoice_contract_v/query?contract_id=${/parameter/@contract_id}">
                <a:fields>
                    <a:field name="billing_object_id"/>
                    <a:field name="invoice_title" prompt="开票抬头"/>
                    <a:field name="invoice_bp_address_phone_num" prompt="开票地址及电话"/>
                    <a:field name="invoice_bp_bank_account" prompt="开票银行及帐号"/>
                    <a:field name="object_taxpayer_type"/>
                    <a:field name="object_taxpayer_type_desc"/>
                    <a:field name="object_tax_registry_num"/>
                    <a:field name="billing_object_name" lovGridHeight="300" lovHeight="450" lovLabelWidth="100" lovService="csh.CSH501.acp_invoice_billing_object_lov?contract_id=${/parameter/@contract_id}" lovWidth="650" required="true" title="ACR510.ACR_INVOICE_CONTRACT_V.BILLING_OBJECT_NAME">
                        <a:mapping>
                            <a:map from="bp_id" to="billing_object_id"/>
                            <a:map from="bp_name" to="billing_object_name"/>
                            <a:map from="invoice_title" to="invoice_title"/>
                            <a:map from="invoice_bp_address_phone_num" to="invoice_bp_address_phone_num"/>
                            <a:map from="invoice_bp_bank_account" to="invoice_bp_bank_account"/>
                            <a:map from="taxpayer_type" to="object_taxpayer_type"/>
                            <a:map from="taxpayer_type_desc" to="object_taxpayer_type_desc"/>
                            <a:map from="tax_registry_num" to="object_tax_registry_num"/>
                        </a:mapping>
                    </a:field>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="acp_invoice_contract_cf_v_result_ds" autoCreate="true" autoQuery="true" model="csh.CSH501.acp_invoice_contract_cf_v" queryUrl="${/request/@context_path}/autocrud/csh.CSH501.acp_invoice_contract_cf_v/query?cashflow_id=${/parameter/@cashflow_id}"/>
            <a:dataSet id="invoiceKindDs" lookupCode="ACR510_INVOICE_KIND"/>
            <a:dataSet id="headDs" autoCreate="true">
                <a:fields>
                    <a:field name="group_billing_method" defaultValue="GROUP_BY_TIMES"/>
                    <a:field name="accounting_date" defaultValue="${/model/default_value_record/record/@today}" required="true"/>
                    <a:field name="invoice_date" defaultValue="${/model/default_value_record/record/@today}" required="true"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="selectedDs" bindName="line_info" bindTarget="headDs" fetchAll="true" model="acp.ACP510.acp_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: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:screenTitle/>
                <a:gridButton click="acp_invoice_contract_exit" text="HLS.EXIT"/>
                <a:gridButton id="acp_invoice_contract_confirm_btn_id" click="acp_invoice_contract_confirm" text="ACR510.BUTTON.COMFIRM"/>
                <a:gridButton id="acp_invoice_contract_create_btn_id" click="acp_invoice_contract_create" text="录入"/>
            </a:screenTopToolbar>
            <a:form column="4" labelWidth="110" marginWidth="35" title="基本信息">
                <a:textField name="contract_number" bindTarget="acp_invoice_contract_v_result_ds" prompt="合同编号" readOnly="true"/>
                <a:textField name="contract_name" bindTarget="acp_invoice_contract_v_result_ds" prompt="合同名称" readOnly="true"/>
                <a:textField name="pin_billing_object_name" bindTarget="acp_invoice_contract_v_result_ds" prompt="购方对象名称" readOnly="true"/>
                <a:textField name="pin_object_tax_registry_num" bindTarget="acp_invoice_contract_v_result_ds" prompt="购方识别号" readOnly="true"/>
                <a:textField name="contract_status_desc" bindTarget="acp_invoice_contract_v_result_ds" prompt="合同状态" readOnly="true"/>
                <a:textField name="object_tax_registry_num" bindTarget="acp_invoice_contract_v_result_ds" prompt="销方识别号" readOnly="true"/>
                <a:textField name="invoice_bp_address_phone_num" bindTarget="acp_invoice_contract_v_result_ds" prompt="开票地址及电话" readOnly="true"/>
                <a:textField name="invoice_bp_bank_account" bindTarget="acp_invoice_contract_v_result_ds" prompt="开票银行及帐号" readOnly="true"/>
                <a:lov name="billing_object_name" bindTarget="acp_invoice_contract_v_result_ds" prompt="销方对象名称"/>
                <a:textField name="description" bindTarget="acp_invoice_contract_v_result_ds" prompt="备注"/>
                <a:datePicker name="invoice_date" bindTarget="headDs" prompt="发票日期"/>
                <a:datePicker name="accounting_date" bindTarget="headDs" prompt="记账日期"/>
            </a:form>
            <a:form column="4" labelWidth="110" marginWidth="35" title="现金流信息">
                <a:textField name="times" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="应付期数" readOnly="true"/>
                <a:textField name="cf_item_desc" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="应付项目" readOnly="true"/>
                <a:datePicker name="due_date" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="应付日期" readOnly="true"/>
                <a:numberField name="due_amount" align="right" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="应付金额" readOnly="true" renderer="Leaf.formatMoney"/>
                <a:numberField name="received_amount" align="right" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="已付金额" readOnly="true" renderer="Leaf.formatMoney"/>
                <a:numberField name="billing_amount" align="right" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="已开票金额" readOnly="true" renderer="Leaf.formatMoney"/>
                <a:textField name="currency_desc" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="币种" readOnly="true"/>
                <a:textField name="exchange_rate" bindTarget="acp_invoice_contract_cf_v_result_ds" prompt="汇率" readOnly="true"/>
            </a:form>
            <div id="bill_div_id" style="display:none">
                <a:tabPanel marginHeight="330" marginWidth="35">
                    <a:tabs>
                        <a:tab prompt="ACR510.TAB.BILLING_INFO" width="100">
                            <a:grid id="selectedDs_grid" bindTarget="selectedDs" marginHeight="360" marginWidth="60" navBar="true">
                                <a:columns>
                                    <a:column name="contract_number" prompt="合同编号" width="150"/>
                                    <a:column name="times" align="right" prompt="期数" width="40"/>
                                    <a:column name="cf_item_desc" prompt="应付项目"/>
                                    <a:column name="product_name" editor="selectedDs_grid_editor_tf" prompt="货物及应税劳务名称" width="150"/>
                                    <a:column name="invoice_kind_desc" editor="selectedDs_grid_editor_comb" prompt="发票类型"/>
                                    <a:column name="currency_desc" align="center" prompt="币种"/>
                                    <a:column name="due_amount" align="right" prompt="应付金额" renderer="Leaf.formatMoney"/>
                                    <a:column name="received_amount" align="right" prompt="已付金额" renderer="Leaf.formatMoney"/>
                                    <a:column name="cf_billing_amount" align="right" prompt="应开票金额" renderer="Leaf.formatMoney"/>
                                    <a:column name="billing_amount" align="right" editor="selectedDs_grid_editor_nf" prompt="本次开票金额" renderer="Leaf.formatMoney"/>
                                </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="360" marginWidth="60" navBar="true">
                                <a:columns>
                                    <a:column name="contract_number" prompt="合同编号" width="150"/>
                                    <a:column name="times" align="right" prompt="期数" width="40"/>
                                    <a:column name="cf_item_desc" prompt="应付项目"/>
                                    <a:column name="contract_name" prompt="合同名称" width="200"/>
                                    <a:column name="project_number" prompt="项目编号" width="150"/>
                                    <a:column name="project_name" prompt="项目名称" width="200"/>
                                    <a:column name="last_received_date" prompt="收款日期" renderer="Leaf.formatDate" width="80"/>
                                    <a:column name="exchange_rate" align="right" prompt="汇率"/>
                                    <a:column name="exchange_rate_type_desc" prompt="汇率类型"/>
                                </a:columns>
                            </a:grid>
                        </a:tab>
                    </a:tabs>
                </a:tabPanel>
            </div>
        </a:screenBody>
    </a:view>
</a:screen>