3
from __future__ import print_function
4
from unifield_test import UnifieldTest
5
from time import strftime
6
from random import randint
7
from oerplib import error
9
class FinanceTest(UnifieldTest):
11
def __init__(self, *args, **kwargs):
13
Include some finance data in the databases (except sync one).
14
Include/create them only if they have not been already created.
15
To know this, we use the key: "finance_test_class"
17
super(FinanceTest, self).__init__(*args, **kwargs)
19
def _hook_db_process(self, name, database):
21
Check that finance data are loaded into the given database
23
keyword = 'finance_test_class'
25
# If no one, do some changes on DBs
26
if not self.is_keyword_present(database, keyword):
27
# 00 Open periods from january to today's one
28
month = strftime('%m')
29
today = strftime('%Y-%m-%d')
30
fy_obj = database.get('account.fiscalyear')
31
period_obj = database.get('account.period')
33
fy_ids = fy_obj.search([('date_start', '<=', today), ('date_stop', '>=', today)])
35
raise Exception('error', 'No fiscalyear found!')
36
# Sort periods by number
37
periods = period_obj.search([('fiscalyear_id', 'in', fy_ids), ('number', '<=', month), ('state', '=', 'created')], 0, 16, 'number')
38
for period in periods:
40
period_obj.action_set_state(period, context={'state': 'draft'})
41
except error.RPCError as e:
42
print(e.oerp_traceback)
45
raise Exception('error', str(e))
46
# Write the fact that data have been loaded
47
database.get(self.test_module_obj_name).create({'name': keyword, 'active': True})
48
print (database.colored_name + ' [' + colors.BGreen + 'OK'.center(4) + colors.Color_Off + '] %s: Data loaded' % (keyword))
50
print (database.colored_name + ' [' + colors.BYellow + 'WARN'.center(4) + colors.Color_Off + '] %s: Data already exists' % (keyword))
51
return super(FinanceTest, self)._hook_db_process(name, database)
53
def create_journal_entry(self, database):
55
Create a journal entry (account.move) with 2 lines:
56
- an expense one (with an analytic distribution)
58
Return the move ID, expense line ID, then counterpart ID
61
move_obj = database.get('account.move')
62
aml_obj = database.get('account.move.line')
63
period_obj = database.get('account.period')
64
journal_obj = database.get('account.journal')
65
partner_obj = database.get('res.partner')
66
account_obj = database.get('account.account')
67
distrib_obj = database.get('analytic.distribution')
68
curr_date = strftime('%Y-%m-%d')
70
journal_ids = journal_obj.search([('type', '=', 'purchase')])
71
self.assert_(journal_ids != [], "No purchase journal found!")
73
period_ids = period_obj.get_period_from_date(curr_date)
75
partner_ids = partner_obj.search([('partner_type', '=', 'external')])
76
# Create a random amount
77
random_amount = randint(100, 10000)
80
'journal_id': journal_ids[0],
81
'period_id': period_ids[0],
83
'document_date': curr_date,
84
'partner_id': partner_ids[0],
87
move_id = move_obj.create(move_vals)
88
self.assert_(move_id != False, "Move creation failed with these values: %s" % move_vals)
89
# Create some move lines
90
account_ids = account_obj.search([('is_analytic_addicted', '=', True), ('code', '=', '6101-expense-test')])
91
random_account = randint(0, len(account_ids) - 1)
94
'account_id': account_ids[random_account],
95
'name': 'fp_changes expense',
96
'amount_currency': random_amount,
98
# Search analytic distribution
99
distribution_ids = distrib_obj.search([('name', '=', 'DISTRIB 1')])
100
distribution_id = distrib_obj.copy(distribution_ids[0], {'name': 'distribution-test'})
101
vals.update({'analytic_distribution_id': distribution_id})
102
aml_expense_id = aml_obj.create(vals)
103
counterpart_ids = account_obj.search([('is_analytic_addicted', '=', False), ('code', '=', '401-supplier-test'), ('type', '!=', 'view')])
104
random_counterpart = randint(0, len(counterpart_ids) - 1)
106
'account_id': counterpart_ids[random_counterpart],
107
'amount_currency': -1 * random_amount,
108
'name': 'fp_changes counterpart',
109
'analytic_distribution_id': False,
111
aml_counterpart_id = aml_obj.create(vals)
112
# Validate the journal entry
113
move_obj.button_validate([move_id]) # WARNING: we use button_validate so that it check the analytic distribution validity/presence
114
return move_id, aml_expense_id, aml_counterpart_id
116
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: