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.

212 lines
10 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 PartnersOpenInvoicesWebkit(report_sxw.rml_parse, CommonPartnersReportHeaderWebkit):
  30. def __init__(self, cursor, uid, name, context):
  31. super(PartnersOpenInvoicesWebkit, 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((_('OPEN INVOICES REPORT'), 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':_('Open Invoices Report'),
  41. 'display_account_raw': self._get_display_account_raw,
  42. 'filter_form': self._get_filter,
  43. 'target_move': self._get_target_move,
  44. 'amount_currency': self._get_amount_currency,
  45. 'display_partner_account': self._get_display_partner_account,
  46. 'display_target_move': self._get_display_target_move,
  47. 'additional_args': [
  48. ('--header-font-name', 'Helvetica'),
  49. ('--footer-font-name', 'Helvetica'),
  50. ('--header-font-size', '10'),
  51. ('--footer-font-size', '6'),
  52. ('--header-left', header_report_name),
  53. ('--header-spacing', '2'),
  54. ('--footer-left', footer_date_time),
  55. ('--footer-right', ' '.join((_('Page'), '[page]', _('of'), '[topage]'))),
  56. ('--footer-line',),
  57. ],
  58. })
  59. def set_context(self, objects, data, ids, report_type=None):
  60. """Populate a ledger_lines attribute on each browse record that will be used
  61. by mako template"""
  62. new_ids = data['form']['chart_account_id']
  63. # Account initial balance memoizer
  64. init_balance_memoizer = {}
  65. # Reading form
  66. main_filter = self._get_form_param('filter', data, default='filter_no')
  67. target_move = self._get_form_param('target_move', data, default='all')
  68. start_date = self._get_form_param('date_from', data)
  69. stop_date = self._get_form_param('date_to', data)
  70. start_period = self.get_start_period_br(data)
  71. stop_period = self.get_end_period_br(data)
  72. fiscalyear = self.get_fiscalyear_br(data)
  73. partner_ids = self._get_form_param('partner_ids', data)
  74. result_selection = self._get_form_param('result_selection', data)
  75. date_until = self._get_form_param('until_date', data)
  76. chart_account = self._get_chart_account_id_br(data)
  77. if main_filter == 'filter_no' and fiscalyear:
  78. start_period = self.get_first_fiscalyear_period(fiscalyear)
  79. stop_period = self.get_last_fiscalyear_period(fiscalyear)
  80. # Retrieving accounts
  81. filter_type = ('payable', 'receivable')
  82. if result_selection == 'customer':
  83. filter_type = ('receivable',)
  84. if result_selection == 'supplier':
  85. filter_type = ('payable',)
  86. account_ids = self.get_all_accounts(new_ids, exclude_type=['view'], only_type=filter_type)
  87. if not account_ids:
  88. raise osv.except_osv(_('Error'), _('No accounts to print.'))
  89. # computation of ledeger lines
  90. if main_filter == 'filter_date':
  91. start = start_date
  92. stop = stop_date
  93. else:
  94. start = start_period
  95. stop = stop_period
  96. ledger_lines_memoizer = self._compute_open_transactions_lines(account_ids,
  97. main_filter,
  98. target_move,
  99. start,
  100. stop,
  101. date_until,
  102. partner_filter=partner_ids)
  103. objects = []
  104. for account in self.pool.get('account.account').browse(self.cursor, self.uid, account_ids):
  105. account.ledger_lines = ledger_lines_memoizer.get(account.id, {})
  106. account.init_balance = init_balance_memoizer.get(account.id, {})
  107. ## we have to compute partner order based on inital balance
  108. ## and ledger line as we may have partner with init bal
  109. ## that are not in ledger line and vice versa
  110. ledg_lines_pids = ledger_lines_memoizer.get(account.id, {}).keys()
  111. non_null_init_balances = dict([(ib, amounts) for ib, amounts in account.init_balance.iteritems()
  112. if amounts['init_balance'] or amounts['init_balance_currency']])
  113. init_bal_lines_pids = non_null_init_balances.keys()
  114. account.partners_order = self._order_partners(ledg_lines_pids, init_bal_lines_pids)
  115. account.ledger_lines = ledger_lines_memoizer.get(account.id, {})
  116. objects.append(account)
  117. self.localcontext.update({
  118. 'fiscalyear': fiscalyear,
  119. 'start_date': start_date,
  120. 'stop_date': stop_date,
  121. 'start_period': start_period,
  122. 'stop_period': stop_period,
  123. 'date_until': date_until,
  124. 'partner_ids': partner_ids,
  125. 'chart_account': chart_account,
  126. })
  127. return super(PartnersOpenInvoicesWebkit, self).set_context(objects, data, new_ids,
  128. report_type=report_type)
  129. def _compute_open_transactions_lines(self, accounts_ids, main_filter, target_move, start, stop, date_until=False, partner_filter=False):
  130. res = defaultdict(dict)
  131. ## we check if until date and date stop have the same value
  132. if main_filter in ('filter_period', 'filter_no'):
  133. date_stop = stop.date_stop
  134. date_until_match = (date_stop == date_until)
  135. elif main_filter == 'filter_date':
  136. date_stop = stop
  137. date_until_match = (stop == date_until)
  138. else:
  139. raise osv.except_osv(_('Unsuported filter'),
  140. _('Filter has to be in filter date, period, or none'))
  141. initial_move_lines_per_account = {}
  142. if main_filter in ('filter_period', 'filter_no'):
  143. initial_move_lines_per_account = self._tree_move_line_ids(
  144. self._partners_initial_balance_line_ids(accounts_ids,
  145. start,
  146. partner_filter,
  147. exclude_reconcile=True,
  148. force_period_ids=False,
  149. date_stop=date_stop), key='id')
  150. for account_id in accounts_ids:
  151. initial_move_lines_ids_per_partner = initial_move_lines_per_account.get(account_id, {})
  152. # We get the move line ids of the account depending of the
  153. # way the initial balance was created we include or not opening entries
  154. move_line_ids_per_partner = self.get_partners_move_lines_ids(account_id,
  155. main_filter,
  156. start,
  157. stop,
  158. target_move,
  159. exclude_reconcile=True,
  160. partner_filter=partner_filter)
  161. if not initial_move_lines_ids_per_partner and not move_line_ids_per_partner:
  162. continue
  163. for partner_id in list(set(initial_move_lines_ids_per_partner.keys() + move_line_ids_per_partner.keys())):
  164. partner_line_ids = (move_line_ids_per_partner.get(partner_id, []) +
  165. initial_move_lines_ids_per_partner.get(partner_id, []))
  166. clearance_line_ids = []
  167. if date_until and not date_until_match and partner_line_ids:
  168. clearance_line_ids = self._get_clearance_move_line_ids(partner_line_ids, date_stop, date_until)
  169. partner_line_ids += clearance_line_ids
  170. lines = self._get_move_line_datas(list(set(partner_line_ids)))
  171. for line in lines:
  172. if line['id'] in initial_move_lines_ids_per_partner.get(partner_id, []):
  173. line['is_from_previous_periods'] = True
  174. if line['id'] in clearance_line_ids:
  175. line['is_clearance_line'] = True
  176. res[account_id][partner_id] = lines
  177. return res
  178. HeaderFooterTextWebKitParser('report.account.account_report_open_invoices_webkit',
  179. 'account.account',
  180. 'addons/account_financial_report_webkit/report/templates/account_report_open_invoices.mako',
  181. parser=PartnersOpenInvoicesWebkit)