[IMP] wording
[odoo/odoo.git] / addons / product_expiry / product_expiry.py
1 ##############################################################################
2 #    
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
18 #
19 ##############################################################################
20
21 import datetime
22
23 import openerp
24 from openerp.osv import fields, osv
25
26 class stock_production_lot(osv.osv):
27     _inherit = 'stock.production.lot'
28
29     def _get_date(dtype):
30         """Return a function to compute the limit date for this type"""
31         def calc_date(self, cr, uid, context=None):
32             """Compute the limit date for a given date"""
33             if context is None:
34                 context = {}
35             if not context.get('product_id', False):
36                 date = False
37             else:
38                 product = openerp.registry(cr.dbname)['product.product'].browse(
39                     cr, uid, context['product_id'])
40                 duration = getattr(product, dtype)
41                 # set date to False when no expiry time specified on the product
42                 date = duration and (datetime.datetime.today()
43                     + datetime.timedelta(days=duration))
44             return date and date.strftime('%Y-%m-%d %H:%M:%S') or False
45         return calc_date
46
47     _columns = {
48         'life_date': fields.datetime('End of Life Date',
49             help='This is the date on which the goods with this Serial Number may become dangerous and must not be consumed.'),
50         'use_date': fields.datetime('Best before Date',
51             help='This is the date on which the goods with this Serial Number start deteriorating, without being dangerous yet.'),
52         'removal_date': fields.datetime('Removal Date',
53             help='This is the date on which the goods with this Serial Number should be removed from the stock.'),
54         'alert_date': fields.datetime('Alert Date',
55             help="This is the date on which an alert should be notified about the goods with this Serial Number."),
56     }
57     # Assign dates according to products data
58     def create(self, cr, uid, vals, context=None):
59         newid = super(stock_production_lot, self).create(cr, uid, vals, context=context)
60         obj = self.browse(cr, uid, newid, context=context)
61         towrite = []
62         for f in ('life_date', 'use_date', 'removal_date', 'alert_date'):
63             if not getattr(obj, f):
64                 towrite.append(f)
65         if context is None:
66             context = {}
67         context['product_id'] = obj.product_id.id
68         self.write(cr, uid, [obj.id], self.default_get(cr, uid, towrite, context=context))
69         return newid
70
71     _defaults = {
72         'life_date': _get_date('life_time'),
73         'use_date': _get_date('use_time'),
74         'removal_date': _get_date('removal_time'),
75         'alert_date': _get_date('alert_time'),
76     }
77
78 class product_product(osv.osv):
79     _inherit = 'product.product'
80     _columns = {
81         'life_time': fields.integer('Product Life Time',
82             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.'),
83         'use_time': fields.integer('Product Use Time',
84             help='When a new a Serial Number is issued, this is the number of days before the goods starts deteriorating, without being dangerous yet.'),
85         'removal_time': fields.integer('Product Removal Time',
86             help='When a new a Serial Number is issued, this is the number of days before the goods should be removed from the stock.'),
87         'alert_time': fields.integer('Product Alert Time',
88             help='When a new a Serial Number is issued, this is the number of days before an alert should be notified.'),
89     }
90 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: