-3

I have a problem with 2 modules in Odoo, the first is my auxiliary module that stores the price per kilo of each beef:

enter code herefrom odoo import api, fields, models

class LivestockPrice(models.Model):
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _name = 'price.pound'
    _description = "New pound price"
    _order = 'date_pound desc'

    name = fields.Char(string="New")
    active = fields.Boolean(string="Active", default=True)
    cow_id = fields.Many2one(
        'livestock.model',
        string="Beef identifier",
        required=True
    )
    date_pound = fields.Date(
        string="New price date",
        required=True,
        default=fields.Date.today()
    )
    price_import = fields.Monetary(
        string="Price per kilo",
        required=True,
        tracking=True
    )
    type_cow = fields.Selection(
        related='cow_id.type_cow',
        string="Type of livestock",
        readonly=False,
        store=True,
        required=True
    )

What I want is to record prices per pound on a certain date and according to the type of cattle, that is, it stores several prices per pound according to the type_cow, now, this is my main model:

from odoo import api, fields, models
from odoo.exceptions import ValidationError


class LivestockModel(models.Model):

    _name = 'livestock.model'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = 'New cattle from the agricultural and livestock farm created'
    _order = 'date_create desc'
    _rec_name = 'cow_id'
    _order = 'cow_id asc'
    cow_id = fields.Char(
        required=True,
        string="Beef identifier",
        tracking=True
    )
    weight_cow = fields.Float(
        string="Current weight",
        tracking=True,
        store=True
    )
    other_model = fields.Many2one(
        'price.pound',
        string="Price per pound"
    )
    price_actually = fields.Monetary(
        compute="_compute_price_actually",
        string="Current price per kilo",
        store=True
    )
    price_import = fields.Monetary(
        related="other_model.price_import",
        string="Price per kilo",
        store=True
    )
    price_total = fields.Monetary(
      compute="_compute_cost_import",
      string="Amount",
      store=True
    )
    type_cow = fields.Selection(
        [
            ('Steer', 'Steer of meat'),
            ('Heifer', 'Beef Heifer')
        ], 
        string="Type of livestock:",
        required=True
    )

    @api.depends('weight_cow', 'other_model.price_import')
    def _compute_cost_import(self):
        """This function calculates the amount of each beef"""
        for record in self:
            price_import_record = self.env['price.pound'].search(
                [('type_cow', '=', record.type_cow)], 
                order='date_pound desc',  limit=1
            )
            if record.weight_cow and price_import_record:
                record.price_total = record.weight_cow * price_import_record.price_import
            else:
                record.price_total = 0.0 

As you can see, in my livestock.model I use my own type_cow for that model, what I am looking for is to calculate the amount for each cow according to the type of livestock, which is type_cow, but I have a problem, it does it but only when I enter a new peso, which it shouldn't be, because it should be calculated automatically when I enter a new price per pound or a new peso. You must do the calculation whenever a new price_import or weight record is introduced in that model.

1
  • Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debugging" and it helps to see what code is really doing. Commented Jun 11, 2024 at 16:41

1 Answer 1

0

I found the solution using a trigger in the price.pound model. This method executes the function that updates the related records in the livestock_model: I share it here:

@api.model_create_multi
def create(self, vals_list):
    """método create para volver a lanzar forzosamente el cálculo de los registros existentes"""
    records = super(LivestockPrice, self).create(vals_list)
    for record in records:
        # Buscar los registros relacionados en el otro modelo y actualizarlos
        related_records = self.env['livestock.model'].search([('type_cow', '=', record.type_cow)])
        related_records._compute_cost_import()
        related_records._compute_price_actually()
    return records
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.