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.

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