mirror of
https://github.com/OCA/manufacture.git
synced 2025-01-28 16:37:15 +02:00
[IMP] mrp_production_grouped_by_product: black, isort
This commit is contained in:
@@ -1,2 +1 @@
|
||||
|
||||
from . import models
|
||||
|
||||
@@ -4,18 +4,13 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
{
|
||||
'name': 'Production Grouped By Product',
|
||||
'version': '12.0.1.0.0',
|
||||
'category': 'MRP',
|
||||
'author': 'Tecnativa, '
|
||||
'Odoo Community Association (OCA)',
|
||||
'website': 'https://github.com/oca/manufacture',
|
||||
'license': 'AGPL-3',
|
||||
'depends': [
|
||||
'mrp',
|
||||
],
|
||||
'data': [
|
||||
'views/stock_picking_type_views.xml',
|
||||
],
|
||||
'installable': True,
|
||||
"name": "Production Grouped By Product",
|
||||
"version": "13.0.1.0.0",
|
||||
"category": "MRP",
|
||||
"author": "Tecnativa, " "Odoo Community Association (OCA)",
|
||||
"website": "https://github.com/oca/manufacture",
|
||||
"license": "AGPL-3",
|
||||
"depends": ["mrp"],
|
||||
"data": ["views/stock_picking_type_views.xml"],
|
||||
"installable": True,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
from . import mrp_production
|
||||
from . import stock_rule
|
||||
from . import stock_picking_type
|
||||
|
||||
@@ -3,12 +3,13 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo.tools import config
|
||||
|
||||
|
||||
class MrpProduction(models.Model):
|
||||
_inherit = 'mrp.production'
|
||||
_inherit = "mrp.production"
|
||||
|
||||
def _post_mo_merging_adjustments(self, vals):
|
||||
"""Called when a new MO is merged onto existing one for adjusting the
|
||||
@@ -18,12 +19,10 @@ class MrpProduction(models.Model):
|
||||
:param vals: Dictionary with the new record values.
|
||||
"""
|
||||
self.ensure_one()
|
||||
new_vals = {
|
||||
'origin': (self.origin or '') + ",%s" % vals['origin'],
|
||||
}
|
||||
if vals.get('move_dest_ids'):
|
||||
new_vals['move_dest_ids'] = vals['move_dest_ids']
|
||||
self.move_finished_ids.move_dest_ids = vals['move_dest_ids']
|
||||
new_vals = {"origin": (self.origin or "") + ",%s" % vals["origin"]}
|
||||
if vals.get("move_dest_ids"):
|
||||
new_vals["move_dest_ids"] = vals["move_dest_ids"]
|
||||
self.move_finished_ids.move_dest_ids = vals["move_dest_ids"]
|
||||
self.write(new_vals)
|
||||
|
||||
def _get_grouping_target_domain(self, vals):
|
||||
@@ -35,32 +34,27 @@ class MrpProduction(models.Model):
|
||||
:return: Odoo domain.
|
||||
"""
|
||||
domain = [
|
||||
('product_id', '=', vals['product_id']),
|
||||
('picking_type_id', '=', vals['picking_type_id']),
|
||||
('bom_id', '=', vals.get('bom_id', False)),
|
||||
('routing_id', '=', vals.get('routing_id', False)),
|
||||
('company_id', '=', vals.get('company_id', False)),
|
||||
('state', '=', 'confirmed'),
|
||||
("product_id", "=", vals["product_id"]),
|
||||
("picking_type_id", "=", vals["picking_type_id"]),
|
||||
("bom_id", "=", vals.get("bom_id", False)),
|
||||
("routing_id", "=", vals.get("routing_id", False)),
|
||||
("company_id", "=", vals.get("company_id", False)),
|
||||
("state", "=", "confirmed"),
|
||||
]
|
||||
if not vals.get('date_planned_finished'):
|
||||
if not vals.get("date_planned_finished"):
|
||||
return domain
|
||||
date = fields.Datetime.from_string(vals['date_planned_finished'])
|
||||
pt = self.env['stock.picking.type'].browse(vals['picking_type_id'])
|
||||
date = fields.Datetime.from_string(vals["date_planned_finished"])
|
||||
pt = self.env["stock.picking.type"].browse(vals["picking_type_id"])
|
||||
if date.hour < pt.mo_grouping_max_hour:
|
||||
date_end = date.replace(
|
||||
hour=pt.mo_grouping_max_hour, minute=0, second=0,
|
||||
)
|
||||
date_end = date.replace(hour=pt.mo_grouping_max_hour, minute=0, second=0)
|
||||
else:
|
||||
date_end = date.replace(
|
||||
day=date.day + 1, hour=pt.mo_grouping_max_hour, minute=0,
|
||||
second=0,
|
||||
day=date.day + 1, hour=pt.mo_grouping_max_hour, minute=0, second=0
|
||||
)
|
||||
date_start = date_end - relativedelta(days=pt.mo_grouping_interval)
|
||||
domain += [
|
||||
('date_planned_finished', '>',
|
||||
fields.Datetime.to_string(date_start)),
|
||||
('date_planned_finished', '<=',
|
||||
fields.Datetime.to_string(date_end)),
|
||||
("date_planned_finished", ">", fields.Datetime.to_string(date_start)),
|
||||
("date_planned_finished", "<=", fields.Datetime.to_string(date_end)),
|
||||
]
|
||||
return domain
|
||||
|
||||
@@ -71,21 +65,24 @@ class MrpProduction(models.Model):
|
||||
|
||||
:return: Target manufacturing order record (or empty record).
|
||||
"""
|
||||
return self.env['mrp.production'].search(
|
||||
self._get_grouping_target_domain(vals), limit=1,
|
||||
return self.env["mrp.production"].search(
|
||||
self._get_grouping_target_domain(vals), limit=1
|
||||
)
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
context = self.env.context
|
||||
if (context.get('group_mo_by_product') and
|
||||
(not config['test_enable'] or context.get('test_group_mo'))):
|
||||
if context.get("group_mo_by_product") and (
|
||||
not config["test_enable"] or context.get("test_group_mo")
|
||||
):
|
||||
mo = self._find_grouping_target(vals)
|
||||
if mo:
|
||||
self.env['change.production.qty'].create({
|
||||
'mo_id': mo.id,
|
||||
'product_qty': mo.product_qty + vals['product_qty'],
|
||||
}).change_prod_qty()
|
||||
self.env["change.production.qty"].create(
|
||||
{
|
||||
"mo_id": mo.id,
|
||||
"product_qty": mo.product_qty + vals["product_qty"],
|
||||
}
|
||||
).change_prod_qty()
|
||||
mo._post_mo_merging_adjustments(vals)
|
||||
return mo
|
||||
return super(MrpProduction, self).create(vals)
|
||||
|
||||
@@ -5,33 +5,33 @@ from odoo import _, api, exceptions, fields, models
|
||||
|
||||
|
||||
class StockPickingType(models.Model):
|
||||
_inherit = 'stock.picking.type'
|
||||
_inherit = "stock.picking.type"
|
||||
|
||||
mo_grouping_max_hour = fields.Integer(
|
||||
string="MO grouping max. hour (UTC)",
|
||||
help="The maximum hour (between 0 and 23) for considering new "
|
||||
"manufacturing orders inside the same interval period, and thus "
|
||||
"being grouped on the same MO. IMPORTANT: The hour should be "
|
||||
"expressed in UTC.",
|
||||
"manufacturing orders inside the same interval period, and thus "
|
||||
"being grouped on the same MO. IMPORTANT: The hour should be "
|
||||
"expressed in UTC.",
|
||||
default=19,
|
||||
)
|
||||
mo_grouping_interval = fields.Integer(
|
||||
string="MO grouping interval (days)",
|
||||
help="The number of days for grouping together on the same "
|
||||
"manufacturing order.",
|
||||
"manufacturing order.",
|
||||
default=1,
|
||||
)
|
||||
|
||||
@api.constrains('mo_grouping_max_hour')
|
||||
@api.constrains("mo_grouping_max_hour")
|
||||
def _check_mo_grouping_max_hour(self):
|
||||
if self.mo_grouping_max_hour < 0 or self.mo_grouping_max_hour > 23:
|
||||
raise exceptions.ValidationError(
|
||||
_("You have to enter a valid hour between 0 and 23."),
|
||||
_("You have to enter a valid hour between 0 and 23.")
|
||||
)
|
||||
|
||||
@api.constrains('mo_grouping_interval')
|
||||
@api.constrains("mo_grouping_interval")
|
||||
def _check_mo_grouping_interval(self):
|
||||
if self.mo_grouping_interval < 0:
|
||||
raise exceptions.ValidationError(
|
||||
_("You have to enter a positive value for interval."),
|
||||
_("You have to enter a positive value for interval.")
|
||||
)
|
||||
|
||||
@@ -6,13 +6,13 @@ from odoo import models
|
||||
|
||||
|
||||
class StockRule(models.Model):
|
||||
_inherit = 'stock.rule'
|
||||
_inherit = "stock.rule"
|
||||
|
||||
def _run_manufacture(self, product_id, product_qty, product_uom,
|
||||
location_id, name, origin, values):
|
||||
def _run_manufacture(
|
||||
self, product_id, product_qty, product_uom, location_id, name, origin, values
|
||||
):
|
||||
return super(
|
||||
StockRule, self.with_context(group_mo_by_product=True),
|
||||
StockRule, self.with_context(group_mo_by_product=True)
|
||||
)._run_manufacture(
|
||||
product_id, product_qty, product_uom, location_id, name, origin,
|
||||
values,
|
||||
product_id, product_qty, product_uom, location_id, name, origin, values
|
||||
)
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
|
||||
from . import test_mrp_production_grouped_by_product
|
||||
|
||||
@@ -14,89 +14,94 @@ class TestProductionGroupedByProduct(common.SavepointCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestProductionGroupedByProduct, cls).setUpClass()
|
||||
cls.ProcurementGroup = cls.env['procurement.group']
|
||||
cls.MrpProduction = cls.env['mrp.production']
|
||||
cls.ProcurementGroup = cls.env["procurement.group"]
|
||||
cls.MrpProduction = cls.env["mrp.production"]
|
||||
cls.env.user.company_id.manufacturing_lead = 0
|
||||
cls.env.user.tz = False # Make sure there's no timezone in user
|
||||
|
||||
cls.picking_type = cls.env['stock.picking.type'].search([
|
||||
('code', '=', 'mrp_operation'),
|
||||
('sequence_id.company_id', '=', cls.env.user.company_id.id)
|
||||
], limit=1)
|
||||
cls.product1 = cls.env['product.product'].create({
|
||||
'name': 'TEST Muffin',
|
||||
'route_ids': [(6, 0, [
|
||||
cls.env.ref('mrp.route_warehouse0_manufacture').id])],
|
||||
'type': 'product',
|
||||
'produce_delay': 0,
|
||||
})
|
||||
cls.product2 = cls.env['product.product'].create({
|
||||
'name': 'TEST Paper muffin cup',
|
||||
'type': 'product',
|
||||
})
|
||||
cls.product3 = cls.env['product.product'].create({
|
||||
'name': 'TEST Muffin paset',
|
||||
'type': 'product',
|
||||
})
|
||||
cls.bom = cls.env['mrp.bom'].create({
|
||||
'product_id': cls.product1.id,
|
||||
'product_tmpl_id': cls.product1.product_tmpl_id.id,
|
||||
'type': 'normal',
|
||||
'bom_line_ids': [(0, 0, {
|
||||
'product_id': cls.product2.id,
|
||||
'product_qty': 1,
|
||||
}), (0, 0, {
|
||||
'product_id': cls.product3.id,
|
||||
'product_qty': 0.2,
|
||||
})]
|
||||
})
|
||||
cls.stock_picking_type = cls.env.ref('stock.picking_type_out')
|
||||
cls.mo = cls.MrpProduction.create({
|
||||
'bom_id': cls.bom.id,
|
||||
'product_id': cls.product1.id,
|
||||
'product_qty': 2,
|
||||
'product_uom_id': cls.product1.uom_id.id,
|
||||
'date_planned_finished': '2018-06-01 15:00:00',
|
||||
'date_planned_start': '2018-06-01 15:00:00',
|
||||
})
|
||||
cls.warehouse = cls.env['stock.warehouse'].search([
|
||||
('company_id', '=', cls.env.user.company_id.id),
|
||||
], limit=1)
|
||||
cls.picking_type = cls.env["stock.picking.type"].search(
|
||||
[
|
||||
("code", "=", "mrp_operation"),
|
||||
("sequence_id.company_id", "=", cls.env.user.company_id.id),
|
||||
],
|
||||
limit=1,
|
||||
)
|
||||
cls.product1 = cls.env["product.product"].create(
|
||||
{
|
||||
"name": "TEST Muffin",
|
||||
"route_ids": [
|
||||
(6, 0, [cls.env.ref("mrp.route_warehouse0_manufacture").id])
|
||||
],
|
||||
"type": "product",
|
||||
"produce_delay": 0,
|
||||
}
|
||||
)
|
||||
cls.product2 = cls.env["product.product"].create(
|
||||
{"name": "TEST Paper muffin cup", "type": "product"}
|
||||
)
|
||||
cls.product3 = cls.env["product.product"].create(
|
||||
{"name": "TEST Muffin paset", "type": "product"}
|
||||
)
|
||||
cls.bom = cls.env["mrp.bom"].create(
|
||||
{
|
||||
"product_id": cls.product1.id,
|
||||
"product_tmpl_id": cls.product1.product_tmpl_id.id,
|
||||
"type": "normal",
|
||||
"bom_line_ids": [
|
||||
(0, 0, {"product_id": cls.product2.id, "product_qty": 1}),
|
||||
(0, 0, {"product_id": cls.product3.id, "product_qty": 0.2}),
|
||||
],
|
||||
}
|
||||
)
|
||||
cls.stock_picking_type = cls.env.ref("stock.picking_type_out")
|
||||
cls.mo = cls.MrpProduction.create(
|
||||
{
|
||||
"bom_id": cls.bom.id,
|
||||
"product_id": cls.product1.id,
|
||||
"product_qty": 2,
|
||||
"product_uom_id": cls.product1.uom_id.id,
|
||||
"date_planned_finished": "2018-06-01 15:00:00",
|
||||
"date_planned_start": "2018-06-01 15:00:00",
|
||||
}
|
||||
)
|
||||
cls.warehouse = cls.env["stock.warehouse"].search(
|
||||
[("company_id", "=", cls.env.user.company_id.id)], limit=1
|
||||
)
|
||||
# Add an MTO move
|
||||
cls.move = cls.env['stock.move'].create({
|
||||
'name': cls.product1.name,
|
||||
'product_id': cls.product1.id,
|
||||
'product_uom_qty': 10,
|
||||
'product_uom': cls.product1.uom_id.id,
|
||||
'location_id': cls.warehouse.lot_stock_id.id,
|
||||
'location_dest_id': (
|
||||
cls.env.ref('stock.stock_location_customers').id
|
||||
),
|
||||
'procure_method': 'make_to_order',
|
||||
'warehouse_id': cls.warehouse.id,
|
||||
'date': '2018-06-01 18:00:00',
|
||||
'date_expected': '2018-06-01 18:00:00',
|
||||
})
|
||||
cls.move = cls.env["stock.move"].create(
|
||||
{
|
||||
"name": cls.product1.name,
|
||||
"product_id": cls.product1.id,
|
||||
"product_uom_qty": 10,
|
||||
"product_uom": cls.product1.uom_id.id,
|
||||
"location_id": cls.warehouse.lot_stock_id.id,
|
||||
"location_dest_id": (cls.env.ref("stock.stock_location_customers").id),
|
||||
"procure_method": "make_to_order",
|
||||
"warehouse_id": cls.warehouse.id,
|
||||
"date": "2018-06-01 18:00:00",
|
||||
"date_expected": "2018-06-01 18:00:00",
|
||||
}
|
||||
)
|
||||
|
||||
def test_mo_by_product(self):
|
||||
self.move.with_context(test_group_mo=True)._action_confirm(merge=False)
|
||||
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler()
|
||||
mo = self.MrpProduction.search([('product_id', '=', self.product1.id)])
|
||||
mo = self.MrpProduction.search([("product_id", "=", self.product1.id)])
|
||||
self.assertEqual(len(mo), 1)
|
||||
self.assertEqual(mo.product_qty, 12)
|
||||
# Run again the scheduler to see if quantities are altered
|
||||
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler()
|
||||
mo = self.MrpProduction.search([('product_id', '=', self.product1.id)])
|
||||
mo = self.MrpProduction.search([("product_id", "=", self.product1.id)])
|
||||
self.assertEqual(len(mo), 1)
|
||||
self.assertEqual(mo.product_qty, 12)
|
||||
|
||||
def test_mo_other_date(self):
|
||||
self.move.write(
|
||||
{'date_expected': '2018-06-01 20:01:00',
|
||||
'date': '2018-06-01 20:01:00'})
|
||||
{"date_expected": "2018-06-01 20:01:00", "date": "2018-06-01 20:01:00"}
|
||||
)
|
||||
self.move.with_context(test_group_mo=True)._action_confirm(merge=False)
|
||||
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler()
|
||||
mo = self.MrpProduction.search([('product_id', '=', self.product1.id)])
|
||||
mo = self.MrpProduction.search([("product_id", "=", self.product1.id)])
|
||||
self.assertEqual(len(mo), 2)
|
||||
|
||||
def test_check_mo_grouping_max_hour(self):
|
||||
|
||||
Reference in New Issue
Block a user