[MERGE] forward port of branch 7.0 up to 3a0af6a
[odoo/odoo.git] / addons / sale_order_dates / sale_order_dates.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from datetime import datetime
23 from dateutil.relativedelta import relativedelta
24
25 from openerp.osv import fields, osv
26
27 class sale_order_dates(osv.osv):
28     _inherit = 'sale.order'
29
30     def _get_effective_date(self, cr, uid, ids, name, arg, context=None):
31         res = {}
32         dates_list = []
33         for order in self.browse(cr, uid, ids, context=context):
34             dates_list = []
35             for pick in order.picking_ids:
36                 dates_list.append(pick.date)
37             if dates_list:
38                 res[order.id] = min(dates_list)
39             else:
40                 res[order.id] = False
41         return res
42
43     def _get_commitment_date(self, cr, uid, ids, name, arg, context=None):
44         res = {}
45         dates_list = []
46         for order in self.browse(cr, uid, ids, context=context):
47             dates_list = []
48             for line in order.order_line:
49                 dt = datetime.strptime(order.date_order, '%Y-%m-%d') + relativedelta(days=line.delay or 0.0)
50                 dt_s = dt.strftime('%Y-%m-%d')
51                 dates_list.append(dt_s)
52             if dates_list:
53                 res[order.id] = min(dates_list)
54         return res
55
56     _columns = {
57         'commitment_date': fields.function(_get_commitment_date, store=True, type='date', string='Commitment Date', help="Committed date for delivery."),
58         'requested_date': fields.date('Requested Date', help="Date requested by the customer for the sale."),
59         'effective_date': fields.function(_get_effective_date, type='date', store=True, string='Effective Date',help="Date on which picking is created."),
60     }
61
62
63 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: