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.

132 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 osv import fields, osv
  23. class AccountReportGeneralLedgerWizard(osv.osv_memory):
  24. """Will launch general ledger report and pass requiered 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. if context is None:
  61. context = {}
  62. # will be used to attach the report on the main account
  63. data['ids'] = [data['form']['chart_account_id']]
  64. vals = self.read(cr, uid, ids,
  65. ['amount_currency',
  66. 'display_account',
  67. 'account_ids',
  68. 'centralize'],
  69. context=context)[0]
  70. data['form'].update(vals)
  71. return data
  72. def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
  73. res = {}
  74. if filter == 'filter_no':
  75. res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
  76. if filter == 'filter_date':
  77. if fiscalyear_id:
  78. fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
  79. date_from = fyear.date_start
  80. date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
  81. else:
  82. date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
  83. res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
  84. if filter == 'filter_period' and fiscalyear_id:
  85. start_period = end_period = False
  86. cr.execute('''
  87. SELECT * FROM (SELECT p.id
  88. FROM account_period p
  89. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  90. WHERE f.id = %s
  91. AND COALESCE(p.special, FALSE) = FALSE
  92. ORDER BY p.date_start ASC
  93. LIMIT 1) AS period_start
  94. UNION
  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 p.date_start < NOW()
  100. AND COALESCE(p.special, FALSE) = FALSE
  101. ORDER BY p.date_stop DESC
  102. LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
  103. periods = [i[0] for i in cr.fetchall()]
  104. if periods:
  105. start_period = end_period = periods[0]
  106. if len(periods) > 1:
  107. end_period = periods[1]
  108. res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
  109. return res
  110. def _print_report(self, cursor, uid, ids, data, context=None):
  111. context = context or {}
  112. # we update form with display account value
  113. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  114. return {'type': 'ir.actions.report.xml',
  115. 'report_name': 'account.account_report_general_ledger_webkit',
  116. 'datas': data}
  117. AccountReportGeneralLedgerWizard()