Skip to content

Commit

Permalink
[MIG] purchase_stock_operating_unit: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidderen-nsi committed Jun 3, 2024
1 parent e1711e2 commit bc7201f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion purchase_stock_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "Purchase Stock Operating Unit",
"summary": "Copies the operating unit of purchase picking to the stock picking",
"version": "15.0.1.0.0",
"version": "17.0.1.0.0",
"author": "Ecosoft, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/operating-unit",
"category": "Purchase Management",
Expand Down
2 changes: 1 addition & 1 deletion purchase_stock_operating_unit/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import purchase_order_line
from . import purchase
from . import stock_rule
41 changes: 40 additions & 1 deletion purchase_stock_operating_unit/models/purchase.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError


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

picking_type_id = fields.Many2one(
compute="_compute_picking_type_id", store=True, readonly=False
)
operating_unit_id = fields.Many2one(
compute="_compute_operating_unit_id",
store=True,
readonly=False,
)

@api.depends("operating_unit_id")
def _compute_picking_type_id(self):
for purchase in self:
if purchase.operating_unit_id:
purchase.picking_type_id = self.env["stock.picking.type"].search(
[
(
"warehouse_id.operating_unit_id",
"=",
purchase.operating_unit_id.id,
),
("code", "=", "incoming"),
],
limit=1,
)

@api.depends("picking_type_id")
def _compute_operating_unit_id(self):
for purchase in self:
if purchase.picking_type_id:
purchase.operating_unit_id = (
purchase.picking_type_id.warehouse_id.operating_unit_id
)

@api.constrains("operating_unit_id", "picking_type_id")
def _check_operating_unit_picking_type(self):
for rec in self:
if (
rec.operating_unit_id
and rec.picking_type_id.warehouse_id.operating_unit_id
and rec.operating_unit_id
!= rec.picking_type_id.warehouse_id.operating_unit_id
):
Expand All @@ -22,3 +56,8 @@ def _check_operating_unit_picking_type(self):
"the Purchase and Deliver To must be the same."
)
)

def _prepare_picking(self):
vals = super()._prepare_picking()
vals["operating_unit_id"] = self.operating_unit_id.id
return vals
15 changes: 0 additions & 15 deletions purchase_stock_operating_unit/models/purchase_order_line.py

This file was deleted.

14 changes: 14 additions & 0 deletions purchase_stock_operating_unit/models/stock_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class StockRule(models.Model):
_inherit = "stock.rule"

def _prepare_purchase_order(self, company_id, origins, values):
res = super()._prepare_purchase_order(company_id, origins, values)
if self.operating_unit_id:
res["operating_unit_id"] = self.operating_unit_id.id
return res
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
# Copyright 2023 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.exceptions import UserError
from odoo.tests import Form
from odoo.models import Command

from odoo.addons.purchase_operating_unit.tests.test_purchase_operating_unit import (
TestPurchaseOperatingUnit,
)


class TestPurchaseStockOperatingUnit(TestPurchaseOperatingUnit):
def setUp(self):
super().setUp()
self.warehouse_b2b = self.env.ref("stock_operating_unit.stock_warehouse_b2b")
self.picking_type2 = self.env["stock.picking.type"].search(
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.warehouse_b2b = cls.env.ref("stock_operating_unit.stock_warehouse_b2b")
cls.picking_type2 = cls.env["stock.picking.type"].search(
[
("code", "=", "incoming"),
("warehouse_id", "=", self.warehouse_b2b.id),
("warehouse_id", "=", cls.warehouse_b2b.id),
],
limit=1,
)
# Add permission b2b operating unit in user1
self.b2b = self.env.ref("operating_unit.b2b_operating_unit")
user = self.env["res.users"].browse(self.user1_id)
user.operating_unit_ids = [(4, self.b2b.id)]
cls.user1.write(
{
"operating_unit_ids": [
Command.link(cls.b2b.id),
],
}
)

def test_01_purchase_stock_operating_unit(self):
self.assertEqual(self.purchase1.state, "purchase")
self.assertEqual(
self.purchase1.picking_ids.operating_unit_id,
self.purchase1.picking_type_id.warehouse_id.operating_unit_id,
)
# Ensure that in case of picking type changes,
# the operating unit is also updated.
self.purchase1.button_cancel()
self.purchase1.button_draft()
# Check change picking type is not equal operating unit, it should error
with self.assertRaises(UserError):
with Form(self.purchase1) as po:
po.picking_type_id = self.picking_type2
self.purchase1.picking_type_id = self.picking_type2
self.assertEqual(
self.purchase1.operating_unit_id,
self.purchase1.picking_type_id.warehouse_id.operating_unit_id,
)

0 comments on commit bc7201f

Please sign in to comment.