diff --git a/l10n_br_purchase_stock/demo/purchase_order.xml b/l10n_br_purchase_stock/demo/purchase_order.xml index 2f47face97aa..d5760ef7634d 100644 --- a/l10n_br_purchase_stock/demo/purchase_order.xml +++ b/l10n_br_purchase_stock/demo/purchase_order.xml @@ -97,6 +97,46 @@ + + + + TEST SECTION 1 + line_section + 0 + + + + + + TEST NOTE 1 + line_note + 0 + + + + + + 2 + + 500 + + + + 999999 + 003 + Teste - Additional Data + 10 + 10 + 10 + + + + + + Main l10n_br_purchase_stock - teste agrupamento @@ -170,6 +210,46 @@ + + + + TEST SECTION 2 + line_section + 0 + + + + + + TEST NOTE 2 + line_note + 0 + + + + + + 2 + + 500 + + + + 999999 + 003 + Teste - Additional Data + 10 + 10 + 10 + + + + + + diff --git a/l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock.py b/l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock.py index d336f63b2034..fce9ffbb07ea 100644 --- a/l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock.py +++ b/l10n_br_purchase_stock/tests/test_l10n_br_purchase_stock.py @@ -52,7 +52,7 @@ def test_grouping_pickings(self): self.assertIn(picking_2, invoice.picking_ids) # Validar o price_unit usado - for inv_line in invoice.invoice_line_ids: + for inv_line in invoice.invoice_line_ids.filtered(lambda ln: ln.product_id): # TODO: A forma de instalação dos modulos feita no CI # falha o browse aqui # l10n_br_stock_account/models/stock_invoice_onshipping.py:105 @@ -72,6 +72,17 @@ def test_grouping_pickings(self): inv_line.fiscal_operation_line_id, "Missing Fiscal Operation Line." ) + # Section Lines + section_lines = invoice.invoice_line_ids.filtered( + lambda ln: ln.display_type == "line_section" + ) + self.assertEqual(len(section_lines), 2) + # Note Lines + note_lines = invoice.invoice_line_ids.filtered( + lambda ln: ln.display_type == "line_note" + ) + self.assertEqual(len(note_lines), 2) + if hasattr(invoice, "document_serie"): invoice.document_serie = "1" invoice.document_number = "123" @@ -150,7 +161,7 @@ def test_purchase_order_lucro_presumido(self): invoice = self.create_invoice_wizard(picking) # Validar o price_unit usado - for inv_line in invoice.invoice_line_ids: + for inv_line in invoice.invoice_line_ids.filtered(lambda ln: ln.product_id): # TODO: A forma de instalação dos modulos feita no CI # falha o browse aqui # l10n_br_stock_account/models/stock_invoice_onshipping.py:105 diff --git a/l10n_br_purchase_stock/wizards/stock_invocing_onshipping.py b/l10n_br_purchase_stock/wizards/stock_invocing_onshipping.py index fc8f86f1b24b..491d19ffffe4 100644 --- a/l10n_br_purchase_stock/wizards/stock_invocing_onshipping.py +++ b/l10n_br_purchase_stock/wizards/stock_invocing_onshipping.py @@ -158,3 +158,67 @@ def _get_invoice_line_values(self, moves, invoice_values, invoice): values.update(purchase_line_values_rm) return values + + def _create_invoice(self, invoice_values): + """Override this method if you need to change any values of the + invoice and the lines before the invoice creation + :param invoice_values: dict with the invoice and its lines + :return: invoice + """ + purchase = self.env["purchase.order"].browse(invoice_values.get("purchase_id")) + pickings = self._load_pickings() + purchase_pickings = pickings.filtered(lambda pk: pk.purchase_id) + if not purchase_pickings or self._get_invoice_type() == "in_refund": + return super()._create_invoice(invoice_values) + + # Check Other Purchase Lines + section_note_lines = self.env["purchase.order.line"] + # Resequencing + invoice_item_sequence = 10 + invoice_item_seq_dict = {} + for picking in purchase_pickings.sorted(key=lambda p: p.name): + purchase = picking.purchase_id + # Resequencing + for line in purchase.order_line: + invoice_item_seq_dict[line.id] = invoice_item_sequence + invoice_item_sequence += 1 + + # Section and Note Lines + section_note_lines |= purchase.order_line.filtered( + lambda ln: ln.display_type in ("line_section", "line_note") + ) + + for line in section_note_lines: + line_vals = line._prepare_account_move_line() + invoice_values["invoice_line_ids"].append((0, 0, line_vals)) + + # Resequence + for ln in invoice_values["invoice_line_ids"]: + if ln[0] != 5: + if ln[2] and ln[2].get("purchase_line_id"): + ln[2].update( + { + "sequence": invoice_item_seq_dict.get( + ln[2].get("purchase_line_id") + ) + } + ) + + # 3) Create invoices. + moves = self.env["account.move"] + AccountMove = self.env["account.move"].with_context( + default_move_type="in_invoice" + ) + # for vals in invoice_vals_list: + moves |= AccountMove.with_company(self.env.company).create(invoice_values) + + # 4) Some moves might actually be refunds: convert them if the + # total amount is negative + # We do this after the moves have been created since we need taxes, + # etc. to know if the total + # is actually negative or not + moves.filtered( + lambda m: m.currency_id.round(m.amount_total) < 0 + ).action_switch_invoice_into_refund_credit_note() + + return moves