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.

140 lines
5.9 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 time
  22. from openerp.osv import fields, orm
  23. class AccountReportPartnersLedgerWizard(orm.TransientModel):
  24. """Will launch partner ledger report and pass required args"""
  25. _inherit = "account.common.partner.report"
  26. _name = "partners.ledger.webkit"
  27. _description = "Partner Ledger Report"
  28. _columns = {
  29. 'amount_currency': fields.boolean("With Currency",
  30. help="It adds the currency column"),
  31. 'partner_ids': fields.many2many(
  32. 'res.partner',
  33. string='Filter on partner',
  34. help="Only selected partners will be printed. "
  35. "Leave empty to print all partners."),
  36. 'filter': fields.selection(
  37. [('filter_no', 'No Filters'),
  38. ('filter_date', 'Date'),
  39. ('filter_period', 'Periods')], "Filter by", required=True,
  40. help='Filter by date: no opening balance will be displayed. '
  41. '(opening balance can only be computed based on period to be \
  42. correct).'),
  43. }
  44. _defaults = {
  45. 'amount_currency': False,
  46. 'result_selection': 'customer_supplier',
  47. }
  48. def _check_fiscalyear(self, cr, uid, ids, context=None):
  49. obj = self.read(
  50. cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
  51. if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
  52. return False
  53. return True
  54. _constraints = [
  55. (_check_fiscalyear,
  56. 'When no Fiscal year is selected, you must choose to '
  57. 'filter by periods or by date.',
  58. ['filter']),
  59. ]
  60. def onchange_filter(self, cr, uid, ids, filter='filter_no',
  61. fiscalyear_id=False, context=None):
  62. res = {}
  63. if filter == 'filter_no':
  64. res['value'] = {'period_from': False,
  65. 'period_to': False,
  66. 'date_from': False,
  67. 'date_to': False}
  68. if filter == 'filter_date':
  69. if fiscalyear_id:
  70. fyear = self.pool.get('account.fiscalyear').browse(
  71. cr, uid, fiscalyear_id, context=context)
  72. date_from = fyear.date_start
  73. date_to = fyear.date_stop > time.strftime(
  74. '%Y-%m-%d') and time.strftime('%Y-%m-%d') \
  75. or fyear.date_stop
  76. else:
  77. date_from, date_to = time.strftime(
  78. '%Y-01-01'), time.strftime('%Y-%m-%d')
  79. res['value'] = {'period_from': False, 'period_to':
  80. False, 'date_from': date_from, 'date_to': date_to}
  81. if filter == 'filter_period' and fiscalyear_id:
  82. start_period = end_period = False
  83. cr.execute('''
  84. SELECT * FROM (SELECT p.id
  85. FROM account_period p
  86. LEFT JOIN account_fiscalyear f
  87. ON (p.fiscalyear_id = f.id)
  88. WHERE f.id = %s
  89. AND COALESCE(p.special, FALSE) = FALSE
  90. ORDER BY p.date_start ASC
  91. LIMIT 1) AS period_start
  92. UNION ALL
  93. SELECT * FROM (SELECT p.id
  94. FROM account_period p
  95. LEFT JOIN account_fiscalyear f
  96. ON (p.fiscalyear_id = f.id)
  97. WHERE f.id = %s
  98. AND p.date_start < NOW()
  99. AND COALESCE(p.special, FALSE) = FALSE
  100. ORDER BY p.date_stop DESC
  101. LIMIT 1) AS period_stop''',
  102. (fiscalyear_id, fiscalyear_id))
  103. periods = [i[0] for i in cr.fetchall()]
  104. if periods:
  105. start_period = end_period = periods[0]
  106. if len(periods) > 1:
  107. end_period = periods[1]
  108. res['value'] = {'period_from': start_period, 'period_to':
  109. end_period, 'date_from': False, 'date_to': False}
  110. return res
  111. def pre_print_report(self, cr, uid, ids, data, context=None):
  112. data = super(AccountReportPartnersLedgerWizard, self).pre_print_report(
  113. cr, uid, ids, data, context)
  114. if context is None:
  115. context = {}
  116. # will be used to attach the report on the main account
  117. data['ids'] = [data['form']['chart_account_id']]
  118. vals = self.read(cr, uid, ids,
  119. ['amount_currency', 'partner_ids'],
  120. context=context)[0]
  121. data['form'].update(vals)
  122. return data
  123. def _print_report(self, cursor, uid, ids, data, context=None):
  124. # we update form with display account value
  125. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  126. return {'type': 'ir.actions.report.xml',
  127. 'report_name': 'account.account_report_partners_ledger_webkit',
  128. 'datas': data}