Invoices: alanyi adómentes (AAM) by default
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 5m57s
CI / deploy (push) Skipped

INVOICE_VAT picks the VAT of the invoices: AAM (no VAT, net = gross, the
invoice says so) or a rate like 27% once the exemption limit is passed.
The packages page tells which.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-20 08:03:04 +00:00
co-authored by Claude Opus 5.5
parent 2a0830fda0
commit 4338a35202
14 changed files with 90 additions and 27 deletions
+8
View File
@@ -48,6 +48,8 @@ pub struct Config {
/// Stripe Checkout, when `STRIPE_SECRET_KEY` is set (it wins over
/// `MOCK_PAYMENTS`).
pub stripe: Option<StripeConfig>,
/// `INVOICE_VAT` (default `AAM`, alanyi adómentes; or e.g. `27%`).
pub invoice_vat: crate::invoices::Vat,
/// `BILLINGO_API_KEY`: downloading invoices (the worker issues them).
pub billingo_api_key: Option<String>,
/// `BILLINGO_API_URL` (default `https://api.billingo.hu/v3`).
@@ -174,6 +176,11 @@ impl Config {
.trim_end_matches('/')
.to_string(),
}),
invoice_vat: match var("INVOICE_VAT") {
None => crate::invoices::Vat::Exempt,
Some(raw) => crate::invoices::Vat::parse(&raw)
.with_context(|| format!("invalid INVOICE_VAT '{raw}': use AAM or a rate like 27%"))?,
},
billingo_api_key: var("BILLINGO_API_KEY"),
billingo_api_url: var("BILLINGO_API_URL")
.unwrap_or_else(|| "https://api.billingo.hu/v3".into())
@@ -220,6 +227,7 @@ impl Config {
password_login: true,
mock_payments: true,
stripe: None,
invoice_vat: crate::invoices::Vat::Exempt,
billingo_api_key: Some("billingo-fake".into()),
billingo_api_url: format!("{horoscope_api_url}/billingo"),
purchases_open: true,
+49 -14
View File
@@ -9,8 +9,41 @@ use uuid::Uuid;
use crate::error::ApiError;
/// VAT of every package, in percent.
pub const VAT_PERCENT: i64 = 27;
/// The VAT on the invoices (`INVOICE_VAT`): `AAM` (alanyi adómentes, subject
/// to the small business exemption: no VAT, net = gross) or a rate like `27%`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Vat {
Exempt,
Percent(i64),
}
impl Vat {
pub fn parse(raw: &str) -> Option<Self> {
match raw.trim() {
"AAM" => Some(Vat::Exempt),
rate => rate.strip_suffix('%')?.trim().parse().ok().filter(|p| (1..=27).contains(p)).map(Vat::Percent),
}
}
/// Billingo's VAT value.
pub fn code(self) -> String {
match self {
Vat::Exempt => "AAM".into(),
Vat::Percent(p) => format!("{p}%"),
}
}
/// Net and VAT parts of a gross price (rounded to forints, as Billingo does).
pub fn split(self, gross: i64) -> (i64, i64) {
match self {
Vat::Exempt => (gross, 0),
Vat::Percent(p) => {
let net = (gross * 200 + 100 + p) / (200 + 2 * p);
(net, gross - net)
}
}
}
}
/// The buyer as invoiced, from Stripe's `customer_details`.
#[derive(Debug, PartialEq, Eq)]
@@ -61,12 +94,6 @@ impl Buyer {
}
}
/// Net and VAT parts of a gross price (rounded to forints, as Billingo does).
pub fn split_gross(gross: i64) -> (i64, i64) {
let net = (gross * 200 + 100 + VAT_PERCENT) / (200 + 2 * VAT_PERCENT);
(net, gross - net)
}
/// Queues the invoice of a paid checkout, inside the caller's transaction.
pub async fn queue(
tx: &mut PgConnection,
@@ -75,9 +102,10 @@ pub async fn queue(
buyer: &Buyer,
item_name: &str,
gross_huf: i64,
vat: Vat,
) -> Result<Uuid, ApiError> {
let id = Uuid::now_v7();
let (net, vat) = split_gross(gross_huf);
let (net, vat_huf) = vat.split(gross_huf);
sqlx::query(
"INSERT INTO invoices (id, checkout_id, user_id, buyer_name, buyer_email, buyer_country, buyer_post_code, \
buyer_city, buyer_address, buyer_tax_type, buyer_tax_number, item_name, gross_huf, net_huf, vat_huf, vat_rate) \
@@ -98,8 +126,8 @@ pub async fn queue(
.bind(item_name)
.bind(gross_huf as i32)
.bind(net as i32)
.bind(vat as i32)
.bind(format!("{VAT_PERCENT}%"))
.bind(vat_huf as i32)
.bind(vat.code())
.execute(&mut *tx)
.await?;
Ok(id)
@@ -113,9 +141,16 @@ mod tests {
#[test]
fn gross_prices_split_into_net_and_vat() {
assert_eq!(split_gross(2000), (1575, 425));
assert_eq!(split_gross(6000), (4724, 1276));
assert_eq!(split_gross(127), (100, 27));
let vat = Vat::parse("27%").unwrap();
assert_eq!(vat.split(2000), (1575, 425));
assert_eq!(vat.split(6000), (4724, 1276));
assert_eq!(vat.split(127), (100, 27));
assert_eq!(vat.code(), "27%");
// Alanyi adómentes: no VAT at all.
assert_eq!(Vat::parse("AAM"), Some(Vat::Exempt));
assert_eq!(Vat::Exempt.split(6000), (6000, 0));
assert_eq!(Vat::parse("huszonhét"), None);
assert_eq!(Vat::parse("99%"), None);
}
#[test]
+1 -1
View File
@@ -37,7 +37,7 @@ pub struct Package {
pub name: &'static str,
pub tagline: &'static str,
pub tier: Tier,
/// Gross price (27% VAT included).
/// The price paid (with VAT if `INVOICE_VAT` is a rate; none under AAM).
pub price_huf: i64,
pub natal: i64,
pub synastry: i64,
+2
View File
@@ -173,6 +173,8 @@ async fn public_config(State(state): St) -> Json<Value> {
"payments": payments::method(c),
"payments_admins_only": payments::admins_only(c),
"payments_test": c.stripe.as_ref().is_some_and(|s| s.test_mode()),
// "AAM" (alanyi adómentes: the prices carry no VAT) or a rate like "27%".
"vat": c.invoice_vat.code(),
"user_invites": c.user_invites,
"referral_rewards": c.referral_rewards,
"signup_mode": c.signup_mode.as_str(),
+2 -1
View File
@@ -207,7 +207,8 @@ async fn complete(state: &AppState, session: &stripe::Session) -> Result<bool, A
.await?;
let buyer = invoices::Buyer::from_stripe(session.customer_details.as_ref().unwrap_or(&Value::Null), None);
let item = format!("Sorsfonalak – {}", package.name);
invoices::queue(&mut tx, &session.id, user_id, &buyer, &item, amount_huf.unwrap_or(i64::from(price_huf))).await?;
let gross = amount_huf.unwrap_or(i64::from(price_huf));
invoices::queue(&mut tx, &session.id, user_id, &buyer, &item, gross, state.config.invoice_vat).await?;
tx.commit().await?;
tracing::info!(user = ?user_id, package = %package_id, session = %session.id, "package paid");
Ok(true)
+3 -1
View File
@@ -2295,6 +2295,7 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
let r = app.get("/v1/config", None).await;
assert_eq!(r.body["payments"], "stripe");
assert_eq!(r.body["payments_test"], false);
assert_eq!(r.body["vat"], "AAM");
// Stripe replaces the stand-in payment.
assert_eq!(r.body["mock_payments"], false);
let anna = app.register("anna@example.com", "Anna").await;
@@ -2404,7 +2405,8 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
("HAS_TAX_NUMBER", Some("12345678-1-23"))
);
assert_eq!(invoice.item_name, "Sorsfonalak – Páros");
assert_eq!((invoice.gross_huf, invoice.net_huf, invoice.vat_huf), (6000, 4724, 1276));
// Alanyi adómentes (the default): no VAT.
assert_eq!((invoice.gross_huf, invoice.net_huf, invoice.vat_huf), (6000, 6000, 0));
// The user's invoices: being prepared until Billingo issues it, then the
// PDF comes from Billingo.