-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor framework for generating batch codes - Provide additional kwargs to plugin - Move into new file - Error handling * Implement API endpoint for generating a new batch code * Fixes * Refactor into stock.generators * Fix API endpoint * Pass time context through to plugins * Generate batch code when receiving items * Create useGenerator hook - Build up a dataset and query server whenever it changes - Look for result in response data - For now, just used for generating batch codes - may be used for more in the future * Refactor PurchaseOrderForms to use new generator hook * Refactor StockForms implementation * Remove dead code * add OAS diff * fix ref * fix ref again * wrong branch, sorry * Update src/frontend/src/hooks/UseGenerator.tsx Co-authored-by: Lukas <76838159+wolflu05@users.noreply.github.com> * Bump API version * Do not override batch code if already generated * Add serial number generator - Move to /generate/ API endpoint - Move batch code generator too * Update PUI endpoints * Add debouncing to useGenerator hook * Refactor useGenerator func * Add serial number generator to stock form * Add batch code genereator to build order form * Update buildfields * Use build batch code when creating new output --------- Co-authored-by: Matthias Mair <code@mjmair.com> Co-authored-by: Lukas <76838159+wolflu05@users.noreply.github.com>
- Loading branch information
1 parent
5cb61d5
commit e93d9c4
Showing
21 changed files
with
513 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"""Generator functions for the stock app.""" | ||
|
||
from inspect import signature | ||
|
||
from django.core.exceptions import ValidationError | ||
|
||
from jinja2 import Template | ||
|
||
import common.models | ||
import InvenTree.exceptions | ||
import InvenTree.helpers | ||
|
||
|
||
def generate_batch_code(**kwargs): | ||
"""Generate a default 'batch code' for a new StockItem. | ||
By default, this uses the value of the 'STOCK_BATCH_CODE_TEMPLATE' setting (if configured), | ||
which can be passed through a simple template. | ||
Also, this function is exposed to the ValidationMixin plugin class, | ||
allowing custom plugins to be used to generate new batch code values. | ||
Various kwargs can be passed to the function, which will be passed through to the plugin functions. | ||
""" | ||
# First, check if any plugins can generate batch codes | ||
from plugin.registry import registry | ||
|
||
now = InvenTree.helpers.current_time() | ||
|
||
context = { | ||
'date': now, | ||
'year': now.year, | ||
'month': now.month, | ||
'day': now.day, | ||
'hour': now.hour, | ||
'minute': now.minute, | ||
'week': now.isocalendar()[1], | ||
**kwargs, | ||
} | ||
|
||
for plugin in registry.with_mixin('validation'): | ||
generate = getattr(plugin, 'generate_batch_code', None) | ||
|
||
if not generate: | ||
continue | ||
|
||
# Check if the function signature accepts kwargs | ||
sig = signature(generate) | ||
|
||
if 'kwargs' in sig.parameters: | ||
# Pass the kwargs through to the plugin | ||
try: | ||
batch = generate(**context) | ||
except Exception: | ||
InvenTree.exceptions.log_error('plugin.generate_batch_code') | ||
continue | ||
else: | ||
# Ignore the kwargs (legacy plugin) | ||
try: | ||
batch = generate() | ||
except Exception: | ||
InvenTree.exceptions.log_error('plugin.generate_batch_code') | ||
continue | ||
|
||
# Return the first non-null value generated by a plugin | ||
if batch is not None: | ||
return batch | ||
|
||
# If we get to this point, no plugin was able to generate a new batch code | ||
batch_template = common.models.InvenTreeSetting.get_setting( | ||
'STOCK_BATCH_CODE_TEMPLATE', '' | ||
) | ||
|
||
return Template(batch_template).render(context) | ||
|
||
|
||
def generate_serial_number(part=None, quantity=1, **kwargs) -> str: | ||
"""Generate a default 'serial number' for a new StockItem.""" | ||
from plugin.registry import registry | ||
|
||
quantity = quantity or 1 | ||
|
||
if part is None: | ||
# Cannot generate a serial number without a part | ||
return None | ||
|
||
try: | ||
quantity = int(quantity) | ||
except Exception: | ||
raise ValidationError({'quantity': 'Invalid quantity value'}) | ||
|
||
if quantity < 1: | ||
raise ValidationError({'quantity': 'Quantity must be greater than zero'}) | ||
|
||
# If we are here, no plugins were available to generate a serial number | ||
# In this case, we will generate a simple serial number based on the provided part | ||
sn = part.get_latest_serial_number() | ||
|
||
serials = [] | ||
|
||
# Generate the required quantity of serial numbers | ||
# Note that this call gets passed through to the plugin system | ||
while quantity > 0: | ||
sn = InvenTree.helpers.increment_serial_number(sn) | ||
|
||
# Exit if an empty or duplicated serial is generated | ||
if not sn or sn in serials: | ||
break | ||
|
||
serials.append(sn) | ||
quantity -= 1 | ||
|
||
return ','.join(serials) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.