diff --git a/l10n_br_account_payment_order/demo/account_invoice.xml b/l10n_br_account_payment_order/demo/account_invoice.xml index 0cfa23aaf51d..e8930f9a1293 100644 --- a/l10n_br_account_payment_order/demo/account_invoice.xml +++ b/l10n_br_account_payment_order/demo/account_invoice.xml @@ -11,6 +11,7 @@ out_invoice + TESTE Intruções Boleto @@ -31,6 +32,7 @@ out_invoice + TESTE Intruções Boleto @@ -51,6 +53,7 @@ out_invoice + TESTE Intruções Boleto @@ -71,6 +74,7 @@ out_invoice + TESTE Intruções Boleto @@ -91,6 +95,7 @@ out_invoice + TESTE Intruções Boleto @@ -111,6 +116,7 @@ out_invoice + TESTE Intruções Boleto diff --git a/l10n_br_account_payment_order/demo/account_payment_order.xml b/l10n_br_account_payment_order/demo/account_payment_order.xml index c8a5416f7882..972b3c9a6a56 100644 --- a/l10n_br_account_payment_order/demo/account_payment_order.xml +++ b/l10n_br_account_payment_order/demo/account_payment_order.xml @@ -1,6 +1,11 @@ + diff --git a/l10n_br_account_payment_order/tests/test_payment_order.py b/l10n_br_account_payment_order/tests/test_payment_order.py index 7f4de3f3c649..13e244c9cea6 100644 --- a/l10n_br_account_payment_order/tests/test_payment_order.py +++ b/l10n_br_account_payment_order/tests/test_payment_order.py @@ -4,7 +4,7 @@ from odoo.tests import tagged from odoo.tests import SavepointCase -from odoo.exceptions import ValidationError, UserError +from odoo.exceptions import ValidationError @tagged('post_install', '-at_install') @@ -14,118 +14,52 @@ class TestPaymentOrder(SavepointCase): def setUpClass(self): super().setUpClass() - # Get Invoice for test - self.invoice_cef = self.env.ref( + # Product Tax Boleto + self.invoice_product_tax_boleto = self.env.ref( 'l10n_br_account_payment_order.' - 'demo_invoice_payment_order_cef_cnab240' + 'demo_invoice_payment_order_bb_cnab400' ) - def test_create_payment_order(self): - """ Test Create Payment Order """ + # Product Tax Boleto + self.invoice_cheque = self.env.ref( + 'l10n_br_account_payment_order.demo_invoice_payment_order_cheque' + ) - # I check that Initially customer invoice is in the "Draft" state - self.assertEquals(self.invoice_cef.state, 'draft') + def test_product_tax_boleto(self): + """ Test Invoice where Payment Mode has Product Tax. """ + self.invoice_product_tax_boleto._onchange_payment_mode_id() - # I validate invoice by creating on - self.invoice_cef.action_invoice_open() + # Produto Taxa adicionado + line_product_tax = self.invoice_product_tax_boleto. \ + invoice_line_ids.filtered( + lambda l: l.product_id == self.invoice_product_tax_boleto. + payment_mode_id.product_tax_id) + self.assertEquals(len(line_product_tax), 1) + # I validate invoice by creating on + self.invoice_product_tax_boleto.action_invoice_open() # I check that the invoice state is "Open" - self.assertEquals(self.invoice_cef.state, 'open') - - # I check that now there is a move attached to the invoice - assert self.invoice_cef.move_id, \ - "Move not created for open invoice" - - payment_order = self.env['account.payment.order'].search([ - ('payment_mode_id', '=', self.invoice_cef.payment_mode_id.id) - ]) - - assert payment_order, "Payment Order not created." - - # TODO: Caso CNAB pode cancelar o Move ? - # Aparetemente isso precisa ser validado - # Change status of Move to draft just to test - self.invoice_cef.move_id.button_cancel() - - for line in self.invoice_cef.move_id.line_ids.filtered( - lambda l: l.account_id.id == self.invoice_cef.account_id.id): - self.assertEquals( - line.journal_entry_ref, line.invoice_id.name, - "Error with compute field journal_entry_ref") - - # Return the status of Move to Posted - self.invoice_cef.move_id.action_post() - - # Verificar os campos CNAB na account.move.line - for line in self.invoice_cef.move_id.line_ids.filtered( - lambda l: l.account_id.id == self.invoice_cef.account_id.id): - assert line.own_number,\ - 'own_number field is not filled in created Move Line.' - assert line.mov_instruction_code_id, \ - 'mov_instruction_code_id field is not filled in created Move Line.' - self.assertEquals( - line.journal_entry_ref, line.invoice_id.name, - 'Error with compute field journal_entry_ref') - # testar com a parcela 700 - if line.debit == 700.0: - test_balance_value = line.get_balance() - - self.assertEquals( - test_balance_value, 700.0, - 'Error with method get_balance()') + self.assertEquals(self.invoice_product_tax_boleto.state, 'open') + def test_payment_mode_without_payment_order(self): + """ Test Invoice when Payment Mode not generate Payment Order. """ + self.invoice_cheque._onchange_payment_mode_id() + # I validate invoice by creating on + self.invoice_cheque.action_invoice_open() + # I check that the invoice state is "Open" + self.assertEquals(self.invoice_cheque.state, 'open') payment_order = self.env['account.payment.order'].search([ - ('payment_mode_id', '=', self.invoice_cef.payment_mode_id.id) + ('payment_mode_id', '=', self.invoice_cheque.payment_mode_id.id) ]) - - # Verifica os campos CNAB na linhas de pagamentos - for l in payment_order.payment_line_ids: - assert l.own_number, \ - 'own_number field is not filled in Payment Line.' - assert l.mov_instruction_code_id, \ - 'mov_instruction_code_id field are not filled in Payment Line.' - - # Ordem de Pagto CNAB não pode ser apagada - with self.assertRaises(UserError): - payment_order.unlink() - - # Open payment order - payment_order.draft2open() - - # Criação da Bank Line - self.assertEquals(len(payment_order.bank_line_ids), 2) - - # A geração do arquivo é feita pelo modulo que implementa a - # biblioteca a ser usada - # Generate and upload - # payment_order.open2generated() - # payment_order.generated2uploaded() - - self.assertEquals(payment_order.state, 'open') - - # Verifica os campos CNAB na linhas de bancarias - for l in payment_order.bank_line_ids: - assert l.own_number, \ - 'own_number field is not filled in Payment Line.' - assert l.mov_instruction_code_id, \ - 'mov_instruction_code_id field are not filled in Payment Line.' - - # Ordem de Pagto CNAB não pode ser Cancelada - with self.assertRaises(UserError): - payment_order.action_done_cancel() - - # Testar Cancelamento - # with self.assertRaises(UserError): - self.invoice_cef.action_invoice_cancel() + self.assertEquals(len(payment_order), 0) def test_bra_number_constrains(self): """ Test bra_number constrains. """ self.banco_bradesco = self.env[ 'res.bank'].search([('code_bc', '=', '033')]) with self.assertRaises(ValidationError): - self.env[ - 'res.partner.bank'].create(dict( - bank_id=self.banco_bradesco.id, - partner_id=self.ref('l10n_br_base.res_partner_akretion'), - bra_number='12345' - )) + self.env['res.partner.bank'].create(dict( + bank_id=self.banco_bradesco.id, + partner_id=self.ref('l10n_br_base.res_partner_akretion'), + bra_number='12345' + )) diff --git a/l10n_br_account_payment_order/tests/test_payment_order_inbound.py b/l10n_br_account_payment_order/tests/test_payment_order_inbound.py index b652fbd9570b..94b32e1743d0 100644 --- a/l10n_br_account_payment_order/tests/test_payment_order_inbound.py +++ b/l10n_br_account_payment_order/tests/test_payment_order_inbound.py @@ -4,9 +4,11 @@ # Luis Felipe Mileo # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) +import time from odoo.tests import tagged from odoo.tests import SavepointCase +from odoo.exceptions import UserError @tagged('post_install', '-at_install') @@ -16,43 +18,191 @@ class TestPaymentOrderInbound(SavepointCase): def setUpClass(self): super().setUpClass() - # Product Tax Boleto - self.invoice_product_tax_boleto = self.env.ref( - 'l10n_br_account_payment_order.demo_invoice_payment_order_bb_cnab400' - ) + self.register_payments_model = \ + self.env['account.register.payments'].with_context( + active_model='account.invoice') + self.payment_model = self.env['account.payment'] - # Product Tax Boleto - self.invoice_cheque = self.env.ref( - 'l10n_br_account_payment_order.demo_invoice_payment_order_cheque' + # Get Invoice for test + self.invoice_cef = self.env.ref( + 'l10n_br_account_payment_order.' + 'demo_invoice_payment_order_cef_cnab240' + ) + self.invoice_unicred = self.env.ref( + 'l10n_br_account_payment_order.' + 'demo_invoice_payment_order_unicred_cnab400' ) - def test_product_tax_boleto(self): - """ Test Invoice where Payment Mode has Product Tax. """ - self.invoice_product_tax_boleto._onchange_payment_mode_id() + # Journal + self.journal_cash = self.env[ + 'account.journal'].search([('type', '=', 'cash')], limit=1) + self.payment_method_manual_in = \ + self.env.ref('account.account_payment_method_manual_in') + + def test_create_payment_order(self): + """ Test Create Payment Order """ - # Produto Taxa adicionado - line_product_tax = self.invoice_product_tax_boleto.\ - invoice_line_ids.filtered( - lambda l: l.product_id == self.invoice_product_tax_boleto. - payment_mode_id.product_tax_id) + # I check that Initially customer invoice is in the "Draft" state + self.assertEquals(self.invoice_cef.state, 'draft') - self.assertEquals(len(line_product_tax), 1) # I validate invoice by creating on - self.invoice_product_tax_boleto.action_invoice_open() + self.invoice_cef.action_invoice_open() + # I check that the invoice state is "Open" - self.assertEquals(self.invoice_product_tax_boleto.state, 'open') + self.assertEquals(self.invoice_cef.state, 'open') + + # I check that now there is a move attached to the invoice + assert self.invoice_cef.move_id, \ + "Move not created for open invoice" + + payment_order = self.env['account.payment.order'].search([ + ('payment_mode_id', '=', self.invoice_cef.payment_mode_id.id) + ]) + + assert payment_order, "Payment Order not created." + + # TODO: Caso CNAB pode cancelar o Move ? + # Aparetemente isso precisa ser validado + # Change status of Move to draft just to test + self.invoice_cef.move_id.button_cancel() + + for line in self.invoice_cef.move_id.line_ids.filtered( + lambda l: l.account_id.id == self.invoice_cef.account_id.id): + self.assertEquals( + line.journal_entry_ref, line.invoice_id.name, + "Error with compute field journal_entry_ref") + + # Return the status of Move to Posted + self.invoice_cef.move_id.action_post() + + # Verificar os campos CNAB na account.move.line + for line in self.invoice_cef.move_id.line_ids.filtered( + lambda l: l.account_id.id == self.invoice_cef.account_id.id): + assert line.own_number, \ + 'own_number field is not filled in created Move Line.' + assert line.mov_instruction_code_id, \ + 'mov_instruction_code_id field is not filled' \ + ' in created Move Line.' + self.assertEquals( + line.journal_entry_ref, line.invoice_id.name, + 'Error with compute field journal_entry_ref') + # testar com a parcela 700 + if line.debit == 700.0: + test_balance_value = line.get_balance() + + self.assertEquals( + test_balance_value, 700.0, + 'Error with method get_balance()') + + payment_order = self.env['account.payment.order'].search([ + ('payment_mode_id', '=', self.invoice_cef.payment_mode_id.id) + ]) + + # Verifica os campos CNAB na linhas de pagamentos + for l in payment_order.payment_line_ids: + assert l.own_number, \ + 'own_number field is not filled in Payment Line.' + assert l.mov_instruction_code_id, \ + 'mov_instruction_code_id field are not filled in Payment Line.' + + # Ordem de Pagto CNAB não pode ser apagada + with self.assertRaises(UserError): + payment_order.unlink() + + # Open payment order + payment_order.draft2open() + + # Criação da Bank Line + self.assertEquals(len(payment_order.bank_line_ids), 2) - def test_payment_mode_without_payment_order(self): - """ Test Invoice when Payment Mode not generate Payment Order. """ - self.invoice_cheque._onchange_payment_mode_id() + # A geração do arquivo é feita pelo modulo que implementa a + # biblioteca a ser usada + # Generate and upload + # payment_order.open2generated() + # payment_order.generated2uploaded() + + self.assertEquals(payment_order.state, 'open') + + # Verifica os campos CNAB na linhas de bancarias + for l in payment_order.bank_line_ids: + assert l.own_number, \ + 'own_number field is not filled in Payment Line.' + assert l.mov_instruction_code_id, \ + 'mov_instruction_code_id field are not filled in Payment Line.' + + # Ordem de Pagto CNAB não pode ser Cancelada + with self.assertRaises(UserError): + payment_order.action_done_cancel() + + # Testar Cancelamento + self.invoice_cef.action_invoice_cancel() + + def test_payment_outside_cnab_payment_order_draft(self): + """ + Caso de Pagamento ser feito quando a Ordem de Pagamento em Draft deve + apagar as linhas de pagamentos. + """ # I validate invoice by creating on - self.invoice_cheque.action_invoice_open() - # I check that the invoice state is "Open" - self.assertEquals(self.invoice_cheque.state, 'open') + self.invoice_unicred.action_invoice_open() + payment_order = self.env['account.payment.order'].search([ - ('payment_mode_id', '=', self.invoice_cheque.payment_mode_id.id) + ('payment_mode_id', '=', self.invoice_unicred.payment_mode_id.id) ]) - self.assertEquals(len(payment_order), 0) + self.assertEqual(len(payment_order.payment_line_ids), 2) + + ctx = { + 'active_model': 'account.invoice', + 'active_ids': [self.invoice_unicred.id] + } + register_payments = \ + self.register_payments_model.with_context(ctx).create({ + 'payment_date': time.strftime('%Y') + '-07-15', + 'journal_id': self.journal_cash.id, + 'payment_method_id': self.payment_method_manual_in.id, + }) + + # Caso a Ordem de Pagamento ainda não esteja Confirmada + register_payments.create_payments() + payment = self.payment_model.search([], order="id desc", limit=1) + + self.assertAlmostEquals(payment.amount, 1000) + self.assertEqual(payment.state, 'posted') + self.assertEqual(self.invoice_unicred.state, 'paid') + # Linhas Apagadas + self.assertEqual(len(payment_order.payment_line_ids), 0) + + def test_payment_outside_cnab_payment_order_open(self): + """ + Caso de Pagamento ser feito quando a Ordem de Pagamento em Open deve + gerar erro por ter uma Instrução CNAB a ser enviada. + """ + # I validate invoice by creating on + self.invoice_unicred.action_invoice_open() + + payment_order = self.env['account.payment.order'].search([ + ('payment_mode_id', '=', self.invoice_unicred.payment_mode_id.id) + ]) + # Open payment order + payment_order.draft2open() + self.assertEqual(payment_order.state, 'open') + + ctx = { + 'active_model': 'account.invoice', + 'active_ids': [self.invoice_unicred.id] + } + register_payments = \ + self.register_payments_model.with_context(ctx).create({ + 'payment_date': time.strftime('%Y') + '-07-15', + 'journal_id': self.journal_cash.id, + 'payment_method_id': self.payment_method_manual_in.id, + }) + + # Erro de ter uma Instrução CNAB Pendente, como não é possivel gerar a + # Ordem de Pagto o teste de crição de Write Off e Alteração do Valor do + # Titulo no caso de um pagamento parcial precisa ser feito no modulo + # que implementa biblioteca a ser usada. + with self.assertRaises(UserError): + register_payments.create_payments() def test_payment_inbound_change_due_date(self): """ Change account.move.line due date. Automatic add this aml to a new