2010-06-15 22:46:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2011-12-14 15:37:43 +00:00
|
|
|
from datetime import datetime, timedelta
|
2010-07-26 11:06:59 +00:00
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import fields, osv
|
2013-07-17 10:33:17 +00:00
|
|
|
from openerp.tools.translate import _
|
2014-05-08 14:28:59 +00:00
|
|
|
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
|
2010-08-09 06:35:42 +00:00
|
|
|
|
2010-06-15 22:46:18 +00:00
|
|
|
class sale_order_dates(osv.osv):
|
2011-12-13 15:17:09 +00:00
|
|
|
"""Add several date fields to Sale Orders, computed or user-entered"""
|
2010-06-15 22:46:18 +00:00
|
|
|
_inherit = 'sale.order'
|
|
|
|
|
2014-04-14 16:12:29 +00:00
|
|
|
def _get_date_planned(self, cr, uid, order, line, start_date, context=None):
|
2011-12-14 15:37:43 +00:00
|
|
|
"""Compute the expected date from the requested date, not the order date"""
|
|
|
|
if order and order.requested_date:
|
2014-05-08 14:28:59 +00:00
|
|
|
date_planned = datetime.strptime(order.requested_date, DEFAULT_SERVER_DATETIME_FORMAT)
|
2011-12-14 15:37:43 +00:00
|
|
|
date_planned -= timedelta(days=order.company_id.security_lead)
|
|
|
|
return date_planned.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
|
2014-03-06 22:50:09 +00:00
|
|
|
return super(sale_order_dates, self)._get_date_planned(
|
|
|
|
cr, uid, order, line, start_date, context=context)
|
2014-04-14 16:12:29 +00:00
|
|
|
|
2010-10-22 13:16:15 +00:00
|
|
|
def _get_effective_date(self, cr, uid, ids, name, arg, context=None):
|
2011-12-13 15:17:09 +00:00
|
|
|
"""Read the shipping date from the related packings"""
|
2014-03-06 22:50:09 +00:00
|
|
|
# TODO: would be better if it returned the date the picking was processed?
|
2010-06-15 22:46:18 +00:00
|
|
|
res = {}
|
|
|
|
dates_list = []
|
2010-11-22 10:37:53 +00:00
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
2010-06-15 22:46:18 +00:00
|
|
|
dates_list = []
|
|
|
|
for pick in order.picking_ids:
|
|
|
|
dates_list.append(pick.date)
|
|
|
|
if dates_list:
|
|
|
|
res[order.id] = min(dates_list)
|
|
|
|
else:
|
2010-10-22 07:23:46 +00:00
|
|
|
res[order.id] = False
|
2010-10-22 13:16:15 +00:00
|
|
|
return res
|
2010-06-15 22:46:18 +00:00
|
|
|
|
2010-10-22 13:16:15 +00:00
|
|
|
def _get_commitment_date(self, cr, uid, ids, name, arg, context=None):
|
2011-12-13 15:17:09 +00:00
|
|
|
"""Compute the commitment date"""
|
2010-06-15 22:46:18 +00:00
|
|
|
res = {}
|
|
|
|
dates_list = []
|
2010-11-22 10:37:53 +00:00
|
|
|
for order in self.browse(cr, uid, ids, context=context):
|
2010-06-15 22:46:18 +00:00
|
|
|
dates_list = []
|
2014-04-15 09:02:48 +00:00
|
|
|
order_datetime = datetime.strptime(order.date_order, DEFAULT_SERVER_DATETIME_FORMAT)
|
2010-06-15 22:46:18 +00:00
|
|
|
for line in order.order_line:
|
2014-04-15 09:02:48 +00:00
|
|
|
dt = order_datetime + timedelta(days=line.delay or 0.0)
|
2014-05-08 14:28:59 +00:00
|
|
|
dt_s = dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
|
2010-06-15 22:46:18 +00:00
|
|
|
dates_list.append(dt_s)
|
2010-06-16 11:15:00 +00:00
|
|
|
if dates_list:
|
|
|
|
res[order.id] = min(dates_list)
|
2010-10-22 13:16:15 +00:00
|
|
|
return res
|
2010-06-15 22:46:18 +00:00
|
|
|
|
2011-12-13 17:28:59 +00:00
|
|
|
def onchange_requested_date(self, cr, uid, ids, requested_date,
|
|
|
|
commitment_date, context=None):
|
|
|
|
"""Warn if the requested dates is sooner than the commitment date"""
|
2014-05-08 14:28:59 +00:00
|
|
|
if (requested_date and commitment_date and requested_date < commitment_date):
|
2011-12-13 17:28:59 +00:00
|
|
|
return {'warning': {
|
|
|
|
'title': _('Requested date is too soon!'),
|
2012-01-04 16:02:03 +00:00
|
|
|
'message': _("The date requested by the customer is "
|
|
|
|
"sooner than the commitment date. You may be "
|
|
|
|
"unable to honor the customer's request.")
|
2011-12-13 17:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 22:50:09 +00:00
|
|
|
return {}
|
2011-12-13 17:28:59 +00:00
|
|
|
|
2010-06-15 22:46:18 +00:00
|
|
|
_columns = {
|
2011-12-13 15:17:09 +00:00
|
|
|
'commitment_date': fields.function(_get_commitment_date, store=True,
|
2014-05-08 14:28:59 +00:00
|
|
|
type='datetime', string='Commitment Date',
|
2014-03-06 22:50:09 +00:00
|
|
|
help="Date by which the products are sure to be delivered. This is "
|
2011-12-14 15:37:43 +00:00
|
|
|
"a date that you can promise to the customer, based on the "
|
|
|
|
"Product Lead Times."),
|
2014-05-08 14:28:59 +00:00
|
|
|
'requested_date': fields.datetime('Requested Date',
|
2014-07-06 14:44:26 +00:00
|
|
|
readonly=True, states={'draft': [('readonly', False)]}, copy=False,
|
2011-12-14 15:37:43 +00:00
|
|
|
help="Date by which the customer has requested the items to be "
|
|
|
|
"delivered.\n"
|
|
|
|
"When this Order gets confirmed, the Delivery Order's "
|
|
|
|
"expected date will be computed based on this date and the "
|
|
|
|
"Company's Security Delay.\n"
|
|
|
|
"Leave this field empty if you want the Delivery Order to be "
|
|
|
|
"processed as soon as possible. In that case the expected "
|
|
|
|
"date will be computed using the default method: based on "
|
|
|
|
"the Product Lead Times and the Company's Security Delay."),
|
2011-12-13 15:17:09 +00:00
|
|
|
'effective_date': fields.function(_get_effective_date, type='date',
|
|
|
|
store=True, string='Effective Date',
|
2011-12-14 15:37:43 +00:00
|
|
|
help="Date on which the first Delivery Order was created."),
|
2010-06-15 22:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-05 18:40:03 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|