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.

194 lines
8.0 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, fields=None, context=None):
  43. res = super(account_print_journal_xls, self).fields_get(
  44. cr, uid, fields, context)
  45. if context.get('print_by') == 'fiscalyear':
  46. if 'fiscalyear_id' in res:
  47. res['fiscalyear_id']['required'] = True
  48. if 'period_from' in res:
  49. res['period_from']['readonly'] = True
  50. if 'period_to' in res:
  51. res['period_to']['readonly'] = True
  52. else:
  53. if 'period_from' in res:
  54. res['period_from']['required'] = True
  55. if 'period_to' in res:
  56. res['period_to']['required'] = True
  57. return res
  58. def fy_period_ids(self, cr, uid, fiscalyear_id):
  59. """ returns all periods from a fiscalyear sorted by date """
  60. fy_period_ids = []
  61. cr.execute('''
  62. SELECT id, coalesce(special, False) AS special
  63. FROM account_period
  64. WHERE fiscalyear_id=%s ORDER BY date_start, special DESC''',
  65. (fiscalyear_id,))
  66. res = cr.fetchall()
  67. if res:
  68. fy_period_ids = [x[0] for x in res]
  69. return fy_period_ids
  70. def onchange_fiscalyear_id(self, cr, uid, ids, fiscalyear_id=False,
  71. context=None):
  72. res = {'value': {}}
  73. if context.get('print_by') == 'fiscalyear':
  74. # get period_from/to with opening/close periods
  75. fy_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  76. if fy_period_ids:
  77. res['value']['period_from'] = fy_period_ids[0]
  78. res['value']['period_to'] = fy_period_ids[-1]
  79. return res
  80. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  81. context=None, toolbar=False, submenu=False):
  82. """ skip account.common.journal.report,fields_view_get
  83. (adds domain filter on journal type) """
  84. return super(account_common_journal_report, self).\
  85. fields_view_get(cr, uid, view_id, view_type, context, toolbar,
  86. submenu)
  87. def xls_export(self, cr, uid, ids, context=None):
  88. return self.print_report(cr, uid, ids, context=context)
  89. def print_report(self, cr, uid, ids, context=None):
  90. if context is None:
  91. context = {}
  92. move_obj = self.pool.get('account.move')
  93. print_by = context.get('print_by')
  94. wiz_form = self.browse(cr, uid, ids)[0]
  95. fiscalyear_id = wiz_form.fiscalyear_id.id
  96. company_id = wiz_form.company_id.id
  97. if print_by == 'fiscalyear':
  98. wiz_period_ids = self.fy_period_ids(cr, uid, fiscalyear_id)
  99. else:
  100. period_from = wiz_form.period_from
  101. period_to = wiz_form.period_to
  102. cr.execute("""
  103. SELECT id, coalesce(special, False) AS special
  104. FROM account_period ap
  105. WHERE ap.date_start>=%s AND ap.date_stop<=%s AND company_id=%s
  106. ORDER BY date_start, special DESC""",
  107. (period_from.date_start,
  108. period_to.date_stop,
  109. company_id))
  110. wiz_period_ids = map(lambda x: x[0], cr.fetchall())
  111. wiz_journal_ids = [j.id for j in wiz_form.journal_ids]
  112. # sort journals
  113. cr.execute('SELECT id FROM account_journal '
  114. 'WHERE id IN %s ORDER BY type DESC',
  115. (tuple(wiz_journal_ids),))
  116. wiz_journal_ids = map(lambda x: x[0], cr.fetchall())
  117. datas = {
  118. 'model': 'account.journal',
  119. 'print_by': print_by,
  120. 'sort_selection': wiz_form.sort_selection,
  121. 'target_move': wiz_form.target_move,
  122. 'display_currency': wiz_form.amount_currency,
  123. 'group_entries': wiz_form.group_entries,
  124. }
  125. if wiz_form.target_move == 'posted':
  126. move_states = ['posted']
  127. else:
  128. move_states = ['draft', 'posted']
  129. if print_by == 'fiscalyear':
  130. journal_fy_ids = []
  131. for journal_id in wiz_journal_ids:
  132. aml_ids = move_obj.search(cr, uid,
  133. [('journal_id', '=', journal_id),
  134. ('period_id', 'in', wiz_period_ids),
  135. ('state', 'in', move_states)],
  136. limit=1)
  137. if aml_ids:
  138. journal_fy_ids.append((journal_id, fiscalyear_id))
  139. if not journal_fy_ids:
  140. raise orm.except_orm(
  141. _('No Data Available'),
  142. _('No records found for your selection!'))
  143. datas.update({
  144. 'ids': [x[0] for x in journal_fy_ids],
  145. 'journal_fy_ids': journal_fy_ids,
  146. })
  147. else:
  148. # perform account.move.line query in stead of
  149. # 'account.journal.period' since this table is not always reliable
  150. journal_period_ids = []
  151. for journal_id in wiz_journal_ids:
  152. period_ids = []
  153. for period_id in wiz_period_ids:
  154. aml_ids = move_obj.search(cr, uid,
  155. [('journal_id', '=', journal_id),
  156. ('period_id', '=', period_id),
  157. ('state', 'in', move_states)],
  158. limit=1)
  159. if aml_ids:
  160. period_ids.append(period_id)
  161. if period_ids:
  162. journal_period_ids.append((journal_id, period_ids))
  163. if not journal_period_ids:
  164. raise orm.except_orm(
  165. _('No Data Available'),
  166. _('No records found for your selection!'))
  167. datas.update({
  168. 'ids': [x[0] for x in journal_period_ids],
  169. 'journal_period_ids': journal_period_ids,
  170. })
  171. if context.get('xls_export'):
  172. return {'type': 'ir.actions.report.xml',
  173. 'report_name': 'nov.account.journal.xls',
  174. 'datas': datas}
  175. else:
  176. return {
  177. 'type': 'ir.actions.report.xml',
  178. 'report_name': 'nov.account.journal.print',
  179. 'datas': datas}