diff --git a/docs/docs/api/schema.md b/docs/docs/api/schema.md index f4ce2837f3..ba610a0773 100644 --- a/docs/docs/api/schema.md +++ b/docs/docs/api/schema.md @@ -7,7 +7,7 @@ The API schema as documented below is generated using the [drf-spectactular](htt ## API Version -This documentation is for API version: `352` +This documentation is for API version: `449` !!! tip "API Schema History" We track API schema changes, and provide a snapshot of each API schema version in the [API schema repository](https://github.com/inventree/schema/). diff --git a/docs/docs/assets/images/stock/part_tracking_history.png b/docs/docs/assets/images/stock/part_tracking_history.png new file mode 100644 index 0000000000..cf31097ffe Binary files /dev/null and b/docs/docs/assets/images/stock/part_tracking_history.png differ diff --git a/docs/docs/assets/images/stock/stock_item_tracking_history.png b/docs/docs/assets/images/stock/stock_item_tracking_history.png new file mode 100644 index 0000000000..895fb61e04 Binary files /dev/null and b/docs/docs/assets/images/stock/stock_item_tracking_history.png differ diff --git a/docs/docs/hooks.py b/docs/docs/hooks.py index 52e5998e51..2cd00cd586 100644 --- a/docs/docs/hooks.py +++ b/docs/docs/hooks.py @@ -304,6 +304,6 @@ def on_post_build(*args, **kwargs): if missing: raise NotImplementedError( 'Missing Settings:\n' - + f"There are {len(missing)} missing settings in the '{group}' group:\n" + + f"There are {len(missing)} missing settings in the '{group}' group:\n- " + '\n- '.join(missing) ) diff --git a/docs/docs/part/stocktake.md b/docs/docs/part/stocktake.md index e38e514d24..a226587b91 100644 --- a/docs/docs/part/stocktake.md +++ b/docs/docs/part/stocktake.md @@ -1,10 +1,10 @@ --- -title: Part Stock History +title: Part Stocktake --- -## Part Stock History +## Part Stocktake -InvenTree can track the historical stock levels of parts, allowing users to view past stocktake data and generate reports based on this information. +InvenTree can record the historical stock levels of parts, allowing users to view past stocktake data and generate reports based on this information. A *Stocktake* refers to a "snapshot" of stock levels for a particular part, at a specific point in time. Stocktake information is used for tracking a historical record of the quantity and value of part stock. @@ -75,9 +75,9 @@ A dashboard widget is available for generating stocktake reports, which can be a Here, the user can specify the report parameters, and then click the *Generate Report* button to generate a new stocktake report based on the specified parameters. -## Stock History Settings +## Stocktake Settings -There are a number of configuration options available in the [settings view](../settings/global.md): +There are a number of configuration options available for controlling the behavior of part stocktake functionality in the [system settings view](../settings/global.md): | Name | Description | Default | Units | | ---- | ----------- | ------- | ----- | @@ -87,9 +87,9 @@ There are a number of configuration options available in the [settings view](../ {{ globalsetting("STOCKTAKE_DELETE_OLD_ENTRIES")}} {{ globalsetting("STOCKTAKE_DELETE_DAYS") }} -{{ image("part/part_stocktake_settings.png", "Stock history settings") }} +{{ image("part/part_stocktake_settings.png", "Stocktake settings") }} -### Enable Stock History +### Enable Stocktake Enable or disable stocktake functionality. Note that by default, stocktake functionality is disabled. @@ -97,10 +97,10 @@ Enable or disable stocktake functionality. Note that by default, stocktake funct Configure the number of days between generation of [automatic stocktake reports](#automatic-stocktake). If this value is set to zero, automatic stocktake reports will not be generated. -### Delete Old Stock History Entries +### Delete Old Stocktake Entries -If enabled, stock history entries older than the specified number of days will be automatically deleted from the database. +If enabled, stocktake entries older than the specified number of days will be automatically deleted from the database. -### Stock History Deletion Interval +### Stocktake Deletion Interval -Configure how many days historical stock records are retained in the database. +Configure how many days historical stocktake records are retained in the database. diff --git a/docs/docs/stock/index.md b/docs/docs/stock/index.md index d020dd67d1..d3c544a3c8 100644 --- a/docs/docs/stock/index.md +++ b/docs/docs/stock/index.md @@ -2,13 +2,15 @@ title: Stock --- -## Stock Items +## Stock Item A *Stock Item* is an actual instance of a [*Part*](../part/index.md) item. It represents a physical quantity of the *Part* in a specific location. +Each Part instance may have multiple stock items associated with it, in various quantities and locations. Additionally, each stock item may have a serial number (if the part is tracked by serial number) and may be associated with a particular supplier part (if the item was purchased from a supplier). + ### Stock Item Details -The *Stock Item* detail view shows information regarding the particular stock item: +Each *Stock Item* is linked to the following information: **Part** - Which *Part* this stock item is an instance of @@ -26,17 +28,27 @@ The *Stock Item* detail view shows information regarding the particular stock it **Status** - Status of this stock item -### Stock Availability +**Serial Number** - If the part is tracked by serial number, the unique serial number of this stock item + +**Batch Code** - If the part is tracked by batch code, the batch code of this stock item + +## Stock Availability InvenTree has a number of different mechanisms to determine whether stock is available for use. See the [Stock Availability](./availability.md) page for more information. -### Stock Tracking +## Traceability + +Stock items can be associated with a unique serial number and / or a batch code, which allows for traceability of individual stock items. This is particularly useful for tracking the history of specific items, and for ensuring that items can be traced back to their source (e.g. supplier, purchase order, etc). + +Refer to the [traceability](./traceability.md) page for more information on how serial numbers and batch codes work in InvenTree. + +## Stock Tracking Every time a *Stock Item* is adjusted, a *Stock Tracking* entry is automatically created. This ensures a complete history of the *Stock Item* is maintained as long as the item is in the system. Each stock tracking historical item records the user who performed the action. [Read more about stock tracking here](./tracking.md). -## Stock Location +## Stock Locations A stock location represents a physical real-world location where *Stock Items* are stored. Locations are arranged in a cascading manner and each location may contain multiple sub-locations, or stock, or both. @@ -60,3 +72,7 @@ in the build order line items view where the material is allocated. {{ image("stock/stock_external_icon.png", title="External stock indication") }} The external flag does not get inherited to sublocations. + +### Structural Locations + +A stock location may be optionally marked as *structural*. Structural locations are used to represent physical locations which are not directly associated with stock items, but rather serve as a means of organizing the stock location hierarchy. For example, a structural location might represent a particular shelf or drawer within a warehouse, while the actual stock items are stored in sub-locations within that location. diff --git a/docs/docs/stock/status.md b/docs/docs/stock/status.md index 8a50d02a5e..d153047941 100644 --- a/docs/docs/stock/status.md +++ b/docs/docs/stock/status.md @@ -36,6 +36,8 @@ Refer to the source code for the Stock status codes: show_source: True members: [] +### Custom Status Codes + Stock Status supports [custom states](../concepts/custom_states.md). ### Default Status Code diff --git a/docs/docs/stock/traceability.md b/docs/docs/stock/traceability.md new file mode 100644 index 0000000000..5fb21aa608 --- /dev/null +++ b/docs/docs/stock/traceability.md @@ -0,0 +1,152 @@ +--- +title: Stock Traceability +--- + +## Stock Traceability + +It may be desirable to track individual stock items, or groups of stock items, with unique identifier values. Stock items may be *tracked* using either *Batch Codes* or *Serial Numbers*. + +Individual stock items can be assigned a batch code, or a serial number, or both, or neither, as requirements dictate. + +{{ image("stock/batch_and_serial.png", title="Batch and Serial Number Tracking") }} + +Out of the box, the default implementations for both batch codes and serial numbers are (intentionally) simplistic. + +As the particular requirements for serial number or batch code conventions may vary significantly from one application to another, InvenTree provides the ability for custom plugins to determine exactly how batch codes and serial numbers are implemented. + +### Batch Codes + +Batch codes can be used to specify a particular "group" of items, and can be assigned to any stock item without restriction. Batch codes are tracked even as stock items are split into separate items. + +Multiple stock items may share the same batch code without restriction, even across different parts. + +#### Generating Batch Codes + +Batch codes can be generated automatically based on a provided pattern. The default pattern simply uses the current date-code as the batch number, however this can be customized within a certain scope. + +{{ image("stock/batch_code_template.png", title="Batch code pattern") }} + +#### Context Variables + +The following context variables are available by default when generating a batch code using the builtin generation functionality: + +| Variable | Description | +| --- | --- | +| year | The current year e.g. `2024` | +| month | The current month number, e.g. `5` | +| day | The current day of month, e.g. `21` | +| hour | The current hour of day, in 24-hour format, e.g. `23` | +| minute | The current minute of hour, e.g. `17` | +| week | The current week of year, e.g. `51` | + +#### Plugin Support + +To implement custom batch code functionality, refer to the details on the [Validation Plugin Mixin](../plugins/mixins/validation.md#batch-codes). + +### Serial Numbers + +A serial "number" is used to uniquely identify a single, unique stock item. Note that while *number* is used throughout the documentation, these values are not required to be numeric. + +#### Uniqueness Requirements + +By default, serial numbers must be unique across any given [Part](../part/index.md) instance (including any variants of that part). + +However, it is also possible to specify that serial numbers must be globally unique across all types of parts. This is configurable in the settings display (see below): + +{{ image("stock/serial_numbers_unique.png", title="Serial number uniqueness") }} + +#### Generating Serial Numbers + +When creating a group of serialized stock items, it can be very useful for the user to be able to generate a group of unique serial numbers, with one serial number for each serialized stock item. + +{{ image("stock/serial_next.png", title="Serial number entry") }} + +For a given serial number *schema* (either the in-built schema or a custom schema defined by a plugin), a group (or *range*) of serial numbers can be generated using a number of possible patterns: + +##### Comma Separated Values + +Individual serial numbers can be specified by separating using a comma character (`,`). + +| Pattern | Serial Numbers | +| --- | --- | +| `1, 2, 45, 99, 101` | `1, 2, 45, 99, 101` | + +##### Hyphen Separated Range + +Use a hyphen character (`-`) to specify a *range* of sequential values, inclusive of the two values separated by the hyphen. + +| Pattern | Serial Numbers | +| --- | --- | +| `10-15` | `10, 11, 12, 13, 14, 15` | + +##### Starting Value Range + +A *starting value* can be supplied, followed by the plus (`+`) character to indicate a number of sequential values following the provided starting value. The `+` character should be followed by an integer value to indicate the number of serial numbers which will be generated. + +| Pattern | Serial Numbers | +| --- | --- | +| `10+3` | `10, 11, 12, 13` | +| `100 + 2` | `100, 101, 102` | + +##### Next Value + +When specifying serial numbers, the tilde (`~`) character is replaced with the next available serial number. It can be used in combination with the available patterns specified above. + +For example, if the *next* available serial number is `100`, the following patterns can be used: + +| Pattern | Serial Numbers | +| --- | --- | +| `~` | `100` | +| `~, ~, ~` | `100, 101, 102` | +| `800, ~, 900` | `800, 100, 900` | +| `~+5` | `100, 101, 102, 103, 104, 105` | + +##### Combination Groups + +Any of the above patterns can be combined using multiple groups separated by the comma (`,`) character: + +| Pattern | Serial Numbers | +| --- | --- | +| `1, 2, 4-7, 10` | `1, 2, 4, 5, 6, 7, 10` | +| `40+4, 50+4` | `40, 41, 42, 43, 44, 50, 51, 52, 53, 54` | +| `10, 14, 20+3, 30-35` | `10, 14, 20, 21, 22, 23, 30, 31, 32, 33, 34, 35` | + +In the default implementation, InvenTree assumes that serial "numbers" are integer values in a simple incrementing sequence e.g. `{1, 2, 3, 4, 5, 6}`. When generating the *next* value for a serial number, the algorithm looks for the *most recent* serial number, and attempts to coerce that value into an integer, and then increment that value. + +While this approach is reasonably robust, it is definitely simplistic and is not expected to meet the requirements of every installation. For this reason, more complex serial number management is intended to be implemented using a custom plugin (see below). + +#### Serial Number Errors + +If a provided serial number (or group of numbers) is not considered valid, an error message is provided to the user. + +##### Example: Invalid Quantity + +{{ image("stock/serial_error_quantity.png", title="Serial number - invalid quantity") }} + +##### Example: Duplicate Serial Numbers + +{{ image("stock/serial_error_unique.png", title="Serial number - duplicate values") }} + +##### Example: Invalid Serial Numbers + +!!! tip "Serial Number Validation" + Custom serial number validation can be implemented using an external plugin + +#### Adjusting Serial Numbers + +Once a stock item has been created with a serial number, it is possible to adjust that serial number value if required. This can be achieved by navigating to the stock item's detail page, and selecting the "Edit" option from the actions menu. Here, the serial number value can be modified as required: + +{{ image("stock/serial_edit.png", title="Editing a serial number") }} + +Note that any serial number adjustments are subject to the same validation rules as when the stock item was created. If the new serial number value is not valid, an error message will be displayed to the user: + +{{ image("stock/serial_edit_error.png", title="Error while editing a serial number") }} + + +#### Plugin Support + +Custom serial number functionality, with any arbitrary requirements or level of complexity, can be implemented using the [Validation Plugin Mixin class](../plugins/mixins/validation.md#serial-numbers). Refer to the documentation for this plugin for technical details. + +A custom plugin allows the user to determine how a "valid" serial number is defined, and (crucially) how any given serial number value is incremented to provide the next value in the sequence. + +Implementing custom methods for these two considerations allows for complex serial number schema to be supported with minimal effort. diff --git a/docs/docs/stock/tracking.md b/docs/docs/stock/tracking.md index 5ce0809602..309c682ba7 100644 --- a/docs/docs/stock/tracking.md +++ b/docs/docs/stock/tracking.md @@ -4,149 +4,47 @@ title: Stock Tracking ## Stock Tracking -It may be desirable to track individual stock items, or groups of stock items, with unique identifier values. Stock items may be *tracked* using either *Batch Codes* or *Serial Numbers*. +Stock tracking entries record the history of stock item adjustments, including the user who performed the action, the date of the action, and the quantity change. This allows users to maintain a complete history of stock item movements and adjustments over time. -Individual stock items can be assigned a batch code, or a serial number, or both, or neither, as requirements dictate. +### Tracking Events -{{ image("stock/batch_and_serial.png", title="Batch and Serial Number Tracking") }} +Stock tracking entries are created automatically whenever a stock item is adjusted, either through manual adjustments or through automated processes such as order fulfillment or build completion. -Out of the box, the default implementations for both batch codes and serial numbers are (intentionally) simplistic. +Some examples of events that may trigger stock tracking entries include: -As the particular requirements for serial number or batch code conventions may vary significantly from one application to another, InvenTree provides the ability for custom plugins to determine exactly how batch codes and serial numbers are implemented. +- Manual stock adjustments (e.g. correcting inventory counts) +- Creation of new stock items (e.g. receiving new inventory) +- Allocation of stock items to orders (e.g. shipping items against sales orders) +- Consumption of stock items during build processes (e.g. using items to complete a build order) -### Batch Codes +## Viewing Stock Tracking History -Batch codes can be used to specify a particular "group" of items, and can be assigned to any stock item without restriction. Batch codes are tracked even as stock items are split into separate items. +There are multiple ways to view the stock tracking history for a particular stock item or part via the user interface. -Multiple stock items may share the same batch code without restriction, even across different parts. +### Stock Item Tracking History -#### Generating Batch Codes +The stock tracking history for a particular stock item can be viewed on the *Stock Item Detail* page, under the *Stock Tracking* tab: -Batch codes can be generated automatically based on a provided pattern. The default pattern simply uses the current date-code as the batch number, however this can be customized within a certain scope. +{{ image("stock/stock_item_tracking_history.png", title="Stock tracking tab") }} -{{ image("stock/batch_code_template.png", title="Batch code pattern") }} +This view displays all tracking entries associated with the particular stock item. -#### Context Variables +### Part Tracking History -The following context variables are available by default when generating a batch code using the builtin generation functionality: +Additionally, the stock tracking history for a particular part can be viewed on the *Part Detail* page, under the *Stock History* tab: -| Variable | Description | -| --- | --- | -| year | The current year e.g. `2024` | -| month | The current month number, e.g. `5` | -| day | The current day of month, e.g. `21` | -| hour | The current hour of day, in 24-hour format, e.g. `23` | -| minute | The current minute of hour, e.g. `17` | -| week | The current week of year, e.g. `51` | +{{ image("stock/part_tracking_history.png", title="Part stock tracking history") }} -#### Plugin Support +This view displays all tracking entries associated with any stock item linked to the particular part. -To implement custom batch code functionality, refer to the details on the [Validation Plugin Mixin](../plugins/mixins/validation.md#batch-codes). +!!! info "Deleted Stock Items" + Even if a stock item is deleted from the system, the associated stock tracking entries are retained for historical reference. They will be visible in the part tracking history, but not in the stock item tracking history (as the stock item itself has been deleted). -### Serial Numbers +## Stock Tracking Settings -A serial "number" is used to uniquely identify a single, unique stock item. Note that while *number* is used throughout the documentation, these values are not required to be numeric. +There are a number of configuration options available for controlling the behavior of stock tracking functionality in the [system settings view](../settings/global.md): -#### Uniqueness Requirements - -By default, serial numbers must be unique across any given [Part](../part/index.md) instance (including any variants of that part). - -However, it is also possible to specify that serial numbers must be globally unique across all types of parts. This is configurable in the settings display (see below): - -{{ image("stock/serial_numbers_unique.png", title="Serial number uniqueness") }} - -#### Generating Serial Numbers - -When creating a group of serialized stock items, it can be very useful for the user to be able to generate a group of unique serial numbers, with one serial number for each serialized stock item. - -{{ image("stock/serial_next.png", title="Serial number entry") }} - -For a given serial number *schema* (either the in-built schema or a custom schema defined by a plugin), a group (or *range*) of serial numbers can be generated using a number of possible patterns: - -##### Comma Separated Values - -Individual serial numbers can be specified by separating using a comma character (`,`). - -| Pattern | Serial Numbers | -| --- | --- | -| `1, 2, 45, 99, 101` | `1, 2, 45, 99, 101` | - -##### Hyphen Separated Range - -Use a hyphen character (`-`) to specify a *range* of sequential values, inclusive of the two values separated by the hyphen. - -| Pattern | Serial Numbers | -| --- | --- | -| `10-15` | `10, 11, 12, 13, 14, 15` | - -##### Starting Value Range - -A *starting value* can be supplied, followed by the plus (`+`) character to indicate a number of sequential values following the provided starting value. The `+` character should be followed by an integer value to indicate the number of serial numbers which will be generated. - -| Pattern | Serial Numbers | -| --- | --- | -| `10+3` | `10, 11, 12, 13` | -| `100 + 2` | `100, 101, 102` | - -##### Next Value - -When specifying serial numbers, the tilde (`~`) character is replaced with the next available serial number. It can be used in combination with the available patterns specified above. - -For example, if the *next* available serial number is `100`, the following patterns can be used: - -| Pattern | Serial Numbers | -| --- | --- | -| `~` | `100` | -| `~, ~, ~` | `100, 101, 102` | -| `800, ~, 900` | `800, 100, 900` | -| `~+5` | `100, 101, 102, 103, 104, 105` | - -##### Combination Groups - -Any of the above patterns can be combined using multiple groups separated by the comma (`,`) character: - -| Pattern | Serial Numbers | -| --- | --- | -| `1, 2, 4-7, 10` | `1, 2, 4, 5, 6, 7, 10` | -| `40+4, 50+4` | `40, 41, 42, 43, 44, 50, 51, 52, 53, 54` | -| `10, 14, 20+3, 30-35` | `10, 14, 20, 21, 22, 23, 30, 31, 32, 33, 34, 35` | - -In the default implementation, InvenTree assumes that serial "numbers" are integer values in a simple incrementing sequence e.g. `{1, 2, 3, 4, 5, 6}`. When generating the *next* value for a serial number, the algorithm looks for the *most recent* serial number, and attempts to coerce that value into an integer, and then increment that value. - -While this approach is reasonably robust, it is definitely simplistic and is not expected to meet the requirements of every installation. For this reason, more complex serial number management is intended to be implemented using a custom plugin (see below). - -#### Serial Number Errors - -If a provided serial number (or group of numbers) is not considered valid, an error message is provided to the user. - -##### Example: Invalid Quantity - -{{ image("stock/serial_error_quantity.png", title="Serial number - invalid quantity") }} - -##### Example: Duplicate Serial Numbers - -{{ image("stock/serial_error_unique.png", title="Serial number - duplicate values") }} - -##### Example: Invalid Serial Numbers - -!!! tip "Serial Number Validation" - Custom serial number validation can be implemented using an external plugin - -#### Adjusting Serial Numbers - -Once a stock item has been created with a serial number, it is possible to adjust that serial number value if required. This can be achieved by navigating to the stock item's detail page, and selecting the "Edit" option from the actions menu. Here, the serial number value can be modified as required: - -{{ image("stock/serial_edit.png", title="Editing a serial number") }} - -Note that any serial number adjustments are subject to the same validation rules as when the stock item was created. If the new serial number value is not valid, an error message will be displayed to the user: - -{{ image("stock/serial_edit_error.png", title="Error while editing a serial number") }} - - -#### Plugin Support - -Custom serial number functionality, with any arbitrary requirements or level of complexity, can be implemented using the [Validation Plugin Mixin class](../plugins/mixins/validation.md#serial-numbers). Refer to the documentation for this plugin for technical details. - -A custom plugin allows the user to determine how a "valid" serial number is defined, and (crucially) how any given serial number value is incremented to provide the next value in the sequence. - -Implementing custom methods for these two considerations allows for complex serial number schema to be supported with minimal effort. +| Name | Description | Default | Units | +| ---- | ----------- | ------- | ----- | +{{ globalsetting("STOCK_TRACKING_DELETE_OLD_ENTRIES") }} +{{ globalsetting("STOCK_TRACKING_DELETE_DAYS") }} diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index b43acc31df..a79df4ce30 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -138,6 +138,7 @@ nav: - Stock: - Stock Items: stock/index.md - Availability: stock/availability.md + - Traceability: stock/traceability.md - Stock Tracking: stock/tracking.md - Stock Status: stock/status.md - Adjusting Stock: stock/adjust.md diff --git a/src/backend/InvenTree/InvenTree/api_version.py b/src/backend/InvenTree/InvenTree/api_version.py index dae49d459d..a844018e94 100644 --- a/src/backend/InvenTree/InvenTree/api_version.py +++ b/src/backend/InvenTree/InvenTree/api_version.py @@ -1,11 +1,15 @@ """InvenTree API version information.""" # InvenTree API version -INVENTREE_API_VERSION = 449 +INVENTREE_API_VERSION = 450 """Increment this API version number whenever there is a significant change to the API that any clients need to know about.""" INVENTREE_API_TEXT = """ +v450 -> 2026-02-10 : https://github.com/inventree/InvenTree/pull/11260 + - Adds "part" field to the StockItemTracking model and API endpoints + - Additional filtering options for the StockItemTracking API endpoint + v449 -> 2026-02-07 : https://github.com/inventree/InvenTree/pull/11266 - Add missing nullable annotations to PartStocktakeSerializer diff --git a/src/backend/InvenTree/build/models.py b/src/backend/InvenTree/build/models.py index 03219f62a4..497aeeece6 100644 --- a/src/backend/InvenTree/build/models.py +++ b/src/backend/InvenTree/build/models.py @@ -1173,6 +1173,7 @@ class Build( user, notes=notes, deltas={ + 'quantity': float(quantity), 'location': location.pk, 'status': StockStatus.REJECTED.value, 'buildorder': self.pk, @@ -1267,7 +1268,11 @@ class Build( output.save(add_note=False) - deltas = {'status': status, 'buildorder': self.pk} + deltas = { + 'status': status, + 'buildorder': self.pk, + 'quantity': float(output.quantity), + } if location: deltas['location'] = location.pk diff --git a/src/backend/InvenTree/common/setting/system.py b/src/backend/InvenTree/common/setting/system.py index a216b5d1f5..3d02e9ccfd 100644 --- a/src/backend/InvenTree/common/setting/system.py +++ b/src/backend/InvenTree/common/setting/system.py @@ -1099,7 +1099,7 @@ SYSTEM_SETTINGS: dict[str, InvenTreeSettingsKeyType] = { 'validator': bool, }, 'STOCKTAKE_ENABLE': { - 'name': _('Enable Stock History'), + 'name': _('Enable Stocktake'), 'description': _( 'Enable functionality for recording historical stock levels and value' ), @@ -1109,30 +1109,47 @@ SYSTEM_SETTINGS: dict[str, InvenTreeSettingsKeyType] = { 'STOCKTAKE_EXCLUDE_EXTERNAL': { 'name': _('Exclude External Locations'), 'description': _( - 'Exclude stock items in external locations from stock history calculations' + 'Exclude stock items in external locations from stocktake calculations' ), 'validator': bool, 'default': False, }, 'STOCKTAKE_AUTO_DAYS': { 'name': _('Automatic Stocktake Period'), - 'description': _('Number of days between automatic stock history recording'), + 'description': _('Number of days between automatic stocktake recording'), 'validator': [int, MinValueValidator(1)], 'default': 7, 'units': _('days'), }, 'STOCKTAKE_DELETE_OLD_ENTRIES': { - 'name': _('Delete Old Stock History Entries'), + 'name': _('Delete Old Stocktake Entries'), 'description': _( - 'Delete stock history entries older than the specified number of days' + 'Delete stocktake entries older than the specified number of days' ), 'default': False, 'validator': bool, }, 'STOCKTAKE_DELETE_DAYS': { - 'name': _('Stock History Deletion Interval'), + 'name': _('Stocktake Deletion Interval'), 'description': _( - 'Stock history entries will be deleted after specified number of days' + 'Stocktake entries will be deleted after specified number of days' + ), + 'default': 365, + 'units': _('days'), + 'validator': [int, MinValueValidator(30)], + }, + 'STOCK_TRACKING_DELETE_OLD_ENTRIES': { + 'name': _('Delete Old Stock Tracking Entries'), + 'description': _( + 'Delete stock tracking entries older than the specified number of days' + ), + 'default': False, + 'validator': bool, + }, + 'STOCK_TRACKING_DELETE_DAYS': { + 'name': _('Stock Tracking Deletion Interval'), + 'description': _( + 'Stock tracking entries will be deleted after specified number of days' ), 'default': 365, 'units': _('days'), diff --git a/src/backend/InvenTree/order/models.py b/src/backend/InvenTree/order/models.py index 6a54c2b277..2d06ca09ef 100644 --- a/src/backend/InvenTree/order/models.py +++ b/src/backend/InvenTree/order/models.py @@ -2915,7 +2915,12 @@ class ReturnOrder(TotalPriceMixin, Order): if status is None: status = StockStatus.QUARANTINED.value - deltas = {'status': status, 'returnorder': self.pk, 'location': location.pk} + deltas = { + 'status': status, + 'returnorder': self.pk, + 'location': location.pk, + 'quantity': float(line.quantity), + } if stock_item.customer: deltas['customer'] = stock_item.customer.pk diff --git a/src/backend/InvenTree/order/test_api.py b/src/backend/InvenTree/order/test_api.py index 3166ca3134..9b3a4283de 100644 --- a/src/backend/InvenTree/order/test_api.py +++ b/src/backend/InvenTree/order/test_api.py @@ -1267,7 +1267,7 @@ class PurchaseOrderReceiveTest(OrderTest): ], 'location': location.pk, }, - max_query_count=104 + 2 * N_LINES, + max_query_count=104 + 3 * N_LINES, ).data # Check for expected response diff --git a/src/backend/InvenTree/part/models.py b/src/backend/InvenTree/part/models.py index e36b8e36db..2d040e539c 100644 --- a/src/backend/InvenTree/part/models.py +++ b/src/backend/InvenTree/part/models.py @@ -4128,7 +4128,7 @@ class BomItem(InvenTree.models.MetadataMixin, InvenTree.models.InvenTreeModel): # Normalize decimal values to ensure consistent representation # These values are only included if they are non-zero - # This is to provide some backwards compatibility from before these fields were addede + # This is to provide some backwards compatibility from before these fields were added if value is not None and field in [ 'quantity', 'attrition', diff --git a/src/backend/InvenTree/part/tasks.py b/src/backend/InvenTree/part/tasks.py index a7008f7576..87bc6a2f84 100644 --- a/src/backend/InvenTree/part/tasks.py +++ b/src/backend/InvenTree/part/tasks.py @@ -329,7 +329,7 @@ def scheduled_stocktake_reports(): threshold = datetime.now() - timedelta(days=delete_n_days) old_entries = PartStocktake.objects.filter(date__lt=threshold) - if old_entries.count() > 0: + if old_entries.exists(): logger.info('Deleting %s old stock entries', old_entries.count()) old_entries.delete() diff --git a/src/backend/InvenTree/stock/api.py b/src/backend/InvenTree/stock/api.py index baa1be9b38..88d25a9130 100644 --- a/src/backend/InvenTree/stock/api.py +++ b/src/backend/InvenTree/stock/api.py @@ -1490,6 +1490,54 @@ class StockTrackingOutputOptions(OutputConfiguration): ] +class StockTrackingFilter(FilterSet): + """API filter options for the StockTrackingList endpoint.""" + + class Meta: + """Metaclass options.""" + + model = StockItemTracking + fields = ['item', 'user'] + + include_variants = rest_filters.BooleanFilter( + label=_('Include Part Variants'), method='filter_include_variants' + ) + + def filter_include_variants(self, queryset, name, value): + """Filter by whether or not to include part variants. + + Note: + - This filter does nothing by itself, and is only used to modify the behavior of the 'part' filter. + - Refer to the 'filter_part' method for more information on how this works. + """ + return queryset + + part = rest_filters.ModelChoiceFilter( + label=_('Part'), queryset=Part.objects.all(), method='filter_part' + ) + + def filter_part(self, queryset, name, part): + """Filter StockTracking entries by the linked part. + + Note: + - This filter behavior also takes into account the 'include_variants' filter, which determines whether or not to include part variants in the results. + """ + include_variants = str2bool(self.data.get('include_variants', False)) + + if include_variants: + return queryset.filter(part__in=part.get_descendants(include_self=True)) + else: + return queryset.filter(part=part) + + min_date = InvenTreeDateFilter( + label=_('Date after'), field_name='date', lookup_expr='gt' + ) + + max_date = InvenTreeDateFilter( + label=_('Date before'), field_name='date', lookup_expr='lt' + ) + + class StockTrackingList( SerializerContextMixin, DataExportViewMixin, OutputOptionsMixin, ListAPI ): @@ -1501,8 +1549,9 @@ class StockTrackingList( - GET: Return list of StockItemTracking objects """ - queryset = StockItemTracking.objects.all() + queryset = StockItemTracking.objects.all().prefetch_related('item', 'part') serializer_class = StockSerializers.StockTrackingSerializer + filterset_class = StockTrackingFilter output_options = StockTrackingOutputOptions def get_delta_model_map(self) -> dict: @@ -1598,8 +1647,6 @@ class StockTrackingList( filter_backends = SEARCH_ORDER_FILTER - filterset_fields = ['item', 'user'] - ordering = '-date' ordering_fields = ['date'] diff --git a/src/backend/InvenTree/stock/migrations/0117_stockitemtracking_part_alter_stockitemtracking_item.py b/src/backend/InvenTree/stock/migrations/0117_stockitemtracking_part_alter_stockitemtracking_item.py new file mode 100644 index 0000000000..9091c57a30 --- /dev/null +++ b/src/backend/InvenTree/stock/migrations/0117_stockitemtracking_part_alter_stockitemtracking_item.py @@ -0,0 +1,36 @@ +# Generated by Django 5.2.10 on 2026-02-05 12:17 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("part", "0146_auto_20251203_1241"), + ("stock", "0116_alter_stockitem_link"), + ] + + operations = [ + migrations.AddField( + model_name="stockitemtracking", + name="part", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="stock_tracking_info", + to="part.part", + ), + ), + migrations.AlterField( + model_name="stockitemtracking", + name="item", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="tracking_info", + to="stock.stockitem", + ), + ), + ] diff --git a/src/backend/InvenTree/stock/migrations/0118_auto_20260205_1218.py b/src/backend/InvenTree/stock/migrations/0118_auto_20260205_1218.py new file mode 100644 index 0000000000..9e5c4bcee9 --- /dev/null +++ b/src/backend/InvenTree/stock/migrations/0118_auto_20260205_1218.py @@ -0,0 +1,57 @@ +# Generated by Django 5.2.10 on 2026-02-05 12:18 + +from django.db import migrations + + +def add_part_links(apps, schema_editor): + """Add links to the Part model for all existing StockItemTracking entries.""" + + StockItemTracking = apps.get_model('stock', 'StockItemTracking') + + history_entries = [] + + for tracking in StockItemTracking.objects.all(): + + item = tracking.item + + if item is None: + continue + + part = item.part + + if part is None: + continue + + tracking.part = part + history_entries.append(tracking) + + if len(history_entries) > 0: + StockItemTracking.objects.bulk_update(history_entries, ['part']) + print(f"\nUpdated {len(history_entries)} StockItemTracking entries with part links") + + +def remove_null_items(apps, schema_editor): + """Reverse migration - remove any StockItemTracking entries which have a null item link.""" + + StockItemTracking = apps.get_model('stock', 'StockItemTracking') + + null_items = StockItemTracking.objects.filter(item__isnull=True) + + count = null_items.count() + + if count > 0: + null_items.delete() + print(f"\nDeleted {count} StockItemTracking entries with null item links") + +class Migration(migrations.Migration): + + dependencies = [ + ("stock", "0117_stockitemtracking_part_alter_stockitemtracking_item"), + ] + + operations = [ + migrations.RunPython( + add_part_links, + reverse_code=remove_null_items, + ) + ] diff --git a/src/backend/InvenTree/stock/models.py b/src/backend/InvenTree/stock/models.py index 913bc1a11b..68c5b53416 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -1360,7 +1360,7 @@ class StockItem( item.save(add_note=False) code = StockHistoryCode.SENT_TO_CUSTOMER - deltas = {} + deltas = {'quantity': float(quantity)} if customer is not None: deltas['customer'] = customer.pk @@ -1441,7 +1441,11 @@ class StockItem( # Split the stock item item = self.splitStock(quantity, None, user) - tracking_info = {} + tracking_info = { + 'quantity': float(quantity) + if quantity is not None + else float(item.quantity) + } if location: tracking_info['location'] = location.pk @@ -1651,7 +1655,7 @@ class StockItem( stock_item.location = None stock_item.save(add_note=False) - deltas = {'stockitem': self.pk} + deltas = {'stockitem': self.pk, 'quantity': float(quantity)} if build is not None: deltas['buildorder'] = build.pk @@ -1666,7 +1670,7 @@ class StockItem( StockHistoryCode.INSTALLED_CHILD_ITEM, user, notes=notes, - deltas={'stockitem': stock_item.pk}, + deltas={'stockitem': stock_item.pk, 'quantity': float(quantity)}, ) trigger_event( @@ -1692,11 +1696,14 @@ class StockItem( self.belongs_to.add_tracking_entry( StockHistoryCode.REMOVED_CHILD_ITEM, user, - deltas={'stockitem': self.pk}, + deltas={'stockitem': self.pk, 'quantity': float(self.quantity)}, notes=notes, ) - tracking_info = {'stockitem': self.belongs_to.pk} + tracking_info = { + 'stockitem': self.belongs_to.pk, + 'quantity': float(self.quantity), + } self.add_tracking_entry( StockHistoryCode.REMOVED_FROM_ASSEMBLY, @@ -1835,6 +1842,7 @@ class StockItem( entry = StockItemTracking( item=self, + part=self.part, tracking_type=entry_type.value, user=user, date=InvenTree.helpers.current_time(), @@ -1964,7 +1972,7 @@ class StockItem( # Remove the equivalent number of items self.take_stock( - quantity, user, code=StockHistoryCode.STOCK_SERIZALIZED, notes=notes + quantity, user, code=StockHistoryCode.STOCK_SERIALIZED, notes=notes ) return items @@ -2175,7 +2183,10 @@ class StockItem( user, quantity=self.quantity, notes=notes, - deltas={'location': location.pk if location else None}, + deltas={ + 'location': location.pk if location else None, + 'quantity': self.quantity, + }, ) # Update the location of the item @@ -2416,7 +2427,7 @@ class StockItem( self.location = location - tracking_info = {} + tracking_info = {'quantity': float(quantity)} tracking_code = StockHistoryCode.STOCK_MOVE @@ -2869,21 +2880,18 @@ def after_save_stock_item(sender, instance: StockItem, created, **kwargs): class StockItemTracking(InvenTree.models.InvenTreeModel): """Stock tracking entry - used for tracking history of a particular StockItem. - Note: 2021-05-11 - The legacy StockTrackingItem model contained very little information about the "history" of the item. - In fact, only the "quantity" of the item was recorded at each interaction. - Also, the "title" was translated at time of generation, and thus was not really translatable. - The "new" system tracks all 'delta' changes to the model, - and tracks change "type" which can then later be translated - - Attributes: item: ForeignKey reference to a particular StockItem + part: ForeignKey reference to the Part associated with this StockItem date: Date that this tracking info was created tracking_type: The type of tracking information notes: Associated notes (input by user) user: The user associated with this tracking info deltas: The changes associated with this history item + + Notes: + If the underlying stock item is deleted, the "item" field will be set to null, but the tracking information will be retained. + The tracking data will be removed if the associated part is deleted, as the tracking information is not relevant without the part context. """ class Meta: @@ -2896,6 +2904,13 @@ class StockItemTracking(InvenTree.models.InvenTreeModel): """Return API url.""" return reverse('api-stock-tracking-list') + def save(self, *args, **kwargs): + """Ensure that the 'part' link is always correct.""" + if self.item: + self.part = self.item.part + + super().save(*args, **kwargs) + def get_absolute_url(self): """Return url for instance.""" return InvenTree.helpers.pui_url(f'/stock/item/{self.item.id}') @@ -2910,7 +2925,19 @@ class StockItemTracking(InvenTree.models.InvenTreeModel): tracking_type = models.IntegerField(default=StockHistoryCode.LEGACY) item = models.ForeignKey( - StockItem, on_delete=models.CASCADE, related_name='tracking_info' + StockItem, + on_delete=models.SET_NULL, + null=True, + blank=False, + related_name='tracking_info', + ) + + part = models.ForeignKey( + 'part.part', + on_delete=models.CASCADE, + related_name='stock_tracking_info', + null=True, + blank=True, ) date = models.DateTimeField(auto_now_add=True, editable=False) diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index a1025ed536..11ac344e1e 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -1248,6 +1248,8 @@ class StockTrackingSerializer( 'pk', 'item', 'item_detail', + 'part', + 'part_detail', 'date', 'deltas', 'label', @@ -1256,13 +1258,21 @@ class StockTrackingSerializer( 'user', 'user_detail', ] - read_only_fields = ['date', 'user', 'label', 'tracking_type'] + read_only_fields = ['date', 'part', 'user', 'label', 'tracking_type'] label = serializers.CharField(read_only=True) item_detail = enable_filter( StockItemSerializer(source='item', many=False, read_only=True, allow_null=True), - prefetch_fields=['item'], + prefetch_fields=['item', 'item__part'], + ) + + part_detail = enable_filter( + part_serializers.PartBriefSerializer( + source='part', many=False, read_only=True, allow_null=True + ), + default_include=False, + prefetch_fields=['part'], ) user_detail = enable_filter( diff --git a/src/backend/InvenTree/stock/status_codes.py b/src/backend/InvenTree/stock/status_codes.py index 978bca0b0e..50cad87a90 100644 --- a/src/backend/InvenTree/stock/status_codes.py +++ b/src/backend/InvenTree/stock/status_codes.py @@ -53,7 +53,7 @@ class StockHistoryCode(StatusCode): STOCK_COUNT = 10, _('Stock counted') STOCK_ADD = 11, _('Stock manually added') STOCK_REMOVE = 12, _('Stock manually removed') - STOCK_SERIZALIZED = 13, _('Serialized stock items') + STOCK_SERIALIZED = 13, _('Serialized stock items') RETURNED_TO_STOCK = 15, _('Returned to stock') # Stock item returned to stock diff --git a/src/backend/InvenTree/stock/tasks.py b/src/backend/InvenTree/stock/tasks.py index e3b4f5e4ab..d8b647152f 100644 --- a/src/backend/InvenTree/stock/tasks.py +++ b/src/backend/InvenTree/stock/tasks.py @@ -1,8 +1,13 @@ """Background tasks for the stock app.""" +from datetime import datetime, timedelta + import structlog from opentelemetry import trace +from common.settings import get_global_setting +from InvenTree.tasks import ScheduledTask, offload_task, scheduled_task + tracer = trace.get_tracer(__name__) logger = structlog.get_logger('inventree') @@ -43,7 +48,6 @@ def rebuild_stock_item_tree(tree_id: int, rebuild_on_fail: bool = True) -> bool: """ from InvenTree.exceptions import log_error from InvenTree.sentry import report_exception - from InvenTree.tasks import offload_task from stock.models import StockItem if tree_id: @@ -65,3 +69,27 @@ def rebuild_stock_item_tree(tree_id: int, rebuild_on_fail: bool = True) -> bool: # No tree_id provided, so rebuild the entire tree StockItem.objects.rebuild() return True + + +@tracer.start_as_current_span('delete_old_stock_tracking') +@scheduled_task(ScheduledTask.DAILY) +def delete_old_stock_tracking(): + """Remove old stock tracking entries before a certain date.""" + from stock.models import StockItemTracking + + if not get_global_setting('STOCK_TRACKING_DELETE_OLD_ENTRIES', False): + return + + delete_n_days = int(get_global_setting('STOCK_TRACKING_DELETE_DAYS', 365)) + + threshold = datetime.now() - timedelta(days=delete_n_days) + + old_entries = StockItemTracking.objects.filter(date__lte=threshold) + + if old_entries.exists(): + logger.info( + 'Deleting old stock tracking entries', + count=old_entries.count(), + threshold=threshold, + ) + old_entries.delete() diff --git a/src/frontend/src/components/settings/SettingList.tsx b/src/frontend/src/components/settings/SettingList.tsx index db5a3492bb..a0eb5ebd18 100644 --- a/src/frontend/src/components/settings/SettingList.tsx +++ b/src/frontend/src/components/settings/SettingList.tsx @@ -1,6 +1,6 @@ import { t } from '@lingui/core/macro'; import { Trans } from '@lingui/react/macro'; -import { Alert, Skeleton, Stack, Text } from '@mantine/core'; +import { Alert, Divider, Skeleton, Stack, Text, Title } from '@mantine/core'; import { notifications } from '@mantine/notifications'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useStore } from 'zustand'; @@ -24,11 +24,13 @@ import { SettingItem } from './SettingItem'; * Display a list of setting items, based on a list of provided keys */ export function SettingList({ + heading, settingsState, keys, onChange, onLoaded }: Readonly<{ + heading?: string; settingsState: SettingsStateProps; keys?: string[]; onChange?: () => void; @@ -162,6 +164,8 @@ export function SettingList({ <> {editSettingModal.modal} + {heading && {heading}} + {heading && } {(keys || allKeys)?.map((key, i) => { const setting = settingsState?.settings?.find( (s: any) => s.key === key @@ -198,16 +202,26 @@ export function SettingList({ ); } -export function UserSettingList({ keys }: Readonly<{ keys: string[] }>) { +export function UserSettingList({ + keys, + heading +}: Readonly<{ keys: string[]; heading?: string }>) { const userSettings = useUserSettingsState(); - return ; + return ( + + ); } -export function GlobalSettingList({ keys }: Readonly<{ keys: string[] }>) { +export function GlobalSettingList({ + keys, + heading +}: Readonly<{ keys: string[]; heading?: string }>) { const globalSettings = useGlobalSettingsState(); - return ; + return ( + + ); } export function PluginSettingList({ diff --git a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx index 0e3ef515d7..e27fb6aa9e 100644 --- a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx +++ b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx @@ -1,5 +1,5 @@ import { t } from '@lingui/core/macro'; -import { Skeleton, Stack } from '@mantine/core'; +import { Divider, Skeleton, Stack } from '@mantine/core'; import { IconBellCog, IconCategory, @@ -254,15 +254,26 @@ export default function SystemSettings() { label: t`Stock History`, icon: , content: ( - + + + + + ) }, { diff --git a/src/frontend/src/pages/part/PartStockHistoryDetail.tsx b/src/frontend/src/pages/part/PartStockHistoryDetail.tsx index 6ddc9571ef..dfca4baa2b 100644 --- a/src/frontend/src/pages/part/PartStockHistoryDetail.tsx +++ b/src/frontend/src/pages/part/PartStockHistoryDetail.tsx @@ -7,6 +7,7 @@ import type { TableColumn } from '@lib/types/Tables'; import { t } from '@lingui/core/macro'; import { type ChartTooltipProps, LineChart } from '@mantine/charts'; import { + Accordion, Center, Divider, Loader, @@ -16,6 +17,7 @@ import { } from '@mantine/core'; import dayjs from 'dayjs'; import { useCallback, useMemo, useState } from 'react'; +import { StylishText } from '../../components/items/StylishText'; import { formatDate, formatPriceRange } from '../../defaults/formatters'; import { partStocktakeFields } from '../../forms/PartForms'; import { @@ -27,6 +29,7 @@ import { useTable } from '../../hooks/UseTable'; import { useUserState } from '../../states/UserState'; import { DateColumn, DecimalColumn } from '../../tables/ColumnRenderers'; import { InvenTreeTable } from '../../tables/InvenTreeTable'; +import { StockTrackingTable } from '../../tables/stock/StockTrackingTable'; /* * Render a tooltip for the chart, with correct date information @@ -64,9 +67,7 @@ function ChartTooltip({ label, payload }: Readonly) { ); } -export default function PartStockHistoryDetail({ - partId -}: Readonly<{ partId: number }>) { +export function PartStocktakePanel({ partId }: Readonly<{ partId: number }>) { const user = useUserState(); const table = useTable('part-stocktake'); @@ -208,7 +209,7 @@ export default function PartStockHistoryDetail({ {newStocktakeEntry.modal} {editStocktakeEntry.modal} {deleteStocktakeEntry.modal} - + ); } + +export default function PartStockHistoryDetail({ + partId +}: Readonly<{ partId: number }>) { + return ( + + + + {t`Stock Tracking`} + + + + + + + + {t`Stocktake Entries`} + + + + + + + ); +} diff --git a/src/frontend/src/tables/stock/StockTrackingTable.tsx b/src/frontend/src/tables/stock/StockTrackingTable.tsx index 52e909994e..0a6f4f1166 100644 --- a/src/frontend/src/tables/stock/StockTrackingTable.tsx +++ b/src/frontend/src/tables/stock/StockTrackingTable.tsx @@ -24,8 +24,13 @@ import { } from '../../components/render/Stock'; import { RenderUser } from '../../components/render/User'; import { useTable } from '../../hooks/UseTable'; -import { DateColumn, DescriptionColumn } from '../ColumnRenderers'; -import { UserFilter } from '../Filter'; +import { DateColumn, DescriptionColumn, PartColumn } from '../ColumnRenderers'; +import { + IncludeVariantsFilter, + MaxDateFilter, + MinDateFilter, + UserFilter +} from '../Filter'; import { InvenTreeTable } from '../InvenTreeTable'; type StockTrackingEntry = { @@ -34,9 +39,15 @@ type StockTrackingEntry = { details: ReactNode; }; -export function StockTrackingTable({ itemId }: Readonly<{ itemId: number }>) { +export function StockTrackingTable({ + itemId, + partId +}: Readonly<{ + itemId?: number; + partId?: number; +}>) { const navigate = useNavigate(); - const table = useTable('stock_tracking'); + const table = useTable(partId ? 'part_stock_tracking' : 'stock_tracking'); // Render "details" for a stock tracking record const renderDetails = useCallback( @@ -200,6 +211,9 @@ export function StockTrackingTable({ itemId }: Readonly<{ itemId: number }>) { const filters: TableFilter[] = useMemo(() => { return [ + MinDateFilter(), + MaxDateFilter(), + IncludeVariantsFilter(), UserFilter({ name: 'user', label: t`User`, @@ -213,6 +227,43 @@ export function StockTrackingTable({ itemId }: Readonly<{ itemId: number }>) { DateColumn({ switchable: false }), + PartColumn({ + title: t`Part`, + part: 'part_detail', + switchable: true, + hidden: !partId + }), + { + title: t`IPN`, + accessor: 'part_detail.IPN', + sortable: true, + defaultVisible: false, + switchable: true, + hidden: !partId + }, + { + accessor: 'item', + title: t`Stock Item`, + sortable: false, + switchable: false, + hidden: !partId, + render: (record: any) => { + const item = record.item_detail; + if (!item) { + return ( + {t`Stock item no longer exists`} + ); + } else if (item.serial && item.quantity == 1) { + return `${t`Serial`} #${item.serial}`; + } else { + return `${t`Item ID`} ${item.pk}`; + } + } + }, DescriptionColumn({ accessor: 'label' }), @@ -250,10 +301,15 @@ export function StockTrackingTable({ itemId }: Readonly<{ itemId: number }>) { props={{ params: { item: itemId, + part: partId, + part_detail: partId ? true : undefined, + item_detail: partId ? true : undefined, user_detail: true }, enableDownload: true, - tableFilters: filters + tableFilters: filters, + modelType: partId ? ModelType.stockitem : undefined, + modelField: 'item' }} /> ); diff --git a/src/frontend/tests/pages/pui_stock.spec.ts b/src/frontend/tests/pages/pui_stock.spec.ts index 57e485a76a..ec330474e3 100644 --- a/src/frontend/tests/pages/pui_stock.spec.ts +++ b/src/frontend/tests/pages/pui_stock.spec.ts @@ -423,6 +423,55 @@ test('Stock - Tracking', async ({ browser }) => { await page.getByText('- - Factory/Office Block/Room').first().waitFor(); await page.getByRole('link', { name: 'Widget Assembly' }).waitFor(); await page.getByRole('cell', { name: 'Installed into assembly' }).waitFor(); + + /* Add some more stock items and tracking information: + * - Duplicate this stock item + * - Give it a unique serial number + * - Ensure the tracking information is duplicated correctly + * - Delete the new stock item + * - Ensure that the tracking information is retained against the base part + */ + + // Duplicate the stock item + await page + .getByRole('button', { name: 'action-menu-stock-item-actions' }) + .click(); + await page + .getByRole('menuitem', { name: 'action-menu-stock-item-actions-duplicate' }) + .click(); + await page + .getByRole('textbox', { name: 'text-field-serial_numbers' }) + .fill('9876'); + await page.getByRole('button', { name: 'Submit' }).click(); + + // Check stock tracking information is correct + await page.getByText('Serial Number: 9876').first().waitFor(); + await loadTab(page, 'Stock Tracking'); + await page + .getByRole('cell', { name: 'Stock item created' }) + .first() + .waitFor(); + + // Delete this stock item + await page + .getByRole('button', { name: 'action-menu-stock-item-actions' }) + .click(); + await page + .getByRole('menuitem', { name: 'action-menu-stock-item-actions-delete' }) + .click(); + await page.getByRole('button', { name: 'Delete' }).click(); + + // Check stock tracking for base part + await loadTab(page, 'Stock History'); + await page.getByRole('button', { name: 'Stock Tracking' }).click(); + + await page.getByText('Stock item no longer exists').first().waitFor(); + await page + .getByRole('cell', { name: 'Thumbnail Blue Widget' }) + .first() + .waitFor(); + await page.getByRole('cell', { name: 'Item ID 232' }).first().waitFor(); + await page.getByRole('cell', { name: 'Serial #116' }).first().waitFor(); }); test('Stock - Location', async ({ browser }) => {