code wiki / _hdl_build / nx_cms_ecommerce.nx

nx_cms_ecommerce.nx source

↩ module page · 61 lines · 2996 B

1// nx_cms_ecommerce.nx -- CMS ECOMMERCE core (sovereign cart, exact money, inventory, order FSM). The 2// WooCommerce class, made Nishi-native: ALL money is INTEGER CENTS (basis-point tax/discount) so there is 3// ZERO floating-point rounding drift (the exceed angle -- float money is a real WooCommerce/Stripe pain), 4// inventory is OVERSELL-PROOF (stock can never go negative, an over-quantity is refused not clamped), and 5// the order lifecycle is an EXACT state machine. Pure deterministic integer logic -- the payment gateway is 6// the only last-mile boundary; the commerce core is sovereign. Rates are the data-driven seam (rule 11). 7// license_tier: ORIGINAL 8import "nx_syscalls.nx" 9 10// order lifecycle states 11const NX_ORD_CART: i64 = 0 12const NX_ORD_PENDING: i64 = 1 // placed, awaiting payment 13const NX_ORD_PAID: i64 = 2 14const NX_ORD_SHIPPED: i64 = 3 15const NX_ORD_CANCELLED: i64 = 4 16 17const NX_EC_BPS: i64 = 10000 // basis-point denominator (100.00% = 10000 bps) 18 19// a single line total in cents = unit price (cents) * quantity. Exact, no float. 20func ec_line_total(unit_cents: i64, qty: i64) -> i64 { return unit_cents * qty } 21 22// cart subtotal in cents across n lines. Exact integer accumulation. 23func ec_cart_subtotal(unit_cents: *i64, qty: *i64, n: i64) -> i64 { 24 var sum: i64 = 0 25 var i: i64 = 0 26 while i < n { sum = sum + ec_line_total(unit_cents[i], qty[i]); i = i + 1 } 27 return sum 28} 29 30// apply a tax rate in basis points (e.g. 825 = 8.25%): integer floor, deterministic, no float drift. 31func ec_apply_tax(subtotal_cents: i64, tax_bps: i64) -> i64 { 32 return subtotal_cents + (subtotal_cents * tax_bps) / NX_EC_BPS 33} 34 35// apply a discount rate in basis points (e.g. 1500 = 15% off): integer floor, deterministic. 36func ec_apply_discount(subtotal_cents: i64, discount_bps: i64) -> i64 { 37 return subtotal_cents - (subtotal_cents * discount_bps) / NX_EC_BPS 38} 39 40// is a stock request fulfillable? quantity >= 1 and not more than what's available (no overselling). 41func ec_stock_ok(available: i64, requested: i64) -> i64 { 42 if requested < 1 { return 0 } 43 if requested > available { return 0 } 44 return 1 45} 46 47// decrement stock for a request: only if fulfillable, else UNCHANGED (refused). Never goes negative. 48func ec_decrement_stock(available: i64, requested: i64) -> i64 { 49 if ec_stock_ok(available, requested) == 0 { return available } 50 return available - requested 51} 52 53// is an order lifecycle transition allowed? (idempotent re-set ok; no backwards/illegal jumps) 54func ec_order_can_transition(from: i64, to: i64) -> i64 { 55 if from == to { return 1 } 56 if from == NX_ORD_CART { if to == NX_ORD_PENDING { return 1 } if to == NX_ORD_CANCELLED { return 1 } } 57 if from == NX_ORD_PENDING { if to == NX_ORD_PAID { return 1 } if to == NX_ORD_CANCELLED { return 1 } } 58 if from == NX_ORD_PAID { if to == NX_ORD_SHIPPED { return 1 } if to == NX_ORD_CANCELLED { return 1 } } 59 // SHIPPED and CANCELLED are terminal 60 return 0 61}