[MIG] product_expiry: migration to new api
Thanks rgo-odoo for the _get_dates method and the removal of the context abuse.
This commit is contained in:
committed by
David Monjoie
parent
86033b0f93
commit
71fae64534
@@ -1,7 +1,6 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
{
|
||||
'name' : 'Products Expiration Date',
|
||||
'version' : '1.0',
|
||||
'category' : 'Specific Industry Applications',
|
||||
'depends' : ['stock'],
|
||||
'demo' : ['data/product_expiry_demo.xml'],
|
||||
@@ -18,10 +17,8 @@ Following dates can be tracked:
|
||||
|
||||
Also implements the removal strategy First Expiry First Out (FEFO) widely used, for example, in food industries.
|
||||
""",
|
||||
'data': ['views/product_expiry_view.xml',
|
||||
'data': ['views/production_lot_views.xml',
|
||||
'views/product_template_views.xml',
|
||||
'views/stock_quant_views.xml',
|
||||
'data/product_expiry_data.xml'],
|
||||
'auto_install': False,
|
||||
'installable': True,
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<record id="removal_fefo" model="product.removal">
|
||||
<field name="name">First Expiry First Out (FEFO)</field>
|
||||
<field name="method">fefo</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version='1.0' encoding='UTF-8'?>
|
||||
<openerp>
|
||||
<odoo>
|
||||
<data noupdate="1">
|
||||
|
||||
<record id="product_product_jambon" model="product.product">
|
||||
@@ -87,4 +87,4 @@
|
||||
<field name="product_tmpl_id" ref="product_product_lait_product_template" />
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import production_lot
|
||||
import production_lot
|
||||
import product_product
|
||||
import stock_quant
|
||||
import product_product
|
||||
@@ -1,21 +1,17 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import datetime
|
||||
|
||||
import openerp
|
||||
from openerp.osv import fields, osv
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class product_product(osv.osv):
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
_columns = {
|
||||
'life_time': fields.integer('Product Life Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods may become dangerous and must not be consumed.'),
|
||||
'use_time': fields.integer('Product Use Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods starts deteriorating, without being dangerous yet.'),
|
||||
'removal_time': fields.integer('Product Removal Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods should be removed from the stock.'),
|
||||
'alert_time': fields.integer('Product Alert Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before an alert should be notified.'),
|
||||
}
|
||||
|
||||
life_time = fields.Integer(string='Product Life Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods may become dangerous and must not be consumed.')
|
||||
use_time = fields.Integer(string='Product Use Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods starts deteriorating, without being dangerous yet.')
|
||||
removal_time = fields.Integer(string='Product Removal Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before the goods should be removed from the stock.')
|
||||
alert_time = fields.Integer(string='Product Alert Time',
|
||||
help='When a new a Serial Number is issued, this is the number of days before an alert should be notified.')
|
||||
|
||||
@@ -1,52 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import datetime
|
||||
|
||||
import openerp
|
||||
from openerp.osv import fields, osv
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class stock_production_lot(osv.osv):
|
||||
class StockProductionLot(models.Model):
|
||||
_inherit = 'stock.production.lot'
|
||||
|
||||
def _get_date(dtype):
|
||||
"""Return a function to compute the limit date for this type"""
|
||||
def calc_date(self, cr, uid, context=None):
|
||||
"""Compute the limit date for a given date"""
|
||||
if context is None:
|
||||
context = {}
|
||||
if not context.get('product_id', False):
|
||||
date = False
|
||||
else:
|
||||
product = openerp.registry(cr.dbname)['product.product'].browse(
|
||||
cr, uid, context['product_id'])
|
||||
duration = getattr(product, dtype)
|
||||
# set date to False when no expiry time specified on the product
|
||||
date = duration and (datetime.datetime.today()
|
||||
+ datetime.timedelta(days=duration))
|
||||
return date and date.strftime('%Y-%m-%d %H:%M:%S') or False
|
||||
return calc_date
|
||||
life_date = fields.Datetime(string='End of Life Date',
|
||||
help='This is the date on which the goods with this Serial Number may become dangerous and must not be consumed.')
|
||||
use_date = fields.Datetime(string='Best before Date',
|
||||
help='This is the date on which the goods with this Serial Number start deteriorating, without being dangerous yet.')
|
||||
removal_date = fields.Datetime(string='Removal Date',
|
||||
help='This is the date on which the goods with this Serial Number should be removed from the stock.')
|
||||
alert_date = fields.Datetime(string='Alert Date',
|
||||
help="This is the date on which an alert should be notified about the goods with this Serial Number.")
|
||||
|
||||
def _get_dates(self, product_id=None):
|
||||
"""Returns dates based on number of days configured in current lot's product."""
|
||||
mapped_fields = {
|
||||
'life_date': 'life_time',
|
||||
'use_date': 'use_time',
|
||||
'removal_date': 'removal_time',
|
||||
'alert_date': 'alert_time'
|
||||
}
|
||||
res = dict.fromkeys(mapped_fields.keys(), False)
|
||||
product = self.env['product.product'].browse(product_id) or self.product_id
|
||||
if product:
|
||||
for field in mapped_fields.keys():
|
||||
duration = getattr(product, mapped_fields[field])
|
||||
if duration:
|
||||
date = datetime.datetime.now() + datetime.timedelta(days=duration)
|
||||
res[field] = fields.Datetime.to_string(date)
|
||||
return res
|
||||
|
||||
_columns = {
|
||||
'life_date': fields.datetime('End of Life Date',
|
||||
help='This is the date on which the goods with this Serial Number may become dangerous and must not be consumed.'),
|
||||
'use_date': fields.datetime('Best before Date',
|
||||
help='This is the date on which the goods with this Serial Number start deteriorating, without being dangerous yet.'),
|
||||
'removal_date': fields.datetime('Removal Date',
|
||||
help='This is the date on which the goods with this Serial Number should be removed from the stock.'),
|
||||
'alert_date': fields.datetime('Alert Date',
|
||||
help="This is the date on which an alert should be notified about the goods with this Serial Number."),
|
||||
}
|
||||
# Assign dates according to products data
|
||||
def create(self, cr, uid, vals, context=None):
|
||||
context = dict(context or {})
|
||||
context['product_id'] = vals.get('product_id', context.get('default_product_id'))
|
||||
return super(stock_production_lot, self).create(cr, uid, vals, context=context)
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
vals.update(self._get_dates(vals.get('product_id')))
|
||||
return super(StockProductionLot, self).create(vals)
|
||||
|
||||
_defaults = {
|
||||
'life_date': _get_date('life_time'),
|
||||
'use_date': _get_date('use_time'),
|
||||
'removal_date': _get_date('removal_time'),
|
||||
'alert_date': _get_date('alert_time'),
|
||||
}
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product(self):
|
||||
dates_dict = self._get_dates()
|
||||
for field, value in dates_dict.items():
|
||||
setattr(self, field, value)
|
||||
|
||||
@@ -1,29 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import datetime
|
||||
|
||||
import openerp
|
||||
from openerp.osv import fields, osv
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class stock_quant(osv.osv):
|
||||
class StockQuant(models.Model):
|
||||
_inherit = 'stock.quant'
|
||||
|
||||
def _get_quants(self, cr, uid, ids, context=None):
|
||||
return self.pool.get('stock.quant').search(cr, uid, [('lot_id', 'in', ids)], context=context)
|
||||
removal_date = fields.Datetime(related='lot_id.removal_date', store=True)
|
||||
|
||||
_columns = {
|
||||
'removal_date': fields.related('lot_id', 'removal_date', type='datetime', string='Removal Date',
|
||||
store={
|
||||
'stock.quant': (lambda self, cr, uid, ids, ctx: ids, ['lot_id'], 20),
|
||||
'stock.production.lot': (_get_quants, ['removal_date'], 20),
|
||||
}),
|
||||
}
|
||||
|
||||
def apply_removal_strategy(self, cr, uid, qty, move, ops=False, domain=None, removal_strategy='fifo', context=None):
|
||||
@api.model
|
||||
def apply_removal_strategy(self, qty, move, ops=False, domain=None, removal_strategy='fifo'):
|
||||
if removal_strategy == 'fefo':
|
||||
order = 'removal_date, in_date, id'
|
||||
return self._quants_get_order(cr, uid, qty, move, ops=ops, domain=domain, orderby=order, context=context)
|
||||
return super(stock_quant, self).apply_removal_strategy(cr, uid, qty, move, ops=ops, domain=domain,
|
||||
removal_strategy=removal_strategy, context=context)
|
||||
return self._quants_get_order(qty, move, ops=ops, domain=domain, orderby='removal_date, in_date, id')
|
||||
return super(StockQuant, self).apply_removal_strategy(qty, move, ops=ops, domain=domain, removal_strategy=removal_strategy)
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="view_move_form_expiry">
|
||||
<field name="name">stock.production.lot.form</field>
|
||||
<field name="model">stock.production.lot</field>
|
||||
<field name="inherit_id" ref="stock.view_production_lot_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//group[@name='main_group']" position="after">
|
||||
<group string="Dates">
|
||||
<group>
|
||||
<field name="use_date" />
|
||||
<field name="removal_date" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="life_date" />
|
||||
<field name="alert_date" />
|
||||
</group>
|
||||
</group>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="view_product_form_expiry">
|
||||
<field name="name">product.normal.form</field>
|
||||
<?xml version="1.0" encoding='UTF-8'?>
|
||||
<odoo>
|
||||
<record id="view_product_form_expiry" model="ir.ui.view">
|
||||
<field name="name">product.template.inherit.form</field>
|
||||
<field name="model">product.template</field>
|
||||
<field name="inherit_id" ref="stock.view_template_property_form" />
|
||||
<field name="arch" type="xml">
|
||||
@@ -16,5 +15,4 @@
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding='UTF-8'?>
|
||||
<odoo>
|
||||
<record id="view_move_form_expiry" model="ir.ui.view">
|
||||
<field name="name">stock.production.lot.inherit.form</field>
|
||||
<field name="model">stock.production.lot</field>
|
||||
<field name="inherit_id" ref="stock.view_production_lot_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//group[@name='main_group']" position="after">
|
||||
<group string="Dates">
|
||||
<group>
|
||||
<field name="use_date" />
|
||||
<field name="removal_date" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="life_date" />
|
||||
<field name="alert_date" />
|
||||
</group>
|
||||
</group>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
@@ -1,8 +1,7 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="view_quant_form_expiry">
|
||||
<field name="name">stock.quant.form</field>
|
||||
<?xml version="1.0" encoding='UTF-8'?>
|
||||
<odoo>
|
||||
<record id="view_quant_form_expiry" model="ir.ui.view">
|
||||
<field name="name">stock.quant.inherit.form</field>
|
||||
<field name="model">stock.quant</field>
|
||||
<field name="inherit_id" ref="stock.view_stock_quant_form"/>
|
||||
<field name="arch" type="xml">
|
||||
@@ -11,5 +10,4 @@
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
</odoo>
|
||||
|
||||
Reference in New Issue
Block a user