From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- .../wizards/__init__.py | 4 ++ .../wizards/account_debit_note.py | 15 +++++ .../wizards/account_move_reversal.py | 74 ++++++++++++++++++++++ .../wizards/account_move_reversal_view.xml | 21 ++++++ 4 files changed, 114 insertions(+) create mode 100644 addons/l10n_latam_invoice_document/wizards/__init__.py create mode 100644 addons/l10n_latam_invoice_document/wizards/account_debit_note.py create mode 100644 addons/l10n_latam_invoice_document/wizards/account_move_reversal.py create mode 100644 addons/l10n_latam_invoice_document/wizards/account_move_reversal_view.xml (limited to 'addons/l10n_latam_invoice_document/wizards') diff --git a/addons/l10n_latam_invoice_document/wizards/__init__.py b/addons/l10n_latam_invoice_document/wizards/__init__.py new file mode 100644 index 00000000..4d069b50 --- /dev/null +++ b/addons/l10n_latam_invoice_document/wizards/__init__.py @@ -0,0 +1,4 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import account_move_reversal +from . import account_debit_note diff --git a/addons/l10n_latam_invoice_document/wizards/account_debit_note.py b/addons/l10n_latam_invoice_document/wizards/account_debit_note.py new file mode 100644 index 00000000..e84f6ba6 --- /dev/null +++ b/addons/l10n_latam_invoice_document/wizards/account_debit_note.py @@ -0,0 +1,15 @@ +from odoo import models + + +class AccountDebitNote(models.TransientModel): + + _inherit = 'account.debit.note' + + def create_debit(self): + """ Properly compute the latam document type of type debit note. """ + res = super().create_debit() + new_move_id = res.get('res_id') + if new_move_id: + new_move = self.env['account.move'].browse(new_move_id) + new_move._compute_l10n_latam_document_type() + return res diff --git a/addons/l10n_latam_invoice_document/wizards/account_move_reversal.py b/addons/l10n_latam_invoice_document/wizards/account_move_reversal.py new file mode 100644 index 00000000..caef7474 --- /dev/null +++ b/addons/l10n_latam_invoice_document/wizards/account_move_reversal.py @@ -0,0 +1,74 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models, fields, api, _ +from odoo.exceptions import UserError + + +class AccountMoveReversal(models.TransientModel): + _inherit = "account.move.reversal" + + l10n_latam_use_documents = fields.Boolean(compute='_compute_document_type') + 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) + l10n_latam_available_document_type_ids = fields.Many2many('l10n_latam.document.type', compute='_compute_document_type') + l10n_latam_document_number = fields.Char(string='Document Number') + l10n_latam_manual_document_number = fields.Boolean(compute='_compute_l10n_latam_manual_document_number', string='Manual Number') + + @api.depends('l10n_latam_document_type_id') + def _compute_l10n_latam_manual_document_number(self): + self.l10n_latam_manual_document_number = False + for rec in self.filtered('move_ids'): + move = rec.move_ids[0] + if move.journal_id and move.journal_id.l10n_latam_use_documents: + rec.l10n_latam_manual_document_number = self.env['account.move']._is_manual_document_number(move.journal_id) + + @api.model + def _reverse_type_map(self, move_type): + match = { + 'entry': 'entry', + 'out_invoice': 'out_refund', + 'in_invoice': 'in_refund', + 'in_refund': 'in_invoice', + 'out_receipt': 'in_receipt', + 'in_receipt': 'out_receipt'} + return match.get(move_type) + + @api.depends('move_ids') + def _compute_document_type(self): + self.l10n_latam_available_document_type_ids = False + self.l10n_latam_document_type_id = False + self.l10n_latam_use_documents = False + for record in self: + if len(record.move_ids) > 1: + move_ids_use_document = record.move_ids._origin.filtered(lambda move: move.l10n_latam_use_documents) + if move_ids_use_document: + 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'))) + else: + record.l10n_latam_use_documents = record.move_ids.journal_id.l10n_latam_use_documents + + if record.l10n_latam_use_documents: + refund = record.env['account.move'].new({ + 'move_type': record._reverse_type_map(record.move_ids.move_type), + 'journal_id': record.move_ids.journal_id.id, + 'partner_id': record.move_ids.partner_id.id, + 'company_id': record.move_ids.company_id.id, + }) + record.l10n_latam_document_type_id = refund.l10n_latam_document_type_id + record.l10n_latam_available_document_type_ids = refund.l10n_latam_available_document_type_ids + + def _prepare_default_reversal(self, move): + """ Set the default document type and number in the new revsersal move taking into account the ones selected in + the wizard """ + res = super()._prepare_default_reversal(move) + res.update({ + 'l10n_latam_document_type_id': self.l10n_latam_document_type_id.id, + 'l10n_latam_document_number': self.l10n_latam_document_number, + }) + return res + + @api.onchange('l10n_latam_document_number', 'l10n_latam_document_type_id') + def _onchange_l10n_latam_document_number(self): + if self.l10n_latam_document_type_id: + l10n_latam_document_number = self.l10n_latam_document_type_id._format_document_number( + self.l10n_latam_document_number) + if self.l10n_latam_document_number != l10n_latam_document_number: + self.l10n_latam_document_number = l10n_latam_document_number diff --git a/addons/l10n_latam_invoice_document/wizards/account_move_reversal_view.xml b/addons/l10n_latam_invoice_document/wizards/account_move_reversal_view.xml new file mode 100644 index 00000000..90f7a67a --- /dev/null +++ b/addons/l10n_latam_invoice_document/wizards/account_move_reversal_view.xml @@ -0,0 +1,21 @@ + + + + + account.move.reversal.form + account.move.reversal + + +
+ + + + + + + + + +
+ +
-- cgit v1.2.3