|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +from datetime import datetime |
| 4 | + |
3 | 5 | from bs4 import BeautifulSoup
|
4 | 6 |
|
5 |
| -from .data_models import LegalRepresentative, TaxpayerProvidedInfo |
| 7 | +from .data_models import LastFiledDocument, LegalRepresentative, TaxpayerData, TaxpayerProvidedInfo |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def parse_taxpayer_provided_info(html_content: str) -> TaxpayerProvidedInfo:
|
@@ -89,3 +91,88 @@ def parse_taxpayer_provided_info(html_content: str) -> TaxpayerProvidedInfo:
|
89 | 91 | company_formation=company_formation,
|
90 | 92 | participation_in_existing_companies=participation_in_companies,
|
91 | 93 | )
|
| 94 | + |
| 95 | + |
| 96 | +def parse_taxpayer_data(html_content: str) -> TaxpayerData: |
| 97 | + """ |
| 98 | + Parse the CTE HTML content to extract the content of the section: |
| 99 | + "Datos del Contribuyente" |
| 100 | +
|
| 101 | + Args: |
| 102 | + html_content: HTML string containing the taxpayer information table |
| 103 | +
|
| 104 | + Returns: |
| 105 | + TaxpayerData instance with the parsed data |
| 106 | + """ |
| 107 | + soup = BeautifulSoup(html_content, 'html.parser') |
| 108 | + table = soup.find('table', id='tbl_dbcontribuyente') |
| 109 | + if not table: |
| 110 | + raise ValueError("Could not find 'Datos del Contribuyente' table in HTML") |
| 111 | + |
| 112 | + fecha_inicio_elem = table.find(id='td_fecha_inicio') # type: ignore[attr-defined] |
| 113 | + if fecha_inicio_elem: |
| 114 | + start_of_activities_date = datetime.strptime( |
| 115 | + fecha_inicio_elem.get_text(strip=True), "%d-%m-%Y" |
| 116 | + ).date() |
| 117 | + else: |
| 118 | + start_of_activities_date = None |
| 119 | + |
| 120 | + actividades_elem = table.find(id='td_actividades') # type: ignore[attr-defined] |
| 121 | + if actividades_elem: |
| 122 | + economic_activities = actividades_elem.get_text(separator="\n", strip=True) |
| 123 | + else: |
| 124 | + economic_activities = "" |
| 125 | + |
| 126 | + categoria_elem = table.find(id='td_categoria') # type: ignore[attr-defined] |
| 127 | + if categoria_elem: |
| 128 | + tax_category = categoria_elem.get_text(strip=True) |
| 129 | + else: |
| 130 | + tax_category = "" |
| 131 | + |
| 132 | + domicilio_elem = table.find(id='td_domicilio') # type: ignore[attr-defined] |
| 133 | + if domicilio_elem: |
| 134 | + address = domicilio_elem.get_text(strip=True) |
| 135 | + else: |
| 136 | + address = "" |
| 137 | + |
| 138 | + # Sucursales |
| 139 | + branches = [] |
| 140 | + sucursales_row = table.find( # type: ignore[attr-defined] |
| 141 | + 'td', |
| 142 | + string=lambda s: s and 'Sucursales:' in s, |
| 143 | + ) |
| 144 | + if sucursales_row: |
| 145 | + sucursales_td = sucursales_row.find_next_sibling('td') |
| 146 | + if sucursales_td: |
| 147 | + branches_text = sucursales_td.get_text(separator="\n", strip=True) |
| 148 | + branches = [b for b in branches_text.split("\n") if b] |
| 149 | + |
| 150 | + # Últimos documentos timbrados |
| 151 | + last_filed_documents = [] |
| 152 | + tim_nombre_elem = table.find(id='td_tim_nombre') # type: ignore[attr-defined] |
| 153 | + tim_fecha_elem = table.find(id='td_tim_fecha') # type: ignore[attr-defined] |
| 154 | + if tim_nombre_elem and tim_fecha_elem: |
| 155 | + names = tim_nombre_elem.get_text(separator="\n", strip=True).split("\n") |
| 156 | + dates = tim_fecha_elem.get_text(separator="\n", strip=True).split("\n") |
| 157 | + for name, date_str in zip(names, dates): |
| 158 | + if name and date_str: |
| 159 | + doc_date = datetime.strptime(date_str, "%d-%m-%Y").date() |
| 160 | + last_filed_documents.append(LastFiledDocument(name=name, date=doc_date)) |
| 161 | + |
| 162 | + # Observaciones tributarias |
| 163 | + tax_observations = None |
| 164 | + observaciones_elem = table.find(id='td_observaciones') # type: ignore[attr-defined] |
| 165 | + if observaciones_elem: |
| 166 | + obs_span = observaciones_elem.find('span', class_='textof') |
| 167 | + if obs_span: |
| 168 | + tax_observations = obs_span.get_text(strip=True) |
| 169 | + |
| 170 | + return TaxpayerData( |
| 171 | + start_of_activities_date=start_of_activities_date, |
| 172 | + economic_activities=economic_activities, |
| 173 | + tax_category=tax_category, |
| 174 | + Address=address, |
| 175 | + branches=branches, |
| 176 | + last_filed_documents=last_filed_documents, |
| 177 | + tax_observations=tax_observations, |
| 178 | + ) |
0 commit comments