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.

127 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Andrea andrea4ever Gallina
  3. # Author: Francesco OpenCode Apruzzese
  4. # Author: Ciro CiroBoxHub Urselli
  5. # Copyright 2016 Camptocamp SA
  6. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  7. from openerp import models, fields, api, _
  8. from openerp.exceptions import Warning as UserError
  9. from datetime import datetime
  10. class OpenInvoiceWizard(models.TransientModel):
  11. _name = 'open.invoice.wizard'
  12. company_id = fields.Many2one(
  13. 'res.company', required=True,
  14. default=lambda s: s.env.user.company_id)
  15. at_date = fields.Date(
  16. required=True,
  17. default=fields.Date.to_string(datetime.today()))
  18. partner_ids = fields.Many2many(
  19. 'res.partner', string='Filter partners')
  20. amount_currency = fields.Boolean(
  21. "With Currency", help="It adds the currency column")
  22. group_by_currency = fields.Boolean(
  23. "Group Partner by currency", help="It adds the currency column")
  24. result_selection = fields.Selection([
  25. ('customer', 'Receivable Accounts'),
  26. ('supplier', 'Payable Accounts'),
  27. ('customer_supplier', 'Receivable and Payable Accounts')],
  28. "Partner's", required=True, default='customer')
  29. target_move = fields.Selection([
  30. ('posted', 'All Posted Entries'),
  31. ('all', 'All Entries')], 'Target Moves',
  32. required=True, default='all')
  33. until_date = fields.Date(
  34. "Clearance date",
  35. help="""The clearance date is essentially a tool used for debtors
  36. provisionning calculation.
  37. By default, this date is equal to the the end date (
  38. ie: 31/12/2011 if you select fy 2011).
  39. By amending the clearance date, you will be, for instance,
  40. able to answer the question : 'based on my last
  41. year end debtors open invoices, which invoices are still
  42. unpaid today (today is my clearance date)?'""")
  43. @api.onchange('at_date')
  44. def onchange_atdate(self):
  45. self.until_date = self.at_date
  46. @api.onchange('until_date')
  47. def onchange_untildate(self):
  48. # ---- until_date must be always >= of at_date
  49. if self.until_date:
  50. if self.until_date < self.at_date:
  51. raise UserError(
  52. 'Until Date must be equal or greater than At Date')
  53. @staticmethod
  54. def _get_domain(data):
  55. account_type = ('payable', 'receivable')
  56. if data['result_selection'] == 'customer':
  57. account_type = ('receivable', )
  58. elif data['result_selection'] == 'supplier':
  59. account_type = ('payable', )
  60. domain = [
  61. ('reconciled', '=', False),
  62. ('company_id', '=', data['company_id'].id),
  63. ('move_id.date', '<=', data['at_date']),
  64. ('account_id.user_type_id.type', 'in', account_type)
  65. ]
  66. if data['target_move'] != 'all':
  67. domain.append(('move_id.state', 'in', ('posted', )), )
  68. if data['partner_ids']:
  69. domain.append(('partner_id', 'in', [p.id
  70. for p
  71. in data['partner_ids']]), )
  72. return domain
  73. @staticmethod
  74. def _get_move_line_data(move):
  75. label = move.name
  76. if move.invoice_id:
  77. label = '{label} ({inv_nummber})'.format(
  78. label=label, inv_nummber=move.invoice_id.number)
  79. return {
  80. 'date': move.date,
  81. 'period': '',
  82. 'entry': move.move_id.name,
  83. 'journal': move.move_id.journal_id.code,
  84. 'reference': move.ref,
  85. 'label': label,
  86. 'rec': move.full_reconcile_id.name,
  87. 'due_date': move.date_maturity,
  88. 'debit': move.debit,
  89. 'credit': move.credit,
  90. }
  91. @api.multi
  92. def print_report(self):
  93. self.ensure_one()
  94. moves = self.env['account.move.line'].search(self._get_domain(self))
  95. if not moves:
  96. return True # ----- Show a message here
  97. datas = {}
  98. for move in moves:
  99. if move.account_id.name not in datas:
  100. datas[move.account_id.name] = {}
  101. if move.partner_id.name not in datas[move.account_id.name]:
  102. datas[move.account_id.name][move.partner_id.name] = []
  103. datas[move.account_id.name][move.partner_id.name].append(
  104. self._get_move_line_data(move))
  105. generals = {
  106. 'company': self.company_id.name,
  107. 'fiscal_year': '',
  108. 'at_date': self.at_date,
  109. 'account_filters': dict(
  110. self._columns['result_selection'].selection)[
  111. self.result_selection],
  112. 'target_moves': dict(
  113. self._columns['target_move'].selection)[self.target_move],
  114. }
  115. return self.env['report'].with_context(landscape=True).get_action(
  116. self, 'account_financial_report_qweb.open_invoice_report_qweb',
  117. data={'data': datas, 'general': generals})