Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Add generate_invoices script
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Jan 18, 2024
1 parent eaf7ccc commit 17c4291
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Defelo <[email protected]>"]
readme = "README.md"
homepage = "https://github.com/Bootstrap-Academy/shop-ms"
repository = "https://github.com/Bootstrap-Academy/shop-ms"
packages = [{ include = "api" }]
packages = [{ include = "api" }, { include = "scripts" }]
include = ["pyproject.toml", "templates/*", "assets/*"]

[tool.poetry.dependencies]
Expand Down Expand Up @@ -47,9 +47,11 @@ ruff = "^0.1.13"
[tool.poetry.scripts]
api = "api.main:main"
alembic = "alembic.config:main"
generate_invoices = "scripts.generate_invoices:main"

[tool.poe.tasks]
api = { script = "api.main:main", envfile = ".env" }
generate_invoices = { script = "scripts.generate_invoices:main", envfile = ".env" }
flake8 = "flake8 . --count --statistics --show-source"
isort = "isort ."
black = "black ."
Expand Down
61 changes: 61 additions & 0 deletions scripts/generate_invoices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import asyncio
from decimal import Decimal

from api.database import db, db_context
from api.database.database import filter_by
from api.models.paypal_orders import PaypalOrder
from api.services.auth import get_userinfo
from api.utils.invoices import generate_invoice_pdf


async def _main() -> None:
mwst = Decimal("0.19")
recs: dict[str, list[str]] = {}

async def user_info(id: str) -> list[str] | None:
if id in recs:
return recs[id]

info = await get_userinfo.__wrapped__(id) # type: ignore
if info is None:
return None
rec = [
f"{info.first_name} {info.last_name}" if info.first_name or info.last_name else f"{info.display_name}",
info.street,
f"{info.zip_code} {info.city}",
info.country,
]
recs[id] = rec
return rec

async with db_context():
order: PaypalOrder
async for order in await db.stream(
filter_by(PaypalOrder, pending=False).where(PaypalOrder.invoice_no != None) # noqa: E711
):
print(order.id, order.user_id, order.created_at, order.coins, order.invoice_no)
rec = await user_info(order.user_id)
if not rec:
print(f"ERROR: No user info for {order.user_id}")
rec = [order.user_id]
invoice = await generate_invoice_pdf(
(ino := f"R{order.invoice_no:07}"),
"Rechnung",
"EUR",
mwst,
4,
2,
[("MorphCoins", Decimal("0.01") / (mwst + 1), order.coins)],
[r for r in rec if r and r.strip()],
order.created_at.date(),
)
with open(f"{ino}.pdf", "wb") as f:
f.write(invoice)


def main() -> None:
asyncio.run(_main())


if __name__ == "__main__":
main()

0 comments on commit 17c4291

Please sign in to comment.