Spaces:
Sleeping
Sleeping
File size: 9,360 Bytes
de77711 3eff181 474aef0 de77711 fb99e77 eee30e1 802b31f aef5cd6 de77711 eee30e1 802b31f aef5cd6 fb99e77 de77711 474aef0 de77711 474aef0 de77711 474aef0 de77711 eee30e1 802b31f eee30e1 de77711 3eff181 de77711 eee30e1 802b31f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | """SQLite persistence layer for VoiceLedger transactions."""
from __future__ import annotations
import sqlite3
import tempfile
from datetime import datetime
from pathlib import Path
from typing import Any
import pandas as pd
from voiceledger.config import get_database_path
from voiceledger.ledger.corrections import initialize_correction_log_table
from voiceledger.ledger.customers import add_credit, initialize_customers_table, record_payment
from voiceledger.ledger.inventory import add_stock, initialize_inventory_table, remove_stock
from voiceledger.ledger.settings import initialize_business_settings_table
from voiceledger.parser.schema import Transaction
SCHEMA_SQL = """
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
transaction_type TEXT NOT NULL,
item TEXT,
quantity REAL,
unit_price REAL,
amount REAL,
customer TEXT,
payment_status TEXT NOT NULL,
notes TEXT NOT NULL,
confidence REAL NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
"""
COLUMNS = [
"id",
"transaction_type",
"item",
"quantity",
"unit_price",
"amount",
"customer",
"payment_status",
"notes",
"confidence",
"created_at",
]
def initialize_database(db_path: str | Path | None = None) -> Path:
"""Create the SQLite database and transactions table if needed."""
path = _resolve_db_path(db_path)
path.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(path) as connection:
connection.execute(SCHEMA_SQL)
connection.commit()
initialize_customers_table(path)
initialize_inventory_table(path)
initialize_business_settings_table(path)
initialize_correction_log_table(path)
return path
def add_transaction(transaction: Transaction, db_path: str | Path | None = None) -> int:
"""Insert a transaction and return its database id."""
path = initialize_database(db_path)
payload = transaction.model_dump()
with sqlite3.connect(path) as connection:
cursor = connection.execute(
"""
INSERT INTO transactions (
transaction_type,
item,
quantity,
unit_price,
amount,
customer,
payment_status,
notes,
confidence,
created_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
payload["transaction_type"],
payload["item"],
payload["quantity"],
payload["unit_price"],
payload["amount"],
payload["customer"],
payload["payment_status"],
payload["notes"],
payload["confidence"],
datetime.now().isoformat(sep=" ", timespec="seconds"),
),
)
connection.commit()
transaction_id = int(cursor.lastrowid)
_apply_customer_balance_update(transaction, path)
_apply_inventory_update(transaction, path)
return transaction_id
def get_transactions(db_path: str | Path | None = None) -> pd.DataFrame:
"""Return all saved transactions as a Pandas DataFrame."""
path = initialize_database(db_path)
with sqlite3.connect(path) as connection:
rows = connection.execute(
"""
SELECT
id,
transaction_type,
item,
quantity,
unit_price,
amount,
customer,
payment_status,
notes,
confidence,
created_at
FROM transactions
ORDER BY id DESC
"""
).fetchall()
records: list[dict[str, Any]] = [dict(zip(COLUMNS, row, strict=True)) for row in rows]
return pd.DataFrame.from_records(records, columns=COLUMNS)
def get_transaction(transaction_id: int, db_path: str | Path | None = None) -> Transaction | None:
"""Return one transaction by id, or None when it does not exist."""
path = initialize_database(db_path)
with sqlite3.connect(path) as connection:
row = connection.execute(
"""
SELECT
transaction_type,
item,
quantity,
unit_price,
amount,
customer,
payment_status,
notes,
confidence
FROM transactions
WHERE id = ?
""",
(int(transaction_id),),
).fetchone()
if row is None:
return None
payload = dict(zip(COLUMNS[1:-1], row, strict=True))
return Transaction.model_validate(payload)
def update_transaction(
transaction_id: int,
transaction: Transaction,
db_path: str | Path | None = None,
) -> bool:
"""Update a transaction and rebuild derived balances when found."""
path = initialize_database(db_path)
payload = transaction.model_dump()
with sqlite3.connect(path) as connection:
cursor = connection.execute(
"""
UPDATE transactions
SET
transaction_type = ?,
item = ?,
quantity = ?,
unit_price = ?,
amount = ?,
customer = ?,
payment_status = ?,
notes = ?,
confidence = ?
WHERE id = ?
""",
(
payload["transaction_type"],
payload["item"],
payload["quantity"],
payload["unit_price"],
payload["amount"],
payload["customer"],
payload["payment_status"],
payload["notes"],
payload["confidence"],
int(transaction_id),
),
)
connection.commit()
updated = cursor.rowcount > 0
if updated:
rebuild_derived_tables(path)
return updated
def delete_transaction(transaction_id: int, db_path: str | Path | None = None) -> bool:
"""Delete a transaction and rebuild derived balances when found."""
path = initialize_database(db_path)
with sqlite3.connect(path) as connection:
cursor = connection.execute(
"DELETE FROM transactions WHERE id = ?",
(int(transaction_id),),
)
connection.commit()
deleted = cursor.rowcount > 0
if deleted:
rebuild_derived_tables(path)
return deleted
def export_transactions_csv(
db_path: str | Path | None = None,
export_path: str | Path | None = None,
) -> Path:
"""Export all transactions to a CSV file and return the file path."""
ledger = get_transactions(db_path)
if export_path is None:
export_path = Path(tempfile.gettempdir()) / "voiceledger_transactions.csv"
path = Path(export_path).expanduser()
path.parent.mkdir(parents=True, exist_ok=True)
ledger.to_csv(path, index=False, columns=COLUMNS)
return path
def rebuild_derived_tables(db_path: str | Path | None = None) -> None:
"""Rebuild customer balances and inventory from saved transactions."""
path = initialize_database(db_path)
with sqlite3.connect(path) as connection:
connection.execute("DELETE FROM customers")
connection.execute("DELETE FROM inventory")
rows = connection.execute(
"""
SELECT
transaction_type,
item,
quantity,
unit_price,
amount,
customer,
payment_status,
notes,
confidence
FROM transactions
ORDER BY id ASC
"""
).fetchall()
connection.commit()
for row in rows:
transaction = Transaction.model_validate(dict(zip(COLUMNS[1:-1], row, strict=True)))
_apply_customer_balance_update(transaction, path)
_apply_inventory_update(transaction, path)
def _resolve_db_path(db_path: str | Path | None) -> Path:
"""Resolve an explicit or configured database path."""
if db_path is None:
return get_database_path()
return Path(db_path).expanduser()
def _apply_customer_balance_update(transaction: Transaction, db_path: Path) -> None:
"""Apply customer balance side effects for credit-related transactions."""
if not transaction.customer or transaction.amount is None:
return
if transaction.transaction_type == "customer_credit":
add_credit(transaction.customer, transaction.amount, db_path)
elif transaction.transaction_type == "customer_payment":
record_payment(transaction.customer, transaction.amount, db_path)
def _apply_inventory_update(transaction: Transaction, db_path: Path) -> None:
"""Apply inventory side effects for stock-related transactions."""
if not transaction.item or transaction.quantity is None:
return
if transaction.transaction_type == "inventory_purchase":
add_stock(transaction.item, transaction.quantity, db_path)
elif transaction.transaction_type == "sale":
remove_stock(transaction.item, transaction.quantity, db_path)
|