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.

287 lines
13 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. # -*- encoding: utf-8 -*-
  2. ###########################################################################
  3. # Module Writen to OpenERP, Open Source Management Solution
  4. # Copyright (C) OpenERP Venezuela (<http://openerp.com.ve>).
  5. # All Rights Reserved
  6. # Credits######################################################
  7. # Coded by: Humberto Arocha humberto@openerp.com.ve
  8. # Angelica Barrios angelicaisabelb@gmail.com
  9. # Jordi Esteve <jesteve@zikzakmedia.com>
  10. # Javier Duran <javieredm@gmail.com>
  11. # Planified by: Humberto Arocha
  12. # Finance by: LUBCAN COL S.A.S http://www.lubcancol.com
  13. # Audited by: Humberto Arocha humberto@openerp.com.ve
  14. #############################################################################
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. ##############################################################################
  28. from osv import osv, fields
  29. import pooler
  30. import time
  31. from tools.translate import _
  32. class wizard_report(osv.osv_memory):
  33. _name = "wizard.report"
  34. _columns = {
  35. 'afr_id': fields.many2one('afr', 'Custom Report', help='If you have already set a Custom Report, Select it Here.'),
  36. 'company_id': fields.many2one('res.company', 'Company', required=True),
  37. 'currency_id': fields.many2one('res.currency', 'Currency', help="Currency at which this report will be expressed. If not selected will be used the one set in the company"),
  38. 'inf_type': fields.selection([('BS', 'Balance Sheet'), ('IS', 'Income Statement')], 'Type', required=True),
  39. 'columns': fields.selection([('one', 'End. Balance'), ('two', 'Debit | Credit'), ('four', 'Initial | Debit | Credit | YTD'), ('five', 'Initial | Debit | Credit | Period | YTD'), ('qtr', "4 QTR's | YTD"), ('thirteen', '12 Months | YTD')], 'Columns', required=True),
  40. 'display_account': fields.selection([('all', 'All Accounts'), ('bal', 'With Balance'), ('mov', 'With movements'), ('bal_mov', 'With Balance / Movements')], 'Display accounts'),
  41. 'display_account_level': fields.integer('Up to level', help='Display accounts up to this level (0 to show all)'),
  42. 'account_list': fields.many2many('account.account', 'rel_wizard_account', 'account_list', 'account_id', 'Root accounts', required=True),
  43. 'fiscalyear': fields.many2one('account.fiscalyear', 'Fiscal year', help='Fiscal Year for this report', required=True),
  44. 'periods': fields.many2many('account.period', 'rel_wizard_period', 'wizard_id', 'period_id', 'Periods', help='All periods in the fiscal year if empty'),
  45. 'analytic_ledger': fields.boolean('Analytic Ledger', help="Allows to Generate an Analytic Ledger for accounts with moves. Available when Balance Sheet and 'Initial | Debit | Credit | YTD' are selected"),
  46. 'tot_check': fields.boolean('Summarize?', help='Checking will add a new line at the end of the Report which will Summarize Columns in Report'),
  47. 'lab_str': fields.char('Description', help='Description for the Summary', size=128),
  48. 'target_move': fields.selection([('posted', 'All Posted Entries'),
  49. ('all', 'All Entries'),
  50. ], 'Entries to Include', required=True,
  51. help='Print All Accounting Entries or just Posted Accounting Entries'),
  52. #~ Deprecated fields
  53. 'filter': fields.selection([('bydate', 'By Date'), ('byperiod', 'By Period'), ('all', 'By Date and Period'), ('none', 'No Filter')], 'Date/Period Filter'),
  54. 'date_to': fields.date('End date'),
  55. 'date_from': fields.date('Start date'),
  56. }
  57. _defaults = {
  58. 'date_from': lambda *a: time.strftime('%Y-%m-%d'),
  59. 'date_to': lambda *a: time.strftime('%Y-%m-%d'),
  60. 'filter': lambda *a: 'byperiod',
  61. 'display_account_level': lambda *a: 0,
  62. 'inf_type': lambda *a: 'BS',
  63. 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.invoice', context=c),
  64. 'fiscalyear': lambda self, cr, uid, c: self.pool.get('account.fiscalyear').find(cr, uid),
  65. 'display_account': lambda *a: 'bal_mov',
  66. 'columns': lambda *a: 'five',
  67. 'target_move': 'posted',
  68. }
  69. def onchange_inf_type(self, cr, uid, ids, inf_type, context=None):
  70. if context is None:
  71. context = {}
  72. res = {'value': {}}
  73. if inf_type != 'BS':
  74. res['value'].update({'analytic_ledger': False})
  75. return res
  76. def onchange_columns(self, cr, uid, ids, columns, fiscalyear, periods, context=None):
  77. if context is None:
  78. context = {}
  79. res = {'value': {}}
  80. p_obj = self.pool.get("account.period")
  81. all_periods = p_obj.search(cr, uid, [('fiscalyear_id', '=', fiscalyear), (
  82. 'special', '=', False)], context=context)
  83. s = set(periods[0][2])
  84. t = set(all_periods)
  85. go = periods[0][2] and s.issubset(t) or False
  86. if columns != 'four':
  87. res['value'].update({'analytic_ledger': False})
  88. if columns in ('qtr', 'thirteen'):
  89. res['value'].update({'periods': all_periods})
  90. else:
  91. if go:
  92. res['value'].update({'periods': periods})
  93. else:
  94. res['value'].update({'periods': []})
  95. return res
  96. def onchange_analytic_ledger(self, cr, uid, ids, company_id, analytic_ledger, context=None):
  97. if context is None:
  98. context = {}
  99. context['company_id'] = company_id
  100. res = {'value': {}}
  101. cur_id = self.pool.get('res.company').browse(
  102. cr, uid, company_id, context=context).currency_id.id
  103. res['value'].update({'currency_id': cur_id})
  104. return res
  105. def onchange_company_id(self, cr, uid, ids, company_id, context=None):
  106. if context is None:
  107. context = {}
  108. context['company_id'] = company_id
  109. res = {'value': {}}
  110. if not company_id:
  111. return res
  112. cur_id = self.pool.get('res.company').browse(
  113. cr, uid, company_id, context=context).currency_id.id
  114. fy_id = self.pool.get('account.fiscalyear').find(
  115. cr, uid, context=context)
  116. res['value'].update({'fiscalyear': fy_id})
  117. res['value'].update({'currency_id': cur_id})
  118. res['value'].update({'account_list': []})
  119. res['value'].update({'periods': []})
  120. res['value'].update({'afr_id': None})
  121. return res
  122. def onchange_afr_id(self, cr, uid, ids, afr_id, context=None):
  123. if context is None:
  124. context = {}
  125. res = {'value': {}}
  126. if not afr_id:
  127. return res
  128. afr_brw = self.pool.get('afr').browse(cr, uid, afr_id, context=context)
  129. res['value'].update({
  130. 'currency_id': afr_brw.currency_id and afr_brw.currency_id.id or afr_brw.company_id.currency_id.id})
  131. res['value'].update({'inf_type': afr_brw.inf_type or 'BS'})
  132. res['value'].update({'columns': afr_brw.columns or 'five'})
  133. res['value'].update({
  134. 'display_account': afr_brw.display_account or 'bal_mov'})
  135. res['value'].update({
  136. 'display_account_level': afr_brw.display_account_level or 0})
  137. res['value'].update({
  138. 'fiscalyear': afr_brw.fiscalyear_id and afr_brw.fiscalyear_id.id})
  139. res['value'].update({'account_list': [
  140. acc.id for acc in afr_brw.account_ids]})
  141. res['value'].update({'periods': [p.id for p in afr_brw.period_ids]})
  142. res['value'].update({
  143. 'analytic_ledger': afr_brw.analytic_ledger or False})
  144. res['value'].update({'tot_check': afr_brw.tot_check or False})
  145. res['value'].update({'lab_str': afr_brw.lab_str or _(
  146. 'Write a Description for your Summary Total')})
  147. return res
  148. def _get_defaults(self, cr, uid, data, context=None):
  149. if context is None:
  150. context = {}
  151. user = pooler.get_pool(cr.dbname).get(
  152. 'res.users').browse(cr, uid, uid, context=context)
  153. if user.company_id:
  154. company_id = user.company_id.id
  155. else:
  156. company_id = pooler.get_pool(cr.dbname).get(
  157. 'res.company').search(cr, uid, [('parent_id', '=', False)])[0]
  158. data['form']['company_id'] = company_id
  159. fiscalyear_obj = pooler.get_pool(cr.dbname).get('account.fiscalyear')
  160. data['form']['fiscalyear'] = fiscalyear_obj.find(cr, uid)
  161. data['form']['context'] = context
  162. return data['form']
  163. def _check_state(self, cr, uid, data, context=None):
  164. if context is None:
  165. context = {}
  166. if data['form']['filter'] == 'bydate':
  167. self._check_date(cr, uid, data, context)
  168. return data['form']
  169. def _check_date(self, cr, uid, data, context=None):
  170. if context is None:
  171. context = {}
  172. if data['form']['date_from'] > data['form']['date_to']:
  173. raise osv.except_osv(_('Error !'), (
  174. 'La fecha final debe ser mayor a la inicial'))
  175. sql = """SELECT f.id, f.date_start, f.date_stop
  176. FROM account_fiscalyear f
  177. WHERE '%s' = f.id """ % (data['form']['fiscalyear'])
  178. cr.execute(sql)
  179. res = cr.dictfetchall()
  180. if res:
  181. if (data['form']['date_to'] > res[0]['date_stop'] or data['form']['date_from'] < res[0]['date_start']):
  182. raise osv.except_osv(_('UserError'), 'Las fechas deben estar entre %s y %s' % (
  183. res[0]['date_start'], res[0]['date_stop']))
  184. else:
  185. return 'report'
  186. else:
  187. raise osv.except_osv(_('UserError'), 'No existe periodo fiscal')
  188. def period_span(self, cr, uid, ids, fy_id, context=None):
  189. if context is None:
  190. context = {}
  191. ap_obj = self.pool.get('account.period')
  192. fy_id = fy_id and type(fy_id) in (list, tuple) and fy_id[0] or fy_id
  193. if not ids:
  194. #~ No hay periodos
  195. return ap_obj.search(cr, uid, [('fiscalyear_id', '=', fy_id), ('special', '=', False)], order='date_start asc')
  196. ap_brws = ap_obj.browse(cr, uid, ids, context=context)
  197. date_start = min([period.date_start for period in ap_brws])
  198. date_stop = max([period.date_stop for period in ap_brws])
  199. return ap_obj.search(cr, uid, [('fiscalyear_id', '=', fy_id), ('special', '=', False), ('date_start', '>=', date_start), ('date_stop', '<=', date_stop)], order='date_start asc')
  200. def print_report(self, cr, uid, ids, data, context=None):
  201. if context is None:
  202. context = {}
  203. data = {}
  204. data['ids'] = context.get('active_ids', [])
  205. data['model'] = context.get('active_model', 'ir.ui.menu')
  206. data['form'] = self.read(cr, uid, ids[0])
  207. if data['form']['filter'] == 'byperiod':
  208. del data['form']['date_from']
  209. del data['form']['date_to']
  210. data['form']['periods'] = self.period_span(cr, uid, data[
  211. 'form']['periods'], data['form']['fiscalyear'])
  212. elif data['form']['filter'] == 'bydate':
  213. self._check_date(cr, uid, data)
  214. del data['form']['periods']
  215. elif data['form']['filter'] == 'none':
  216. del data['form']['date_from']
  217. del data['form']['date_to']
  218. del data['form']['periods']
  219. else:
  220. self._check_date(cr, uid, data)
  221. lis2 = str(data['form']['periods']).replace(
  222. "[", "(").replace("]", ")")
  223. sqlmm = """select min(p.date_start) as inicio, max(p.date_stop) as fin
  224. from account_period p
  225. where p.id in %s""" % lis2
  226. cr.execute(sqlmm)
  227. minmax = cr.dictfetchall()
  228. if minmax:
  229. if (data['form']['date_to'] < minmax[0]['inicio']) or (data['form']['date_from'] > minmax[0]['fin']):
  230. raise osv.except_osv(_('Error !'), _(
  231. 'La interseccion entre el periodo y fecha es vacio'))
  232. if data['form']['columns'] == 'one':
  233. name = 'afr.1cols'
  234. if data['form']['columns'] == 'two':
  235. name = 'afr.2cols'
  236. if data['form']['columns'] == 'four':
  237. if data['form']['analytic_ledger'] and data['form']['inf_type'] == 'BS':
  238. name = 'afr.analytic.ledger'
  239. else:
  240. name = 'afr.4cols'
  241. if data['form']['columns'] == 'five':
  242. name = 'afr.5cols'
  243. if data['form']['columns'] == 'qtr':
  244. name = 'afr.qtrcols'
  245. if data['form']['columns'] == 'thirteen':
  246. name = 'afr.13cols'
  247. return {'type': 'ir.actions.report.xml', 'report_name': name, 'datas': data}
  248. wizard_report()