Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
515 Vistas

How to add a field that displays information related to the distance of the last movement of goods with the date the report was drawn

For example, if the last product movement was on 6/1/2025, and I want to see the report for the movement of goods on 6/10/2025, the system will display the stock age as 10 days. Then, if I pull up the date 6/15/2025, the system will display the stock age as 15 days.

Avatar
Descartar
Mejor respuesta

Hi,


You want to display the stock age of a product based on the last stock movement date. This age should be calculated dynamically based on a user-defined report date.

This can be done by extending the 'product.product' model using a custom module in Odoo 17 on-premise.


1. Create a Custom Module.


from odoo import models, fields, api

from datetime import date


class ProductProduct(models.Model):

    _inherit = 'product.product'


    report_date = fields.Date(string="Report Date")

    stock_age_days = fields.Integer(string="Stock Age (Days)", compute="_compute_stock_age")


    @api.depends('report_date')

    def _compute_stock_age(self):

        for product in self:

            if product.report_date:

                last_move = self.env['stock.move'].search([

                    ('product_id', '=', product.id),

                    ('state', '=', 'done'),

                    ('location_dest_id.usage', '=', 'internal'),

                ], order='date desc', limit=1)

               

                if last_move:

                    move_date = last_move.date.date()

                    delta_days = (product.report_date - move_date).days

                    product.stock_age_days = max(delta_days, 0)

                else:

                    product.stock_age_days = 0

            else:

                product.stock_age_days = 0


2. Add the Fields to Views


<odoo>

    <record id="view_product_form_inherit_stock_age" model="ir.ui.view">

        <field name="name">product.product.form.inherit.stock.age</field>

        <field name="model">product.product</field>

        <field name="inherit_id" ref="product.product_normal_form_view"/>

        <field name="arch" type="xml">

            <xpath expr="//sheet/group" position="inside">

                <group string="Stock Age Info">

                    <field name="report_date"/>

                    <field name="stock_age_days" readonly="1"/>

                </group>

            </xpath>

        </field>

    </record>

</odoo>


How It Works

     - The user sets a Report Date on the product form.

     - Odoo will search for the most recent stock move into internal location for that product.

     - The system calculates the number of days between the report_date and the last move date.

     - The result appears in the Stock Age (Days) field.


By customizing the 'product.product' model in your on-premise Odoo 17 instance, you can efficiently track and display the stock age of products based on any date selected for reporting. This approach offers full control and dynamic stock aging without relying on external spreadsheets or filters.


More information.

- Refer to the flow of the Inventory Aging' default report in Odoo.

(Inventory --> Reporting --> Inventory Aging )


Hope it helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 20
3045
1
sept 15
3870
1
mar 15
5029
1
mar 15
5239
0
ago 24
3549