From b581fa25304dbfc8e678eab70eb1191e38b4a700 Mon Sep 17 00:00:00 2001 From: Magno Costa Date: Fri, 26 Feb 2021 13:36:12 -0300 Subject: [PATCH] [IMP+REF] Register Not Payment/Default and abstract commom methods used in CNAB changes. --- l10n_br_account_payment_order/constants.py | 1 + .../demo/account_account.xml | 8 + .../demo/account_payment_mode.xml | 9 + .../models/account_move_line.py | 193 +++++++++++++----- .../models/account_payment_mode.py | 7 + .../views/account_payment_mode.xml | 1 + .../wizards/account_move_line_change.py | 12 +- 7 files changed, 183 insertions(+), 48 deletions(-) diff --git a/l10n_br_account_payment_order/constants.py b/l10n_br_account_payment_order/constants.py index 8ba79ad2b63b..ded7cea713e4 100644 --- a/l10n_br_account_payment_order/constants.py +++ b/l10n_br_account_payment_order/constants.py @@ -463,6 +463,7 @@ ("liquidada", "Liquidada"), ("baixa", "Baixa Simples"), ("baixa_liquidacao", "Baixa por Liquidação fora do CNAB"), + ("nao_pagamento", "Baixa por Não Pagamento/Inadimplência") ] BOLETO_ESPECIE = [ diff --git a/l10n_br_account_payment_order/demo/account_account.xml b/l10n_br_account_payment_order/demo/account_account.xml index 2d95b20d71bf..b545e048a892 100644 --- a/l10n_br_account_payment_order/demo/account_account.xml +++ b/l10n_br_account_payment_order/demo/account_account.xml @@ -56,5 +56,13 @@ + + + 3.2.3.33-* + Não Pagamento/Inadimplencia - AVOID_TRAVIS_ERROR + + + + 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 1cf92be35c49..eb30913df2ca 100644 --- a/l10n_br_account_payment_order/demo/account_payment_mode.xml +++ b/l10n_br_account_payment_order/demo/account_payment_mode.xml @@ -91,6 +91,7 @@ + 0001222130126 @@ -128,6 +129,7 @@ + 92035760 + @@ -192,6 +195,7 @@ + 0001222130126 @@ -223,6 +227,7 @@ + 0001222130126 @@ -248,6 +253,7 @@ + 000122 + 000122 @@ -321,6 +328,7 @@ + 0001222130126 @@ -348,6 +356,7 @@ + 0001222130126 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 525db4efdc4b..0b733fc0e74c 100644 --- a/l10n_br_account_payment_order/models/account_move_line.py +++ b/l10n_br_account_payment_order/models/account_move_line.py @@ -241,6 +241,44 @@ def _get_payment_order(self, invoice): return payorder, new_payorder + def _check_cnab_instruction_to_be_send(self): + """ + Não pode ser enviada uma Instrução de CNAB se houver uma pendente + :return: Mensagem de Erro caso exista + """ + payment_line_to_be_send = self.payment_line_ids.filtered( + lambda t: t.order_id.state in ('draft', 'open', 'generated')) + if payment_line_to_be_send: + 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 another CNAB Instruction.' + ) % (payment_line_to_be_send.order_id.name, + payment_line_to_be_send.order_id.state, self.invoice_id.number)) + pass + + def _msg_payment_order_at_invoice(self, new_payorder, payorder): + """ + Registra a mensagem de alteração no Fatura para rastreabilidade. + :param new_payorder: Se é uma nova Ordem de Pagamento/Debito + :param payorder: Objeto Ordem de Pagamento/Debito + :return: + """ + cnab_instruction = self.mov_instruction_code_id.code + ' - ' + \ + self.mov_instruction_code_id.name + if new_payorder: + self.invoice_id.message_post(body=_( + 'Payment line added to the the new draft payment ' + 'order %s which has been automatically created,' + ' to send CNAB Instruction %s for OWN NUMBER %s.' + ) % (payorder.name, cnab_instruction, self.own_number)) + else: + self.invoice_id.message_post(body=_( + 'Payment line added to the existing draft ' + 'order %s to send CNAB Instruction %s for OWN NUMBER %s.' + ) % (payorder.name, cnab_instruction, self.own_number)) + def _change_date_maturity(self, new_date, reason): """ Alteração da Data de Vencimento de um lançamento CNAB. @@ -262,60 +300,27 @@ def _change_date_maturity(self, new_date, reason): "Payment Mode %s don't has Change Date Maturity Code," ' check if should have.' ) % self.payment_mode_id.name) - count = 0 + + self._check_cnab_instruction_to_be_send() + 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.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' self.write({ 'date_maturity': new_date, 'last_change_reason': reason, }) + # Registra as Alterações na Fatura + self._msg_payment_order_at_invoice(new_payorder, payorder) + def _change_payment_mode(self, reason, new_payment_mode_id, **kwargs): moves_to_sync = self.filtered( lambda m: m.payment_mode_id != new_payment_mode_id) @@ -335,6 +340,100 @@ def _create_baixa(self, reason, **kwargs): 'payment_situation': 'baixa', # FIXME: Podem ser múltiplos motivos }) + def _create_not_payment(self, reason): + """ + Não Pagamento/Inadimplencia + :param reason: descrição do motivo da alteração + :return: deveria retornar algo ? Uma mensagem de confirmação talvez ? + """ + # Modo de Pagto usado precisa ter a Conta Contabil de + # Não Pagamento/Inadimplencia + if not self.invoice_id.payment_mode_id.not_payment_account_id: + raise UserError(_( + "Payment Mode %s don't has the Account to Not Payment," + ' check if should have.' + ) % self.payment_mode_id.name) + + if not self.invoice_id.payment_mode_id.cnab_write_off_code_id: + raise UserError(_( + "Payment Mode %s don't has the CNAB Writte Off Code," + ' check if should have.' + ) % self.payment_mode_id.name) + + self._check_cnab_instruction_to_be_send() + + payorder, new_payorder = self._get_payment_order(self.invoice_id) + + # TODO: O codigo usado seria o mesmo do writte off ? + # Em todos os casos? + self.mov_instruction_code_id = \ + self.payment_mode_id.cnab_write_off_code_id + self.message_post(body=_( + 'Movement Instruction Code Updated for Request to' + ' Write Off, because not payment.')) + self.create_payment_line_from_move_line(payorder) + self.cnab_state = 'added' + + # Reconciliação e Baixa do Título + move_obj = self.env['account.move'] + move_line_obj = self.env['account.move.line'] + journal = self.payment_mode_id.fixed_journal_id + move = move_obj.create({ + 'name': 'CNAB - Banco ' + journal.bank_id.short_name + ' - Conta ' + + journal.bank_account_id.acc_number + '- Inadimplência', + 'date': fields.Datetime.now(), + # TODO - Campo está sendo preenchido em outro lugar + 'ref': 'CNAB Baixa por Inadimplêcia', + # O Campo abaixo é usado apenas para mostrar ou não a aba + # referente ao LOG do CNAB mas nesse caso não há. + # 'is_cnab': True, + 'journal_id': journal.id, + }) + # Linha a ser conciliada + counterpart_values = { + 'credit': self.amount_residual, + 'debit': 0.0, + 'account_id': self.account_id.id, + } + # linha referente a Conta Contabil de Inadimplecia + move_not_payment_values = { + 'debit': self.amount_residual, + 'credit': 0.0, + 'account_id': self.invoice_id. + payment_mode_id.not_payment_account_id.id, + } + + commom_move_values = { + 'move_id': move.id, + 'partner_id': self.partner_id.id, + 'already_completed': True, + 'ref': self.own_number, + 'journal_id': journal.id, + 'company_id': self.company_id.id, + 'currency_id': self.currency_id.id, + 'company_currency_id': self.company_id.currency_id.id, + } + + counterpart_values.update(commom_move_values) + move_not_payment_values.update(commom_move_values) + + moves = move_line_obj.with_context( + check_move_validity=False).create( + (counterpart_values, move_not_payment_values) + ) + + move_line_to_reconcile = moves.filtered( + lambda m: m.credit > 0.0) + (self + move_line_to_reconcile).reconcile() + + self.write({ + 'last_change_reason': reason, + 'payment_situation': 'nao_pagamento', + }) + + # Registra as Alterações na Fatura + self._msg_payment_order_at_invoice(new_payorder, payorder) + def _create_change(self, change_type, new_date, reason='', **kwargs): if change_type == 'change_date_maturity': self._change_date_maturity(new_date, reason) @@ -342,6 +441,8 @@ def _create_change(self, change_type, new_date, reason='', **kwargs): self._change_payment_mode(reason, **kwargs) elif change_type == 'baixa': self._create_baixa(reason, **kwargs) + elif change_type == 'not_payment': + self._create_not_payment(reason) @api.multi @api.depends('own_number') 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 2bd08d687456..ff01cf7de50e 100644 --- a/l10n_br_account_payment_order/models/account_payment_mode.py +++ b/l10n_br_account_payment_order/models/account_payment_mode.py @@ -333,6 +333,13 @@ class AccountPaymentMode(models.Model): related='fixed_journal_id.bank_id', ) + not_payment_account_id = fields.Many2one( + comodel_name='account.account', + string='Conta Contabil para Não Pagamento/Inadimplência', + help='Conta padrão para Não Pagamento/Inadimplência', + track_visibility='always', + ) + @api.constrains( 'code_convetion', 'cnab_sequence_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 cd3dfa81f7ad..9880b5b9d871 100644 --- a/l10n_br_account_payment_order/views/account_payment_mode.xml +++ b/l10n_br_account_payment_order/views/account_payment_mode.xml @@ -79,6 +79,7 @@ +