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.

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