diff --git a/addons/sale_order_dates/__openerp__.py b/addons/sale_order_dates/__openerp__.py index 5cd6f517b3a..cd2058e62ac 100644 --- a/addons/sale_order_dates/__openerp__.py +++ b/addons/sale_order_dates/__openerp__.py @@ -19,8 +19,5 @@ You can add the following additional dates to a sales order: 'website': 'https://www.odoo.com/page/crm', 'depends': ['sale_stock'], 'data': ['views/sale_order_views.xml'], - 'demo': [], 'test': ['test/requested_date.yml'], - 'installable': True, - 'auto_install': False, } diff --git a/addons/sale_order_dates/models/sale_order.py b/addons/sale_order_dates/models/sale_order.py index cd9dcc9023b..75176271e48 100644 --- a/addons/sale_order_dates/models/sale_order.py +++ b/addons/sale_order_dates/models/sale_order.py @@ -1,52 +1,57 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from datetime import datetime, timedelta +from datetime import timedelta +from odoo import api, fields, models, _ -from openerp.osv import fields, osv -from openerp.tools.translate import _ -from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT -class sale_order_dates(osv.osv): +class SaleOrder(models.Model): """Add several date fields to Sale Orders, computed or user-entered""" _inherit = 'sale.order' - def _get_effective_date(self, cr, uid, ids, name, arg, context=None): - """Read the shipping date from the related packings""" - # TODO: would be better if it returned the date the picking was processed? - res = {} - dates_list = [] - for order in self.browse(cr, uid, ids, context=context): + commitment_date = fields.Datetime(compute='_compute_commitment_date', string='Commitment Date', store=True, + help="Date by which the products are sure to be delivered. This is " + "a date that you can promise to the customer, based on the " + "Product Lead Times.") + requested_date = fields.Datetime('Requested Date', readonly=True, states={'draft': [('readonly', False)], + 'sent': [('readonly', False)]}, copy=False, + 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.") + effective_date = fields.Date(compute='_compute_picking_ids', string='Effective Date', store=True, + help="Date on which the first Delivery Order was created.") + + @api.depends('date_order', 'order_line.customer_lead') + def _compute_commitment_date(self): + """Compute the commitment date""" + for order in self: + dates_list = [] + order_datetime = fields.Datetime.from_string(order.date_order) + for line in order.order_line.filtered(lambda x: x.state != 'cancel'): + dt = order_datetime + timedelta(days=line.customer_lead or 0.0) + dates_list.append(dt) + if dates_list: + order.commitment_date = fields.Datetime.to_string(min(dates_list)) + + def _compute_picking_ids(self): + super(SaleOrder, self)._compute_picking_ids() + for order in self: dates_list = [] for pick in order.picking_ids: - dates_list.append(pick.date) + dates_list.append(fields.Datetime.from_string(pick.date)) if dates_list: - res[order.id] = min(dates_list) - else: - res[order.id] = False - return res + order.write({'effective_date': fields.Datetime.to_string(min(dates_list))}) - def _get_commitment_date(self, cr, uid, ids, name, arg, context=None): - """Compute the commitment date""" - res = {} - dates_list = [] - for order in self.browse(cr, uid, ids, context=context): - dates_list = [] - order_datetime = datetime.strptime(order.date_order, DEFAULT_SERVER_DATETIME_FORMAT) - for line in order.order_line: - if line.state == 'cancel': - continue - dt = order_datetime + timedelta(days=line.customer_lead or 0.0) - dt_s = dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT) - dates_list.append(dt_s) - if dates_list: - res[order.id] = min(dates_list) - return res - - def onchange_requested_date(self, cr, uid, ids, requested_date, - commitment_date, context=None): + @api.onchange('requested_date') + def onchange_requested_date(self): """Warn if the requested dates is sooner than the commitment date""" - if (requested_date and commitment_date and requested_date < commitment_date): + if (self.requested_date and self.commitment_date and self.requested_date < self.commitment_date): return {'warning': { 'title': _('Requested date is too soon!'), 'message': _("The date requested by the customer is " @@ -54,41 +59,17 @@ class sale_order_dates(osv.osv): "unable to honor the customer's request.") } } - return {} - - _columns = { - 'commitment_date': fields.function(_get_commitment_date, store=True, - type='datetime', string='Commitment Date', - help="Date by which the products are sure to be delivered. This is " - "a date that you can promise to the customer, based on the " - "Product Lead Times."), - 'requested_date': fields.datetime('Requested Date', - readonly=True, states={'draft': [('readonly', False)], - 'sent': [('readonly', False)]}, copy=False, - 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."), - 'effective_date': fields.function(_get_effective_date, type='date', - store=True, string='Effective Date', - help="Date on which the first Delivery Order was created."), - } -class SaleOrderLine(osv.osv): +class SaleOrderLine(models.Model): _inherit = 'sale.order.line' - def _prepare_order_line_procurement(self, cr, uid, ids, group_id=False, context=None): - vals = super(SaleOrderLine, self)._prepare_order_line_procurement(cr, uid, ids, group_id=group_id, context=context) - line = self.browse(cr, uid, ids, context=context) - if line.order_id.requested_date: - date_planned = datetime.strptime(line.order_id.requested_date, DEFAULT_SERVER_DATETIME_FORMAT) - timedelta(days=line.order_id.company_id.security_lead) + @api.multi + def _prepare_order_line_procurement(self, group_id): + vals = super(SaleOrderLine, self)._prepare_order_line_procurement(group_id=group_id) + for line in self.filtered("order_id.requested_date"): + date_planned = fields.Datetime.from_string(line.order_id.requested_date) - timedelta(days=line.order_id.company_id.security_lead) vals.update({ - 'date_planned': date_planned.strftime(DEFAULT_SERVER_DATETIME_FORMAT), + 'date_planned': fields.Datetime.to_string(date_planned), }) return vals diff --git a/addons/sale_order_dates/views/sale_order_views.xml b/addons/sale_order_dates/views/sale_order_views.xml index e06c46da090..26851848930 100644 --- a/addons/sale_order_dates/views/sale_order_views.xml +++ b/addons/sale_order_dates/views/sale_order_views.xml @@ -1,41 +1,38 @@ - - - - - - sale.order.form.inherit5 - sale.order - - - - - - - + + + + sale.order.form.inherit + sale.order + + + + + + + + + + + sale.order + + + + + - - - sale.order - - - - - - - - + + - - - sale.order - - - - - - + + + sale.order + + + + + - + + - - +