Skip to content

Commit

Permalink
Port sale_start_end_dates and sale_rental (OCA#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis de Lattre authored and alexis-via committed Dec 23, 2020
1 parent ce19af9 commit f59d599
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 289 deletions.
30 changes: 26 additions & 4 deletions sale_start_end_dates/README.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

====================
Sale Start End Dates
====================

Expand All @@ -7,6 +12,21 @@ sale order line is copied to the start/end dates of invoice lines. This
module is a technical module: it is designed to be used with other
modules such as the *sale_rental* module.

Usage
=====

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/167/9.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/sale-workflow/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.

Credits
=======

Expand All @@ -18,12 +38,14 @@ Contributors
Maintainer
----------

.. image:: http://odoo-community.org/logo.png
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: http://odoo-community.org
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.
OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit http://odoo-community.org.
To contribute to this module, please visit https://odoo-community.org.
24 changes: 2 additions & 22 deletions sale_start_end_dates/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Sale Start End Dates module for Odoo
# Copyright (C) 2014-2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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/>.
#
##############################################################################
# -*- coding: utf-8 -*-

from . import sale
from . import models
34 changes: 8 additions & 26 deletions sale_start_end_dates/__openerp__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Sale Start End Dates module for Odoo
# Copyright (C) 2014-2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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/>.
#
##############################################################################

# -*- coding: utf-8 -*-
# © 2014-2016 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Sale Start End Dates',
'version': '8.0.0.1.0',
'version': '9.0.1.0.0',
'category': 'Sales Management',
'license': 'AGPL-3',
'summary': 'Adds start date and end date on sale order lines',
'author': 'Akretion,Odoo Community Association (OCA)',
'website': 'http://www.akretion.com',
'depends': ['account_cutoff_prepaid', 'sale', 'web_context_tunnel'],
'data': ['sale_view.xml'],
'demo': ['sale_demo.xml'],
'depends': ['account_invoice_start_end_dates', 'sale'],
'data': ['views/sale_order.xml'],
'demo': ['demo/sale_demo.xml'],
'installable': True,
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014-2015 Akretion (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
The licence is in the file __openerp__.py
© 2014-2016 Akretion (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->

<openerp>
<data noupdate="1">
<odoo noupdate="1">


<!-- Add admin and demo user to group "Properties on lines", so that they have
Expand All @@ -20,5 +19,4 @@ access to the form view of sale order lines -->
</record>


</data>
</openerp>
</odoo>
3 changes: 3 additions & 0 deletions sale_start_end_dates/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from . import sale
158 changes: 158 additions & 0 deletions sale_start_end_dates/models/sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# -*- coding: utf-8 -*-
# © 2014-2016 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, fields, api, _
from openerp.exceptions import ValidationError
from dateutil.relativedelta import relativedelta


class SaleOrder(models.Model):
_inherit = 'sale.order'

default_start_date = fields.Date(string='Default Start Date')
default_end_date = fields.Date(string='Default End Date')

@api.one
@api.constrains('default_start_date', 'default_end_date')
def _check_default_start_end_dates(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
raise ValidationError(
_("Default Start Date should be before or be the "
"same as Default End Date for sale order %s")
% self.name)

@api.onchange('default_start_date')
def default_start_date_change(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
self.default_end_date = self.default_start_date

@api.onchange('default_end_date')
def default_end_date_change(self):
if (
self.default_start_date and
self.default_end_date and
self.default_start_date > self.default_end_date):
self.default_start_date = self.default_end_date


class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'

start_date = fields.Date(
string='Start Date', readonly=True,
states={'draft': [('readonly', False)]})
end_date = fields.Date(
string='End Date', readonly=True,
states={'draft': [('readonly', False)]})
number_of_days = fields.Integer(string='Number of Days')
must_have_dates = fields.Boolean(
related='product_id.must_have_dates', readonly=True)

@api.one
@api.constrains('start_date', 'end_date', 'number_of_days')
def _check_start_end_dates(self):
if self.product_id and self.must_have_dates:
if not self.end_date:
raise ValidationError(_(
"Missing End Date for sale order line with "
"Product '%s'.") % (self.product_id.name))
if not self.start_date:
raise ValidationError(_(
"Missing Start Date for sale order line with "
"Product '%s'.") % (self.product_id.name))
if not self.number_of_days:
raise ValidationError(_(
"Missing number of days for sale order line with "
"Product '%s'.") % (self.product_id.name))
if self.start_date > self.end_date:
raise ValidationError(_(
"Start Date should be before or be the same as "
"End Date for sale order line with Product '%s'.")
% (self.product_id.name))
if self.number_of_days < 0:
raise ValidationError(_(
"On sale order line with Product '%s', the "
"number of days is negative ; this is not allowed.")
% (self.product_id.name))
days_delta = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != days_delta:
raise ValidationError(_(
"On the sale order line with Product '%s', "
"there are %d days between the Start Date (%s) and "
"the End Date (%s), but the number of days field "
"has a value of %d days.")
% (self.product_id.name, days_delta, self.start_date,
self.end_date, self.number_of_days))

@api.multi
def _prepare_invoice_line(self, qty):
self.ensure_one()
res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
if self.must_have_dates:
res.update({
'start_date': self.start_date,
'end_date': self.end_date,
})
return res

@api.onchange('end_date')
def end_date_change(self):
if self.end_date:
if self.start_date and self.start_date > self.end_date:
self.start_date = self.end_date
if self.start_date:
number_of_days = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != number_of_days:
self.number_of_days = number_of_days

@api.onchange('start_date')
def start_date_change(self):
if self.start_date:
if self.end_date and self.start_date > self.end_date:
self.end_date = self.start_date
if self.end_date:
number_of_days = (
fields.Date.from_string(self.end_date) -
fields.Date.from_string(self.start_date)).days + 1
if self.number_of_days != number_of_days:
self.number_of_days = number_of_days

@api.onchange('number_of_days')
def number_of_days_change(self):
if self.number_of_days:
if self.start_date:
end_date_dt = fields.Date.from_string(self.start_date) +\
relativedelta(days=self.number_of_days - 1)
end_date = fields.Date.to_string(end_date_dt)
if self.end_date != end_date:
self.end_date = end_date
elif self.end_date:
self.start_date = fields.Date.from_string(self.end_date) -\
relativedelta(days=self.number_of_days - 1)

@api.onchange('product_id')
def start_end_dates_product_id_change(self):
if self.product_id:
if self.order_id.default_start_date:
self.start_date = self.order_id.default_start_date
else:
self.start_date = False
if self.order_id.default_end_date:
self.end_date = self.order_id.default_end_date
else:
self.end_date = False
else:
self.start_date = False
self.end_date = False
Loading

0 comments on commit f59d599

Please sign in to comment.