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.

196 lines
8.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. # Copyright (C) 2013 Agile Business Group sagl
  7. # (<http://www.agilebg.com>) (<lorenzo.battistini@agilebg.com>)
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU Affero General Public License as
  11. # published by the Free Software Foundation, either version 3 of the
  12. # License, or (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. import time
  24. from lxml import etree
  25. from openerp.osv import fields, orm
  26. from openerp.tools.translate import _
  27. class account_common_report(orm.TransientModel):
  28. _name = "account_financial_report_horizontal.common.report"
  29. _description = "Account Common Report"
  30. _columns = {
  31. 'chart_account_id': fields.many2one(
  32. 'account.account', 'Chart of account',
  33. help='Select Charts of Accounts',
  34. required=True, domain=[('parent_id', '=', False)]),
  35. 'fiscalyear_id': fields.many2one(
  36. 'account.fiscalyear', 'Fiscal year',
  37. help='Keep empty for all open fiscal year'),
  38. 'filter': fields.selection([
  39. ('filter_no', 'No Filters'), ('filter_date', 'Date'),
  40. ('filter_period', 'Periods')
  41. ],
  42. "Filter by", required=True),
  43. 'period_from': fields.many2one('account.period', 'Start period'),
  44. 'period_to': fields.many2one('account.period', 'End period'),
  45. 'date_from': fields.date("Start Date"),
  46. 'date_to': fields.date("End Date"),
  47. 'target_move': fields.selection([
  48. ('posted', 'All Posted Entries'),
  49. ('all', 'All Entries'),
  50. ], 'Target Moves', required=True
  51. ),
  52. }
  53. def fields_view_get(
  54. self, cr, uid, view_id=None, view_type='form', context=None,
  55. toolbar=False, submenu=False
  56. ):
  57. if context is None:
  58. context = {}
  59. res = super(account_common_report, self).fields_view_get(
  60. cr, uid, view_id=view_id, view_type=view_type, context=context,
  61. toolbar=toolbar, submenu=False)
  62. if context.get('active_model', False) == 'account.account' and view_id:
  63. doc = etree.XML(res['arch'])
  64. nodes = doc.xpath("//field[@name='chart_account_id']")
  65. for node in nodes:
  66. node.set('readonly', '1')
  67. node.set(
  68. 'help',
  69. 'If you print the report from Account list/form view '
  70. 'it will not consider Charts of account')
  71. res['arch'] = etree.tostring(doc)
  72. return res
  73. def onchange_filter(
  74. self, cr, uid, ids, filter='filter_no', fiscalyear_id=False,
  75. context=None
  76. ):
  77. res = {}
  78. if filter == 'filter_no':
  79. res['value'] = {
  80. 'period_from': False, 'period_to': False,
  81. 'date_from': False, 'date_to': False
  82. }
  83. if filter == 'filter_date':
  84. res['value'] = {
  85. 'period_from': False, 'period_to': False,
  86. 'date_from': time.strftime('%Y-01-01'),
  87. 'date_to': time.strftime('%Y-%m-%d')
  88. }
  89. if filter == 'filter_period' and fiscalyear_id:
  90. start_period = end_period = False
  91. cr.execute('''
  92. SELECT * FROM (SELECT p.id
  93. FROM account_period p
  94. LEFT JOIN account_fiscalyear f
  95. ON (p.fiscalyear_id = f.id)
  96. WHERE f.id = %s
  97. ORDER BY p.date_start ASC
  98. LIMIT 1) AS period_start
  99. UNION
  100. SELECT * FROM (SELECT p.id
  101. FROM account_period p
  102. LEFT JOIN account_fiscalyear f
  103. ON (p.fiscalyear_id = f.id)
  104. WHERE f.id = %s
  105. AND p.date_start < NOW()
  106. ORDER BY p.date_stop DESC
  107. LIMIT 1) AS period_stop''', (
  108. fiscalyear_id, fiscalyear_id
  109. )
  110. )
  111. periods = [i[0] for i in cr.fetchall()]
  112. if periods and len(periods) > 1:
  113. start_period = periods[0]
  114. end_period = periods[1]
  115. res['value'] = {
  116. 'period_from': start_period, 'period_to': end_period,
  117. 'date_from': False, 'date_to': False
  118. }
  119. return res
  120. def _get_account(self, cr, uid, context=None):
  121. accounts = self.pool.get('account.account').search(
  122. cr, uid, [('parent_id', '=', False)], limit=1)
  123. return accounts and accounts[0] or False
  124. def _get_fiscalyear(self, cr, uid, context=None):
  125. now = time.strftime('%Y-%m-%d')
  126. fiscalyears = self.pool.get('account.fiscalyear').search(
  127. cr, uid, [('date_start', '<', now), (
  128. 'date_stop', '>', now)], limit=1)
  129. return fiscalyears and fiscalyears[0] or False
  130. _defaults = {
  131. 'fiscalyear_id': _get_fiscalyear,
  132. 'filter': 'filter_no',
  133. 'chart_account_id': _get_account,
  134. 'target_move': 'posted',
  135. }
  136. def _build_contexts(self, cr, uid, ids, data, context=None):
  137. if context is None:
  138. context = {}
  139. result = {}
  140. result['fiscalyear'] = 'fiscalyear_id' in data['form'] and data[
  141. 'form']['fiscalyear_id'] or False
  142. result['chart_account_id'] = 'chart_account_id' in data[
  143. 'form'] and data['form']['chart_account_id'] or False
  144. if data['form']['filter'] == 'filter_date':
  145. result['date_from'] = data['form']['date_from']
  146. result['date_to'] = data['form']['date_to']
  147. elif data['form']['filter'] == 'filter_period':
  148. if not data['form']['period_from'] or not data[
  149. 'form'
  150. ]['period_to']:
  151. raise orm.except_orm(
  152. _('Error'), _('Select a starting and an ending period'))
  153. result['period_from'] = data['form']['period_from']
  154. result['period_to'] = data['form']['period_to']
  155. if data['form']['period_to'] and result['period_to']:
  156. period_from = data['form'].get('period_from', False) and data[
  157. 'form']['period_from'][0] or False
  158. period_to = data['form'].get('period_to', False) and data[
  159. 'form']['period_to'][0] or False
  160. period_obj = self.pool.get('account.period')
  161. result['periods'] = period_obj.build_ctx_periods(
  162. cr, uid, period_from, period_to)
  163. return result
  164. def _print_report(self, cr, uid, ids, data, context=None):
  165. raise (_('Error'), _('not implemented'))
  166. def check_report(self, cr, uid, ids, context=None):
  167. if context is None:
  168. context = {}
  169. data = {}
  170. data['ids'] = context.get('active_ids', [])
  171. data['model'] = context.get('active_model', 'ir.ui.menu')
  172. data['form'] = self.read(cr, uid, ids, [
  173. 'date_from', 'date_to', 'fiscalyear_id', 'period_from',
  174. 'period_to', 'filter', 'chart_account_id', 'target_move'
  175. ])[0]
  176. used_context = self._build_contexts(
  177. cr, uid, ids, data, context=context)
  178. data['form']['periods'] = used_context.get(
  179. 'periods', False) and used_context['periods'] or []
  180. data['form']['used_context'] = used_context
  181. return self._print_report(cr, uid, ids, data, context=context)