CZK implemented

This commit is contained in:
Priec
2026-06-23 12:54:11 +02:00
parent 6b7422806f
commit c409e85995
31 changed files with 606 additions and 51 deletions

View File

@@ -13,8 +13,9 @@ use serde_json::json;
use crate::{
controllers::i18n::current_lang,
shared::{
currency::{self, Currency},
guard,
money::{format_price, parse_price_to_cents},
money::parse_price_to_cents,
pricing,
},
models::{categories, product_images, product_variants, products, users},
@@ -90,6 +91,7 @@ async fn run_search(
ctx: &AppContext,
user: Option<&users::Model>,
params: &SearchParams,
cur: &Currency,
) -> Result<serde_json::Value> {
let q = params.q.clone().unwrap_or_default();
let q_trim = q.trim().to_string();
@@ -136,9 +138,19 @@ async fn run_search(
let price_floor = items.iter().map(|i| i.priced.price_cents).min().unwrap_or(0);
let price_ceil = items.iter().map(|i| i.priced.price_cents).max().unwrap_or(0);
// 3. Non-category filters: price band + in-stock.
let min_c = params.min_price.as_deref().and_then(|s| parse_price_to_cents(s).ok());
let max_c = params.max_price.as_deref().and_then(|s| parse_price_to_cents(s).ok());
// 3. Non-category filters: price band + in-stock. The typed bounds are in
// the buyer's display currency; convert them back to EUR cents to compare
// against the (EUR) resolved prices.
let min_c = params
.min_price
.as_deref()
.and_then(|s| parse_price_to_cents(s).ok())
.map(|c| cur.to_eur_cents(c));
let max_c = params
.max_price
.as_deref()
.and_then(|s| parse_price_to_cents(s).ok())
.map(|c| cur.to_eur_cents(c));
let in_stock_only = is_on(&params.in_stock);
items.retain(|i| {
min_c.is_none_or(|m| i.priced.price_cents >= m)
@@ -203,6 +215,7 @@ async fn run_search(
item.count,
image,
cat_name,
cur,
));
}
@@ -219,8 +232,9 @@ async fn run_search(
"in_stock": in_stock_only,
"min_price": params.min_price.clone().unwrap_or_default(),
"max_price": params.max_price.clone().unwrap_or_default(),
"price_floor": format_price(price_floor),
"price_ceil": format_price(price_ceil),
"price_floor": cur.format(price_floor),
"price_ceil": cur.format(price_ceil),
"currency_symbol": cur.symbol,
"total": total,
"page": page,
"pages": pages,
@@ -240,6 +254,7 @@ async fn product_rows(
ctx: &AppContext,
user: Option<&users::Model>,
list: Vec<products::Model>,
cur: &Currency,
) -> Result<Vec<serde_json::Value>> {
let ids: Vec<i32> = list.iter().map(|p| p.id).collect();
let grouped = product_variants::Entity::grouped_for_products(&ctx.db, &ids).await?;
@@ -261,7 +276,7 @@ async fn product_rows(
let mut rows = Vec::with_capacity(entries.len());
for ((product, rep, count), priced) in entries.iter().zip(priced.iter()) {
let image = product_images::first_for(ctx, product.id).await?;
rows.push(view::product_card(product, rep, priced, *count, image, None));
rows.push(view::product_card(product, rep, priced, *count, image, None, cur));
}
Ok(rows)
}
@@ -272,6 +287,7 @@ pub(crate) async fn featured_products(
ctx: &AppContext,
user: Option<&users::Model>,
limit: u64,
cur: &Currency,
) -> Result<Vec<serde_json::Value>> {
let list = products::Entity::find()
.filter(products::Column::Published.eq(true))
@@ -279,7 +295,7 @@ pub(crate) async fn featured_products(
.limit(limit)
.all(&ctx.db)
.await?;
product_rows(ctx, user, list).await
product_rows(ctx, user, list, cur).await
}
/// The site-wide category sidebar, loaded lazily via htmx by the base layout so
@@ -320,7 +336,8 @@ async fn index(
State(ctx): State<AppContext>,
) -> Result<Response> {
let user = guard::current_user(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &SearchParams::default()).await?;
let cur = currency::resolve(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &SearchParams::default(), &cur).await?;
let c = guard::chrome_from(&ctx, user.as_ref());
add_chrome(&mut context, &c, &current_lang(&jar));
format::view(&v, "shop/index.html", context)
@@ -341,7 +358,8 @@ async fn search(
State(ctx): State<AppContext>,
) -> Result<Response> {
let user = guard::current_user(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &params).await?;
let cur = currency::resolve(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &params, &cur).await?;
let lang = current_lang(&jar);
if headers.contains_key("HX-Request") {
@@ -385,12 +403,13 @@ async fn show(
};
let user = guard::current_user(&ctx, &jar).await;
let cur = currency::resolve(&ctx, &jar).await;
let variants = product_variants::Entity::for_product(&ctx.db, product.id).await?;
let variant_prices = pricing::price_variants(&ctx, &variants, user.as_ref()).await?;
let options: Vec<serde_json::Value> = variants
.iter()
.zip(variant_prices.iter())
.map(|(variant, priced)| view::variant_option(variant, priced))
.map(|(variant, priced)| view::variant_option(variant, priced, &cur))
.collect();
// The card header uses the representative (first) variant for its headline
// price; the picker below lets the customer switch.
@@ -404,6 +423,7 @@ async fn show(
variants.len(),
None,
category.as_ref().map(|c| c.name.clone()),
&cur,
),
// A product with no variants isn't purchasable; show it without a price.
_ => serde_json::json!({
@@ -428,6 +448,7 @@ async fn show(
"logged_in_customer": c.logged_in_customer,
"customer_name": c.customer_name,
"customer_account_type": c.customer_account_type,
"currency_symbol": cur.symbol,
"lang": current_lang(&jar),
}),
)
@@ -463,7 +484,8 @@ async fn category(
};
let user = guard::current_user(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &params).await?;
let cur = currency::resolve(&ctx, &jar).await;
let mut context = run_search(&ctx, user.as_ref(), &params, &cur).await?;
if let Some(map) = context.as_object_mut() {
map.insert("category".into(), serde_json::to_value(&category)?);
map.insert("breadcrumbs".into(), serde_json::to_value(&breadcrumbs)?);