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.

369 lines
16 KiB

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