Skip to content

Commit

Permalink
[IMP] stock_buffer_route: Do not consider other locations Buy routes
Browse files Browse the repository at this point in the history
Considering all buy routes should not be the default behaviour. If this is needed should be in a customized module.
  • Loading branch information
BernatPForgeFlow committed Jan 16, 2024
1 parent a6557af commit 6ddd8ba
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 20 deletions.
4 changes: 2 additions & 2 deletions stock_buffer_route/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Stock Buffer Route
==================

..
..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:012eb267c5f3b2dc1aee2e761f61510e4a51b010f0c23ac588a92bf7c3d50344
!! source digest: sha256:71fbdc11c56d2d40834b454605a88fb709845b8483b6d4962c4afba9a2099d8e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
2 changes: 2 additions & 0 deletions stock_buffer_route/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from . import mrp_production
from . import purchase_order
from . import stock_buffer
42 changes: 42 additions & 0 deletions stock_buffer_route/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import models


class MrpProduction(models.Model):
_inherit = "mrp.production"

def _get_domain_buffer_link_alternative(self, warehouse_level=False):
self.ensure_one()

Check warning on line 11 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L11

Added line #L11 was not covered by tests
if not warehouse_level:
locations = self.env["stock.location"].search(

Check warning on line 13 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L13

Added line #L13 was not covered by tests
[("id", "child_of", [self.location_dest_id.id])]
)
return [

Check warning on line 16 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L16

Added line #L16 was not covered by tests
("product_id", "=", self.product_id.id),
("company_id", "=", self.company_id.id),
("item_type_alternative", "=", "manufactured"),
("location_id", "in", locations.ids),
]
else:
return [

Check warning on line 23 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L23

Added line #L23 was not covered by tests
("product_id", "=", self.product_id.id),
("company_id", "=", self.company_id.id),
("item_type_alternative", "=", "manufactured"),
("warehouse_id", "=", self.picking_type_id.warehouse_id.id),
]

def _find_buffer_link(self):
res = super()._find_buffer_link()
buffer_model = self.env["stock.buffer"]

Check warning on line 32 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L31-L32

Added lines #L31 - L32 were not covered by tests
for rec in self.filtered(lambda r: not r.buffer_id):
domain = rec._get_domain_buffer_link_alternative()
buffer = buffer_model.search(domain, limit=1)

Check warning on line 35 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L34-L35

Added lines #L34 - L35 were not covered by tests
if not buffer:
domain = rec._get_domain_buffer_link_alternative(warehouse_level=True)
buffer = buffer_model.search(domain, limit=1)
rec.buffer_id = buffer

Check warning on line 39 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L37-L39

Added lines #L37 - L39 were not covered by tests
if buffer:
rec._calc_execution_priority()
return res

Check warning on line 42 in stock_buffer_route/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/mrp_production.py#L41-L42

Added lines #L41 - L42 were not covered by tests
38 changes: 38 additions & 0 deletions stock_buffer_route/models/purchase_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import models


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

def _get_domain_buffer_link_alternative(self):
self.ensure_one()

Check warning on line 11 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L11

Added line #L11 was not covered by tests
if not self.product_id:
# Return impossible domain -> no buffer.
return [(0, "=", 1)]
return [

Check warning on line 15 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L14-L15

Added lines #L14 - L15 were not covered by tests
("product_id", "=", self.product_id.id),
("company_id", "=", self.order_id.company_id.id),
("item_type_alternative", "=", "purchased"),
("warehouse_id", "=", self.order_id.picking_type_id.warehouse_id.id),
]

def _find_buffer_link(self):
res = super()._find_buffer_link()
buffer_model = self.env["stock.buffer"]
move_model = self.env["stock.move"]

Check warning on line 25 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L23-L25

Added lines #L23 - L25 were not covered by tests
for rec in self.filtered(lambda r: not r.buffer_ids):
mto_move = move_model.search(

Check warning on line 27 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L27

Added line #L27 was not covered by tests
[("created_purchase_line_id", "=", rec.id)], limit=1
)
if mto_move:
# MTO lines are not accounted in MTS stock buffers.
continue
domain = rec._get_domain_buffer_link_alternative()
buffer = buffer_model.search(domain, limit=1)

Check warning on line 34 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L32-L34

Added lines #L32 - L34 were not covered by tests
if buffer:
rec.buffer_ids = buffer
rec._calc_execution_priority()
return res

Check warning on line 38 in stock_buffer_route/models/purchase_order.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/purchase_order.py#L36-L38

Added lines #L36 - L38 were not covered by tests
36 changes: 21 additions & 15 deletions stock_buffer_route/models/stock_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class StockBuffer(models.Model):
"stock.route",
string="Allowed routes",
compute="_compute_route_ids",
store=True,
)
route_id = fields.Many2one(
"stock.route",
Expand Down Expand Up @@ -94,31 +95,37 @@ def _compute_dlt_alternative(self):
"item_type",
"product_id.seller_ids",
"product_id.seller_ids.company_id",
"product_id.seller_ids.name",
"product_id.seller_ids.partner_id",
"product_id.seller_ids.product_id",
"product_id.seller_ids.sequence",
"product_id.seller_ids.min_qty",
"product_id.seller_ids.price",
"item_type_alternative",
)
def _compute_main_supplier(self):
res = super()._compute_main_supplier()
for rec in self:
if rec.item_type_alternative == "purchased":
suppliers = rec._get_product_sellers()
rec.main_supplier_id = suppliers[0].name if suppliers else False
rec.main_supplier_id = (

Check warning on line 109 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L108-L109

Added lines #L108 - L109 were not covered by tests
suppliers[0].partner_id.id if suppliers else False
)
return res

def _calc_incoming_dlt_qty(self):
res = super()._calc_incoming_dlt_qty()
for rec in self:
if rec.item_type_alternative == "purchased":
cut_date = rec._get_incoming_supply_date_limit()
pols = rec.purchase_line_ids.filtered(
lambda l: l.date_planned > fields.Datetime.to_datetime(cut_date)
def _get_rfq_dlt_qty(self, outside_dlt=False):
res = super()._get_rfq_dlt_qty(outside_dlt)

Check warning on line 115 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L115

Added line #L115 was not covered by tests
if self.item_type_alternative == "purchased":
cut_date = self._get_incoming_supply_date_limit()

Check warning on line 117 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L117

Added line #L117 was not covered by tests
if not outside_dlt:
pols = self.purchase_line_ids.filtered(
lambda l: l.date_planned <= fields.Datetime.to_datetime(cut_date)
and l.state in ("draft", "sent")
)
rec.rfq_outside_dlt_qty = sum(pols.mapped("product_qty"))
else:
pols = self.purchase_line_ids.filtered(
lambda l: l.date_planned > fields.Datetime.to_datetime(cut_date)
and l.order_id.state in ("draft", "sent")
)
res += sum(pols.mapped("product_qty"))
return res

Check warning on line 129 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L128-L129

Added lines #L128 - L129 were not covered by tests

def _get_date_planned(self, force_lt=None):
Expand All @@ -145,7 +152,6 @@ def _get_location_routes_of_parents(self, routes, parents):
)
& parents
)
or any(rule.action == "buy" for rule in route.rule_ids)
)

def get_parents(self):
Expand Down Expand Up @@ -200,11 +206,11 @@ def write(self, vals):
self._calc_distributed_source_location()
return res

def action_view_supply(self, outside_dlt=False, view_rfq=False):
res = super().action_view_supply(outside_dlt, view_rfq)
def action_view_supply(self, outside_dlt=False):
res = super().action_view_supply(outside_dlt)

Check warning on line 210 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L210

Added line #L210 was not covered by tests
# If route is set it means that there is at least two alternatively ways to
# procure the buffer. Therefore, we will show Stock Pickings.
if self.route_id and not view_rfq:
if self.route_id:
moves = self._search_stock_moves_incoming(outside_dlt)
picks = moves.mapped("picking_id")
res = self.env["ir.actions.actions"]._for_xml_id(

Check warning on line 216 in stock_buffer_route/models/stock_buffer.py

View check run for this annotation

Codecov / codecov/patch

stock_buffer_route/models/stock_buffer.py#L214-L216

Added lines #L214 - L216 were not covered by tests
Expand Down
3 changes: 2 additions & 1 deletion stock_buffer_route/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Stock Buffer Route</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:012eb267c5f3b2dc1aee2e761f61510e4a51b010f0c23ac588a92bf7c3d50344
!! source digest: sha256:71fbdc11c56d2d40834b454605a88fb709845b8483b6d4962c4afba9a2099d8e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/ddmrp/tree/16.0/stock_buffer_route"><img alt="OCA/ddmrp" src="https://img.shields.io/badge/github-OCA%2Fddmrp-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/ddmrp-16-0/ddmrp-16-0-stock_buffer_route"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/ddmrp&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows to restrict a specific route to be used in the
Expand Down
4 changes: 2 additions & 2 deletions stock_buffer_route/wizards/make_procurement_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class MakeProcurementBufferItem(models.TransientModel):
_inherit = "make.procurement.buffer.item"

route_ids = fields.Many2many(
"stock.location.route",
"stock.route",
string="Allowed routes",
)
route_id = fields.Many2one(
"stock.location.route",
"stock.route",
domain="[('id', 'in', route_ids)]",
)
item_type_alternative = fields.Selection(
Expand Down

0 comments on commit 6ddd8ba

Please sign in to comment.