Units of Measure System
Relevant source files
The following files were used as context for generating this wiki page:
The Units of Measure (UoM) system in DRX is a core architectural component designed to handle flexible inventory tracking and sales calculations across different physical forms of products. It specifically supports weight-based tracking (grams/ounces) for solids and unit-based tracking (count/ounces) for liquids and discrete items. This system ensures high-precision calculations for Cost of Goods Sold (COGS), inventory depletion, and bulk pricing.
The system is primarily used for retail operations requiring high granularity, such as dispensaries. It allows for "Canonical Stock" tracking where a base unit is defined (e.g., Grams), and various conversions or aliases are applied to deduct the correct amount from that base stock during a sale.
Sources: README.md:144-152, scripts/help-center-data.ts:400-410
Core Data Schema
The database schema provides the foundation for unit-based tracking by extending the standard inventory table with specialized fields for weight and unit conversions.
Inventory Extensions
The inventory table includes several critical columns to handle weight-based logic:
| Column | Type | Description |
|---|---|---|
base_unit | VARCHAR(10) | The primary measurement unit (default 'g' for grams). |
convert_to_grams | DECIMAL(12, 4) | Multiplier used to convert units into the base gram measurement. |
inventory_in_grams | DECIMAL(16, 4) | The current stock level represented in the base unit of grams. |
allow_fractional_weight | BOOLEAN | Flag to permit sales of non-integer quantities (e.g., 3.5g). |
price_per_gram | DECIMAL(12, 4) | The standard price assigned to a single unit of the base measurement. |
Sources: db/migrations/20250228000001_pos_weight_shrinkage_loyalty_consolidated.sql:50-55
Stock Movement Tracking
Every change in stock, whether via sale, intake, or shrinkage, is recorded in the stock_movements table with high precision for both quantity and weight.
The stock_movements table distinguishes between quantity_change (for unit-based items) and weight_change_grams (for weight-based items) to maintain an accurate audit trail.
Sources: db/migrations/20250228000001_pos_weight_shrinkage_loyalty_consolidated.sql:85-98
Unit Conversion & Canonical Stock
DRX utilizes a "Canonical Stock" model. This means regardless of how a product is sold (as a single unit, a pre-roll, or a 1/8th ounce), the system translates that quantity back to the base unit for inventory deduction.
Measurement Categories
The system categorizes products to determine the default unit automatically:
- Solids: Measured in grams (g) - used for cannabis flower, edibles, etc.
- Liquids: Measured in ounces (oz) - used for beverages, syrups, and oils.
Sources: README.md:144-152
Conversion Logic
When a conversion is applied (e.g., an "eighth"), the system calculates the deduction based on the convert_to_grams multiplier. For instance, selling "1/8th" triggers a 3.5g deduction from the canonical stock.
Sources: scripts/help-center-data.ts:405-410
Cost & Intake Calculations
The intake system processes incoming inventory by calculating the cost per base unit. This ensures that even when products are purchased in bulk, the per-unit value is tracked for profitability analysis.
Intake Processing Logic
During inventory intake, the system performs the following calculations:
- Cost Per Gram:
Total Cost / Total Weight in Grams. - New Quantity:
Current Quantity + Total Weight in Grams. - Total Intake Cost:
Total Cost + Delivery Cost.
Sources: tests/inventory-intake-processing.unit.test.ts:16-43
Precision and Validation
The system maintains high decimal precision (up to 12 decimal places in tests) to prevent rounding errors during bulk-to-unit conversions. Validation rules require a minimum weight of 0.01g for an intake to be considered valid.
Sources: tests/inventory-intake-processing.unit.test.ts:107-120, tests/inventory-intake-processing.unit.test.ts:45-50
POS Integration & Bulk Pricing
The Point of Sale (POS) system dynamically interacts with the UoM system to calculate real-time pricing and inventory updates during checkout.
Weight-Based Sales Flow
The following sequence describes how the system processes a sale for a weight-based item:
Bulk Pricing Rules
The system supports bulk_pricing_rules which allow the price_per_gram to change based on the quantity sold. This is managed via tiers (e.g., 1g, 3.5g, 7g).
| Tier Name | Min Weight (g) | Max Weight (g) | Result |
|---|---|---|---|
| 1g | 1.0 | 3.4 | Standard Price |
| 3.5g | 3.5 | 6.9 | Discounted Price per gram |
| 10g+ | 10.0 | NULL | Bulk Price per gram |
Sources: db/migrations/20250228000001_pos_weight_shrinkage_loyalty_consolidated.sql:58-71, db/migrations/20250228000001_pos_weight_shrinkage_loyalty_consolidated.json:step_8
Summary
The Units of Measure System is a robust framework within DRX that enables precise tracking of diverse inventory types. By centralizing all measurements around a canonical base unit (primarily grams) and providing flexible conversion, intake, and pricing logic, it ensures business accuracy across the supply chain from intake to the point of sale. This precision is critical for maintaining correct stock levels, calculating accurate COGS, and enforcing complex pricing structures like bulk discounts.