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.

139 lines
6.3 KiB

  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 osv import osv
  25. from osv import fields
  26. import time
  27. from lxml import etree
  28. class AccountReportPrintJournalWizard(osv.osv_memory):
  29. """Will launch print journal report and pass requiered args"""
  30. _inherit = "account.common.account.report"
  31. _name = "print.journal.webkit"
  32. _description = "Journals Report"
  33. _columns = {
  34. 'amount_currency': fields.boolean("With Currency", help="It adds the currency column"),
  35. }
  36. _defaults = {
  37. 'amount_currency': False,
  38. 'journal_ids': False,
  39. 'filter': 'filter_period',
  40. }
  41. def _check_fiscalyear(self, cr, uid, ids, context=None):
  42. obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
  43. if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
  44. return False
  45. return True
  46. _constraints = [
  47. (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
  48. ]
  49. def pre_print_report(self, cr, uid, ids, data, context=None):
  50. data = super(AccountReportPrintJournalWizard, self).pre_print_report(cr, uid, ids, data, context)
  51. # will be used to attach the report on the main account
  52. data['ids'] = [data['form']['chart_account_id']]
  53. vals = self.read(cr, uid, ids,
  54. ['amount_currency',
  55. 'display_account',
  56. 'journal_ids'],
  57. context=context)[0]
  58. data['form'].update(vals)
  59. return data
  60. def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
  61. res = {}
  62. if filter == 'filter_no':
  63. res['value'] = {'period_from': False, 'period_to': False, 'date_from': False, 'date_to': False}
  64. if filter == 'filter_date':
  65. if fiscalyear_id:
  66. fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
  67. date_from = fyear.date_start
  68. date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
  69. else:
  70. date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
  71. res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
  72. if filter == 'filter_period' and fiscalyear_id:
  73. start_period = end_period = False
  74. cr.execute('''
  75. SELECT * FROM (SELECT p.id
  76. FROM account_period p
  77. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  78. WHERE f.id = %s
  79. AND COALESCE(p.special, FALSE) = FALSE
  80. ORDER BY p.date_start ASC
  81. LIMIT 1) AS period_start
  82. UNION ALL
  83. SELECT * FROM (SELECT p.id
  84. FROM account_period p
  85. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  86. WHERE f.id = %s
  87. AND p.date_start < NOW()
  88. AND COALESCE(p.special, FALSE) = FALSE
  89. ORDER BY p.date_stop DESC
  90. LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
  91. periods = [i[0] for i in cr.fetchall()]
  92. if periods:
  93. start_period = end_period = periods[0]
  94. if len(periods) > 1:
  95. end_period = periods[1]
  96. res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
  97. return res
  98. def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
  99. '''
  100. used to set the domain on 'journal_ids' field: we exclude or only propose the journals of type
  101. sale/purchase (+refund) accordingly to the presence of the key 'sale_purchase_only' in the context.
  102. '''
  103. if context is None:
  104. context = {}
  105. res = super(AccountReportPrintJournalWizard, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
  106. doc = etree.XML(res['arch'])
  107. if context.get('sale_purchase_only'):
  108. domain = "[('type', 'in', ('sale','purchase','sale_refund','purchase_refund'))]"
  109. else:
  110. domain = "[('type', 'not in', ('sale','purchase','sale_refund','purchase_refund'))]"
  111. nodes = doc.xpath("//field[@name='journal_ids']")
  112. for node in nodes:
  113. node.set('domain', domain)
  114. res['arch'] = etree.tostring(doc)
  115. return res
  116. def _print_report(self, cursor, uid, ids, data, context=None):
  117. context = context or {}
  118. # we update form with display account value
  119. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  120. return {'type': 'ir.actions.report.xml',
  121. 'report_name': 'account.account_report_print_journal_webkit',
  122. 'datas': data}
  123. AccountReportPrintJournalWizard()
  124. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: