diff --git a/account_banking_pain_base/migrations/8.0.0.2/post-migration.py b/account_banking_pain_base/migrations/8.0.0.2/post-migration.py index eae959b03..bb6322681 100644 --- a/account_banking_pain_base/migrations/8.0.0.2/post-migration.py +++ b/account_banking_pain_base/migrations/8.0.0.2/post-migration.py @@ -21,10 +21,12 @@ from openerp.addons.account_banking_pain_base.post_install\ import set_default_initiating_party +from openerp import pooler def migrate(cr, version): if not version: return - set_default_initiating_party(cr, None) + pool = pooler.get_pool(cr.dbname) + set_default_initiating_party(cr, pool) diff --git a/account_banking_pain_base/models/res_company.py b/account_banking_pain_base/models/res_company.py index 465984559..bb656b55a 100644 --- a/account_banking_pain_base/models/res_company.py +++ b/account_banking_pain_base/models/res_company.py @@ -22,7 +22,10 @@ # ############################################################################## -from openerp import models, fields +from openerp import models, fields, api +import logging + +logger = logging.getLogger(__name__) class ResCompany(models.Model): @@ -36,3 +39,33 @@ class ResCompany(models.Model): string='Initiating Party Identifier', size=35, help="This will be used as the 'Initiating Party Identifier' in " "the PAIN files generated by Odoo.") + + @api.model + def _default_initiating_party(self, company): + '''This method is called from post_install.py, which itself is also + called from migrations/8.0.0.2/post-migration.py''' + party_issuer_per_country = { + 'BE': 'KBO-BCE', # KBO-BCE = the registry of companies in Belgium + } + logger.debug( + 'Calling _default_initiating_party on company %s', company.name) + country_code = company.country_id.code + if not company.initiating_party_issuer: + if country_code and country_code in party_issuer_per_country: + company.write({ + 'initiating_party_issuer': + party_issuer_per_country[country_code]}) + logger.info( + 'Updated initiating_party_issuer on company %s', + company.name) + party_identifier = False + if not company.initiating_party_identifier: + if company.vat and country_code: + if country_code == 'BE': + party_identifier = company.vat[2:].replace(' ', '') + if party_identifier: + company.write({ + 'initiating_party_identifier': party_identifier}) + logger.info( + 'Updated initiating_party_identifier on company %s', + company.name) diff --git a/account_banking_pain_base/post_install.py b/account_banking_pain_base/post_install.py index 2ae817710..d48fd2f63 100644 --- a/account_banking_pain_base/post_install.py +++ b/account_banking_pain_base/post_install.py @@ -20,38 +20,13 @@ # ############################################################################## +from openerp import SUPERUSER_ID -def set_default_initiating_party(cr, registry): - party_issuer_per_country = { - 'BE': 'KBO-BCE', # KBO-BCE = the registry of companies in Belgium - } - cr.execute(''' - SELECT - res_company.id, - res_country.code AS country_code, - res_partner.vat, - res_company.initiating_party_identifier, - res_company.initiating_party_issuer - FROM res_company - LEFT JOIN res_partner ON res_partner.id = res_company.partner_id - LEFT JOIN res_country ON res_country.id = res_partner.country_id - ''') - for company in cr.dictfetchall(): - country_code = company['country_code'] - if not company['initiating_party_issuer']: - if country_code and country_code in party_issuer_per_country: - cr.execute( - 'UPDATE res_company SET initiating_party_issuer=%s ' - 'WHERE id=%s', - (party_issuer_per_country[country_code], company['id'])) - party_identifier = False - if not company['initiating_party_identifier']: - if company['vat'] and country_code: - if country_code == 'BE': - party_identifier = company['vat'][2:].replace(' ', '') - if party_identifier: - cr.execute( - 'UPDATE res_company SET initiating_party_identifier=%s ' - 'WHERE id=%s', - (party_identifier, company['id'])) + +def set_default_initiating_party(cr, pool): + company_ids = pool['res.company'].search(cr, SUPERUSER_ID, []) + companies = pool['res.company'].browse(cr, SUPERUSER_ID, company_ids) + for company in companies: + pool['res.company']._default_initiating_party( + cr, SUPERUSER_ID, company) return