[IMP] mrp_production_grouped_by_product: black, isort

This commit is contained in:
ps-tubtim
2020-01-09 16:20:25 +07:00
committed by Lois Rilo
parent 742c2fc1e7
commit c2fdb69c52
8 changed files with 123 additions and 129 deletions

View File

@@ -1,2 +1 @@
from . import models from . import models

View File

@@ -4,18 +4,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{ {
'name': 'Production Grouped By Product', "name": "Production Grouped By Product",
'version': '12.0.1.0.0', "version": "13.0.1.0.0",
'category': 'MRP', "category": "MRP",
'author': 'Tecnativa, ' "author": "Tecnativa, " "Odoo Community Association (OCA)",
'Odoo Community Association (OCA)', "website": "https://github.com/oca/manufacture",
'website': 'https://github.com/oca/manufacture', "license": "AGPL-3",
'license': 'AGPL-3', "depends": ["mrp"],
'depends': [ "data": ["views/stock_picking_type_views.xml"],
'mrp', "installable": True,
],
'data': [
'views/stock_picking_type_views.xml',
],
'installable': True,
} }

View File

@@ -1,4 +1,3 @@
from . import mrp_production from . import mrp_production
from . import stock_rule from . import stock_rule
from . import stock_picking_type from . import stock_picking_type

View File

@@ -3,12 +3,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from odoo import api, fields, models from odoo import api, fields, models
from odoo.tools import config from odoo.tools import config
class MrpProduction(models.Model): class MrpProduction(models.Model):
_inherit = 'mrp.production' _inherit = "mrp.production"
def _post_mo_merging_adjustments(self, vals): def _post_mo_merging_adjustments(self, vals):
"""Called when a new MO is merged onto existing one for adjusting the """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. :param vals: Dictionary with the new record values.
""" """
self.ensure_one() self.ensure_one()
new_vals = { new_vals = {"origin": (self.origin or "") + ",%s" % vals["origin"]}
'origin': (self.origin or '') + ",%s" % vals['origin'], if vals.get("move_dest_ids"):
} new_vals["move_dest_ids"] = vals["move_dest_ids"]
if vals.get('move_dest_ids'): self.move_finished_ids.move_dest_ids = vals["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) self.write(new_vals)
def _get_grouping_target_domain(self, vals): def _get_grouping_target_domain(self, vals):
@@ -35,32 +34,27 @@ class MrpProduction(models.Model):
:return: Odoo domain. :return: Odoo domain.
""" """
domain = [ domain = [
('product_id', '=', vals['product_id']), ("product_id", "=", vals["product_id"]),
('picking_type_id', '=', vals['picking_type_id']), ("picking_type_id", "=", vals["picking_type_id"]),
('bom_id', '=', vals.get('bom_id', False)), ("bom_id", "=", vals.get("bom_id", False)),
('routing_id', '=', vals.get('routing_id', False)), ("routing_id", "=", vals.get("routing_id", False)),
('company_id', '=', vals.get('company_id', False)), ("company_id", "=", vals.get("company_id", False)),
('state', '=', 'confirmed'), ("state", "=", "confirmed"),
] ]
if not vals.get('date_planned_finished'): if not vals.get("date_planned_finished"):
return domain return domain
date = fields.Datetime.from_string(vals['date_planned_finished']) date = fields.Datetime.from_string(vals["date_planned_finished"])
pt = self.env['stock.picking.type'].browse(vals['picking_type_id']) pt = self.env["stock.picking.type"].browse(vals["picking_type_id"])
if date.hour < pt.mo_grouping_max_hour: if date.hour < pt.mo_grouping_max_hour:
date_end = date.replace( date_end = date.replace(hour=pt.mo_grouping_max_hour, minute=0, second=0)
hour=pt.mo_grouping_max_hour, minute=0, second=0,
)
else: else:
date_end = date.replace( date_end = date.replace(
day=date.day + 1, hour=pt.mo_grouping_max_hour, minute=0, day=date.day + 1, hour=pt.mo_grouping_max_hour, minute=0, second=0
second=0,
) )
date_start = date_end - relativedelta(days=pt.mo_grouping_interval) date_start = date_end - relativedelta(days=pt.mo_grouping_interval)
domain += [ domain += [
('date_planned_finished', '>', ("date_planned_finished", ">", fields.Datetime.to_string(date_start)),
fields.Datetime.to_string(date_start)), ("date_planned_finished", "<=", fields.Datetime.to_string(date_end)),
('date_planned_finished', '<=',
fields.Datetime.to_string(date_end)),
] ]
return domain return domain
@@ -71,21 +65,24 @@ class MrpProduction(models.Model):
:return: Target manufacturing order record (or empty record). :return: Target manufacturing order record (or empty record).
""" """
return self.env['mrp.production'].search( return self.env["mrp.production"].search(
self._get_grouping_target_domain(vals), limit=1, self._get_grouping_target_domain(vals), limit=1
) )
@api.model @api.model
def create(self, vals): def create(self, vals):
context = self.env.context context = self.env.context
if (context.get('group_mo_by_product') and if context.get("group_mo_by_product") and (
(not config['test_enable'] or context.get('test_group_mo'))): not config["test_enable"] or context.get("test_group_mo")
):
mo = self._find_grouping_target(vals) mo = self._find_grouping_target(vals)
if mo: if mo:
self.env['change.production.qty'].create({ self.env["change.production.qty"].create(
'mo_id': mo.id, {
'product_qty': mo.product_qty + vals['product_qty'], "mo_id": mo.id,
}).change_prod_qty() "product_qty": mo.product_qty + vals["product_qty"],
}
).change_prod_qty()
mo._post_mo_merging_adjustments(vals) mo._post_mo_merging_adjustments(vals)
return mo return mo
return super(MrpProduction, self).create(vals) return super(MrpProduction, self).create(vals)

View File

@@ -5,33 +5,33 @@ from odoo import _, api, exceptions, fields, models
class StockPickingType(models.Model): class StockPickingType(models.Model):
_inherit = 'stock.picking.type' _inherit = "stock.picking.type"
mo_grouping_max_hour = fields.Integer( mo_grouping_max_hour = fields.Integer(
string="MO grouping max. hour (UTC)", string="MO grouping max. hour (UTC)",
help="The maximum hour (between 0 and 23) for considering new " help="The maximum hour (between 0 and 23) for considering new "
"manufacturing orders inside the same interval period, and thus " "manufacturing orders inside the same interval period, and thus "
"being grouped on the same MO. IMPORTANT: The hour should be " "being grouped on the same MO. IMPORTANT: The hour should be "
"expressed in UTC.", "expressed in UTC.",
default=19, default=19,
) )
mo_grouping_interval = fields.Integer( mo_grouping_interval = fields.Integer(
string="MO grouping interval (days)", string="MO grouping interval (days)",
help="The number of days for grouping together on the same " help="The number of days for grouping together on the same "
"manufacturing order.", "manufacturing order.",
default=1, default=1,
) )
@api.constrains('mo_grouping_max_hour') @api.constrains("mo_grouping_max_hour")
def _check_mo_grouping_max_hour(self): def _check_mo_grouping_max_hour(self):
if self.mo_grouping_max_hour < 0 or self.mo_grouping_max_hour > 23: if self.mo_grouping_max_hour < 0 or self.mo_grouping_max_hour > 23:
raise exceptions.ValidationError( 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): def _check_mo_grouping_interval(self):
if self.mo_grouping_interval < 0: if self.mo_grouping_interval < 0:
raise exceptions.ValidationError( raise exceptions.ValidationError(
_("You have to enter a positive value for interval."), _("You have to enter a positive value for interval.")
) )

View File

@@ -6,13 +6,13 @@ from odoo import models
class StockRule(models.Model): class StockRule(models.Model):
_inherit = 'stock.rule' _inherit = "stock.rule"
def _run_manufacture(self, product_id, product_qty, product_uom, def _run_manufacture(
location_id, name, origin, values): self, product_id, product_qty, product_uom, location_id, name, origin, values
):
return super( return super(
StockRule, self.with_context(group_mo_by_product=True), StockRule, self.with_context(group_mo_by_product=True)
)._run_manufacture( )._run_manufacture(
product_id, product_qty, product_uom, location_id, name, origin, product_id, product_qty, product_uom, location_id, name, origin, values
values,
) )

View File

@@ -1,2 +1 @@
from . import test_mrp_production_grouped_by_product from . import test_mrp_production_grouped_by_product

View File

@@ -14,89 +14,94 @@ class TestProductionGroupedByProduct(common.SavepointCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
super(TestProductionGroupedByProduct, cls).setUpClass() super(TestProductionGroupedByProduct, cls).setUpClass()
cls.ProcurementGroup = cls.env['procurement.group'] cls.ProcurementGroup = cls.env["procurement.group"]
cls.MrpProduction = cls.env['mrp.production'] cls.MrpProduction = cls.env["mrp.production"]
cls.env.user.company_id.manufacturing_lead = 0 cls.env.user.company_id.manufacturing_lead = 0
cls.env.user.tz = False # Make sure there's no timezone in user cls.env.user.tz = False # Make sure there's no timezone in user
cls.picking_type = cls.env['stock.picking.type'].search([ cls.picking_type = cls.env["stock.picking.type"].search(
('code', '=', 'mrp_operation'), [
('sequence_id.company_id', '=', cls.env.user.company_id.id) ("code", "=", "mrp_operation"),
], limit=1) ("sequence_id.company_id", "=", cls.env.user.company_id.id),
cls.product1 = cls.env['product.product'].create({ ],
'name': 'TEST Muffin', limit=1,
'route_ids': [(6, 0, [ )
cls.env.ref('mrp.route_warehouse0_manufacture').id])], cls.product1 = cls.env["product.product"].create(
'type': 'product', {
'produce_delay': 0, "name": "TEST Muffin",
}) "route_ids": [
cls.product2 = cls.env['product.product'].create({ (6, 0, [cls.env.ref("mrp.route_warehouse0_manufacture").id])
'name': 'TEST Paper muffin cup', ],
'type': 'product', "type": "product",
}) "produce_delay": 0,
cls.product3 = cls.env['product.product'].create({ }
'name': 'TEST Muffin paset', )
'type': 'product', cls.product2 = cls.env["product.product"].create(
}) {"name": "TEST Paper muffin cup", "type": "product"}
cls.bom = cls.env['mrp.bom'].create({ )
'product_id': cls.product1.id, cls.product3 = cls.env["product.product"].create(
'product_tmpl_id': cls.product1.product_tmpl_id.id, {"name": "TEST Muffin paset", "type": "product"}
'type': 'normal', )
'bom_line_ids': [(0, 0, { cls.bom = cls.env["mrp.bom"].create(
'product_id': cls.product2.id, {
'product_qty': 1, "product_id": cls.product1.id,
}), (0, 0, { "product_tmpl_id": cls.product1.product_tmpl_id.id,
'product_id': cls.product3.id, "type": "normal",
'product_qty': 0.2, "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, cls.stock_picking_type = cls.env.ref("stock.picking_type_out")
'product_qty': 2, cls.mo = cls.MrpProduction.create(
'product_uom_id': cls.product1.uom_id.id, {
'date_planned_finished': '2018-06-01 15:00:00', "bom_id": cls.bom.id,
'date_planned_start': '2018-06-01 15:00:00', "product_id": cls.product1.id,
}) "product_qty": 2,
cls.warehouse = cls.env['stock.warehouse'].search([ "product_uom_id": cls.product1.uom_id.id,
('company_id', '=', cls.env.user.company_id.id), "date_planned_finished": "2018-06-01 15:00:00",
], limit=1) "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 # Add an MTO move
cls.move = cls.env['stock.move'].create({ cls.move = cls.env["stock.move"].create(
'name': cls.product1.name, {
'product_id': cls.product1.id, "name": cls.product1.name,
'product_uom_qty': 10, "product_id": cls.product1.id,
'product_uom': cls.product1.uom_id.id, "product_uom_qty": 10,
'location_id': cls.warehouse.lot_stock_id.id, "product_uom": cls.product1.uom_id.id,
'location_dest_id': ( "location_id": cls.warehouse.lot_stock_id.id,
cls.env.ref('stock.stock_location_customers').id "location_dest_id": (cls.env.ref("stock.stock_location_customers").id),
), "procure_method": "make_to_order",
'procure_method': 'make_to_order', "warehouse_id": cls.warehouse.id,
'warehouse_id': cls.warehouse.id, "date": "2018-06-01 18:00:00",
'date': '2018-06-01 18:00:00', "date_expected": "2018-06-01 18:00:00",
'date_expected': '2018-06-01 18:00:00', }
}) )
def test_mo_by_product(self): def test_mo_by_product(self):
self.move.with_context(test_group_mo=True)._action_confirm(merge=False) self.move.with_context(test_group_mo=True)._action_confirm(merge=False)
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler() 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(len(mo), 1)
self.assertEqual(mo.product_qty, 12) self.assertEqual(mo.product_qty, 12)
# Run again the scheduler to see if quantities are altered # Run again the scheduler to see if quantities are altered
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler() 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(len(mo), 1)
self.assertEqual(mo.product_qty, 12) self.assertEqual(mo.product_qty, 12)
def test_mo_other_date(self): def test_mo_other_date(self):
self.move.write( self.move.write(
{'date_expected': '2018-06-01 20:01:00', {"date_expected": "2018-06-01 20:01:00", "date": "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.move.with_context(test_group_mo=True)._action_confirm(merge=False)
self.ProcurementGroup.with_context(test_group_mo=True).run_scheduler() 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) self.assertEqual(len(mo), 2)
def test_check_mo_grouping_max_hour(self): def test_check_mo_grouping_max_hour(self):