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

14.0 add warehouse archive #290

Open
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions setup/stock_warehouse_archive/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
1 change: 1 addition & 0 deletions stock_warehouse_archive/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions stock_warehouse_archive/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Stock Warehouse Archive",
"summary": "Helper for forcing to archive a warehouse",
"version": "14.0.1.0.0",
"development_status": "Alpha",
"category": "Uncategorized",
"website": "https://github.com/akretion/ak-odoo-incubator",
"author": " Akretion",
"license": "AGPL-3",
"external_dependencies": {
"python": [],
"bin": [],
},
"depends": [
"stock",
],
"data": [],
"demo": [],
}
1 change: 1 addition & 0 deletions stock_warehouse_archive/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import stock_warehouse
175 changes: 175 additions & 0 deletions stock_warehouse_archive/models/stock_warehouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Copyright 2024 Akretion (https://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


import logging

from odoo import _, models

_logger = logging.getLogger(__name__)


LOCATION_FIELDS = [
"view_location_id",
"lot_stock_id",
"wh_input_stock_loc_id",
"wh_qc_stock_loc_id",
"wh_pack_stock_loc_id",
"wh_output_stock_loc_id",
"lot_rma_id",
"sam_loc_id",
"pbm_loc_id",
"repair_location_id",
]


class StockWarehouse(models.Model):
_name = "stock.warehouse"
_inherit = ["mail.thread", "stock.warehouse"]

def _cancel_pickings(self, pickings, messages):
for picking in pickings:
messages.append(

Check warning on line 33 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L33

Added line #L33 was not covered by tests
_("%(name)s created %(create_date)s")
% dict(
name=picking.name,
create_date=picking.create_date,
)
)
picking.action_cancel()

Check warning on line 40 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L40

Added line #L40 was not covered by tests

def _cancel_moves(self, moves, messages):
for move in moves:
messages.append(

Check warning on line 44 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L44

Added line #L44 was not covered by tests
_("%(qty)s X %(name)s created %(create_date)s")
% dict(
qty=move.product_qty,
name=move.name,
create_date=move.create_date,
)
)
move._action_cancel()

Check warning on line 52 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L52

Added line #L52 was not covered by tests

def _action_cancel_related_move_from_picking_type(self):
messages = []
picking_types = (

Check warning on line 56 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L55-L56

Added lines #L55 - L56 were not covered by tests
self.env["stock.picking.type"]
.with_context(active_test=False)
.search([("warehouse_id", "=", self.id)])
)

for picking_type in picking_types:
pickings = self.env["stock.picking"].search(

Check warning on line 63 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L63

Added line #L63 was not covered by tests
[
("picking_type_id", "=", picking_type.id),
("state", "not in", ("done", "cancel")),
]
)

if pickings:
messages += ["", _("Cancel picking with type %s") % picking_type.name]
self._cancel_pickings(pickings, messages)

Check warning on line 72 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L71-L72

Added lines #L71 - L72 were not covered by tests

moves = self.env["stock.move"].search(

Check warning on line 74 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L74

Added line #L74 was not covered by tests
[
("picking_type_id", "=", picking_type.id),
("state", "not in", ("done", "cancel")),
]
)
if moves:
messages += ["", _("Cancel Move with type %s") % picking_type.name]
self._cancel_moves(moves, messages)
return messages

Check warning on line 83 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L81-L83

Added lines #L81 - L83 were not covered by tests

def _action_cancel_related_move_from_location(self, locations):
messages = []

Check warning on line 86 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L86

Added line #L86 was not covered by tests
for location in locations:
pickings = self.env["stock.picking"].search(

Check warning on line 88 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L88

Added line #L88 was not covered by tests
[
("location_id", "=", location.id),
("state", "not in", ("done", "cancel")),
]
)
if pickings:
messages += ["", _("Cancel Picking with source %s") % location.name]
self._cancel_pickings(pickings, messages)

Check warning on line 96 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L95-L96

Added lines #L95 - L96 were not covered by tests

pickings = self.env["stock.picking"].search(

Check warning on line 98 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L98

Added line #L98 was not covered by tests
[
("location_dest_id", "=", location.id),
("state", "not in", ("done", "cancel")),
]
)

if pickings:
messages += [

Check warning on line 106 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L106

Added line #L106 was not covered by tests
"",
_("Cancel Picking with destination %s") % location.name,
]
self._cancel_pickings(pickings, messages)

Check warning on line 110 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L110

Added line #L110 was not covered by tests

moves = self.env["stock.move"].search(

Check warning on line 112 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L112

Added line #L112 was not covered by tests
[
("location_id", "=", location.id),
("state", "not in", ("done", "cancel")),
]
)
if moves:
messages += ["", _("Cancel Move with source %s") % location.name]
self._cancel_moves(moves, messages)

Check warning on line 120 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L119-L120

Added lines #L119 - L120 were not covered by tests

moves = self.env["stock.move"].search(

Check warning on line 122 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L122

Added line #L122 was not covered by tests
[
("location_dest_id", "=", location.id),
("state", "not in", ("done", "cancel")),
]
)
if moves:
messages += ["", _("Cancel Move with destination %s") % location.name]
self._cancel_moves(moves, messages)
return messages

Check warning on line 131 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L129-L131

Added lines #L129 - L131 were not covered by tests

def _action_purge_with_inventory(self, locations):
for location in locations:
if location.usage == "internal":
if not location.active:
location.active = True

Check warning on line 137 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L137

Added line #L137 was not covered by tests

inventory = self.env["stock.inventory"].create(

Check warning on line 139 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L139

Added line #L139 was not covered by tests
{
"name": _("Archive %(warehouse)s %(location)s")
% dict(warehouse=self.name, location=location.name),
"location_ids": [(6, 0, [location.id])],
"company_id": self.company_id.id,
}
)
inventory.action_start()

Check warning on line 147 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L147

Added line #L147 was not covered by tests
if inventory.line_ids:
# Compatibility with stock_inventory_location_state
if "sub_location_ids" in inventory._fields:
inventory.sub_location_ids.state = "done"
inventory._action_done()

Check warning on line 152 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L151-L152

Added lines #L151 - L152 were not covered by tests
else:
inventory.action_cancel_draft()
inventory.unlink()

Check warning on line 155 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L154-L155

Added lines #L154 - L155 were not covered by tests

def force_archive(self):

# Add the key in the context so you can adapt custom module behaviour
# if needed
self = self.with_context(archive_warehouse=True)

Check warning on line 161 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L161

Added line #L161 was not covered by tests

for warehouse in self:
messages = ["Force to archive warehouse"]
messages += warehouse._action_cancel_related_move_from_picking_type()

Check warning on line 165 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L164-L165

Added lines #L164 - L165 were not covered by tests

locations = self.env["stock.location"]

Check warning on line 167 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L167

Added line #L167 was not covered by tests
for field in LOCATION_FIELDS:
if field in warehouse._fields:
locations |= self[field]

Check warning on line 170 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L170

Added line #L170 was not covered by tests

messages += warehouse._action_cancel_related_move_from_location(locations)
warehouse._action_purge_with_inventory(locations)
warehouse.message_post(body="<br/>".join(messages))
warehouse.active = False

Check warning on line 175 in stock_warehouse_archive/models/stock_warehouse.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_archive/models/stock_warehouse.py#L172-L175

Added lines #L172 - L175 were not covered by tests
Loading