Add button to generate documents in party #043876

This commit is contained in:
Jared Esparza 2021-10-04 12:22:12 +02:00
parent 2193770708
commit ab42e37fc0
4 changed files with 52 additions and 10 deletions

View File

@ -11,7 +11,8 @@ class Document(Workflow, ModelSQL, ModelView):
party = fields.Many2One('party.party', "Party", required=True)
document_type = fields.Many2One(
'certification.document.type', "Document Type", required=True)
type = fields.Char('Type', depends=['document_type'])
type = fields.Function(fields.Char('Type', depends=['document_type']),
'get_type')
text = fields.Char('Text', states={
'invisible': Eval('type') != 'text',
'required': And(
@ -106,6 +107,11 @@ class Document(Workflow, ModelSQL, ModelView):
def expire(cls, documents):
pass
@fields.depends('document_type')
def get_type(self, name):
if self.document_type:
return self.document_type.type
@fields.depends('document_type')
def on_change_with_type(self):
if self.document_type:

View File

@ -1,5 +1,6 @@
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.model import ModelView
class Party(metaclass=PoolMeta):
@ -11,6 +12,13 @@ class Party(metaclass=PoolMeta):
valid_documents = fields.Function(
fields.Boolean("Valid Documents"), 'get_valid_documents')
@classmethod
def __setup__(cls):
super().__setup__()
cls._buttons.update({
'generate_party_documents': {},
})
def get_valid_documents(self, name):
pool = Pool()
partyTypeParty = pool.get('certification.party.type-party.party')
@ -22,15 +30,35 @@ class Party(metaclass=PoolMeta):
else:
return True
@fields.depends('party_types', 'documents')
def on_change_party_types(self):
@classmethod
@ModelView.button
def generate_party_documents(cls, records):
pool = Pool()
Document = pool.get('certification.document')
current_types = [d.document_type for d in self.documents]
for party_type in self.party_types:
for document_type in party_type.document_types:
if document_type.document_type not in current_types:
cls.save(records)
for record in records:
to_delete = []
current_types = [d.document_type for d in record.documents]
expected_types = []
for party_type in record.party_types:
for document_type in party_type.document_types:
expected_types.append(document_type.document_type)
for document in record.documents:
if (not document.text
and not document.attachment
and not document.selection
and document.document_type not in expected_types):
to_delete.append(document)
current_types.remove(document.document_type)
Document.delete(to_delete)
for document_type in expected_types:
if document_type not in current_types:
document = Document()
document.document_type = document_type.document_type.id
document.party = self.id
self.documents += (document,)
document.document_type = document_type.id
document.party = record.id
document.state = 'waiting-approval'
document.type = document_type.type
record.documents += (document,)
cls.save(records)

View File

@ -8,5 +8,10 @@
<field name="inherit" ref="party.party_view_form"/>
<field name="name">party_form</field>
</record>
<record model="ir.model.button" id="generate_party_documents_button">
<field name="name">generate_party_documents</field>
<field name="string">Generate Documents</field>
<field name="model" search="[('model', '=', 'party.party')]" />
</record>
</data>
</tryton>

View File

@ -8,6 +8,9 @@ copyright notices and license types. -->
<field name="documents" colspan="4"/>
<label name="valid_documents"/>
<field name="valid_documents"/>
<group col="-1" colspan="3" id="buttons">
<button name="generate_party_documents"/>
</group>
</page>
</xpath>
</data>