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.

210 lines
9.0 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Nicolas Bessi, Guewen Baconnier
  5. # Copyright Camptocamp SA 2011
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from collections import defaultdict
  22. from datetime import datetime
  23. from openerp import pooler
  24. from openerp.osv import osv
  25. from openerp.report import report_sxw
  26. from openerp.tools.translate import _
  27. from .common_partner_reports import CommonPartnersReportHeaderWebkit
  28. from .webkit_parser_header_fix import HeaderFooterTextWebKitParser
  29. class PartnersLedgerWebkit(report_sxw.rml_parse,
  30. CommonPartnersReportHeaderWebkit):
  31. def __init__(self, cursor, uid, name, context):
  32. super(PartnersLedgerWebkit, self).__init__(
  33. cursor, uid, name, context=context)
  34. self.pool = pooler.get_pool(self.cr.dbname)
  35. self.cursor = self.cr
  36. company = self.pool.get('res.users').browse(
  37. self.cr, uid, uid, context=context).company_id
  38. header_report_name = ' - '.join((_('PARTNER LEDGER'),
  39. company.name,
  40. company.currency_id.name))
  41. footer_date_time = self.formatLang(
  42. str(datetime.today()), date_time=True)
  43. self.localcontext.update({
  44. 'cr': cursor,
  45. 'uid': uid,
  46. 'report_name': _('Partner Ledger'),
  47. 'display_account_raw': self._get_display_account_raw,
  48. 'filter_form': self._get_filter,
  49. 'target_move': self._get_target_move,
  50. 'initial_balance': self._get_initial_balance,
  51. 'amount_currency': self._get_amount_currency,
  52. 'display_partner_account': self._get_display_partner_account,
  53. 'display_target_move': self._get_display_target_move,
  54. 'additional_args': [
  55. ('--header-font-name', 'Helvetica'),
  56. ('--footer-font-name', 'Helvetica'),
  57. ('--header-font-size', '10'),
  58. ('--footer-font-size', '6'),
  59. ('--header-left', header_report_name),
  60. ('--header-spacing', '2'),
  61. ('--footer-left', footer_date_time),
  62. ('--footer-right',
  63. ' '.join((_('Page'), '[page]', _('of'), '[topage]'))),
  64. ('--footer-line',),
  65. ],
  66. })
  67. def _get_initial_balance_mode(self, start_period):
  68. """ Force computing of initial balance for the partner ledger,
  69. because we cannot use the entries generated by
  70. OpenERP in the opening period.
  71. OpenERP allows to reconcile move lines between different partners,
  72. so the generated entries in the opening period are unreliable.
  73. """
  74. return 'initial_balance'
  75. def set_context(self, objects, data, ids, report_type=None):
  76. """Populate a ledger_lines attribute on each browse record that will
  77. be used by mako template"""
  78. new_ids = data['form']['chart_account_id']
  79. # account partner memoizer
  80. # Reading form
  81. main_filter = self._get_form_param('filter', data, default='filter_no')
  82. target_move = self._get_form_param('target_move', data, default='all')
  83. start_date = self._get_form_param('date_from', data)
  84. stop_date = self._get_form_param('date_to', data)
  85. start_period = self.get_start_period_br(data)
  86. stop_period = self.get_end_period_br(data)
  87. fiscalyear = self.get_fiscalyear_br(data)
  88. partner_ids = self._get_form_param('partner_ids', data)
  89. result_selection = self._get_form_param('result_selection', data)
  90. chart_account = self._get_chart_account_id_br(data)
  91. if main_filter == 'filter_no' and fiscalyear:
  92. start_period = self.get_first_fiscalyear_period(fiscalyear)
  93. stop_period = self.get_last_fiscalyear_period(fiscalyear)
  94. # Retrieving accounts
  95. filter_type = ('payable', 'receivable')
  96. if result_selection == 'customer':
  97. filter_type = ('receivable',)
  98. if result_selection == 'supplier':
  99. filter_type = ('payable',)
  100. accounts = self.get_all_accounts(new_ids, exclude_type=['view'],
  101. only_type=filter_type)
  102. if not accounts:
  103. raise osv.except_osv(_('Error'), _('No accounts to print.'))
  104. if main_filter == 'filter_date':
  105. start = start_date
  106. stop = stop_date
  107. else:
  108. start = start_period
  109. stop = stop_period
  110. # when the opening period is included in the selected range of periods
  111. # and the opening period contains move lines, we must not compute the
  112. # initial balance from previous periods but only display the move lines
  113. # of the opening period we identify them as:
  114. # - 'initial_balance' means compute the sums of move lines from
  115. # previous periods
  116. # - 'opening_balance' means display the move lines of the opening
  117. # period
  118. init_balance = main_filter in ('filter_no', 'filter_period')
  119. initial_balance_mode = init_balance and self._get_initial_balance_mode(
  120. start) or False
  121. initial_balance_lines = {}
  122. if initial_balance_mode == 'initial_balance':
  123. initial_balance_lines = self._compute_partners_initial_balances(
  124. accounts, start_period, partner_filter=partner_ids,
  125. exclude_reconcile=False)
  126. ledger_lines = self._compute_partner_ledger_lines(
  127. accounts, main_filter, target_move, start, stop,
  128. partner_filter=partner_ids)
  129. objects = []
  130. for account in self.pool.get('account.account').browse(
  131. self.cursor, self.uid, accounts, context=self.localcontext):
  132. account.ledger_lines = ledger_lines.get(account.id, {})
  133. account.init_balance = initial_balance_lines.get(account.id, {})
  134. # we have to compute partner order based on inital balance
  135. # and ledger line as we may have partner with init bal
  136. # that are not in ledger line and vice versa
  137. ledg_lines_pids = ledger_lines.get(account.id, {}).keys()
  138. if initial_balance_mode:
  139. non_null_init_balances = dict(
  140. [(ib, amounts) for ib, amounts
  141. in account.init_balance.iteritems()
  142. if amounts['init_balance']
  143. or amounts['init_balance_currency']])
  144. init_bal_lines_pids = non_null_init_balances.keys()
  145. else:
  146. account.init_balance = {}
  147. init_bal_lines_pids = []
  148. account.partners_order = self._order_partners(
  149. ledg_lines_pids, init_bal_lines_pids)
  150. objects.append(account)
  151. self.localcontext.update({
  152. 'fiscalyear': fiscalyear,
  153. 'start_date': start_date,
  154. 'stop_date': stop_date,
  155. 'start_period': start_period,
  156. 'stop_period': stop_period,
  157. 'partner_ids': partner_ids,
  158. 'chart_account': chart_account,
  159. 'initial_balance_mode': initial_balance_mode,
  160. })
  161. return super(PartnersLedgerWebkit, self).set_context(
  162. objects, data, new_ids, report_type=report_type)
  163. def _compute_partner_ledger_lines(self, accounts_ids, main_filter,
  164. target_move, start, stop,
  165. partner_filter=False):
  166. res = defaultdict(dict)
  167. for acc_id in accounts_ids:
  168. move_line_ids = self.get_partners_move_lines_ids(
  169. acc_id, main_filter, start, stop, target_move,
  170. exclude_reconcile=False, partner_filter=partner_filter)
  171. if not move_line_ids:
  172. continue
  173. for partner_id in move_line_ids:
  174. partner_line_ids = move_line_ids.get(partner_id, [])
  175. lines = self._get_move_line_datas(list(set(partner_line_ids)))
  176. res[acc_id][partner_id] = lines
  177. return res
  178. HeaderFooterTextWebKitParser(
  179. 'report.account.account_report_partners_ledger_webkit',
  180. 'account.account',
  181. 'addons/account_financial_report_webkit/report/templates/\
  182. account_report_partners_ledger.mako',
  183. parser=PartnersLedgerWebkit)