Skip to content

Commit

Permalink
Added support for buyer_tin
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzadelic committed May 21, 2024
1 parent 7a433a3 commit a817491
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sr_invoice_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__author__ = "Innovigo"
__website__ = "https://wwwinnovigo.co/"
__email__ = "[email protected]"
__version__ = "1.0.0"
__version__ = "1.0.1"

VERSION = __version__

Expand Down
13 changes: 13 additions & 0 deletions sr_invoice_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(",", "."))

Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a817491

Please sign in to comment.