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.

186 lines
8.9 KiB

  1. # -*- encoding: utf-8 -*-
  2. from osv import osv,fields
  3. from tools.translate import _
  4. import pooler
  5. import time
  6. class wizard_reporte_comprobacion(osv.osv_memory):
  7. _name = "wizard.reporte.comprobacion"
  8. _columns = {
  9. 'company_id': fields.many2one('res.company', 'Company', required=True),
  10. 'account_list': fields.many2many ('account.account', 'rel_wizard_account', 'account_list', 'account_id', 'Cuentas Contables', required=True),
  11. 'filter': fields.selection([('bydate','By Date'), ('byperiod','By Period'), ('all','By Date and Period'), ('none','No Filter')], 'Date/Period Filter'),
  12. 'fiscalyear': fields.many2one('account.fiscalyear', 'Fiscal year', help='Keep empty to use all open fiscal years to compute the balance', required=True),
  13. 'periods': fields.many2many('account.period', 'rel_wizard_period', 'wizard_id', 'period_id', 'Periods', help='All periods in the fiscal year if empty'),
  14. 'display_account': fields.selection([('all','All'), ('con_balance', 'With balance'),('con_movimiento', 'With movements')], 'Display accounts'),
  15. 'display_account_level': fields.integer('Up to level', help='Display accounts up to this level (0 to show all)'),
  16. 'date_from': fields.date('Start date'),
  17. 'date_to': fields.date('End date'),
  18. 'tot_check': fields.boolean('Show Total'),
  19. 'lab_str': fields.char('Description', size= 128),
  20. 'inf_type': fields.selection([('bml', 'Mayor Analitico')], 'Tipo Informe', required=True),
  21. 'asentado': fields.selection([('posted', 'Todos los asientos asentados'), ('todo', 'Todos los asientos')], 'Movimientos destino', required=True),
  22. 'analytic_list': fields.many2many('account.analytic.account', 'rel_wizard_account_analytic', 'analytic_list', 'account_analytic_id', 'Cuentas Analiticas'),
  23. }
  24. _defaults = {
  25. 'date_from': lambda *a: time.strftime('%Y-%m-%d'),
  26. 'date_to': lambda *a: time.strftime('%Y-%m-%d'),
  27. 'filter': lambda *a:'byperiod',
  28. 'display_account_level': lambda *a: 0,
  29. 'inf_type': lambda *a:'bml',
  30. 'company_id': lambda *a: 1,
  31. 'fiscalyear': lambda self, cr, uid, c: self.pool.get('account.fiscalyear').find(cr, uid),
  32. 'display_account': lambda *a:'con_movimiento',
  33. 'asentado': lambda *a:'todo',
  34. }
  35. def onchange_filter(self,cr,uid,ids,fiscalyear,filters,context=None):
  36. if context is None:
  37. context = {}
  38. res = {}
  39. if filters in ("bydate","all"):
  40. fisy = self.pool.get("account.fiscalyear")
  41. fis_actual = fisy.browse(cr,uid,fiscalyear,context=context)
  42. res = {'value':{'date_from': fis_actual.date_start, 'date_to': fis_actual.date_stop}}
  43. return res
  44. def _get_defaults(self, cr, uid, data, context=None):
  45. if context is None:
  46. context = {}
  47. user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, uid, context=context)
  48. if user.company_id:
  49. company_id = user.company_id.id
  50. else:
  51. company_id = pooler.get_pool(cr.dbname).get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
  52. data['form']['company_id'] = company_id
  53. fiscalyear_obj = pooler.get_pool(cr.dbname).get('account.fiscalyear')
  54. data['form']['fiscalyear'] = fiscalyear_obj.find(cr, uid)
  55. data['form']['context'] = context
  56. return data['form']
  57. def _check_state(self, cr, uid, data, context=None):
  58. if context is None:
  59. context = {}
  60. if data['form']['filter'] == 'bydate':
  61. self._check_date(cr, uid, data, context)
  62. return data['form']
  63. def _check_date(self, cr, uid, data, context=None):
  64. if context is None:
  65. context = {}
  66. if data['form']['date_from'] > data['form']['date_to']:
  67. raise osv.except_osv(_('Error !'),('La fecha final debe ser mayor a la inicial'))
  68. sql = """SELECT f.id, f.date_start, f.date_stop
  69. FROM account_fiscalyear f
  70. WHERE '%s' = f.id """%(data['form']['fiscalyear'])
  71. cr.execute(sql)
  72. res = cr.dictfetchall()
  73. if res:
  74. if (data['form']['date_to'] > res[0]['date_stop'] or data['form']['date_from'] < res[0]['date_start']):
  75. raise osv.except_osv(_('UserError'),'Las fechas deben estar entre %s y %s' % (res[0]['date_start'], res[0]['date_stop']))
  76. else:
  77. return 'report'
  78. else:
  79. raise osv.except_osv(_('UserError'),'No existe periodo fiscal')
  80. def esta_en_lista(self, cuenta_id, lista_cuentas):
  81. for i in lista_cuentas:
  82. if cuenta_id == i:
  83. return True
  84. return False
  85. def limpiar_hijos(self, cr, uid, ids, data):
  86. accounts = []
  87. accounts = data
  88. aa_obj = self.pool.get("account.account")
  89. new_account_list = []
  90. for aa_brw in aa_obj.browse(cr,uid,accounts):
  91. eliminar = False
  92. parent_id_cur = aa_brw.parent_id
  93. while parent_id_cur:
  94. if self.esta_en_lista(parent_id_cur.id, accounts):
  95. parent_id_cur = False
  96. eliminar = True
  97. else:
  98. parent_id_cur = parent_id_cur.parent_id
  99. if not eliminar:
  100. new_account_list.append(aa_brw.id)
  101. return new_account_list
  102. def todos_periodos(self, cr, uid, fiscalyear):
  103. date = self.pool.get('account.fiscalyear').read(cr, uid, fiscalyear, ['date_start','date_stop'])
  104. return date['date_start'],date['date_stop']
  105. def get_cuentas_analiticas(self, cr, uid, lista_analitica):
  106. if lista_analitica != []:
  107. return lista_analitica
  108. else:
  109. return self.pool.get('account.analytic.account').search(cr, uid, [('type','<>','view')])
  110. def print_report(self, cr, uid, ids, data, context=None):
  111. if context is None:
  112. context = {}
  113. data = {}
  114. data['ids'] = context.get('active_ids', [])
  115. data['model'] = context.get('active_model', 'ir.ui.menu')
  116. data['form'] = self.read(cr, uid, ids[0])
  117. if data['form']['filter'] == 'byperiod':
  118. if data['form']['periods'] == []:
  119. raise osv.except_osv(_('Error !'),('Debe agregar al menos un periodo'))
  120. del data['form']['date_from']
  121. del data['form']['date_to']
  122. elif data['form']['filter'] == 'bydate':
  123. self._check_date(cr, uid, data)
  124. del data['form']['periods']
  125. elif data['form']['filter'] == 'none':
  126. del data['form']['periods']
  127. data['form']['date_from'],data['form']['date_to'] = self.todos_periodos(cr,uid,data['form']['fiscalyear'])
  128. else:
  129. if data['form']['periods'] == []:
  130. raise osv.except_osv(_('Error !'),('Debe agregar al menos un periodo'))
  131. self._check_date(cr, uid, data)
  132. lis2 = str(data['form']['periods']).replace("[","(").replace("]",")")
  133. sqlmm = """select min(p.date_start) as inicio, max(p.date_stop) as fin
  134. from account_period p
  135. where p.id in %s"""%lis2
  136. cr.execute(sqlmm)
  137. minmax = cr.dictfetchall()
  138. if minmax:
  139. if (data['form']['date_to'] < minmax[0]['inicio']) or (data['form']['date_from'] > minmax[0]['fin']):
  140. raise osv.except_osv(_('Error !'),('La intersepcion entre el periodo y fecha es vacio'))
  141. if data.has_key('form') and data['form']:
  142. form = data['form']
  143. if form.has_key('account_list') and form['account_list']:
  144. form['account_list'] = self.limpiar_hijos(cr, uid, ids, form['account_list'])
  145. if str(data['form']['asentado']).strip() == 'posted':
  146. data['form']['state'] = 'posted'
  147. del data['form']['asentado']
  148. if data['form']['inf_type'] == 'bml':
  149. data['form']['tot_check'] = False
  150. data['form']['display_account_level'] = 0
  151. data['form']['display_account'] = 'con_movimiento'
  152. return {'type': 'ir.actions.report.xml', 'report_name': 'wizard.reporte.comprobacion.mayor.analitico', 'datas': data}
  153. elif data['form']['inf_type'] == 'bac':
  154. data['form']['tot_check'] = False
  155. data['form']['display_account_level'] = 0
  156. data['form']['display_account'] = 'con_movimiento'
  157. data['form']['analytic_list'] = self.get_cuentas_analiticas(cr, uid,data['form']['analytic_list'])
  158. return {'type': 'ir.actions.report.xml', 'report_name': 'wizard.reporte.comprobacion.analisis.cuentas', 'datas': data}
  159. elif data['form']['inf_type'] == 'bgen':
  160. return {'type': 'ir.actions.report.xml', 'report_name': 'wizard.reporte.comprobacion.un.col', 'datas': data}
  161. else:
  162. return {'type': 'ir.actions.report.xml', 'report_name': 'wizard.reporte.comprobacion.cuatro.col', 'datas': data}
  163. wizard_reporte_comprobacion()