Skip to content

Commit 33bab4e

Browse files
[MIG] purchase_stock_operating_unit: Migration to 18.0
1 parent b29632c commit 33bab4e

File tree

9 files changed

+105
-34
lines changed

9 files changed

+105
-34
lines changed

purchase_stock_operating_unit/README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Contributors
6969
------------
7070

7171
- Saran Lim. <saranl@ecosoft.co.th>
72+
- `Heliconia Solutions Pvt. Ltd. <https://www.heliconia.io>`__
73+
74+
- Bhavesh Heliconia
7275

7376
Maintainers
7477
-----------

purchase_stock_operating_unit/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Purchase Stock Operating Unit",
66
"summary": "Copies the operating unit of purchase picking to the stock picking",
7-
"version": "15.0.1.0.0",
7+
"version": "18.0.1.0.0",
88
"author": "Ecosoft, Odoo Community Association (OCA)",
99
"website": "https://github.com/OCA/operating-unit",
1010
"category": "Purchase Management",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
22

3-
from . import purchase_order_line
43
from . import purchase
4+
from . import stock_rule
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
11
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from odoo import _, api, models
4+
from odoo import api, fields, models
55
from odoo.exceptions import UserError
66

77

88
class PurchaseOrder(models.Model):
99
_inherit = "purchase.order"
1010

11+
picking_type_id = fields.Many2one(
12+
compute="_compute_picking_type_id", store=True, readonly=False
13+
)
14+
operating_unit_id = fields.Many2one(
15+
compute="_compute_operating_unit_id",
16+
store=True,
17+
readonly=False,
18+
)
19+
20+
@api.depends("operating_unit_id")
21+
def _compute_picking_type_id(self):
22+
for purchase in self:
23+
if purchase.operating_unit_id:
24+
purchase.picking_type_id = self.env["stock.picking.type"].search(
25+
[
26+
(
27+
"warehouse_id.operating_unit_id",
28+
"=",
29+
purchase.operating_unit_id.id,
30+
),
31+
("code", "=", "incoming"),
32+
],
33+
limit=1,
34+
)
35+
36+
@api.depends("picking_type_id")
37+
def _compute_operating_unit_id(self):
38+
for purchase in self:
39+
if purchase.picking_type_id:
40+
purchase.operating_unit_id = (
41+
purchase.picking_type_id.warehouse_id.operating_unit_id
42+
)
43+
1144
@api.constrains("operating_unit_id", "picking_type_id")
1245
def _check_operating_unit_picking_type(self):
1346
for rec in self:
1447
if (
1548
rec.operating_unit_id
49+
and rec.picking_type_id.warehouse_id.operating_unit_id
1650
and rec.operating_unit_id
1751
!= rec.picking_type_id.warehouse_id.operating_unit_id
1852
):
1953
raise UserError(
20-
_(
54+
self.env._(
2155
"Configuration error. The Operating Unit in "
2256
"the Purchase and Deliver To must be the same."
2357
)
2458
)
59+
60+
def _prepare_picking(self):
61+
vals = super()._prepare_picking()
62+
vals["operating_unit_id"] = self.operating_unit_id.id
63+
return vals

purchase_stock_operating_unit/models/purchase_order_line.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
4+
from odoo import models
5+
6+
7+
class StockRule(models.Model):
8+
_inherit = "stock.rule"
9+
10+
def _prepare_purchase_order(self, company_id, origins, values):
11+
res = super()._prepare_purchase_order(company_id, origins, values)
12+
if self.operating_unit_id:
13+
res["operating_unit_id"] = self.operating_unit_id.id
14+
return res
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
- Saran Lim. \<<saranl@ecosoft.co.th>\>
2+
- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io)
3+
- Bhavesh Heliconia

purchase_stock_operating_unit/static/description/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ <h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
416416
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
417417
<ul class="simple">
418418
<li>Saran Lim. &lt;<a class="reference external" href="mailto:saranl&#64;ecosoft.co.th">saranl&#64;ecosoft.co.th</a>&gt;</li>
419+
<li><a class="reference external" href="https://www.heliconia.io">Heliconia Solutions Pvt. Ltd.</a><ul>
420+
<li>Bhavesh Heliconia</li>
421+
</ul>
422+
</li>
419423
</ul>
420424
</div>
421425
<div class="section" id="maintainers">
Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from odoo.exceptions import UserError
5-
from odoo.tests import Form
4+
from odoo.models import Command
65

76
from odoo.addons.purchase_operating_unit.tests.test_purchase_operating_unit import (
87
TestPurchaseOperatingUnit,
98
)
109

1110

1211
class TestPurchaseStockOperatingUnit(TestPurchaseOperatingUnit):
13-
def setUp(self):
14-
super().setUp()
15-
self.warehouse_b2b = self.env.ref("stock_operating_unit.stock_warehouse_b2b")
16-
self.picking_type2 = self.env["stock.picking.type"].search(
12+
@classmethod
13+
def setUpClass(cls):
14+
super().setUpClass()
15+
cls.warehouse_b2b = cls.env.ref("stock_operating_unit.stock_warehouse_b2b")
16+
cls.picking_type2 = cls.env["stock.picking.type"].search(
1717
[
1818
("code", "=", "incoming"),
19-
("warehouse_id", "=", self.warehouse_b2b.id),
19+
("warehouse_id", "=", cls.warehouse_b2b.id),
2020
],
2121
limit=1,
2222
)
23-
# Add permission b2b operating unit in user1
24-
self.b2b = self.env.ref("operating_unit.b2b_operating_unit")
25-
user = self.env["res.users"].browse(self.user1_id)
26-
user.operating_unit_ids = [(4, self.b2b.id)]
23+
cls.user1.write(
24+
{
25+
"operating_unit_ids": [
26+
Command.link(cls.b2b.id),
27+
],
28+
}
29+
)
2730

2831
def test_01_purchase_stock_operating_unit(self):
2932
self.assertEqual(self.purchase1.state, "purchase")
3033
self.assertEqual(
3134
self.purchase1.picking_ids.operating_unit_id,
3235
self.purchase1.picking_type_id.warehouse_id.operating_unit_id,
3336
)
37+
# Ensure that in case of picking type changes,
38+
# the operating unit is also updated.
3439
self.purchase1.button_cancel()
3540
self.purchase1.button_draft()
36-
# Check change picking type is not equal operating unit, it should error
37-
with self.assertRaises(UserError):
38-
with Form(self.purchase1) as po:
39-
po.picking_type_id = self.picking_type2
41+
self.purchase1.picking_type_id = self.picking_type2
42+
self.assertEqual(
43+
self.purchase1.operating_unit_id,
44+
self.purchase1.picking_type_id.warehouse_id.operating_unit_id,
45+
)
46+
47+
def test_02_compute_picking_type_id(self):
48+
self.purchase1.operating_unit_id = self.b2b
49+
self.purchase1._compute_picking_type_id()
50+
expected_picking_type = self.env["stock.picking.type"].search(
51+
[
52+
("warehouse_id.operating_unit_id", "=", self.b2b.id),
53+
("code", "=", "incoming"),
54+
],
55+
limit=1,
56+
)
57+
self.assertEqual(self.purchase1.picking_type_id, expected_picking_type)
58+
59+
def test_03_prepare_picking(self):
60+
picking_vals = self.purchase1._prepare_picking()
61+
self.assertEqual(
62+
picking_vals.get("operating_unit_id"), self.purchase1.operating_unit_id.id
63+
)

0 commit comments

Comments
 (0)