diff --git a/crates/db/migrations/0029_invoice_item_details.sql b/crates/db/migrations/0029_invoice_item_details.sql new file mode 100644 index 0000000..14cd149 --- /dev/null +++ b/crates/db/migrations/0029_invoice_item_details.sql @@ -0,0 +1,5 @@ +-- The invoice item must identify the service (Áfa tv. 169. § e): what the +-- package contains goes under the item, and the unit is "csomag". +ALTER TABLE invoices + ADD COLUMN item_comment text, + ADD COLUMN item_unit text NOT NULL DEFAULT 'csomag'; diff --git a/docs/fizetes.md b/docs/fizetes.md index a21e9e6..1e3d5d0 100644 --- a/docs/fizetes.md +++ b/docs/fizetes.md @@ -26,7 +26,9 @@ A sorok a fiók törlése után is megmaradnak (`user_id` → NULL), mert a szá ## Számla -Fizetéskor a backend egy `invoices` sort is létrehoz a számla minden adatával: a vevő (név, e-mail, ország, irányítószám, város, cím, Billingo-adótípus: `HAS_TAX_NUMBER` / `NO_TAX_NUMBER` / `FOREIGN`, adószám) és a tétel (név, végösszeg, nettó, áfa, áfakód). Az áfakód az `INVOICE_VAT` (a backend `.env`-je): alapból `AAM` (alanyi adómentes: nettó = végösszeg, áfa 0, a számla megjegyzésében „Alanyi adómentes.”), vagy egy kulcs, pl. `27%`. Cégnél a `business_name` és a magyar adószám (`hu_tin`, ennek híján `eu_vat`) kerül a számlára. +Fizetéskor a backend egy `invoices` sort is létrehoz a számla minden adatával: a vevő (név, e-mail, ország, irányítószám, város, cím, Billingo-adótípus: `HAS_TAX_NUMBER` / `NO_TAX_NUMBER` / `FOREIGN`, adószám) és a tétel (név, végösszeg, nettó, áfa, áfakód). Az áfakód az `INVOICE_VAT` (a backend `.env`-je): alapból `AAM` (alanyi adómentes: nettó = végösszeg, áfa 0, a számla megjegyzésében „Alanyi adómentes.”), vagy egy kulcs, pl. `27%`. A tétel a törvény szerint azonosítja a szolgáltatást (Áfa tv. 169. § e): megnevezése pl. „Online tartalomszolgáltatás – Páros csomag”, egysége `csomag`, megjegyzése a tartalom („Hozzáférés a sorsfonalak.hu szoftveresen előállított, személyre szabott szöveges tartalmaihoz: 2 képletelemzés, … Digitális tartalom.”; `Package::invoice_item`). Szándékosan nem „asztrológiai”: a szolgáltatás egy weboldal programmal előállított tartalma, nem személyes szakmai tanácsadás. Cégnél a `business_name` és a magyar adószám (`hu_tin`, ennek híján `eu_vat`) kerül a számlára. + +**Teszt módú fizetéshez (`sk_test_…`) nem készül számla**, mert a Billingo számlái valódiak (a NAV-hoz is bekerülnek); a csomagot ilyenkor is megkapja az admin. A worker `invoices` feladata (`WORKER_TASKS`, prodon a `worker` konténerben) a Billingo API v3-mal: diff --git a/services/backend/src/invoices.rs b/services/backend/src/invoices.rs index ccba08d..74f003b 100644 --- a/services/backend/src/invoices.rs +++ b/services/backend/src/invoices.rs @@ -100,7 +100,7 @@ pub async fn queue( checkout_id: &str, user_id: Option, buyer: &Buyer, - item_name: &str, + (item_name, item_comment): (&str, &str), gross_huf: i64, vat: Vat, ) -> Result { @@ -108,8 +108,8 @@ pub async fn queue( 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) \ - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) \ + buyer_city, buyer_address, buyer_tax_type, buyer_tax_number, item_name, gross_huf, net_huf, vat_huf, vat_rate, \ + item_comment) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) \ ON CONFLICT (checkout_id) DO NOTHING", ) .bind(id) @@ -128,6 +128,7 @@ pub async fn queue( .bind(net as i32) .bind(vat_huf as i32) .bind(vat.code()) + .bind(item_comment) .execute(&mut *tx) .await?; Ok(id) diff --git a/services/backend/src/packages.rs b/services/backend/src/packages.rs index 91f1341..b0b76f4 100644 --- a/services/backend/src/packages.rs +++ b/services/backend/src/packages.rs @@ -202,6 +202,33 @@ impl Source { } impl Package { + /// The invoice item: a name that says what the service is (online, + /// software-made content; no profession implied), and what the package + /// contains (Áfa tv. 169. § e). + pub fn invoice_item(&self) -> (String, String) { + let name = match self.tier { + Tier::Main => format!("Online tartalomszolgáltatás – {} csomag", self.name), + Tier::Addon => format!("Online tartalomszolgáltatás – {} kiegészítő", self.name), + }; + let lines: Vec = [ + (self.natal, "képletelemzés"), + (self.synastry, "kapcsolati elemzés"), + (self.forecast, "éves előrejelzés"), + (self.persons, "további személy felvétele"), + (self.questions, "kérdés"), + ] + .into_iter() + .filter(|&(n, _)| n > 0) + .map(|(n, what)| format!("{n} {what}")) + .collect(); + let comment = format!( + "Hozzáférés a sorsfonalak.hu szoftveresen előállított, személyre szabott szöveges tartalmaihoz: {}. \ + Digitális tartalom.", + lines.join(", ") + ); + (name, comment) + } + /// What the package gives, unit by unit. pub fn contents(&self) -> [(Unit, i64); 5] { [ @@ -341,4 +368,21 @@ mod tests { assert!(CATALOG.iter().filter(|p| p.tier == Tier::Main).all(|p| p.questions == QUESTIONS_INCLUDED)); assert_eq!(find("addon_questions").unwrap().questions, 5); } + + #[test] + fn invoice_items_say_what_was_bought() { + let (name, comment) = find("couple").unwrap().invoice_item(); + assert_eq!(name, "Online tartalomszolgáltatás – Páros csomag"); + assert_eq!( + comment, + "Hozzáférés a sorsfonalak.hu szoftveresen előállított, személyre szabott szöveges tartalmaihoz: \ + 2 képletelemzés, 1 kapcsolati elemzés, 2 éves előrejelzés, 1 további személy felvétele, 3 kérdés. \ + Digitális tartalom." + ); + let (name, comment) = find("addon_persons").unwrap().invoice_item(); + assert_eq!(name, "Online tartalomszolgáltatás – +5 személy kiegészítő"); + assert!(comment.ends_with("tartalmaihoz: 5 további személy felvétele. Digitális tartalom."), "{comment}"); + // No profession is implied. + assert!(CATALOG.iter().all(|p| !format!("{:?}", p.invoice_item()).to_lowercase().contains("asztrol"))); + } } diff --git a/services/backend/src/routes/payments.rs b/services/backend/src/routes/payments.rs index e53d602..5eced3e 100644 --- a/services/backend/src/routes/payments.rs +++ b/services/backend/src/routes/payments.rs @@ -170,13 +170,14 @@ pub async fn checkout( /// grants its package and queues the invoice, once. `false`: not our session. async fn complete(state: &AppState, session: &stripe::Session) -> Result { let mut tx = state.db.begin().await?; - let row: Option<(Option, String, i32, Option)> = sqlx::query_as( - "SELECT user_id, package, price_huf, completed_at FROM stripe_checkouts WHERE id = $1 FOR UPDATE", + type Checkout = (Option, String, i32, bool, Option); + let row: Option = sqlx::query_as( + "SELECT user_id, package, price_huf, live, completed_at FROM stripe_checkouts WHERE id = $1 FOR UPDATE", ) .bind(&session.id) .fetch_optional(&mut *tx) .await?; - let Some((user_id, package_id, price_huf, completed_at)) = row else { return Ok(false) }; + let Some((user_id, package_id, price_huf, live, completed_at)) = row else { return Ok(false) }; if completed_at.is_some() { return Ok(true); } @@ -205,10 +206,15 @@ async fn complete(state: &AppState, session: &stripe::Session) -> Result, item_name: String, + item_comment: Option, + item_unit: String, gross_huf: i32, vat_rate: String, payment_method: String, @@ -128,7 +130,7 @@ pub async fn run_once(db: &PgPool, billingo: &Billingo, max_attempts: i32) -> Re WHERE c.id = i.checkout_id AND i.id = (SELECT id FROM invoices WHERE status = 'pending' \ AND next_attempt_at <= now() ORDER BY next_attempt_at FOR UPDATE SKIP LOCKED LIMIT 1) \ RETURNING i.id, i.attempts, i.buyer_name, i.buyer_email, i.buyer_country, i.buyer_post_code, \ - i.buyer_city, i.buyer_address, i.buyer_tax_type, i.buyer_tax_number, i.item_name, i.gross_huf, \ + i.buyer_city, i.buyer_address, i.buyer_tax_type, i.buyer_tax_number, i.item_name, i.item_comment, i.item_unit, i.gross_huf, \ i.vat_rate, i.payment_method, i.billingo_partner_id, i.billingo_document_id, i.sent_at, \ coalesce(c.completed_at, i.created_at) AS paid_at" )) @@ -240,8 +242,9 @@ async fn issue(db: &PgPool, billingo: &Billingo, due: &Due) -> Result