<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: wangwei  
    $Date: 2015-12-25 下午2:53:29  
    $Revision: 1.0  
    $Purpose:待开票行维护
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application">
    <a:init-procedure/>
    <a:view>
        <script src="${/request/@context_path}/javascripts/calculate.js" type="text/javascript"/>
        <script type="text/javascript"><![CDATA[
            function acr_invoice_line_close() {
                $('${/parameter/@winid}').close();
            }
            
            function percentRenders(value, record, name) {
                if (!Ext.isEmpty(value)) {
                    return Leaf.formatMoney(mul(100, value)) + '%';
                }
            }
            
            function beforesumit_check(ds) {
                var head_record = $('sdic_invoice_header_info_ds').getAt(0);
                var head_amount = head_record.get('due_amount');
                var line_sum_amount = 0;
                if (ds.validate()) {
                    var records = ds.getAll();
                    for (i = 0;i < records.length;i++) {
                        if (head_record.get('vat_type') == 'VALUE_ADDED_TAX') {
                            line_sum_amount = line_sum_amount + records[i].get('invoice_amount');
                            if (records[i].get('invoice_amount') != plus(records[i].get('net_invoice_amount'), records[i].get('vat_invoice_amount'))) {
            
                                Leaf.showMessage('提示', '第' + (i + 1) + '行价税合计校验出错（增值税）！');
                                return false;
                            }
                        } else {
                            if (records[i].get('invoice_amount') != records[i].get('net_invoice_amount')) {
                                Leaf.showMessage('提示', '第' + (i + 1) + '行价税合计校验出错（营业税）！');
                                return false;
                            }
                        }
                    }
                    if (head_amount < line_sum_amount) {
                        Leaf.showMessage('提示', '开票价税合计总金额不能大于应开发票价税合计金额！');
                        return false;
                    }
                    return true;
                } else {
                    return false;
                }
            }
            
            
            function invoice_sum_foot_render(data, name) {
                if (name == 'invoice_date') {
                    return '<font color=red>' + '合计:' + '</font>';
                } else if (name == 'vat_invoice_amount') {
                    return invoice_caculateTotal(data, name);
                } else if (name == 'invoice_amount') {
                    return invoice_caculateTotal(data, name);
                } else if (name == "net_invoice_amount") {
                    return invoice_caculateTotal(data, name);
                }
            }
            
            function invoice_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 update_invoice_status(dataSet, record, name, value, oldValue) {
                if (name == 'net_invoice_amount' || name == 'vat_invoice_rate') {
                    if (record.get('net_invoice_amount') && record.get('vat_invoice_rate')) {
                        var head_record = $('sdic_invoice_header_info_ds').getAt(0);
                        //保留两位
                        var vat_invocie_amount = div(Math.round(mul(record.get('net_invoice_amount'), record.get('vat_invoice_rate')) * 100, 2), 100);
                        record.set('vat_invoice_amount', vat_invocie_amount);
                        if (head_record.get('vat_type') == 'VALUE_ADDED_TAX') {
                            //增值税
                            record.set('invoice_amount', plus(record.get('net_invoice_amount'), vat_invocie_amount));
                        } else {
                            //营业税
                            record.set('invoice_amount', record.get('net_invoice_amount'));
                        }
                    }
                }
            }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="sdic_invoice_header_info_ds" loadData="true" model="sdic.SDIC001.sdic_acr_invoice" queryUrl="${/request/@context_path}/autocrud/sdic.SDIC001.sdic_acr_invoice/query?invoice_id=${/parameter/@invoice_id}">
                <a:fields>
                    <a:field name="document_number" readOnly="true"/>
                    <a:field name="contract_number" readOnly="true"/>
                    <a:field name="bp_name" readOnly="true"/>
                    <a:field name="cf_item_desc" readOnly="true"/>
                    <a:field name="times" readOnly="true"/>
                    <a:field name="vat_type_desc" readOnly="true"/>
                    <a:field name="net_due_amount" readOnly="true"/>
                    <a:field name="vat_due_amount" readOnly="true"/>
                    <a:field name="due_amount" readOnly="true"/>
                    <a:field name="invoice_bp_address_phone_num" readOnly="true"/>
                </a:fields>
            </a:dataSet>
            <a:dataSet id="sdic_acr_invoice_line_ds" autoQuery="true" model="sdic.SDIC001.sdic_acr_invoice_line" pageSize="20" queryUrl="${/request/@context_path}/autocrud/sdic.SDIC001.sdic_acr_invoice_line/query?invoice_id=${/parameter/@invoice_id}" selectable="true">
                <a:fields>
                    <a:field name="invoice_id" defaultValue="${/parameter/@invoice_id}" readOnly="true"/>
                    <a:field name="invoice_date" required="true"/>
                    <a:field name="invoice_number" required="true"/>
                    <a:field name="invoice_code" required="true"/>
                    <a:field name="net_invoice_amount" required="true"/>
                    <a:field name="vat_invoice_amount" readOnly="true"/>
                    <a:field name="vat_invoice_rate" required="true"/>
                    <a:field name="invoice_amount" readOnly="true"/>
                    <a:field name="cf_item_desc" required="true"/>
                </a:fields>
                <a:events>
                    <a:event name="beforesubmit" handler="beforesumit_check"/>
                    <a:event name="update" handler="update_invoice_status"/>
                </a:events>
            </a:dataSet>
        </a:dataSets>
        <a:screenBody>
            <a:screenTopToolbar>
                <a:screenTitle/>
                <a:gridButton click="acr_invoice_line_close" text="关闭"/>
            </a:screenTopToolbar>
            <a:form column="4" title="发票信息" width="1000">
                <a:textField name="document_number" bindTarget="sdic_invoice_header_info_ds" prompt="单据编号"/>
                <a:textField name="contract_number" bindTarget="sdic_invoice_header_info_ds" prompt="合同编号"/>
                <a:textField name="bp_name" bindTarget="sdic_invoice_header_info_ds" prompt="购货单位" span="2"/>
                <a:textField name="cf_item_desc" bindTarget="sdic_invoice_header_info_ds" prompt="货物或应税劳务名称"/>
                <a:textField name="times" bindTarget="sdic_invoice_header_info_ds" prompt="期数"/>
                <a:textField name="vat_type_desc" bindTarget="sdic_invoice_header_info_ds" prompt="税种"/>
                <a:numberField name="net_due_amount" allowDecimals="true" allowFormat="true" bindTarget="sdic_invoice_header_info_ds" prompt="金额"/>
                <a:numberField name="vat_due_amount" bindTarget="sdic_invoice_header_info_ds" prompt="税额"/>
                <a:textField name="due_amount" bindTarget="sdic_invoice_header_info_ds" prompt="价税合计"/>
                <a:textField name="invoice_bp_address_phone_num" bindTarget="sdic_invoice_header_info_ds" prompt="地址"/>
            </a:form>
            <a:grid bindTarget="sdic_acr_invoice_line_ds" height="250" navBar="true" width="1000">
                <a:toolBar>
                    <a:button type="add"/>
                    <a:button type="delete"/>
                    <a:button type="save"/>
                </a:toolBar>
                <a:columns>
                    <a:column name="invoice_date" align="center" editor="date_e" footerRenderer="invoice_sum_foot_render" prompt="开票日期" renderer="Leaf.formatDate"/>
                    <a:column name="invoice_code" editor="text_e" prompt="发票代码" width="140"/>
                    <a:column name="invoice_number" editor="text_e" prompt="发票号" width="140"/>
                    <a:column name="cf_item_desc" editor="text_e" prompt="货物或应税劳务名称" width="150"/>
                    <a:column name="net_invoice_amount" align="right" editor="number_e" footerRenderer="invoice_sum_foot_render" prompt="开票金额" width="130"/>
                    <a:column name="vat_invoice_rate" align="right" editor="percent_e" prompt="开票税率" renderer="percentRenders" width="130"/>
                    <a:column name="vat_invoice_amount" align="right" editor="number_e" footerRenderer="invoice_sum_foot_render" prompt="开票税额" width="130"/>
                    <a:column name="invoice_amount" align="right" editor="number_e" footerRenderer="invoice_sum_foot_render" prompt="价税合计" width="130"/>
                </a:columns>
                <a:editors>
                    <a:datePicker id="date_e"/>
                    <a:textField id="text_e"/>
                    <a:numberField id="number_e" allowDecimals="true" allowFormat="true" decimalPrecision="2"/>
                    <a:percentField id="percent_e"/>
                </a:editors>
            </a:grid>
        </a:screenBody>
    </a:view>
</a:screen>
