Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0] l10n_br_account inherits aml #1205

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion l10n_br_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"author": "Akretion, Odoo Community Association (OCA)",
"website": "http://github.com/OCA/l10n-brazil",
"version": "12.0.2.4.0",
"depends": ["account_cancel", "l10n_br_coa", "l10n_br_fiscal"],
"depends": [
"account_cancel",
"account_payment_mode",
"l10n_br_coa",
"l10n_br_fiscal",
],
"data": [
# security
'security/ir.model.access.csv',
Expand Down
2 changes: 2 additions & 0 deletions l10n_br_account/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
from . import fiscal_document
from . import account_move
from . import account_move_line
from . import payment
from . import payment_line
25 changes: 24 additions & 1 deletion l10n_br_account/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (C) 2009 - TODAY Renato Lima - Akretion
# Copyright (C) 2019 - TODAY Raphaël Valyi - Akretion
# Copyright (C) 2020 - TODAY Luis Felipe Miléo - KMEE
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from lxml import etree
Expand Down Expand Up @@ -41,6 +42,7 @@
'date',
'currency_id',
'partner_shipping_id',
'payment_term_id',
]


Expand All @@ -49,9 +51,16 @@ class AccountInvoice(models.Model):
_inherit = [
_name,
'l10n_br_fiscal.document.mixin.methods',
'l10n_br_fiscal.document.invoice.mixin']
'l10n_br_fiscal.document.invoice.mixin',
'l10n_br_fiscal.payment.mixin',
]
_inherits = {'l10n_br_fiscal.document': 'fiscal_document_id'}
_order = 'date_invoice DESC, number DESC'
_payment_inverse_name = 'invoice_id'

@api.depends("amount_total", "fiscal_payment_ids")
def _compute_payment_change_value(self):
self._abstract_compute_payment_change_value()

# initial account.invoice inherits on fiscal.document that are
# disable with active=False in their fiscal_document table.
Expand Down Expand Up @@ -106,6 +115,20 @@ class AccountInvoice(models.Model):
ondelete='cascade',
)

financial_ids = fields.One2many(
comodel_name='l10n_br_fiscal.payment.line',
inverse_name=_payment_inverse_name,
string='Duplicatas',
copy=True,
)

fiscal_payment_ids = fields.One2many(
comodel_name='l10n_br_fiscal.payment',
inverse_name=_payment_inverse_name,
string='Pagamentos',
copy=True,
)

@api.multi
@api.depends('move_id.line_ids')
def _compute_financial(self):
Expand Down
89 changes: 88 additions & 1 deletion l10n_br_account/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,97 @@
# Copyright (C) 2016-TODAY Akretion <http://www.akretion.com>
# @author Magno Costa <[email protected]>
# Copyright (C) 2021 - TODAY Luis Felipe Miléo - KMEE
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import models
from odoo import models, fields, api

SHADOWED_FIELDS = [
'date_maturity',
'company_id',
'currency_id',
'invoice_id',
]


class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
_order = 'date_maturity, date desc, id desc'
_inherits = {'l10n_br_fiscal.payment.line': 'fiscal_payment_line_id'}

fiscal_document_id = fields.Many2one(
comodel_name='l10n_br_fiscal.document',
related='invoice_id.fiscal_document_id',
store=True,
)

fiscal_payment_line_id = fields.Many2one(
comodel_name='l10n_br_fiscal.payment.line',
string='Fiscal Payment Line',
required=True,
copy=False,
ondelete='cascade',
)

@api.model
def _shadowed_fields(self):
"""Returns the list of shadowed fields that are synced
from the parent."""
return SHADOWED_FIELDS

@api.multi
def _prepare_shadowed_fields_dict(self, default=False):
self.ensure_one()
vals = self._convert_to_write(self.read(self._shadowed_fields())[0])
if default: # in case you want to use new rather than write later
return {"default_%s" % (k,): vals[k] for k in vals.keys()}
return vals

@api.model
def create(self, values):
dummy_payment_line_id = self.env.ref(
'l10n_br_fiscal.fiscal_payment_line_dummy').id
dummy_doc_id = self.env.ref('l10n_br_fiscal.fiscal_document_dummy').id
fiscal_doc_id = self.env['account.invoice'].browse(
values['invoice_id']).fiscal_document_id.id

account_id = self.env['account.account'].browse(values['account_id'])

if dummy_doc_id == fiscal_doc_id or not account_id.reconcile:
values['fiscal_payment_line_id'] = dummy_payment_line_id
aml = super().create(values)
if dummy_doc_id != fiscal_doc_id and account_id.reconcile:
shadowed_fiscal_vals = aml._prepare_shadowed_fields_dict()
doc_id = aml.invoice_id.fiscal_document_id.id
shadowed_fiscal_vals['document_id'] = doc_id
aml.fiscal_payment_line_id.write(shadowed_fiscal_vals)
return aml

@api.multi
def write(self, values):
dummy_payment_line_id = self.env.ref(
'l10n_br_fiscal.fiscal_payment_line_dummy')
if values.get('invoice_id'):
values['document_id'] = self.env[
"account.invoice"].browse(
values['invoice_id']).fiscal_document_id.id
result = super().write(values)
for aml in self:
if aml.fiscal_payment_line_id != dummy_payment_line_id:
shadowed_fiscal_vals = aml._prepare_shadowed_fields_dict()
aml.fiscal_payment_line_id.write(shadowed_fiscal_vals)
return result

@api.multi
def unlink(self):
dummy_payment_line_id = self.env.ref(
'l10n_br_fiscal.fiscal_payment_line_dummy').id
unlink_fiscal_payment_lines = self.env['l10n_br_fiscal.payment.line']
for record in self:
if not record.exists():
continue
if record.fiscal_payment_line_id.id != dummy_payment_line_id:
unlink_fiscal_payment_lines |= record.fiscal_document_line_id
result = super().unlink()
unlink_fiscal_payment_lines.unlink()
self.clear_caches()
return result
17 changes: 17 additions & 0 deletions l10n_br_account/models/payment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2020 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class FiscalPayment(models.Model):
_inherit = "l10n_br_fiscal.payment"

payment_term_id = fields.Many2one(
comodel_name='account.payment.term',
)
invoice_id = fields.Many2one(
comodel_name='account.invoice',
string='Invoice',
ondelete='cascade',
)
15 changes: 15 additions & 0 deletions l10n_br_account/models/payment_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields


class FiscalPaymentLine(models.Model):
_inherit = 'l10n_br_fiscal.payment.line'

invoice_id = fields.Many2one(
comodel_name='account.invoice',
string='Invoice',
related='payment_id.invoice_id',
store=True,
)