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.

208 lines
7.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Yannick Vaucher (Camptocamp)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. from openerp import tools
  6. class FinancialReportLine(models.Model):
  7. _inherit = 'financial.report.line'
  8. _name = 'general.ledger.line'
  9. _description = "General Ledger report"
  10. _auto = False
  11. _order = 'account_id, date'
  12. @api.depends('invoice_number', 'name')
  13. def _get_label(self):
  14. for rec in self:
  15. label = rec.name
  16. if rec.invoice_number:
  17. label += u' ({})'.format(rec.invoice_number)
  18. rec.label = label
  19. label = fields.Char(compute='_get_label', readonly=True, store=False)
  20. def init(self, cr):
  21. report_name = self._name.replace('.', '_')
  22. tools.drop_view_if_exists(cr, report_name)
  23. query = """
  24. CREATE OR REPLACE VIEW %(report_name)s AS (
  25. SELECT
  26. acc.id AS account_id,
  27. acc.code AS account_code,
  28. acc.centralized,
  29. ml.id,
  30. ml.name,
  31. ml.ref,
  32. ml.date,
  33. date_part('year', ml.date) || '-' || date_part('month', ml.date)
  34. AS month,
  35. part.ref AS partner_ref,
  36. part.name AS partner_name,
  37. ml.journal_id,
  38. ml.currency_id,
  39. cur.name AS currency_code,
  40. ml.debit,
  41. ml.credit,
  42. ml.debit - ml.credit AS balance,
  43. ml.amount_currency,
  44. SUM(amount_currency) OVER w_account AS balance_curr,
  45. SUM(debit) OVER w_account AS cumul_debit,
  46. SUM(credit) OVER w_account AS cumul_credit,
  47. SUM(debit - credit) OVER w_account AS cumul_balance,
  48. SUM(amount_currency) OVER w_account AS cumul_balance_curr,
  49. SUM(debit) OVER w_account - debit AS init_debit,
  50. SUM(credit) OVER w_account - credit AS init_credit,
  51. SUM(debit - credit) OVER w_account - (debit - credit) AS init_balance,
  52. SUM(amount_currency) OVER w_account - (amount_currency)
  53. AS init_balance_curr,
  54. SUM(debit) OVER w_account_centralized AS debit_centralized,
  55. SUM(credit) OVER w_account_centralized AS credit_centralized,
  56. SUM(debit - credit) OVER w_account_centralized AS balance_centralized,
  57. SUM(amount_currency) OVER w_account_centralized
  58. AS balance_curr_centralized,
  59. SUM(debit) OVER w_account - SUM(debit)
  60. OVER w_account_centralized AS init_debit_centralized,
  61. SUM(credit) OVER w_account - SUM(credit)
  62. OVER w_account_centralized AS init_credit_centralized,
  63. SUM(debit - credit) OVER w_account - SUM(debit - credit)
  64. OVER w_account_centralized AS init_balance_centralized,
  65. SUM(amount_currency) OVER w_account - SUM(amount_currency)
  66. OVER w_account_centralized AS init_balance_curr_centralized,
  67. m.name AS move_name,
  68. m.state AS move_state,
  69. i.number AS invoice_number
  70. FROM
  71. account_account AS acc
  72. LEFT JOIN account_move_line AS ml ON (ml.account_id = acc.id)
  73. INNER JOIN res_partner AS part ON (ml.partner_id = part.id)
  74. INNER JOIN account_move AS m ON (ml.move_id = m.id)
  75. LEFT JOIN account_invoice AS i ON (m.id = i.move_id)
  76. LEFT JOIN res_currency AS cur ON (ml.currency_id = cur.id)
  77. WINDOW w_account AS (PARTITION BY acc.code ORDER BY ml.date, ml.id),
  78. w_account_centralized AS (
  79. PARTITION BY acc.code,
  80. date_part('year', ml.date),
  81. date_part('month', ml.date),
  82. ml.journal_id,
  83. ml.partner_id
  84. ORDER BY ml.date, ml.journal_id, ml.id)
  85. )
  86. """ % {'report_name': report_name}
  87. cr.execute(query)
  88. class GeneralLedgerReport(models.TransientModel):
  89. _name = 'report.account.report_generalledger_qweb'
  90. _inherit = 'account.report.common'
  91. @api.multi
  92. def _get_account_ids(self):
  93. res = False
  94. context = self.env.context
  95. if (context.get('active_model') == 'account.account' and
  96. context.get('active_ids')):
  97. res = context['active_ids']
  98. return res
  99. name = fields.Char()
  100. initial_balance = fields.Integer()
  101. account_ids = fields.Many2many(
  102. 'account.account',
  103. string='Filter on accounts',
  104. default=_get_account_ids,
  105. help="Only selected accounts will be printed. Leave empty to "
  106. "print all accounts.")
  107. journal_ids = fields.Many2many(
  108. 'account.journal',
  109. string='Filter on jourvals',
  110. help="Only selected journals will be printed. Leave empty to "
  111. "print all journals.")
  112. balance_mode = fields.Selection(
  113. [('initial_balance', 'Initial balance'),
  114. ('opening_balance', 'Opening balance')]
  115. )
  116. display_account = fields.Char()
  117. display_ledger_lines = fields.Boolean()
  118. display_initial_balance = fields.Boolean()
  119. MAPPING = {
  120. 'date_from': 'start_date',
  121. 'date_to': 'end_date',
  122. }
  123. @api.model
  124. def _get_values_from_wizard(self, data):
  125. """ Get values from wizard """
  126. values = {}
  127. for key, val in data.iteritems():
  128. if key in self.MAPPING:
  129. values[self.MAPPING[key]] = val
  130. elif key == 'fiscalyear':
  131. if val:
  132. values[key] = val[0]
  133. elif key == 'journal_ids':
  134. if val:
  135. values[key] = [(6, 0, val)]
  136. else:
  137. values[key] = val
  138. return values
  139. @api.multi
  140. def _get_centralized_move_ids(self, domain):
  141. """ Get last line of each selected centralized accounts """
  142. # inverse search on centralized boolean to finish the search to get the
  143. # ids of last lines of centralized accounts
  144. # XXX USE DISTINCT to speed up ?
  145. domain = domain[:]
  146. centralize_index = domain.index(('centralized', '=', False))
  147. domain[centralize_index] = ('centralized', '=', True)
  148. gl_lines = self.env['general.ledger.line'].search(domain)
  149. accounts = gl_lines.mapped('account_id')
  150. line_ids = []
  151. for acc in accounts:
  152. acc_lines = gl_lines.filtered(lambda rec: rec.account_id == acc)
  153. line_ids.append(acc_lines[-1].id)
  154. return line_ids
  155. @api.multi
  156. def _get_moves_from_dates(self):
  157. domain = self._get_moves_from_dates_domain()
  158. if self.centralize:
  159. centralized_ids = self._get_centralized_move_ids(domain)
  160. if centralized_ids:
  161. domain.insert(0, '|')
  162. domain.append(('id', 'in', centralized_ids))
  163. return self.env['general.ledger.line'].search(domain)
  164. @api.multi
  165. def render_html(self, data=None):
  166. report_name = 'account.report_generalledger_qweb'
  167. if data is None:
  168. return
  169. values = self._get_values_from_wizard(data['form'])
  170. report = self.create(values)
  171. report_lines = report._get_moves_from_dates()
  172. # TODO warning if no report_lines
  173. self.env['report']._get_report_from_name(report_name)
  174. docargs = {
  175. 'doc_ids': report.ids,
  176. 'doc_model': self._name,
  177. 'report_lines': report_lines,
  178. 'docs': report,
  179. # XXX
  180. 'has_currency': True
  181. }
  182. return self.env['report'].render(report_name, docargs)