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.1 KiB

11 years ago
11 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2014 Noviat nv/sa (www.noviat.com). All rights reserved.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.tools.translate import _
  23. from openerp.osv import orm, fields
  24. from openerp.addons.account.wizard.account_report_common_journal \
  25. import account_common_journal_report
  26. import logging
  27. _logger = logging.getLogger(__name__)
  28. class account_print_journal_xls(orm.TransientModel):
  29. _inherit = 'account.print.journal'
  30. _name = 'account.print.journal.xls'
  31. _description = 'Print/Export Journal'
  32. _columns = {
  33. 'journal_ids': fields.many2many('account.journal', string='Journals',
  34. required=True),
  35. 'group_entries': fields.boolean(
  36. 'Group Entries',
  37. help="Group entries with same General Account & Tax Code."),
  38. }
  39. _defaults = {
  40. 'group_entries': True,
  41. }
  42. def fields_get(self, cr, uid, allfields=None, context=None,
  43. write_access=True):
  44. res = super(account_print_journal_xls, self).fields_get(
  45. cr, uid, allfields=allfields, context=context,
  46. write_access=write_access)
  47. if context.get('print_by') == 'fiscalyear':
  48. if 'fiscalyear_id' in res:
  49. res['fiscalyear_id']['required'] = True
  50. if 'period_from' in res:
  51. res['period_from']['readonly'] = True
  52. if 'period_to' in res:
  53. res['period_to']['readonly'] = True
  54. else:
  55. if 'period_from' in res:
  56. res['period_from']['required'] = True
  57. if 'period_to' in res:
  58. res['period_to']['required'] = True
  59. return res
  60. def fy_period_ids(self, cr, uid, fiscalyear_id):
  61. """ returns all periods from a fiscalyear sorted by date """
  62. fy_period_ids = []
  63. cr.execute('''
  64. SELECT id, coalesce(special, False) AS special
  65. FROM account_period
  66. WHERE fiscalyear_id=%s ORDER BY date_start, special DESC''',
  67. (fiscalyear_id,))
  68. res = cr.fetchall()
  69. if res:
  70. fy_period_ids = [x[0] for x in res]
  71. return fy_period_ids
  72. def onchange_fiscalyear_id(self, cr, uid, ids, fiscalyear_id=False,
  73. context=None):
  74. res = {'value': {}}
  75. if context.get('print_by') == 'fiscalyear':
  76. # get period_from/to with opening/close periods
  77. fy_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  78. if fy_period_ids:
  79. res['value']['period_from'] = fy_period_ids[0]
  80. res['value']['period_to'] = fy_period_ids[-1]
  81. return res
  82. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  83. context=None, toolbar=False, submenu=False):
  84. """ skip account.common.journal.report,fields_view_get
  85. (adds domain filter on journal type) """
  86. return super(account_common_journal_report, self).\
  87. fields_view_get(cr, uid, view_id, view_type, context, toolbar,
  88. submenu)
  89. def xls_export(self, cr, uid, ids, context=None):
  90. return self.print_report(cr, uid, ids, context=context)
  91. def print_report(self, cr, uid, ids, context=None):
  92. if context is None:
  93. context = {}
  94. move_obj = self.pool.get('account.move')
  95. print_by = context.get('print_by')
  96. wiz_form = self.browse(cr, uid, ids)[0]
  97. fiscalyear_id = wiz_form.fiscalyear_id.id
  98. company_id = wiz_form.company_id.id
  99. if print_by == 'fiscalyear':
  100. wiz_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  101. else:
  102. period_from = wiz_form.period_from
  103. period_to = wiz_form.period_to
  104. cr.execute("""
  105. SELECT id, coalesce(special, False) AS special
  106. FROM account_period ap
  107. WHERE ap.date_start>=%s AND ap.date_stop<=%s AND company_id=%s
  108. ORDER BY date_start, special DESC""",
  109. (period_from.date_start,
  110. period_to.date_stop,
  111. company_id))
  112. wiz_period_ids = map(lambda x: x[0], cr.fetchall())
  113. wiz_journal_ids = [j.id for j in wiz_form.journal_ids]
  114. # sort journals
  115. cr.execute('SELECT id FROM account_journal '
  116. 'WHERE id IN %s ORDER BY type DESC',
  117. (tuple(wiz_journal_ids),))
  118. wiz_journal_ids = map(lambda x: x[0], cr.fetchall())
  119. datas = {
  120. 'model': 'account.journal',
  121. 'print_by': print_by,
  122. 'sort_selection': wiz_form.sort_selection,
  123. 'target_move': wiz_form.target_move,
  124. 'display_currency': wiz_form.amount_currency,
  125. 'group_entries': wiz_form.group_entries,
  126. }
  127. if wiz_form.target_move == 'posted':
  128. move_states = ['posted']
  129. else:
  130. move_states = ['draft', 'posted']
  131. if print_by == 'fiscalyear':
  132. journal_fy_ids = []
  133. for journal_id in wiz_journal_ids:
  134. aml_ids = move_obj.search(cr, uid,
  135. [('journal_id', '=', journal_id),
  136. ('period_id', 'in', wiz_period_ids),
  137. ('state', 'in', move_states)],
  138. limit=1)
  139. if aml_ids:
  140. journal_fy_ids.append((journal_id, fiscalyear_id))
  141. if not journal_fy_ids:
  142. raise orm.except_orm(
  143. _('No Data Available'),
  144. _('No records found for your selection!'))
  145. datas.update({
  146. 'ids': [x[0] for x in journal_fy_ids],
  147. 'journal_fy_ids': journal_fy_ids,
  148. })
  149. else:
  150. # perform account.move.line query in stead of
  151. # 'account.journal.period' since this table is not always reliable
  152. journal_period_ids = []
  153. for journal_id in wiz_journal_ids:
  154. period_ids = []
  155. for period_id in wiz_period_ids:
  156. aml_ids = move_obj.search(cr, uid,
  157. [('journal_id', '=', journal_id),
  158. ('period_id', '=', period_id),
  159. ('state', 'in', move_states)],
  160. limit=1)
  161. if aml_ids:
  162. period_ids.append(period_id)
  163. if period_ids:
  164. journal_period_ids.append((journal_id, period_ids))
  165. if not journal_period_ids:
  166. raise orm.except_orm(
  167. _('No Data Available'),
  168. _('No records found for your selection!'))
  169. datas.update({
  170. 'ids': [x[0] for x in journal_period_ids],
  171. 'journal_period_ids': journal_period_ids,
  172. })
  173. if context.get('xls_export'):
  174. return {'type': 'ir.actions.report.xml',
  175. 'report_name': 'nov.account.journal.xls',
  176. 'datas': datas}
  177. else:
  178. return {
  179. 'type': 'ir.actions.report.xml',
  180. 'report_name': 'nov.account.journal.print',
  181. 'datas': datas}