Invoices: alanyi adómentes (AAM) by default
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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user