You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
6.1 KiB

  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. from openerp.osv import fields, orm
  23. class AccountReportGeneralLedgerWizard(orm.TransientModel):
  24. """Will launch general ledger report and pass required args"""
  25. _inherit = "account.common.account.report"
  26. _name = "general.ledger.webkit"
  27. _description = "General Ledger Report"
  28. def _get_account_ids(self, cr, uid, context=None):
  29. res = False
  30. if context.get('active_model', False) == 'account.account' and context.get('active_ids', False):
  31. res = context['active_ids']
  32. return res
  33. _columns = {
  34. 'amount_currency': fields.boolean("With Currency",
  35. help="It adds the currency column"),
  36. 'display_account': fields.selection([('bal_all', 'All'),
  37. ('bal_mix', 'With transactions or non zero balance')],
  38. 'Display accounts',
  39. required=True),
  40. 'account_ids': fields.many2many('account.account', string='Filter on accounts',
  41. help="""Only selected accounts will be printed. Leave empty to print all accounts."""),
  42. 'centralize': fields.boolean('Activate Centralization', help='Uncheck to display all the details of centralized accounts.')
  43. }
  44. _defaults = {
  45. 'amount_currency': False,
  46. 'display_account': 'bal_mix',
  47. 'account_ids': _get_account_ids,
  48. 'centralize': True,
  49. }
  50. def _check_fiscalyear(self, cr, uid, ids, context=None):
  51. obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
  52. if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
  53. return False
  54. return True
  55. _constraints = [
  56. (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
  57. ]
  58. def pre_print_report(self, cr, uid, ids, data, context=None):
  59. data = super(AccountReportGeneralLedgerWizard, self).pre_print_report(cr, uid, ids, data, context)
  60. # will be used to attach the report on the main account
  61. data['ids'] = [data['form']['chart_account_id']]
  62. vals = self.read(cr, uid, ids,
  63. ['amount_currency',
  64. 'display_account',
  65. 'account_ids',
  66. 'centralize'],
  67. context=context)[0]
  68. data['form'].update(vals)
  69. return data
  70. def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
  71. res = {}
  72. if filter == 'filter_no':
  73. res['value'] = {
  74. 'period_from': False,
  75. 'period_to': False,
  76. 'date_from': False,
  77. 'date_to': False,
  78. }
  79. if filter == 'filter_date':
  80. if fiscalyear_id:
  81. fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
  82. date_from = fyear.date_start
  83. date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
  84. else:
  85. date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
  86. res['value'] = {
  87. 'period_from': False,
  88. 'period_to': False,
  89. 'date_from': date_from,
  90. 'date_to': date_to
  91. }
  92. if filter == 'filter_period' and fiscalyear_id:
  93. start_period = end_period = False
  94. cr.execute('''
  95. SELECT * FROM (SELECT p.id
  96. FROM account_period p
  97. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  98. WHERE f.id = %s
  99. AND COALESCE(p.special, FALSE) = FALSE
  100. ORDER BY p.date_start ASC
  101. LIMIT 1) AS period_start
  102. UNION ALL
  103. SELECT * FROM (SELECT p.id
  104. FROM account_period p
  105. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  106. WHERE f.id = %s
  107. AND p.date_start < NOW()
  108. AND COALESCE(p.special, FALSE) = FALSE
  109. ORDER BY p.date_stop DESC
  110. LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
  111. periods = [i[0] for i in cr.fetchall()]
  112. if periods:
  113. start_period = end_period = periods[0]
  114. if len(periods) > 1:
  115. end_period = periods[1]
  116. res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
  117. return res
  118. def _print_report(self, cursor, uid, ids, data, context=None):
  119. # we update form with display account value
  120. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  121. return {'type': 'ir.actions.report.xml',
  122. 'report_name': 'account.account_report_general_ledger_webkit',
  123. 'datas': data}