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.

172 lines
7.5 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2013 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 import account_common_journal_report
  25. import time
  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', required=True),
  34. 'group_entries': fields.boolean('Group Entries', help="Group entries with same General Account & Tax Code."),
  35. }
  36. _defaults = {
  37. 'group_entries': True,
  38. }
  39. def fields_get(self, cr, uid, fields=None, context=None):
  40. res = super(account_print_journal_xls, self).fields_get(cr, uid, fields, context)
  41. if context.get('print_by') == 'fiscalyear':
  42. if 'fiscalyear_id' in res:
  43. res['fiscalyear_id']['required'] = True
  44. if 'period_from' in res:
  45. res['period_from']['readonly'] = True
  46. if 'period_to' in res:
  47. res['period_to']['readonly'] = True
  48. else:
  49. if 'period_from' in res:
  50. res['period_from']['required'] = True
  51. if 'period_to' in res:
  52. res['period_to']['required'] = True
  53. return res
  54. def fy_period_ids(self, cr, uid, fiscalyear_id):
  55. """ returns all periods from a fiscalyear sorted by date """
  56. fy_period_ids = []
  57. cr.execute('SELECT id, coalesce(special, False) AS special FROM account_period '
  58. 'WHERE fiscalyear_id=%s ORDER BY date_start, special DESC',
  59. (fiscalyear_id,))
  60. res = cr.fetchall()
  61. if res:
  62. fy_period_ids = [x[0] for x in res]
  63. return fy_period_ids
  64. def onchange_fiscalyear_id(self, cr, uid, ids, fiscalyear_id=False, context=None):
  65. res = {'value': {}}
  66. if context.get('print_by') == 'fiscalyear':
  67. # get period_from/to with opening/close periods
  68. fy_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  69. if fy_period_ids:
  70. res['value']['period_from'] = fy_period_ids[0]
  71. res['value']['period_to'] = fy_period_ids[-1]
  72. return res
  73. def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
  74. """ skip account.common.journal.report,fields_view_get (adds domain filter on journal type) """
  75. return super(account_common_journal_report, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
  76. def xls_export(self, cr, uid, ids, context=None):
  77. return self.print_report(cr, uid, ids, context=context)
  78. def print_report(self, cr, uid, ids, context=None):
  79. if context is None:
  80. context = {}
  81. move_obj = self.pool.get('account.move')
  82. print_by = context.get('print_by')
  83. wiz_form = self.browse(cr, uid, ids)[0]
  84. fiscalyear_id = wiz_form.fiscalyear_id.id
  85. company_id = wiz_form.company_id.id
  86. if print_by == 'fiscalyear':
  87. wiz_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  88. else:
  89. period_from = wiz_form.period_from
  90. period_to = wiz_form.period_to
  91. cr.execute("SELECT id, coalesce(special, False) AS special FROM account_period ap "
  92. "WHERE ap.date_start>=%s AND ap.date_stop<=%s AND company_id=%s "
  93. "ORDER BY date_start, special DESC",
  94. (period_from.date_start, period_to.date_stop, company_id))
  95. wiz_period_ids = map(lambda x: x[0], cr.fetchall())
  96. wiz_journal_ids = [j.id for j in wiz_form.journal_ids]
  97. # sort journals
  98. cr.execute('SELECT id FROM account_journal '
  99. 'WHERE id IN %s ORDER BY type DESC',
  100. (tuple(wiz_journal_ids),))
  101. wiz_journal_ids = map(lambda x: x[0], cr.fetchall())
  102. datas = {
  103. 'model': 'account.journal',
  104. 'print_by': print_by,
  105. 'sort_selection': wiz_form.sort_selection,
  106. 'target_move': wiz_form.target_move,
  107. 'display_currency': wiz_form.amount_currency,
  108. 'group_entries': wiz_form.group_entries,
  109. }
  110. if wiz_form.target_move == 'posted':
  111. move_states = ['posted']
  112. else:
  113. move_states = ['draft', 'posted']
  114. if print_by == 'fiscalyear':
  115. journal_fy_ids = []
  116. for journal_id in wiz_journal_ids:
  117. aml_ids = move_obj.search(cr, uid,
  118. [('journal_id', '=', journal_id), ('period_id', 'in', wiz_period_ids), ('state', 'in', move_states)],
  119. limit=1)
  120. if aml_ids:
  121. journal_fy_ids.append((journal_id, fiscalyear_id))
  122. if not journal_fy_ids:
  123. raise orm.except_orm(_('No Data Available'), _('No records found for your selection!'))
  124. datas.update({
  125. 'ids': [x[0] for x in journal_fy_ids],
  126. 'journal_fy_ids': journal_fy_ids,
  127. })
  128. else:
  129. # perform account.move.line query in stead of 'account.journal.period' since this table is not always reliable
  130. journal_period_ids = []
  131. for journal_id in wiz_journal_ids:
  132. period_ids = []
  133. for period_id in wiz_period_ids:
  134. aml_ids = move_obj.search(cr, uid,
  135. [('journal_id', '=', journal_id), ('period_id', '=', period_id), ('state', 'in', move_states)],
  136. limit=1)
  137. if aml_ids:
  138. period_ids.append(period_id)
  139. if period_ids:
  140. journal_period_ids.append((journal_id, period_ids))
  141. if not journal_period_ids:
  142. raise orm.except_orm(_('No Data Available'), _('No records found for your selection!'))
  143. datas.update({
  144. 'ids': [x[0] for x in journal_period_ids],
  145. 'journal_period_ids': journal_period_ids,
  146. })
  147. if context.get('xls_export'):
  148. return {'type': 'ir.actions.report.xml',
  149. 'report_name': 'nov.account.journal.xls',
  150. 'datas': datas}
  151. else:
  152. return {
  153. 'type': 'ir.actions.report.xml',
  154. 'report_name': 'nov.account.journal.print',
  155. 'datas': datas}
  156. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: