~unifield-team/unifield-wm/us-671-homere

« back to all changes in this revision

Viewing changes to unifield_tests/tests/finance.py

  • Committer: jf
  • Date: 2012-04-17 15:29:16 UTC
  • mfrom: (631.3.7 UF_828)
  • Revision ID: jf@tempo4-20120417152916-svm6ioq8ur2bi5tu
UF-955 [DEV] Reporting (Month-end) - 2 remaining reports
lp:~unifield-team/unifield-wm/UF_955

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf8 -*-
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
8
 
 
9
 
class FinanceTest(UnifieldTest):
10
 
 
11
 
    def __init__(self, *args, **kwargs):
12
 
        '''
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"
16
 
        '''
17
 
        super(FinanceTest, self).__init__(*args, **kwargs)
18
 
 
19
 
    def _hook_db_process(self, name, database):
20
 
        '''
21
 
        Check that finance data are loaded into the given database
22
 
        '''
23
 
        keyword = 'finance_test_class'
24
 
        colors = self.colors
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')
32
 
            # Fiscal years
33
 
            fy_ids = fy_obj.search([('date_start', '<=', today), ('date_stop', '>=', today)])
34
 
            if not fy_ids:
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:
39
 
                try:
40
 
                    period_obj.action_set_state(period, context={'state': 'draft'})
41
 
                except error.RPCError as e:
42
 
                    print(e.oerp_traceback)
43
 
                    print(e.message)
44
 
                except Exception, e:
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))
49
 
        else:
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)
52
 
 
53
 
    def create_journal_entry(self, database):
54
 
        '''
55
 
        Create a journal entry (account.move) with 2 lines: 
56
 
          - an expense one (with an analytic distribution)
57
 
          - a counterpart one
58
 
        Return the move ID, expense line ID, then counterpart ID
59
 
        '''
60
 
        # Prepare some values
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')
69
 
        # Search journal
70
 
        journal_ids = journal_obj.search([('type', '=', 'purchase')])
71
 
        self.assert_(journal_ids != [], "No purchase journal found!")
72
 
        # Search period
73
 
        period_ids = period_obj.get_period_from_date(curr_date)
74
 
        # Search partner
75
 
        partner_ids = partner_obj.search([('partner_type', '=', 'external')])
76
 
        # Create a random amount
77
 
        random_amount = randint(100, 10000)
78
 
        # Create a move
79
 
        move_vals = {
80
 
            'journal_id': journal_ids[0],
81
 
            'period_id': period_ids[0],
82
 
            'date': curr_date,
83
 
            'document_date': curr_date,
84
 
            'partner_id': partner_ids[0],
85
 
            'status': 'manu',
86
 
        }
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)
92
 
        vals = {
93
 
            'move_id': move_id,
94
 
            'account_id': account_ids[random_account],
95
 
            'name': 'fp_changes expense',
96
 
            'amount_currency': random_amount,
97
 
        }
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)
105
 
        vals.update({
106
 
            'account_id': counterpart_ids[random_counterpart],
107
 
            'amount_currency': -1 * random_amount,
108
 
            'name': 'fp_changes counterpart',
109
 
            'analytic_distribution_id': False,
110
 
        })
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
115
 
 
116
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: