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/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2010-07-26 11:06:59 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
2010-08-09 06:35:42 +00:00
|
|
|
from osv import fields, osv
|
2011-12-13 17:28:59 +00:00
|
|
|
from tools.translate import _
|
|
|
|
from tools import DEFAULT_SERVER_DATE_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'
|
|
|
|
|
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"""
|
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
|
2011-12-13 15:17:09 +00:00
|
|
|
|
|
|
|
def _prepare_order_picking(self, cr, uid, order, *args):
|
|
|
|
"""Take the requested date into account when creating the picking"""
|
|
|
|
picking_data = super(sale_order_dates, self)._prepare_order_picking(cr,
|
|
|
|
uid, order, *args)
|
|
|
|
picking_data['date'] = order.requested_date
|
|
|
|
return picking_data
|
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 = []
|
|
|
|
for line in order.order_line:
|
2011-12-13 17:28:59 +00:00
|
|
|
dt = (datetime.strptime(order.date_order,
|
|
|
|
DEFAULT_SERVER_DATE_FORMAT)
|
2011-12-13 15:17:09 +00:00
|
|
|
+ relativedelta(days=line.delay or 0.0) )
|
2011-12-13 17:28:59 +00:00
|
|
|
dt_s = dt.strftime(DEFAULT_SERVER_DATE_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"""
|
|
|
|
if requested_date < commitment_date:
|
|
|
|
lang = self.pool.get("res.users").browse(cr, uid, uid,
|
|
|
|
context=context).context_lang
|
|
|
|
if lang:
|
|
|
|
lang_ids = self.pool.get('res.lang').search(cr, uid,
|
|
|
|
[('code', '=', lang)])
|
|
|
|
date_format = self.pool.get("res.lang").browse(cr, uid,
|
|
|
|
lang_ids, context=context)[0].date_format
|
|
|
|
# Parse the dates...
|
|
|
|
req_date_formated = datetime.strptime(requested_date,
|
|
|
|
DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
com_date_formated = datetime.strptime(commitment_date,
|
|
|
|
DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
# ... and reformat them according to the user's language
|
|
|
|
req_date_formated = req_date_formated.strftime(date_format)
|
|
|
|
com_date_formated = com_date_formated.strftime(date_format)
|
|
|
|
else:
|
|
|
|
req_date_formated = requested_date
|
|
|
|
com_date_formated = commitment_date
|
|
|
|
print lang, req_date_formated, com_date_formated
|
|
|
|
return {'warning': {
|
|
|
|
'title': _('Requested date is too soon!'),
|
|
|
|
'message': _("The date requested by the customer (%s) is "
|
|
|
|
"sooner than the commitment date (%s). You may be "
|
|
|
|
"unable to honor the customer's request." %
|
|
|
|
(req_date_formated, com_date_formated))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {}
|
|
|
|
|
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,
|
|
|
|
type='date', string='Commitment Date',
|
|
|
|
help="Date by which the products must be delivered."),
|
|
|
|
'requested_date': fields.date('Requested Date',
|
|
|
|
help="Date by which the customer has requested the products to be delivered."),
|
|
|
|
'effective_date': fields.function(_get_effective_date, type='date',
|
|
|
|
store=True, string='Effective Date',
|
|
|
|
help="Date on which shipping is created."),
|
2010-06-15 22:46:18 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 06:35:42 +00:00
|
|
|
sale_order_dates()
|
2010-06-15 22:46:18 +00:00
|
|
|
|
2010-08-09 06:35:42 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|