A pure, zero-dependency Domain Model wrapped in a reactive Filament 5 Admin Panel.
Engineered with strict Pest Architecture tests, commercial integer cent arithmetic, and transparent, waterfall-explainable pricing.
Your star helps fellow developers discover clean Domain-Driven Design and strict architecture patterns in modern PHP 8.4.
In typical e-commerce codebases, pricing logic quickly decays into deeply nested SQL queries, fragile floating-point math, and unexplainable black-box discounts. When a customer or B2B buyer asks:
βWhy does this item cost exactly this price?β
...traditional systems cannot provide a proof. Smart Pricing Engine solves this permanently.
Product: Premium T-Shirt (Base Price: 29.90 β¬)
Order Quantity: 25 pieces
Customer Group: B2B (Business Account)
Active Campaign: SUMMER2026 (15 % Off)
Sequential Rule Evaluation (Strict Priority Pipeline):
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
1. Base Price 29.90 β¬
2. Quantity Tier Discount (25β49 pcs, 15 %) - 4.49 β¬ β 25.41 β¬
3. B2B Customer Group Discount (10 %) - 2.54 β¬ β 22.87 β¬
4. Summer Campaign Discount (SUMMER2026, 15 %) - 3.43 β¬ β 19.44 β¬
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Total Discount per piece: - 10.46 β¬ (34.98 % Off)
Final Price per piece: 19.44 β¬
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Total Order Position (25 pieces): 486.00 β¬
Total Position Savings: - 261.50 β¬ (from 747.50 β¬)
Every single cent calculation uses commercial rounding (PHP_ROUND_HALF_UP), strictly respecting configurable Floor Prices (MinimumPrice) and full rule-rejection auditing.
flowchart TD
subgraph InfrastructureLayer["Infrastructure Layer (Frameworks & Drivers)"]
Filament["Filament 5 UI & Livewire 4"]
MariaDB[("MariaDB 11 (Eloquent Data Mappers)")]
RedisCache[("Redis 7 (Tagged Caching)")]
AuditDB[("pricing_calculations (Immutable Audit Trail)")]
end
subgraph ApplicationLayer["Application Layer (Use Cases & Orchestration)"]
CalculatePriceCommand["CalculatePriceCommand (Immutable DTO)"]
CalculatePriceHandler["CalculatePriceHandler"]
Ports["Repository & Lookup Ports (Interfaces)"]
end
subgraph DomainLayer["Core Domain Layer (PURE PHP 8.4 - ZERO FRAMEWORK DEPENDENCY)"]
PricingEngine["PricingEngine (Sequential Pipeline)"]
Rules["Rules: QuantityDiscountRule | CustomerGroupDiscountRule | CampaignDiscountRule | CouponDiscountRule"]
ValueObjects["Value Objects: Money | Percentage | Quantity | Discount | RulePriority | CustomerGroup"]
Results["Results: PricingResult | AppliedPricingRule | RejectedPricingRule"]
end
Filament --> CalculatePriceCommand
CalculatePriceCommand --> CalculatePriceHandler
CalculatePriceHandler --> Ports
Ports --> MariaDB
CalculatePriceHandler --> PricingEngine
PricingEngine --> Rules
Rules --> ValueObjects
PricingEngine --> Results
CalculatePriceHandler --> AuditDB
- Domain Layer (
app/Domain/Pricing/) β PURE PHP 8.4:- Zero Framework Footprint: Imports absolutely no framework classes (
Illuminate\*,Filament\*,Symfony\*). Enforced automatically by Pest Arch tests on every commit. - Immutable Value Objects: All Value Objects are
readonly classwith cent-integer arithmetic (int $amountInCents). Floating-point money bugs are mathematically impossible. - Floor Price Ceiling Protection: No combination of volume discounts, VIP status, or vouchers can breach the merchant's configured minimum floor price (
FloorPrice).
- Zero Framework Footprint: Imports absolutely no framework classes (
- Application Layer (
app/Application/Pricing/):- Pure CQRS-style commands (
CalculatePriceCommand) and handlers (CalculatePriceHandler). - Completely agnostic of whether requests originate from the Filament Admin Panel, REST API, CLI, or GraphQL.
- Pure CQRS-style commands (
- Infrastructure Layer (
app/Infrastructure/):- Data Mapper Pattern: Clean separation where Eloquent models (
ProductModel,CustomerModel,PricingRuleModel) are purely persistence mappers. They never bleed into Domain logic. - Immutable Audit Trail: Calculations automatically write immutable audit records to
pricing_calculationsfor auditing and compliance.
- Data Mapper Pattern: Clean separation where Eloquent models (
The engine includes a 100% native Filament 5 administrative interface:
| Feature | Description |
|---|---|
| Interactive Simulator | Real-time pricing calculation with 1-click test scenarios (B2B Bulk, Retail, Coupon Valid, Coupon Rejected). |
| Waterfall Explanation | Interactive table dissecting every deduction step, initial price, running price, and rule metadata. |
| Dual-Mode Discounts | Rules support both Percentage (%) and Fixed Euro Amount (β¬) deductions. |
| Dynamic Form Validation | Rule parameters dynamically adapt per type (QUANTITY, CUSTOMER_GROUP, CAMPAIGN, COUPON) with strict validation. |
| Audit Log Inspection | Read-only inspection of all historic price calculations with full JSON explainability dumps. |
- Chain of Responsibility / Pipeline: Dynamic sequential rule processing ordered by
RulePriority. - Specification Pattern: Business rules encapsulate criteria (
minQuantity,targetGroup,dateRange,minCartValue) and return explicit rejection reasons. - Strategy Pattern: Polymorphic discount calculations supporting both relative percentages and absolute currency amounts.
- Data Mapper Pattern: Bidirectional translation between relational tables and immutable domain entities.
- Value Object Pattern:
Money,Percentage,Quantity,Discounteliminating Primitive Obsession.
Get the entire application and database running locally with Docker via ./zenv:
git clone https://github.com/allgorithm/zenv.git smart-pricing-engine
cd smart-pricing-engine
./zenv up./zenv artisan migrate --seedCreates demo catalog (T-Shirt, Jeans, Hoodie), customer accounts (B2B, VIP), and standard pricing rules.
- URL: http://localhost/admin/pricing-simulator
- Default Credentials:
admin@zenv.local/password
The test suite runs with Pest 5 and Pest Arch Plugin:
./zenv pest PASS Tests\Architecture\LayerRulesTest
β domain layer must not depend on external frameworks or infrastructure
β application layer only depends on domain and support, never on infrastructure
β no debugging statements left in production code
β strict types are enforced across the entire application
PASS Tests\Architecture\PricingDomainArchTest
β pricing domain layer must not depend on illuminate or framework infrastructure
β pricing value objects must be readonly classes
β pricing domain exceptions must extend DomainException or Exception
PASS Tests\Unit\Domain\Pricing\BoundaryRulesTest
β Quantity Discount Boundary Tests (4, 5, 9, 10, 24, 25, 49, 50+ pieces)
β Campaign Date Boundary Tests (Before start, during window, after expiration)
β Coupon Cart Value Boundary Tests (49,99 β¬ rejected vs 50,00 β¬ applied)
PASS Tests\Feature\CalculatePriceFeatureTest
β it calculates pricing end-to-end using database-backed repositories
β it calculates fixed euro discount rule correctly
Tests: 42 passed (127 assertions)
Run Code Style verification:
./zenv vendor/bin/pint --testapp/
βββ Domain/Pricing/ # π‘οΈ PURE PHP 8.4 (ZERO FRAMEWORK DEPS)
β βββ Context/PricingContext.php
β βββ Exceptions/ # Domain Exceptions (FloorPrice, NegativeMoney, etc.)
β βββ Ports/ # Repository & Lookup Interfaces
β βββ Results/ # Explainable Result Objects
β βββ Rules/ # Strategy & Specification Pricing Rules
β βββ Services/PricingEngine.php # Sequential Domain Engine
β βββ ValueObjects/ # Immutable Money, Quantity, Percentage, Discount
β
βββ Application/Pricing/ # βοΈ APPLICATION LAYER
β βββ Commands/CalculatePriceCommand.php
β βββ Handlers/CalculatePriceHandler.php
β
βββ Infrastructure/ # π INFRASTRUCTURE LAYER
β βββ Persistence/Eloquent/ # Eloquent Models & Data Mappers
β βββ Providers/PricingServiceProvider.php
β
βββ Filament/ # π¨ PRESENTATION LAYER (FILAMENT 5)
βββ Pages/PricingSimulator.php # Livewire Simulator & Explainability Page
βββ Resources/ # Product, Customer, Rule & Audit Resources
Contributions are welcome! Please ensure:
- All changes adhere strictly to the DDD layers (keep
Domain/framework-free). - All new Value Objects and Domain Rules include full boundary unit tests.
- Architecture tests pass (
./zenv pest). - Code passes style checks (
./zenv vendor/bin/pint).