[FIX] website: Odoo's social URLs
[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         context = dict(context or {})
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
77
78 class stock_quant(osv.osv):
79     _inherit = 'stock.quant'
80
81     def _get_quants(self, cr, uid, ids, context=None):
82         return self.pool.get('stock.quant').search(cr, uid, [('lot_id', 'in', ids)], context=context)
83
84     _columns = {
85         'removal_date': fields.related('lot_id', 'removal_date', type='datetime', string='Removal Date',
86             store={
87                 'stock.quant': (lambda self, cr, uid, ids, ctx: ids, ['lot_id'], 20),
88                 'stock.production.lot': (_get_quants, ['removal_date'], 20),
89             }),
90     }
91
92     def apply_removal_strategy(self, cr, uid, location, product, qty, domain, removal_strategy, context=None):
93         if removal_strategy == 'fefo':
94             order = 'removal_date, in_date, id'
95             return self._quants_get_order(cr, uid, location, product, qty, domain, order, context=context)
96         return super(stock_quant, self).apply_removal_strategy(cr, uid, location, product, qty, domain, removal_strategy, context=context)
97
98
99 class product_product(osv.osv):
100     _inherit = 'product.template'
101     _columns = {
102         'life_time': fields.integer('Product Life Time',
103             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.'),
104         'use_time': fields.integer('Product Use Time',
105             help='When a new a Serial Number is issued, this is the number of days before the goods starts deteriorating, without being dangerous yet.'),
106         'removal_time': fields.integer('Product Removal Time',
107             help='When a new a Serial Number is issued, this is the number of days before the goods should be removed from the stock.'),
108         'alert_time': fields.integer('Product Alert Time',
109             help='When a new a Serial Number is issued, this is the number of days before an alert should be notified.'),
110     }
111 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: