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

[11.0][MIG] mrp_mto_with_stock #255

Merged
merged 11 commits into from
Apr 10, 2018
64 changes: 31 additions & 33 deletions mrp_mto_with_stock/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

from odoo import api, models
from odoo.exceptions import UserError
import copy
import logging
_logger = logging.getLogger(__name__)

@@ -25,60 +26,57 @@ def action_assign(self):
res = super(MrpProduction, self).action_assign()
# try to create procurements:
move_obj = self.env['stock.move']
procurement_obj = self.env['procurement.group']
for production in self:
warehouse = production.location_src_id.get_warehouse()
mto_with_no_move_dest_id = warehouse.mrp_mto_mts_forecast_qty
for move in self.move_raw_ids:
group = new_move = procurement = False
move_ids = copy.copy(self.move_raw_ids.ids)
for move in move_obj.browse(move_ids):
new_move = False
qty_to_procure = 0.0
if move.state in ('partially_available', 'confirmed') \
and move.location_id in \
move.product_id.mrp_mts_mto_location_ids \
and not mto_with_no_move_dest_id:
# Search procurement group which has created from here
group_name = '{0}:{1}'.format(
production.name, move.product_id.name)
procurement = procurement_obj.search(
[('name', '=', group_name)])
if not procurement:
# We have to split the move because we can't have
# a part of the move that have ancestors and not the
# other else it won't ever be reserved.
qty_to_procure = (
move.product_uom_qty - move.reserved_availability)
if qty_to_procure < move.product_uom_qty:
move._do_unreserve()
new_move_id = move._split(
qty_to_procure,
restrict_partner_id=move.restrict_partner_id)
new_move = move_obj.browse(new_move_id)
move._action_assign()
else:
new_move = move
pg_data = production._get_procurement_group_data(
new_move)
group = procurement_obj.create(pg_data)
if move.state in ('partially_available', 'confirmed') \
# We have to split the move because we can't have
# a part of the move that have ancestors and not the
# other else it won't ever be reserved.
qty_to_procure = (
move.product_uom_qty - move.reserved_availability)
if qty_to_procure < move.product_uom_qty:
move._do_unreserve()
new_move_id = move._split(
qty_to_procure,
restrict_partner_id=move.restrict_partner_id)
new_move = move_obj.browse(new_move_id)
move._action_assign()
else:
new_move = move
elif move.state in ('partially_available', 'confirmed') \
and move.procure_method == 'make_to_stock' \
and mto_with_no_move_dest_id and \
move.location_id in \
move.product_id.mrp_mts_mto_location_ids:
qty_to_procure = production.get_mto_qty_to_procure(move)
if qty_to_procure > 0.0:
pg_data = production._get_procurement_group_data(move)
group = procurement_obj.create(pg_data)
new_move = move
if group:
production.run_procurement(new_move, group, qty_to_procure)
else:
continue
if new_move:
production.run_procurement(new_move, qty_to_procure,
mto_with_no_move_dest_id)
return res

@api.multi
def run_procurement(self, move, group, qty):
def run_procurement(self, move, qty, mto_with_no_move_dest_id):
self.ensure_one()
errors = []
values = move._prepare_procurement_values()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of mto_with_no_move_dest_id, you need to remove 'move_dest_ids' from values.
Else the 2 case handled in this module are the same.
Indeed, with mto_with_no_move_dest_id, we want to buy/manufacture to make the future qty (virtual qty) = 0
but we do not want to link the purchase/manufacture order to the stock move.

It would be nice to add a small test to show this issue.
It can be easily done, for instance, adding

self.production.action_assign()
self.assertEquals(self.production.move_raw_ids[0].state, 'assigned')

after https://github.com/ursais/manufacture/blob/dab59bcc211b61bab0f67486818bdaed38539e2a/mrp_mto_with_stock/tests/test_mrp_mto_with_stock.py#L77
Indeed, if you add this test with the present code, it will fail because the raw moves have ancestor and they should not.
Of course, the rest of the test may need to be adapted.

origin = ((group and group.name + ":") or "") + 'MTO -> Production'
# In that mode, we don't want any link between the raw material move
# And the previous move generated now.
if mto_with_no_move_dest_id:
values.pop('move_dest_ids', None)
origin = '{0}:{1}'.format(self.name, move.product_id.name) + \
':MTO -> Production'
values['route_ids'] = move.product_id.route_ids
try:
self.env['procurement.group'].run(
2 changes: 1 addition & 1 deletion mrp_mto_with_stock/tests/test_mrp_mto_with_stock.py
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ def test_manufacture_with_forecast_stock(self):
# We check if first MO is able to assign it self even if it has
# previously generate procurements, it would not be the case in the
# other mode (without mrp_mto_mts_reservable_stock on warehouse)
self.assertEquals(self.production.availability, 'partially_available')
self.assertEquals(self.production.availability, 'assigned')

self.assertEquals(self.subproduct1.virtual_available, 0)