~rr.clearcorp/openerp-costa-rica/6.1-cr_hr_report

« back to all changes in this revision

Viewing changes to cr_account_financial_report/wizard/partners_ledger_wizard.py

[MRG] juan c2c-financial-addons merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Author: Nicolas Bessi, Guewen Baconnier
 
5
#    Copyright Camptocamp SA 2011
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
import time
 
22
 
 
23
from osv import fields, osv
 
24
 
 
25
 
 
26
class AccountReportPartnersLedgerWizard(osv.osv_memory):
 
27
    """Will launch partner ledger report and pass required args"""
 
28
 
 
29
    _inherit = "account.common.partner.report"
 
30
    _name = "partners.ledger.webkit"
 
31
    _description = "Partner Ledger Report"
 
32
 
 
33
    _columns = {
 
34
        'amount_currency': fields.boolean("With Currency",
 
35
                                          help="It adds the currency column"),
 
36
        'partner_ids': fields.many2many('res.partner', string='Filter on partner',
 
37
                                         help="Only selected partners will be printed. Leave empty to print all partners."),
 
38
        'filter': fields.selection([('filter_no', 'No Filters'),
 
39
                                    ('filter_date', 'Date'),
 
40
                                    ('filter_period', 'Periods')], "Filter by", required=True, help='Filter by date : no opening balance will be displayed. (opening balance can only be calculated based on period to be correct).'),
 
41
    }
 
42
    _defaults = {
 
43
        'amount_currency': False,
 
44
        'result_selection': 'customer_supplier',
 
45
    }
 
46
 
 
47
    def _check_fiscalyear(self, cr, uid, ids, context=None):
 
48
        obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
 
49
        if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
 
50
            return False
 
51
        return True
 
52
 
 
53
    _constraints = [
 
54
        (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
 
55
    ]
 
56
 
 
57
    def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
 
58
        res = {}
 
59
        if filter == 'filter_no':
 
60
            res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
 
61
        if filter == 'filter_date':
 
62
            if fiscalyear_id:
 
63
                fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
 
64
                date_from = fyear.date_start
 
65
                date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
 
66
            else:
 
67
                date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
 
68
            res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
 
69
        if filter == 'filter_period' and fiscalyear_id:
 
70
            start_period = end_period = False
 
71
            cr.execute('''
 
72
                SELECT * FROM (SELECT p.id
 
73
                               FROM account_period p
 
74
                               LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
 
75
                               WHERE f.id = %s
 
76
                               AND COALESCE(p.special, FALSE) = FALSE
 
77
                               ORDER BY p.date_start ASC
 
78
                               LIMIT 1) AS period_start
 
79
                UNION ALL
 
80
                SELECT * FROM (SELECT p.id
 
81
                               FROM account_period p
 
82
                               LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
 
83
                               WHERE f.id = %s
 
84
                               AND p.date_start < NOW()
 
85
                               AND COALESCE(p.special, FALSE) = FALSE
 
86
                               ORDER BY p.date_stop DESC
 
87
                               LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
 
88
            periods =  [i[0] for i in cr.fetchall()]
 
89
            if periods:
 
90
                start_period = end_period = periods[0]
 
91
                if len(periods) > 1:
 
92
                    end_period = periods[1]
 
93
            res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
 
94
        return res
 
95
 
 
96
    def pre_print_report(self, cr, uid, ids, data, context=None):
 
97
        data = super(AccountReportPartnersLedgerWizard, self).pre_print_report(cr, uid, ids, data, context)
 
98
        if context is None:
 
99
            context = {}
 
100
        # will be used to attach the report on the main account
 
101
        data['ids'] = [data['form']['chart_account_id']]
 
102
        vals = self.read(cr, uid, ids,
 
103
                         ['amount_currency', 'partner_ids',],
 
104
                         context=context)[0]
 
105
        data['form'].update(vals)
 
106
        return data
 
107
 
 
108
    def _print_report(self, cursor, uid, ids, data, context=None):
 
109
        context = context or {}
 
110
        # we update form with display account value
 
111
        data = self.pre_print_report(cursor, uid, ids, data, context=context)
 
112
        return {'type': 'ir.actions.report.xml',
 
113
                'report_name': 'account.account_report_partners_ledger_webkit',
 
114
                'datas': data}
 
115
 
 
116
AccountReportPartnersLedgerWizard()