diff --git a/purchase_order_line_sequence/__manifest__.py b/purchase_order_line_sequence/__manifest__.py index 8c7bd596963..6ea38e6cda6 100644 --- a/purchase_order_line_sequence/__manifest__.py +++ b/purchase_order_line_sequence/__manifest__.py @@ -7,7 +7,7 @@ "name": "Purchase Order Line Sequence", "summary": "Adds sequence to PO lines and propagates it to" "Invoice lines and Stock Moves", - "version": "16.0.1.0.0", + "version": "17.0.1.0.0", "category": "Purchase Management", "author": "Camptocamp, " "ForgeFlow, " diff --git a/purchase_order_line_sequence/models/purchase.py b/purchase_order_line_sequence/models/purchase.py index 78d4377fdeb..b5c2c58b01e 100644 --- a/purchase_order_line_sequence/models/purchase.py +++ b/purchase_order_line_sequence/models/purchase.py @@ -2,6 +2,7 @@ # Copyright 2017 ForgeFlow S.L. # Copyright 2017 Serpent Consulting Services Pvt. Ltd. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from lxml import etree from odoo import api, fields, models @@ -26,7 +27,7 @@ def _compute_max_line_sequence(self): ) def _create_picking(self): - res = super(PurchaseOrder, self)._create_picking() + res = super()._create_picking() self._update_moves_sequence() return res @@ -46,12 +47,31 @@ def _update_moves_sequence(self): @api.model_create_multi def create(self, vals_list): - res = super(PurchaseOrder, self).create(vals_list) + res = super().create(vals_list) self._update_moves_sequence() return res def write(self, line_values): - res = super(PurchaseOrder, self).write(line_values) + res = super().write(line_values) if "order_line" in line_values: self._update_moves_sequence() return res + + @api.model + def get_view(self, view_id=None, view_type="form", **options): + """Append the default sequence. + + Other modules might want to update the context of `order_line` as well. + This will not scale overwriting the attribute in the view. + """ + res = super().get_view(view_id=view_id, view_type=view_type, **options) + if res.get("arch") and view_type == "form": + doc = etree.XML(res["arch"]) + elements = doc.xpath("//field[@name='order_line']") + if elements: + element = elements[0] + context = element.get("context", "{}") + context = f"{{'default_sequence': max_line_sequence, {context[1:]}" + element.set("context", context) + res["arch"] = etree.tostring(doc, encoding="unicode") + return res diff --git a/purchase_order_line_sequence/models/purchase_line.py b/purchase_order_line_sequence/models/purchase_line.py index 48e0b66c1a8..6348cbb1525 100644 --- a/purchase_order_line_sequence/models/purchase_line.py +++ b/purchase_order_line_sequence/models/purchase_line.py @@ -27,7 +27,7 @@ class PurchaseOrderLine(models.Model): def _compute_visible_sequence(self): for po in self.mapped("order_id"): sequence = 1 - order_lines = po.order_line.filtered(lambda l: not l.display_type) - for line in sorted(order_lines, key=lambda l: l.sequence): + order_lines = po.order_line.filtered(lambda pol: not pol.display_type) + for line in sorted(order_lines, key=lambda pol: pol.sequence): line.visible_sequence = sequence sequence += 1 diff --git a/purchase_order_line_sequence/tests/test_po_lines_sequence.py b/purchase_order_line_sequence/tests/test_po_lines_sequence.py index 8ed3057eff6..d04978308a6 100644 --- a/purchase_order_line_sequence/tests/test_po_lines_sequence.py +++ b/purchase_order_line_sequence/tests/test_po_lines_sequence.py @@ -5,25 +5,25 @@ from datetime import datetime -from odoo.tests import common, tagged -from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT +from odoo.tests import Form, common, tagged @tagged("post_install", "-at_install") class TestPurchaseOrder(common.TransactionCase): - def setUp(self): - super(TestPurchaseOrder, self).setUp() + @classmethod + def setUpClass(cls): + super().setUpClass() # Useful models - self.PurchaseOrder = self.env["purchase.order"] - self.PurchaseOrderLine = self.env["purchase.order.line"] - self.partner_id = self.env.ref("base.res_partner_1") - self.product_id_1 = self.env.ref("product.product_product_8") - self.product_id_2 = self.env.ref("product.product_product_11") + cls.PurchaseOrder = cls.env["purchase.order"] + cls.PurchaseOrderLine = cls.env["purchase.order.line"] + cls.partner_id = cls.env.ref("base.res_partner_1") + cls.product_id_1 = cls.env.ref("product.product_product_8") + cls.product_id_2 = cls.env.ref("product.product_product_11") - self.AccountInvoice = self.env["account.move"] - self.AccountInvoiceLine = self.env["account.move.line"] + cls.AccountInvoice = cls.env["account.move"] + cls.AccountInvoiceLine = cls.env["account.move.line"] - self.category = self.env.ref("product.product_category_1").copy( + cls.category = cls.env.ref("product.product_category_1").copy( { "name": "Test category", "property_valuation": "real_time", @@ -31,7 +31,7 @@ def setUp(self): } ) - self.account_expense = self.env["account.account"].create( + cls.account_expense = cls.env["account.account"].create( { "name": "Expense", "code": "EXP00", @@ -39,7 +39,7 @@ def setUp(self): "reconcile": True, } ) - self.account_payable = self.env["account.account"].create( + cls.account_payable = cls.env["account.account"].create( { "name": "Payable", "code": "PAY00", @@ -48,14 +48,14 @@ def setUp(self): } ) - self.category.property_account_expense_categ_id = self.account_expense + cls.category.property_account_expense_categ_id = cls.account_expense - self.category.property_stock_journal = self.env["account.journal"].create( + cls.category.property_stock_journal = cls.env["account.journal"].create( {"name": "Stock journal", "type": "sale", "code": "STK00"} ) - self.product_id_1.categ_id = self.category - self.product_id_2.categ_id = self.category - self.partner_id.property_account_payable_id = self.account_payable + cls.product_id_1.categ_id = cls.category + cls.product_id_2.categ_id = cls.category + cls.partner_id.property_account_payable_id = cls.account_payable def _create_purchase_order(self): po_vals = { @@ -70,9 +70,7 @@ def _create_purchase_order(self): "product_qty": 5.0, "product_uom": self.product_id_1.uom_po_id.id, "price_unit": 500.0, - "date_planned": datetime.today().strftime( - DEFAULT_SERVER_DATETIME_FORMAT - ), + "date_planned": datetime.today(), }, ), ( @@ -84,9 +82,7 @@ def _create_purchase_order(self): "product_qty": 5.0, "product_uom": self.product_id_2.uom_po_id.id, "price_unit": 250.0, - "date_planned": datetime.today().strftime( - DEFAULT_SERVER_DATETIME_FORMAT - ), + "date_planned": datetime.today(), }, ), ], @@ -130,6 +126,11 @@ def test_purchase_order_line_sequence(self): "The Sequence is not copied properly", ) + po_form = Form(self.po) + with po_form.order_line.new() as po_line_form: + po_line_form.product_id = self.product_id_1 + self.assertEqual(po_line_form.sequence, self.po.max_line_sequence) + def test_purchase_order_line_sequence_with_section_note(self): """ Verify that the sequence is correctly assigned to the move associated @@ -151,9 +152,7 @@ def test_purchase_order_line_sequence_with_section_note(self): "product_qty": 15.0, "product_uom": self.product_id_1.uom_po_id.id, "price_unit": 150.0, - "date_planned": datetime.today().strftime( - DEFAULT_SERVER_DATETIME_FORMAT - ), + "date_planned": datetime.today(), "order_id": po.id, } ) @@ -172,9 +171,7 @@ def test_purchase_order_line_sequence_with_section_note(self): "product_qty": 1.0, "product_uom": self.product_id_2.uom_po_id.id, "price_unit": 50.0, - "date_planned": datetime.today().strftime( - DEFAULT_SERVER_DATETIME_FORMAT - ), + "date_planned": datetime.today(), "order_id": po.id, } ) @@ -206,9 +203,7 @@ def test_write_purchase_order_line(self): "product_qty": 2, "product_uom": self.product_id_2.uom_id.id, "price_unit": 30, - "date_planned": datetime.today().strftime( - DEFAULT_SERVER_DATETIME_FORMAT - ), + "date_planned": datetime.today(), }, ) ] diff --git a/purchase_order_line_sequence/views/account_move_view.xml b/purchase_order_line_sequence/views/account_move_view.xml index 3fc0f3d4e1f..3c755e4bcdf 100644 --- a/purchase_order_line_sequence/views/account_move_view.xml +++ b/purchase_order_line_sequence/views/account_move_view.xml @@ -12,7 +12,7 @@ > diff --git a/purchase_order_line_sequence/views/purchase_view.xml b/purchase_order_line_sequence/views/purchase_view.xml index aa58c360452..c69581049e7 100644 --- a/purchase_order_line_sequence/views/purchase_view.xml +++ b/purchase_order_line_sequence/views/purchase_view.xml @@ -21,10 +21,6 @@ - - {'default_sequence': - max_line_sequence, 'default_state': 'draft'} -