2018-10-16 23:30:19 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2019-09-06 00:15:48 +02:00
|
|
|
import os
|
2018-10-16 23:30:19 +02:00
|
|
|
|
|
|
|
dbname = sys.argv[1]
|
|
|
|
config_file = sys.argv[2]
|
2019-09-06 00:15:48 +02:00
|
|
|
digits = int(os.environ['upgrade_account_chart_digits'])
|
|
|
|
domain = os.environ.get('upgrade_account_chart_domain')
|
2019-05-09 12:12:30 +02:00
|
|
|
if domain:
|
|
|
|
domain = eval(domain)
|
2019-02-07 10:31:23 +01:00
|
|
|
|
2018-10-16 23:30:19 +02:00
|
|
|
from trytond.config import config as CONFIG
|
|
|
|
CONFIG.update_etc(config_file)
|
|
|
|
|
|
|
|
from trytond.pool import Pool
|
2020-05-11 19:52:59 +02:00
|
|
|
from trytond.transaction import Transaction
|
2018-10-16 23:30:19 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
Pool.start()
|
|
|
|
pool = Pool(dbname)
|
|
|
|
pool.init()
|
|
|
|
|
|
|
|
context = {}
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
ch.setLevel(logging.DEBUG)
|
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
ch.setFormatter(formatter)
|
|
|
|
logger.addHandler(ch)
|
|
|
|
|
2019-02-07 10:31:23 +01:00
|
|
|
|
2018-10-16 23:30:19 +02:00
|
|
|
with Transaction().start(dbname, 1, context=context):
|
|
|
|
Company = pool.get('company.company')
|
|
|
|
ModelData = pool.get('ir.model.data')
|
|
|
|
Configuration = pool.get('account.configuration')
|
|
|
|
Account = pool.get('account.account')
|
|
|
|
AccountTemplate = pool.get('account.account.template')
|
2018-11-19 14:50:49 +01:00
|
|
|
Party = pool.get('party.party')
|
|
|
|
|
2018-12-17 14:18:04 +01:00
|
|
|
party = Party.search([('name', '=', 'Generic for Party required Accounts')])
|
2018-11-19 14:50:49 +01:00
|
|
|
if not party:
|
2020-09-15 11:25:59 +02:00
|
|
|
party = Party(name='Generic for Party required Accounts', code='GPRA')
|
2018-11-19 14:50:49 +01:00
|
|
|
party.save()
|
|
|
|
else:
|
|
|
|
party, = party
|
|
|
|
|
2019-05-09 12:12:30 +02:00
|
|
|
user_obj = pool.get('res.user')
|
|
|
|
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
|
|
|
|
user_id = user.id
|
|
|
|
|
2018-11-19 14:50:49 +01:00
|
|
|
cursor = Transaction().connection.cursor()
|
2019-05-16 12:32:12 +02:00
|
|
|
|
|
|
|
to_save = []
|
2019-06-23 15:29:02 +02:00
|
|
|
for template in AccountTemplate.search([]):
|
|
|
|
if template.type or template.childs:
|
2019-05-16 12:32:12 +02:00
|
|
|
continue
|
2019-06-23 15:29:02 +02:00
|
|
|
code = template.code[:-1]
|
|
|
|
cursor.execute(
|
|
|
|
"select id, code, type from account_account_template where code like '%s' and type > 0 limit 1" % code)
|
|
|
|
res = cursor.fetchone()
|
|
|
|
if not res:
|
2019-06-20 15:23:14 +02:00
|
|
|
code = code[:-1]
|
2019-06-23 15:29:02 +02:00
|
|
|
cursor.execute(
|
|
|
|
"select id, code, type from account_account_template where code like '%s' and type > 0 limit 1" % code)
|
|
|
|
res = cursor.fetchone()
|
2019-05-16 12:32:12 +02:00
|
|
|
|
2019-06-23 15:29:02 +02:00
|
|
|
if res:
|
|
|
|
id, code, type = res
|
2019-07-02 12:18:08 +02:00
|
|
|
template.type = type
|
2019-06-23 15:29:02 +02:00
|
|
|
to_save.append(template)
|
2019-05-16 12:32:12 +02:00
|
|
|
|
2019-06-23 15:29:02 +02:00
|
|
|
AccountTemplate.save(to_save)
|
2019-05-17 10:32:07 +02:00
|
|
|
Transaction().commit()
|
2019-05-16 12:32:12 +02:00
|
|
|
|
2018-11-19 14:50:49 +01:00
|
|
|
cursor.execute(''' update account_move_line set party=%s where id in (
|
|
|
|
select l.id from account_move_line l, account_account a where
|
|
|
|
l.account=a.id and a.party_required and l.party is null) ''' %
|
|
|
|
party.id)
|
2018-10-16 23:30:19 +02:00
|
|
|
|
|
|
|
UpdateChart = pool.get('account.update_chart', type='wizard')
|
|
|
|
|
2018-11-19 14:50:49 +01:00
|
|
|
Account.parent.left = None
|
|
|
|
Account.parent.right = None
|
2023-10-11 12:43:49 +02:00
|
|
|
Account._mptt_fields = set()
|
2018-11-19 14:50:49 +01:00
|
|
|
|
2019-05-09 12:12:30 +02:00
|
|
|
if domain is None:
|
2023-10-11 12:43:49 +02:00
|
|
|
domain=[]
|
2018-12-20 17:08:59 +01:00
|
|
|
|
|
|
|
for company in Company.search(domain):
|
|
|
|
logger.info("company %s" % company.id)
|
2023-10-11 12:43:49 +02:00
|
|
|
user.companies += (company,)
|
2019-05-09 12:12:30 +02:00
|
|
|
user.company = company.id
|
|
|
|
user.save()
|
2018-10-16 23:30:19 +02:00
|
|
|
with Transaction().set_context(company=company.id):
|
2020-10-21 11:23:49 +02:00
|
|
|
config = Configuration(1)
|
|
|
|
|
|
|
|
if not config.default_account_code_digits:
|
|
|
|
config.default_account_code_digits = digits
|
|
|
|
config.force_digits = False
|
|
|
|
config.save()
|
|
|
|
|
|
|
|
print("digits:", digits, config.default_account_code_digits)
|
2018-11-19 14:50:49 +01:00
|
|
|
|
2018-10-16 23:30:19 +02:00
|
|
|
template = AccountTemplate(ModelData.get_id('account_es', 'pgc_0'))
|
2019-05-09 12:12:30 +02:00
|
|
|
account = Account.search([('template', '=', template),
|
|
|
|
('company','=', company.id)], limit=1)
|
|
|
|
|
|
|
|
if not account:
|
|
|
|
print("No Account Found for company:", company.id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
account = account[0]
|
|
|
|
|
2018-10-16 23:30:19 +02:00
|
|
|
session_id, _, _ = UpdateChart.create()
|
|
|
|
update_chart = UpdateChart(session_id)
|
|
|
|
update_chart.start.account = account
|
|
|
|
update_chart.start.account_code_digits = config.default_account_code_digits
|
2018-12-20 17:08:59 +01:00
|
|
|
#print(update_chart.start.account_code_digits)
|
2018-10-16 23:30:19 +02:00
|
|
|
logger.info('%s: Upgrading Account Chart' % (company.rec_name))
|
|
|
|
update_chart.transition_update()
|
|
|
|
logger.info('%s: End Account Chart' % (company.rec_name))
|
|
|
|
|
2018-11-19 14:50:49 +01:00
|
|
|
Transaction().commit()
|
|
|
|
|
|
|
|
Account.parent.left = 'left'
|
|
|
|
Account.parent.right = 'right'
|
|
|
|
|
2019-02-07 10:31:23 +01:00
|
|
|
# for company in Company.search(domain):
|
|
|
|
# logger.info("company %s" % company.id)
|
|
|
|
# logger.info('%s: Rebuild tree' % (company.rec_name))
|
|
|
|
# with Transaction().set_context(company=company.id):
|
|
|
|
# Account._rebuild_tree('parent', None, 0)
|
|
|
|
# logger.info('%s: End rebuild tree' % (company.rec_name))
|
|
|
|
#
|
|
|
|
# logger.info('Done')
|
|
|
|
|
|
|
|
|
|
|
|
# Fast way to calculate parent_left and right
|
|
|
|
cr = Transaction().connection.cursor()
|
|
|
|
|
|
|
|
def browse_rec(root, pos=0):
|
|
|
|
where = 'parent' + '=' + str(root)
|
|
|
|
|
|
|
|
if not root:
|
2023-10-11 12:43:49 +02:00
|
|
|
where = 'parent IS NULL'
|
2019-02-07 10:31:23 +01:00
|
|
|
|
|
|
|
cr.execute('SELECT id FROM %s WHERE %s \
|
|
|
|
ORDER BY %s' % ('account_account', where, 'parent'))
|
|
|
|
pos2 = pos + 1
|
|
|
|
childs = cr.fetchall()
|
|
|
|
for id in childs:
|
|
|
|
pos2 = browse_rec(id[0], pos2)
|
|
|
|
cr.execute('update %s set "left"=%s, "right"=%s\
|
|
|
|
where id=%s' % ('account_account', pos, pos2, root))
|
|
|
|
return pos2 + 1
|
|
|
|
|
|
|
|
query = 'SELECT id FROM %s WHERE %s IS NULL order by %s' % (
|
|
|
|
'account_account', 'parent', 'parent')
|
|
|
|
pos = 0
|
|
|
|
cr.execute(query)
|
|
|
|
for (root,) in cr.fetchall():
|
|
|
|
pos = browse_rec(root, pos)
|
|
|
|
|
|
|
|
cr.execute(query)
|
|
|
|
for (root,) in cr.fetchall():
|
|
|
|
pos = browse_rec(root, pos)
|
2019-05-09 12:12:30 +02:00
|
|
|
|
|
|
|
Transaction().commit()
|