[MOV] product_expiry: split file product_expiry.py
This commit is contained in:
committed by
David Monjoie
parent
1119681de0
commit
86033b0f93
@@ -19,6 +19,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',
|
||||
'views/product_template_views.xml',
|
||||
'views/stock_quant_views.xml',
|
||||
'data/product_expiry_data.xml'],
|
||||
'auto_install': False,
|
||||
'installable': True,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import product_expiry
|
||||
import production_lot
|
||||
import stock_quant
|
||||
import product_product
|
||||
@@ -1,100 +0,0 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import datetime
|
||||
|
||||
import openerp
|
||||
from openerp import api, models
|
||||
from openerp.osv import fields, osv
|
||||
|
||||
class stock_production_lot(osv.osv):
|
||||
_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
|
||||
|
||||
_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)
|
||||
|
||||
_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'),
|
||||
}
|
||||
|
||||
|
||||
# Onchange added in new api to avoid having to change views
|
||||
class StockProductionLot(models.Model):
|
||||
_inherit = 'stock.production.lot'
|
||||
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product(self):
|
||||
defaults = self.with_context(
|
||||
product_id=self.product_id.id).default_get(
|
||||
['life_date', 'use_date', 'removal_date', 'alert_date'])
|
||||
for field, value in defaults.items():
|
||||
setattr(self, field, value)
|
||||
|
||||
|
||||
class stock_quant(osv.osv):
|
||||
_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)
|
||||
|
||||
_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):
|
||||
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)
|
||||
|
||||
|
||||
class product_product(osv.osv):
|
||||
_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.'),
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
# -*- 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
|
||||
|
||||
|
||||
class product_product(osv.osv):
|
||||
_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.'),
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
# -*- 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
|
||||
|
||||
|
||||
class stock_production_lot(osv.osv):
|
||||
_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
|
||||
|
||||
_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)
|
||||
|
||||
_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'),
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
# -*- 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
|
||||
|
||||
|
||||
class stock_quant(osv.osv):
|
||||
_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)
|
||||
|
||||
_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):
|
||||
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)
|
||||
@@ -20,34 +20,6 @@
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_product_form_expiry">
|
||||
<field name="name">product.normal.form</field>
|
||||
<field name="model">product.template</field>
|
||||
<field name="inherit_id" ref="stock.view_template_property_form" />
|
||||
<field name="arch" type="xml">
|
||||
<group name="stock_property" position="after">
|
||||
<group name="expiry_and_lots" groups="stock.group_production_lot">
|
||||
<field name="life_time" />
|
||||
<field name="use_time" />
|
||||
<field name="removal_time" />
|
||||
<field name="alert_time" />
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="view_quant_form_expiry">
|
||||
<field name="name">stock.quant.form</field>
|
||||
<field name="model">stock.quant</field>
|
||||
<field name="inherit_id" ref="stock.view_stock_quant_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='lot_id']" position="after" >
|
||||
<field name="removal_date"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="view_product_form_expiry">
|
||||
<field name="name">product.normal.form</field>
|
||||
<field name="model">product.template</field>
|
||||
<field name="inherit_id" ref="stock.view_template_property_form" />
|
||||
<field name="arch" type="xml">
|
||||
<group name="stock_property" position="after">
|
||||
<group name="expiry_and_lots" groups="stock.group_production_lot">
|
||||
<field name="life_time" />
|
||||
<field name="use_time" />
|
||||
<field name="removal_time" />
|
||||
<field name="alert_time" />
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" ?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="view_quant_form_expiry">
|
||||
<field name="name">stock.quant.form</field>
|
||||
<field name="model">stock.quant</field>
|
||||
<field name="inherit_id" ref="stock.view_stock_quant_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='lot_id']" position="after" >
|
||||
<field name="removal_date"/>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
Reference in New Issue
Block a user