diff --git a/edocument.py b/edocument.py index b1d7eec..9036910 100644 --- a/edocument.py +++ b/edocument.py @@ -6,6 +6,8 @@ from sql.conditionals import Coalesce from sql.operators import Equal as SqlEqual from trytond.pool import Pool from trytond.transaction import Transaction +from trytond.i18n import gettext +from trytond.exceptions import UserError from genshi.template import TextTemplate from io import open import oyaml as yaml @@ -42,10 +44,13 @@ class EdocumentTemplate(ModelView, ModelSQL): ('document_party_exclude', Exclude(t, (t.message_type, SqlEqual), (Coalesce(t.party, -1), SqlEqual)), - 'Combination of message type and party must be unique.')] + 'edocument_edifact.msg_edi_party_message_type')] def get_rec_name(self, name): - return self.message_type + ' ' + self.party.name + return ' '.join([ + self.message_type, + self.party and self.party.name or '' + ]) class EdocumentMessage(ModelView, ModelSQL): @@ -90,13 +95,13 @@ class EdocumentMessage(ModelView, ModelSQL): class EdocumentMixin(object): + messages = fields.One2Many('edocument.message', 'origin', 'Message') edocument_processed = fields.Function( fields.Boolean('Processed'), 'get_edocument_processed', searcher='search_edocument_processed') - edi_message = fields.Function( - fields.Char('EDI Code'), 'get_edi_message') + fields.Char('EDI Message'), 'get_edi_message') def get_edocument_processed(self, name=None): return len(self.messages) > 0 @@ -239,22 +244,10 @@ class EdocumentExportMixin(object): edi_file.write(file) edi_file.close() - @classmethod - def __setup__(cls): - super(cls).__setup__() - cls._error_messages.update({ - 'company_unique': ('Cannot send invoices from more than ' - '1 company.'), - 'EDI_sender': 'Company %s lacks EDI sender identifier.', - 'EDI_sequence': - 'EDI Sequence must be defined in Edocument Configuration.', - 'EDI_receiver': 'Party %s lacks EDI receiver identifier.', - 'EDI_export_path': - 'EDI export path is not defined in Edocument Configuration'}) - def _edi_template(self, message_type, party): pool = Pool() EdocumentTemplate = pool.get('edocument.template') + templates = EdocumentTemplate.search([ ('message_type', '=', message_type), ('party', '=', party)]) @@ -269,9 +262,9 @@ class EdocumentExportMixin(object): def generate_message(self, message_type, export_again=False): pool = Pool() - _model_name = Transaction().context['active_model'] - ActiveModel = pool.get(_model_name) + ActiveModel = self.model Message = pool.get('edocument.message') + domain = [('id', 'in', Transaction().context['active_ids'])] if not export_again: domain.append(('edocument_processed', '=', False)) @@ -280,18 +273,20 @@ class EdocumentExportMixin(object): return None, None companies = set(x.company for x in records) if len(companies) > 1: - self.raise_user_error('company_unique') + raise UserError(gettext('edocument_edifact.msg_company_unique')) company, = companies edifact_sender = [x for x in company.party.identifiers if x.type == 'EDI_sender'] if not edifact_sender: - self.raise_user_error('EDI_sender', company.party.name) + raise UserError(gettext('edocument_edifact.msg_EDI_sender', + company=company.party.name)) parties = set(x.party for x in records) party, = parties edifact_receiver = [x for x in party.identifiers if x.type == 'EDI_receiver'] if not edifact_receiver: - self.raise_user_error('EDI_receiver', party.name) + raise UserError(gettext('edocument_edifact.msg_EDI_receiver', + party=party.name)) template, encoding = self._edi_template(message_type, party) loader = TextTemplate(template) for record in records: @@ -302,7 +297,7 @@ class EdocumentExportMixin(object): "message": message, "sender": edifact_sender[0].code, "receiver": edifact_receiver[0].code, - "records": [[message.rec_name, record]], + "records": [[message.code, record]], "date": message.create_date.strftime("%y%m%d"), "time": message.create_date.strftime("%H%M") } @@ -319,5 +314,5 @@ class EdocumentExportMixin(object): file_name = os.path.join(conf.export_path, name) self._write_file(file_name, message.message, encoding=encoding) if not message.code: - self.raise_user_error('EDI_sequence') + raise UserError(gettext('edocument_edifact.msg_EDI_sequence')) return message.message, message_type + message.code + '.EDI' diff --git a/edocument.xml b/edocument.xml index 8303bef..5df2057 100644 --- a/edocument.xml +++ b/edocument.xml @@ -4,7 +4,6 @@ - edocument Electronic Document Electronic Document sequence - edocument + - + edocument.template diff --git a/locale/es.po b/locale/es.po index befd282..61cedac 100644 --- a/locale/es.po +++ b/locale/es.po @@ -2,10 +2,30 @@ msgid "" msgstr "Content-Type: text/plain; charset=utf-8\n" -msgctxt "error:edocument.template:" +msgctxt "model:ir.message,text:msg_edi_party_message_type" msgid "Combination of message type and party must be unique." msgstr "La combinación de tipo de mensaje y tercero debe ser única." +msgctxt "model:ir.message,text:msg_company_unique" +msgid "Cannot send invoices from more than 1 company." +msgstr "No se pueden enviar facturas desde más de una empresa." + +msgctxt "model:ir.message,text:msg_EDI_sender" +msgid "Company \"%(company)s\" lacks EDI sender identifier." +msgstr "La empresa \"%(company)s\" carece del identificador EDI de emisor." + +msgctxt "model:ir.message,text:msg_EDI_sequence" +msgid "EDI Sequence must be defined in Edocument Configuration." +msgstr "Secuencia EDI debe definirse en Configuración de Documentación Electrónica." + +msgctxt "model:ir.message,text:msg_EDI_receiver" +msgid "Party \"%(party)s\" lacks EDI receiver identifier." +msgstr "El tercero \"%(party)s\" carece del identificador EDI de receptor" + +msgctxt "model:ir.message,text:msg_EDI_export_path" +msgid "EDI export path is not defined in Edocument Configuration." +msgstr "Ruta de exportación EDI no está definida en Configuración de Documentación Electrónica." + msgctxt "field:edocument.configuration,create_date:" msgid "Create Date" msgstr "Fecha de creación" diff --git a/message.xml b/message.xml new file mode 100644 index 0000000..6bb5cf7 --- /dev/null +++ b/message.xml @@ -0,0 +1,25 @@ + + + + + + Cannot send invoices from more than 1 company. + + + Combination of message type and party must be unique. + + + Company "%(company)s" lacks EDI sender identifier. + + + EDI Sequence must be defined in Edocument Configuration. + + + Party "%(party)s" lacks EDI receiver identifier. + + + EDI export path is not defined in Edocument Configuration. + + + \ No newline at end of file diff --git a/party.py b/party.py index b5d6516..da89581 100644 --- a/party.py +++ b/party.py @@ -35,12 +35,11 @@ class PartyIdentifier(metaclass=PoolMeta): __name__ = 'party.identifier' @classmethod - def __setup__(cls): - super(PartyIdentifier, cls).__setup__() - cls.type.selection.extend([ + def get_types(cls): + return super().get_types() + [ ('EDI_sender', 'EDI Sender'), ('EDI_receiver', 'EDI Receiver'), ('EDI_supplier', 'EDI Supplier'), ('EDI_payer', 'EDI Payer'), ('EDI_buyer', 'EDI Buyer'), - ('EDI_invoice', 'EDI Invoice')]) + ('EDI_invoice', 'EDI Invoice')] diff --git a/party.xml b/party.xml index 1b06d24..768b46e 100644 --- a/party.xml +++ b/party.xml @@ -5,7 +5,7 @@ party.party - + party_form diff --git a/tryton.cfg b/tryton.cfg index a90c999..d2d97b8 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -17,3 +17,4 @@ xml: incoterm.xml edocument.xml party.xml + message.xml