Как получить значение поля из другого класса, унаследованного от sales.order? - PullRequest
0 голосов
/ 01 апреля 2020

У меня возникла такая ситуация, когда мне нужно получить начальную цену из класса B, потому что вычисления наследуются в «sale.order», который я не могу поместить в модель A. Как это сделать, поскольку каждый раз, когда я перемещаю вычисления для класса model_A возникнет ошибка, так как он не сможет найти наследование, но если я добавлю на него наследование, снова возникнет ошибка.

class model_A(models.Model):

    _name='module.model_a'

    sale_id = fields.Many2one(string='sale', comodel_name='sale.order')
    initial_price_value =???

class model_B(models.Model):
    _inherit = 'sale.order'

    model_id = fields.One2many(string='model_b', comodel_name='module.model_a', inverse_name='sale_id')

    initial_price = fields.Monetary(string="Price Initial", store=True, readonly=True, compute='_product_amount',
                                    tracking=4)
    amount_untaxed = fields.Monetary(string="Untaxed Amount", store=True, readonly=True, compute='_product_amount',
                                     tracking=5)
    amount_tax = fields.Monetary(string="Taxes", store=True, readonly=True, compute='_product_amount')

    @api.depends('order_line.price_total')
    def _product_amount(self):
        for order in self:
            amount_untaxed = amount_tax = 0.0
            for line in order.order_line:
                amount_untaxed += line.price_subtotal
                amount_tax += line.price_tax
            order.update({
                'amount_untaxed': amount_untaxed,
                'amount_tax': amount_tax,
                'amount_total': amount_untaxed + amount_tax,
                'initial_price': amount_untaxed + amount_tax,
            })

Я пытался это:

initial_price_value = fields.Monetary(related='sale_id.initial_price')

и получил эту ошибку

enter image description here

И еще не работает. Initial_price_value не получил значения total_price. На картинке ниже показана общая стоимость sale.order.line.

enter image description here

Но начальная цена в моем древовидном представлении все еще равна нулю. он должен отражать сумму 141k

enter image description here

xml:

<odoo>
    <record id="view_order_form" model="ir.ui.view">
        <field name="name">Architect Page View</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//page[@name='order_lines']" position="after">
                <page string="Architect Page">
                    <field name='architect_ids'>
                        <tree string="Architect and Interior Designer"
                              editable="bottom"
                              delete="true"
                        >
                            <control>
                                <create name="add_architect_control" string="Add a architect"/>
                            </control>
                            <field name="architect_id"/>
                            <field name="architect_commission"/>
                            <field name="architect_com_type"/>
                            <field name="initial_price_value" widget="monetary"
                                   options="{'currency_field': 'currency_id'}"
                                   modifiers="{'readonly':true}"/>
                        </tree>
                    </field>
                    <group name="note_group" col="6" modifiers="{}">
                        <group class="oe_subtotal_footer oe_right" colspan="2" name="sale_commission"
                               modifiers="{}">
                            <field name="total_price" widget="monetary" options="{'currency_field': 'currency_id'}"
                                   modifiers="{'readonly':true}"/>
                        </group>
                    </group>
                </page>
            </xpath>
        </field>
    </record>
</odoo>

модель

from odoo import models, fields, api, exceptions


class Architect(models.Model):
    _name = 'metrotiles.architect'

    name = fields.Char('name')
    architect_id = fields.Many2one('res.partner', string="Architect Name")
    architect_com_type = fields.Selection(string='Commission type',
                                          selection=[('percentage', 'Percentage'), ('amount', 'Amount')])
    architect_commission = fields.Float(string='Architect Commission')
    sale_id = fields.Many2one(string='sale', comodel_name='sale.order')
    currency_id = fields.Many2one('res.currency', string='Currency')

    initial_price_value = fields.Monetary(related="sale_id.total_price", string="Initial Price value",
                                          currency_field="currency_id")

    # constraint - architect_com_type, architect_commission
    @api.constrains('architect_com_type', 'architect_commission')
    def _validate_commission(self):
        for field in self:
            if field.architect_com_type == 'percentage':
                if (field.architect_commission > 100) or (field.architect_commission <= 0):
                    raise exceptions.ValidationError(
                        "Percentage fields must be less than equal to 100 or greater than 0")


class ArchitectPage(models.Model):
    _inherit = 'sale.order'

    architect_ids = fields.One2many(string="Architect",
                                    comodel_name='metrotiles.architect', inverse_name='sale_id')
    total_price = fields.Monetary(string="Price Initial", store=True, readonly=True, compute='_amount_all',
                                  tracking=4)
    amount_untaxed = fields.Monetary(string="Untaxed Amount", store=True, readonly=True, compute='_amount_all',
                                     tracking=5)
    amount_tax = fields.Monetary(string="Taxes", store=True, readonly=True, compute='_amount_all')

    @api.depends('order_line.price_total')
    def _amount_all(self):
        for order in self:
            amount_untaxed = amount_tax = 0.0
            for line in order.order_line:
                amount_untaxed += line.price_subtotal
                amount_tax += line.price_tax

            total_price = amount_tax + amount_untaxed
            print(total_price)
            order.update({
                'amount_untaxed': amount_untaxed,
                'amount_tax': amount_tax,
                'amount_total': amount_untaxed + amount_tax,
                'total_price': amount_untaxed + amount_tax,
            })

1 Ответ

1 голос
/ 01 апреля 2020

Вы можете взять related field,

initial_price_value = fields.Monetary(related="sale_id.initial_price", string="Initial Price value")

Спасибо

...