| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
from odoo import models, fields, api, _ |
| 4 |
from odoo.exceptions import UserError |
| 5 |
|
| 6 |
|
| 7 |
class AccountMoveReversal(models.TransientModel): |
| 8 |
_inherit = "account.move.reversal" |
| 9 |
|
| 10 |
l10n_latam_use_documents = fields.Boolean(compute='_compute_documents_info') |
| 11 |
l10n_latam_document_type_id = fields.Many2one('l10n_latam.document.type', 'Document Type', ondelete='cascade', domain="[('id', 'in', l10n_latam_available_document_type_ids)]", compute='_compute_document_type', readonly=False, store=True) |
| 12 |
l10n_latam_available_document_type_ids = fields.Many2many('l10n_latam.document.type', compute='_compute_documents_info') |
| 13 |
l10n_latam_document_number = fields.Char(string='Document Number') |
| 14 |
l10n_latam_manual_document_number = fields.Boolean(compute='_compute_l10n_latam_manual_document_number', string='Manual Number') |
| 15 |
|
| 16 |
@api.depends('l10n_latam_document_type_id', 'journal_id') |
| 17 |
def _compute_l10n_latam_manual_document_number(self): |
| 18 |
self.l10n_latam_manual_document_number = False |
| 19 |
for wiz in self.filtered(lambda x: x.journal_id and x.journal_id.l10n_latam_use_documents): |
| 20 |
wiz.l10n_latam_manual_document_number = self.env['account.move'].new({ |
| 21 |
'move_type': wiz._reverse_type_map(wiz.move_ids[0].move_type), |
| 22 |
'journal_id': wiz.journal_id.id, |
| 23 |
'partner_id': wiz.move_ids[0].partner_id.id, |
| 24 |
'company_id': wiz.move_ids[0].company_id.id, |
| 25 |
'reversed_entry_id': wiz.move_ids[0].id, |
| 26 |
})._is_manual_document_number() |
| 27 |
|
| 28 |
@api.model |
| 29 |
def _reverse_type_map(self, move_type): |
| 30 |
self.ensure_one() |
| 31 |
match = { |
| 32 |
'entry': 'entry', |
| 33 |
'out_invoice': 'out_refund', |
| 34 |
'in_invoice': 'in_refund', |
| 35 |
'in_refund': 'in_invoice', |
| 36 |
'out_receipt': 'in_receipt', |
| 37 |
'in_receipt': 'out_receipt'} |
| 38 |
return match.get(move_type) |
| 39 |
|
| 40 |
@api.depends('l10n_latam_available_document_type_ids', 'journal_id') |
| 41 |
def _compute_document_type(self): |
| 42 |
for record in self.filtered( |
| 43 |
lambda x: not x.l10n_latam_document_type_id or |
| 44 |
x.l10n_latam_document_type_id not in x.l10n_latam_available_document_type_ids): |
| 45 |
document_types = record.l10n_latam_available_document_type_ids._origin |
| 46 |
record.l10n_latam_document_type_id = document_types[0] if document_types else False |
| 47 |
|
| 48 |
@api.depends('move_ids', 'journal_id') |
| 49 |
def _compute_documents_info(self): |
| 50 |
self.l10n_latam_available_document_type_ids = False |
| 51 |
self.l10n_latam_use_documents = False |
| 52 |
for record in self: |
| 53 |
if len(record.move_ids) > 1: |
| 54 |
move_ids_use_document = record.move_ids._origin.filtered(lambda move: move.l10n_latam_use_documents) |
| 55 |
if move_ids_use_document: |
| 56 |
raise UserError(_('You can only reverse documents with legal invoicing documents from Latin America one at a time.\nProblematic documents: %s', ", ".join(move_ids_use_document.mapped('name')))) |
| 57 |
else: |
| 58 |
record.l10n_latam_use_documents = record.journal_id.l10n_latam_use_documents |
| 59 |
|
| 60 |
if record.l10n_latam_use_documents: |
| 61 |
refund = record.env['account.move'].new({ |
| 62 |
'move_type': record._reverse_type_map(record.move_ids.move_type), |
| 63 |
'journal_id': record.journal_id.id, |
| 64 |
'partner_id': record.move_ids.partner_id.id, |
| 65 |
'company_id': record.move_ids.company_id.id, |
| 66 |
'reversed_entry_id': record.move_ids.id, |
| 67 |
}) |
| 68 |
record.l10n_latam_available_document_type_ids = refund.l10n_latam_available_document_type_ids |
| 69 |
|
| 70 |
def _prepare_default_reversal(self, move): |
| 71 |
""" Set the default document type and number in the new revsersal move taking into account the ones selected in |
| 72 |
the wizard """ |
| 73 |
res = super()._prepare_default_reversal(move) |
| 74 |
res.update({ |
| 75 |
'l10n_latam_document_type_id': self.l10n_latam_document_type_id.id, |
| 76 |
'l10n_latam_document_number': self.l10n_latam_document_number, |
| 77 |
}) |
| 78 |
return res |
| 79 |
|
| 80 |
@api.onchange('l10n_latam_document_number', 'l10n_latam_document_type_id') |
| 81 |
def _onchange_l10n_latam_document_number(self): |
| 82 |
if self.l10n_latam_document_type_id: |
| 83 |
l10n_latam_document_number = self.l10n_latam_document_type_id._format_document_number( |
| 84 |
self.l10n_latam_document_number) |
| 85 |
if self.l10n_latam_document_number != l10n_latam_document_number: |
| 86 |
self.l10n_latam_document_number = l10n_latam_document_number |