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.

199 lines
9.4 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. import pooler
  22. from collections import defaultdict
  23. from report import report_sxw
  24. from osv import osv
  25. from tools.translate import _
  26. from datetime import datetime
  27. from common_partner_reports import CommonPartnersReportHeaderWebkit
  28. from webkit_parser_header_fix import HeaderFooterTextWebKitParser
  29. class PartnersLedgerWebkit(report_sxw.rml_parse, CommonPartnersReportHeaderWebkit):
  30. def __init__(self, cursor, uid, name, context):
  31. super(PartnersLedgerWebkit, self).__init__(cursor, uid, name, context=context)
  32. self.pool = pooler.get_pool(self.cr.dbname)
  33. self.cursor = self.cr
  34. company = self.pool.get('res.users').browse(self.cr, uid, uid, context=context).company_id
  35. header_report_name = ' - '.join((_('PARTNER LEDGER'), company.name, company.currency_id.name))
  36. footer_date_time = self.formatLang(str(datetime.today()), date_time=True)
  37. self.localcontext.update({
  38. 'cr': cursor,
  39. 'uid': uid,
  40. 'report_name':_('Partner Ledger'),
  41. 'display_account_raw': self._get_display_account_raw,
  42. 'filter_form': self._get_filter,
  43. 'target_move': self._get_target_move,
  44. 'initial_balance': self._get_initial_balance,
  45. 'amount_currency': self._get_amount_currency,
  46. 'display_partner_account': self._get_display_partner_account,
  47. 'display_target_move': self._get_display_target_move,
  48. 'additional_args': [
  49. ('--header-font-name', 'Helvetica'),
  50. ('--footer-font-name', 'Helvetica'),
  51. ('--header-font-size', '10'),
  52. ('--footer-font-size', '6'),
  53. ('--header-left', header_report_name),
  54. ('--header-spacing', '2'),
  55. ('--footer-left', footer_date_time),
  56. ('--footer-right', ' '.join((_('Page'), '[page]', _('of'), '[topage]'))),
  57. ('--footer-line',),
  58. ],
  59. })
  60. def _get_initial_balance_mode(self, start_period):
  61. """ Force computing of initial balance for the partner ledger,
  62. because we cannot use the entries generated by
  63. OpenERP in the opening period.
  64. OpenERP allows to reconcile move lines between different partners,
  65. so the generated entries in the opening period are unreliable.
  66. """
  67. return 'initial_balance'
  68. def set_context(self, objects, data, ids, report_type=None):
  69. """Populate a ledger_lines attribute on each browse record that will be used
  70. by mako template"""
  71. new_ids = data['form']['chart_account_id']
  72. # account partner memoizer
  73. # Reading form
  74. main_filter = self._get_form_param('filter', data, default='filter_no')
  75. target_move = self._get_form_param('target_move', data, default='all')
  76. start_date = self._get_form_param('date_from', data)
  77. stop_date = self._get_form_param('date_to', data)
  78. start_period = self.get_start_period_br(data)
  79. stop_period = self.get_end_period_br(data)
  80. fiscalyear = self.get_fiscalyear_br(data)
  81. partner_ids = self._get_form_param('partner_ids', data)
  82. result_selection = self._get_form_param('result_selection', data)
  83. chart_account = self._get_chart_account_id_br(data)
  84. if main_filter == 'filter_no' and fiscalyear:
  85. start_period = self.get_first_fiscalyear_period(fiscalyear)
  86. stop_period = self.get_last_fiscalyear_period(fiscalyear)
  87. # Retrieving accounts
  88. filter_type = ('payable', 'receivable')
  89. if result_selection == 'customer':
  90. filter_type = ('receivable',)
  91. if result_selection == 'supplier':
  92. filter_type = ('payable',)
  93. accounts = self.get_all_accounts(new_ids, exclude_type=['view'],
  94. only_type=filter_type)
  95. if not accounts:
  96. raise osv.except_osv(_('Error'), _('No accounts to print.'))
  97. if main_filter == 'filter_date':
  98. start = start_date
  99. stop = stop_date
  100. else:
  101. start = start_period
  102. stop = stop_period
  103. # when the opening period is included in the selected range of periods and
  104. # the opening period contains move lines, we must not compute the initial balance from previous periods
  105. # but only display the move lines of the opening period
  106. # we identify them as :
  107. # - 'initial_balance' means compute the sums of move lines from previous periods
  108. # - 'opening_balance' means display the move lines of the opening period
  109. init_balance = main_filter in ('filter_no', 'filter_period')
  110. initial_balance_mode = init_balance and self._get_initial_balance_mode(start) or False
  111. initial_balance_lines = {}
  112. if initial_balance_mode == 'initial_balance':
  113. initial_balance_lines = self._compute_partners_initial_balances(accounts,
  114. start_period,
  115. partner_filter=partner_ids,
  116. exclude_reconcile=False)
  117. ledger_lines = self._compute_partner_ledger_lines(accounts,
  118. main_filter,
  119. target_move,
  120. start,
  121. stop,
  122. partner_filter=partner_ids)
  123. objects = []
  124. for account in self.pool.get('account.account').browse(self.cursor, self.uid, accounts):
  125. account.ledger_lines = ledger_lines.get(account.id, {})
  126. account.init_balance = initial_balance_lines.get(account.id, {})
  127. ## we have to compute partner order based on inital balance
  128. ## and ledger line as we may have partner with init bal
  129. ## that are not in ledger line and vice versa
  130. ledg_lines_pids = ledger_lines.get(account.id, {}).keys()
  131. if initial_balance_mode:
  132. non_null_init_balances = dict([(ib, amounts) for ib, amounts in account.init_balance.iteritems()
  133. if amounts['init_balance'] or amounts['init_balance_currency']])
  134. init_bal_lines_pids = non_null_init_balances.keys()
  135. else:
  136. account.init_balance = {}
  137. init_bal_lines_pids = []
  138. account.partners_order = self._order_partners(ledg_lines_pids, init_bal_lines_pids)
  139. objects.append(account)
  140. self.localcontext.update({
  141. 'fiscalyear': fiscalyear,
  142. 'start_date': start_date,
  143. 'stop_date': stop_date,
  144. 'start_period': start_period,
  145. 'stop_period': stop_period,
  146. 'partner_ids': partner_ids,
  147. 'chart_account': chart_account,
  148. 'initial_balance_mode': initial_balance_mode,
  149. })
  150. return super(PartnersLedgerWebkit, self).set_context(objects, data, new_ids,
  151. report_type=report_type)
  152. def _compute_partner_ledger_lines(self, accounts_ids, main_filter, target_move, start, stop, partner_filter=False):
  153. res = defaultdict(dict)
  154. for acc_id in accounts_ids:
  155. move_line_ids = self.get_partners_move_lines_ids(acc_id,
  156. main_filter,
  157. start,
  158. stop,
  159. target_move,
  160. exclude_reconcile=False,
  161. partner_filter=partner_filter)
  162. if not move_line_ids:
  163. continue
  164. for partner_id in move_line_ids:
  165. partner_line_ids = move_line_ids.get(partner_id, [])
  166. lines = self._get_move_line_datas(list(set(partner_line_ids)))
  167. res[acc_id][partner_id] = lines
  168. return res
  169. HeaderFooterTextWebKitParser('report.account.account_report_partners_ledger_webkit',
  170. 'account.account',
  171. 'addons/account_financial_report_webkit/report/templates/account_report_partners_ledger.mako',
  172. parser=PartnersLedgerWebkit)