personal discounts to businesses done

This commit is contained in:
Priec
2026-06-21 23:21:24 +02:00
parent ed566b5347
commit c713627a2c
35 changed files with 912 additions and 39 deletions

View File

@@ -4,6 +4,8 @@ use sea_orm::{Set, TransactionTrait};
use uuid::Uuid;
use crate::models::_entities::{order_items, products, shipping_methods};
use crate::models::users;
use crate::shared::pricing;
pub use crate::models::_entities::orders::{ActiveModel, Column, Entity, Model};
pub type Orders = Entity;
@@ -42,7 +44,12 @@ fn generate_order_number() -> String {
/// snapshot each product's price/name, decrement stock (re-checking inside the
/// transaction so an item can't oversell between cart and pay), then write the
/// order and its line items. Returns the persisted order.
pub async fn place(ctx: &AppContext, items: &[(i32, i32)], details: Checkout) -> Result<Model> {
pub async fn place(
ctx: &AppContext,
items: &[(i32, i32)],
details: Checkout,
user: Option<&users::Model>,
) -> Result<Model> {
let txn = ctx.db.begin().await?;
let mut subtotal: i64 = 0;
@@ -61,8 +68,10 @@ pub async fn place(ctx: &AppContext, items: &[(i32, i32)], details: Checkout) ->
)));
}
currency = product.currency.clone();
// Snapshot the effective price (honouring any active discount).
let unit_price_cents = product.effective_price_cents();
// Snapshot the price the buyer actually pays — public sale or, for a
// business account, their negotiated/lowest price (same resolver the
// cart and storefront use).
let unit_price_cents = pricing::price_for(ctx, &product, user).await?.price_cents;
subtotal += unit_price_cents * i64::from(*qty);
let mut active = product.clone().into_active_model();