Skip to content

Commit

Permalink
[MIG] stock_available_to_promise_release: Migration to 13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
guewen committed Dec 18, 2019
1 parent 075ff84 commit 743e43a
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 69 deletions.
2 changes: 1 addition & 1 deletion sale_stock_available_to_promise_release/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Stock Available to Promise Release - Sale Integration",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"summary": "Integration between Sales and Available to Promise Release",
"author": "Camptocamp,Odoo Community Association (OCA)",
"category": "Stock Management",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Copyright 2019 Camptocamp (https://www.camptocamp.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models
from odoo import models


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

@api.multi
def _prepare_procurement_values(self, group_id=False):
values = super()._prepare_procurement_values(group_id)
values["date_priority"] = self.order_id.confirmation_date
values["date_priority"] = self.order_id.date_order
return values
2 changes: 1 addition & 1 deletion stock_available_to_promise_release/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Stock Available to Promise Release",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"summary": "Release Operations based on available to promise",
"author": "Camptocamp,Odoo Community Association (OCA)",
"category": "Stock Management",
Expand Down
37 changes: 20 additions & 17 deletions stock_available_to_promise_release/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def _previous_promised_qty(self):
)
return promised_qty

@api.multi
def release_available_to_promise(self):
self._run_stock_rule()

Expand All @@ -90,7 +89,6 @@ def _prepare_move_split_vals(self, qty):
vals.update({"procure_method": self.procure_method, "need_release": True})
return vals

@api.multi
def _run_stock_rule(self):
"""Launch procurement group run method with remaining quantity
Expand All @@ -102,6 +100,8 @@ def _run_stock_rule(self):
precision = self.env["decimal.precision"].precision_get(
"Product Unit of Measure"
)
procurement_requests = []
pulled_moves = self.env["stock.move"]
for move in self:
if not move.need_release:
continue
Expand All @@ -118,27 +118,30 @@ def _run_stock_rule(self):

if float_compare(remaining, 0, precision_digits=precision) > 0:
if move.picking_id.move_type == "one":
# we don't want to delivery unless we can deliver all at
# we don't want to deliver unless we can deliver all at
# once
continue
move.with_context(release_available_to_promise=True)._split(remaining)

values = move._prepare_procurement_values()

self.env["procurement.group"].run_defer(
move.product_id,
move.product_id.uom_id._compute_quantity(
quantity, move.product_uom, rounding_method="HALF-UP"
),
move.product_uom,
move.location_id,
move.origin,
values,
procurement_requests.append(
self.env["procurement.group"].Procurement(
move.product_id,
move.product_uom_qty,
move.product_uom,
move.location_id,
move.rule_id and move.rule_id.name or "/",
move.origin,
move.company_id,
values,
)
)
pulled_moves |= move

self.env["procurement.group"].run_defer(procurement_requests)

pull_move = move
while pull_move:
pull_move._action_assign()
pull_move = pull_move.move_orig_ids
while pulled_moves:
pulled_moves._action_assign()
pulled_moves = pulled_moves.mapped("move_orig_ids")

return True
1 change: 0 additions & 1 deletion stock_available_to_promise_release/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ def _compute_need_release(self):
for picking in self:
picking.need_release = any(move.need_release for move in picking.move_lines)

@api.multi
def release_available_to_promise(self):
self.mapped("move_lines").release_available_to_promise()
90 changes: 47 additions & 43 deletions stock_available_to_promise_release/models/stock_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,65 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging

from odoo import api, fields, models
from odoo import fields, models

_logger = logging.getLogger(__name__)


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

def _run_pull(
self, product_id, product_qty, product_uom, location_id, name, origin, values
):
if (
not self.env.context.get("_rule_no_available_defer")
and self.route_id.available_to_promise_defer_pull
# We still want to create the first part of the chain
and not self.picking_type_id.code == "outgoing"
):
moves = values.get("move_dest_ids")
# Track the moves that needs to have their pull rule
# done. Before the 'pull' is done, we don't know the
# which route is chosen. We update the destination
# move (ie. the outgoing) when the current route
# defers the pull rules and return so we don't create
# the next move of the chain (pick or pack).
if moves:
moves.write({"need_release": True})
return True

super()._run_pull(
product_id, product_qty, product_uom, location_id, name, origin, values
)
moves = values.get("move_dest_ids")
if moves:
def _run_pull(self, procurements):
actions_to_run = []

for procurement, rule in procurements:
if (
not self.env.context.get("_rule_no_available_defer")
and rule.route_id.available_to_promise_defer_pull
# We still want to create the first part of the chain
and not rule.picking_type_id.code == "outgoing"
):
moves = procurement.values.get("move_dest_ids")
# Track the moves that needs to have their pull rule
# done. Before the 'pull' is done, we don't know the
# which route is chosen. We update the destination
# move (ie. the outgoing) when the current route
# defers the pull rules and return so we don't create
# the next move of the chain (pick or pack).
if moves:
moves.write({"need_release": True})
else:
actions_to_run.append((procurement, rule))

super()._run_pull(actions_to_run)
# use first of list of ids and browse it for performance
move_ids = [
move.id
for move in procurement.values.get("move_dest_ids", [])
for procurement, _rule in actions_to_run
]
if move_ids:
moves = self.env["stock.move"].browse(move_ids)
moves.filtered(lambda r: r.need_release).write({"need_release": False})
return True


class ProcurementGroup(models.Model):
_inherit = "procurement.group"

@api.model
def run_defer(
self, product_id, product_qty, product_uom, location_id, origin, values
):
values.setdefault(
"company_id",
self.env["res.company"]._company_default_get("procurement.group"),
)
values.setdefault("priority", "1")
values.setdefault("date_planned", fields.Datetime.now())
rule = self._get_rule(product_id, location_id, values)
if not rule or rule.action not in ("pull", "pull_push"):
return

rule.with_context(_rule_no_available_defer=True)._run_pull(
product_id, product_qty, product_uom, location_id, rule.name, origin, values
)
def run_defer(self, procurements):
actions_to_run = []
for procurement in procurements:
values = procurement.values
values.setdefault("company_id", self.env.company)
values.setdefault("priority", "1")
values.setdefault("date_planned", fields.Datetime.now())
rule = self._get_rule(
procurement.product_id, procurement.location_id, procurement.values
)
if rule.action in ("pull", "pull_push"):
actions_to_run.append((procurement, rule))

if actions_to_run:
rule.with_context(_rule_no_available_defer=True)._run_pull(actions_to_run)
return True
15 changes: 14 additions & 1 deletion stock_available_to_promise_release/tests/test_reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ def _create_picking_chain(self, wh, products=None, date=None, move_type="direct"
raise ValueError(
"Expect (product, quantity, uom) or (product, quantity)"
)

self.env["procurement.group"].run(
product, qty, uom, self.loc_customer, "TEST", "TEST", values
[
self.env["procurement.group"].Procurement(
product,
qty,
uom,
self.loc_customer,
"TEST",
"TEST",
wh.company_id,
values,
)
]
)
pickings = self._pickings_in_group(group)
pickings.mapped("move_lines").write(
Expand Down Expand Up @@ -166,6 +178,7 @@ def test_defer_creation(self):

self._update_qty_in_location(self.loc_bin1, self.product1, 20.0)
pickings = self._create_picking_chain(self.wh, [(self.product1, 5)])

self.assertEqual(len(pickings), 1, "expect only the last out->customer")
cust_picking = pickings
self.assertRecordValues(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<field name="name">Stock Moves To Release</field>
<field name="res_model">stock.move</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree,kanban,form</field>
<field name="search_view_id" ref="stock.view_move_search"/>
<field name="view_ids" eval="[(5, 0, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<field name="name">Release Stock Move</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">stock.move.release</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="groups_id" eval="[(4,ref('stock.group_stock_user'))]"/>
Expand Down

0 comments on commit 743e43a

Please sign in to comment.