[FIX] sale: in config wizard, do not assume the 'service' product hasn't been deleted...
[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 openerp.osv import fields, osv
23 from openerp 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='This is the date on which the goods with this Serial Number may become dangerous and must not be consumed.'),
49         'use_date': fields.datetime('Best before Date',
50             help='This is the date on which the goods with this Serial Number start deteriorating, without being dangerous yet.'),
51         'removal_date': fields.datetime('Removal Date',
52             help='This is the date on which the goods with this Serial Number should be removed from the stock.'),
53         'alert_date': fields.datetime('Alert Date',
54             help="This is the date on which an alert should be notified about the goods with this Serial Number."),
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         if context is None:
65             context = {}
66         context['product_id'] = obj.product_id.id
67         self.write(cr, uid, [obj.id], self.default_get(cr, uid, towrite, context=context))
68         return newid
69
70     _defaults = {
71         'life_date': _get_date('life_time'),
72         'use_date': _get_date('use_time'),
73         'removal_date': _get_date('removal_time'),
74         'alert_date': _get_date('alert_time'),
75     }
76 stock_production_lot()
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 product_product()
91 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: