odoo 10 Addon视图未被使用

时间:2018-01-23 20:42:26

标签: python odoo-10 odoo

我已经创建了一个新模块以扩展sale.advance.payment.inv。问题是我的模块中的表单没有被使用,它正在使用默认表单(并且我的模块中定义的新值也没有以旧表单加载...)

我正在使用这些文件:

__清单__吡啶

# -*- coding: utf-8 -*-
{
    'name': "invoice_sales_order",
    'summary': """Allows invoice percentage for each line in sales order""",
    'description': """
        Allows invoice percentage for each line in sales order
    """,
    'author': "Miquel",
    'website': "http://example.com",
    'category': 'Uncategorized',
    'version': '0.5',
    'depends': ['base', 'account', 'Sales'],
    'data': [
        'views/views.xml',
    ],
    'demo': [
        'demo/demo.xml',
    ],
    'installable': True,
}

控制器/ controllers.py

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import time

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError


class SaleAdvancePaymentInv(models.TransientModel):
    _inherit = "sale.advance.payment.inv"

    @api.model
    def _count(self):
        return len(self._context.get('active_ids', []))

    @api.model
    def _get_advance_payment_method(self):
        if self._count() == 1:
            sale_obj = self.env['sale.order']
            order = sale_obj.browse(self._context.get('active_ids'))[0]
            if all([line.product_id.invoice_policy == 'order' for line in order.order_line]) or order.invoice_count:
                return 'all'
        return 'delivered'

    advance_payment_method = fields.Selection([
        ('delivered', 'Invoiceable lines'),
        ('all', 'Invoiceable lines (deduct down payments and quantity percentage)'),
        ('soprecentage', 'Quantity percentage line by line'),
        ('percentage', 'Down payment (percentage)'),
        ('fixed', 'Down payment (fixed amount)')
        ], string='What do you want to invoice?', default=_get_advance_payment_method, required=True)

    @api.onchange('advance_payment_method')
    def onchange_advance_payment_method(self):
        if self.advance_payment_method == 'percentage' or self.advance_payment_method == 'soprecentage':
            return {'value': {'amount': 0}}
        return {}

    @api.multi
    def _create_invoice(self, order, so_line, amount):
        inv_obj = self.env['account.invoice']
        ir_property_obj = self.env['ir.property']

        account_id = False
        if self.product_id.id:
            account_id = self.product_id.property_account_income_id.id
        if not account_id:
            inc_acc = ir_property_obj.get('property_account_income_categ_id', 'product.category')
            account_id = order.fiscal_position_id.map_account(inc_acc).id if inc_acc else False
        if not account_id:
            raise UserError(
                _('There is no income account defined for this product: "%s". You may have to install a chart of account from Accounting app, settings menu.') %
                (self.product_id.name,))

        if self.amount <= 0.00:
            raise UserError(_('The value of the down payment amount must be positive.'))
        context = {'lang': order.partner_id.lang}
        if self.advance_payment_method == 'percentage' or self.advance_payment_method == 'soprecentage':
            amount = order.amount_untaxed * self.amount / 100
            name = _("Down payment of %s%%") % (self.amount,)
        else:
            amount = self.amount
            name = _('Down Payment')
        del context
        taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
        if order.fiscal_position_id and taxes:
            tax_ids = order.fiscal_position_id.map_tax(taxes).ids
        else:
            tax_ids = taxes.ids

        invoice_line_ids = []
        sale_line_obj = self.env['sale.order.line']
        for line in sale_line_obj:
            if not line.is_downpayment:
                invoice_line_ids.append((0, 0, {
                    'name': line.name,
                    'origin': order.name,
                    'account_id': account_id,
                    'price_unit': line.price_unit,
                    'quantity': line.product_uom_qty * self.amount / 100,
                    'discount': line.discount,
                    'uom_id': line.product_uom,
                    'product_id': line.product_id,
                    'sale_line_ids': [(6, 0, [line.id])],
                    'invoice_line_tax_ids': line.tax_id,
                    'account_analytic_id': order.analytic_account_id.id or False,
                }))

        invoice = inv_obj.create({
            'name': order.client_order_ref or order.name,
            'origin': order.name,
            'type': 'out_invoice',
            'reference': False,
            'account_id': order.partner_id.property_account_receivable_id.id,
            'partner_id': order.partner_invoice_id.id,
            'partner_shipping_id': order.partner_shipping_id.id,
            'invoice_line_ids': invoice_line_ids,
            'currency_id': order.pricelist_id.currency_id.id,
            'payment_term_id': order.payment_term_id.id,
            'fiscal_position_id': order.fiscal_position_id.id or order.partner_id.property_account_position_id.id,
            'team_id': order.team_id.id,
            'user_id': order.user_id.id,
            'comment': order.note,
        })
        invoice.compute_taxes()
        invoice.message_post_with_view('mail.message_origin_link',
                    values={'self': invoice, 'origin': order},
                    subtype_id=self.env.ref('mail.mt_note').id)
        return invoice

    @api.multi
    def create_invoices(self):
        sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))

        if self.advance_payment_method == 'delivered':
            sale_orders.action_invoice_create()
        elif self.advance_payment_method == 'all':
            sale_orders.action_invoice_create(final=True)
        else:
            # Create deposit product if necessary
            if not self.product_id:
                vals = self._prepare_deposit_product()
                self.product_id = self.env['product.product'].create(vals)
                self.env['ir.config_parameter'].sudo().set_param('sale.default_deposit_product_id', self.product_id.id)

            sale_line_obj = self.env['sale.order.line']
            for order in sale_orders:
                if self.advance_payment_method == 'percentage' or self.advance_payment_method == 'soprecentage':
                    amount = order.amount_untaxed * self.amount / 100
                else:
                    amount = self.amount
                if self.product_id.invoice_policy != 'order':
                    raise UserError(_('The product used to invoice a down payment should have an invoice policy set to "Ordered quantities". Please update your deposit product to be able to create a deposit invoice.'))
                if self.product_id.type != 'service':
                    raise UserError(_("The product used to invoice a down payment should be of type 'Service'. Please use another product or update this product."))
                taxes = self.product_id.taxes_id.filtered(lambda r: not order.company_id or r.company_id == order.company_id)
                if order.fiscal_position_id and taxes:
                    tax_ids = order.fiscal_position_id.map_tax(taxes).ids
                else:
                    tax_ids = taxes.ids
                context = {'lang': order.partner_id.lang}
                so_line = sale_line_obj.create({
                    'name': _('Advance: %s') % (time.strftime('%m %Y'),),
                    'price_unit': amount,
                    'product_uom_qty': 0.0,
                    'order_id': order.id,
                    'discount': 0.0,
                    'product_uom': self.product_id.uom_id.id,
                    'product_id': self.product_id.id,
                    'tax_id': [(6, 0, tax_ids)],
                    'is_downpayment': True,
                })
                del context
                self._create_invoice(order, so_line, amount)
        if self._context.get('open_invoices', False):
            return sale_orders.action_view_invoice()
        return {'type': 'ir.actions.act_window_close'}

视图/ views.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
        <record id="view_sale_advance_payment_inv" model="ir.ui.view">
            <field name="name">Invoice Orders</field>
            <field name="model">sale.advance.payment.inv</field>
            <field name="arch" type="xml">
                <form string="Invoice Sales Order">
                    <p class="oe_grey">
                        Invoices will be created in draft so that you can review
                        them before validation.
                    </p>
                    <p class="oe_grey">
                        Invoices will be created in draft so that you can review
                        them before validation.
                    </p>
                    <group>
                        <field name="count" invisible="[('count','=',1)]" readonly="True"/>
                        <field name="advance_payment_method" class="oe_inline" widget="radio"
                            attrs="{'invisible': [('count','&gt;',1)]}"/>
                        <field name="product_id"
                            context="{'search_default_services': 1, 'default_type': 'service', 'default_invoice_policy': 'order'}" class="oe_inline"
                            attrs="{'invisible': 1}"/>
                        <label for="amount" attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}"/>
                        <div attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}">
                            <field name="amount"
                                attrs="{'required': [('advance_payment_method', 'in', ('fixed','percentage'))]}" class="oe_inline" widget="monetary"/>
                            <label string="%%"
                                attrs="{'invisible': [('advance_payment_method', '!=', 'percentage')]}" class="oe_inline"/>
                        </div>
                        <field name="deposit_account_id" class="oe_inline"
                            attrs="{'invisible': ['|', ('advance_payment_method', 'not in', ('fixed', 'percentage')), ('product_id', '!=', False)]}" groups="account.group_account_manager"/>
                        <field name="deposit_taxes_id" class="oe_inline" widget="many2many_tags"
                            domain="[('type_tax_use','=','sale')]"
                            attrs="{'invisible': ['|', ('advance_payment_method', 'not in', ('fixed', 'percentage')), ('product_id', '!=', False)]}"/>
                    </group>
                    <footer>
                        <button name="create_invoices" string="Create and View Invoices" type="object"
                            context="{'open_invoices': True}" class="btn-primary"/>
                        <button name="create_invoices" string="Create Invoices" type="object"
                            class="btn-primary"/>
                        <button string="Cancel" class="btn-default" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_view_sale_advance_payment_inv" model="ir.actions.act_window">
            <field name="name">Invoice Order</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">sale.advance.payment.inv</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
            <field name="groups_id" eval="[(4,ref('sales_team.group_sale_salesman'))]"/>
        </record>

        <!-- TODO: check if we need this -->
        <record model="ir.values" id="sale_order_line_make_invoice">
            <field name="model_id" ref="sale.model_sale_order_line" />
            <field name="name">Invoice Orders</field>
            <field name="key2">client_action_multi</field>
            <field name="value" eval="'ir.actions.act_window,' + str(ref('action_view_sale_advance_payment_inv'))" />
            <field name="key">action</field>
            <field name="model">sale.order</field>
        </record>

</odoo>

我真的很绝望,很多时间尝试不同的事情而且看不到光......

1 个答案:

答案 0 :(得分:0)

我发现它,显示错误:

<强> __清单__吡啶

# -*- coding: utf-8 -*-
{
    'name': "invoice_sales_order",
    'summary': """Allows invoice percentage for each line in sales order""",
    'description': """
        Allows invoice percentage for each line in sales order
    """,
    'author': "Miquel",
    'website': "http://example.com",
    'category': 'Uncategorized',
    'version': '0.5',
    'depends': ['base', 'account', 'sale'],
    'data': [
        'views/views.xml',
    ],
    'demo': [
        'demo/demo.xml',
    ],
    'installable': True,
}