Files
boc/projects/landvex/20-automation/aamos_client.py
T
Bernt 6de2455917 v1.2.0: Add Global Markets footer, translated to 9 languages
- Added GLOBAL_MARKETS_TITLE to all translation files
- Updated footer with 12 markets (4 active + 8 upcoming)
- Translated market section to: zh-cn, zh-tw, ja, ko, th, vi, id, ms, hi
- Built and deployed to production
- CloudFront invalidation: I3RTMXVFDJXWLG3SYX208OP1CC
2026-07-08 19:56:03 +00:00

144 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
Aamos Ledger Client för Landvex Artikelautomation
Använder aamos-ledger API för att lagra artiklar
"""
import json
import os
from datetime import datetime, timezone
from typing import Dict, Any, Optional
import requests
AAMOS_BASE_URL = "http://localhost:3250"
class AamosClient:
"""Klient för att kommunicera med aamos-ledger"""
def __init__(self, base_url: str = AAMOS_BASE_URL):
self.base_url = base_url
self.token = None
def login(self, username: str = "admin", password: str = "password") -> bool:
"""Logga in och få JWT-token"""
try:
resp = requests.post(
f"{self.base_url}/login",
json={"username": username, "password": password}
)
if resp.status_code == 200:
data = resp.json()
self.token = data.get("token")
return True
return False
except Exception as e:
print(f"Login failed: {e}")
return False
def _headers(self) -> Dict[str, str]:
"""Headers med auth-token"""
headers = {"Content-Type": "application/json"}
if self.token:
headers["Authorization"] = f"Bearer {self.token}"
return headers
def create_account(self, code: str, name: str, account_type: str = "Asset") -> Optional[Dict]:
"""Skapa ett konto i aamos-ledger"""
try:
resp = requests.post(
f"{self.base_url}/accounts",
headers=self._headers(),
json={"code": code, "name": name, "account_type": account_type}
)
if resp.status_code in (200, 201):
return resp.json()
print(f"Create account failed: {resp.status_code} - {resp.text}")
return None
except Exception as e:
print(f"Create account error: {e}")
return None
def list_accounts(self) -> list:
"""Lista alla konton"""
try:
resp = requests.get(
f"{self.base_url}/accounts",
headers=self._headers()
)
if resp.status_code == 200:
return resp.json()
return []
except Exception as e:
print(f"List accounts error: {e}")
return []
def create_journal_entry(self, entry_number: str, description: str,
entry_date: str, lines: list) -> Optional[Dict]:
"""Skapa ett bokföringsverifikat"""
try:
resp = requests.post(
f"{self.base_url}/journal-entries",
headers=self._headers(),
json={
"entry_number": entry_number,
"description": description,
"entry_date": entry_date,
"lines": lines
}
)
if resp.status_code in (200, 201):
return resp.json()
print(f"Create journal entry failed: {resp.status_code} - {resp.text}")
return None
except Exception as e:
print(f"Create journal entry error: {e}")
return None
def get_trial_balance(self) -> Optional[Dict]:
"""Hämta saldoöversikt"""
try:
resp = requests.get(
f"{self.base_url}/trial-balance",
headers=self._headers()
)
if resp.status_code == 200:
return resp.json()
return None
except Exception as e:
print(f"Trial balance error: {e}")
return None
def test_aamos():
"""Testa aamos-ledger integration"""
client = AamosClient()
# Logga in
if not client.login():
print("❌ Login failed")
return False
print("✅ Login successful")
# Lista konton
accounts = client.list_accounts()
print(f"✅ Found {len(accounts)} accounts")
for acc in accounts[:3]:
print(f" - {acc['code']}: {acc['name']} ({acc['account_type']})")
# Skapa test-konto
test_acc = client.create_account(
code="9999",
name="Landvex Test Account",
account_type="Asset"
)
if test_acc:
print(f"✅ Created test account: {test_acc['id']}")
return True
if __name__ == "__main__":
test_aamos()