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.

373 lines
16 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 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 openerp.osv import osv, fields
  29. import time
  30. from openerp.tools.translate import _
  31. class wizard_report(osv.osv_memory):
  32. _name = "wizard.report"
  33. _columns = {
  34. 'afr_id': fields.many2one(
  35. 'afr', 'Custom Report',
  36. help='If you have already set a Custom Report, Select it Here.'),
  37. 'company_id': fields.many2one('res.company', 'Company', required=True),
  38. 'currency_id': fields.many2one(
  39. 'res.currency', 'Currency',
  40. help="Currency at which this report will be expressed. If not \
  41. selected will be used the one set in the company"),
  42. 'inf_type': fields.selection([('BS', 'Balance Sheet'),
  43. ('IS', 'Income Statement')],
  44. 'Type',
  45. required=True),
  46. 'columns': fields.selection(
  47. [('one', 'End. Balance'),
  48. ('two', 'Debit | Credit'),
  49. ('four', 'Initial | Debit | Credit | YTD'),
  50. ('five', 'Initial | Debit | Credit | Period | YTD'),
  51. ('qtr', "4 QTR's | YTD"), ('thirteen', '12 Months | YTD')],
  52. 'Columns', required=True),
  53. 'display_account': fields.selection(
  54. [('all', 'All Accounts'),
  55. ('bal', 'With Balance'),
  56. ('mov', 'With movements'),
  57. ('bal_mov', 'With Balance / Movements')],
  58. 'Display accounts'),
  59. 'display_account_level': fields.integer(
  60. 'Up to level',
  61. help='Display accounts up to this level (0 to show all)'),
  62. 'account_list': fields.many2many('account.account',
  63. 'rel_wizard_account',
  64. 'account_list',
  65. 'account_id',
  66. 'Root accounts',
  67. required=True),
  68. 'fiscalyear': fields.many2one('account.fiscalyear', 'Fiscal year',
  69. help='Fiscal Year for this report',
  70. required=True),
  71. 'periods': fields.many2many(
  72. 'account.period', 'rel_wizard_period',
  73. 'wizard_id', 'period_id', 'Periods',
  74. help='All periods in the fiscal year if empty'),
  75. 'analytic_ledger': fields.boolean(
  76. 'Analytic Ledger',
  77. help="Allows to Generate an Analytic Ledger for accounts with \
  78. moves. Available when Balance Sheet and 'Initial | Debit | Credit \
  79. | YTD' are selected"),
  80. 'journal_ledger': fields.boolean(
  81. 'Journal Ledger',
  82. help="Allows to Generate an Journal Ledger for accounts with \
  83. moves. Available when Balance Sheet and 'Initial | Debit | Credit \
  84. | YTD' are selected"),
  85. 'partner_balance': fields.boolean(
  86. 'Partner Balance',
  87. help="Allows to Generate a Partner Balance for accounts with \
  88. moves. Available when Balance Sheet and 'Initial | Debit | Credit \
  89. | YTD' are selected"),
  90. 'tot_check': fields.boolean('Summarize?',
  91. help='Checking will add a new line at the \
  92. end of the Report which will Summarize \
  93. Columns in Report'),
  94. 'lab_str': fields.char('Description',
  95. help='Description for the Summary', size=128),
  96. 'target_move': fields.selection(
  97. [('posted', 'All Posted Entries'),
  98. ('all', 'All Entries'),
  99. ], 'Entries to Include',
  100. required=True,
  101. help='Print All Accounting Entries or just Posted Accounting \
  102. Entries'),
  103. # ~ Deprecated fields
  104. 'filter': fields.selection([('bydate', 'By Date'),
  105. ('byperiod', 'By Period'),
  106. ('all', 'By Date and Period'),
  107. ('none', 'No Filter')],
  108. 'Date/Period Filter'),
  109. 'date_to': fields.date('End date'),
  110. 'date_from': fields.date('Start date'),
  111. }
  112. _defaults = {
  113. 'date_from': lambda *a: time.strftime('%Y-%m-%d'),
  114. 'date_to': lambda *a: time.strftime('%Y-%m-%d'),
  115. 'filter': lambda *a: 'byperiod',
  116. 'display_account_level': lambda *a: 0,
  117. 'inf_type': lambda *a: 'BS',
  118. 'company_id': lambda self, cr, uid, c: self.pool['res.company'].
  119. _company_default_get(cr, uid, 'account.invoice', context=c),
  120. 'fiscalyear': lambda self, cr, uid, c: self.
  121. pool['account.fiscalyear'].find(cr, uid),
  122. 'display_account': lambda *a: 'bal_mov',
  123. 'columns': lambda *a: 'five',
  124. 'target_move': 'posted',
  125. }
  126. def onchange_inf_type(self, cr, uid, ids, inf_type, context=None):
  127. if context is None:
  128. context = {}
  129. res = {'value': {}}
  130. if inf_type != 'BS':
  131. res['value'].update({'analytic_ledger': False})
  132. return res
  133. def onchange_columns(self, cr, uid, ids, columns, fiscalyear, periods,
  134. context=None):
  135. if context is None:
  136. context = {}
  137. res = {'value': {}}
  138. p_obj = self.pool.get("account.period")
  139. all_periods = p_obj.search(cr, uid,
  140. [('fiscalyear_id', '=', fiscalyear),
  141. ('special', '=', False)], context=context)
  142. s = set(periods[0][2])
  143. t = set(all_periods)
  144. go = periods[0][2] and s.issubset(t) or False
  145. if columns != 'four':
  146. res['value'].update({'analytic_ledger': False})
  147. if columns in ('qtr', 'thirteen'):
  148. res['value'].update({'periods': all_periods})
  149. else:
  150. if go:
  151. res['value'].update({'periods': periods})
  152. else:
  153. res['value'].update({'periods': []})
  154. return res
  155. def onchange_analytic_ledger(self, cr, uid, ids, company_id,
  156. analytic_ledger, context=None):
  157. if context is None:
  158. context = {}
  159. context['company_id'] = company_id
  160. res = {'value': {}}
  161. cur_id = self.pool.get('res.company').browse(
  162. cr, uid, company_id, context=context).currency_id.id
  163. res['value'].update({'currency_id': cur_id})
  164. return res
  165. def onchange_company_id(self, cr, uid, ids, company_id, context=None):
  166. if context is None:
  167. context = {}
  168. context['company_id'] = company_id
  169. res = {'value': {}}
  170. if not company_id:
  171. return res
  172. cur_id = self.pool.get('res.company').browse(
  173. cr, uid, company_id, context=context).currency_id.id
  174. fy_id = self.pool.get('account.fiscalyear').find(
  175. cr, uid, context=context)
  176. res['value'].update({'fiscalyear': fy_id})
  177. res['value'].update({'currency_id': cur_id})
  178. res['value'].update({'account_list': []})
  179. res['value'].update({'periods': []})
  180. res['value'].update({'afr_id': None})
  181. return res
  182. def onchange_afr_id(self, cr, uid, ids, afr_id, context=None):
  183. if context is None:
  184. context = {}
  185. res = {'value': {}}
  186. if not afr_id:
  187. return res
  188. afr_brw = self.pool.get('afr').browse(cr, uid, afr_id, context=context)
  189. res['value'].update({
  190. 'currency_id': afr_brw.currency_id
  191. and afr_brw.currency_id.id
  192. or afr_brw.company_id.currency_id.id})
  193. res['value'].update({'inf_type': afr_brw.inf_type or 'BS'})
  194. res['value'].update({'columns': afr_brw.columns or 'five'})
  195. res['value'].update({
  196. 'display_account': afr_brw.display_account
  197. or 'bal_mov'})
  198. res['value'].update({
  199. 'display_account_level': afr_brw.
  200. display_account_level or 0})
  201. res['value'].update({
  202. 'fiscalyear': afr_brw.fiscalyear_id
  203. and afr_brw.fiscalyear_id.id})
  204. res['value'].update({'account_list': [
  205. acc.id for acc in afr_brw.account_ids]})
  206. res['value'].update({'periods': [p.id for p in afr_brw.period_ids]})
  207. res['value'].update({
  208. 'analytic_ledger':
  209. afr_brw.analytic_ledger or False})
  210. res['value'].update({'tot_check': afr_brw.tot_check or False})
  211. res['value'].update({'lab_str': afr_brw.lab_str or _(
  212. 'Write a Description for your Summary Total')})
  213. return res
  214. def _get_defaults(self, cr, uid, data, context=None):
  215. if context is None:
  216. context = {}
  217. user = self.pool['res.users'].browse(cr, uid, uid, context=context)
  218. if user.company_id:
  219. company_id = user.company_id.id
  220. else:
  221. company_id = self.pool['res.company'].search(
  222. cr, uid, [('parent_id', '=', False)])[0]
  223. data['form']['company_id'] = company_id
  224. fiscalyear_obj = self.pool['account.fiscalyear']
  225. data['form']['fiscalyear'] = fiscalyear_obj.find(cr, uid)
  226. data['form']['context'] = context
  227. return data['form']
  228. def _check_state(self, cr, uid, data, context=None):
  229. if context is None:
  230. context = {}
  231. if data['form']['filter'] == 'bydate':
  232. self._check_date(cr, uid, data, context)
  233. return data['form']
  234. def _check_date(self, cr, uid, data, context=None):
  235. if context is None:
  236. context = {}
  237. if data['form']['date_from'] > data['form']['date_to']:
  238. raise osv.except_osv(_('Error !'), (
  239. 'La fecha final debe ser mayor a la inicial'))
  240. sql = """SELECT f.id, f.date_start, f.date_stop
  241. FROM account_fiscalyear f
  242. WHERE '%s' = f.id """ % (data['form']['fiscalyear'])
  243. cr.execute(sql)
  244. res = cr.dictfetchall()
  245. if res:
  246. if (data['form']['date_to'] > res[0]['date_stop']
  247. or data['form']['date_from'] < res[0]['date_start']):
  248. raise osv.except_osv(_('UserError'),
  249. 'Las fechas deben estar entre %s y %s'
  250. % (res[0]['date_start'],
  251. res[0]['date_stop']))
  252. else:
  253. return 'report'
  254. else:
  255. raise osv.except_osv(_('UserError'), 'No existe periodo fiscal')
  256. def period_span(self, cr, uid, ids, fy_id, context=None):
  257. if context is None:
  258. context = {}
  259. ap_obj = self.pool.get('account.period')
  260. fy_id = fy_id and type(fy_id) in (list, tuple) and fy_id[0] or fy_id
  261. if not ids:
  262. # ~ No hay periodos
  263. return ap_obj.search(cr, uid, [('fiscalyear_id', '=', fy_id),
  264. ('special', '=', False)],
  265. order='date_start asc')
  266. ap_brws = ap_obj.browse(cr, uid, ids, context=context)
  267. date_start = min([period.date_start for period in ap_brws])
  268. date_stop = max([period.date_stop for period in ap_brws])
  269. return ap_obj.search(cr, uid, [('fiscalyear_id', '=', fy_id),
  270. ('special', '=', False),
  271. ('date_start', '>=', date_start),
  272. ('date_stop', '<=', date_stop)],
  273. order='date_start asc')
  274. def print_report(self, cr, uid, ids, data, context=None):
  275. if context is None:
  276. context = {}
  277. data = {}
  278. data['ids'] = context.get('active_ids', [])
  279. data['model'] = context.get('active_model', 'ir.ui.menu')
  280. data['form'] = self.read(cr, uid, ids[0])
  281. if data['form']['filter'] == 'byperiod':
  282. del data['form']['date_from']
  283. del data['form']['date_to']
  284. data['form']['periods'] = self.period_span(
  285. cr, uid,
  286. data['form']['periods'],
  287. data['form']['fiscalyear'])
  288. elif data['form']['filter'] == 'bydate':
  289. self._check_date(cr, uid, data)
  290. del data['form']['periods']
  291. elif data['form']['filter'] == 'none':
  292. del data['form']['date_from']
  293. del data['form']['date_to']
  294. del data['form']['periods']
  295. else:
  296. self._check_date(cr, uid, data)
  297. lis2 = str(data['form']['periods']).replace(
  298. "[", "(").replace("]", ")")
  299. sqlmm = """select min(p.date_start) as inicio,
  300. max(p.date_stop) as fin
  301. from account_period p
  302. where p.id in %s""" % lis2
  303. cr.execute(sqlmm)
  304. minmax = cr.dictfetchall()
  305. if minmax:
  306. if (data['form']['date_to'] < minmax[0]['inicio']) \
  307. or (data['form']['date_from'] > minmax[0]['fin']):
  308. raise osv.except_osv(_('Error !'), _(
  309. 'La interseccion entre el periodo y fecha es vacio'))
  310. if data['form']['columns'] == 'one':
  311. name = 'afr.1cols'
  312. if data['form']['columns'] == 'two':
  313. name = 'afr.2cols'
  314. if data['form']['columns'] == 'four':
  315. if data['form']['analytic_ledger'] \
  316. and data['form']['inf_type'] == 'BS':
  317. name = 'afr.analytic.ledger'
  318. elif data['form']['journal_ledger'] \
  319. and data['form']['inf_type'] == 'BS':
  320. name = 'afr.journal.ledger'
  321. elif data['form']['partner_balance'] \
  322. and data['form']['inf_type'] == 'BS':
  323. name = 'afr.partner.balance'
  324. else:
  325. name = 'afr.4cols'
  326. if data['form']['columns'] == 'five':
  327. name = 'afr.5cols'
  328. if data['form']['columns'] == 'qtr':
  329. name = 'afr.qtrcols'
  330. if data['form']['columns'] == 'thirteen':
  331. name = 'afr.13cols'
  332. return {'type': 'ir.actions.report.xml',
  333. 'report_name': name,
  334. 'datas': data}
  335. wizard_report()