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.

202 lines
9.9 KiB

  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'),('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. 'tot_check': fields.boolean('Summarize?', help='Checking will add a new line at the end of the Report which will Summarize Columns in Report'),
  46. 'lab_str': fields.char('Description', help='Description for the Summary', size= 128),
  47. #~ Deprecated fields
  48. 'filter': fields.selection([('bydate','By Date'),('byperiod','By Period'),('all','By Date and Period'),('none','No Filter')],'Date/Period Filter'),
  49. 'date_to': fields.date('End date'),
  50. 'date_from': fields.date('Start date'),
  51. }
  52. _defaults = {
  53. 'date_from': lambda *a: time.strftime('%Y-%m-%d'),
  54. 'date_to': lambda *a: time.strftime('%Y-%m-%d'),
  55. 'filter': lambda *a:'byperiod',
  56. 'display_account_level': lambda *a: 0,
  57. 'inf_type': lambda *a:'BS',
  58. 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.invoice', context=c),
  59. 'fiscalyear': lambda self, cr, uid, c: self.pool.get('account.fiscalyear').find(cr, uid),
  60. 'display_account': lambda *a:'bal_mov',
  61. 'columns': lambda *a:'five',
  62. }
  63. def onchange_columns(self,cr,uid,ids,columns,fiscalyear,periods,context=None):
  64. if context is None:
  65. context = {}
  66. res = {'value':{}}
  67. if columns=='thirteen':
  68. p_obj = self.pool.get("account.period")
  69. periods = p_obj.search(cr,uid,[('fiscalyear_id','=',fiscalyear),('special','=',False)],context=context)
  70. res['value'].update({'periods':periods})
  71. else:
  72. res['value'].update({'periods':periods})
  73. return res
  74. def onchange_company_id(self,cr,uid,ids,company_id,context=None):
  75. if context is None:
  76. context = {}
  77. context['company_id']=company_id
  78. res = {'value':{}}
  79. cur_id = self.pool.get('res.company').browse(cr,uid,company_id,context=context).currency_id.id
  80. fy_id = self.pool.get('account.fiscalyear').find(cr, uid,context=context)
  81. res['value'].update({'fiscalyear':fy_id})
  82. res['value'].update({'currency_id':cur_id})
  83. res['value'].update({'account_list':[]})
  84. res['value'].update({'periods':[]})
  85. res['value'].update({'afr_id':None})
  86. return res
  87. def onchange_afr_id(self,cr,uid,ids,afr_id,context=None):
  88. if context is None:
  89. context = {}
  90. res = {'value':{}}
  91. if not afr_id: return res
  92. afr_brw = self.pool.get('afr').browse(cr,uid,afr_id,context=context)
  93. res['value'].update({'currency_id':afr_brw.currency_id and afr_brw.currency_id.id or afr_brw.company_id.currency_id.id})
  94. res['value'].update({'inf_type':afr_brw.inf_type or 'BS'})
  95. res['value'].update({'columns':afr_brw.columns or 'five'})
  96. res['value'].update({'display_account':afr_brw.display_account or 'bal_mov'})
  97. res['value'].update({'display_account_level':afr_brw.display_account_level or 0})
  98. res['value'].update({'fiscalyear':afr_brw.fiscalyear_id and afr_brw.fiscalyear_id.id})
  99. res['value'].update({'account_list':[acc.id for acc in afr_brw.account_ids]})
  100. res['value'].update({'periods':[p.id for p in afr_brw.period_ids]})
  101. return res
  102. def _get_defaults(self, cr, uid, data, context=None):
  103. if context is None:
  104. context = {}
  105. user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, uid, context=context)
  106. if user.company_id:
  107. company_id = user.company_id.id
  108. else:
  109. company_id = pooler.get_pool(cr.dbname).get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]
  110. data['form']['company_id'] = company_id
  111. fiscalyear_obj = pooler.get_pool(cr.dbname).get('account.fiscalyear')
  112. data['form']['fiscalyear'] = fiscalyear_obj.find(cr, uid)
  113. data['form']['context'] = context
  114. return data['form']
  115. def _check_state(self, cr, uid, data, context=None):
  116. if context is None:
  117. context = {}
  118. if data['form']['filter'] == 'bydate':
  119. self._check_date(cr, uid, data, context)
  120. return data['form']
  121. def _check_date(self, cr, uid, data, context=None):
  122. if context is None:
  123. context = {}
  124. if data['form']['date_from'] > data['form']['date_to']:
  125. raise osv.except_osv(_('Error !'),('La fecha final debe ser mayor a la inicial'))
  126. sql = """SELECT f.id, f.date_start, f.date_stop
  127. FROM account_fiscalyear f
  128. WHERE '%s' = f.id """%(data['form']['fiscalyear'])
  129. cr.execute(sql)
  130. res = cr.dictfetchall()
  131. if res:
  132. if (data['form']['date_to'] > res[0]['date_stop'] or data['form']['date_from'] < res[0]['date_start']):
  133. raise osv.except_osv(_('UserError'),'Las fechas deben estar entre %s y %s' % (res[0]['date_start'], res[0]['date_stop']))
  134. else:
  135. return 'report'
  136. else:
  137. raise osv.except_osv(_('UserError'),'No existe periodo fiscal')
  138. def print_report(self, cr, uid, ids,data, context=None):
  139. if context is None:
  140. context = {}
  141. data = {}
  142. data['ids'] = context.get('active_ids', [])
  143. data['model'] = context.get('active_model', 'ir.ui.menu')
  144. data['form'] = self.read(cr, uid, ids[0])
  145. if data['form']['filter'] == 'byperiod':
  146. del data['form']['date_from']
  147. del data['form']['date_to']
  148. elif data['form']['filter'] == 'bydate':
  149. self._check_date(cr, uid, data)
  150. del data['form']['periods']
  151. elif data['form']['filter'] == 'none':
  152. del data['form']['date_from']
  153. del data['form']['date_to']
  154. del data['form']['periods']
  155. else:
  156. self._check_date(cr, uid, data)
  157. lis2 = str(data['form']['periods']).replace("[","(").replace("]",")")
  158. sqlmm = """select min(p.date_start) as inicio, max(p.date_stop) as fin
  159. from account_period p
  160. where p.id in %s"""%lis2
  161. cr.execute(sqlmm)
  162. minmax = cr.dictfetchall()
  163. if minmax:
  164. if (data['form']['date_to'] < minmax[0]['inicio']) or (data['form']['date_from'] > minmax[0]['fin']):
  165. raise osv.except_osv(_('Error !'),_('La interseccion entre el periodo y fecha es vacio'))
  166. if data['form']['columns'] == 'one':
  167. name = 'afr.1cols'
  168. if data['form']['columns'] == 'two':
  169. name = 'afr.2cols'
  170. if data['form']['columns'] == 'four':
  171. name = 'afr.4cols'
  172. if data['form']['columns'] == 'five':
  173. name = 'afr.5cols'
  174. if data['form']['columns'] == 'thirteen':
  175. name = 'afr.13cols'
  176. print 'NOMBRE DEL REPORTE, ', name
  177. return {'type': 'ir.actions.report.xml', 'report_name': name, 'datas': data}
  178. wizard_report()