Initial commit.

This commit is contained in:
Albert Cervera i Areny 2017-01-19 01:59:38 +01:00
commit 03c1ba0d69
7 changed files with 771 additions and 0 deletions

43
after/convert_domain_rules.py Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
import logging
from trytond.pyson import PYSONEncoder
Pool.start()
pool = Pool(dbname)
pool.init()
context = {'company': 1}
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)
with Transaction().start(dbname, 0, context=context):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
with Transaction().start(dbname, 0, context=context) as transaction:
pool = Pool()
Rule = pool.get('ir.rule.group')
groups = Rule.search([('create_uid', '>', 0)])
for group in groups:
for rule in group.rules:
rule.domain = PYSONEncoder().encode(eval(rule.domain))
rule.save()

View File

@ -0,0 +1,177 @@
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
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)
with Transaction().start(dbname, 0, context=context):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
with Transaction().start(dbname, 0, context=context) as transaction:
WorkProject = pool.get('work.project')
Project = pool.get('project.work')
Category = pool.get('sale.opportunity.category')
# Certification = pool.get('project.certification')
# CertificationLine = pool.get('project.certification.line')
Sale = pool.get('sale.sale')
SaleLine = pool.get('sale.line')
Product = pool.get('product.product')
Template = pool.get('product.template')
Uom = pool.get('product.uom')
unit, = Uom.search([
('name', '=', 'Unit'),
])
product, = Product.search([('type', '=', 'goods')], limit=1)
template = Template()
template.name = 'Projectes'
template.list_price = 0
template.cost_price = 0
template.default_uom = unit
template.sale_uom = unit
template.purchase_uom = unit
template.type = 'goods'
template.account_category = product.account_category
template.save()
p = Product(template=template, code='PROJECTES')
p.save()
work_projects = WorkProject.search([])
work_project = dict((x.code, x) for x in work_projects)
projects = []
default_category = Category(name='Per Configurar')
logger.info('Start reading projects')
for wp in work_projects:
if wp.contract_lines:
continue
project = Project()
project.invoice_product_type = 'goods'
project.product_goods = product
project.uom = product.default_uom
project.project_invoice_method = 'milestone'
project.name = wp.code
project.type = 'project'
project.company = wp.company
project.party = wp.party
project.state = 'draft' #TODO
project.note = wp.note
project.asset = wp.asset
# project.category = wp.category or default_category
project.children = []
project.quantity = 1
project.progress_quantity = 0
project.start_date = wp.start_date
# project.parent = None
#project.end_date = wp.end_date
# wp.maintenance =
projects.append(project)
# print project._save_values
# project.save()
logger.info('Writing projects')
#Project.create([x._save_values for x in projects])
Project.save(projects)
logger.info('%s projects createds' % len(projects))
transaction.commit()
logger.info('Start upload Tasks')
projects = Project.search([])
tasks = []
sales = []
cursor = Transaction().connection.cursor()
table_sale_line = SaleLine.__table__()
table_sale = Sale.__table__()
to_create = []
for project in projects:
wp = work_project[project.name]
childs = {}
for sale in wp.sales:
sale.work = project
# Update parent_project Sale
cursor.execute(*table_sale.update(columns=
[table_sale.parent_project],
values=[project.id],
where=table_sale.id == sale.id))
if sale.state in ('draft', 'cancel'):
continue
p2 = Project()
p2.invoice_product_type = 'goods'
p2.product_goods = product
p2.uom = product.default_uom
p2.project_invoice_method = 'milestone'
p2.name = sale.number
p2.type = 'project'
p2.company = sale.company
p2.party = sale.party
p2.state = 'draft'
p2.note = wp.note
p2.asset = wp.asset # TODO: Problemes with domains
# p2.category = wp.category or default_category
p2.children = []
p2.quantity = 1
p2.progress_quantity = 0
p2.start_date = sale.sale_date
p2.parent = project
to_create.append(p2)
logger.info('Writing projects %s' % len(to_create))
offset = 1000
i = 0
while i < len(to_create):
logger.info('Writing projects %s-%s/%s' %
(i, i+offset, len(to_create)))
create = to_create[i:i + offset]
Project.save(create)
i += offset
i = min(i, len(to_create))
logger.info('Projects createds')
# Update All sale lines with project created
cursor.execute(
"update sale_line l set project = project_id FROM "
"( "
"select s.id as sale, l.id, number, p.id as project_id "
" from sale_sale s, "
" sale_line l,"
" project_work p "
" where s.id = l.sale and "
" s.number = p.name "
") as sub "
"where l.sale = sub.sale"
)
transaction.commit()

View File

@ -0,0 +1,73 @@
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
import logging
Pool.start()
pool = Pool(dbname)
pool.init()
context = {'company': 1}
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)
with Transaction().start(dbname, 0, context=context):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
with Transaction().start(dbname, 0, context=context) as transaction:
ShipmentWork = pool.get('shipment.work')
Project = pool.get('project.work')
Contract = pool.get('contract')
ContractLine = pool.get('contract.line')
cursor = Transaction().connection.cursor()
table = ShipmentWork.__table__()
shipment_works = ShipmentWork.search([])
# relate shipment.work and project.work
for sw in shipment_works:
logger.info("sw: %s/%s" % (sw.number, sw.work_project))
if not sw.work_project:
continue
project = Project.search([('name', '=', sw.work_project.code)])
if project:
project, = project
logger.info("sw: %s/%s => %s" % (sw.number,
sw.work_project.code, project))
cursor.execute(*table.update(columns=[table.origin, table.project],
values=['project.work,' + str(project.id), project.id],
where=table.id == sw.id))
else:
cl = ContractLine.search([
('asset', '=', sw.asset),
('contract.party', '=', sw.party)])
if not cl:
continue
cl = cl[0]
logger.info("sw: %s/%s => %s" % (sw.number,
sw.work_project.code, cl))
cursor.execute(*table.update(columns=[table.origin],
values=['contract.line,' + str(cl.id)],
where=table.id == sw.id))
# logger.info('Not null column project in shipment.work')
# cursor.execute('ALTER TABLE "shipment_work" ALTER COLUMN "project"'
# ' SET NOT NULL;')
logger.info('Done')

119
after/milestone_migration.py Executable file
View File

@ -0,0 +1,119 @@
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
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)
with Transaction().start(dbname, 0, context=context):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
with Transaction().start(dbname, 0, context=context) as transaction:
WorkProject = pool.get('work.project')
WorkMilestone = pool.get('account.invoice.milestone')
WorkMilestoneGroup = pool.get('account.invoice.milestone.group')
Milestone = pool.get('project.invoice_milestone')
Project = pool.get('project.work')
milestoneGroup = WorkMilestoneGroup.search([])
trigger = {
'confirmed_sale': 'start_project',
'shipped_amount': 'progress',
'sent_sale': 'finish_project',
}
states = {
'draft': 'draft',
'confirmed': 'confirmed',
'processing': 'invoiced',
'succeeded': 'invoiced',
'failed': 'cancel',
'cancel': 'cancel',
}
invoice_method = {
'amount': 'fixed',
'progress': 'progress',
'sale_lines': 'progress',
'shipped_goods': 'progress',
'remainder': 'remainder',
}
projects = Project.search([('type', '=', 'project')])
projects = dict((x.name, x) for x in projects)
def get_project(group):
work_projects = list(set([x.work_project for x in group.sales if x]))
project = None
code = (work_projects and work_projects[0] != None and
work_projects[0].code or None)
project = projects.get(code, None)
if not project:
print "*"*10, "CHECK", "*"*10
print "group:", group.code, "projects:", work_projects, code
return project
to_create = []
for group in milestoneGroup:
project = get_project(group)
if not project:
continue
for mil in group.milestones:
milestone = Milestone()
milestone.kind = mil.kind
if mil.trigger:
milestone.trigger = trigger[mil.trigger]
if mil.trigger_shipped_amount:
milestone.trigger_progress = mil.trigger_shipped_amount
milestone.invoice_method = invoice_method[mil.invoice_method]
milestone.advancement_product = mil.advancement_product
milestone.compensation_product = mil.advancement_product
milestone.advancement_amount = mil.amount
milestone.currency = group.currency
milestone.month = mil.month
milestone.weeks = mil.weeks
milestone.weekday = mil.weekday
milestone.days = mil.days
milestone.day = mil.day
milestone.description = mil.description
milestone.number = mil.code
milestone.project = project
milestone.invoice_date = mil.invoice_date
milestone.planned_invoice_date = mil.planned_invoice_date
milestone.processed_date = mil.processed_date
milestone.invoice = mil.invoice
milestone.state = states[mil.state]
# TODO: CHECK this domain
# Check why invoice.party != project.party
invoice_party = mil.invoice and mil.invoice.party
if mil.invoice and (invoice_party != project.party):
print "CHECK:", project.name, mil.invoice.number
else:
to_create.append(milestone)
logger.info('Writing Milestones')
Milestone.save(to_create)
logger.info('%s Milestones created' % len(to_create))

49
after/purchase_work.py Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
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)
with Transaction().start(dbname, 0, context=context):
user_obj = pool.get('res.user')
user = user_obj.search([('login', '=', 'admin')], limit=1)[0]
user_id = user.id
with Transaction().start(dbname, 0, context=context) as transaction:
Project = pool.get('project.work')
PurchaseLine = pool.get('purchase.line')
projects = Project.search([])
to_save = []
for project in projects:
purchase_lines = []
project.purchase_lines = []
for line in project.sale_lines:
purchase_lines += line.purchase_lines
if purchase_lines:
for pl in purchase_lines:
project.purchase_lines += (pl, )
to_save.append(project)
Project.save(to_save)

123
config.yml Normal file
View File

@ -0,0 +1,123 @@
---
# asset_work_project
# work_project
to_uninstall:
- account_invoice_post_in_tree
- account_chart_speedup
- account_invoice_data
- account_invoice_post_in_tree
- account_move_party_required
- account_payment_es_csb_19
- account_payment_es_csb_32
- account_payment_es_csb_34
- account_payment_es_csb_34_01
- account_payment_es_csb_34_1_la_caixa
- account_payment_es_csb_34_11
- account_payment_es_csb_58
- account_statement_of_account
- aeat_303_es
- aeat_349_es
- analytic_account_move
- asset_guarantee
- asset_estate
- audit_log
- guarantee
- party_identifier
- product_configuration
- project_unittest
- stock_delivery_note_jreport
to_install:
- account_invoice_tree
- asset_owner
- asset_shipment_work
- contract_revenue
- project
- project_asset
- project_certification
- project_invoice_milestone
- project_management
- project_management_with_sales
- project_management_with_purchase
- project_management_with_shipment_work
- project_menu_tree_remove
- project_invoice
- project_revenue
- project_product
- project_shipment_work
- project_state_by_buttons
- purchase_request
- sale_tree
before:
# Rename table name - 3.6 migration
- tables: account_financial_statement
query:
ALTER TABLE account_financial_statement_report_line_account
RENAME TO account_financial_statement_rep_lin_acco
- tables: account_financial_statement
query:
ALTER TABLE account_financial_statement_report_line_account_id_seq
RENAME TO account_financial_statement_rep_lin_acco_id_seq
# [SQL]: Change tax sign for credit note (must be run before update):
- tables: account_tax_template
query:
UPDATE account_tax_template
SET
credit_note_base_sign = credit_note_base_sign * -1,
credit_note_tax_sign = credit_note_tax_sign * -1;
- tables: account_tax
query:
UPDATE account_tax
SET
credit_note_base_sign = credit_note_base_sign * -1,
credit_note_tax_sign = credit_note_tax_sign * -1;
# [SQL]: Fix amount second currency with:
- tables: account_move_line
query:
UPDATE account_move_line
SET
amount_second_currency = (amount_second_currency * -1)
WHERE amount_second_currency IS NOT NULL
AND SIGN(amount_second_currency) != SIGN(debit - credit)
- DELETE FROM ir_model_data WHERE model='stock.location' AND fs_id LIKE '%transit%'
- tables: shipment_work
query: ALTER TABLE shipment_work RENAME COLUMN project TO work_project
- fields: sale_sale.project
query: ALTER TABLE sale_sale RENAME COLUMN project TO work_project
- UPDATE ir_model_data SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_model_data SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- UPDATE ir_translation SET module = 'aeat_303' WHERE module = 'aeat_303_es';
- UPDATE ir_translation SET module = 'aeat_349' WHERE module = 'aeat_349_es';
- DELETE from ir_ui_view where module = '';
- DELETE from ir_ui_view where data = '' and module = 'account_payment_type';
- UPDATE ir_ui_view SET type = NULL WHERE inherit IS NOT NULL AND type IS NOT NULL;
after:
- DELETE FROM ir_translation WHERE module = 'account_es';
- DELETE FROM ir_translation WHERE module = 'account_es_pyme';
# Mark all categories as accounting type
- UPDATE product_category SET accounting = True;
# DROP category COLUMN
- ALTER TABLE product_template DROP COLUMN category;
# Change login's length
- ALTER TABLE res_user_login_attempt ALTER COLUMN login TYPE character varying(512);
# Set reconciliation date
- UPDATE account_move_reconciliation SET date=create_date WHERE date IS NULL;
- ALTER TABLE account_move_reconciliation ALTER COLUMN date SET NOT NULL;
- UPDATE contract SET first_invoice_date=start_period_date WHERE
first_invoice_date IS NULL AND state = 'cancelled';
- ALTER TABLE asset ALTER COLUMN company SET NOT NULL;
- ALTER TABLE timesheet_line ALTER COLUMN duration SET NOT NULL;
- ALTER TABLE stock_shipment_in_return ALTER COLUMN supplier SET NOT NULL;
- ALTER TABLE contract ALTER COLUMN first_invoice_date SET NOT NULL;
- UPDATE asset SET company = 1 WHERE company IS NULL;

187
upgrade Executable file
View File

@ -0,0 +1,187 @@
#!/usr/bin/env python
import os
import yaml
import argparse
import psycopg2
import subprocess
from blessings import Terminal
t = Terminal()
parser = argparse.ArgumentParser(description='Upgrade a Tryton database.')
parser.add_argument('database', nargs=1, help='PostgreSQL database to upgrade')
parser.add_argument('-c', '--config', default='trytond.conf',
help='path to the trytond configuration file')
args = parser.parse_args()
database_name, = args.database
config_file = args.config
#host = args.host
#port = args.port
#user = args.user
#password = args.password
#if args.json and args.yaml:
#print('Cannot use both --json and --yaml parameters.')
#sys.exit(1)
#
#if not args.check and not args.list:
#print('One of --list or check arguments is required.')
#sys.exit(1)
config = yaml.load(open('upgrades/config.yml', 'r').read())
to_uninstall = config.get('to_uninstall')
#db = psycopg2.connect(dbname=database, host=host, port=port, user=user,
#password=password)
connection = psycopg2.connect(dbname=database_name)
cursor = connection.cursor()
def run(*args):
#process = subprocess.Popen(args, stdout=subprocess.PIPE,
# stderr=subprocess.PIPE)
#process.wait()
#stdout = process.stdout.read()
#stderr = process.stderr.read()
print "RUNING: ", args
process = subprocess.Popen(args, stdout=subprocess.PIPE, bufsize=1)
for line in iter(process.stdout.readline, b''):
print line,
process.stdout.close()
process.wait()
#return process.returncode, stdout, stderr
def execute(query, *args, **kwargs):
if not args:
args = kwargs
cursor.execute(query, args)
def run_trytond():
to_run = ['trytond/bin/trytond-admin', '-v', '-c', config_file]
to_install = config.get('to_install')
if to_install:
to_run += ['-u']
to_run += to_install
to_run.append('--all')
to_run.append('-d')
to_run.append(database_name)
return run(*to_run)
def run_after():
for filename in os.listdir('upgrades/after'):
print 'Running %s...' % filename
run('upgrades/after/' + filename, database_name, args.config)
# INSERT INTO stock_location (name, code, type, create_uid, create_date, "right", "left") VALUES ('Migration Drop Shipment', 'DROPMIGRATION', 'drop', 0, '2016-10-04 00:00:00', 0, 0);
def table_exists(table):
execute('SELECT count(*) FROM information_schema.tables '
'WHERE table_name=%s', table)
return bool(cursor.fetchone()[0])
def field_exists(field):
table, field = field.split('.')
execute('SELECT count(*) FROM information_schema.columns '
'WHERE table_name=%s AND column_name=%s', table, field)
return bool(cursor.fetchone()[0])
def uninstall_modules(to_uninstall):
module_table = None
for table in ('ir_module_module', 'ir_module'):
if table_exists(table):
module_table = table
break
for module in to_uninstall:
print 'Module:', module
execute('DELETE FROM ' + module_table + '_dependency WHERE '
'module IN (SELECT id FROM ' + module_table + ' WHERE name=%s)',
module)
execute('DELETE FROM ' + module_table + ' WHERE name=%s', module)
execute('SELECT model, db_id FROM ir_model_data WHERE module=%s',
module)
for model, db_id in cursor.fetchall():
print 'DELETING', model
execute('DELETE FROM "' + model.replace('.', '_')
+ '" WHERE id=%s', db_id)
execute('DELETE FROM ir_model_data WHERE module=%s', module)
execute('DELETE FROM ir_action_act_window WHERE res_model IN (SELECT '
'model FROM ir_model WHERE module NOT IN (SELECT name FROM %s))' %
module_table)
execute('DELETE FROM ir_action_wizard WHERE model in (SELECT model FROM '
'ir_model WHERE module NOT IN (SELECT name FROM %s))' % module_table)
execute('DELETE FROM ir_model WHERE module NOT IN (SELECT name FROM '
'%s)' % module_table)
execute('DELETE FROM ir_model_field WHERE module NOT IN (SELECT name FROM '
'%s)' % module_table)
execute('DELETE FROM ir_ui_view WHERE module NOT IN (SELECT name FROM '
'%s)' % module_table)
def process_queries(queries):
if not queries:
return
for query in queries:
if isinstance(query, dict):
tables = query.get('tables', '')
fields = query.get('fields', '')
query = query.get('query')
found = True
tables = tables.split()
for table in tables:
if not table_exists(table):
print t.red("TABLE '%s' NOT FOUND" % table)
found = False
break
if not found:
continue
found = True
fields = fields.split()
for field in fields:
if not field_exists(field):
print t.red("FIELD '%s' NOT FOUND" % field)
found = False
break
if not found:
continue
query = query.replace('%', '%%')
print query
execute(query)
print t.green('Uninstalling modules...')
uninstall_modules(to_uninstall)
print t.green('Executing queries before update...')
process_queries(config.get('before'))
connection.commit()
print t.green('Updating trytond...')
run_trytond()
run_after()
#python ./upgrades/4.0/migration_project_product.py $1 $2
#python ./upgrades/4.0/migration_shipment_work.py $1 $2
#python ./upgrades/4.0/purchase_work.py $1 $2
#python ./upgrades/4.0/milestone_migration.py $1 $2
#python ./upgrades/4.0/convert_domain_rules.py $1 $2
print t.green('Executing queries after update...')
process_queries(config.get('after'))
connection.rollback()
connection.commit()