diff --git a/account_banking_pain_base/__init__.py b/account_banking_pain_base/__init__.py index a54725775..6662843e6 100644 --- a/account_banking_pain_base/__init__.py +++ b/account_banking_pain_base/__init__.py @@ -23,3 +23,4 @@ from . import payment_line from . import payment_mode from . import company +from . import banking_export_pain diff --git a/account_banking_pain_base/__openerp__.py b/account_banking_pain_base/__openerp__.py index 8a66aaafa..5d461865e 100644 --- a/account_banking_pain_base/__openerp__.py +++ b/account_banking_pain_base/__openerp__.py @@ -27,6 +27,9 @@ 'website': 'http://openerp-community-association.org/', 'category': 'Hidden', 'depends': ['account_banking_payment_export'], + 'external_dependencies': { + 'python': ['unidecode', 'lxml'], + }, 'data': [ 'payment_line_view.xml', 'payment_mode_view.xml', diff --git a/account_banking_pain_base/banking_export_pain.py b/account_banking_pain_base/banking_export_pain.py new file mode 100644 index 000000000..6f84033a8 --- /dev/null +++ b/account_banking_pain_base/banking_export_pain.py @@ -0,0 +1,186 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# PAIN Base module for OpenERP +# Copyright (C) 2013 Akretion (http://www.akretion.com) +# @author: Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import orm +from openerp.tools.translate import _ +from openerp.tools.safe_eval import safe_eval +from unidecode import unidecode +from lxml import etree +from openerp import tools +import logging + +logger = logging.getLogger(__name__) + + +class banking_export_pain(orm.AbstractModel): + _name = 'banking.export.pain' + + def _validate_iban(self, cr, uid, iban, context=None): + '''if IBAN is valid, returns IBAN + if IBAN is NOT valid, raises an error message''' + partner_bank_obj = self.pool.get('res.partner.bank') + if partner_bank_obj.is_iban_valid(cr, uid, iban, context=context): + return iban.replace(' ', '') + else: + raise orm.except_orm( + _('Error:'), _("This IBAN is not valid : %s") % iban) + + def _prepare_field( + self, cr, uid, field_name, field_value, eval_ctx, max_size=0, + convert_to_ascii=False, context=None): + '''This function is designed to be inherited !''' + assert isinstance(eval_ctx, dict), 'eval_ctx must contain a dict' + try: + value = safe_eval(field_value, eval_ctx) + # SEPA uses XML ; XML = UTF-8 ; UTF-8 = support for all characters + # But we are dealing with banks... + # and many banks don't want non-ASCCI characters ! + # cf section 1.4 "Character set" of the SEPA Credit Transfer + # Scheme Customer-to-bank guidelines + if convert_to_ascii: + value = unidecode(value) + except: + line = eval_ctx.get('line') + if line: + raise orm.except_orm( + _('Error:'), + _("Cannot compute the '%s' of the Payment Line with Invoice Reference '%s'.") + % (field_name, self.pool['account.invoice'].name_get( + cr, uid, [line.ml_inv_ref.id], context=context)[0][1])) + else: + raise orm.except_orm( + _('Error:'), + _("Cannot compute the '%s'.") % field_name) + if not isinstance(value, (str, unicode)): + raise orm.except_orm( + _('Field type error:'), + _("The type of the field '%s' is %s. It should be a string or unicode.") + % (field_name, type(value))) + if not value: + raise orm.except_orm( + _('Error:'), + _("The '%s' is empty or 0. It should have a non-null value.") + % field_name) + if max_size and len(value) > max_size: + value = value[0:max_size] + return value + + def _validate_xml(self, cr, uid, xml_string, pain_xsd_file): + xsd_etree_obj = etree.parse( + tools.file_open(pain_xsd_file)) + official_pain_schema = etree.XMLSchema(xsd_etree_obj) + + try: + root_to_validate = etree.fromstring(xml_string) + official_pain_schema.assertValid(root_to_validate) + except Exception, e: + logger.warning( + "The XML file is invalid against the XML Schema Definition") + logger.warning(xml_string) + logger.warning(e) + raise orm.except_orm( + _('Error:'), + _('The generated XML file is not valid against the official XML Schema Definition. The generated XML file and the full error have been written in the server logs. Here is the error, which may give you an idea on the cause of the problem : %s') + % str(e)) + return True + + def generate_initiating_party_block( + self, cr, uid, parent_node, sepa_export, gen_args, + context=None): + my_company_name = self._prepare_field( + cr, uid, 'Company Name', + 'sepa_export.payment_order_ids[0].mode.bank_id.partner_id.name', + {'sepa_export': sepa_export}, gen_args.get('name_maxsize'), + convert_to_ascii=gen_args.get('convert_to_ascii'), context=context) + initiating_party_1_8 = etree.SubElement(parent_node, 'InitgPty') + initiating_party_name = etree.SubElement(initiating_party_1_8, 'Nm') + initiating_party_name.text = my_company_name + initiating_party_identifier = self.pool['res.company'].\ + _get_initiating_party_identifier( + cr, uid, sepa_export.payment_order_ids[0].company_id.id, + context=context) + initiating_party_issuer = \ + sepa_export.payment_order_ids[0].company_id.initiating_party_issuer + if initiating_party_identifier and initiating_party_issuer: + iniparty_id = etree.SubElement(initiating_party_1_8, 'Id') + iniparty_org_id = etree.SubElement(iniparty_id, 'OrgId') + iniparty_org_other = etree.SubElement(iniparty_org_id, 'Othr') + iniparty_org_other_id = etree.SubElement(iniparty_org_other, 'Id') + iniparty_org_other_id.text = initiating_party_identifier + iniparty_org_other_issuer = etree.SubElement( + iniparty_org_other, 'Issr') + iniparty_org_other_issuer.text = initiating_party_issuer + return True + + def generate_party_bic( + self, cr, uid, parent_node, party_type, party_type_label, bic, + eval_ctx, gen_args, context=None): + '''Generate the piece of the XML file corresponding to BIC + This code is mutualized between TRF and DD''' + party_agent = etree.SubElement(parent_node, '%sAgt' % party_type) + party_agent_institution = etree.SubElement( + party_agent, 'FinInstnId') + party_agent_bic = etree.SubElement( + party_agent_institution, gen_args.get('bic_xml_tag')) + party_agent_bic.text = self._prepare_field( + cr, uid, '%s BIC' % party_type_label, bic, eval_ctx, + convert_to_ascii=gen_args.get('convert_to_ascii'), context=context) + return True + + def generate_party_block( + self, cr, uid, parent_node, party_type, order, name, iban, bic, + eval_ctx, gen_args, context=None): + '''Generate the piece of the XML file corresponding to Name+IBAN+BIC + This code is mutualized between TRF and DD''' + assert order in ('B', 'C'), "Order can be 'B' or 'C'" + if party_type == 'Cdtr': + party_type_label = 'Creditor' + elif party_type == 'Dbtr': + party_type_label = 'Debtor' + # At C level, the order is : BIC, Name, IBAN + # At B level, the order is : Name, IBAN, BIC + if order == 'C': + self.generate_party_bic( + cr, uid, parent_node, party_type, party_type_label, bic, + eval_ctx, gen_args, context=context) + party = etree.SubElement(parent_node, party_type) + party_name = etree.SubElement(party, 'Nm') + party_name.text = self._prepare_field( + cr, uid, '%s Name' % party_type_label, name, eval_ctx, + gen_args.get('name_maxsize'), + convert_to_ascii=gen_args.get('convert_to_ascii'), context=context) + party_account = etree.SubElement( + parent_node, '%sAcct' % party_type) + party_account_id = etree.SubElement(party_account, 'Id') + party_account_iban = etree.SubElement( + party_account_id, 'IBAN') + piban = self._prepare_field( + cr, uid, '%s IBAN' % party_type_label, iban, eval_ctx, + convert_to_ascii=gen_args.get('convert_to_ascii'), + context=context) + viban = self._validate_iban(cr, uid, piban, context=context) + party_account_iban.text = viban + if order == 'B': + self.generate_party_bic( + cr, uid, parent_node, party_type, party_type_label, bic, + eval_ctx, gen_args, context=context) + return True diff --git a/account_banking_pain_base/company.py b/account_banking_pain_base/company.py index b40f64440..c11c13d9b 100644 --- a/account_banking_pain_base/company.py +++ b/account_banking_pain_base/company.py @@ -38,7 +38,10 @@ class res_company(orm.Model): self, cr, uid, company_id, context=None): '''This function is designed to be inherited by localization modules''' assert isinstance(company_id, int), 'Only one company ID' - return False + # TODO REMOOOOOOOOOOOOOOOOOOOVE BEFORE COMMIT + + return self.browse(cr, uid, company_id).partner_id.vat + #return False def _initiating_party_issuer_default(self, cr, uid, context=None): '''This function is designed to be inherited by localization modules'''