0

I got this situation, where I need to get the initial price from class B because the computation is inherited to the 'sale.order' which I cannot put in model A. How to do this since every time I move the calculations to class model_A, it will have an error since it cannot find the inheritance but if I put a inherit on it, it will be having an error again.

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,
            })

I tried to this:

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

and got this error

enter image description here

And still does not work. The initial_price_value did not fetch the values of the total_price. The below picture is the sale.order.line total price.

enter image description here

But the price initial in my tree view is still zero. it should reflect the amount of 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>

model

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,
            })
William
  • 89
  • 1
  • 14

1 Answers1

1

You can take the related field,

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

Thanks

Dipen Shah
  • 2,396
  • 2
  • 9
  • 21
  • There is no `initial_price_value` on `sale.order` ;-) And i would stay with field type `Monetary`, too. – CZoellner Apr 01 '20 at 07:22
  • @CZoellner not exactly got your comment for what stand for but wasn't he try to fetch some price value on the model A from model B where the model B stand with sale.order and some calculation there. So on the Model A he has the many2one field and also gets the price value from the model b itself. Right? – Dipen Shah Apr 01 '20 at 07:33
  • Yes, my comment meant: it's `sale_id.initial_price` and the type should be `Monetary` as in `sale.order.initial_price`. – CZoellner Apr 01 '20 at 07:38
  • Oh.! Yeah, correct.@CZoellner just misplace the field. – Dipen Shah Apr 01 '20 at 07:44
  • I tried to do this but the field causes assertion error: "Field model.initial_price with unknown currency_field None" -- – William Apr 01 '20 at 08:19
  • @William take the currency_id = fields.Many2one('res.currency', string='Currency') and on initial_price_value field add the "currency_field=currency_id". – Dipen Shah Apr 01 '20 at 09:28
  • @DipenShah Thank you for the currency but the initial_price_value did not fetch the values of the initial_price from model_B. – William Apr 01 '20 at 10:12