# Product Lot Sequence Per Product This module extends Odoo's lot and serial number generation to support unique sequences per product, aligning Odoo 18 with the behavior introduced in Odoo 19. ## Features - **Per-Product Sequence Configuration**: Define a unique sequence for lot/serial number generation for each product. - **Inventory Tab Integration**: Configure the custom sequence directly on the product form under the Inventory tab. - **Automatic Generation**: Lot/serial numbers generated during incoming receipts and manufacturing orders follow the product-specific sequence. - **Performance Optimized**: Batch generation for large quantities (500,000+ units) using optimized database queries. - **Fallback Mechanism**: If no sequence is defined for a product, it falls back to the global lot/serial sequence. - **UI Enhancements**: Avoids generation of invalid "0" lot numbers in manual and wizard flows. ## Configuration 1. Navigate to **Inventory > Products**. 2. Open or create a product. 3. Go to the **Inventory** tab. 4. Set the **Custom Lot/Serial** prefix to define a new sequence or select an existing sequence from the **Lot Sequence** field. 5. The **Next Number** field displays the next lot/serial number that will be generated. ## Behavior - **Incoming Shipments**: When receiving products, if a product has a custom sequence, the generated lot/serial numbers will follow this sequence. - **Manufacturing Orders**: When producing products, the finished lots/serials will be generated using the product's custom sequence. - **Manual Creation**: Creating lots/serials manually or via "Generate Serials/Lots" will respect the product's sequence if configured. ## Performance Optimizations The module includes several performance optimizations for handling large quantities: ### Batch Sequence Allocation - Uses PostgreSQL's `generate_series()` to allocate multiple sequence numbers in a single database query - Reduces database operations from N queries to 1 query for N lots - Automatically activated for quantities > 10 units ### Batch Lot Creation - Creates all lot records in a single `create()` operation - Significantly faster for large quantities (100+ units) - Maintains data integrity and uniqueness ### Performance Benchmarks - **Small batch (10 units)**: < 5 seconds - **Medium batch (100 units)**: < 10 seconds - **Large batch (500 units)**: < 30 seconds - **Very large batch (5,000 units)**: < 2 minutes - **Extreme batch (500,000 units)**: Optimized for production use ### When Optimizations Apply - Batch allocation: Automatically used when generating > 10 lots at once - Applies to: Incoming shipments, manufacturing orders, and manual generation ## Technical Details - The module adds a `lot_sequence_id` field to `product.template` to link the sequence. - It overrides the `stock.lot` creation to use the product's sequence with batch optimization. - It extends `stock.move` and `stock.move.line` to handle UI inputs and normalize "0" or empty inputs. - It overrides `mrp.production._prepare_stock_lot_values` to ensure manufacturing flows use the product sequence. - Uses `_allocate_sequence_batch()` method for efficient sequence number allocation. ## Testing The module includes comprehensive test suites: ### Performance Tests Run performance tests to verify optimization: ```bash odoo-bin -c odoo.conf -d your_database --test-tags product_lot_sequence_per_product.performance ``` ### Inventory Adjustment Tests Test auto-generation in inventory adjustments: ```bash odoo-bin -c odoo.conf -d your_database --test-tags product_lot_sequence_per_product ``` ## Dependencies - `stock` - `mrp` ## Compatibility - Odoo 18 - Requires `mrp` module for manufacturing order support