diff --git a/README.md b/README.md index a98ef95..13bcf69 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ The `InvoiceParser` class is the entry point for using the parser. - `get_data()` - Extracts all the data from the invoice and returns it as a dictionary - `get_company_name()` - Extracts the company name. - `get_company_tin()` - Extracts the company's tax identification number/PFR. +- `get_buyer_tin()` - Extracts the buyer's tax identification number/PFR. - `get_total_amount()` - Extracts the total amount of the invoice. - `get_dt()` - Extracts the date and time of the invoice and converts it to UTC as a datetime object. - `get_invoice_number()` - Extracts the invoice number. @@ -43,6 +44,7 @@ parser.data() parser.get_company_name() parser.get_company_tin() +parser.get_buyer_tin() parser.get_total_amount() parser.get_dt() parser.get_invoice_number() @@ -57,6 +59,7 @@ parser.get_items() { "company_name": "Company Name", "company_tin": "123456789", + "buyer_tin": "987654321", "invoice_number": "QWERTYU1-QWERTYU1-12345", "invoice_datetime": datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc), "invoice_total_amount": 123.45, diff --git a/sr_invoice_parser/__init__.py b/sr_invoice_parser/__init__.py index 527295c..b2048c4 100644 --- a/sr_invoice_parser/__init__.py +++ b/sr_invoice_parser/__init__.py @@ -4,7 +4,7 @@ __author__ = "Innovigo" __website__ = "https://wwwinnovigo.co/" __email__ = "hello@innovigo.co" -__version__ = "1.0.0" +__version__ = "1.0.1" VERSION = __version__ diff --git a/sr_invoice_parser/parser.py b/sr_invoice_parser/parser.py index 332e755..9366d15 100644 --- a/sr_invoice_parser/parser.py +++ b/sr_invoice_parser/parser.py @@ -74,6 +74,17 @@ def get_company_tin(self) -> str: value = self.html_selector.css("span#tinLabel::text").get().strip() return value + @handle_exception() + def get_buyer_tin(self) -> str: + """Get the buyer tin/tax identification number""" + + value = self.html_selector.css("span#buyerIdLabel::text").get().strip() + if value: + value = value.split(":") + if len(value) == 2: + return value[1] + return value + def string_to_float(self, string: str) -> float: return float(string.replace(".", "").replace(",", ".")) @@ -196,6 +207,7 @@ def data(self) -> dict: company_name = self.get_company_name() company_tin = self.get_company_tin() + buyer_tin = self.get_buyer_tin() total_amount = self.get_total_amount() invoice_number = self.get_invoice_number() invoice_text = self.get_invoice_text() @@ -204,6 +216,7 @@ def data(self) -> dict: return { "company_name": company_name, "company_tin": company_tin, + "buyer_tin": buyer_tin, "invoice_number": invoice_number, "invoice_datetime": dt, "invoice_total_amount": total_amount, diff --git a/tests/test_parser.py b/tests/test_parser.py index d6c7f99..87d33a7 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -152,6 +152,20 @@ def test_get_company_tin(self, mock_get): parser = InvoiceParser(html_text=self.example_response) assert parser.get_company_tin() == value + @mock.patch("sr_invoice_parser.parser.requests.get") + def test_get_buyer_tin(self, mock_get): + # Create a mock response + mock_get.return_value = self.create_success_mock_response() + value = "987654321" + + # test with URL + parser = InvoiceParser(url="https://suf.purs.gov.rs/v/vl?") + assert parser.get_buyer_tin() == value + + # test with HTML content + parser = InvoiceParser(html_text=self.example_response) + assert parser.get_buyer_tin() == value + @mock.patch("sr_invoice_parser.parser.requests.get") def test_get_total_amount_failed(self, mock_get): # Create a mock response @@ -417,6 +431,7 @@ def test_get_data(self, mock_get): value = { "company_name": "Primer naziva firme", "company_tin": "123456789", + "buyer_tin": "987654321", "invoice_number": "QWERTYU1-QWERTYU1-12345", "invoice_datetime": datetime(2024, 4, 7, 15, 0, 30).replace(tzinfo=utc), "invoice_total_amount": 8960.0,