Skip to content

Commit

Permalink
[IMP+REF] Register Not Payment/Default and abstract commom methods us…
Browse files Browse the repository at this point in the history
…ed in CNAB changes.
  • Loading branch information
mbcosta committed Feb 26, 2021
1 parent 575a9a4 commit b581fa2
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 48 deletions.
1 change: 1 addition & 0 deletions l10n_br_account_payment_order/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
8 changes: 8 additions & 0 deletions l10n_br_account_payment_order/demo/account_account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@
<field name="reconcile" eval="True"/>
</record>

<!-- Conta Contabil de Não Pagamento/Inadimplencia para Modo de Pagamento -->
<record model="account.account" id="1_account_template_32333_avoid_travis_error">
<field name="code">3.2.3.33-*</field>
<field name="name"> Não Pagamento/Inadimplencia - AVOID_TRAVIS_ERROR</field>
<field name="user_type_id" ref="account.data_account_type_expenses"/>
<field name="company_id" ref="base.main_company"/>
<field name="reconcile" eval="True"/>
</record>

</odoo>
9 changes: 9 additions & 0 deletions l10n_br_account_payment_order/demo/account_payment_mode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="code_convetion">0001222130126</field>
<field name="cnab_liq_return_move_code_ids"
eval="[(6,0,[ref('cnab_240-mov_return_code_06'), ref('cnab_240-mov_return_code_09')])]"/>
Expand Down Expand Up @@ -128,6 +129,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<!-- Code de Convenio maximo 10 caracteres -->
<field name="code_convetion">92035760</field>
<field name="cnab_liq_return_move_code_ids"
Expand Down Expand Up @@ -163,6 +165,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="cnab_liq_return_move_code_ids"
eval="[(6,0,[ref('unicred_cnab_400-mov_return_code_01'), ref('unicred_cnab_400-mov_return_code_06'),
ref('unicred_cnab_400-mov_return_code_07'), ref('unicred_cnab_400-mov_return_code_09')])]"/>
Expand Down Expand Up @@ -192,6 +195,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="code_convetion">0001222130126</field>
<field name="cnab_liq_return_move_code_ids"
eval="[(6,0,[ref('cnab_240-mov_return_code_06'), ref('cnab_240-mov_return_code_09')])]"/>
Expand Down Expand Up @@ -223,6 +227,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="code_convetion">0001222130126</field>
</record>

Expand All @@ -248,6 +253,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<!-- Code de Convenio maximo 6 caracteres -->
<field name="code_convetion">000122</field>
<field name="cnab_liq_return_move_code_ids"
Expand Down Expand Up @@ -281,6 +287,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<!-- Code de Convenio maximo 6 caracteres -->
<field name="code_convetion">000122</field>
</record>
Expand Down Expand Up @@ -321,6 +328,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="code_convetion">0001222130126</field>
</record>

Expand Down Expand Up @@ -348,6 +356,7 @@
<field name="interest_fee_account_id" ref="1_account_template_31202_avoid_travis_error"/>
<field name="discount_account_id" ref="1_account_template_32202_avoid_travis_error"/>
<field name="rebate_account_id" ref="1_account_template_32203_avoid_travis_error"/>
<field name="not_payment_account_id" ref="1_account_template_32333_avoid_travis_error"/>
<field name="code_convetion">0001222130126</field>
</record>

Expand Down
193 changes: 147 additions & 46 deletions l10n_br_account_payment_order/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -335,13 +340,109 @@ 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)
elif change_type == 'change_payment_mode':
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')
Expand Down
7 changes: 7 additions & 0 deletions l10n_br_account_payment_order/models/account_payment_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<field name="interest_fee_account_id"/>
<field name="discount_account_id"/>
<field name="rebate_account_id"/>
<field name="not_payment_account_id"/>
</group>
</page>
<page string="Configuração dos Codigos CNAB"
Expand Down
12 changes: 10 additions & 2 deletions l10n_br_account_payment_order/wizards/account_move_line_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ def default_get(self, fields_list):
change_type = fields.Selection(
selection=[
('change_date_maturity', 'Vencimento'),
('change_payment_mode', 'Modo de Pagamento'),
('baixa', 'Baixa'),
# TODO: É preciso mais detalhes dessa operação ao enviar fora o
# codigo de alteração e carteira o que mais deve ir ?
# Ao alterar a carteira/payment mode de um Título deveria ser
# alterada de todas as account.move.line dessa invoice ?
# ('change_payment_mode', 'Modo de Pagamento'),
# TODO: qual seria esse caso Baixa ? Já que em caso de pagamento
# por fora do CNAB é preciso registrar o pagamento e isso está
# sendo chamado no metodo post do account.payment .
# ('baixa', 'Baixa'),
('not_payment', 'Baixa por Não Pagamento/Inadimplência')
],
string='Tipo Alteração',
)
Expand Down

0 comments on commit b581fa2

Please sign in to comment.