• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OCA / account-financial-reporting / 799

pending completion
799

Pull #121

travis-ci

web-flow
Merge pull request #16 from BT-tpiric/i8496_Fehlermeldung

[i8496] Fehlermeldung
Pull Request #121: translation / context in browse

12 of 12 new or added lines in 6 files covered. (100.0%)

3000 of 4444 relevant lines covered (67.51%)

1.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/account_financial_report_webkit/report/common_partner_balance_reports.py
1
# -*- encoding: utf-8 -*-
2
##############################################################################
3
#
4
#    Author: Guewen Baconnier
5
#    Copyright Camptocamp SA 2011
6
#    SQL inspired from OpenERP original code
7
#
8
#    This program is free software: you can redistribute it and/or modify
9
#    it under the terms of the GNU Affero General Public License as
10
#    published by the Free Software Foundation, either version 3 of the
11
#    License, or (at your option) any later version.
12
#
13
#    This program is distributed in the hope that it will be useful,
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
#    GNU Affero General Public License for more details.
17
#
18
#    You should have received a copy of the GNU Affero General Public License
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
#
21
##############################################################################
22

23
from collections import defaultdict
2✔
24
from operator import add
2✔
25

26
from openerp.tools.float_utils import float_is_zero
2✔
27

28
from .common_balance_reports import CommonBalanceReportHeaderWebkit
2✔
29
from .common_partner_reports import CommonPartnersReportHeaderWebkit
2✔
30

31

32
class CommonPartnerBalanceReportHeaderWebkit(CommonBalanceReportHeaderWebkit,
2✔
33
                                             CommonPartnersReportHeaderWebkit):
34

35
    """Define common helper for balance (trial balance, P&L,
36
        BS oriented financial report"""
37

38
    def _get_account_partners_details(self, account_by_ids, main_filter,
2✔
39
                                      target_move, start, stop,
40
                                      initial_balance_mode,
41
                                      partner_filter_ids=False,
42
                                      display_partner='all'):
43
        res = {}
2✔
44
        filter_from = False
2✔
45
        if main_filter in ('filter_period', 'filter_no', 'filter_opening'):
2✔
46
            filter_from = 'period'
2✔
47
        elif main_filter == 'filter_date':
2✔
48
            filter_from = 'date'
2✔
49

50
        partners_init_balances_by_ids = {}
2✔
51
        for account_id, account_details in account_by_ids.iteritems():
2✔
52
            partners_init_balances_by_ids.update(
2✔
53
                self._get_partners_initial_balances(
54
                    account_id, start, initial_balance_mode,
55
                    partner_filter_ids=partner_filter_ids,
56
                    # we'll never exclude reconciled entries in the legal
57
                    # reports
58
                    exclude_reconcile=False))
59
            opening_mode = 'exclude_opening'
2✔
60
            if main_filter == 'filter_opening':
2✔
61
                opening_mode = 'include_opening'
×
62
            # get credit and debit for partner
63
            details = self._get_partners_totals_account(
2✔
64
                filter_from,
65
                account_id,
66
                start,
67
                stop,
68
                target_move,
69
                partner_filter_ids=partner_filter_ids,
70
                mode=opening_mode)
71

72
            # merge initial balances in partner details
73
            if partners_init_balances_by_ids.get(account_id):
2✔
74
                for partner_id, initial_balances in \
×
75
                        partners_init_balances_by_ids[account_id].iteritems():
76
                    if initial_balances.get('init_balance'):
×
77
                        details[partner_id].update(
×
78
                            {'init_balance': initial_balances['init_balance']})
79

80
            # compute balance for the partner
81
            for partner_id, partner_details in details.iteritems():
2✔
82
                details[partner_id]['balance'] = details[partner_id].\
2✔
83
                    get('init_balance', 0.0) + \
84
                    details[partner_id].get('debit', 0.0) - \
85
                    details[partner_id].get('credit', 0.0)
86

87
            if display_partner == 'non-zero_balance':
2✔
88
                details = {
×
89
                    k: v
90
                    for k, v in details.iteritems()
91
                    if not float_is_zero(v['balance'], precision_digits=5)
92
                }
93
            res[account_id] = details
2✔
94

95
        return res
2✔
96

97
    def _get_partners_initial_balances(self, account_ids, start_period,
2✔
98
                                       initial_balance_mode,
99
                                       partner_filter_ids=None,
100
                                       exclude_reconcile=False):
101
        # we get the initial balance from the opening period (opening_balance)
102
        # when the opening period is included in the start period and
103
        # when there is at least one entry in the opening period. Otherwise we
104
        # compute it from previous periods
105
        if initial_balance_mode == 'opening_balance':
2✔
106
            opening_period_selected = self.get_included_opening_period(
×
107
                start_period)
108
            res = self._compute_partners_initial_balances(
×
109
                account_ids, start_period, partner_filter_ids,
110
                force_period_ids=opening_period_selected,
111
                exclude_reconcile=exclude_reconcile)
112
        elif initial_balance_mode == 'initial_balance':
2✔
113
            res = self._compute_partners_initial_balances(
2✔
114
                account_ids, start_period, partner_filter_ids,
115
                exclude_reconcile=exclude_reconcile)
116
        else:
117
            res = {}
2✔
118
        return res
2✔
119

120
    def _get_partners_totals_account(self, filter_from, account_id, start,
2✔
121
                                     stop, target_move,
122
                                     partner_filter_ids=None,
123
                                     mode='exclude_opening'):
124
        final_res = defaultdict(dict)
2✔
125

126
        sql_select = """
2✔
127
                 SELECT account_move_line.partner_id,
128
                        sum(account_move_line.debit) AS debit,
129
                        sum(account_move_line.credit) AS credit
130
                 FROM account_move_line"""
131
        sql_joins = ''
2✔
132
        sql_where = "WHERE account_move_line.account_id = %(account_id)s \
2✔
133
                     AND account_move_line.state = 'valid' "
134
        method = getattr(self, '_get_query_params_from_' + filter_from + 's')
2✔
135
        sql_conditions, search_params = method(start, stop, mode=mode)
2✔
136
        sql_where += sql_conditions
2✔
137

138
        if partner_filter_ids:
2✔
139
            sql_where += "   AND account_move_line.partner_id \
×
140
                             in %(partner_ids)s"
141
            search_params.update({'partner_ids': tuple(partner_filter_ids)})
×
142

143
        if target_move == 'posted':
2✔
144
            sql_joins += "INNER JOIN account_move \
2✔
145
                            ON account_move_line.move_id = account_move.id"
146
            sql_where += " AND account_move.state = %(target_move)s"
2✔
147
            search_params.update({'target_move': target_move})
2✔
148

149
        sql_groupby = "GROUP BY account_move_line.partner_id"
2✔
150

151
        search_params.update({'account_id': account_id})
2✔
152
        query = ' '.join((sql_select, sql_joins, sql_where, sql_groupby))
2✔
153

154
        self.cursor.execute(query, search_params)
2✔
155
        res = self.cursor.dictfetchall()
2✔
156
        if res:
2✔
157
            for row in res:
2✔
158
                final_res[row['partner_id']] = row
2✔
159
        return final_res
2✔
160

161
    def _get_filter_type(self, result_selection):
2✔
162
        filter_type = ('payable', 'receivable')
2✔
163
        if result_selection == 'customer':
2✔
164
            filter_type = ('receivable',)
×
165
        if result_selection == 'supplier':
2✔
166
            filter_type = ('payable',)
×
167
        return filter_type
2✔
168

169
    def _get_partners_comparison_details(self, data, account_ids, target_move,
2✔
170
                                         comparison_filter, index,
171
                                         partner_filter_ids=False):
172
        """
173

174
        @param data: data of the wizard form
175
        @param account_ids: ids of the accounts to get details
176
        @param comparison_filter: selected filter on the form for
177
            the comparison (filter_no, filter_year, filter_period, filter_date)
178
        @param index: index of the fields to get (ie. comp1_fiscalyear_id
179
            where 1 is the index)
180
        @param partner_filter_ids: list of ids of partners to select
181
        @return: dict of account details (key = account id)
182
        """
183
        fiscalyear = self._get_info(
2✔
184
            data, "comp%s_fiscalyear_id" % (index,), 'account.fiscalyear')
185
        start_period = self._get_info(
2✔
186
            data, "comp%s_period_from" % (index,), 'account.period')
187
        stop_period = self._get_info(
2✔
188
            data, "comp%s_period_to" % (index,), 'account.period')
189
        start_date = self._get_form_param("comp%s_date_from" % (index,), data)
2✔
190
        stop_date = self._get_form_param("comp%s_date_to" % (index,), data)
2✔
191
        init_balance = self.is_initial_balance_enabled(comparison_filter)
2✔
192

193
        comp_params = {}
2✔
194
        accounts_details_by_ids = defaultdict(dict)
2✔
195
        if comparison_filter != 'filter_no':
2✔
196
            start_period, stop_period, start, stop = \
2✔
197
                self._get_start_stop_for_filter(
198
                    comparison_filter, fiscalyear, start_date, stop_date,
199
                    start_period, stop_period)
200
            details_filter = comparison_filter
2✔
201
            if comparison_filter == 'filter_year':
2✔
202
                details_filter = 'filter_no'
2✔
203

204
            initial_balance_mode = init_balance \
2✔
205
                and self._get_initial_balance_mode(start) or False
206

207
            accounts_by_ids = self._get_account_details(
2✔
208
                account_ids, target_move, fiscalyear, details_filter, start,
209
                stop, initial_balance_mode)
210

211
            partner_details_by_ids = self._get_account_partners_details(
2✔
212
                accounts_by_ids, details_filter,
213
                target_move, start, stop, initial_balance_mode,
214
                partner_filter_ids=partner_filter_ids,
215
                display_partner=data['form']['display_partner']
216
            )
217

218
            for account_id in account_ids:
2✔
219
                accounts_details_by_ids[account_id][
2✔
220
                    'account'] = accounts_by_ids[account_id]
221
                accounts_details_by_ids[account_id][
2✔
222
                    'partners_amounts'] = partner_details_by_ids[account_id]
223

224
            comp_params = {
2✔
225
                'comparison_filter': comparison_filter,
226
                'fiscalyear': fiscalyear,
227
                'start': start,
228
                'stop': stop,
229
                'initial_balance_mode': initial_balance_mode,
230
            }
231

232
        return accounts_details_by_ids, comp_params
2✔
233

234
    def compute_partner_balance_data(self, data, filter_report_type=None):
2✔
235
        new_ids = data['form']['account_ids'] or data[
2✔
236
            'form']['chart_account_id']
237
        max_comparison = self._get_form_param(
2✔
238
            'max_comparison', data, default=0)
239
        main_filter = self._get_form_param('filter', data, default='filter_no')
2✔
240

241
        comp_filters, nb_comparisons, comparison_mode = self._comp_filters(
2✔
242
            data, max_comparison)
243

244
        fiscalyear = self.get_fiscalyear_br(data)
2✔
245

246
        start_period = self.get_start_period_br(data)
2✔
247
        stop_period = self.get_end_period_br(data)
2✔
248
        target_move = self._get_form_param('target_move', data, default='all')
2✔
249
        start_date = self._get_form_param('date_from', data)
2✔
250
        stop_date = self._get_form_param('date_to', data)
2✔
251
        chart_account = self._get_chart_account_id_br(data)
2✔
252
        result_selection = self._get_form_param('result_selection', data)
2✔
253
        partner_ids = self._get_form_param('partner_ids', data)
2✔
254

255
        filter_type = self._get_filter_type(result_selection)
2✔
256

257
        start_period, stop_period, start, stop = \
2✔
258
            self._get_start_stop_for_filter(
259
                main_filter, fiscalyear, start_date, stop_date, start_period,
260
                stop_period)
261

262
        initial_balance = self.is_initial_balance_enabled(main_filter)
2✔
263
        initial_balance_mode = initial_balance \
2✔
264
            and self._get_initial_balance_mode(start) or False
265

266
        # Retrieving accounts
267
        account_ids = self.get_all_accounts(
2✔
268
            new_ids, only_type=filter_type,
269
            filter_report_type=filter_report_type)
270

271
        # get details for each accounts, total of debit / credit / balance
272
        accounts_by_ids = self._get_account_details(
2✔
273
            account_ids, target_move, fiscalyear, main_filter, start, stop,
274
            initial_balance_mode)
275

276
        partner_details_by_ids = self._get_account_partners_details(
2✔
277
            accounts_by_ids, main_filter, target_move, start, stop,
278
            initial_balance_mode, partner_filter_ids=partner_ids,
279
            display_partner=data['form']['display_partner'])
280

281
        comparison_params = []
2✔
282
        comp_accounts_by_ids = []
2✔
283
        for index in range(max_comparison):
2✔
284
            if comp_filters[index] != 'filter_no':
2✔
285
                comparison_result, comp_params = self.\
2✔
286
                    _get_partners_comparison_details(
287
                        data, account_ids,
288
                        target_move,
289
                        comp_filters[index],
290
                        index,
291
                        partner_filter_ids=partner_ids)
292
                comparison_params.append(comp_params)
2✔
293
                comp_accounts_by_ids.append(comparison_result)
2✔
294
        objects = self.pool.get('account.account').browse(
2✔
295
            self.cursor,
296
            self.uid,
297
            account_ids,
298
            context=self.localcontext)
299

300
        init_balance_accounts = {}
2✔
301
        comparisons_accounts = {}
2✔
302
        partners_order_accounts = {}
2✔
303
        partners_amounts_accounts = {}
2✔
304
        debit_accounts = {}
2✔
305
        credit_accounts = {}
2✔
306
        balance_accounts = {}
2✔
307

308
        for account in objects:
2✔
309
            if not account.parent_id:  # hide top level account
2✔
310
                continue
×
311
            debit_accounts[account.id] = accounts_by_ids[account.id]['debit']
2✔
312
            credit_accounts[account.id] = accounts_by_ids[account.id]['credit']
2✔
313
            balance_accounts[account.id] = \
2✔
314
                accounts_by_ids[account.id]['balance']
315
            init_balance_accounts[account.id] = accounts_by_ids[
2✔
316
                account.id].get('init_balance', 0.0)
317
            partners_amounts_accounts[account.id] =\
2✔
318
                partner_details_by_ids[account.id]
319
            comp_accounts = []
2✔
320
            for comp_account_by_id in comp_accounts_by_ids:
2✔
321
                values = comp_account_by_id.get(account.id)
2✔
322

323
                values['account'].update(
2✔
324
                    self._get_diff(balance_accounts[account.id],
325
                                   values['account'].get('balance', 0.0)))
326
                comp_accounts.append(values)
2✔
327

328
                for partner_id, partner_values in \
2✔
329
                        values['partners_amounts'].copy().iteritems():
330
                    partners_amounts_account =\
2✔
331
                        partners_amounts_accounts[account.id]
332
                    base_partner_balance =\
2✔
333
                        partners_amounts_account[partner_id]['balance']\
334
                        if partners_amounts_accounts.get(account.id)\
335
                        and partners_amounts_accounts.get(account.id)\
336
                        .get(partner_id) else 0.0
337
                    partner_values.update(self._get_diff(
2✔
338
                        base_partner_balance,
339
                        partner_values.get('balance', 0.0)))
340
                    values['partners_amounts'][
2✔
341
                        partner_id].update(partner_values)
342

343
            comparisons_accounts[account.id] = comp_accounts
2✔
344

345
            all_partner_ids = reduce(add, [comp['partners_amounts'].keys()
2✔
346
                                           for comp in comp_accounts],
347
                                     partners_amounts_accounts[account.id]
348
                                     .keys())
349

350
            partners_order_accounts[account.id] = \
2✔
351
                self._order_partners(all_partner_ids)
352

353
        context_report_values = {
2✔
354
            'fiscalyear': fiscalyear,
355
            'start_date': start_date,
356
            'stop_date': stop_date,
357
            'start_period': start_period,
358
            'stop_period': stop_period,
359
            'chart_account': chart_account,
360
            'comparison_mode': comparison_mode,
361
            'nb_comparison': nb_comparisons,
362
            'comp_params': comparison_params,
363
            'initial_balance_mode': initial_balance_mode,
364
            'compute_diff': self._get_diff,
365
            'init_balance_accounts': init_balance_accounts,
366
            'comparisons_accounts': comparisons_accounts,
367
            'partners_order_accounts': partners_order_accounts,
368
            'partners_amounts_accounts': partners_amounts_accounts,
369
            'debit_accounts': debit_accounts,
370
            'credit_accounts': credit_accounts,
371
            'balance_accounts': balance_accounts,
372
        }
373

374
        return objects, new_ids, context_report_values
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc