diff --git a/purchase_start_end_dates/README.rst b/purchase_start_end_dates/README.rst new file mode 100644 index 00000000000..bb170b66c80 --- /dev/null +++ b/purchase_start_end_dates/README.rst @@ -0,0 +1,84 @@ +======================== +Purchase Start End Dates +======================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:757c6e55d092de01f3be83c25a8b0ba88ac95b36e038e121d47765c40da46fa8 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpurchase--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/purchase-workflow/tree/17.0/purchase_start_end_dates + :alt: OCA/purchase-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/purchase-workflow-17-0/purchase-workflow-17-0-purchase_start_end_dates + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/purchase-workflow&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adds the fields *Start Date* and *End Date* on purchase +order lines (requied fields when the product has the *Must Have Start +and End Dates* property). You can also define a *Default Start Date* and +*Default End Date* on the purchase order; when set, these dates will be +used by default on new purchase order lines. Upon invoice creation, the +values of the start/end dates of the purchase order line are copied to +the start/end dates of invoice lines. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* elego Software Solutions GmbH + +Contributors +------------ + +- `Stesi Consulting `__: + + - Michele Di Croce + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/purchase-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/purchase_start_end_dates/__init__.py b/purchase_start_end_dates/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/purchase_start_end_dates/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/purchase_start_end_dates/__manifest__.py b/purchase_start_end_dates/__manifest__.py new file mode 100644 index 00000000000..f327383710f --- /dev/null +++ b/purchase_start_end_dates/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2022 elego Software Solutions, Germany (https://www.elegosoft.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Purchase Start End Dates", + "version": "17.0.1.0.0", + "category": "Purchase", + "license": "AGPL-3", + "summary": "Adds start date and end date on purchase order lines", + "author": "elego Software Solutions GmbH, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/purchase-workflow", + "depends": ["purchase", "account_invoice_start_end_dates"], + "data": [ + "views/purchase_order.xml", + ], + "installable": True, +} diff --git a/purchase_start_end_dates/i18n/purchase_start_end_dates.pot b/purchase_start_end_dates/i18n/purchase_start_end_dates.pot new file mode 100644 index 00000000000..b9a671cb323 --- /dev/null +++ b/purchase_start_end_dates/i18n/purchase_start_end_dates.pot @@ -0,0 +1,101 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * purchase_start_end_dates +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 12.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order__default_end_date +msgid "Default End Date" +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order__default_start_date +msgid "Default Start Date" +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:23 +#, python-format +msgid "Default Start Date should be before or be the same as Default End Date for purchase order '%s'." +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order_line__end_date +msgid "End Date" +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,help:purchase_start_end_dates.field_purchase_order_line__must_have_dates +msgid "If this option is active, the user will have to enter a Start Date and an End Date on the invoice lines that have this product." +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model,name:purchase_start_end_dates.model_account_invoice +msgid "Invoice" +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:111 +#, python-format +msgid "Missing End Date for purchase order line with Product '%s'." +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:103 +#, python-format +msgid "Missing Start Date for purchase order line with Product '%s'." +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order_line__must_have_dates +msgid "Must Have Start and End Dates" +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order_line__number_of_days +msgid "Number of Days" +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:81 +#, python-format +msgid "On purchase order line with product '%s', the number of days is negative (%d) ; this is not allowed. The number of days has been forced to 1." +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model,name:purchase_start_end_dates.model_purchase_order +msgid "Purchase Order" +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model,name:purchase_start_end_dates.model_purchase_order_line +msgid "Purchase Order Line" +msgstr "" + +#. module: purchase_start_end_dates +#: model:ir.model.fields,field_description:purchase_start_end_dates.field_purchase_order_line__start_date +msgid "Start Date" +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:119 +#, python-format +msgid "Start Date should be before or be the same as End Date for purchase order line with Product '%s'." +msgstr "" + +#. module: purchase_start_end_dates +#: code:addons/purchase_start_end_dates/models/purchase_order.py:80 +#, python-format +msgid "Wrong number of days" +msgstr "" + diff --git a/purchase_start_end_dates/models/__init__.py b/purchase_start_end_dates/models/__init__.py new file mode 100644 index 00000000000..9f03530643d --- /dev/null +++ b/purchase_start_end_dates/models/__init__.py @@ -0,0 +1 @@ +from . import purchase_order diff --git a/purchase_start_end_dates/models/purchase_order.py b/purchase_start_end_dates/models/purchase_order.py new file mode 100644 index 00000000000..f6eef06318a --- /dev/null +++ b/purchase_start_end_dates/models/purchase_order.py @@ -0,0 +1,164 @@ +# Copyright 2022 elego Software Solutions, Germany (https://www.elegosoft.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from dateutil.relativedelta import relativedelta + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + default_start_date = fields.Date() + default_end_date = fields.Date() + + @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 purchase order '%s'." + ) + % self.display_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 PurchaseOrderLine(models.Model): + _inherit = "purchase.order.line" + + start_date = fields.Date(readonly=True) + end_date = fields.Date(readonly=True) + number_of_days = fields.Integer( + compute="_compute_number_of_days", + inverse="_inverse_number_of_days", + string="Number of Days", + readonly=False, + store=True, + ) + must_have_dates = fields.Boolean(related="product_id.must_have_dates") + + def _prepare_account_move_line(self, move=False): + data = super()._prepare_account_move_line(move=move) + if self.product_id.must_have_dates: + data.update( + { + "start_date": self.start_date, + "end_date": self.end_date, + } + ) + return data + + @api.depends("start_date", "end_date") + def _compute_number_of_days(self): + for line in self: + days = False + if line.start_date and line.end_date: + days = (line.end_date - line.start_date).days + 1 + line.number_of_days = days + + @api.onchange("number_of_days") + def _inverse_number_of_days(self): + res = {"warning": {}} + for line in self: + if line.number_of_days < 0: + res["warning"]["title"] = _("Wrong number of days") + res["warning"]["message"] = _( + "On purchase order line with product '%(product_name)s', the " + "number of days is negative (%(number_of_days)s) ; this is not " + "allowed. The number of days has been forced to 1." + ) % ( + { + "product_name": line.product_id.display_name, + "number_of_days": line.number_of_days, + } + ) + line.number_of_days = 1 + if line.start_date: + line.end_date = line.start_date + relativedelta( + days=line.number_of_days - 1 + ) + elif line.end_date: + line.start_date = line.end_date - relativedelta( + days=line.number_of_days - 1 + ) + return res + + @api.constrains("start_date", "end_date") + def _check_start_end_dates(self): + for line in self: + if line.must_have_dates: + if not line.start_date: + raise ValidationError( + _( + "Missing Start Date for purchase order line with " + "Product '%s'." + ) + % (line.product_id.name) + ) + if not line.end_date: + raise ValidationError( + _( + "Missing End Date for purchase order line with " + "Product '%s'." + ) + % (line.product_id.name) + ) + if line.start_date > line.end_date: + raise ValidationError( + _( + "Start Date should be before or be the same as " + "End Date for purchase order line with Product '%s'." + ) + % (line.product_id.name) + ) + + @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 + + @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 + + @api.onchange("product_id") + def start_end_dates_product_id_change(self): + if self.product_id.must_have_dates: + 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 diff --git a/purchase_start_end_dates/pyproject.toml b/purchase_start_end_dates/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/purchase_start_end_dates/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/purchase_start_end_dates/readme/CONTRIBUTORS.md b/purchase_start_end_dates/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..d94fefdc319 --- /dev/null +++ b/purchase_start_end_dates/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Stesi Consulting](https://www.stesi.consulting): + - Michele Di Croce \ No newline at end of file diff --git a/purchase_start_end_dates/readme/DESCRIPTION.md b/purchase_start_end_dates/readme/DESCRIPTION.md new file mode 100644 index 00000000000..e91b419860a --- /dev/null +++ b/purchase_start_end_dates/readme/DESCRIPTION.md @@ -0,0 +1,7 @@ +This module adds the fields *Start Date* and *End Date* on purchase order +lines (requied fields when the product has the *Must Have Start and End +Dates* property). You can also define a *Default Start Date* and +*Default End Date* on the purchase order; when set, these dates will be used +by default on new purchase order lines. Upon invoice creation, the values of +the start/end dates of the purchase order line are copied to the start/end +dates of invoice lines. \ No newline at end of file diff --git a/purchase_start_end_dates/static/description/icon.png b/purchase_start_end_dates/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/purchase_start_end_dates/static/description/icon.png differ diff --git a/purchase_start_end_dates/static/description/index.html b/purchase_start_end_dates/static/description/index.html new file mode 100644 index 00000000000..ffc7ec23571 --- /dev/null +++ b/purchase_start_end_dates/static/description/index.html @@ -0,0 +1,432 @@ + + + + + +Purchase Start End Dates + + + +
+

Purchase Start End Dates

+ + +

Beta License: AGPL-3 OCA/purchase-workflow Translate me on Weblate Try me on Runboat

+

This module adds the fields Start Date and End Date on purchase +order lines (requied fields when the product has the Must Have Start +and End Dates property). You can also define a Default Start Date and +Default End Date on the purchase order; when set, these dates will be +used by default on new purchase order lines. Upon invoice creation, the +values of the start/end dates of the purchase order line are copied to +the start/end dates of invoice lines.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • elego Software Solutions GmbH
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

This module is part of the OCA/purchase-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/purchase_start_end_dates/tests/__init__.py b/purchase_start_end_dates/tests/__init__.py new file mode 100644 index 00000000000..627c7aa65ed --- /dev/null +++ b/purchase_start_end_dates/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2022 elego Software Solutions, Germany (https://www.elegosoft.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_purchase_start_end_dates diff --git a/purchase_start_end_dates/tests/test_purchase_start_end_dates.py b/purchase_start_end_dates/tests/test_purchase_start_end_dates.py new file mode 100644 index 00000000000..097f4717f46 --- /dev/null +++ b/purchase_start_end_dates/tests/test_purchase_start_end_dates.py @@ -0,0 +1,144 @@ +# Copyright 2022 elego Software Solutions, Germany (https://www.elegosoft.com) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import datetime + +from odoo import fields +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestPurchaseStartEndDates(TransactionCase): + def setUp(self): + super().setUp() + self.AccountInvoice = self.env["account.move"] + self.partner = self.env.ref("base.res_partner_1") + uom_unit = self.env.ref("uom.product_uom_unit") + self.product_id = self.env["product.product"].create( + { + "name": "Product", + "standard_price": 235.0, + "list_price": 280.0, + "type": "consu", + "uom_id": uom_unit.id, + "uom_po_id": uom_unit.id, + "purchase_method": "purchase", + "default_code": "PRODUCT", + "taxes_id": False, + "must_have_dates": True, + } + ) + self.default_start_date = datetime.datetime.now() + self.default_end_date = self.default_start_date + datetime.timedelta(days=9) + self.po = self.env["purchase.order"].create( + { + "partner_id": self.partner.id, + "default_start_date": self.default_start_date, + "default_end_date": self.default_end_date, + "order_line": [ + ( + 0, + 0, + { + "name": self.product_id.name, + "product_id": self.product_id.id, + "product_qty": 2, + "product_uom": self.product_id.uom_id.id, + "price_unit": self.product_id.list_price, + "date_planned": fields.Datetime.now(), + }, + ) + ], + } + ) + self.po.button_confirm() + for po_lines in self.po.order_line: + po_lines.write( + { + "start_date": self.default_start_date, + "end_date": self.default_end_date, + } + ) + + def test_default_start_date_change(self): + with self.assertRaises(ValidationError): + self.po.write( + { + "default_start_date": self.default_end_date, + "default_end_date": self.default_start_date, + } + ) + self.po.default_start_date_change() + + def test_default_end_date_change(self): + with self.assertRaises(ValidationError): + self.po.write( + { + "default_start_date": self.default_end_date, + "default_end_date": self.default_start_date, + } + ) + self.po.default_end_date_change() + + def test_start_end_dates_product_id_change(self): + if self.po.default_end_date and self.po.default_end_date: + self.po.order_line.start_end_dates_product_id_change() + self.po.order_line.start_date_change() + self.po.order_line.end_date_change() + + def test_start_end_dates_product_id(self): + self.product_id.must_have_dates = False + self.po.default_start_date = self.po.default_end_date = False + self.po.order_line.start_end_dates_product_id_change() + + def test_end_date_change(self): + self.product_id.must_have_dates = False + self.po.order_line.write( + {"start_date": self.default_end_date, "end_date": self.default_start_date} + ) + self.po.order_line.end_date_change() + + def test_start_date_change(self): + self.product_id.must_have_dates = False + self.po.order_line.write( + {"start_date": self.default_end_date, "end_date": self.default_start_date} + ) + self.po.order_line.start_date_change() + + def test_constrains_end_dates(self): + with self.assertRaises(ValidationError): + self.po.order_line.end_date = False + + def test_constrains_start_date(self): + with self.assertRaises(ValidationError): + self.po.order_line.start_date = False + + def test_constrains_greater_st_date(self): + with self.assertRaises(ValidationError): + self.po.order_line.write( + { + "start_date": self.default_end_date, + "end_date": self.default_start_date, + } + ) + + def test_compute_number_of_days(self): + self.assertEqual(self.po.order_line[0].number_of_days, 10) + + def test_inverse_number_of_days(self): + self.po.order_line[0].number_of_days = 1 + self.assertEqual( + self.po.order_line[0].start_date, self.po.order_line[0].end_date + ) + + def test_vendor_bill_start_end_date(self): + action = self.po.action_create_invoice() + self.invoice = self.env["account.move"].browse(action["res_id"]) + self.assertEqual( + self.invoice.invoice_line_ids.mapped("start_date")[0], + self.default_start_date.date(), + ) + self.assertEqual( + self.invoice.invoice_line_ids.mapped("end_date")[0], + self.default_end_date.date(), + ) diff --git a/purchase_start_end_dates/views/purchase_order.xml b/purchase_start_end_dates/views/purchase_order.xml new file mode 100644 index 00000000000..896a1c22e43 --- /dev/null +++ b/purchase_start_end_dates/views/purchase_order.xml @@ -0,0 +1,53 @@ + + + + + + purchase.order.form.start.end.dates. + purchase.order + + + + + + + + + + + + + + + + + + + + + +