diff --git a/l10n_br_account_payment_order/demo/account_payment_mode.xml b/l10n_br_account_payment_order/demo/account_payment_mode.xml
index 3b45d622ba55..1cf92be35c49 100644
--- a/l10n_br_account_payment_order/demo/account_payment_mode.xml
+++ b/l10n_br_account_payment_order/demo/account_payment_mode.xml
@@ -97,6 +97,7 @@
+
@@ -134,6 +135,7 @@
ref('unicred_cnab_400-mov_return_code_07'), ref('unicred_cnab_400-mov_return_code_09')])]"/>
+
@@ -196,6 +198,7 @@
+
@@ -253,6 +256,7 @@
+
diff --git a/l10n_br_account_payment_order/models/account_move_line.py b/l10n_br_account_payment_order/models/account_move_line.py
index d6d7676d2f28..525db4efdc4b 100644
--- a/l10n_br_account_payment_order/models/account_move_line.py
+++ b/l10n_br_account_payment_order/models/account_move_line.py
@@ -2,7 +2,8 @@
# Luis Felipe Miléo - mileo@kmee.com.br
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-from odoo import api, fields, models
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
from ..constants import ESTADOS_CNAB, SITUACAO_PAGAMENTO
@@ -218,10 +219,99 @@ def _create_payment_order_change(self, **kwargs):
self.ensure_one()
# TODO:
- def _change_date_maturity(self, new_date, reason, **kwargs):
- moves_to_sync = self.filtered(lambda m: m.date_maturity != new_date)
- moves_to_sync._create_payment_order_change(new_date=new_date, **kwargs)
- moves_to_sync.write({
+ def _get_payment_order(self, invoice):
+ """
+ Obtem a Ordem de Pagamento a ser usada e se é uma nova
+ :param invoice:
+ :return: Orderm de Pagamento, E se é uma nova
+ """
+ # Verificar Ordem de Pagto
+ apo = self.env['account.payment.order']
+ # Existe a possibilidade de uma Fatura ter diferentes
+ # Modos de Pagto nas linhas no caso CNAB ?
+ payorder = apo.search([
+ ('payment_mode_id', '=', invoice.payment_mode_id.id),
+ ('state', '=', 'draft')], limit=1)
+ new_payorder = False
+ if not payorder:
+ payorder = apo.create(
+ invoice._prepare_new_payment_order(invoice.payment_mode_id)
+ )
+ new_payorder = True
+
+ return payorder, new_payorder
+
+ def _change_date_maturity(self, new_date, reason):
+ """
+ Alteração da Data de Vencimento de um lançamento CNAB.
+ :param new_date: nova data de vencimento
+ :param reason: descrição do motivo da alteração
+ :return: deveria retornar algo ? Uma mensagem de confirmação talvez ?
+ """
+ # moves_to_sync = self.filtered(lambda m: m.date_maturity != new_date)
+ # moves_to_sync._create_payment_order_change(new_date=new_date)
+
+ if new_date == self.date_maturity:
+ raise UserError(_(
+ 'New Date Maturity %s is equal to actual Date Maturity %s'
+ ) % (new_date, self.date_maturity))
+
+ # Modo de Pagto usado precisa ter o codigo de alteração do vencimento
+ if not self.invoice_id.payment_mode_id.cnab_code_change_maturity_date_id:
+ raise UserError(_(
+ "Payment Mode %s don't has Change Date Maturity Code,"
+ ' check if should have.'
+ ) % self.payment_mode_id.name)
+ count = 0
+ payorder, new_payorder = self._get_payment_order(self.invoice_id)
+ for payment_line in self.payment_line_ids:
+ # Verificar qual o status da
+ # Ordem de Pagto relacionada
+ if payment_line.order_id.state == 'draft':
+ old_date = payment_line.ml_maturity_date
+ payment_line.ml_maturity_date = self.date_maturity
+ self.message_post(body=_(
+ 'Change Maturity Date of Title from %s to'
+ ' %s before sending.') % (old_date, self.date_maturity))
+
+ elif payment_line.order_id.state in ('uploaded', 'done'):
+
+ # Arquivo Enviado necessário solicitar a Baixa
+ # ao Banco enviando a respectiva Instrução do Movimento
+ self.mov_instruction_code_id = \
+ self.payment_mode_id.cnab_code_change_maturity_date_id
+ self.message_post(body=_(
+ 'Movement Instruction Code Updated for Request to'
+ ' Change Maturity Date.'))
+ self.create_payment_line_from_move_line(payorder)
+ self.cnab_state = 'added'
+ count += 1
+
+ # TODO existe possibilidade de uma Ordem de
+ # Pagto CNAB ser cancelada ?
+ elif payment_line.order_id.state in (
+ 'open', 'generated', 'cancel'):
+ raise UserError(_(
+ 'There is a CNAB Payment Order %s in status %s'
+ ' related to invoice %s created, the CNAB file'
+ ' should be sent to bank, because only after'
+ ' that it is possible make new Payment Order with'
+ ' the instruction to Request Change Maturity Date.'
+ ) % (payment_line.order_id.name,
+ payment_line.order_id.state, self.invoice_id.number))
+
+ if new_payorder:
+ self.invoice_id.message_post(body=_(
+ '%d payment lines added to the new draft payment '
+ 'order %s which has been automatically created.'
+ ) % (count, payorder.name))
+ else:
+ self.invoice_id.message_post(body=_(
+ '%d payment lines added to the existing draft '
+ 'payment order %s.'
+ ) % (count, payorder.name))
+
+ self.write({
'date_maturity': new_date,
'last_change_reason': reason,
})
@@ -245,9 +335,9 @@ def _create_baixa(self, reason, **kwargs):
'payment_situation': 'baixa', # FIXME: Podem ser múltiplos motivos
})
- def _create_change(self, change_type, reason='', **kwargs):
+ def _create_change(self, change_type, new_date, reason='', **kwargs):
if change_type == 'change_date_maturity':
- self._change_date_maturity(reason, **kwargs)
+ self._change_date_maturity(new_date, reason)
elif change_type == 'change_payment_mode':
self._change_payment_mode(reason, **kwargs)
elif change_type == 'baixa':
diff --git a/l10n_br_account_payment_order/models/account_payment_mode.py b/l10n_br_account_payment_order/models/account_payment_mode.py
index fc01cd668ca5..2bd08d687456 100644
--- a/l10n_br_account_payment_order/models/account_payment_mode.py
+++ b/l10n_br_account_payment_order/models/account_payment_mode.py
@@ -320,6 +320,14 @@ class AccountPaymentMode(models.Model):
track_visibility='always',
)
+ # Codigo para Alteração da Data de Vencimento
+ cnab_code_change_maturity_date_id = fields.Many2one(
+ comodel_name='l10n_br_cnab.mov.instruction.code',
+ string='Change Maturity Date Movement Instruction Code',
+ help='CNAB Movement Instruction Code for Change Maturity Date.',
+ track_visibility='always',
+ )
+
# Field used to make invisible banks specifics fields
bank_id = fields.Many2one(
related='fixed_journal_id.bank_id',
diff --git a/l10n_br_account_payment_order/views/account_payment_mode.xml b/l10n_br_account_payment_order/views/account_payment_mode.xml
index 01c8ed8ed1d0..cd3dfa81f7ad 100644
--- a/l10n_br_account_payment_order/views/account_payment_mode.xml
+++ b/l10n_br_account_payment_order/views/account_payment_mode.xml
@@ -99,6 +99,9 @@
+