summaryrefslogtreecommitdiff
path: root/addons/l10n_latam_invoice_document/wizards
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/l10n_latam_invoice_document/wizards
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/l10n_latam_invoice_document/wizards')
-rw-r--r--addons/l10n_latam_invoice_document/wizards/__init__.py4
-rw-r--r--addons/l10n_latam_invoice_document/wizards/account_debit_note.py15
-rw-r--r--addons/l10n_latam_invoice_document/wizards/account_move_reversal.py74
-rw-r--r--addons/l10n_latam_invoice_document/wizards/account_move_reversal_view.xml21
4 files changed, 114 insertions, 0 deletions
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+
+ <record id="view_account_move_reversal" model="ir.ui.view">
+ <field name="name">account.move.reversal.form</field>
+ <field name="model">account.move.reversal</field>
+ <field name="inherit_id" ref="account.view_account_move_reversal"/>
+ <field name="arch" type="xml">
+ <form>
+ <field name="l10n_latam_use_documents" invisible="1"/>
+ <field name="l10n_latam_manual_document_number" invisible="1"/>
+ </form>
+ <field name="date" position="before">
+ <field name="l10n_latam_available_document_type_ids" invisible="1"/>
+ <field name="l10n_latam_document_type_id" attrs="{'invisible': ['|', ('l10n_latam_use_documents', '=', False), ('refund_method', '=', 'refund')], 'required': [('l10n_latam_use_documents', '=', True), ('refund_method', '!=', 'refund')]}" options="{'no_open': True, 'no_create': True}"/>
+ <field name="l10n_latam_document_number" attrs="{'invisible': ['|', '|', ('l10n_latam_use_documents', '=', False), ('l10n_latam_manual_document_number', '=', False), ('refund_method', '=', 'refund')], 'required': [('l10n_latam_manual_document_number', '=', True), ('l10n_latam_use_documents', '=', True), ('refund_method', '!=', 'refund')]}"/>
+ </field>
+ </field>
+ </record>
+
+</odoo>