Metadata-Version: 2.4
Name: inventree-loan
Version: 0.2.1
Summary: Loan and return management for InvenTree, with requests, approvals, barcode/QR support and full history.
Author: InvenTree Loan Contributors
License-Expression: MIT
Project-URL: Homepage, https://github.com/inventree/inventree-loan
Keywords: inventree,plugin,loan,library,barcode,qr
Classifier: Programming Language :: Python :: 3
Classifier: Framework :: InvenTree
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: qrcode>=7.4
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: pytest-django>=4.8; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"
Dynamic: license-file

# InvenTree Loan Manager

A loan and return management plugin for **InvenTree 1.6+** (developed and tested
against InvenTree 1.7.4). It adds a complete request → approval → handout →
return workflow with audit history, barcode/QR support, due-date reminders, and
a role-aware interface for requesters and loan desk staff.

## Features

- A **public request catalogue** that works without an InvenTree account.
- Browse loanable parts by category or search, open part details, and inspect
  available stock items before adding them to a request.
- Anonymous requesters enter their own name, email, phone, organisation and ID,
  so no InvenTree user account is required.
- Requesters sign at submission. The loan desk captures a second signature at
  handout, and a third at return.
- Approvers can review, approve, and reject requests.
- The admin console has a **View** action for every request, showing requester
  details, purpose, dates, requested items, notes and the submitted signature
  before approval or handout.
- Loan desk staff can hand out items by scanning barcodes or searching the catalog.
- Trackable/serialized parts are loaned as whole units and returned in full.
  Non-trackable parts support partial quantities; split items are moved to the
  loan location and returned to their original location and status.
- Items move to a dedicated **On Loan** location and receive an **On Loan** status.
- While on loan, stock items are excluded from availability and normal allocation flows.
- Direct edits, moves, quantity changes, allocation, and deletion of loaned items are
  blocked by plugin validation.
- Returns support full or partial quantities, condition notes, and restoring the
  original location and status.
- Every request, decision, handout, reminder, renewal, loss, and return is recorded
  in an append-only audit timeline.
- Every anonymous request gets a secret tracking link showing its status, loan
  progress, and signatures.
- QR codes identify loans; printed receipts include the QR, due date, and
  captured signatures.
- Barcode scanning supports USB/keyboard scanners and camera scanning where the
  browser exposes `BarcodeDetector`.
- Optional email notifications; in-app notifications are always available.
- Scheduled due-soon and overdue reminders.
- CSV export for requests and loans.
- Responsive, accessible interface with light and dark mode support.

## Requirements

- InvenTree **1.6+** (`MIN_VERSION = 1.6.0`, `MAX_VERSION = 1.7.99`)
  - InvenTree 1.5.x does not support plugin-defined React routes, so the native
    **Loans** tab requires an upgrade to 1.6 or newer.
- Python 3.10+
- The global settings `ENABLE_PLUGINS_APP`, `ENABLE_PLUGINS_URL`, and
  `ENABLE_PLUGINS_INTERFACE` must be enabled
  - `ENABLE_PLUGINS_APP` is required for the plugin's models and migrations.
  - `ENABLE_PLUGINS_INTERFACE` is required for the **Loans** tab and native page.
- The public request page (`/plugin/loan/request/`) does **not** require an
  InvenTree account. It must be reachable from the network that will use it.
- Optional for camera scanning: a browser with `BarcodeDetector` support

> **Upgrading from 1.5.x:** enable `ENABLE_PLUGINS_APP` before restarting. Without
> it the plugin can appear in the list but its Django app, tables, and URLs are
> never registered, which produces the intermittent empty/broken pages.

## Installation

### As a Python package

```bash
pip install inventree-loan
```

Then restart InvenTree, open the plugin management page
(`/web/settings/admin/plugin`), find **InvenTree Loan Manager**, and activate it.

### Editable development install

```bash
pip install -e .
```

Restart the InvenTree server after installation.

#### Docker development workflow

When InvenTree runs in Docker, the container filesystem is ephemeral. Keep the
editable install in a mounted plugin directory and record it in
`data/plugins.txt`:

```text
-e /home/inventree/data/plugins/inventree-loan
```

Plugin JavaScript, CSS, and templates are served directly from the installed
package through the plugin's own URL (`/plugin/loan/assets/...`). They therefore
do not depend on `collectplugins` and are not left stale by the static-file
cache. Python and template changes need a restart because plugins are discovered
at startup:

```bash
docker restart inventree-server inventree-worker
```

### Database migration

The plugin uses its own Django app and migrations. After activating the plugin,
run the InvenTree update/migration command for your deployment, for example:

```bash
invoke update
```

or, in a source install:

```bash
python manage.py migrate
```

## Using the plugin

The plugin adds a **Loans** tab to the InvenTree navigation bar, a **Loans** card
to the dashboard widget list, and a **Loan Manager** spotlight action.

- **Request equipment** (`/plugin/loan/request/`) is the public catalogue.
  It requires no account. It uses an InvenTree-style table with a collapsible
  category tree, category counts that include subcategories, search, barcode
  input, pagination, and a request drawer. Requesters open item details, choose
  available stock, enter their contact details, sign, and submit. The
  confirmation shows the reference code, tracking link, and a printable
  request receipt.
- **Track a request** (`/plugin/loan/track/`) lets an anonymous requester enter
  the reference code and email address to open their request.
- **Request status** (`/plugin/loan/track/<token>/`) shows the request, loan
  dates, decisions, and captured signatures.
- **Loans (navigation)** opens a native InvenTree page at
  `/web/plugin/loan/management`. It is rendered inside the normal InvenTree
  layout, so the header, navigation, theme, and user menu behave exactly like
  the Stock or Parts pages. Requests and loans use searchable, filterable,
  sortable tables with pagination and CSV export; summary cards show pending,
  approved, active and overdue counts. Signed-in requesters use the same page
  for their requests and loans; the old server-rendered My Requests pages
  redirect here, so there is only one version.
- **Request details** (`/web/plugin/loan/requests/<pk>`) is a full native page,
  like a build-order detail page. It shows requester/contact details, purpose,
  dates, lines, signatures, stock allocations, part/stock links, approval and
  handout actions, and print buttons for request/handout/return receipts.
- **Loans (dashboard widget)** provides a requester shortcut and an
  **Open loan desk** button which opens the same native page.
- The plugin settings page (Admin Center → Plugins → InvenTree Loan Manager)
  uses the same native console for initial setup.

The native console adapts to the signed-in user's loan role: requesters see
their own requests and loans, while loan staff additionally see the handout,
staff, setup, and audit-history tabs. Handout and return dialogs require the
borrower's signature. The Staff tab searches InvenTree users by name, username
or email, so you do not need to know the exact username.

## Initial setup

The plugin needs two stock objects:

1. A dedicated **On Loan** stock location.
2. A custom **On Loan** stock status whose logical value is `Quarantined`
   (so InvenTree treats loaned items as unavailable).

Run the setup action from the **Setup** tab of the Loan Manager console, or use
the management command:

```bash
python manage.py loan_setup --create-location --location-name "On Loan"
```

The command is idempotent and can be re-run safely. It creates the custom status,
creates or selects the location, and saves the location to the plugin settings.

> The custom status is what makes an item unavailable. The location keeps the
> stock history and the return path clear. If the custom status cannot be created,
> the plugin falls back to InvenTree's built-in `Quarantined` status.

## Staff roles

The plugin has its own roles, separate from InvenTree's stock roles:

| Role | Capabilities |
|---|---|
| **Approver** | View all requests/loans, approve or reject requests |
| **Loan desk** | Everything an approver can do, plus hand out, return, renew, mark lost |
| **Loan administrator** | Everything, plus manage staff and plugin setup |

Superusers always have full plugin access.

Grant the first role from the **Staff** tab of the Loan Manager console or with:

```bash
python manage.py loan_add_manager <username> --role admin
```

Only a superuser can grant the **Loan administrator** role through the web UI.
The last active administrator cannot be demoted or removed.

### Optional core stock permission

By default, plugin roles govern handout and return actions. If your organization
requires InvenTree's core stock change permission as well, enable
**Require core stock permission** in the plugin settings. Loan desk users will then
also need the normal InvenTree stock change role.

## Workflows

### Request

1. Open the public page at `/plugin/loan/request/` (no account required).
2. Browse categories or search, open a part to see its description and available
   stock items, and add the items you need.
3. Enter your name, email, phone, organisation and optional ID, plus pickup and
   return dates and the purpose.
4. Accept the responsibility statement and sign in the signature box.
5. Send the request. The confirmation includes a secret tracking link.
6. Approvers receive an in-app notification (and email if enabled).

### Allocate stock

Before approval or handout, staff can plan which physical stock items will be
used:

1. Open the request detail page and select **Auto allocate**.
2. Review the allocated stock sub-rows. Each row links to the stock item and
   shows its location and quantity.
3. Use **Allocate** on a line to add a specific stock item manually, or
   **Remove** to release an allocation.
4. Use **Clear allocations** to start again.

Allocations are plans, not stock movements. They reduce the reported public
availability, and checkout revalidates them before moving stock. Non-trackable
parts can be allocated partially across stock items; serialized assets are
allocated one unit per stock item. The part name links to the native InvenTree
part page, and each allocation links to its stock item page.

### Approve or reject

Approvers open the Loan Manager console or the request detail page. The detail
view shows the requester, contact details, purpose, dates, items, signature and
stock allocations. **Approve** or **Reject**, optionally adding a decision
note. Anonymous requesters receive the decision at the email address they
provided, and can follow the request through their tracking link.

### Hand out

1. Open the **Handout** tab in the Loan Manager console, or open the request
   detail page.
2. Select **Hand out** on an approved request.
3. Review the request details and have the borrower sign in the signature box.
4. Confirm the handout.
5. Print the **handout receipt** or show the loan QR. The receipt QR code opens
   the request tracking/detail page.

The service selects available stock items, moves or splits the requested quantities
to the On Loan location, applies the On Loan status, and records the original
location and status. The handout signature is stored on the loan and shown on the
requester's tracking page and the printed receipt.

### Return

1. Open the **Loans** tab in the Loan Manager console.
2. Choose the loan and select **Return**.
3. Adjust return quantities and select the condition.
4. Ask the borrower to sign, then confirm.
5. Print the **return receipt** from the loan row or request detail page.

The item is returned to its original location, the original status is restored, and
the loan is marked partially returned or returned. The return signature is stored
on the loan. The returned stock item remains linked to the loan line for audit
history.

### Trackable and non-trackable parts

- **Non-trackable parts** are quantity-tracked. A request can ask for part of a
  stock item; the plugin splits the item, moves the split quantity to the loan
  location, and returns it to the original location and status. The returned
  split remains a separate stock item so the loan line keeps its audit link.
- **Trackable/serialized assets** are individual stock items with a serial or
  asset label. The public page lists the available units, and each unit is added
  as quantity 1. Trackable parts are requested in whole units; serialized items
  cannot be partially returned.

### Renew, lose, and overdue

- **Renew** extends the due date, up to the configured maximum.
- **Lost** closes a line and marks the stock item `Lost` without losing history.
- A daily scheduled task sends due-soon and overdue reminders once per period.

## Barcode and QR

Loan QR codes contain a JSON payload:

```json
{"inventree_loan": "<loan-uuid>"}
```

Loan QR codes can still open the return workflow at the desk. Printed request,
handout, and return receipts use a separate QR code that links to the request
tracking/detail page. Scanning a stock item barcode delegates to InvenTree's
registered barcode plugins, so existing labels continue to work.

The scanner accepts:

- USB/keyboard-wedge scanners (type + Enter)
- Manual entry
- Camera scanning in browsers that support `BarcodeDetector`

## Stock behavior

- On handout, the item is moved to the On Loan location and its status is changed to
  the custom On Loan status.
- `StockItem.is_in_stock()` becomes false, so the item drops out of available stock
  calculations and normal sales-order/build/transfer allocation flows.
- On return, the original location and status are restored.
- If the custom status is missing, `Quarantined` is used as a safe fallback.
- Direct core edits, moves, quantity changes, customer/order assignment, and deletion
  of loaned stock items are rejected by `ValidationMixin`.
- Superusers and direct database/shell access can always override plugin protections.

## Plugin settings

| Setting | Default | Purpose |
|---|---|---|
| On Loan location | — | Stock location used while items are out |
| On Loan status name | `ON_LOAN` | Internal custom status name |
| Loanable part category | — | Restrict requests to a category tree |
| Request group | — | Restrict signed-in users on the account pages; public requests remain open |
| Default loan duration | 14 days | Default pickup-to-return period |
| Due soon reminder | 2 days | Reminder lead time |
| Overdue reminder interval | 1 day | Repeat interval for overdue notices |
| Max active loans per user | 0 | 0 means unlimited |
| Max items per request | 25 | Request line limit |
| Require approval | on | Requests must be approved before handout |
| Allow walk-in checkout | on | Desk staff can create direct loans |
| Allow renewals / max renewals | on / 1 | Renewal policy |
| Require responsibility acknowledgement | on | Requester agreement checkbox |
| Allow public requests | on | Allow requests without an InvenTree account |
| Require a signature on requests | on | Requester must sign before submitting |
| Require a signature on handout | on | Borrower must sign when items are issued |
| Require a signature on return | on | Borrower must sign when items are returned |
| Require core stock permission | off | Also check InvenTree stock change permission |
| Lock loaned stock items | on | Block core edits/moves/allocations |
| Hide borrower details from requesters | on | Privacy for scanned items |
| Enable email | off | Send emails in addition to in-app notifications |
| Email from address | — | Optional sender override |
| Loan QR prefix | `LOAN:` | Accepted prefix before QR token |
| Catalog page size | 20 | Search result limit |

## Development

```bash
python -m venv .venv
.venv\Scripts\activate
pip install -e ".[dev]"
```

Run the plugin tests inside an InvenTree environment:

```bash
python manage.py test inventree_loan
```

or with pytest-django:

```bash
python manage.py test inventree_loan
```

The tests expect the plugin app to be enabled, as InvenTree's test runner enables
installed plugins. Tests cover role hierarchy, request visibility, approval
permissions, automatic/manual allocation, public availability limits,
serialized stock rules, checkout, partial returns, and status restoration.

## Packaging

```bash
python -m build
```

The built wheel includes templates, static assets, migrations, and the
`inventree_plugins` entry point.

## Limitations and notes

- The plugin targets InvenTree 1.6–1.7. Test against a new major series before
  widening `MAX_VERSION`.
- Plugin roles are separate from InvenTree groups by design; see the optional core
  stock permission setting.
- The custom On Loan status must exist before handout. The setup page and management
  command handle this.
- Returned stock items are not automatically merged into their parent item
  (`merge=False`), which preserves the loan history link. Staff can merge stock
  manually if desired.
- Camera scanning depends on browser support; USB scanners and manual entry always
  work.
- The public catalogue intentionally exposes part names, descriptions, stock
  locations and available quantities. It does not expose pricing, suppliers,
  borrower details, or other users.
- Loan history is append-only in the UI. Use the Django admin only for emergency
  maintenance.
