now products have different options, like different parameters
This commit is contained in:
@@ -21,7 +21,7 @@ use crate::{
|
||||
controllers::i18n::current_lang,
|
||||
models::{
|
||||
account_discount_profiles, account_product_prices, account_product_resolutions,
|
||||
categories, discount_profiles, products, _entities::users,
|
||||
categories, discount_profiles, product_variants, products, _entities::users,
|
||||
},
|
||||
shared::{
|
||||
guard,
|
||||
@@ -142,16 +142,9 @@ async fn show(
|
||||
.order_by_asc(products::Column::Name)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
// Two prices per product:
|
||||
// - the generic business price a freshly-registered company sees (business
|
||||
// baseline + business-audience profiles, no per-company deals), and
|
||||
// - this company's effective price (its negotiated price + assigned profiles).
|
||||
// The effective price is highlighted only when it differs from the generic one.
|
||||
let business = pricing::audience_price_many(&ctx, &list, BUSINESS_AUDIENCE).await?;
|
||||
let details = pricing::detail_many(&ctx, &list, Some(&company)).await?;
|
||||
|
||||
// Category sidebar tree (counts over the full, unfiltered list) plus the
|
||||
// active `?category=` filter applied to the rows.
|
||||
// Category sidebar tree (counts over the full, unfiltered product list) plus
|
||||
// the active `?category=` filter applied to the rows.
|
||||
let category_ids: Vec<Option<i32>> = list.iter().map(|p| p.category_id).collect();
|
||||
let category_groups = view::admin_category_groups(&all_categories, &category_ids);
|
||||
let selected_category = params
|
||||
@@ -161,15 +154,43 @@ async fn show(
|
||||
.to_string();
|
||||
let filter = view::category_filter_ids(&all_categories, &selected_category);
|
||||
|
||||
let rows: Vec<serde_json::Value> = list
|
||||
// Pricing is per variant. Flatten the (filtered) products into their variants
|
||||
// in product-name then variant-position order, carrying each variant's
|
||||
// product for the row's display name.
|
||||
let product_ids: Vec<i32> = list.iter().map(|p| p.id).collect();
|
||||
let grouped = product_variants::Entity::grouped_for_products(&ctx.db, &product_ids).await?;
|
||||
let mut variant_rows: Vec<(&products::Model, product_variants::Model)> = Vec::new();
|
||||
for product in &list {
|
||||
if !view::category_filter_keep(&filter, product.category_id) {
|
||||
continue;
|
||||
}
|
||||
if let Some(variants) = grouped.get(&product.id) {
|
||||
for variant in variants {
|
||||
variant_rows.push((product, variant.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Two prices per variant:
|
||||
// - the generic business price a freshly-registered company sees (business
|
||||
// baseline + business-audience profiles, no per-company deals), and
|
||||
// - this company's effective price (its negotiated price + assigned profiles).
|
||||
// The effective price is highlighted only when it differs from the generic one.
|
||||
let variants_only: Vec<product_variants::Model> =
|
||||
variant_rows.iter().map(|(_, v)| v.clone()).collect();
|
||||
let business = pricing::audience_price_variants(&ctx, &variants_only, BUSINESS_AUDIENCE).await?;
|
||||
let details = pricing::detail_variants(&ctx, &variants_only, Some(&company)).await?;
|
||||
|
||||
let rows: Vec<serde_json::Value> = variant_rows
|
||||
.iter()
|
||||
.zip(business.iter())
|
||||
.zip(details.iter())
|
||||
.filter(|((product, _), _)| view::category_filter_keep(&filter, product.category_id))
|
||||
.map(|((product, b), d)| {
|
||||
.map(|(((product, variant), b), d)| {
|
||||
json!({
|
||||
"product_id": product.id,
|
||||
"variant_id": variant.id,
|
||||
"name": product.name,
|
||||
"variant_label": variant.label,
|
||||
"currency": product.currency,
|
||||
"regular_price": format_price(d.regular_cents),
|
||||
"business_price": format_price(b.price_cents),
|
||||
@@ -207,22 +228,27 @@ async fn price_edit(
|
||||
auth: auth::JWT,
|
||||
jar: CookieJar,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
Path((id, product_id)): Path<(i32, i32)>,
|
||||
Path((id, variant_id)): Path<(i32, i32)>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let company = company_by_id(&ctx, id).await?;
|
||||
let product = products::Entity::find_by_id(product_id)
|
||||
let variant = product_variants::Entity::find_by_id(variant_id)
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
let product = products::Entity::find_by_id(variant.product_id)
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
|
||||
let business =
|
||||
pricing::audience_price_many(&ctx, std::slice::from_ref(&product), BUSINESS_AUDIENCE)
|
||||
pricing::audience_price_variants(&ctx, std::slice::from_ref(&variant), BUSINESS_AUDIENCE)
|
||||
.await?;
|
||||
let business_cents = business[0].price_cents;
|
||||
let detail = pricing::detail_many(&ctx, std::slice::from_ref(&product), Some(&company)).await?;
|
||||
let detail =
|
||||
pricing::detail_variants(&ctx, std::slice::from_ref(&variant), Some(&company)).await?;
|
||||
let d = &detail[0];
|
||||
|
||||
// Names for the covering profiles, used by the collision resolution selector.
|
||||
@@ -248,7 +274,9 @@ async fn price_edit(
|
||||
"customer": { "id": company.id, "name": company.name },
|
||||
"product": {
|
||||
"id": product.id,
|
||||
"variant_id": variant.id,
|
||||
"name": product.name,
|
||||
"variant_label": variant.label,
|
||||
"currency": product.currency,
|
||||
"regular_price": format_price(d.regular_cents),
|
||||
"regular_cents": d.regular_cents,
|
||||
@@ -271,7 +299,7 @@ async fn price_edit(
|
||||
#[debug_handler]
|
||||
async fn set_price(
|
||||
auth: auth::JWT,
|
||||
Path((id, product_id)): Path<(i32, i32)>,
|
||||
Path((id, variant_id)): Path<(i32, i32)>,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<PriceForm>,
|
||||
) -> Result<Response> {
|
||||
@@ -280,7 +308,7 @@ async fn set_price(
|
||||
|
||||
let entered = form.price.trim().to_string();
|
||||
if entered.is_empty() {
|
||||
account_product_prices::Model::clear(&ctx.db, company.id, product_id).await?;
|
||||
account_product_prices::Model::clear(&ctx.db, company.id, variant_id).await?;
|
||||
return format::redirect(&format!("/admin/customers/{id}"));
|
||||
}
|
||||
|
||||
@@ -288,23 +316,23 @@ async fn set_price(
|
||||
Ok(cents) if cents > 0 => cents,
|
||||
_ => {
|
||||
return format::redirect(&format!(
|
||||
"/admin/customers/{id}/prices/{product_id}/edit?error=discount-must-be-positive"
|
||||
"/admin/customers/{id}/prices/{variant_id}/edit?error=discount-must-be-positive"
|
||||
))
|
||||
}
|
||||
};
|
||||
account_product_prices::Model::upsert(&ctx.db, company.id, product_id, cents).await?;
|
||||
account_product_prices::Model::upsert(&ctx.db, company.id, variant_id, cents).await?;
|
||||
format::redirect(&format!("/admin/customers/{id}"))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn remove_price(
|
||||
auth: auth::JWT,
|
||||
Path((id, product_id)): Path<(i32, i32)>,
|
||||
Path((id, variant_id)): Path<(i32, i32)>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let company = company_by_id(&ctx, id).await?;
|
||||
account_product_prices::Model::clear(&ctx.db, company.id, product_id).await?;
|
||||
account_product_prices::Model::clear(&ctx.db, company.id, variant_id).await?;
|
||||
format::redirect(&format!("/admin/customers/{id}"))
|
||||
}
|
||||
|
||||
@@ -347,7 +375,7 @@ async fn sync_profiles(
|
||||
#[debug_handler]
|
||||
async fn set_resolution(
|
||||
auth: auth::JWT,
|
||||
Path((id, product_id)): Path<(i32, i32)>,
|
||||
Path((id, variant_id)): Path<(i32, i32)>,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<ResolutionForm>,
|
||||
) -> Result<Response> {
|
||||
@@ -356,14 +384,14 @@ async fn set_resolution(
|
||||
|
||||
let existing = account_product_resolutions::Entity::find()
|
||||
.filter(account_product_resolutions::Column::UserId.eq(company.id))
|
||||
.filter(account_product_resolutions::Column::ProductId.eq(product_id))
|
||||
.filter(account_product_resolutions::Column::VariantId.eq(variant_id))
|
||||
.one(&ctx.db)
|
||||
.await?;
|
||||
let mut active = match existing {
|
||||
Some(row) => row.into_active_model(),
|
||||
None => account_product_resolutions::ActiveModel {
|
||||
user_id: Set(company.id),
|
||||
product_id: Set(product_id),
|
||||
variant_id: Set(variant_id),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
@@ -378,16 +406,16 @@ pub fn routes() -> Routes {
|
||||
.add("/admin/customers/{id}", get(show))
|
||||
.add("/admin/customers/{id}/profiles", post(sync_profiles))
|
||||
.add(
|
||||
"/admin/customers/{id}/prices/{product_id}/edit",
|
||||
"/admin/customers/{id}/prices/{variant_id}/edit",
|
||||
get(price_edit),
|
||||
)
|
||||
.add("/admin/customers/{id}/prices/{product_id}", post(set_price))
|
||||
.add("/admin/customers/{id}/prices/{variant_id}", post(set_price))
|
||||
.add(
|
||||
"/admin/customers/{id}/prices/{product_id}/remove",
|
||||
"/admin/customers/{id}/prices/{variant_id}/remove",
|
||||
post(remove_price),
|
||||
)
|
||||
.add(
|
||||
"/admin/customers/{id}/resolutions/{product_id}",
|
||||
"/admin/customers/{id}/resolutions/{variant_id}",
|
||||
post(set_resolution),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -38,6 +38,23 @@ impl MultipartForm {
|
||||
Some("on" | "true" | "1")
|
||||
)
|
||||
}
|
||||
|
||||
/// The distinct row indices `N` present among `variants[N][...]` fields,
|
||||
/// sorted ascending. Used to read the repeated variant rows of the product
|
||||
/// form (each row's fields are uniquely keyed, so the HashMap keeps them all).
|
||||
pub(crate) fn variant_indices(&self) -> Vec<usize> {
|
||||
let mut idx: Vec<usize> = self
|
||||
.fields
|
||||
.keys()
|
||||
.filter_map(|k| {
|
||||
let rest = k.strip_prefix("variants[")?;
|
||||
rest.split(']').next()?.parse::<usize>().ok()
|
||||
})
|
||||
.collect();
|
||||
idx.sort_unstable();
|
||||
idx.dedup();
|
||||
idx
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn read_multipart_form(mut multipart: Multipart) -> Result<MultipartForm> {
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
//! Admin product CRUD.
|
||||
//!
|
||||
//! A product is a presentation grouping; its purchasable options live in
|
||||
//! `product_variants` (each with its own label, sku, stock, regular price and
|
||||
//! optional public/business quick-sale prices), edited inline on the product
|
||||
//! form. The products list and the per-audience percentage discount profiles
|
||||
//! operate at the product level, previewing prices on each product's
|
||||
//! representative (first) variant.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
@@ -9,7 +16,6 @@ use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, ModelTrait, PaginatorTrait, QueryFilter,
|
||||
QueryOrder, Set, TransactionTrait,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{
|
||||
@@ -20,12 +26,13 @@ use crate::{
|
||||
},
|
||||
shared::{
|
||||
guard,
|
||||
money::{format_bp, format_price, parse_percent, parse_price_to_cents},
|
||||
money::{format_bp, format_price, parse_price_to_cents},
|
||||
pricing,
|
||||
slug::{slugify, unique_slug},
|
||||
},
|
||||
models::{
|
||||
audience_discount_profiles, categories, discount_profiles, product_images, products,
|
||||
audience_discount_profiles, categories, discount_profiles, product_images,
|
||||
product_variants, products,
|
||||
},
|
||||
views::shop as view,
|
||||
};
|
||||
@@ -45,10 +52,7 @@ struct ProductFields {
|
||||
name: String,
|
||||
slug: String,
|
||||
description: Option<String>,
|
||||
price_cents: i64,
|
||||
currency: String,
|
||||
sku: Option<String>,
|
||||
stock: i32,
|
||||
category_id: Option<i32>,
|
||||
published: bool,
|
||||
}
|
||||
@@ -61,19 +65,8 @@ async fn parse_product_fields(
|
||||
let name = form
|
||||
.text("name")
|
||||
.ok_or_else(|| Error::BadRequest("product name is required".to_string()))?;
|
||||
let price_cents = parse_price_to_cents(
|
||||
form.text("price")
|
||||
.ok_or_else(|| Error::BadRequest("price is required".to_string()))?
|
||||
.as_str(),
|
||||
)?;
|
||||
let currency = form.text("currency").unwrap_or_else(|| "EUR".to_string());
|
||||
let description = form.text("description");
|
||||
let sku = form.text("sku");
|
||||
let stock = form
|
||||
.text("stock")
|
||||
.and_then(|s| s.parse::<i32>().ok())
|
||||
.filter(|n| *n >= 0)
|
||||
.unwrap_or(0);
|
||||
let category_id = form.text("category_id").and_then(|s| s.parse::<i32>().ok());
|
||||
let published = form.checked("published");
|
||||
|
||||
@@ -98,15 +91,150 @@ async fn parse_product_fields(
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
price_cents,
|
||||
currency,
|
||||
sku,
|
||||
stock,
|
||||
category_id,
|
||||
published,
|
||||
})
|
||||
}
|
||||
|
||||
/// One variant row parsed from the product form.
|
||||
struct VariantInput {
|
||||
id: Option<i32>,
|
||||
label: String,
|
||||
sku: Option<String>,
|
||||
stock: i32,
|
||||
price_cents: i64,
|
||||
sale_cents: Option<i64>,
|
||||
business_sale_cents: Option<i64>,
|
||||
position: i32,
|
||||
}
|
||||
|
||||
/// An optional price field on a variant row (sale / business sale): blank means
|
||||
/// "no quick-sale", a value must parse and be below the regular price.
|
||||
fn parse_optional_sale(
|
||||
form: &MultipartForm,
|
||||
i: usize,
|
||||
key: &str,
|
||||
price_cents: i64,
|
||||
) -> Result<Option<i64>> {
|
||||
let Some(raw) = form.text(&format!("variants[{i}][{key}]")) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let cents = parse_price_to_cents(&raw)?;
|
||||
if cents <= 0 || cents >= price_cents {
|
||||
return Err(Error::BadRequest(
|
||||
"a sale price must be positive and below the regular price".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(Some(cents))
|
||||
}
|
||||
|
||||
/// Parse the repeated variant rows from the form, in submission order. Blank
|
||||
/// rows (no price and no label) are skipped; at least one valid row is required.
|
||||
fn parse_variants(form: &MultipartForm) -> Result<Vec<VariantInput>> {
|
||||
let mut out = Vec::new();
|
||||
for i in form.variant_indices() {
|
||||
let label = form
|
||||
.text(&format!("variants[{i}][label]"))
|
||||
.unwrap_or_default();
|
||||
let price_raw = form.text(&format!("variants[{i}][price]"));
|
||||
|
||||
let Some(price_raw) = price_raw else {
|
||||
// A completely empty leftover row is ignored; a labelled row without
|
||||
// a price is a mistake worth reporting.
|
||||
if label.is_empty() {
|
||||
continue;
|
||||
}
|
||||
return Err(Error::BadRequest(
|
||||
"each option needs a price".to_string(),
|
||||
));
|
||||
};
|
||||
let price_cents = parse_price_to_cents(&price_raw)?;
|
||||
if price_cents <= 0 {
|
||||
return Err(Error::BadRequest(
|
||||
"an option price must be positive".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let sku = form.text(&format!("variants[{i}][sku]"));
|
||||
let stock = form
|
||||
.text(&format!("variants[{i}][stock]"))
|
||||
.and_then(|s| s.parse::<i32>().ok())
|
||||
.filter(|n| *n >= 0)
|
||||
.unwrap_or(0);
|
||||
let sale_cents = parse_optional_sale(form, i, "sale", price_cents)?;
|
||||
let business_sale_cents = parse_optional_sale(form, i, "business_sale", price_cents)?;
|
||||
let id = form
|
||||
.text(&format!("variants[{i}][id]"))
|
||||
.and_then(|s| s.parse::<i32>().ok());
|
||||
|
||||
out.push(VariantInput {
|
||||
id,
|
||||
label,
|
||||
sku,
|
||||
stock,
|
||||
price_cents,
|
||||
sale_cents,
|
||||
business_sale_cents,
|
||||
position: out.len() as i32,
|
||||
});
|
||||
}
|
||||
if out.is_empty() {
|
||||
return Err(Error::BadRequest(
|
||||
"add at least one option with a price".to_string(),
|
||||
));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Apply a parsed variant row onto a (new or existing) active model.
|
||||
fn apply_variant(active: &mut product_variants::ActiveModel, input: &VariantInput) {
|
||||
active.label = Set(input.label.clone());
|
||||
active.sku = Set(input.sku.clone());
|
||||
active.stock = Set(input.stock);
|
||||
active.price_cents = Set(input.price_cents);
|
||||
active.sale_price_cents = Set(input.sale_cents);
|
||||
active.business_sale_price_cents = Set(input.business_sale_cents);
|
||||
active.position = Set(input.position);
|
||||
}
|
||||
|
||||
/// Reconcile the product's variants with the submitted rows inside `txn`: update
|
||||
/// rows carrying an id, insert rows without one, and delete existing variants no
|
||||
/// longer present.
|
||||
async fn sync_variants<C: ConnectionTrait>(
|
||||
txn: &C,
|
||||
product_id: i32,
|
||||
inputs: &[VariantInput],
|
||||
) -> Result<()> {
|
||||
let existing = product_variants::Entity::for_product(txn, product_id).await?;
|
||||
let keep: HashSet<i32> = inputs.iter().filter_map(|v| v.id).collect();
|
||||
for variant in &existing {
|
||||
if !keep.contains(&variant.id) {
|
||||
variant.clone().delete(txn).await?;
|
||||
}
|
||||
}
|
||||
let by_id: HashMap<i32, product_variants::Model> =
|
||||
existing.into_iter().map(|v| (v.id, v)).collect();
|
||||
for input in inputs {
|
||||
match input.id.and_then(|id| by_id.get(&id)) {
|
||||
Some(model) => {
|
||||
let mut active = model.clone().into_active_model();
|
||||
apply_variant(&mut active, input);
|
||||
active.update(txn).await?;
|
||||
}
|
||||
None => {
|
||||
let mut active = product_variants::ActiveModel {
|
||||
product_id: Set(product_id),
|
||||
..Default::default()
|
||||
};
|
||||
apply_variant(&mut active, input);
|
||||
active.insert(txn).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn form_context(ctx: &AppContext, jar: &CookieJar) -> Result<serde_json::Value> {
|
||||
let categories = categories::Entity::find()
|
||||
.order_by_asc(categories::Column::Position)
|
||||
@@ -116,6 +244,19 @@ async fn form_context(ctx: &AppContext, jar: &CookieJar) -> Result<serde_json::V
|
||||
Ok(json!({ "categories": categories, "lang": current_lang(jar) }))
|
||||
}
|
||||
|
||||
/// Variant shape used to pre-fill a row of the product form's variant editor.
|
||||
fn variant_form_json(variant: &product_variants::Model) -> serde_json::Value {
|
||||
json!({
|
||||
"id": variant.id,
|
||||
"label": variant.label,
|
||||
"sku": variant.sku,
|
||||
"stock": variant.stock,
|
||||
"price": format_price(variant.price_cents),
|
||||
"sale": variant.sale_price_cents.map(format_price),
|
||||
"business_sale": variant.business_sale_price_cents.map(format_price),
|
||||
})
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn index(
|
||||
auth: auth::JWT,
|
||||
@@ -140,9 +281,8 @@ async fn index(
|
||||
.order_by_desc(products::Column::CreatedAt)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
// Effective price each product carries for the active audience, after the
|
||||
// global per-product discount and any profiles assigned to that audience.
|
||||
let effective = pricing::audience_price_many(&ctx, &list, audience).await?;
|
||||
let ids: Vec<i32> = list.iter().map(|p| p.id).collect();
|
||||
let grouped = product_variants::Entity::grouped_for_products(&ctx.db, &ids).await?;
|
||||
|
||||
// Category sidebar tree (counts over the full, unfiltered list) plus the
|
||||
// active `?category=` filter applied to the rows.
|
||||
@@ -155,16 +295,38 @@ async fn index(
|
||||
.to_string();
|
||||
let filter = view::category_filter_ids(&all_categories, &selected_category);
|
||||
|
||||
let mut rows = Vec::new();
|
||||
for (product, priced) in list.iter().zip(effective.iter()) {
|
||||
// The kept products with their representative (first) variant, priced in one
|
||||
// batch for the active audience.
|
||||
let mut kept: Vec<(&products::Model, &Vec<product_variants::Model>)> = Vec::new();
|
||||
for product in &list {
|
||||
if !view::category_filter_keep(&filter, product.category_id) {
|
||||
continue;
|
||||
}
|
||||
if let Some(variants) = grouped.get(&product.id) {
|
||||
if !variants.is_empty() {
|
||||
kept.push((product, variants));
|
||||
}
|
||||
}
|
||||
}
|
||||
let reps: Vec<product_variants::Model> =
|
||||
kept.iter().map(|(_, vs)| vs[0].clone()).collect();
|
||||
let effective = pricing::audience_price_variants(&ctx, &reps, audience).await?;
|
||||
|
||||
let mut rows = Vec::new();
|
||||
for ((product, variants), priced) in kept.iter().zip(effective.iter()) {
|
||||
let image = product_images::first_for(&ctx, product.id).await?;
|
||||
let category_name = product
|
||||
.category_id
|
||||
.and_then(|id| category_name.get(&id).cloned());
|
||||
rows.push(product_row(product, priced, image, category_name, audience));
|
||||
let total_stock: i32 = variants.iter().map(|v| v.stock).sum();
|
||||
rows.push(product_row(
|
||||
product,
|
||||
priced,
|
||||
variants.len(),
|
||||
total_stock,
|
||||
image,
|
||||
category_name,
|
||||
));
|
||||
}
|
||||
|
||||
format::view(
|
||||
@@ -183,32 +345,32 @@ async fn index(
|
||||
)
|
||||
}
|
||||
|
||||
/// List-row shape: the product card fields plus the active audience's per-product
|
||||
/// discount and its resolved effective price (after profiles).
|
||||
/// List-row shape: the product card fields plus its representative variant's
|
||||
/// resolved effective price (after the active audience's profiles) and the count
|
||||
/// of options.
|
||||
fn product_row(
|
||||
product: &products::Model,
|
||||
effective: &pricing::PricedProduct,
|
||||
variant_count: usize,
|
||||
total_stock: i32,
|
||||
image: Option<String>,
|
||||
category_name: Option<String>,
|
||||
audience: &str,
|
||||
) -> serde_json::Value {
|
||||
let sale = current_value(product, audience);
|
||||
json!({
|
||||
"id": product.id,
|
||||
"name": product.name,
|
||||
"slug": product.slug,
|
||||
"currency": product.currency,
|
||||
"stock": product.stock,
|
||||
"stock": total_stock,
|
||||
"variant_count": variant_count,
|
||||
"has_options": variant_count > 1,
|
||||
"published": product.published,
|
||||
"image": image,
|
||||
"category_name": category_name,
|
||||
"regular_price": format_price(product.price_cents),
|
||||
"on_sale": sale.is_some(),
|
||||
"sale_price": sale.map(format_price),
|
||||
"percent_off": sale.map(|s| percent_off(product.price_cents, s)),
|
||||
"regular_price": format_price(effective.regular_cents),
|
||||
"effective_price": format_price(effective.price_cents),
|
||||
"effective_reduced": effective.is_reduced(),
|
||||
"effective_percent_off": percent_off(product.price_cents, effective.price_cents),
|
||||
"effective_percent_off": percent_off(effective.regular_cents, effective.price_cents),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -252,6 +414,7 @@ async fn new(
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let mut context = form_context(&ctx, &jar).await?;
|
||||
context["product"] = serde_json::Value::Null;
|
||||
context["variants"] = json!([]);
|
||||
format::view(&v, "admin/catalog/product_form.html", context)
|
||||
}
|
||||
|
||||
@@ -264,23 +427,23 @@ async fn create(
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let form = read_multipart_form(multipart).await?;
|
||||
let fields = parse_product_fields(&ctx, &form, None).await?;
|
||||
let variants = parse_variants(&form)?;
|
||||
|
||||
let txn = ctx.db.begin().await?;
|
||||
let product = products::ActiveModel {
|
||||
name: Set(fields.name),
|
||||
slug: Set(fields.slug),
|
||||
description: Set(fields.description),
|
||||
price_cents: Set(fields.price_cents),
|
||||
currency: Set(fields.currency),
|
||||
sku: Set(fields.sku),
|
||||
stock: Set(fields.stock),
|
||||
view_count: Set(0),
|
||||
published: Set(fields.published),
|
||||
published_at: Set(fields.published.then(|| chrono::Utc::now().into())),
|
||||
category_id: Set(fields.category_id),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
sync_variants(&txn, product.id, &variants).await?;
|
||||
|
||||
if let Some(data) = form.image {
|
||||
let filename = store_image(&ctx, data).await?;
|
||||
@@ -291,9 +454,10 @@ async fn create(
|
||||
alt: Set(None),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
}
|
||||
txn.commit().await?;
|
||||
|
||||
format::redirect("/admin/catalog/products")
|
||||
}
|
||||
@@ -309,8 +473,10 @@ async fn edit(
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let product = product_by_id(&ctx, id).await?;
|
||||
let image = product_images::first_for(&ctx, id).await?;
|
||||
let variants = product_variants::Entity::for_product(&ctx.db, id).await?;
|
||||
let mut context = form_context(&ctx, &jar).await?;
|
||||
context["product"] = view::product_form(&product, image);
|
||||
context["variants"] = json!(variants.iter().map(variant_form_json).collect::<Vec<_>>());
|
||||
format::view(&v, "admin/catalog/product_form.html", context)
|
||||
}
|
||||
|
||||
@@ -326,15 +492,14 @@ async fn update(
|
||||
let was_published = existing.published;
|
||||
let form = read_multipart_form(multipart).await?;
|
||||
let fields = parse_product_fields(&ctx, &form, Some(id)).await?;
|
||||
let variants = parse_variants(&form)?;
|
||||
|
||||
let txn = ctx.db.begin().await?;
|
||||
let mut product = existing.into_active_model();
|
||||
product.name = Set(fields.name);
|
||||
product.slug = Set(fields.slug);
|
||||
product.description = Set(fields.description);
|
||||
product.price_cents = Set(fields.price_cents);
|
||||
product.currency = Set(fields.currency);
|
||||
product.sku = Set(fields.sku);
|
||||
product.stock = Set(fields.stock);
|
||||
product.category_id = Set(fields.category_id);
|
||||
product.published = Set(fields.published);
|
||||
if fields.published && !was_published {
|
||||
@@ -342,7 +507,8 @@ async fn update(
|
||||
} else if !fields.published {
|
||||
product.published_at = Set(None);
|
||||
}
|
||||
product.update(&ctx.db).await?;
|
||||
product.update(&txn).await?;
|
||||
sync_variants(&txn, id, &variants).await?;
|
||||
|
||||
if let Some(data) = form.image {
|
||||
let filename = store_image(&ctx, data).await?;
|
||||
@@ -354,9 +520,10 @@ async fn update(
|
||||
alt: Set(None),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
}
|
||||
txn.commit().await?;
|
||||
|
||||
format::redirect("/admin/catalog/products")
|
||||
}
|
||||
@@ -372,23 +539,16 @@ async fn delete(
|
||||
format::redirect("/admin/catalog/products")
|
||||
}
|
||||
|
||||
// --- Discounts -------------------------------------------------------------
|
||||
// --- Discount profiles -----------------------------------------------------
|
||||
//
|
||||
// Two audiences, switched by an `?audience=` tab on the products page:
|
||||
// - **personal** (default): the public sale price (`products.sale_price_cents`)
|
||||
// everyone sees.
|
||||
// - **business**: a baseline discount for all company accounts
|
||||
// (`products.business_sale_price_cents`). Per-company profiles/negotiated
|
||||
// prices still layer on top (lowest price wins). Both are off the regular price.
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct DiscountForm {
|
||||
/// "fixed" (enter the new price) or "percent" (enter % off). Defaults to
|
||||
/// fixed for older/JSON callers.
|
||||
mode: Option<String>,
|
||||
sale_price: Option<String>,
|
||||
percent: Option<String>,
|
||||
}
|
||||
// - **personal** (default): what everyone sees.
|
||||
// - **business**: what company accounts see (per-company profiles/negotiated
|
||||
// prices still layer on top; lowest price wins).
|
||||
//
|
||||
// Per-product absolute quick-sale prices live on each variant and are edited in
|
||||
// the product form. This section is only the reusable *percentage* discount
|
||||
// profiles assigned to an audience.
|
||||
|
||||
fn read_audience(params: &HashMap<String, String>) -> &'static str {
|
||||
match params.get("audience").map(String::as_str) {
|
||||
@@ -397,32 +557,10 @@ fn read_audience(params: &HashMap<String, String>) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
fn current_value(product: &products::Model, audience: &str) -> Option<i64> {
|
||||
if audience == BUSINESS {
|
||||
product.business_sale_price_cents
|
||||
} else {
|
||||
product.sale_price_cents
|
||||
}
|
||||
}
|
||||
|
||||
fn set_value(active: &mut products::ActiveModel, audience: &str, value: Option<i64>) {
|
||||
if audience == BUSINESS {
|
||||
active.business_sale_price_cents = Set(value);
|
||||
} else {
|
||||
active.sale_price_cents = Set(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn list_redirect(audience: &str) -> Result<Response> {
|
||||
format::redirect(&format!("/admin/catalog/products?audience={audience}"))
|
||||
}
|
||||
|
||||
/// Resolve a percentage off the regular price into a fixed sale price in cents.
|
||||
fn percent_to_sale_cents(regular_cents: i64, percent: f64) -> i64 {
|
||||
let off = (regular_cents as f64 * percent / 100.0).round() as i64;
|
||||
regular_cents - off
|
||||
}
|
||||
|
||||
/// Percent off the regular price, rounded to a whole number.
|
||||
fn percent_off(regular_cents: i64, sale_cents: i64) -> i64 {
|
||||
if regular_cents <= 0 {
|
||||
@@ -432,6 +570,28 @@ fn percent_off(regular_cents: i64, sale_cents: i64) -> i64 {
|
||||
off.round() as i64
|
||||
}
|
||||
|
||||
/// Representative (first) variant for each product in `list`, in the same order,
|
||||
/// dropping products with no variants. Returns the products kept alongside their
|
||||
/// representative variant.
|
||||
async fn representatives<'a>(
|
||||
ctx: &AppContext,
|
||||
list: &'a [products::Model],
|
||||
) -> Result<(Vec<&'a products::Model>, Vec<product_variants::Model>)> {
|
||||
let ids: Vec<i32> = list.iter().map(|p| p.id).collect();
|
||||
let grouped = product_variants::Entity::grouped_for_products(&ctx.db, &ids).await?;
|
||||
let mut products_kept = Vec::new();
|
||||
let mut reps = Vec::new();
|
||||
for product in list {
|
||||
if let Some(variants) = grouped.get(&product.id) {
|
||||
if let Some(first) = variants.first() {
|
||||
products_kept.push(product);
|
||||
reps.push(first.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((products_kept, reps))
|
||||
}
|
||||
|
||||
/// Preview the effective prices that the submitted (unsaved) checkbox set would
|
||||
/// produce, without persisting anything. Returns OOB `<span>`s that htmx swaps
|
||||
/// into the effective-price column so the admin sees the effect before Save.
|
||||
@@ -461,14 +621,15 @@ async fn profiles_preview(
|
||||
.order_by_desc(products::Column::CreatedAt)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
let (products_kept, reps) = representatives(&ctx, &list).await?;
|
||||
let effective =
|
||||
pricing::audience_price_many_preview(&ctx, &list, audience, profile_ids).await?;
|
||||
pricing::audience_price_variants_preview(&ctx, &reps, audience, profile_ids).await?;
|
||||
|
||||
let selected_category = params.get("category").map(String::as_str).unwrap_or("all");
|
||||
let filter = view::category_filter_ids(&all_categories, selected_category);
|
||||
|
||||
let mut rows = Vec::new();
|
||||
for (product, priced) in list.iter().zip(effective.iter()) {
|
||||
for (product, priced) in products_kept.iter().zip(effective.iter()) {
|
||||
if !view::category_filter_keep(&filter, product.category_id) {
|
||||
continue;
|
||||
}
|
||||
@@ -477,7 +638,7 @@ async fn profiles_preview(
|
||||
"currency": product.currency,
|
||||
"effective_price": format_price(priced.price_cents),
|
||||
"effective_reduced": priced.is_reduced(),
|
||||
"effective_percent_off": percent_off(product.price_cents, priced.price_cents),
|
||||
"effective_percent_off": percent_off(priced.regular_cents, priced.price_cents),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -523,160 +684,6 @@ async fn sync_profiles(
|
||||
list_redirect(audience)
|
||||
}
|
||||
|
||||
/// What to pre-fill the discount form with: the chosen input mode and the raw
|
||||
/// values for each field, so a rejected submit (or a re-edit) shows what the
|
||||
/// admin had.
|
||||
#[derive(Default)]
|
||||
struct FormPrefill {
|
||||
mode: String,
|
||||
fixed: String,
|
||||
percent: String,
|
||||
}
|
||||
|
||||
fn render_discount_form(
|
||||
v: &TeraView,
|
||||
jar: &CookieJar,
|
||||
product: &products::Model,
|
||||
audience: &str,
|
||||
prefill: &FormPrefill,
|
||||
error: Option<&str>,
|
||||
) -> Result<Response> {
|
||||
let mode = if prefill.mode == "percent" { "percent" } else { "fixed" };
|
||||
format::view(
|
||||
v,
|
||||
"admin/catalog/discount_form.html",
|
||||
json!({
|
||||
"product": {
|
||||
"id": product.id,
|
||||
"name": product.name,
|
||||
"currency": product.currency,
|
||||
"regular_price": format_price(product.price_cents),
|
||||
"regular_cents": product.price_cents,
|
||||
},
|
||||
"audience": audience,
|
||||
"has_discount": current_value(product, audience).is_some(),
|
||||
"mode": mode,
|
||||
"fixed": prefill.fixed,
|
||||
"percent": prefill.percent,
|
||||
"error": error,
|
||||
"lang": current_lang(jar),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn discount_edit(
|
||||
auth: auth::JWT,
|
||||
jar: CookieJar,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
Path(id): Path<i32>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let audience = read_audience(¶ms);
|
||||
let product = product_by_id(&ctx, id).await?;
|
||||
// Re-editing always opens in fixed mode showing the current price.
|
||||
let prefill = FormPrefill {
|
||||
mode: "fixed".to_string(),
|
||||
fixed: current_value(&product, audience)
|
||||
.map(format_price)
|
||||
.unwrap_or_default(),
|
||||
percent: String::new(),
|
||||
};
|
||||
render_discount_form(&v, &jar, &product, audience, &prefill, None)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn discount_update(
|
||||
auth: auth::JWT,
|
||||
jar: CookieJar,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
Path(id): Path<i32>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<DiscountForm>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let audience = read_audience(¶ms);
|
||||
let product = product_by_id(&ctx, id).await?;
|
||||
|
||||
let mode = match form.mode.as_deref() {
|
||||
Some("percent") => "percent",
|
||||
_ => "fixed",
|
||||
};
|
||||
let fixed = form.sale_price.unwrap_or_default().trim().to_string();
|
||||
let percent = form.percent.unwrap_or_default().trim().to_string();
|
||||
|
||||
let prefill = FormPrefill {
|
||||
mode: mode.to_string(),
|
||||
fixed: fixed.clone(),
|
||||
percent: percent.clone(),
|
||||
};
|
||||
let render_err =
|
||||
|key: &str| render_discount_form(&v, &jar, &product, audience, &prefill, Some(key));
|
||||
|
||||
// Resolve the entered discount into a fixed sale price in cents. An empty
|
||||
// input in the active mode clears the discount (same as the Remove action).
|
||||
let sale_cents = if mode == "percent" {
|
||||
if percent.is_empty() {
|
||||
return clear_discount(&ctx, product, audience).await;
|
||||
}
|
||||
let pct = match parse_percent(&percent) {
|
||||
Some(pct) => pct,
|
||||
None => return render_err("discount-invalid"),
|
||||
};
|
||||
if pct <= 0.0 || pct >= 100.0 {
|
||||
return render_err("discount-percent-range");
|
||||
}
|
||||
percent_to_sale_cents(product.price_cents, pct)
|
||||
} else {
|
||||
if fixed.is_empty() {
|
||||
return clear_discount(&ctx, product, audience).await;
|
||||
}
|
||||
match parse_price_to_cents(&fixed) {
|
||||
Ok(cents) => cents,
|
||||
Err(_) => return render_err("discount-invalid"),
|
||||
}
|
||||
};
|
||||
|
||||
if sale_cents <= 0 {
|
||||
return render_err("discount-must-be-positive");
|
||||
}
|
||||
if sale_cents >= product.price_cents {
|
||||
return render_err("discount-below-regular");
|
||||
}
|
||||
|
||||
let mut active = product.into_active_model();
|
||||
set_value(&mut active, audience, Some(sale_cents));
|
||||
active.update(&ctx.db).await?;
|
||||
list_redirect(audience)
|
||||
}
|
||||
|
||||
async fn clear_discount(
|
||||
ctx: &AppContext,
|
||||
product: products::Model,
|
||||
audience: &str,
|
||||
) -> Result<Response> {
|
||||
let mut active = product.into_active_model();
|
||||
set_value(&mut active, audience, None);
|
||||
active.update(&ctx.db).await?;
|
||||
list_redirect(audience)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn discount_remove(
|
||||
auth: auth::JWT,
|
||||
Path(id): Path<i32>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let audience = read_audience(¶ms);
|
||||
let product = product_by_id(&ctx, id).await?;
|
||||
clear_discount(&ctx, product, audience).await
|
||||
}
|
||||
|
||||
pub fn routes() -> Routes {
|
||||
let image_limit = DefaultBodyLimit::max(IMAGE_MAX_BYTES + 1024 * 1024);
|
||||
Routes::new()
|
||||
@@ -697,16 +704,4 @@ pub fn routes() -> Routes {
|
||||
post(update).layer(image_limit),
|
||||
)
|
||||
.add("/admin/catalog/products/{id}/delete", post(delete))
|
||||
.add(
|
||||
"/admin/catalog/products/{id}/discount/edit",
|
||||
get(discount_edit),
|
||||
)
|
||||
.add(
|
||||
"/admin/catalog/products/{id}/discount",
|
||||
post(discount_update),
|
||||
)
|
||||
.add(
|
||||
"/admin/catalog/products/{id}/discount/remove",
|
||||
post(discount_remove),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{controllers::i18n::current_lang, shared::{guard, money::format_price, pricing}, models::products};
|
||||
use crate::{controllers::i18n::current_lang, shared::{guard, money::format_price, pricing}, models::{product_variants, products}};
|
||||
use axum::{
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::Redirect,
|
||||
@@ -15,22 +15,22 @@ const CART_MAX_AGE_DAYS: i64 = 30;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct AddForm {
|
||||
product_id: i32,
|
||||
variant_id: i32,
|
||||
quantity: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct UpdateForm {
|
||||
product_id: i32,
|
||||
variant_id: i32,
|
||||
quantity: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct RemoveForm {
|
||||
product_id: i32,
|
||||
variant_id: i32,
|
||||
}
|
||||
|
||||
/// Parse the `cart` cookie ("id:qty,id:qty") into `(product_id, quantity)`
|
||||
/// Parse the `cart` cookie ("id:qty,id:qty") into `(variant_id, quantity)`
|
||||
/// pairs, silently dropping malformed or non-positive entries.
|
||||
pub(crate) fn parse_cart(jar: &CookieJar) -> Vec<(i32, i32)> {
|
||||
let Some(cookie) = jar.get(CART_COOKIE) else {
|
||||
@@ -64,12 +64,23 @@ fn cart_cookie(value: String) -> Cookie<'static> {
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Look up a published product, returning its current stock cap.
|
||||
async fn published_product(ctx: &AppContext, id: i32) -> Result<Option<products::Model>> {
|
||||
Ok(products::Entity::find_by_id(id)
|
||||
/// Look up a variant whose product is published, returning the variant together
|
||||
/// with its parent product (for name/slug/currency).
|
||||
async fn published_variant(
|
||||
ctx: &AppContext,
|
||||
variant_id: i32,
|
||||
) -> Result<Option<(product_variants::Model, products::Model)>> {
|
||||
let Some(variant) = product_variants::Entity::find_by_id(variant_id)
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let product = products::Entity::find_by_id(variant.product_id)
|
||||
.filter(products::Column::Published.eq(true))
|
||||
.one(&ctx.db)
|
||||
.await?)
|
||||
.await?;
|
||||
Ok(product.map(|p| (variant, p)))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
@@ -79,16 +90,16 @@ async fn add(
|
||||
headers: HeaderMap,
|
||||
Form(form): Form<AddForm>,
|
||||
) -> Result<Response> {
|
||||
let Some(product) = published_product(&ctx, form.product_id).await? else {
|
||||
let Some((variant, _product)) = published_variant(&ctx, form.variant_id).await? else {
|
||||
return Err(Error::NotFound);
|
||||
};
|
||||
|
||||
let mut items = parse_cart(&jar);
|
||||
let add_qty = form.quantity.unwrap_or(1).max(1);
|
||||
if let Some(entry) = items.iter_mut().find(|(id, _)| *id == product.id) {
|
||||
entry.1 = (entry.1 + add_qty).min(product.stock);
|
||||
if let Some(entry) = items.iter_mut().find(|(id, _)| *id == variant.id) {
|
||||
entry.1 = (entry.1 + add_qty).min(variant.stock);
|
||||
} else {
|
||||
items.push((product.id, add_qty.min(product.stock)));
|
||||
items.push((variant.id, add_qty.min(variant.stock)));
|
||||
}
|
||||
items.retain(|(_, qty)| *qty > 0);
|
||||
|
||||
@@ -117,14 +128,14 @@ async fn update(
|
||||
headers: HeaderMap,
|
||||
Form(form): Form<UpdateForm>,
|
||||
) -> Result<Response> {
|
||||
let stock = published_product(&ctx, form.product_id)
|
||||
let stock = published_variant(&ctx, form.variant_id)
|
||||
.await?
|
||||
.map(|p| p.stock)
|
||||
.map(|(v, _)| v.stock)
|
||||
.unwrap_or(0);
|
||||
|
||||
let mut items = parse_cart(&jar);
|
||||
let clamped = form.quantity.clamp(0, stock);
|
||||
if let Some(entry) = items.iter_mut().find(|(id, _)| *id == form.product_id) {
|
||||
if let Some(entry) = items.iter_mut().find(|(id, _)| *id == form.variant_id) {
|
||||
entry.1 = clamped;
|
||||
}
|
||||
items.retain(|(_, qty)| *qty > 0);
|
||||
@@ -142,7 +153,7 @@ async fn remove(
|
||||
Form(form): Form<RemoveForm>,
|
||||
) -> Result<Response> {
|
||||
let mut items = parse_cart(&jar);
|
||||
items.retain(|(id, _)| *id != form.product_id);
|
||||
items.retain(|(id, _)| *id != form.variant_id);
|
||||
|
||||
let jar = jar.add(cart_cookie(serialize_cart(&items)));
|
||||
cart_response(&ctx, &v, jar, &headers).await
|
||||
@@ -192,38 +203,40 @@ pub(crate) async fn resolve_cart(
|
||||
// Resolve the cart entries to in-stock products first, then price them all
|
||||
// for the current viewer in one batch (the price depends on who's logged in).
|
||||
let user = guard::current_user(ctx, jar).await;
|
||||
let mut items: Vec<(products::Model, i32)> = Vec::new();
|
||||
let mut items: Vec<(product_variants::Model, products::Model, i32)> = Vec::new();
|
||||
for (id, qty) in parse_cart(jar) {
|
||||
let Some(product) = published_product(ctx, id).await? else {
|
||||
let Some((variant, product)) = published_variant(ctx, id).await? else {
|
||||
continue;
|
||||
};
|
||||
let qty = qty.clamp(0, product.stock);
|
||||
let qty = qty.clamp(0, variant.stock);
|
||||
if qty == 0 {
|
||||
continue;
|
||||
}
|
||||
items.push((product, qty));
|
||||
items.push((variant, product, qty));
|
||||
}
|
||||
let products_only: Vec<products::Model> = items.iter().map(|(p, _)| p.clone()).collect();
|
||||
let priced = pricing::price_many(ctx, &products_only, user.as_ref()).await?;
|
||||
let variants_only: Vec<product_variants::Model> =
|
||||
items.iter().map(|(v, _, _)| v.clone()).collect();
|
||||
let priced = pricing::price_variants(ctx, &variants_only, user.as_ref()).await?;
|
||||
|
||||
let mut lines = Vec::new();
|
||||
let mut valid = Vec::new();
|
||||
let mut total: i64 = 0;
|
||||
for ((product, qty), priced) in items.iter().zip(priced.iter()) {
|
||||
for ((variant, product, qty), priced) in items.iter().zip(priced.iter()) {
|
||||
let unit_price = priced.price_cents;
|
||||
let line_total = unit_price * i64::from(*qty);
|
||||
total += line_total;
|
||||
valid.push((product.id, *qty));
|
||||
valid.push((variant.id, *qty));
|
||||
lines.push(json!({
|
||||
"id": product.id,
|
||||
"id": variant.id,
|
||||
"name": product.name,
|
||||
"variant_label": variant.label,
|
||||
"slug": product.slug,
|
||||
"price": format_price(unit_price),
|
||||
"regular_price": format_price(priced.regular_cents),
|
||||
"on_sale": priced.is_reduced(),
|
||||
"currency": product.currency,
|
||||
"quantity": qty,
|
||||
"stock": product.stock,
|
||||
"stock": variant.stock,
|
||||
"line_total": format_price(line_total),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -9,22 +9,40 @@ use serde_json::json;
|
||||
use crate::{
|
||||
controllers::i18n::current_lang,
|
||||
shared::{guard, pricing},
|
||||
models::{categories, product_images, products, users},
|
||||
models::{categories, product_images, product_variants, products, users},
|
||||
views::shop as view,
|
||||
};
|
||||
|
||||
/// Shape a list of products into card rows for `user` (None = public), pricing
|
||||
/// each via [`pricing::price_many`] and loading its primary image.
|
||||
/// Shape a list of products into card rows for `user` (None = public). Each card
|
||||
/// shows the resolved price of the product's representative (first) variant; the
|
||||
/// `variant_count` lets the template render "from {price}" for multi-variant
|
||||
/// products. Products with no variants are skipped (not purchasable).
|
||||
async fn product_rows(
|
||||
ctx: &AppContext,
|
||||
user: Option<&users::Model>,
|
||||
list: Vec<products::Model>,
|
||||
) -> Result<Vec<serde_json::Value>> {
|
||||
let priced = pricing::price_many(ctx, &list, user).await?;
|
||||
let mut rows = Vec::with_capacity(list.len());
|
||||
for (product, priced) in list.iter().zip(priced.iter()) {
|
||||
let ids: Vec<i32> = list.iter().map(|p| p.id).collect();
|
||||
let grouped = product_variants::Entity::grouped_for_products(&ctx.db, &ids).await?;
|
||||
|
||||
// Representative (first) variant per product, in list order, dropping any
|
||||
// product that has no variants.
|
||||
let mut entries: Vec<(&products::Model, product_variants::Model, usize)> = Vec::new();
|
||||
for product in &list {
|
||||
if let Some(variants) = grouped.get(&product.id) {
|
||||
if let Some(first) = variants.first() {
|
||||
entries.push((product, first.clone(), variants.len()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let reps: Vec<product_variants::Model> = entries.iter().map(|(_, v, _)| v.clone()).collect();
|
||||
let priced = pricing::price_variants(ctx, &reps, user).await?;
|
||||
|
||||
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, priced, image, None));
|
||||
rows.push(view::product_card(product, rep, priced, *count, image, None));
|
||||
}
|
||||
Ok(rows)
|
||||
}
|
||||
@@ -121,13 +139,44 @@ async fn show(
|
||||
};
|
||||
|
||||
let user = guard::current_user(&ctx, &jar).await;
|
||||
let priced = pricing::price_for(&ctx, &product, user.as_ref()).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))
|
||||
.collect();
|
||||
// The card header uses the representative (first) variant for its headline
|
||||
// price; the picker below lets the customer switch.
|
||||
let representative = variants.first();
|
||||
let priced = variant_prices.first().copied();
|
||||
let card = match (representative, priced) {
|
||||
(Some(rep), Some(priced)) => view::product_card(
|
||||
&product,
|
||||
rep,
|
||||
&priced,
|
||||
variants.len(),
|
||||
None,
|
||||
category.as_ref().map(|c| c.name.clone()),
|
||||
),
|
||||
// A product with no variants isn't purchasable; show it without a price.
|
||||
_ => serde_json::json!({
|
||||
"id": product.id,
|
||||
"name": product.name,
|
||||
"slug": product.slug,
|
||||
"description": product.description,
|
||||
"currency": product.currency,
|
||||
"variant_count": 0,
|
||||
"has_options": false,
|
||||
}),
|
||||
};
|
||||
let c = guard::chrome_from(&ctx, user.as_ref());
|
||||
format::view(
|
||||
&v,
|
||||
"shop/show.html",
|
||||
json!({
|
||||
"product": view::product_card(&product, &priced, None, category.as_ref().map(|c| c.name.clone())),
|
||||
"product": card,
|
||||
"variants": options,
|
||||
"images": images.iter().map(|i| i.image_id.clone()).collect::<Vec<_>>(),
|
||||
"category": category,
|
||||
"logged_in_admin": c.logged_in_admin,
|
||||
|
||||
Reference in New Issue
Block a user