discounts

This commit is contained in:
Priec
2026-06-21 22:33:47 +02:00
parent 2ee87fbdd7
commit 9ce1cb97f0
20 changed files with 399 additions and 10 deletions

View File

@@ -16,6 +16,7 @@ pub struct Model {
#[sea_orm(column_type = "Text", nullable)]
pub description: Option<String>,
pub price_cents: i64,
pub sale_price_cents: Option<i64>,
pub currency: String,
pub sku: Option<String>,
pub stock: i32,

View File

@@ -61,13 +61,15 @@ pub async fn place(ctx: &AppContext, items: &[(i32, i32)], details: Checkout) ->
)));
}
currency = product.currency.clone();
subtotal += product.price_cents * i64::from(*qty);
// Snapshot the effective price (honouring any active discount).
let unit_price_cents = product.effective_price_cents();
subtotal += unit_price_cents * i64::from(*qty);
let mut active = product.clone().into_active_model();
active.stock = Set(product.stock - *qty);
active.update(&txn).await?;
snapshots.push((product.id, product.name, product.price_cents, *qty));
snapshots.push((product.id, product.name, unit_price_cents, *qty));
}
let order = ActiveModel {

View File

@@ -19,7 +19,25 @@ impl ActiveModelBehavior for ActiveModel {
}
// implement your read-oriented logic here
impl Model {}
impl Model {
/// Whether a discount is currently active: a sale price is set and is
/// strictly below the regular price.
#[must_use]
pub fn on_sale(&self) -> bool {
matches!(self.sale_price_cents, Some(sale) if sale < self.price_cents)
}
/// The price actually charged: the sale price when [`Model::on_sale`],
/// otherwise the regular price.
#[must_use]
pub fn effective_price_cents(&self) -> i64 {
if self.on_sale() {
self.sale_price_cents.unwrap_or(self.price_cents)
} else {
self.price_cents
}
}
}
// implement your write-oriented logic here
impl ActiveModel {}