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.

138 lines
6.3 KiB

11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # account_financial_report_webkit module for OpenERP
  5. # Copyright (C) 2012 SYLEAM Info Services (<http://www.syleam.fr/>)
  6. # Sebastien LANGE <sebastien.lange@syleam.fr>
  7. #
  8. # This file is a part of account_financial_report_webkit
  9. #
  10. # account_financial_report_webkit is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU Affero General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # account_financial_report_webkit is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Affero General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Affero General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. ##############################################################################
  24. from openerp.osv import fields, orm
  25. import time
  26. from lxml import etree
  27. class AccountReportPrintJournalWizard(orm.TransientModel):
  28. """Will launch print journal report and pass requiered args"""
  29. _inherit = "account.common.account.report"
  30. _name = "print.journal.webkit"
  31. _description = "Journals Report"
  32. _columns = {
  33. 'amount_currency': fields.boolean("With Currency", help="It adds the currency column"),
  34. }
  35. _defaults = {
  36. 'amount_currency': False,
  37. 'journal_ids': False,
  38. 'filter': 'filter_period',
  39. }
  40. def _check_fiscalyear(self, cr, uid, ids, context=None):
  41. obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
  42. if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
  43. return False
  44. return True
  45. _constraints = [
  46. (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
  47. ]
  48. def pre_print_report(self, cr, uid, ids, data, context=None):
  49. data = super(AccountReportPrintJournalWizard, self).pre_print_report(cr, uid, ids, data, context)
  50. # will be used to attach the report on the main account
  51. data['ids'] = [data['form']['chart_account_id']]
  52. vals = self.read(cr, uid, ids,
  53. ['amount_currency',
  54. 'display_account',
  55. 'journal_ids'],
  56. context=context)[0]
  57. data['form'].update(vals)
  58. return data
  59. def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
  60. res = {}
  61. if filter == 'filter_no':
  62. res['value'] = {'period_from': False, 'period_to': False, 'date_from': False, 'date_to': False}
  63. if filter == 'filter_date':
  64. if fiscalyear_id:
  65. fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
  66. date_from = fyear.date_start
  67. date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
  68. else:
  69. date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
  70. res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
  71. if filter == 'filter_period' and fiscalyear_id:
  72. start_period = end_period = False
  73. cr.execute('''
  74. SELECT * FROM (SELECT p.id
  75. FROM account_period p
  76. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  77. WHERE f.id = %s
  78. AND COALESCE(p.special, FALSE) = FALSE
  79. ORDER BY p.date_start ASC
  80. LIMIT 1) AS period_start
  81. UNION ALL
  82. SELECT * FROM (SELECT p.id
  83. FROM account_period p
  84. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  85. WHERE f.id = %s
  86. AND p.date_start < NOW()
  87. AND COALESCE(p.special, FALSE) = FALSE
  88. ORDER BY p.date_stop DESC
  89. LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
  90. periods = [i[0] for i in cr.fetchall()]
  91. if periods:
  92. start_period = end_period = periods[0]
  93. if len(periods) > 1:
  94. end_period = periods[1]
  95. res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
  96. return res
  97. def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
  98. '''
  99. used to set the domain on 'journal_ids' field: we exclude or only propose the journals of type
  100. sale/purchase (+refund) accordingly to the presence of the key 'sale_purchase_only' in the context.
  101. '''
  102. if context is None:
  103. context = {}
  104. res = super(AccountReportPrintJournalWizard, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
  105. doc = etree.XML(res['arch'])
  106. if context.get('sale_purchase_only'):
  107. domain = "[('type', 'in', ('sale','purchase','sale_refund','purchase_refund'))]"
  108. else:
  109. domain = "[('type', 'not in', ('sale','purchase','sale_refund','purchase_refund'))]"
  110. nodes = doc.xpath("//field[@name='journal_ids']")
  111. for node in nodes:
  112. node.set('domain', domain)
  113. res['arch'] = etree.tostring(doc)
  114. return res
  115. def _print_report(self, cursor, uid, ids, data, context=None):
  116. context = context or {}
  117. # we update form with display account value
  118. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  119. return {'type': 'ir.actions.report.xml',
  120. 'report_name': 'account.account_report_print_journal_webkit',
  121. 'datas': data}
  122. AccountReportPrintJournalWizard()
  123. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: