Skip to content

Commit a30ce75

Browse files
[MIG] stock_intercompany: Migration to 17.0
1 parent 9a036b1 commit a30ce75

7 files changed

Lines changed: 138 additions & 95 deletions

File tree

stock_intercompany/README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ Contributors
7979
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
8080
- Yannick Vaucher <yannick.vaucher@camptocamp.com>
8181
- Italo Lopes <italo.lopes@camptocamp.com>
82+
- `Heliconia Solutions Pvt. Ltd. <https://www.heliconia.io>`__
83+
84+
- Bhavesh Heliconia
8285

8386
Maintainers
8487
-----------

stock_intercompany/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Stock Intercompany Delivery-Reception",
66
"Summary": "Module that adds possibility for intercompany Delivery-Reception",
7-
"version": "16.0.1.0.2",
7+
"version": "17.0.1.0.0",
88
"author": "Camptocamp, Odoo Community Association (OCA)",
99
"website": "https://github.com/OCA/multi-company",
1010
"category": "Warehouse Management",

stock_intercompany/models/stock_picking.py

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from odoo import fields, models
1+
from odoo import Command, fields, models
22

33

44
class StockPicking(models.Model):
@@ -12,71 +12,92 @@ def _create_counterpart_picking(self):
1212
picking = self.env["stock.picking"]
1313
if self.partner_id in partners:
1414
company = partners[self.partner_id]
15+
# Switch to target company context before creating picking
16+
picking = self.env["stock.picking"].sudo().with_company(company)
1517
vals = self._get_counterpart_picking_vals(company)
16-
new_picking_vals = self.sudo().copy_data(default=vals)
17-
picking = (
18-
self.env["stock.picking"]
19-
.sudo()
20-
.with_company(company)
21-
.create(new_picking_vals)
22-
)
18+
# Create picking in correct company context
19+
picking = picking.create(vals)
20+
# Confirm picking in the same company context
2321
picking.action_confirm()
2422
return picking
2523

2624
def _get_counterpart_picking_vals(self, company):
27-
if company.intercompany_in_type_id.warehouse_id:
28-
warehouse = company.intercompany_in_type_id.warehouse_id
29-
else:
30-
warehouse = (
31-
self.env["stock.warehouse"]
32-
.sudo()
33-
.search([("company_id", "=", company.id)], limit=1)
34-
)
35-
ptype = company.intercompany_in_type_id or warehouse.in_type_id
36-
move_ids, move_line_ids = self._check_company_consistency(company)
25+
# Get warehouse and picking type in correct company context
26+
warehouse = False
27+
with_company = self.env["stock.warehouse"].sudo().with_company(company)
28+
ptype = False
29+
30+
if company.intercompany_in_type_id:
31+
ptype = company.intercompany_in_type_id
32+
if ptype.warehouse_id:
33+
warehouse = ptype.warehouse_id
34+
35+
if not warehouse:
36+
warehouse = with_company.search([("company_id", "=", company.id)], limit=1)
37+
38+
if not ptype:
39+
ptype = warehouse.in_type_id
40+
41+
# Ensure locations belong to correct company
42+
location_dest = ptype.default_location_dest_id or warehouse.lot_stock_id
43+
supplier_location = self.env.ref("stock.stock_location_suppliers")
44+
45+
move_ids, move_line_ids = self._check_company_consistency(company, ptype)
46+
3747
return {
3848
"partner_id": self.env.user.company_id.partner_id.id,
3949
"company_id": company.id,
4050
"origin": self.name,
4151
"picking_type_id": ptype.id,
4252
"state": "draft",
43-
"location_id": self.env.ref("stock.stock_location_suppliers").id,
44-
"location_dest_id": ptype.default_location_dest_id.id
45-
or warehouse.lot_stock_id.id,
53+
"location_id": supplier_location.id,
54+
"location_dest_id": location_dest.id,
4655
"counterpart_of_picking_id": self.id,
4756
"move_ids": move_ids,
4857
"move_line_ids": move_line_ids,
58+
"scheduled_date": self.scheduled_date,
59+
"priority": self.priority,
4960
}
5061

51-
def _check_company_consistency(self, company):
52-
# Replace company_id by the right one to create the counterpart picking
62+
def _check_company_consistency(self, company, picking_type):
63+
# Ensure supplier location has no company set
64+
supplier_location = self.env.ref("stock.stock_location_suppliers")
65+
if supplier_location.company_id:
66+
supplier_location.sudo().company_id = False
67+
5368
common_vals = {
5469
"company_id": company.id,
55-
"location_id": self.env.ref("stock.stock_location_suppliers").id,
70+
"location_id": supplier_location.id,
71+
"picking_type_id": picking_type.id,
5672
}
57-
move_ids = [
58-
(
59-
0,
60-
0,
61-
sm.with_company(company).copy_data(
62-
dict(common_vals, counterpart_of_move_id=sm.id)
63-
)[0],
64-
)
65-
for sm in self.move_ids.sudo()
66-
]
67-
move_line_ids = [
68-
(
69-
0,
70-
0,
71-
ln.with_company(company).copy_data(
72-
dict(common_vals, move_id=False, counterpart_of_line_id=ln.id)
73-
)[0],
74-
)
75-
for ln in self.move_line_ids.sudo()
76-
]
73+
74+
# Create moves with correct company context
75+
move_ids = []
76+
for sm in self.move_ids.sudo():
77+
move_vals = sm.with_company(company).copy_data(
78+
dict(
79+
common_vals,
80+
counterpart_of_move_id=sm.id,
81+
picking_type_id=picking_type.id,
82+
)
83+
)[0]
84+
move_ids.append(Command.create(move_vals))
85+
86+
# Create move lines with correct company context
87+
move_line_ids = []
88+
for ln in self.move_line_ids.sudo():
89+
line_vals = ln.with_company(company).copy_data(
90+
dict(
91+
common_vals,
92+
move_id=False,
93+
counterpart_of_line_id=ln.id,
94+
picking_type_id=picking_type.id,
95+
)
96+
)[0]
97+
move_line_ids.append(Command.create(line_vals))
98+
7799
return move_ids, move_line_ids
78100

79-
# override of method from stock module
80101
def _action_done(self):
81102
counterparts = []
82103
for picking in self:

stock_intercompany/readme/CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
- Alexandre Fayolle \<<alexandre.fayolle@camptocamp.com>\>
33
- Yannick Vaucher \<<yannick.vaucher@camptocamp.com>\>
44
- Italo Lopes \<<italo.lopes@camptocamp.com>\>
5+
- [Heliconia Solutions Pvt. Ltd.](https://www.heliconia.io)
6+
- Bhavesh Heliconia

stock_intercompany/static/description/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
425425
<li>Alexandre Fayolle &lt;<a class="reference external" href="mailto:alexandre.fayolle&#64;camptocamp.com">alexandre.fayolle&#64;camptocamp.com</a>&gt;</li>
426426
<li>Yannick Vaucher &lt;<a class="reference external" href="mailto:yannick.vaucher&#64;camptocamp.com">yannick.vaucher&#64;camptocamp.com</a>&gt;</li>
427427
<li>Italo Lopes &lt;<a class="reference external" href="mailto:italo.lopes&#64;camptocamp.com">italo.lopes&#64;camptocamp.com</a>&gt;</li>
428+
<li><a class="reference external" href="https://www.heliconia.io">Heliconia Solutions Pvt. Ltd.</a><ul>
429+
<li>Bhavesh Heliconia</li>
430+
</ul>
431+
</li>
428432
</ul>
429433
</div>
430434
<div class="section" id="maintainers">
Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,93 @@
11
# Copyright 2021 Camptocamp
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
33

4-
from odoo.tests.common import RecordCapturer, TransactionCase
4+
from odoo import Command
5+
from odoo.tests.common import RecordCapturer
56

7+
from odoo.addons.base.tests.common import BaseCommon
68

7-
class TestIntercompanyDelivery(TransactionCase):
8-
def setUp(self):
9-
super().setUp()
10-
self.user_demo = self.env["res.users"].create(
9+
10+
class TestIntercompanyDelivery(BaseCommon):
11+
@classmethod
12+
def setUpClass(cls):
13+
super().setUpClass()
14+
company_obj = cls.env["res.company"]
15+
cls.company1 = company_obj.create({"name": "Company A"})
16+
cls.company2 = company_obj.create({"name": "Company B"})
17+
cls.user_demo = cls.env["res.users"].create(
1118
{
1219
"login": "firstnametest",
1320
"name": "User Demo",
1421
"email": "firstnametest@example.org",
22+
"company_id": cls.company1.id,
23+
"company_ids": [
24+
Command.link(cls.company1.id),
25+
Command.link(cls.company2.id),
26+
],
1527
"groups_id": [
16-
(4, self.env.ref("base.group_user").id),
17-
(4, self.env.ref("stock.group_stock_user").id),
28+
Command.link(cls.env.ref("base.group_user").id),
29+
Command.link(cls.env.ref("stock.group_stock_user").id),
1830
],
1931
}
2032
)
21-
company_obj = self.env["res.company"]
22-
# Create 2 companies and configure intercompany picking type param on them
23-
self.company1 = company_obj.create({"name": "Company A"})
24-
self.company2 = company_obj.create({"name": "Company B"})
25-
self.picking_type_1 = (
26-
self.env["stock.picking.type"]
33+
cls.picking_type_1 = (
34+
cls.env["stock.picking.type"]
2735
.sudo()
2836
.search(
2937
[
30-
("company_id", "=", self.company1.id),
38+
("company_id", "=", cls.company1.id),
3139
("name", "=", "Delivery Orders"),
3240
],
3341
limit=1,
3442
)
3543
)
36-
self.picking_type_2 = (
37-
self.env["stock.picking.type"]
44+
cls.picking_type_2 = (
45+
cls.env["stock.picking.type"]
3846
.sudo()
3947
.search(
40-
[("company_id", "=", self.company2.id), ("name", "=", "Receipts")],
48+
[
49+
("company_id", "=", cls.company2.id),
50+
("name", "=", "Receipts"),
51+
],
4152
limit=1,
4253
)
4354
)
4455

45-
self.company1.intercompany_in_type_id = self.picking_type_1.id
46-
self.company2.intercompany_in_type_id = self.picking_type_2.id
47-
# assign both companies to current user
48-
self.user_demo.write(
56+
cls.company1.intercompany_in_type_id = cls.picking_type_1.id
57+
cls.company2.intercompany_in_type_id = cls.picking_type_2.id
58+
cls.user_demo.write(
4959
{
50-
"company_id": self.company1.id,
51-
"company_ids": [(4, self.company1.id), (4, self.company2.id)],
60+
"company_id": cls.company1.id,
61+
"company_ids": [
62+
Command.link(cls.company1.id),
63+
Command.link(cls.company2.id),
64+
],
5265
}
5366
)
54-
# create storable product
55-
self.product1 = self.env["product.product"].create(
67+
cls.user_demo.partner_id.with_context(
68+
allowed_company_ids=[cls.company1.id, cls.company2.id]
69+
).write({})
70+
71+
cls.product1 = cls.env["product.product"].create(
5672
{
5773
"name": "Product A",
5874
"type": "product",
59-
"categ_id": self.env.ref("product.product_category_all").id,
75+
"categ_id": cls.env.ref("product.product_category_all").id,
6076
"qty_available": 100,
6177
}
6278
)
63-
self.stock_location = (
64-
self.env["stock.location"]
79+
cls.stock_location = (
80+
cls.env["stock.location"]
6581
.sudo()
66-
.search([("name", "=", "Stock"), ("company_id", "=", self.company1.id)])
82+
.search(
83+
[("name", "=", "Stock"), ("company_id", "=", cls.company1.id)], limit=1
84+
)
6785
)
68-
self.uom_unit = self.env.ref("uom.product_uom_unit")
86+
cls.uom_unit = cls.env.ref("uom.product_uom_unit")
6987

7088
def test_picking_creation(self):
7189
stock_location = self.env["stock.location"].search(
72-
[("usage", "=", "internal"), ("company_id", "=", self.company1.id)]
90+
[("usage", "=", "internal"), ("company_id", "=", self.company1.id)], limit=1
7391
)
7492
custs_location = self.env.ref("stock.stock_location_customers")
7593
custs_location.company_id = False
@@ -93,7 +111,7 @@ def test_picking_creation(self):
93111
"location_dest_id": custs_location.id,
94112
"product_id": self.product1.id,
95113
"product_uom_id": self.uom_unit.id,
96-
"qty_done": 1.0,
114+
"quantity": 1.0,
97115
"picking_id": picking.id,
98116
}
99117
)
@@ -105,12 +123,12 @@ def test_picking_creation(self):
105123
self.assertEqual(len(counterpart_picking), 1)
106124
self.assertEqual(counterpart_picking.counterpart_of_picking_id, picking)
107125
self.assertEqual(len(counterpart_picking.move_ids), len(picking.move_ids))
108-
for cp_move, move in zip(counterpart_picking.move_ids, picking.move_ids):
126+
for cp_move, move in zip(counterpart_picking.move_ids, picking.move_ids): # noqa: B905
109127
self.assertEqual(cp_move.counterpart_of_move_id, move)
110128
self.assertEqual(
111129
len(counterpart_picking.move_line_ids), len(picking.move_line_ids)
112130
)
113-
for cp_line, line in zip(
131+
for cp_line, line in zip( # noqa: B905
114132
counterpart_picking.move_line_ids, picking.move_line_ids
115133
):
116134
self.assertEqual(cp_line.counterpart_of_line_id, line)

stock_intercompany/views/res_config_settings.xml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@
66
<field name="model">res.config.settings</field>
77
<field name="inherit_id" ref="stock.res_config_settings_view_form" />
88
<field name="arch" type="xml">
9-
<xpath expr="//div[@name='operations_setting_container']" position="inside">
10-
<div
11-
class="col-12 col-lg-6 o_setting_box"
9+
<xpath
10+
expr="//block[@name='operations_setting_container']"
11+
position="inside"
12+
>
13+
<setting
1214
id="intercompany_delivery_sbox"
15+
company_dependent="1"
16+
title="Values set here are company-specific."
17+
groups="base.group_multi_company"
1318
>
14-
<div class="o_setting_right_pane">
15-
<label for="intercompany_in_type_id" />
16-
<span
17-
class="fa fa-lg fa-building-o"
18-
title="Values set here are company-specific."
19-
groups="base.group_multi_company"
20-
/>
21-
<div class="content-group">
22-
<field name="intercompany_in_type_id" />
23-
</div>
24-
</div>
25-
</div>
19+
<field name="intercompany_in_type_id" />
20+
</setting>
2621
</xpath>
2722
</field>
2823
</record>

0 commit comments

Comments
 (0)