1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from datetime import datetime, timedelta
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26 from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
28 class sale_order_dates(osv.osv):
29 """Add several date fields to Sale Orders, computed or user-entered"""
30 _inherit = 'sale.order'
32 def _get_date_planned(self, cr, uid, order, line, start_date, context=None):
33 """Compute the expected date from the requested date, not the order date"""
34 if order and order.requested_date:
35 date_planned = datetime.strptime(order.requested_date, DEFAULT_SERVER_DATETIME_FORMAT)
36 date_planned -= timedelta(days=order.company_id.security_lead)
37 return date_planned.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
38 return super(sale_order_dates, self)._get_date_planned(
39 cr, uid, order, line, start_date, context=context)
41 def _get_effective_date(self, cr, uid, ids, name, arg, context=None):
42 """Read the shipping date from the related packings"""
43 # TODO: would be better if it returned the date the picking was processed?
46 for order in self.browse(cr, uid, ids, context=context):
48 for pick in order.picking_ids:
49 dates_list.append(pick.date)
51 res[order.id] = min(dates_list)
56 def _get_commitment_date(self, cr, uid, ids, name, arg, context=None):
57 """Compute the commitment date"""
60 for order in self.browse(cr, uid, ids, context=context):
62 order_datetime = datetime.strptime(order.date_order, DEFAULT_SERVER_DATETIME_FORMAT)
63 for line in order.order_line:
64 dt = order_datetime + timedelta(days=line.delay or 0.0)
65 dt_s = dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
66 dates_list.append(dt_s)
68 res[order.id] = min(dates_list)
71 def onchange_requested_date(self, cr, uid, ids, requested_date,
72 commitment_date, context=None):
73 """Warn if the requested dates is sooner than the commitment date"""
74 if (requested_date and commitment_date and requested_date < commitment_date):
76 'title': _('Requested date is too soon!'),
77 'message': _("The date requested by the customer is "
78 "sooner than the commitment date. You may be "
79 "unable to honor the customer's request.")
85 'commitment_date': fields.function(_get_commitment_date, store=True,
86 type='datetime', string='Commitment Date',
87 help="Date by which the products are sure to be delivered. This is "
88 "a date that you can promise to the customer, based on the "
89 "Product Lead Times."),
90 'requested_date': fields.datetime('Requested Date',
91 readonly=True, states={'draft': [('readonly', False)],
92 'sent': [('readonly', False)]}, copy=False,
93 help="Date by which the customer has requested the items to be "
95 "When this Order gets confirmed, the Delivery Order's "
96 "expected date will be computed based on this date and the "
97 "Company's Security Delay.\n"
98 "Leave this field empty if you want the Delivery Order to be "
99 "processed as soon as possible. In that case the expected "
100 "date will be computed using the default method: based on "
101 "the Product Lead Times and the Company's Security Delay."),
102 'effective_date': fields.function(_get_effective_date, type='date',
103 store=True, string='Effective Date',
104 help="Date on which the first Delivery Order was created."),
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: