lims_industry: avoid deleting components associated with samples

This commit is contained in:
Adrián Bernardi 2021-04-06 18:30:16 -03:00
parent 18108b9ac8
commit b10de62fa5
3 changed files with 29 additions and 0 deletions

View File

@ -6,6 +6,8 @@ from trytond.model import ModelSQL, ModelView, fields, Unique
from trytond.pool import Pool
from trytond.pyson import Eval, If
from trytond.transaction import Transaction
from trytond.exceptions import UserError
from trytond.i18n import gettext
class Plant(ModelSQL, ModelView):
@ -400,6 +402,22 @@ class Component(ModelSQL, ModelView):
res.append(component)
return res
@classmethod
def delete(cls, components):
cls.check_delete(components)
super().delete(components)
@classmethod
def check_delete(cls, components):
Sample = Pool().get('lims.sample')
for component in components:
samples = Sample.search_count([
('component', '=', component.id),
])
if samples != 0:
raise UserError(gettext('lims_industry.msg_delete_component',
component=component.get_rec_name(None)))
def get_rec_name(self, name):
res = self.type.rec_name
if self.brand:

View File

@ -837,6 +837,14 @@ msgstr ""
"Ya existe para el equipo un componente del mismo tipo con la misma "
"descripción"
msgctxt "model:ir.message,text:msg_delete_component"
msgid ""
"You can not delete component \"%(component)s\" because it is associated with"
" a sample"
msgstr ""
"No puede eliminar el componente «%(component)s» porque está asociado a una "
"muestra"
msgctxt "model:ir.message,text:msg_edit_entry_party"
msgid "You must select all samples from the same entry to change the Party"
msgstr ""

View File

@ -25,6 +25,9 @@
<record model="ir.message" id="msg_equipment_name_unique">
<field name="text">There is already an equipment with the same name for the plant</field>
</record>
<record model="ir.message" id="msg_delete_component">
<field name="text">You can not delete component "%(component)s" because it is associated with a sample</field>
</record>
<record model="ir.message" id="msg_party_no_email">
<field name="text">Party "%(party)s" has no Email</field>
</record>