[MERGE]
[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     _name = 'stock.production.lot'
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 = pooler.get_pool(cr.dbname).get('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')
45         return calc_date
46
47     _columns = {
48         'life_date': fields.datetime('End of Life Date',
49             help='The date the lot may become dangerous and should not be consumed.'),
50         'use_date': fields.datetime('Best before Date',
51             help='The date the lot starts deteriorating without becoming dangerous.'),
52         'removal_date': fields.datetime('Removal Date',
53             help='The date the lot should be removed.'),
54         'alert_date': fields.datetime('Alert Date'),
55     }
56     # Assign dates according to products data
57     def create(self, cr, uid, vals, context=None):
58         newid = super(stock_production_lot, self).create(cr, uid, vals, context=context)
59         obj = self.browse(cr, uid, newid, context=context)
60         towrite = []
61         for f in ('life_date','use_date','removal_date','alert_date'):
62             if not getattr(obj, f):
63                 towrite.append(f)
64         context = context or {}
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     _name = 'product.product'
80     _columns = {
81         'life_time': fields.integer('Product lifetime',
82             help='The number of days before a production lot may become dangerous and should not be consumed.'),
83         'use_time': fields.integer('Product usetime',
84             help='The number of days before a production lot starts deteriorating without becoming dangerous.'),
85         'removal_time': fields.integer('Product removal time',
86             help='The number of days before a production lot should be removed.'),
87         'alert_time': fields.integer('Product alert time'),
88     }
89 product_product()
90 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
91