Metadata-Version: 2.4
Name: inventree-sales-order-builds
Version: 0.1.0
Summary: InvenTree plugin: automatically create build orders for assembly lines when a sales order is issued
Author: OMG Harness
License: MIT
Project-URL: Homepage, https://github.com/SXSLYDA/inventree-sales-order-builds
Project-URL: Issues, https://github.com/SXSLYDA/inventree-sales-order-builds/issues
Keywords: inventree,inventreeplugins,build orders,sales orders,manufacturing
Classifier: Framework :: Django
Classifier: Framework :: InvenTree
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: bump-my-version; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: ruff==0.17.6; extra == "dev"
Dynamic: license-file

# InvenTree Sales Order Builds

An [InvenTree](https://inventree.org) plugin that automatically creates **build orders** for the assembly lines on a **sales order** when the order is issued.

InvenTree only lets you raise a build from a sales order one line at a time. If you build to order, that's a lot of clicking for every order. This plugin does it for you:

- When a sales order is **issued**, every line whose part is an **assembly** gets a build order. The build is linked to the sales order, so finished outputs can be allocated back to it.
- Lines for the same part are added together into one build.
- Build orders are created **whether or not the components are in stock**. That's normal for InvenTree: you allocate stock (or order it) afterwards.
- Assemblies whose **BOM isn't validated** (or has changed since it was validated) are skipped, and the order's owner is notified.
- It's **idempotent**. Re-issuing an order, or adding or increasing lines after it's issued, only creates builds for quantity that isn't already covered. It never cancels or shrinks existing builds.

It works alongside InvenTree's built-in **Auto Create Builds** plugin. That plugin creates child builds for sub-assemblies when a build is *issued*. Turn on this plugin's **Issue builds** setting to cascade sales order → top-level build → sub-assembly builds.

## Requirements

- InvenTree 1.0 or newer
- **Settings → Plugins → Enable event integration** must be on, and the background worker must be running

## Installation

From the InvenTree admin, go to **Settings → Plugins → Install Plugin**, enter `inventree-sales-order-builds`, then activate **Sales Order Builds**.

Or add it to `plugins.txt`:

```
inventree-sales-order-builds
```

## Settings

| Setting | Default | What it does |
|---|---|---|
| Build quantity | Full | **Full**: build everything ordered, less stock already allocated to the order (build to order). **Shortfall**: also use free stock first and only build the rest. |
| Require validated BOM | On | Skip assemblies whose BOM isn't validated, or has changed since validation. |
| Require active part | On | Skip inactive assemblies. |
| Require locked part | Off | Only build assemblies that are locked. |
| Follow line changes | On | On an issued order, adding a line or raising a quantity creates a top-up build. |
| Issue builds | Off | Put new builds straight into production. This also triggers the built-in Auto Create Builds plugin for sub-assemblies. |
| Auto-allocate stock | Off | Allocate available untracked component stock to new builds. Missing stock is left unallocated. |
| Build title | `Build for {order}` | Placeholders: `{order}`, `{customer}`, `{part}`. |
| Notify skipped lines | On | Notify the order's creator and responsible owner when an assembly line didn't get a build. |

### How the quantity is worked out

For each assembly part on the order:

```
to build = ordered quantity
         - stock allocated/shipped to the order that did not come from one of its builds
         - existing (non-cancelled) builds for this order and part
         - free stock                                  (Shortfall mode only)
```

The result is rounded up, because build quantities are whole numbers.

## Blocking builds from your own plugin

The plugin creates builds with `Build.objects.create()`, so InvenTree's own checks still apply:

- InvenTree's *Require valid BOM*, *Require active part* and *Require locked part* build settings
- any **ValidationMixin** plugin's `validate_model_instance()`

If a check raises a `ValidationError`, that part is skipped, the reason is included in the notification, and the other lines carry on. This lets you add business rules (for example, "the BOM must match the current design revision") in your own plugin. The same rule also guards builds created by hand:

```python
from django.core.exceptions import ValidationError
from plugin import InvenTreePlugin
from plugin.mixins import ValidationMixin


class MyRules(ValidationMixin, InvenTreePlugin):
    NAME = 'MyRules'

    def validate_model_instance(self, instance, deltas=None):
        from build.models import Build

        if isinstance(instance, Build) and instance.pk is None:
            if not design_revision_matches(instance.part):
                raise ValidationError(
                    {'part': 'BOM does not match the current design revision'}
                )
```

Return `None`, not `True`, from `validate_model_instance`. Returning `True` stops InvenTree from running any other validation plugins.

## Development

Tests run inside an InvenTree source checkout:

```bash
pip install -e .
cd /path/to/InvenTree/src/backend/InvenTree
INVENTREE_PLUGINS_ENABLED=True INVENTREE_PLUGIN_TESTING_SETUP=True \
  python manage.py test inventree_sales_order_builds
```

See `.github/workflows/ci.yml` for the full set of environment variables.

## Releasing

The version lives in `inventree_sales_order_builds/__init__.py` (`PLUGIN_VERSION`, shown in InvenTree's plugin list). `bump-my-version` updates it, so you never edit it by hand.

```bash
pip install -e .[dev]

# 1. Add your changes under "## Unreleased" in CHANGELOG.md and commit them

# 2. Bump: updates the version and CHANGELOG, commits "Release vX.Y.Z" and tags it
bump-my-version bump patch      # 0.1.0 -> 0.1.1  bug fixes
bump-my-version bump minor      # 0.1.0 -> 0.2.0  new features
bump-my-version bump major      # 0.1.0 -> 1.0.0  breaking changes

# 3. Push the commit and the tag
git push --follow-tags
```

Pushing a `v*` tag runs the tests, builds the package, and checks the tag matches `PLUGIN_VERSION` and has a CHANGELOG entry. It then publishes to PyPI and creates a GitHub release using that version's CHANGELOG section as the notes. If the tag and the code disagree, nothing is published.

Use `bump-my-version show-bump` to preview, or `bump-my-version bump patch --dry-run -v` to see exactly what would change.

## License

MIT
