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.

116 lines
5.6 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 osv import fields, osv
  23. class AccountReportPartnersLedgerWizard(osv.osv_memory):
  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('res.partner', string='Filter on partner',
  32. help="Only selected partners will be printed. Leave empty to print all partners."),
  33. 'filter': fields.selection([('filter_no', 'No Filters'),
  34. ('filter_date', 'Date'),
  35. ('filter_period', 'Periods')], "Filter by", required=True, help='Filter by date : no opening balance will be displayed. (opening balance can only be calculated based on period to be correct).'),
  36. }
  37. _defaults = {
  38. 'amount_currency': False,
  39. 'result_selection': 'customer_supplier',
  40. }
  41. def _check_fiscalyear(self, cr, uid, ids, context=None):
  42. obj = self.read(cr, uid, ids[0], ['fiscalyear_id', 'filter'], context=context)
  43. if not obj['fiscalyear_id'] and obj['filter'] == 'filter_no':
  44. return False
  45. return True
  46. _constraints = [
  47. (_check_fiscalyear, 'When no Fiscal year is selected, you must choose to filter by periods or by date.', ['filter']),
  48. ]
  49. def onchange_filter(self, cr, uid, ids, filter='filter_no', fiscalyear_id=False, context=None):
  50. res = {}
  51. if filter == 'filter_no':
  52. res['value'] = {'period_from': False, 'period_to': False, 'date_from': False ,'date_to': False}
  53. if filter == 'filter_date':
  54. if fiscalyear_id:
  55. fyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id, context=context)
  56. date_from = fyear.date_start
  57. date_to = fyear.date_stop > time.strftime('%Y-%m-%d') and time.strftime('%Y-%m-%d') or fyear.date_stop
  58. else:
  59. date_from, date_to = time.strftime('%Y-01-01'), time.strftime('%Y-%m-%d')
  60. res['value'] = {'period_from': False, 'period_to': False, 'date_from': date_from, 'date_to': date_to}
  61. if filter == 'filter_period' and fiscalyear_id:
  62. start_period = end_period = False
  63. cr.execute('''
  64. SELECT * FROM (SELECT p.id
  65. FROM account_period p
  66. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  67. WHERE f.id = %s
  68. AND COALESCE(p.special, FALSE) = FALSE
  69. ORDER BY p.date_start ASC
  70. LIMIT 1) AS period_start
  71. UNION
  72. SELECT * FROM (SELECT p.id
  73. FROM account_period p
  74. LEFT JOIN account_fiscalyear f ON (p.fiscalyear_id = f.id)
  75. WHERE f.id = %s
  76. AND p.date_start < NOW()
  77. AND COALESCE(p.special, FALSE) = FALSE
  78. ORDER BY p.date_stop DESC
  79. LIMIT 1) AS period_stop''', (fiscalyear_id, fiscalyear_id))
  80. periods = [i[0] for i in cr.fetchall()]
  81. if periods:
  82. start_period = end_period = periods[0]
  83. if len(periods) > 1:
  84. end_period = periods[1]
  85. res['value'] = {'period_from': start_period, 'period_to': end_period, 'date_from': False, 'date_to': False}
  86. return res
  87. def pre_print_report(self, cr, uid, ids, data, context=None):
  88. data = super(AccountReportPartnersLedgerWizard, self).pre_print_report(cr, uid, ids, data, context)
  89. if context is None:
  90. context = {}
  91. # will be used to attach the report on the main account
  92. data['ids'] = [data['form']['chart_account_id']]
  93. vals = self.read(cr, uid, ids,
  94. ['amount_currency', 'partner_ids',],
  95. context=context)[0]
  96. data['form'].update(vals)
  97. return data
  98. def _print_report(self, cursor, uid, ids, data, context=None):
  99. context = context or {}
  100. # we update form with display account value
  101. data = self.pre_print_report(cursor, uid, ids, data, context=context)
  102. return {'type': 'ir.actions.report.xml',
  103. 'report_name': 'account.account_report_partners_ledger_webkit',
  104. 'datas': data}
  105. AccountReportPartnersLedgerWizard()