Replace SQL by code that uses the ORM, as suggested by Pedro

Move part of the migration code to res_company.py, so that it's possible to inherit in other modules
This commit is contained in:
Alexis de Lattre
2015-06-02 11:13:26 +02:00
parent e45505ddbf
commit d58bbfa6e8
3 changed files with 45 additions and 35 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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