c16ccc2b1eeaced5aba85058ae9e41f0f6331d4c
[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 from osv import fields, osv
23 import pooler
24
25 class stock_production_lot(osv.osv):
26     _inherit = 'stock.production.lot'
27
28     def _get_date(dtype):
29         """Return a function to compute the limit date for this type"""
30         def calc_date(self, cr, uid, context=None):
31             """Compute the limit date for a given date"""
32             if context is None:
33                 context = {}
34             if not context.get('product_id', False):
35                 date = False
36             else:
37                 product = pooler.get_pool(cr.dbname).get('product.product').browse(
38                     cr, uid, context['product_id'])
39                 duration = getattr(product, dtype)
40                 # set date to False when no expiry time specified on the product
41                 date = duration and (datetime.datetime.today()
42                     + datetime.timedelta(days=duration))
43             return date and date.strftime('%Y-%m-%d %H:%M:%S') or False
44         return calc_date
45
46     _columns = {
47         'life_date': fields.datetime('End of Life Date',
48             help='The date on which the lot may become dangerous and should not be consumed.'),
49         'use_date': fields.datetime('Best before Date',
50             help='The date on which the lot starts deteriorating without becoming dangerous.'),
51         'removal_date': fields.datetime('Removal Date',
52             help='The date on which the lot should be removed.'),
53         'alert_date': fields.datetime('Alert Date', help="The date on which an alert should be notified about the serial number."),
54     }
55     # Assign dates according to products data
56     def create(self, cr, uid, vals, context=None):
57         newid = super(stock_production_lot, self).create(cr, uid, vals, context=context)
58         obj = self.browse(cr, uid, newid, context=context)
59         towrite = []
60         for f in ('life_date', 'use_date', 'removal_date', 'alert_date'):
61             if not getattr(obj, f):
62                 towrite.append(f)
63         if context is None:
64             context = {}
65         context['product_id'] = obj.product_id.id
66         self.write(cr, uid, [obj.id], self.default_get(cr, uid, towrite, context=context))
67         return newid
68
69     _defaults = {
70         'life_date': _get_date('life_time'),
71         'use_date': _get_date('use_time'),
72         'removal_date': _get_date('removal_time'),
73         'alert_date': _get_date('alert_time'),
74     }
75 stock_production_lot()
76
77 class product_product(osv.osv):
78     _inherit = 'product.product'
79     _columns = {
80         'life_time': fields.integer('Product Life Time',
81             help='The number of days before a serial number may become dangerous and should not be consumed.'),
82         'use_time': fields.integer('Product Use Time',
83             help='The number of days before a serial number starts deteriorating without becoming dangerous.'),
84         'removal_time': fields.integer('Product Removal Time',
85             help='The number of days before a serial number should be removed.'),
86         'alert_time': fields.integer('Product Alert Time', help="The number of days after which an alert should be notified about the serial number."),
87     }
88 product_product()
89 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: