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.

119 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Tecnativa - Luis M. Ontalba
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from odoo import _, api, fields, models
  5. class AccountStatementLineCreate(models.TransientModel):
  6. _name = 'account.statement.line.create'
  7. _description = 'Wizard to create statement lines'
  8. statement_id = fields.Many2one(
  9. 'account.bank.statement', string='Bank Statement')
  10. partner_id = fields.Many2one('res.partner', string='Partner Related',
  11. domain=['|', ('parent_id', '=', False),
  12. ('is_company', '=', True)])
  13. journal_ids = fields.Many2many(
  14. 'account.journal', string='Journals Filter')
  15. target_move = fields.Selection([
  16. ('posted', 'All Posted Entries'),
  17. ('all', 'All Entries'),
  18. ], string='Target Moves')
  19. allow_blocked = fields.Boolean(
  20. string='Allow Litigation Move Lines')
  21. invoice = fields.Boolean(
  22. string='Linked to an Invoice or Refund')
  23. date_type = fields.Selection([
  24. ('due', 'Due Date'),
  25. ('move', 'Move Date'),
  26. ], string="Type of Date Filter", required=True)
  27. due_date = fields.Date(string="Due Date",
  28. default=fields.Date.context_today)
  29. move_date = fields.Date(string='Move Date',
  30. default=fields.Date.context_today)
  31. move_line_ids = fields.Many2many(
  32. 'account.move.line', string='Move Lines')
  33. @api.model
  34. def default_get(self, field_list):
  35. res = super(AccountStatementLineCreate, self).default_get(field_list)
  36. context = self.env.context
  37. assert context.get('active_model') == 'account.bank.statement',\
  38. 'active_model should be account.bank.statement'
  39. assert context.get('active_id'), 'Missing active_id in context !'
  40. statement = self.env[
  41. 'account.bank.statement'].browse(context['active_id'])
  42. res.update({
  43. 'target_move': 'posted',
  44. 'date_type': 'due',
  45. 'invoice': True,
  46. 'statement_id': statement.id,
  47. })
  48. return res
  49. @api.multi
  50. def _prepare_move_line_domain(self):
  51. self.ensure_one()
  52. domain = [('reconciled', '=', False),
  53. ('account_id.internal_type', 'in', ('payable',
  54. 'receivable')),
  55. ('company_id', '=', self.env.user.company_id.id)]
  56. if self.journal_ids:
  57. domain += [('journal_id', 'in', self.journal_ids.ids)]
  58. else:
  59. journals = self.env['account.journal'].search([])
  60. domain += [('journal_id', 'in', journals.ids)]
  61. if self.partner_id:
  62. domain += [('partner_id', '=', self.partner_id.id)]
  63. if self.target_move == 'posted':
  64. domain += [('move_id.state', '=', 'posted')]
  65. if not self.allow_blocked:
  66. domain += [('blocked', '!=', True)]
  67. if self.date_type == 'due':
  68. domain += [
  69. '|',
  70. ('date_maturity', '<=', self.due_date),
  71. ('date_maturity', '=', False)]
  72. elif self.date_type == 'move':
  73. domain.append(('date', '<=', self.move_date))
  74. if self.invoice:
  75. domain.append(('invoice_id', '!=', False))
  76. paylines = self.env['account.payment'].search([
  77. ('state', 'in', ('draft', 'posted', 'sent')),
  78. ('move_line_ids', '!=', False)])
  79. if paylines:
  80. move_in_payment_ids = paylines.mapped('move_line_ids.id')
  81. domain += [('id', 'not in', move_in_payment_ids)]
  82. return domain
  83. @api.multi
  84. def populate(self):
  85. domain = self._prepare_move_line_domain()
  86. lines = self.env['account.move.line'].search(domain)
  87. self.move_line_ids = lines
  88. action = {
  89. 'name': _('Select Move Lines to Create Statement'),
  90. 'type': 'ir.actions.act_window',
  91. 'res_model': 'account.statement.line.create',
  92. 'view_mode': 'form',
  93. 'target': 'new',
  94. 'res_id': self.id,
  95. 'context': self._context,
  96. }
  97. return action
  98. @api.onchange(
  99. 'date_type', 'move_date', 'due_date', 'journal_ids', 'invoice',
  100. 'target_move', 'allow_blocked', 'partner_id')
  101. def move_line_filters_change(self):
  102. domain = self._prepare_move_line_domain()
  103. res = {'domain': {'move_line_ids': domain}}
  104. return res
  105. @api.multi
  106. def create_statement_lines(self):
  107. if self.move_line_ids:
  108. self.move_line_ids.create_statement_line_from_move_line(
  109. self.statement_id)
  110. return True