Skip to content

Commit

Permalink
[REF] Make wizard methods private
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokibastos committed Oct 24, 2023
1 parent c9671f3 commit 4881717
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
18 changes: 9 additions & 9 deletions l10n_br_nfe/tests/test_nfe_import_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def test_import_nfe_xml(self):
mock_document = MagicMock(spec=["modelo_documento"])
mock_document.modelo_documento = "65"
with patch.object(
NfeImport, "get_document_by_xml", return_value=mock_document
NfeImport, "_get_document_by_xml", return_value=mock_document
), self.assertRaises(UserError):
self.wizard.check_xml_data(self.wizard.parse_xml())
self.wizard._check_xml_data(self.wizard._parse_xml())

self._prepare_wizard(self.xml_1)
self.wizard.import_xml()
Expand Down Expand Up @@ -105,26 +105,26 @@ def test_create_edoc_from_xml(self):
self._prepare_wizard(self.xml_1)

self.wizard.partner_id = False
edoc = self.wizard.create_edoc_from_xml()
edoc = self.wizard._create_edoc_from_xml()
self.assertEqual(self.wizard.partner_id, edoc.partner_id)

self.check_edoc(edoc)

def test_set_fiscal_operation_type(self):
self._prepare_wizard(self.xml_1)

doc = self.wizard.get_document_by_xml(self.wizard.parse_xml())
doc = self.wizard._get_document_by_xml(self.wizard._parse_xml())
origin_company = self.wizard.company_id

doc_company_id = self.env["res.company"].search(
[("nfe40_CNPJ", "=", re.sub("[^0-9]", "", doc.cnpj_cpf_emitente))], limit=1
)
self.wizard.company_id = doc_company_id
self.wizard.set_fiscal_operation_type()
self.wizard._set_fiscal_operation_type()
self.assertEqual(self.wizard.fiscal_operation_type, "out")

self.wizard.company_id = origin_company
self.wizard.set_fiscal_operation_type()
self.wizard._set_fiscal_operation_type()
self.assertEqual(self.wizard.fiscal_operation_type, "in")

def test_imported_products(self):
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_imported_products(self):
def test_match_xml_product(self):
self._prepare_wizard(self.xml_1)

xml = self.wizard.parse_xml()
xml = self.wizard._parse_xml()
xml_product_1 = xml.NFe.infNFe.det[0].prod
prod_id = self.wizard._match_product(xml_product_1)
self.assertEqual(prod_id, self.env.ref("product.product_product_10"))
Expand Down Expand Up @@ -197,13 +197,13 @@ def test_match_xml_product(self):
prod_id = self.wizard._match_product(MagicMock())
self.assertFalse(prod_id)

def test_parse_xml(self):
def test__parse_xml(self):
self._prepare_wizard(self.xml_1)

first_product = self.wizard.imported_products_ids[0]
first_product.new_cfop_id = self.env.ref("l10n_br_fiscal.cfop_5111").id

xml = self.wizard.parse_xml()
xml = self.wizard._parse_xml()
first_xml_product = xml.NFe.infNFe.det[0].prod
self.assertEqual(first_xml_product.CFOP, "5111")

Expand Down
44 changes: 22 additions & 22 deletions l10n_br_nfe/wizards/import_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class NfeImport(models.TransientModel):
@api.onchange("xml")
def _onchange_xml(self):
if self.xml:
self.set_fields_by_xml_data()
self._set_fields_by_xml_data()

def set_fields_by_xml_data(self):
parsed_xml = self.parse_xml()
document = self.get_document_by_xml(parsed_xml)
def _set_fields_by_xml_data(self):
parsed_xml = self._parse_xml()
document = self._get_document_by_xml(parsed_xml)
infNFe = parsed_xml.NFe.infNFe

self.find_existing_document()
self.check_xml_data(parsed_xml)
self._find_existing_document()
self._check_xml_data(parsed_xml)
self.document_key = document.chave
self.document_number = int(document.numero_documento)
self.document_serie = int(document.numero_serie)
Expand All @@ -60,21 +60,21 @@ def set_fields_by_xml_data(self):
limit=1,
)
self.nat_op = infNFe.ide.natOp
self.create_imported_products_by_xml()
self._create_imported_products_by_xml()

def parse_xml(self):
def _parse_xml(self):
try:
binding = TnfeProc.from_xml(base64.b64decode(self.xml).decode())
except Exception as e:
raise UserError(_("Invalid file: %s" % e))
else:
return self._edit_parsed_xml(binding)

def get_document_by_xml(self, xml):
def _get_document_by_xml(self, xml):
return detectar_chave_edoc(xml.NFe.infNFe.Id[3:])

def check_xml_data(self, xml):
document = self.get_document_by_xml(xml)
def _check_xml_data(self, xml):
document = self._get_document_by_xml(xml)
nfe_model_code = self.env.ref("l10n_br_fiscal.document_55").code

if document.modelo_documento != nfe_model_code:
Expand All @@ -85,8 +85,8 @@ def check_xml_data(self, xml):
)
)

def create_imported_products_by_xml(self):
xml = self.parse_xml()
def _create_imported_products_by_xml(self):
xml = self._parse_xml()
product_ids = []
for product in xml.NFe.infNFe.det:
product_ids.append(
Expand Down Expand Up @@ -196,24 +196,24 @@ def _get_taxes_from_xml_product(self, product):
return {"vICMS": vICMS, "pICMS": pICMS, "vIPI": vIPI, "pIPI": pIPI}

def import_xml(self):
self.find_existing_document()
self._find_existing_document()
if not self.document_id:
self.document_id = self.create_edoc_from_xml()
self.document_id = self._create_edoc_from_xml()

return self.action_open_document()

def find_existing_document(self):
document = self.get_document_by_xml(self.parse_xml())
def _find_existing_document(self):
document = self._get_document_by_xml(self._parse_xml())
self.document_id = self.env["l10n_br_fiscal.document"].search(
[("document_key", "=", document.chave)],
limit=1,
)

def create_edoc_from_xml(self):
self.set_fiscal_operation_type()
def _create_edoc_from_xml(self):
self._set_fiscal_operation_type()

edoc = self.env["l10n_br_fiscal.document"].import_nfe_xml(
self.parse_xml(),
self._parse_xml(),
edoc_type=self.fiscal_operation_type,
)

Expand All @@ -225,8 +225,8 @@ def create_edoc_from_xml(self):

return edoc

def set_fiscal_operation_type(self):
document = self.get_document_by_xml(self.parse_xml())
def _set_fiscal_operation_type(self):
document = self._get_document_by_xml(self._parse_xml())

if document.cnpj_cpf_emitente == self.company_id.cnpj_cpf:
self.fiscal_operation_type = "out"
Expand Down

0 comments on commit 4881717

Please sign in to comment.