Skip to content

Commit

Permalink
Merge 9be8928 into 10f5c53
Browse files Browse the repository at this point in the history
  • Loading branch information
danimaribeiro committed Jan 21, 2019
2 parents 10f5c53 + 9be8928 commit 04b8c27
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 122 deletions.
2 changes: 1 addition & 1 deletion br_account_payment/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ResCompany(models.Model):
l10n_br_payment_interest_account_id = fields.Many2one(
'account.account', string="Conta para pagamento de juros")
l10n_br_payment_discount_account_id = fields.Many2one(
'account.account', string="Conta para pagamento de multa")
'account.account', string="Conta para desconto de pagamentos")

l10n_br_interest_account_id = fields.Many2one(
'account.account', string="Conta para recebimento de juros/multa")
Expand Down
2 changes: 1 addition & 1 deletion br_account_payment/models/payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _compute_identifier(self):
emission_date = fields.Date(string="Data de Emissão")
currency_id = fields.Many2one('res.currency', string="Currency")
amount_total = fields.Monetary(
string="Valor", digits=(18, 2), oldname='value')
string="Valor", digits=(18, 2), oldname='value', store=True)
state = fields.Selection([("draft", "Rascunho"),
("approved", "Aprovado"),
("sent", "Enviado"),
Expand Down
5 changes: 0 additions & 5 deletions br_payment_cnab/i18n/pt_BR.po
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,6 @@ msgstr "FAJ-TR"
msgid "FGTS"
msgstr "FGTS"

#. module: br_payment_cnab
#: model:ir.model.fields,field_description:br_payment_cnab.field_payment_order_line_value_final
msgid "Final Value"
msgstr "Final Value"

#. module: br_payment_cnab
#: code:addons/br_payment_cnab/models/account_voucher.py:81
#, python-format
Expand Down
9 changes: 0 additions & 9 deletions br_payment_cnab/models/payment_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ def name_get(self):

reg_type = fields.Integer('Register Type')

interest_value = fields.Float('Interest Value')

fine_value = fields.Float('Duty Value')

rebate_value = fields.Float('Rebate Value')

discount_value = fields.Float('Discount Value')

mov_type = fields.Selection(
[('0', 'Inclusion'),
('5', 'Modification'),
Expand Down Expand Up @@ -112,7 +104,6 @@ def name_get(self):
], string="Tipo de Serviço")

message2 = fields.Char(string='Note Detail', size=40, default='')

agency_name = fields.Char(string='Agency Name', size=30, default='')

currency_code = fields.Selection(
Expand Down
44 changes: 32 additions & 12 deletions br_payment_cnab/models/payment_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
class PaymentOrder(models.Model):
_inherit = 'payment.order'

def _compute_amount_total(self):
for item in self:
amount_total = 0
for line in item.line_ids:
amount_total += line.value_final
item.amount_total = amount_total

def select_bank_cnab(self, code):
banks = {
'756': sicoob.Sicoob240(self),
Expand Down Expand Up @@ -63,9 +70,16 @@ class PaymentOrderLine(models.Model):
linha_digitavel = fields.Char(string="Linha Digitável")
barcode = fields.Char('Código de Barras')
invoice_date = fields.Date('Data da Fatura')
payment_date = fields.Date('Data efetiva pagamento')

fine_value = fields.Float('Valor Multa')
interest_value = fields.Float('Valor Juros')
rebate_value = fields.Float('Abatimentos')
discount_value = fields.Float('Descontos')

value_final = fields.Float(
string="Final Value", compute="_compute_final_value",
digits=(18, 2), readonly=True)
string="Valor Final", compute="_compute_final_value",
digits=(18, 2), readonly=True, store=True)

autenticacao_pagamento = fields.Char(
string="Chave de Autenticação do pagamento")
Expand All @@ -84,10 +98,18 @@ class PaymentOrderLine(models.Model):
string="Agência do Favorecido",
readonly=True)

_sql_constraints = [
('payment_order_line_barcode_uniq', 'unique (barcode)',
_('O código de barras deve ser único!'))
]
@api.one
@api.constrains('barcode')
def _constrains_unique_barcode(self):
if not self.barcode:
return True
total = self.search_count(
[('barcode', '=', self.barcode), ('id', '!=', self.id),
('state', 'not in', ('rejected', 'cancelled'))])

if total > 0:
raise UserError('O código de barras deve ser único!')
return True

def validate_partner_data(self, vals):
errors = []
Expand Down Expand Up @@ -194,12 +216,12 @@ def validate_information(self, payment_mode, vals):
["Por favor corrija os erros antes de prosseguir"] + errors)
raise UserError(msg)

@api.depends('payment_information_id')
@api.depends('amount_total', 'rebate_value',
'discount_value', 'interest_value')
def _compute_final_value(self):
for item in self:
payment = item.payment_information_id
desconto = payment.rebate_value + payment.discount_value
acrescimo = payment.fine_value + payment.interest_value
desconto = item.rebate_value + item.discount_value
acrescimo = item.fine_value + item.interest_value
item.value_final = (item.amount_total - desconto + acrescimo)

def get_operation_code(self, payment_mode):
Expand Down Expand Up @@ -241,8 +263,6 @@ def get_information_vals(self, payment_mode_id, vals):
'operation_code': self.get_operation_code(payment_mode_id),
'codigo_receita': payment_mode_id.codigo_receita,
'service_type': self.get_service_type(payment_mode_id),
'fine_value': vals.get('fine_value'),
'interest_value': vals.get('interest_value'),
'numero_referencia': payment_mode_id.numero_referencia,
'cod_recolhimento_fgts': payment_mode_id.cod_recolhimento,
'identificacao_fgts': payment_mode_id.identificacao_fgts,
Expand Down
17 changes: 7 additions & 10 deletions br_payment_cnab/serialize/cnab240.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,19 @@ def _get_segmento(self, line, lot_sequency, num_lot, nome_segmento):
"cep_complemento": self._just_numbers(line.partner_id.zip[5:]),
"favorecido_uf": line.partner_id.state_id.code or '',
"valor_documento": self._float_to_monetary(line.amount_total),
"valor_abatimento": self._float_to_monetary(
information_id.rebate_value),
"valor_desconto": self._float_to_monetary(
information_id.discount_value),
"valor_mora": self._float_to_monetary(
information_id.interest_value),
"valor_multa": self._float_to_monetary(information_id.fine_value),
"valor_abatimento": self._float_to_monetary(line.rebate_value),
"valor_desconto": self._float_to_monetary(line.discount_value),
"valor_mora": self._float_to_monetary(line.interest_value),
"valor_multa": self._float_to_monetary(line.fine_value),
"hora_envio_ted": self._hour_now(),
"codigo_historico_credito": information_id.credit_hist_code,
"cedente_nome": self._order.company_id.legal_name[:30],
"valor_nominal_titulo": self._float_to_monetary(
line.amount_total),
"valor_desconto_abatimento": self._float_to_monetary(
information_id.rebate_value + information_id.discount_value),
line.rebate_value + line.discount_value),
"valor_multa_juros": self._float_to_monetary(
information_id.interest_value + information_id.fine_value),
line.interest_value + line.fine_value),
"codigo_moeda": int(information_id.currency_code),
"codigo_de_barras": self._string_to_num(line.barcode),
"codigo_de_barras_alfa": line.barcode or '',
Expand All @@ -153,7 +150,7 @@ def _get_segmento(self, line, lot_sequency, num_lot, nome_segmento):
(line.partner_id.legal_name or line.partner_id.name)[:30],
"data_vencimento": int(self.format_date(line.date_maturity)),
"valor_juros_encargos": self._string_to_monetary(
information_id.interest_value),
line.interest_value),
# GPS
"contribuinte_nome": self._order.company_id.legal_name[:30],
"codigo_receita_tributo": information_id.codigo_receita or '',
Expand Down
4 changes: 0 additions & 4 deletions br_payment_cnab/views/payment_information.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
<field name="lote_serv"/>
<field name="reg_type"/>
<field name="message2"/>
<field name="rebate_value" widget="monetary"/>
<field name="discount_value" widget="monetary"/>
<field name="credit_hist_code"/>
<field name="l10n_br_environment" />
</group>
Expand All @@ -36,8 +34,6 @@
<field name="agency_name"/>
<field name="operation_code"/>
<field name="message1"/>
<field name="fine_value" widget="monetary"/>
<field name="interest_value" widget="monetary"/>
<field name="mov_type"/>
<field name="mov_instruc"/>
<field name="service_type"/>
Expand Down
11 changes: 11 additions & 0 deletions br_payment_cnab/views/payment_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<field name="inherit_id" ref="br_account_payment.trustcode_payment_order_line_tree_view"/>
<field name="type">tree</field>
<field name="arch" type="xml" >
<field name="amount_total" position="after">
<field name="value_final" />
</field>
<field name="nosso_numero" position="after">
<button name="action_aprove_payment_line" type="object" string="APROVAR" class="btn btn-success btn-xs" groups="account.group_account_user" states="draft" />
<button name="action_view_more_info" type="object" string="VER" class="btn btn-success btn-xs"/>
Expand Down Expand Up @@ -47,6 +50,14 @@
</field>
<field name="emission_date" position="after">
<field name="invoice_date"/>
<field name="payment_date"/>
</field>
<field name="amount_total" position="after">
<field name="fine_value"/>
<field name="interest_value"/>
<field name="rebate_value"/>
<field name="discount_value"/>
<field name="value_final"/>
</field>
</field>
</record>
Expand Down
46 changes: 4 additions & 42 deletions br_payment_cnab_voucher/models/account_voucher.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class AccountVoucher(models.Model):
barcode = fields.Char(
'Barcode', compute="_compute_barcode", store=True, readonly=True)
interest_value = fields.Float(
'Interest Value', readonly=True,
'Valor Juros', readonly=True,
states={'draft': [('readonly', False)]})
fine_value = fields.Float(
'Fine Value', readonly=True, states={'draft': [('readonly', False)]})
'Valor Multa', readonly=True, states={'draft': [('readonly', False)]})

numero_parcela_icms = fields.Integer(
'Número da parcela/notificação', readonly=True,
Expand Down Expand Up @@ -136,9 +136,8 @@ def _prepare_payment_order_vals(self):
'invoice_date': self.date,
'barcode': self.barcode,
'linha_digitavel': self.linha_digitavel,
# TODO Ajustar o valor de multa e de juros
# 'fine_value': self.fine_value,
# 'interest_value': self.interest_value,
'fine_value': self.fine_value,
'interest_value': self.interest_value,
}

@api.multi
Expand All @@ -158,40 +157,3 @@ def proforma_voucher(self):
def validate_cnab_fields(self):
if not self.date_due:
raise UserError(_("Please select a Due Date for the payment"))

def create_interest_fine_line(self, line_type, vals):
account_id = self.env['ir.config_parameter'].sudo().get_param(
'br_payment_cnab.{}_account_id'.format(line_type))
if not account_id:
raise UserError(_(
"Please configure the interest and fine accounts"))
line_vals = (0, 0, {
'name': _('Fine Line') if line_type == 'fine' else _(
'Interest Line'),
'quantity': 1,
'price_unit': vals.get('{}_value'.format(line_type)),
'account_id': account_id
})
if vals.get('line_ids'):
vals['line_ids'].append(line_vals)
else:
vals.update(line_ids=[line_vals])
return vals

@api.multi
def write(self, vals):
if vals.get('interest_value'):
vals = self.create_interest_fine_line('interest', vals)
if vals.get('fine_value'):
vals = self.create_interest_fine_line('fine', vals)
res = super(AccountVoucher, self).write(vals)
return res

@api.model
def create(self, vals):
if vals.get('interest_value', 0) > 0:
vals = self.create_interest_fine_line('interest', vals)
if vals.get('fine_value', 0) > 0:
vals = self.create_interest_fine_line('fine', vals)
res = super(AccountVoucher, self).create(vals)
return res
12 changes: 8 additions & 4 deletions br_payment_cnab_voucher/tests/test_cnab_sicoob.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def set_order_lines(self):
'state': 'sent',
'value_final': 150.00,
'payment_information_id': info_id.id,
'rebate_value': 1.00,
'discount_value': 2.00,
'fine_value': 3.00,
'interest_value': 1.00,
'voucher_id': self.get_voucher_line().id,
'bank_account_id': self.receivable_account.id,
}))
Expand All @@ -120,6 +124,10 @@ def set_order_lines(self):
'state': 'sent',
'value_final': 120.00,
'payment_information_id': info_id.id,
'rebate_value': 1.00,
'discount_value': 2.00,
'fine_value': 3.00,
'interest_value': 1.00,
'voucher_id': self.get_voucher_line().id,
'bank_account_id': self.receivable_account.id,
}))
Expand All @@ -134,10 +142,6 @@ def get_payment_information(self):
'mov_finality': '01',
'warning_code': '0',
'mov_instruc': '00',
'rebate_value': 1.00,
'discount_value': 2.00,
'fine_value': 3.00,
'interest_value': 1.00,
'credit_hist_code': '2644',
'operation_code': '018',
'percentual_receita_bruta_acumulada': Decimal('12.00')
Expand Down
48 changes: 48 additions & 0 deletions br_payment_invoice/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
# © 2018 Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import re
import logging
from odoo import api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)
try:
from pycnab240.utils import decode_digitable_line, pretty_format_line
from pycnab240.errors import DvNotValidError
except ImportError:
_logger.error('Cannot import pycnab240', exc_info=True)


class AccountInvoice(models.Model):
_inherit = 'account.invoice'

l10n_br_linha_digitavel = fields.Char(
string="Linha Digitável", readonly=True,
states={'draft': [('readonly', False)]})
l10n_br_barcode = fields.Char(
'Barcode', compute="_compute_barcode", store=True, readonly=True)
l10n_br_payment_type = fields.Selection(
related="payment_mode_id.payment_type", readonly=True)
l10n_br_bank_account_id = fields.Many2one(
'res.partner.bank', string="Conta p/ Transferência",
domain="[('partner_id', '=', partner_id)]", readonly=True,
states={'draft': [('readonly', False)]})

@api.depends('l10n_br_linha_digitavel')
def _compute_barcode(self):
for item in self:
if not item.l10n_br_linha_digitavel:
continue
linha = re.sub('[^0-9]', '', item.l10n_br_linha_digitavel)
if len(linha) not in (47, 48):
raise UserError(
'Tamanho da linha digitável inválido %s' % len(linha))
vals = self._get_digitable_line_vals(linha)
item.l10n_br_barcode = vals['barcode']

@api.onchange('l10n_br_linha_digitavel')
def _onchange_linha_digitavel(self):
linha = re.sub('[^0-9]', '', self.l10n_br_linha_digitavel or '')
if len(linha) in (47, 48):
self.l10n_br_linha_digitavel = pretty_format_line(linha)
vals = self._get_digitable_line_vals(linha)
if self.line_ids:
self.line_ids[0].price_unit = vals.get('valor', 0.0)
else:
self.line_ids = [(0, 0, {
'quantity': 1.0,
'price_unit': vals.get('valor', 0.0)
})]
if vals.get('vencimento'):
self.date_due = vals.get('vencimento')

def _get_digitable_line_vals(self, digitable_line):
try:
return decode_digitable_line(digitable_line)
except DvNotValidError:
raise UserError("DV do código de Barras não confere!")

def prepare_payment_line_vals(self, move_line_id):
return {
'partner_id': self.partner_id.id,
Expand Down
6 changes: 6 additions & 0 deletions br_payment_invoice/views/account_invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<field name="l10n_br_payment_type" invisible="1" />
<field name="l10n_br_bank_account_id" attrs="{'invisible': [('l10n_br_payment_type', 'not in', ('01', '02'))]}" domain="[('partner_id', '=', partner_id)]"/>
</field>
<group position="inside">
<h3 attrs="{'invisible': [('l10n_br_payment_type', 'not in', ('03', '04', '08'))]}">
<strong><label for="l10n_br_linha_digitavel" string="Linha Digitável" /></strong>
<field name="l10n_br_linha_digitavel" />
</h3>
</group>
<tree name="vencimentos" position="inside">
<button name="open_wizard_schedule_payment" type="object" string="AGENDAR" class="btn btn-success btn-xs"/>
</tree>
Expand Down

0 comments on commit 04b8c27

Please sign in to comment.