Skip to content

Commit

Permalink
[IMP] mrp_production_batch: new improvements and implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC013 committed Feb 1, 2024
1 parent 73b389f commit 58583ad
Show file tree
Hide file tree
Showing 22 changed files with 701 additions and 158 deletions.
5 changes: 3 additions & 2 deletions mrp_production_batch/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ MRP Production Batch
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:700bba545c72b4ff4d40b265cfbea8d46b2552a01a76ce668c2f6e033444f228
!! source digest: sha256:61fb5987a03dd433cc41146fb2b0a02cde67896a7c73de4607fda46f2e1dbf4a
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -22,7 +22,7 @@ MRP Production Batch

|badge1| |badge2| |badge3|

* Added
* This module manages production batches.

**Table of contents**

Expand Down Expand Up @@ -54,6 +54,7 @@ Contributors

* Kilian Niubo <kniubo@nuobit.com>
* Frank Cespedes <fcespedes@nuobit.com>
* Eric Antones <eantones@nuobit.com>

Maintainers
~~~~~~~~~~~
Expand Down
5 changes: 4 additions & 1 deletion mrp_production_batch/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "MRP Production Batch",
"summary": "Customizations for Oxigen in MRP Production",
"summary": "This module manages production batches.",
"version": "14.0.1.0.0",
"author": "NuoBiT Solutions",
"website": "https://github.com/nuobit/odoo-addons",
Expand All @@ -12,9 +12,12 @@
"license": "AGPL-3",
"data": [
"security/ir.model.access.csv",
"security/mrp_production_batch.xml",
"views/mrp_production_batch_views.xml",
"views/mrp_production_views.xml",
"views/stock_picking_views.xml",
"views/res_config_settings_views.xml",
"views/mrp_bom_line.xml",
"wizard/mrp_production_batch_wizard.xml",
],
}
298 changes: 249 additions & 49 deletions mrp_production_batch/i18n/es.po

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions mrp_production_batch/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from . import mrp_production
from . import mrp_production_batch

# from . import stock_move
from . import stock_picking_type
from . import res_config_settings
from . import mrp_bom_line
from . import stock_production_lot
from . import res_company
18 changes: 18 additions & 0 deletions mrp_production_batch/models/mrp_bom_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright NuoBiT Solutions - Frank Cespedes <fcespedes@nuobit.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class MrpBomLine(models.Model):
_inherit = "mrp.bom.line"

use_in_batch = fields.Boolean()

@api.constrains("use_in_batch")
def _check_use_in_batch(self):
for rec in self:
if len(rec.bom_id.bom_line_ids.filtered(lambda x: x.use_in_batch)) > 1:
raise ValidationError(

Check warning on line 16 in mrp_production_batch/models/mrp_bom_line.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_bom_line.py#L16

Added line #L16 was not covered by tests
_("You can't set more than one component to be used in a batch.")
)
179 changes: 152 additions & 27 deletions mrp_production_batch/models/mrp_production.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright NuoBiT Solutions - Kilian Niubo <kniubo@nuobit.com>
# Copyright NuoBiT Solutions - Frank Cespedes <fcespedes@nuobit.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError, ValidationError


class FakeException(ValidationError):
Expand All @@ -12,15 +13,42 @@ class FakeException(ValidationError):
class MrpProduction(models.Model):
_inherit = "mrp.production"

is_ready_to_produce = fields.Boolean()
error_message = fields.Char()
is_ready_to_produce = fields.Boolean(copy=False)
error_message = fields.Char(copy=False)
production_batch_id = fields.Many2one(
comodel_name="mrp.production.batch",
string="Production Batch",
copy=False,
ondelete="restrict",
domain="[('state', '!=', 'done')]",
)

@api.constrains("lot_producing_id")
def _check_lot_producing_by_batch(self):
for rec in self:
if not self.env.context.get("mrp_production_batch_create"):
if rec.lot_producing_id and rec.production_batch_id:
raise ValidationError(

Check warning on line 31 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L31

Added line #L31 was not covered by tests
_(
"You can't set a lot producing for a production %s "
"that belongs to a batch: %s.\n"
"It must be processed from the batch."
)
% (rec.name, rec.production_batch_id.name)
)

@api.constrains("production_batch_id")
def _check_production_batch(self):
for rec in self:
if rec.lot_producing_id:
raise ValidationError(

Check warning on line 44 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L44

Added line #L44 was not covered by tests
_(
"You can't set a production batch for a production %s "
"because it has a lot/serial number."
)
% rec.name
)

@api.constrains("state")
def _check_state_batch_creation(self):
for rec in self:
Expand All @@ -34,7 +62,10 @@ def _check_state_batch_creation(self):
% rec.production_batch_id.name
)
elif rec.state == "done":
if not self.env.context.get("mrp_production_batch_create", False):
if (
not self.env.context.get("mrp_production_batch_create", False)
and not rec.scrap_ids
):
raise ValidationError(

Check warning on line 69 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L69

Added line #L69 was not covered by tests
_(
"You can't change the state of a production %s "
Expand All @@ -44,6 +75,20 @@ def _check_state_batch_creation(self):
% (rec.name, rec.production_batch_id.name)
)

@api.constrains("production_batch_id")
def _check_operation_type(self):
for rec in self:
if (
rec.picking_type_id.warehouse_id
!= rec.production_batch_id.operation_type.warehouse_id
):
raise ValidationError(

Check warning on line 85 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L85

Added line #L85 was not covered by tests
_(
"The warehouse of the batch must be the same"
" as the warehouse of the productions."
)
)

def _check_production_to_batch_consistency(self, mrp_productions):
if not mrp_productions:
raise ValidationError(_("No productions selected"))

Check warning on line 94 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L94

Added line #L94 was not covered by tests
Expand All @@ -63,29 +108,37 @@ def _check_production_to_batch_consistency(self, mrp_productions):
raise ValidationError(

Check warning on line 108 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L108

Added line #L108 was not covered by tests
_("Some of the selected productions have different warehouses")
)
picking_type_ids = mrp_productions.picking_type_id
if len(picking_type_ids) > 1:
if len(mrp_productions.picking_type_id) > 1:
raise ValidationError(

Check warning on line 112 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L112

Added line #L112 was not covered by tests
_(
"Some of the selected productions have different picking types:%s"
% picking_type_ids.mapped("name")
% mrp_productions.picking_type_id.mapped("name")
)
)

def mrp_production_batch_create_wizard_action(self):
model = self.env.context.get("active_model")
mrp_production_ids = self.env[model].browse(self.env.context.get("active_ids"))
if (
"active_model" not in self.env.context
or "active_ids" not in self.env.context
):
raise ValidationError(

Check warning on line 124 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L124

Added line #L124 was not covered by tests
_(
"An unexpected error has occurred. There's no `active_model` or "
"`active_ids` in the context. This is a technical error, please "
"contact technical support."
)
)
model, active_ids = [
self.env.context.get(x) for x in ["active_model", "active_ids"]
]
mrp_production_ids = self.env[model].browse(active_ids)
self._check_production_to_batch_consistency(mrp_production_ids)
ctx = dict(self.env.context, active_ids=self.ids)

view_form = self.env.ref(

Check warning on line 136 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L134-L136

Added lines #L134 - L136 were not covered by tests
"mrp_production_batch.wizard_mrp_production_batch_view_form"
"mrp_production_batch.mrp_production_batch_wizard_view_form"
)
res_id = self.env["mrp.production.batch.wizard"].create(

Check warning on line 139 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L139

Added line #L139 was not covered by tests
{
"warehouse_id": mrp_production_ids.mapped("picking_type_id")
.mapped("warehouse_id")
.id,
"warehouse_id": mrp_production_ids.picking_type_id.warehouse_id.id,
}
)
res = {

Check warning on line 144 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L144

Added line #L144 was not covered by tests
Expand All @@ -97,36 +150,46 @@ def mrp_production_batch_create_wizard_action(self):
"views": [(view_form.id, "form")],
"view_id": view_form.id,
"type": "ir.actions.act_window",
"context": ctx,
"context": dict(self.env.context, active_ids=self.ids),
}
return res

Check warning on line 155 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L155

Added line #L155 was not covered by tests

def _action_generate_consumption_wizard(self, consumption_issues):
if not self.env.context.get("mrp_production_batch_create"):
return super()._action_generate_consumption_wizard(consumption_issues)

Check warning on line 159 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L159

Added line #L159 was not covered by tests
if self.bom_id.bom_line_ids.product_id.ids != self.move_raw_ids.product_id.ids:
raise ValidationError(

Check warning on line 161 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L161

Added line #L161 was not covered by tests
_(
"A problem has been detected in production %s. Make sure you "
"have added all the necessary components from the materials "
"list. Please check this and, once corrected, try again."
)
% self.mapped("name")
)
else:
raise ValidationError(

Check warning on line 170 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L170

Added line #L170 was not covered by tests
_(
"Production %s have not recorded produced quantities yet."
% self.mapped("name")
"A problem has been detected in production %s. Verify that the"
" quantities to be consumed are indicated correctly. Please "
"check this and, once corrected, try again."
)
% self.mapped("name")
)

def _action_generate_immediate_wizard(self):
if not self.env.context.get("mrp_production_batch_create"):
return super()._action_generate_immediate_wizard()
else:
raise ValidationError(
_(
"Production %s have not recorded produced quantities yet."
% self.mapped("name")
)
raise ValidationError(

Check warning on line 182 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L181-L182

Added lines #L181 - L182 were not covered by tests
_(
"Production %s have not recorded produced quantities yet."
% self.mapped("name")
)
)

# TODO: xml action
def action_view_production_batch(self):
self.ensure_one()
view = self.env.ref("mrp_production_batch.mrp_production_batch_form_view")
view = self.env.ref("mrp_production_batch.mrp_production_batch_view_form")
return {

Check warning on line 193 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L191-L193

Added lines #L191 - L193 were not covered by tests
"name": _("Detailed Operations"),
"type": "ir.actions.act_window",
Expand All @@ -137,15 +200,77 @@ def action_view_production_batch(self):
"res_id": self.production_batch_id.id,
}

# TODO: Refactor translation management and raise of "You need to supply..."
def action_check_with_batch(self):
self.ensure_one()
try:
with self.env.cr.savepoint():
self.with_context(mrp_production_batch_create=True).button_mark_done()
self_wc = self.with_context(mrp_production_batch_create=True)
lot = self_wc.env["stock.production.lot"].create(

Check warning on line 209 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L205-L209

Added lines #L205 - L209 were not covered by tests
{
"name": "",
"product_id": self.product_id.id,
"company_id": self.company_id.id,
}
)
self_wc.lot_producing_id = lot.id
self_wc.button_mark_done()
raise FakeException("")

Check warning on line 218 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L216-L218

Added lines #L216 - L218 were not covered by tests
except FakeException:
self.is_ready_to_produce = True
self.error_message = False
except Exception as e:
except (UserError, ValidationError) as e:
self.is_ready_to_produce = False
self.error_message = e.name

Check warning on line 224 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L220-L224

Added lines #L220 - L224 were not covered by tests

def create_batch_lot_by_bom(self):
self.ensure_one()

Check warning on line 227 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L227

Added line #L227 was not covered by tests
if not self.bom_id:
raise ValidationError(

Check warning on line 229 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L229

Added line #L229 was not covered by tests
_(
"Bill of Materials is required for the production. "
"Please unbind the batch production."
)
)

batch_component = self.bom_id.bom_line_ids.filtered(
lambda x: x.use_in_batch
).product_id
if not batch_component:
raise ValidationError(

Check warning on line 240 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L240

Added line #L240 was not covered by tests
_("Please assign a batch compatible product in the Bill of Materials")
)
component_lot = False

Check warning on line 243 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L243

Added line #L243 was not covered by tests
for line in self.move_raw_ids.move_line_ids:
if line.product_id == batch_component:
component_lot = line.lot_id

Check warning on line 246 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L246

Added line #L246 was not covered by tests
if not component_lot:
raise ValidationError(

Check warning on line 248 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L248

Added line #L248 was not covered by tests
_(
"No compatible batch product found in the material list."
" Ensure a product marked for batch use is included."
)
)
batch_sequence = (

Check warning on line 254 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L254

Added line #L254 was not covered by tests
self.env["res.company"]
.browse(self.company_id.id)
.sequence_production_batch_id.next_by_id()
)
return component_lot.name + batch_sequence

Check warning on line 259 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L259

Added line #L259 was not covered by tests

def action_generate_batch_serial(self):
self.ensure_one()
seq_name = self.create_batch_lot_by_bom()
self.lot_producing_id = self.env["stock.production.lot"].create(

Check warning on line 264 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L262-L264

Added lines #L262 - L264 were not covered by tests
{
"name": seq_name,
"product_id": self.product_id.id,
"company_id": self.company_id.id,
}
)

def write(self, values):
if "production_batch_id" in values and not values["production_batch_id"]:
values["is_ready_to_produce"] = False
values["error_message"] = False
return super().write(values)

Check warning on line 276 in mrp_production_batch/models/mrp_production.py

View check run for this annotation

Codecov / codecov/patch

mrp_production_batch/models/mrp_production.py#L274-L276

Added lines #L274 - L276 were not covered by tests
Loading

0 comments on commit 58583ad

Please sign in to comment.