From b8ee3b00a5115738cf8f890d69cb383280b0ee43 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 20 Jun 2023 12:17:27 +0200 Subject: [PATCH] sale_start_end_dates: onchange -> compute methods Improve and update some strings Update FR translation --- sale_start_end_dates/i18n/fr.po | 103 ++++++------- .../i18n/sale_start_end_dates.pot | 26 ++-- sale_start_end_dates/models/sale_order.py | 142 ++++++++++-------- .../tests/test_sale_start_end_dates.py | 130 ++++++---------- 4 files changed, 196 insertions(+), 205 deletions(-) diff --git a/sale_start_end_dates/i18n/fr.po b/sale_start_end_dates/i18n/fr.po index 49a51f4c7e7..1527671d17d 100644 --- a/sale_start_end_dates/i18n/fr.po +++ b/sale_start_end_dates/i18n/fr.po @@ -1,23 +1,20 @@ # Translation of Odoo Server. # This file contains the translation of the following modules: -# * sale_start_end_dates +# * sale_start_end_dates # -# Translators: -# guillaume bauer , 2017 -# OCA Transbot , 2017 msgid "" msgstr "" -"Project-Id-Version: Odoo Server 10.0\n" +"Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-11-23 01:51+0000\n" -"PO-Revision-Date: 2017-11-23 01:51+0000\n" -"Last-Translator: OCA Transbot , 2017\n" -"Language-Team: French (https://www.transifex.com/oca/teams/23907/fr/)\n" +"POT-Creation-Date: 2023-06-20 10:09+0000\n" +"PO-Revision-Date: 2023-06-20 10:10+0000\n" +"Last-Translator: Alexis de Lattre \n" +"Language-Team: \n" "Language: fr\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: \n" -"Plural-Forms: nplurals=2; plural=(n > 1);\n" +"Plural-Forms: \n" #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order__default_end_date @@ -30,17 +27,16 @@ msgid "Default Start Date" msgstr "Date de début par défaut" #. module: sale_start_end_dates -#: code:addons/sale_start_end_dates/models/sale_order.py:21 -#, fuzzy, python-format -#| msgid "" -#| "Default Start Date should be before or be the same as Default End Date " -#| "for sale order %s" +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 +#, python-format msgid "" -"Default Start Date should be before or be the same as Default End Date for " -"sale order '%s'." +"Default Start Date (%(start_date)s) should be before or be the same as " +"Default End Date (%(end_date)s) for sale order '%(name)s'." msgstr "" -"La date de début par défaut doit être antérieure ou identique à la date de " -"fin par défaut du bon de commande %s" +"La date de début par défaut (%(start_date)s) doit être antérieure ou " +"identique à la date de fin par défaut (%(end_date)s) pour la commande client " +"\"%(name)s\"." #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order_line__end_date @@ -54,45 +50,56 @@ msgid "" "End Date on the invoice lines that have this product." msgstr "" "Si cette option est activée, l'utilisateur devra saisir une date de début et " -"une date de fin sur les lignes de facture comportant ce produit." +"une date de fin sur les lignes de facture comportant cet article." #. module: sale_start_end_dates -#: code:addons/sale_start_end_dates/models/sale_order.py:86 +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "Missing End Date for sale order line with Product '%s'." msgstr "" -"Un bon de commande avec ligne article saisie doit comporter une date de fin " -"'%s'." +"Date de fin manquante sur la ligne de commande ayant l'article '%s'." #. module: sale_start_end_dates -#: code:addons/sale_start_end_dates/models/sale_order.py:90 +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "Missing Start Date for sale order line with Product '%s'." msgstr "" -"Un bon de commande avec ligne article saisie doit comporter une date de " -"début '%s'." +"Date de début manquante sur la ligne de commande ayant l'article '%s'." #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order_line__must_have_dates -msgid "Must Have Start and End Dates" -msgstr "Doit comporter une date de début et de fin" +msgid "Must Have Start/End Dates" +msgstr "Dates de début et de fin obligatoires" #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order_line__number_of_days msgid "Number of Days" msgstr "Nombre de jours" +#. module: sale_start_end_dates +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 +#, python-format +msgid "" +"On sale 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." +msgstr "" +"Sur la ligne de commande ayant l'article '%(product_name)s', le nombre " +"de jours est négatif (%(number_of_days)s) ; ceci n'est pas autorisé. Le " +"nombre de jours a été forcé à 1." + #. module: sale_start_end_dates #: model:ir.model,name:sale_start_end_dates.model_sale_order -#, fuzzy -#| msgid "Sales Order" -msgid "Sale Order" +msgid "Sales Order" msgstr "Bon de commande" #. module: sale_start_end_dates #: model:ir.model,name:sale_start_end_dates.model_sale_order_line msgid "Sales Order Line" -msgstr "Sales Order Line" +msgstr "Ligne de bons de commande" #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order_line__start_date @@ -100,30 +107,20 @@ msgid "Start Date" msgstr "Date de début" #. module: sale_start_end_dates -#: code:addons/sale_start_end_dates/models/sale_order.py:94 +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "" -"Start Date should be before or be the same as End Date for sale order line " -"with Product '%s'." +"Start Date (%(start_date)s) should be before or be the same as End Date " +"(%(end_date)s) for sale order line with Product '%(product_name)s'." msgstr "" +"La date de début (%(start_date)s) doit être antérieure ou identique à la " +"date de fin (%(end_date)s) pour la ligne de commande client ayant l'article " +"'%(product_name)s'." #. module: sale_start_end_dates -#: code:addons/sale_start_end_dates/models/sale_order.py:67 -#, fuzzy, python-format -#| msgid "Number of Days" +#. odoo-python +#: code:addons/sale_start_end_dates/models/sale_order.py:0 +#, python-format msgid "Wrong number of days" -msgstr "Nombre de jours" - -#~ msgid "Missing number of days for sale order line with Product '%s'." -#~ msgstr "" -#~ "Un bon de commande avec ligne article saisie doit comporter un nombre de " -#~ "jours '%s'." - -#~ msgid "" -#~ "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." -#~ msgstr "" -#~ "Bon de commande avec ligne article saisie'%s', il y a %d jours entre la " -#~ "date de début (%s) et la date de fin (%s), mais le nombre de jours de " -#~ "champ a une valeur de %d jours." +msgstr "Nombre de jours erroné" diff --git a/sale_start_end_dates/i18n/sale_start_end_dates.pot b/sale_start_end_dates/i18n/sale_start_end_dates.pot index 5bc7c0950ef..8d67bb1445c 100644 --- a/sale_start_end_dates/i18n/sale_start_end_dates.pot +++ b/sale_start_end_dates/i18n/sale_start_end_dates.pot @@ -4,8 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-06-20 10:07+0000\n" +"PO-Revision-Date: 2023-06-20 10:07+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -24,11 +26,12 @@ msgid "Default Start Date" msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "" -"Default Start Date ({start_date}) should be before or be the same as Default" -" End Date ({end_date}) for sale order '{name}'." +"Default Start Date (%(start_date)s) should be before or be the same as " +"Default End Date (%(end_date)s) for sale order '%(name)s'." msgstr "" #. module: sale_start_end_dates @@ -40,16 +43,18 @@ msgstr "" #: model:ir.model.fields,help:sale_start_end_dates.field_sale_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/move lines that have this product." +"End Date on the invoice lines that have this product." msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "Missing End Date for sale order line with Product '%s'." msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "Missing Start Date for sale order line with Product '%s'." @@ -62,15 +67,16 @@ msgstr "" #. module: sale_start_end_dates #: model:ir.model.fields,field_description:sale_start_end_dates.field_sale_order_line__number_of_days -msgid "Number Of Days" +msgid "Number of Days" msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "" -"On sale order line with product '{product_name}', the number of days is " -"negative ({number_of_days}) ; this is not allowed. The number of days has " +"On sale 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." msgstr "" @@ -90,14 +96,16 @@ msgid "Start Date" msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "" -"Start Date ({start_date}) should be before or be the same as End Date " -"({end_date}) for sale order line with Product '{product_name}'." +"Start Date (%(start_date)s) should be before or be the same as End Date " +"(%(end_date)s) for sale order line with Product '%(product_name)s'." msgstr "" #. module: sale_start_end_dates +#. odoo-python #: code:addons/sale_start_end_dates/models/sale_order.py:0 #, python-format msgid "Wrong number of days" diff --git a/sale_start_end_dates/models/sale_order.py b/sale_start_end_dates/models/sale_order.py index 2803b37231e..0acd7cdacd6 100644 --- a/sale_start_end_dates/models/sale_order.py +++ b/sale_start_end_dates/models/sale_order.py @@ -13,8 +13,12 @@ class SaleOrder(models.Model): _inherit = "sale.order" - default_start_date = fields.Date() - default_end_date = fields.Date() + default_start_date = fields.Date( + compute="_compute_default_start_date", readonly=False, store=True + ) + default_end_date = fields.Date( + compute="_compute_default_end_date", readonly=False, store=True + ) @api.constrains("default_start_date", "default_end_date") def _check_default_start_end_dates(self): @@ -26,43 +30,48 @@ def _check_default_start_end_dates(self): ): raise ValidationError( _( - "Default Start Date ({start_date}) should be before or be the " - "same as Default End Date ({end_date}) for sale order '{name}'." - ).format( - start_date=format_date(self.env, order.default_start_date), - end_date=format_date(self.env, order.default_end_date), - name=order.display_name, + "Default Start Date (%(start_date)s) should be before or be the " + "same as Default End Date (%(end_date)s) for sale order '%(name)s'." ) + % { + "start_date": format_date(self.env, order.default_start_date), + "end_date": format_date(self.env, order.default_end_date), + "name": order.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 + @api.depends("default_end_date") + def _compute_default_start_date(self): + for order in self: + if ( + order.default_start_date + and order.default_end_date + and order.default_start_date > order.default_end_date + ): + order.default_start_date = order.default_end_date + + @api.depends("default_start_date") + def _compute_default_end_date(self): + for order in self: + if ( + order.default_start_date + and order.default_end_date + and order.default_start_date > order.default_end_date + ): + order.default_end_date = order.default_start_date class SaleOrderLine(models.Model): _inherit = "sale.order.line" start_date = fields.Date( - readonly=True, + compute="_compute_start_date", + store=True, states={"draft": [("readonly", False)], "sent": [("readonly", False)]}, ) end_date = fields.Date( - readonly=True, + compute="_compute_end_date", + store=True, states={"draft": [("readonly", False)], "sent": [("readonly", False)]}, ) number_of_days = fields.Integer( @@ -70,6 +79,7 @@ class SaleOrderLine(models.Model): inverse="_inverse_number_of_days", readonly=False, store=True, + string="Number of Days", ) must_have_dates = fields.Boolean(related="product_id.must_have_dates") @@ -88,13 +98,13 @@ def _inverse_number_of_days(self): if line.number_of_days < 0: res["warning"]["title"] = _("Wrong number of days") res["warning"]["message"] = _( - "On sale order line with product '{product_name}', the " - "number of days is negative ({number_of_days}) ; this is not " + "On sale 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." - ).format( - product_name=line.product_id.display_name, - number_of_days=line.number_of_days, - ) + ) % { + "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( @@ -126,14 +136,15 @@ def _check_start_end_dates(self): if line.start_date > line.end_date: raise ValidationError( _( - "Start Date ({start_date}) should be before or " - "be the same as End Date ({end_date}) for sale order line " - "with Product '{product_name}'." - ).format( - start_date=format_date(self.env, line.start_date), - end_date=format_date(self.env, line.end_date), - product_name=line.product_id.display_name, + "Start Date (%(start_date)s) should be before or " + "be the same as End Date (%(end_date)s) for sale order line " + "with Product '%(product_name)s'." ) + % { + "start_date": format_date(self.env, line.start_date), + "end_date": format_date(self.env, line.end_date), + "product_name": line.product_id.display_name, + } ) def _prepare_invoice_line(self, **optional_values): @@ -143,25 +154,32 @@ def _prepare_invoice_line(self, **optional_values): 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 - - @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 - if self.order_id.default_end_date: - self.end_date = self.order_id.default_end_date - else: - self.start_date = False - self.end_date = False + @api.depends("end_date", "product_id") + def _compute_start_date(self): + for line in self: + order = line.order_id + if ( + not line.start_date + and line.product_id.must_have_dates + and order.default_start_date + ): + line.start_date = order.default_start_date + elif line.start_date and not line.product_id.must_have_dates: + line.start_date = False + elif line.end_date and line.start_date and line.start_date > self.end_date: + line.start_date = line.end_date + + @api.depends("start_date", "product_id") + def _compute_end_date(self): + for line in self: + order = line.order_id + if ( + not line.end_date + and line.product_id.must_have_dates + and order.default_end_date + ): + line.end_date = order.default_end_date + elif line.end_date and not line.product_id.must_have_dates: + line.end_date = False + elif line.end_date and line.start_date and line.start_date > self.end_date: + line.end_date = line.start_date diff --git a/sale_start_end_dates/tests/test_sale_start_end_dates.py b/sale_start_end_dates/tests/test_sale_start_end_dates.py index fec6a520fb6..7e929ee0079 100644 --- a/sale_start_end_dates/tests/test_sale_start_end_dates.py +++ b/sale_start_end_dates/tests/test_sale_start_end_dates.py @@ -11,8 +11,12 @@ class TestSaleStartEndDates(TransactionCase): def setUp(self): super().setUp() self.partner = self.env.ref("base.res_partner_3") - self.product_id = self.env.ref("product.product_product_7") - self.product_id.must_have_dates = True + self.product_id = self.env.ref( + "account_invoice_start_end_dates.product_insurance_contract_demo" + ) + self.assertTrue(self.product_id.must_have_dates) + self.product_no_dates = self.env.ref("product.product_product_7") + self.assertFalse(self.product_no_dates.must_have_dates) self.default_start_date = datetime.datetime.now() self.default_end_date = self.default_start_date + datetime.timedelta(days=9) self.so = self.env["sale.order"].create( @@ -23,26 +27,23 @@ def setUp(self): "default_start_date": self.default_start_date, "default_end_date": self.default_end_date, "pricelist_id": self.env.ref("product.list0").id, - "order_line": [ - ( - 0, - 0, - { - "name": self.product_id.name, - "product_id": self.product_id.id, - "product_uom_qty": 2, - "product_uom": self.product_id.uom_id.id, - "price_unit": self.product_id.list_price, - "start_date": self.default_start_date, - "end_date": self.default_end_date, - }, - ) - ], + } + ) + self.order_line = self.env["sale.order.line"].create( + { + "order_id": self.so.id, + "name": self.product_id.display_name, + "product_id": self.product_id.id, + "product_uom_qty": 2, + "product_uom": self.product_id.uom_id.id, + "price_unit": self.product_id.list_price, + "start_date": self.default_start_date, + "end_date": self.default_end_date, } ) self.so.action_confirm() - def test_default_start_date_change(self): + def test_default_start_end_date_constraint(self): with self.assertRaises(ValidationError): self.so.write( { @@ -50,77 +51,44 @@ def test_default_start_date_change(self): "default_end_date": self.default_start_date, } ) - self.so.default_start_date_change() - def test_default_end_date_change(self): - with self.assertRaises(ValidationError): - self.so.write( - { - "default_start_date": self.default_end_date, - "default_end_date": self.default_start_date, - } - ) - self.so.default_end_date_change() - - def test_start_end_dates_product_id_change(self): - if self.so.default_end_date and self.so.default_end_date: - self.so.order_line.start_end_dates_product_id_change() - self.so.order_line.start_date_change() - self.so.order_line.end_date_change() - self.product_id.must_have_dates = False - self.so.order_line.start_end_dates_product_id_change() - - def test_start_end_dates_product_id(self): - self.product_id.must_have_dates = False - self.so.default_start_date = self.so.default_end_date = False - self.so.order_line.start_end_dates_product_id_change() - - def test_end_date_change(self): - self.product_id.must_have_dates = False - self.so.order_line.write( - {"start_date": self.default_end_date, "end_date": self.default_start_date} + def test_compute_start_end_date(self): + new_line = self.env["sale.order.line"].create( + { + "product_id": self.product_id.id, + "name": self.product_id.display_name, + "product_uom_qty": 2, + "product_uom": self.product_id.uom_id.id, + "order_id": self.so.id, + } ) - self.so.order_line.end_date_change() + self.assertEqual(new_line.start_date, self.so.default_start_date) + self.assertEqual(new_line.end_date, self.so.default_end_date) - def test_start_date_change(self): - self.product_id.must_have_dates = False - self.so.order_line.write( - {"start_date": self.default_end_date, "end_date": self.default_start_date} + def test_compute_product_no_dates(self): + new_line = self.env["sale.order.line"].create( + { + "product_id": self.product_no_dates.id, + "name": self.product_no_dates.display_name, + "product_uom_qty": 2, + "product_uom": self.product_no_dates.uom_id.id, + "order_id": self.so.id, + } ) - self.so.order_line.start_date_change() - - def test_constrains_end_dates(self): - with self.assertRaises(ValidationError): - self.so.order_line.end_date = False - - def test_constrains_start_date(self): - with self.assertRaises(ValidationError): - self.so.order_line.start_date = False - - def test_constrains_greater_st_date(self): - with self.assertRaises(ValidationError): - self.so.order_line.write( - { - "start_date": self.default_end_date, - "end_date": self.default_start_date, - } - ) + self.assertFalse(new_line.start_date) + self.assertFalse(new_line.end_date) def test_compute_number_of_days(self): - self.assertEqual(self.so.order_line[0].number_of_days, 10) + self.assertEqual(self.order_line.number_of_days, 10) def test_inverse_number_of_days(self): - self.so.order_line[0].number_of_days = 1 - self.assertEqual( - self.so.order_line[0].start_date, self.so.order_line[0].end_date - ) - self.so.order_line[0].number_of_days = -1 - self.assertEqual(self.so.order_line[0].number_of_days, 1) + self.order_line.number_of_days = 1 + self.assertEqual(self.order_line.start_date, self.order_line.end_date) + self.order_line.number_of_days = -1 + self.assertEqual(self.order_line.number_of_days, 1) def test_prepare_invoice_line(self): - invoice_line_vals = self.so.order_line._prepare_invoice_line() + invoice_line_vals = self.order_line._prepare_invoice_line() self.assertEqual(invoice_line_vals["product_id"], self.product_id.id) - self.assertEqual( - invoice_line_vals["start_date"], self.so.order_line[0].start_date - ) - self.assertEqual(invoice_line_vals["end_date"], self.so.order_line[0].end_date) + self.assertEqual(invoice_line_vals["start_date"], self.order_line.start_date) + self.assertEqual(invoice_line_vals["end_date"], self.order_line.end_date)