Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] stock_split_picking: Add a common for tests / add specific exceptions #1788

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stock_split_picking/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from . import models
from . import wizards
from . import exceptions
33 changes: 33 additions & 0 deletions stock_split_picking/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _
from odoo.exceptions import UserError


class SplitPickNotAllowedInStateError(UserError):
"""
Exception class to represent stock picking split error for picking wrong state
"""

def __init__(self, env, picking):
self.env = env
super().__init__(
_(
"Cannot split picking %(name)s in state %(state)s",
name=picking.name,
state=picking.state,
)
)


class NotPossibleToSplitPickError(UserError):
"""
Exception class to represent stock picking split error for picking
"""

def __init__(self, env, picking):
self.env = env
super().__init__(
_("Cannot split off all moves from picking %(name)s", name=picking.name)
)
12 changes: 4 additions & 8 deletions stock_split_picking/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare

from ..exceptions import NotPossibleToSplitPickError, SplitPickNotAllowedInStateError


class StockPicking(models.Model):
"""Adds picking split without done state."""
Expand Down Expand Up @@ -100,16 +102,10 @@ def _split_off_moves(self, moves):
new_picking = self.env["stock.picking"]
for this in self:
if this.state in ("done", "cancel"):
raise UserError(
_("Cannot split picking {name} in state {state}").format(
name=this.name, state=this.state
)
)
raise SplitPickNotAllowedInStateError(self.env, this)
new_picking = new_picking or this._create_split_backorder()
if not this.move_ids - moves:
raise UserError(
_("Cannot split off all moves from picking %s") % this.name
)
raise NotPossibleToSplitPickError(self.env, this)
moves.write({"picking_id": new_picking.id})
moves.mapped("move_line_ids").write({"picking_id": new_picking.id})
return new_picking
45 changes: 45 additions & 0 deletions stock_split_picking/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2017 Tecnativa - Vicent Cubells <[email protected]>
# Copyright 2018 Camptocamp SA - Julien Coux
# Copyright 2024 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase

from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT


class TestStockSplitPickingCase(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT))
cls.src_location = cls.env.ref("stock.stock_location_stock")
cls.dest_location = cls.env.ref("stock.stock_location_customers")
cls.product = cls.env["product.product"].create({"name": "Test product"})
cls.product_2 = cls.env["product.product"].create({"name": "Test product 2"})
cls.partner = cls.env["res.partner"].create({"name": "Test partner"})
cls.picking = cls.env["stock.picking"].create(
{
"partner_id": cls.partner.id,
"picking_type_id": cls.env.ref("stock.picking_type_out").id,
"location_id": cls.src_location.id,
"location_dest_id": cls.dest_location.id,
}
)

cls.move = cls._create_stock_move(cls.product)
cls.move_2 = cls._create_stock_move(cls.product_2)

@classmethod
def _create_stock_move(cls, product):
return cls.env["stock.move"].create(
{
"name": "/",
"picking_id": cls.picking.id,
"product_id": product.id,
"product_uom_qty": 10,
"product_uom": product.uom_id.id,
"location_id": cls.src_location.id,
"location_dest_id": cls.dest_location.id,
}
)
37 changes: 2 additions & 35 deletions stock_split_picking/tests/test_stock_split_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase

from .common import TestStockSplitPickingCase

class TestStockSplitPicking(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestStockSplitPicking, cls).setUpClass()

cls.src_location = cls.env.ref("stock.stock_location_stock")
cls.dest_location = cls.env.ref("stock.stock_location_customers")
cls.product = cls.env["product.product"].create({"name": "Test product"})
cls.product_2 = cls.env["product.product"].create({"name": "Test product 2"})
cls.partner = cls.env["res.partner"].create({"name": "Test partner"})
cls.picking = cls.env["stock.picking"].create(
{
"partner_id": cls.partner.id,
"picking_type_id": cls.env.ref("stock.picking_type_out").id,
"location_id": cls.src_location.id,
"location_dest_id": cls.dest_location.id,
}
)

def _create_stock_move(product):
return cls.env["stock.move"].create(
{
"name": "/",
"picking_id": cls.picking.id,
"product_id": product.id,
"product_uom_qty": 10,
"product_uom": product.uom_id.id,
"location_id": cls.src_location.id,
"location_dest_id": cls.dest_location.id,
}
)

cls.move = _create_stock_move(cls.product)
cls.move_2 = _create_stock_move(cls.product_2)

class TestStockSplitPicking(TestStockSplitPickingCase):
def test_stock_split_picking(self):
# Picking state is draft
self.assertEqual(self.picking.state, "draft")
Expand Down
Loading