484 lines
14 KiB
Python
484 lines
14 KiB
Python
from flask import Flask, request, jsonify, render_template_string
|
|||
|
|
from datetime import date
|
||
|
|
import re
|
||
|
|
import requests
|
||
|
|
from requests import Session
|
||
|
|
from zeep import Client
|
||
|
|
from zeep.transports import Transport
|
||
|
|
|
||
|
|
app = Flask(__name__)
|
||
|
|
|
||
|
|
VIES_WSDL_URL = "https://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl"
|
||
|
|
POLISH_WL_API = "https://wl-api.mf.gov.pl/api/search/nip/{nip}?date={query_date}"
|
||
|
|
|
||
|
|
|
||
|
|
def get_vies_client():
|
||
|
|
session = Session()
|
||
|
|
transport = Transport(session=session, timeout=20)
|
||
|
|
return Client(wsdl=VIES_WSDL_URL, transport=transport)
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_vat(country_code: str, vat_number: str):
|
||
|
|
country_code = (country_code or "").strip().upper()
|
||
|
|
vat_number = (vat_number or "").strip().replace(" ", "")
|
||
|
|
if vat_number.upper().startswith(country_code):
|
||
|
|
vat_number = vat_number[len(country_code):]
|
||
|
|
return country_code, vat_number
|
||
|
|
|
||
|
|
|
||
|
|
def split_zip_city(text: str):
|
||
|
|
if not text:
|
||
|
|
return "", ""
|
||
|
|
text = " ".join(text.split())
|
||
|
|
m = re.match(r"^(\d{2}-\d{3})\s+(.+)$", text)
|
||
|
|
if m:
|
||
|
|
return m.group(1), m.group(2)
|
||
|
|
return "", text
|
||
|
|
|
||
|
|
|
||
|
|
def parse_polish_working_address(address: str):
|
||
|
|
if not address:
|
||
|
|
return "", "", ""
|
||
|
|
address = " ".join(address.split())
|
||
|
|
m = re.match(r"^(.*?)(\d{2}-\d{3})\s+(.+)$", address)
|
||
|
|
if m:
|
||
|
|
street = m.group(1).strip(" ,")
|
||
|
|
zip_code = m.group(2).strip()
|
||
|
|
city = m.group(3).strip()
|
||
|
|
return street, zip_code, city
|
||
|
|
return address, "", ""
|
||
|
|
|
||
|
|
|
||
|
|
def parse_vies_address(address: str):
|
||
|
|
if not address:
|
||
|
|
return "", "", ""
|
||
|
|
|
||
|
|
parts = [p.strip() for p in address.split(",") if p.strip()]
|
||
|
|
if not parts:
|
||
|
|
return "", "", ""
|
||
|
|
|
||
|
|
street = parts[0]
|
||
|
|
last_part = parts[-1]
|
||
|
|
|
||
|
|
# Common cases:
|
||
|
|
# "1000 Brussels"
|
||
|
|
# "00-838 Warszawa"
|
||
|
|
# "HR-10000 Zagreb"
|
||
|
|
m = re.match(r"^([A-Z]{0,2}-?\d[\dA-Z -]*)\s+(.+)$", last_part)
|
||
|
|
if m:
|
||
|
|
zip_code = m.group(1).strip()
|
||
|
|
city = m.group(2).strip()
|
||
|
|
return street, zip_code, city
|
||
|
|
|
||
|
|
return street, "", last_part
|
||
|
|
|
||
|
|
|
||
|
|
def check_vies(country_code: str, vat_number: str):
|
||
|
|
client = get_vies_client()
|
||
|
|
result = client.service.checkVat(countryCode=country_code, vatNumber=vat_number)
|
||
|
|
|
||
|
|
name = (result.name or "").strip()
|
||
|
|
address = (result.address or "").strip()
|
||
|
|
street, zip_code, city = parse_vies_address(address)
|
||
|
|
|
||
|
|
return {
|
||
|
|
"source": "vies",
|
||
|
|
"found": bool(result.valid),
|
||
|
|
"country": country_code,
|
||
|
|
"vat_number": vat_number,
|
||
|
|
"name": name if result.valid else "",
|
||
|
|
"street": street if result.valid else "",
|
||
|
|
"zip": zip_code if result.valid else "",
|
||
|
|
"city": city if result.valid else "",
|
||
|
|
"raw_address": address if result.valid else "",
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def check_polish_wl(nip: str):
|
||
|
|
today = date.today().isoformat()
|
||
|
|
url = POLISH_WL_API.format(nip=nip, query_date=today)
|
||
|
|
|
||
|
|
response = requests.get(url, headers={"Accept": "application/json"}, timeout=20)
|
||
|
|
response.raise_for_status()
|
||
|
|
|
||
|
|
data = response.json()
|
||
|
|
result = data.get("result", {})
|
||
|
|
subject = result.get("subject")
|
||
|
|
|
||
|
|
if not subject:
|
||
|
|
subjects = result.get("subjects") or []
|
||
|
|
subject = subjects[0] if subjects else None
|
||
|
|
|
||
|
|
if not subject:
|
||
|
|
return {
|
||
|
|
"source": "polish_wl",
|
||
|
|
"found": False,
|
||
|
|
"country": "PL",
|
||
|
|
"vat_number": nip,
|
||
|
|
"name": "",
|
||
|
|
"street": "",
|
||
|
|
"zip": "",
|
||
|
|
"city": "",
|
||
|
|
"raw_address": "",
|
||
|
|
}
|
||
|
|
|
||
|
|
name = (subject.get("name") or "").strip()
|
||
|
|
working_address = (subject.get("workingAddress") or "").strip()
|
||
|
|
street, zip_code, city = parse_polish_working_address(working_address)
|
||
|
|
|
||
|
|
return {
|
||
|
|
"source": "polish_wl",
|
||
|
|
"found": True,
|
||
|
|
"country": "PL",
|
||
|
|
"vat_number": nip,
|
||
|
|
"name": name,
|
||
|
|
"street": street,
|
||
|
|
"zip": zip_code,
|
||
|
|
"city": city,
|
||
|
|
"raw_address": working_address,
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/api/company_lookup")
|
||
|
|
def company_lookup():
|
||
|
|
country = request.args.get("country", "")
|
||
|
|
vat_number = request.args.get("vat_number", "")
|
||
|
|
|
||
|
|
country, vat_number = normalize_vat(country, vat_number)
|
||
|
|
|
||
|
|
if not country or not vat_number:
|
||
|
|
return jsonify({"error": "country and vat_number are required"}), 400
|
||
|
|
|
||
|
|
try:
|
||
|
|
vies_result = check_vies(country, vat_number)
|
||
|
|
except Exception as e:
|
||
|
|
vies_result = {
|
||
|
|
"source": "vies",
|
||
|
|
"found": False,
|
||
|
|
"country": country,
|
||
|
|
"vat_number": vat_number,
|
||
|
|
"name": "",
|
||
|
|
"street": "",
|
||
|
|
"zip": "",
|
||
|
|
"city": "",
|
||
|
|
"raw_address": "",
|
||
|
|
"warning": f"VIES lookup failed: {e}",
|
||
|
|
}
|
||
|
|
|
||
|
|
if vies_result.get("found"):
|
||
|
|
return jsonify(vies_result)
|
||
|
|
|
||
|
|
if country == "PL":
|
||
|
|
try:
|
||
|
|
polish_result = check_polish_wl(vat_number)
|
||
|
|
if "warning" in vies_result:
|
||
|
|
polish_result["warning"] = vies_result["warning"]
|
||
|
|
return jsonify(polish_result)
|
||
|
|
except Exception as e:
|
||
|
|
response = {
|
||
|
|
"source": "polish_wl",
|
||
|
|
"found": False,
|
||
|
|
"country": country,
|
||
|
|
"vat_number": vat_number,
|
||
|
|
"name": "",
|
||
|
|
"street": "",
|
||
|
|
"zip": "",
|
||
|
|
"city": "",
|
||
|
|
"raw_address": "",
|
||
|
|
"warning": vies_result.get("warning"),
|
||
|
|
"fallback_error": f"Polish WL lookup failed: {e}",
|
||
|
|
}
|
||
|
|
return jsonify(response)
|
||
|
|
|
||
|
|
return jsonify(vies_result)
|
||
|
|
|
||
|
|
|
||
|
|
HTML = """
|
||
|
|
<!DOCTYPE html>
|
||
|
|
<html lang="en">
|
||
|
|
<head>
|
||
|
|
<meta charset="UTF-8">
|
||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
|
<title>VAT Company Lookup</title>
|
||
|
|
<style>
|
||
|
|
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
|
||
|
|
|
||
|
|
:root {
|
||
|
|
--bg1: #eef6ff;
|
||
|
|
--bg2: #dcecff;
|
||
|
|
--panel: rgba(255, 255, 255, 0.9);
|
||
|
|
--blue: #1f5fae;
|
||
|
|
--blue-dark: #123d73;
|
||
|
|
--blue-soft: #6fa6e8;
|
||
|
|
--line: #4d8fdb;
|
||
|
|
--text: #1a2f4d;
|
||
|
|
--muted: #4f6f92;
|
||
|
|
--shadow: 0 18px 50px rgba(20, 71, 132, 0.16);
|
||
|
|
}
|
||
|
|
|
||
|
|
* {
|
||
|
|
box-sizing: border-box;
|
||
|
|
}
|
||
|
|
|
||
|
|
body {
|
||
|
|
margin: 0;
|
||
|
|
min-height: 100vh;
|
||
|
|
font-family: 'Inter', sans-serif;
|
||
|
|
font-size: 24px;
|
||
|
|
color: var(--text);
|
||
|
|
background:
|
||
|
|
radial-gradient(circle at top left, #f7fbff 0%, transparent 32%),
|
||
|
|
linear-gradient(135deg, var(--bg1), var(--bg2));
|
||
|
|
display: flex;
|
||
|
|
justify-content: center;
|
||
|
|
align-items: flex-start;
|
||
|
|
padding: 48px 24px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.container {
|
||
|
|
width: 100%;
|
||
|
|
max-width: 1100px;
|
||
|
|
background: var(--panel);
|
||
|
|
backdrop-filter: blur(8px);
|
||
|
|
border-radius: 28px;
|
||
|
|
box-shadow: var(--shadow);
|
||
|
|
padding: 40px 48px;
|
||
|
|
}
|
||
|
|
|
||
|
|
h1 {
|
||
|
|
margin: 0 0 36px 0;
|
||
|
|
color: var(--blue-dark);
|
||
|
|
font-size: 34px;
|
||
|
|
font-weight: 700;
|
||
|
|
letter-spacing: -0.5px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.form-grid {
|
||
|
|
display: flex;
|
||
|
|
flex-direction: column;
|
||
|
|
gap: 28px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.row {
|
||
|
|
display: grid;
|
||
|
|
grid-template-columns: 220px 1fr 1fr auto;
|
||
|
|
gap: 20px;
|
||
|
|
align-items: end;
|
||
|
|
}
|
||
|
|
|
||
|
|
label {
|
||
|
|
font-weight: 600;
|
||
|
|
color: var(--blue);
|
||
|
|
padding-bottom: 8px;
|
||
|
|
}
|
||
|
|
|
||
|
|
input, select {
|
||
|
|
width: 100%;
|
||
|
|
font-family: inherit;
|
||
|
|
font-size: 24px;
|
||
|
|
color: var(--text);
|
||
|
|
padding: 10px 4px;
|
||
|
|
border: none;
|
||
|
|
border-bottom: 2px solid var(--blue-soft);
|
||
|
|
background: transparent;
|
||
|
|
outline: none;
|
||
|
|
transition: border-color 0.2s ease;
|
||
|
|
border-radius: 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
input:focus, select:focus {
|
||
|
|
border-bottom-color: var(--blue);
|
||
|
|
box-shadow: none;
|
||
|
|
}
|
||
|
|
|
||
|
|
input::placeholder {
|
||
|
|
color: #7f9fc4;
|
||
|
|
}
|
||
|
|
|
||
|
|
button {
|
||
|
|
font-family: inherit;
|
||
|
|
font-size: 24px;
|
||
|
|
font-weight: 700;
|
||
|
|
padding: 14px 24px;
|
||
|
|
border: none;
|
||
|
|
border-radius: 16px;
|
||
|
|
cursor: pointer;
|
||
|
|
background: linear-gradient(135deg, #3b82d6, #1f5fae);
|
||
|
|
color: white;
|
||
|
|
box-shadow: 0 10px 24px rgba(31, 95, 174, 0.24);
|
||
|
|
transition: transform 0.12s ease, box-shadow 0.12s ease;
|
||
|
|
white-space: nowrap;
|
||
|
|
}
|
||
|
|
|
||
|
|
button:hover {
|
||
|
|
transform: translateY(-1px);
|
||
|
|
box-shadow: 0 14px 28px rgba(31, 95, 174, 0.28);
|
||
|
|
}
|
||
|
|
|
||
|
|
button:active {
|
||
|
|
transform: translateY(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
.button-row {
|
||
|
|
margin-top: 36px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.status {
|
||
|
|
margin-top: 28px;
|
||
|
|
padding: 20px 24px;
|
||
|
|
border-radius: 18px;
|
||
|
|
background: linear-gradient(135deg, rgba(204, 227, 255, 0.7), rgba(232, 243, 255, 0.95));
|
||
|
|
color: var(--muted);
|
||
|
|
white-space: pre-wrap;
|
||
|
|
font-size: 22px;
|
||
|
|
border: 1px solid rgba(77, 143, 219, 0.18);
|
||
|
|
}
|
||
|
|
|
||
|
|
@media (max-width: 900px) {
|
||
|
|
body {
|
||
|
|
padding: 20px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.container {
|
||
|
|
padding: 28px 24px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.row {
|
||
|
|
grid-template-columns: 1fr;
|
||
|
|
gap: 14px;
|
||
|
|
}
|
||
|
|
|
||
|
|
h1 {
|
||
|
|
font-size: 30px;
|
||
|
|
}
|
||
|
|
|
||
|
|
input, select, button {
|
||
|
|
font-size: 20px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.status {
|
||
|
|
font-size: 18px;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<div class="container">
|
||
|
|
<h1>VAT Company Lookup</h1>
|
||
|
|
|
||
|
|
<div class="form-grid">
|
||
|
|
<div class="row">
|
||
|
|
<label for="country">Country / VAT</label>
|
||
|
|
<select id="country">
|
||
|
|
<option value="BE">Belgium</option>
|
||
|
|
<option value="HR">Croatia</option>
|
||
|
|
<option value="PL">Poland</option>
|
||
|
|
</select>
|
||
|
|
<input id="vat_number" type="text" placeholder="VAT number">
|
||
|
|
<button type="button" onclick="searchCompany()">Search</button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="row">
|
||
|
|
<label for="company_name">Company</label>
|
||
|
|
<input id="company_name" type="text" style="grid-column: span 3;">
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="row">
|
||
|
|
<label for="street">Street</label>
|
||
|
|
<input id="street" type="text" style="grid-column: span 3;">
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="row">
|
||
|
|
<label for="zip">ZIP / City</label>
|
||
|
|
<input id="zip" type="text" placeholder="ZIP">
|
||
|
|
<input id="city" type="text" placeholder="City">
|
||
|
|
<div></div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="row">
|
||
|
|
<label for="email">Email</label>
|
||
|
|
<input id="email" type="email" placeholder="example@company.com" style="grid-column: span 3;">
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="row">
|
||
|
|
<label for="phone">Phone</label>
|
||
|
|
<input id="phone" type="text" placeholder="+32 123 45 67 89" style="grid-column: span 3;">
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="button-row">
|
||
|
|
<button type="button" onclick="saveData()">Save</button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div id="status" class="status">Ready.</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
async function searchCompany() {
|
||
|
|
const country = document.getElementById("country").value;
|
||
|
|
const vatNumber = document.getElementById("vat_number").value.trim();
|
||
|
|
const status = document.getElementById("status");
|
||
|
|
|
||
|
|
if (!country || !vatNumber) {
|
||
|
|
status.textContent = "Please enter a country and VAT number.";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
status.textContent = "Searching...";
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(`/api/company_lookup?country=${encodeURIComponent(country)}&vat_number=${encodeURIComponent(vatNumber)}`);
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
status.textContent = data.error || "Lookup failed.";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
document.getElementById("company_name").value = data.name || "";
|
||
|
|
document.getElementById("street").value = data.street || "";
|
||
|
|
document.getElementById("zip").value = data.zip || "";
|
||
|
|
document.getElementById("city").value = data.city || "";
|
||
|
|
|
||
|
|
let msg = data.found ? `Found via ${data.source}.` : "No company found.";
|
||
|
|
|
||
|
|
if (data.warning) {
|
||
|
|
msg += `\\nWarning: ${data.warning}`;
|
||
|
|
}
|
||
|
|
if (data.fallback_error) {
|
||
|
|
msg += `\\nFallback error: ${data.fallback_error}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
status.textContent = msg;
|
||
|
|
} catch (err) {
|
||
|
|
status.textContent = "Request failed: " + err;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function saveData() {
|
||
|
|
const payload = {
|
||
|
|
country: document.getElementById("country").value,
|
||
|
|
vat_number: document.getElementById("vat_number").value.trim(),
|
||
|
|
company_name: document.getElementById("company_name").value.trim(),
|
||
|
|
street: document.getElementById("street").value.trim(),
|
||
|
|
zip: document.getElementById("zip").value.trim(),
|
||
|
|
city: document.getElementById("city").value.trim(),
|
||
|
|
email: document.getElementById("email").value.trim(),
|
||
|
|
phone: document.getElementById("phone").value.trim()
|
||
|
|
};
|
||
|
|
|
||
|
|
alert(JSON.stringify(payload, null, 2));
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>
|
||
|
|
"""
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/")
|
||
|
|
def index():
|
||
|
|
return render_template_string(HTML)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
app.run(debug=True)
|