Skip to content

Commit

Permalink
[MIG] purchase_order_line_sequence: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Sep 23, 2024
1 parent c25194c commit 873d829
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion purchase_order_line_sequence/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
26 changes: 23 additions & 3 deletions purchase_order_line_sequence/models/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
4 changes: 2 additions & 2 deletions purchase_order_line_sequence/models/purchase_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 29 additions & 34 deletions purchase_order_line_sequence/tests/test_po_lines_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@

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",
"property_cost_method": "fifo",
}
)

self.account_expense = self.env["account.account"].create(
cls.account_expense = cls.env["account.account"].create(
{
"name": "Expense",
"code": "EXP00",
"account_type": "liability_current",
"reconcile": True,
}
)
self.account_payable = self.env["account.account"].create(
cls.account_payable = cls.env["account.account"].create(
{
"name": "Payable",
"code": "PAY00",
Expand All @@ -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 = {
Expand All @@ -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(),
},
),
(
Expand All @@ -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(),
},
),
],
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
)
Expand All @@ -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,
}
)
Expand Down Expand Up @@ -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(),
},
)
]
Expand Down
2 changes: 1 addition & 1 deletion purchase_order_line_sequence/views/account_move_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
>
<field
name="related_po_sequence"
attrs="{'column_invisible': [('parent.move_type', '!=', 'in_invoice')]}"
column_invisible="parent.move_type != 'in_invoice'"
/>
</xpath>
</field>
Expand Down
4 changes: 0 additions & 4 deletions purchase_order_line_sequence/views/purchase_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
<xpath expr="//field[@name='order_line']" position="before">
<field name="max_line_sequence" invisible="1" />
</xpath>
<xpath expr="//field[@name='order_line']" position="attributes">
<attribute name="context">{'default_sequence':
max_line_sequence, 'default_state': 'draft'}</attribute>
</xpath>
<xpath
expr="//field[@name='order_line']/tree/field[@name='product_id']"
position="before"
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
odoo_test_helper
git+https://github.com/siemenv/[email protected]_picking_line_sequence#subdirectory=setup/stock_picking_line_sequence

0 comments on commit 873d829

Please sign in to comment.