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.

217 lines
7.1 KiB

8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Author: Damien Crier
  3. # Copyright 2016 Camptocamp SA
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from openerp import models, fields, api, _
  6. class LedgerReportWizard(models.TransientModel):
  7. """Base ledger report wizard."""
  8. _name = "ledger.report.wizard"
  9. _description = "Ledger Report Wizard"
  10. company_id = fields.Many2one(comodel_name='res.company')
  11. date_range_id = fields.Many2one(comodel_name='date.range', required=True)
  12. date_from = fields.Date(required=True)
  13. date_to = fields.Date(required=True)
  14. fy_start_date = fields.Date(required=True)
  15. target_move = fields.Selection([('posted', 'All Posted Entries'),
  16. ('all', 'All Entries')],
  17. string='Target Moves',
  18. required=True,
  19. default='posted')
  20. account_ids = fields.Many2many(
  21. comodel_name='account.account',
  22. string='Filter accounts',
  23. )
  24. amount_currency = fields.Boolean(string='With currency',
  25. default=False)
  26. centralize = fields.Boolean(string='Activate centralization',
  27. default=False)
  28. result_selection = fields.Selection(
  29. [
  30. ('customer', 'Receivable Accounts'),
  31. ('supplier', 'Payable Accounts'),
  32. ('customer_supplier', 'Receivable and Payable Accounts'),
  33. ],
  34. string="Partner's",
  35. default='customer')
  36. partner_ids = fields.Many2many(
  37. comodel_name='res.partner',
  38. string='Filter partners',
  39. )
  40. line_ids = fields.One2many(comodel_name='ledger.report.wizard.line',
  41. inverse_name='wizard_id')
  42. def _query(self):
  43. """Execute query.
  44. Short summary:
  45. Prepare all lines for report
  46. by calculating debit/credit amounts
  47. plus the cumulative one.
  48. Narrow the search by using PG windows.
  49. Insert all the rows in `ledger_report_wizard_line`
  50. at once, so that we have real model objects
  51. and we can filter/group them in the tree view.
  52. """
  53. query = """
  54. WITH view_q as (SELECT
  55. ml.date,
  56. acc.id AS account_id,
  57. ml.debit,
  58. ml.credit,
  59. ml.name as name,
  60. ml.ref,
  61. ml.journal_id,
  62. ml.partner_id,
  63. SUM(debit) OVER w_account - debit AS init_debit,
  64. SUM(credit) OVER w_account - credit AS init_credit,
  65. SUM(debit - credit) OVER w_account - (debit - credit)
  66. AS init_balance,
  67. SUM(debit - credit) OVER w_account AS cumul_balance
  68. FROM
  69. account_account AS acc
  70. LEFT JOIN account_move_line AS ml ON (ml.account_id = acc.id)
  71. --INNER JOIN res_partner AS part ON (ml.partner_id = part.id)
  72. INNER JOIN account_move AS m ON (ml.move_id = m.id)
  73. WINDOW w_account AS (PARTITION BY acc.code ORDER BY ml.date, ml.id)
  74. ORDER BY acc.id, ml.date)
  75. INSERT INTO ledger_report_wizard_line (
  76. date,
  77. name,
  78. journal_id,
  79. account_id,
  80. partner_id,
  81. ref,
  82. label,
  83. --counterpart
  84. debit,
  85. credit,
  86. cumul_balance,
  87. wizard_id
  88. )
  89. SELECT
  90. date,
  91. name,
  92. journal_id,
  93. account_id,
  94. partner_id,
  95. ref,
  96. ' TODO label ' as label,
  97. --counterpart
  98. debit,
  99. credit,
  100. cumul_balance,
  101. %(wizard_id)s as wizard_id
  102. FROM view_q
  103. WHERE date BETWEEN %(date_from)s AND %(date_to)s
  104. """
  105. params = dict(fy_date=self.fy_start_date, wizard_id=self.id,
  106. date_from=self.date_from, date_to=self.date_to)
  107. self.env.cr.execute(query, params)
  108. return True
  109. @api.multi
  110. def _print_report(self, data):
  111. # we update form with display account value
  112. data = self.pre_print_report(data)
  113. Report = self.env['report'].with_context(landscape=True)
  114. return Report.get_action(
  115. self, 'account.report_generalledger_qweb',
  116. data=data)
  117. def _build_contexts(self, data):
  118. result = {}
  119. result['journal_ids'] = (
  120. 'journal_ids' in data['form'] and
  121. data['form']['journal_ids'] or False
  122. )
  123. result['state'] = (
  124. 'target_move' in data['form'] and
  125. data['form']['target_move'] or ''
  126. )
  127. result['date_from'] = data['form']['date_from'] or False
  128. result['date_to'] = data['form']['date_to'] or False
  129. result['strict_range'] = True if result['date_from'] else False
  130. return result
  131. @api.multi
  132. def button_view(self):
  133. """Open tree view w/ results."""
  134. return self.process()
  135. @api.multi
  136. def process(self):
  137. """Process data and return window action."""
  138. self._query()
  139. return {
  140. 'domain': [('wizard_id', '=', self.id)],
  141. 'name': _('Ledger lines'),
  142. 'view_type': 'form',
  143. 'view_mode': 'tree',
  144. 'res_model': 'ledger.report.wizard.line',
  145. 'view_id': False,
  146. 'context': {
  147. 'search_default_group_by_account_id': True,
  148. 'search_default_group_by_date': True,
  149. },
  150. 'type': 'ir.actions.act_window'
  151. }
  152. @api.onchange('date_range_id')
  153. def onchange_date_range_id(self):
  154. """Handle date range change."""
  155. self.date_from = self.date_range_id.date_start
  156. self.date_to = self.date_range_id.date_end
  157. if self.date_from:
  158. self.fy_start_date = self.date_range_id.find_daterange_fy_start(
  159. fields.Date.from_string(self.date_range_id.date_start))
  160. class LedgerReportWizardLine(models.TransientModel):
  161. """A wizard line.
  162. Lines are populated on the fly when submitting the wizard.
  163. """
  164. _name = 'ledger.report.wizard.line'
  165. wizard_id = fields.Many2one(comodel_name='ledger.report.wizard')
  166. name = fields.Char()
  167. label = fields.Char()
  168. ref = fields.Char()
  169. date = fields.Date()
  170. month = fields.Char()
  171. partner_name = fields.Char()
  172. partner_ref = fields.Char()
  173. account_id = fields.Many2one('account.account')
  174. account_code = fields.Char()
  175. journal_id = fields.Many2one('account.journal')
  176. partner_id = fields.Many2one('res.partner')
  177. init_credit = fields.Float()
  178. init_debit = fields.Float()
  179. debit = fields.Float()
  180. credit = fields.Float()
  181. balance = fields.Float()
  182. cumul_credit = fields.Float()
  183. cumul_debit = fields.Float()
  184. cumul_balance = fields.Float()
  185. init_credit = fields.Float()
  186. init_debit = fields.Float()
  187. init_balance = fields.Float()
  188. move_name = fields.Char()
  189. move_state = fields.Char()
  190. invoice_number = fields.Char()
  191. centralized = fields.Boolean()