Add asset checkout, quantity/uniqueness, contacts, and QR labels
Checkout:
- assets.AssignUser + POST /app/assets/{id}/checkout assign/unassign an
asset's user independent of the full edit form. New shared
#checkoutModal, wired from a "Check Out..." context-menu action, backed
by GET /app/assets/{id} and GET /app/users/options JSON endpoints.
Quantity / uniqueness:
- Item types gain an IsUnique flag (checked by default, matching prior
behavior). Assets gain a Quantity column, fixed at 1 for unique types
and adjustable via +/- steppers (POST /app/assets/{id}/quantity) for
non-unique ones. Flipping a type back to unique clamps existing assets'
quantity back to 1. CSV import/export gained a Quantity column.
Migration 0003_quantity.sql.
Contacts:
- New models.RoleContact: an assignable person who cannot sign in
(User.CanLogin). Migration 0004_contacts.sql rebuilds the users table
(SQLite can't alter a CHECK constraint in place) to allow the role.
Users form/list updated; contact creation auto-generates an unusable
password since the login path is blocked by role regardless.
QR asset tags and printable labels:
- Each asset's QR code (via github.com/skip2/go-qrcode, pure Go) encodes
an absolute /scan/<tag> URL; GET /scan/{tag} resolves it and redirects
into /inventory?asset=<id>, which app.js picks up on load to
auto-select that asset - works from a phone camera or a USB
scanner-as-keyboard-wedge pointed at the address bar.
- GET /labels renders a print-ready sheet (right-click an asset for one
label, a folder for every asset in it) with a template switcher between
Avery 5160 (verified dimensions) and 61526 asset tag labels (best-effort
layout, flagged as such on the page).
Also refreshed README to reflect the right-click-first tree UX and all of
the above.
This commit is contained in:
@@ -106,6 +106,24 @@ func (s *Store) tagExists(ctx context.Context, tag string, excludeID int64) (boo
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
// resolveQuantity clamps the requested quantity against the asset's item
|
||||
// type: unique types are always exactly 1; non-unique types default to 1
|
||||
// when the request is less than 1 (0 or unset), since AdjustQuantity is the
|
||||
// intended path for deliberately going to 0 ("out of stock").
|
||||
func (s *Store) resolveQuantity(ctx context.Context, itemTypeID int64, requested int) (int, error) {
|
||||
var isUnique bool
|
||||
if err := s.db.QueryRowContext(ctx, `SELECT is_unique FROM item_types WHERE id = ?`, itemTypeID).Scan(&isUnique); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if isUnique {
|
||||
return 1, nil
|
||||
}
|
||||
if requested < 1 {
|
||||
return 1, nil
|
||||
}
|
||||
return requested, nil
|
||||
}
|
||||
|
||||
// Create inserts a new asset. If a.AssetTag is empty, one is generated.
|
||||
// fieldValues maps custom_field_defs.id to the submitted value.
|
||||
func (s *Store) Create(ctx context.Context, a *models.Asset, fieldValues map[int64]string) error {
|
||||
@@ -123,6 +141,11 @@ func (s *Store) Create(ctx context.Context, a *models.Asset, fieldValues map[int
|
||||
} else if taken {
|
||||
return ErrTagTaken
|
||||
}
|
||||
quantity, err := s.resolveQuantity(ctx, a.ItemTypeID, a.Quantity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Quantity = quantity
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -131,9 +154,9 @@ func (s *Store) Create(ctx context.Context, a *models.Asset, fieldValues map[int
|
||||
defer tx.Rollback()
|
||||
|
||||
res, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO assets (asset_id, name, item_type_id, folder_id, status_id, assigned_user_id, notes)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
a.AssetTag, a.Name, a.ItemTypeID, a.FolderID, a.StatusID, a.AssignedUserID, a.Notes)
|
||||
`INSERT INTO assets (asset_id, name, item_type_id, folder_id, status_id, assigned_user_id, notes, quantity)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
a.AssetTag, a.Name, a.ItemTypeID, a.FolderID, a.StatusID, a.AssignedUserID, a.Notes, a.Quantity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -173,6 +196,11 @@ func (s *Store) Update(ctx context.Context, a *models.Asset, fieldValues map[int
|
||||
} else if taken {
|
||||
return ErrTagTaken
|
||||
}
|
||||
quantity, err := s.resolveQuantity(ctx, a.ItemTypeID, a.Quantity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Quantity = quantity
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -181,9 +209,9 @@ func (s *Store) Update(ctx context.Context, a *models.Asset, fieldValues map[int
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err = tx.ExecContext(ctx,
|
||||
`UPDATE assets SET asset_id = ?, name = ?, item_type_id = ?, folder_id = ?, status_id = ?, assigned_user_id = ?, notes = ?,
|
||||
`UPDATE assets SET asset_id = ?, name = ?, item_type_id = ?, folder_id = ?, status_id = ?, assigned_user_id = ?, notes = ?, quantity = ?,
|
||||
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?`,
|
||||
a.AssetTag, a.Name, a.ItemTypeID, a.FolderID, a.StatusID, a.AssignedUserID, a.Notes, a.ID)
|
||||
a.AssetTag, a.Name, a.ItemTypeID, a.FolderID, a.StatusID, a.AssignedUserID, a.Notes, a.Quantity, a.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -209,6 +237,42 @@ func (s *Store) Move(ctx context.Context, id int64, folderID *int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// AssignUser checks an asset out to a user, or checks it in (unassigns)
|
||||
// when userID is nil. It touches only the assignment, nothing else.
|
||||
func (s *Store) AssignUser(ctx context.Context, id int64, userID *int64) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`UPDATE assets SET assigned_user_id = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?`,
|
||||
userID, id)
|
||||
return err
|
||||
}
|
||||
|
||||
var ErrQuantityNotTracked = errors.New("quantity is not tracked for unique item types")
|
||||
|
||||
// AdjustQuantity increments or decrements an asset's quantity by delta,
|
||||
// floored at 0. It fails for assets whose item type is unique (quantity
|
||||
// stays fixed at 1 for those; use duplicate/create for more distinct
|
||||
// units instead).
|
||||
func (s *Store) AdjustQuantity(ctx context.Context, id int64, delta int) (*models.Asset, error) {
|
||||
a, err := s.Get(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.ItemTypeIsUnique {
|
||||
return nil, ErrQuantityNotTracked
|
||||
}
|
||||
newQty := a.Quantity + delta
|
||||
if newQty < 0 {
|
||||
newQty = 0
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx,
|
||||
`UPDATE assets SET quantity = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?`,
|
||||
newQty, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.Quantity = newQty
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// Duplicate copies an asset's fields and custom field values into a new
|
||||
// asset with a freshly generated asset tag.
|
||||
func (s *Store) Duplicate(ctx context.Context, id int64) (*models.Asset, error) {
|
||||
@@ -228,6 +292,7 @@ func (s *Store) Duplicate(ctx context.Context, id int64) (*models.Asset, error)
|
||||
StatusID: orig.StatusID,
|
||||
AssignedUserID: orig.AssignedUserID,
|
||||
Notes: orig.Notes,
|
||||
Quantity: orig.Quantity,
|
||||
}
|
||||
values := map[int64]string{}
|
||||
for _, fv := range orig.CustomFields {
|
||||
@@ -242,8 +307,8 @@ func (s *Store) Duplicate(ctx context.Context, id int64) (*models.Asset, error)
|
||||
}
|
||||
|
||||
const assetSelect = `
|
||||
SELECT a.id, a.asset_id, a.name, a.item_type_id, a.folder_id, a.status_id, a.assigned_user_id, a.notes, a.created_at, a.updated_at,
|
||||
it.name, it.icon, st.name, st.color, COALESCE(u.username, '')
|
||||
SELECT a.id, a.asset_id, a.name, a.item_type_id, a.folder_id, a.status_id, a.assigned_user_id, a.notes, a.quantity, a.created_at, a.updated_at,
|
||||
it.name, it.icon, it.is_unique, st.name, st.color, COALESCE(u.username, '')
|
||||
FROM assets a
|
||||
JOIN item_types it ON it.id = a.item_type_id
|
||||
JOIN statuses st ON st.id = a.status_id
|
||||
@@ -253,8 +318,8 @@ func scanAsset(row interface{ Scan(...any) error }) (*models.Asset, error) {
|
||||
var a models.Asset
|
||||
var folderID, assignedUserID sql.NullInt64
|
||||
var created, updated string
|
||||
if err := row.Scan(&a.ID, &a.AssetTag, &a.Name, &a.ItemTypeID, &folderID, &a.StatusID, &assignedUserID, &a.Notes, &created, &updated,
|
||||
&a.ItemTypeName, &a.ItemTypeIcon, &a.StatusName, &a.StatusColor, &a.AssignedUsername); err != nil {
|
||||
if err := row.Scan(&a.ID, &a.AssetTag, &a.Name, &a.ItemTypeID, &folderID, &a.StatusID, &assignedUserID, &a.Notes, &a.Quantity, &created, &updated,
|
||||
&a.ItemTypeName, &a.ItemTypeIcon, &a.ItemTypeIsUnique, &a.StatusName, &a.StatusColor, &a.AssignedUsername); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if folderID.Valid {
|
||||
|
||||
+12
-1
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"inventree/internal/models"
|
||||
@@ -19,6 +20,7 @@ const (
|
||||
colStatus = "status"
|
||||
colAssignedUser = "assigned user"
|
||||
colNotes = "notes"
|
||||
colQuantity = "quantity"
|
||||
cfColPrefix = "cf_"
|
||||
)
|
||||
|
||||
@@ -201,6 +203,13 @@ func (s *Store) Import(ctx context.Context, r io.Reader, opts ImportOptions,
|
||||
}
|
||||
}
|
||||
|
||||
quantity := 1
|
||||
if qtyStr := get(row, colQuantity); qtyStr != "" {
|
||||
if n, err := strconv.Atoi(qtyStr); err == nil {
|
||||
quantity = n
|
||||
}
|
||||
}
|
||||
|
||||
asset := &models.Asset{
|
||||
AssetTag: get(row, colAssetID),
|
||||
Name: name,
|
||||
@@ -209,6 +218,7 @@ func (s *Store) Import(ctx context.Context, r io.Reader, opts ImportOptions,
|
||||
StatusID: statusID,
|
||||
AssignedUserID: assignedUserID,
|
||||
Notes: get(row, colNotes),
|
||||
Quantity: quantity,
|
||||
}
|
||||
|
||||
if asset.AssetTag != "" {
|
||||
@@ -282,7 +292,7 @@ func (s *Store) Export(ctx context.Context, w io.Writer, f ListFilter) error {
|
||||
}
|
||||
|
||||
cw := csv.NewWriter(w)
|
||||
header := []string{"Asset ID", "Name", "Item Type", "Folder", "Status", "Assigned User", "Notes", "Created At", "Updated At"}
|
||||
header := []string{"Asset ID", "Name", "Item Type", "Folder", "Status", "Assigned User", "Notes", "Quantity", "Created At", "Updated At"}
|
||||
for _, k := range fieldDefs {
|
||||
header = append(header, cfColPrefix+k)
|
||||
}
|
||||
@@ -304,6 +314,7 @@ func (s *Store) Export(ctx context.Context, w io.Writer, f ListFilter) error {
|
||||
}
|
||||
row := []string{
|
||||
a.AssetTag, a.Name, a.ItemTypeName, folderPathString(a.FolderPath), a.StatusName, a.AssignedUsername, a.Notes,
|
||||
strconv.Itoa(a.Quantity),
|
||||
a.CreatedAt.Format("2006-01-02 15:04:05"), a.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
values := map[string]string{}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package assets_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"inventree/internal/assets"
|
||||
"inventree/internal/itemtypes"
|
||||
"inventree/internal/models"
|
||||
)
|
||||
|
||||
func TestQuantityFixedAtOneForUniqueTypes(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
as, _, _ := testStores(t)
|
||||
|
||||
a := &models.Asset{Name: "Laptop 1", ItemTypeID: itemTypeLaptop, StatusID: statusInUse, Quantity: 7}
|
||||
if err := as.Create(ctx, a, nil); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
if a.Quantity != 1 {
|
||||
t.Fatalf("expected quantity forced to 1 for a unique item type, got %d", a.Quantity)
|
||||
}
|
||||
|
||||
if _, err := as.AdjustQuantity(ctx, a.ID, 1); err != assets.ErrQuantityNotTracked {
|
||||
t.Fatalf("expected ErrQuantityNotTracked, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuantityTrackedForNonUniqueTypes(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
as, _, db := testStores(t)
|
||||
itStore := itemtypes.NewStore(db)
|
||||
|
||||
cable := &models.ItemType{Name: "Cable", Enabled: true, IsUnique: false}
|
||||
if err := itStore.Create(ctx, cable); err != nil {
|
||||
t.Fatalf("create item type: %v", err)
|
||||
}
|
||||
|
||||
a := &models.Asset{Name: "USB-A Cable", ItemTypeID: cable.ID, StatusID: statusInUse, Quantity: 10}
|
||||
if err := as.Create(ctx, a, nil); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
if a.Quantity != 10 {
|
||||
t.Fatalf("expected quantity 10, got %d", a.Quantity)
|
||||
}
|
||||
|
||||
updated, err := as.AdjustQuantity(ctx, a.ID, 5)
|
||||
if err != nil {
|
||||
t.Fatalf("adjust +5: %v", err)
|
||||
}
|
||||
if updated.Quantity != 15 {
|
||||
t.Fatalf("expected 15, got %d", updated.Quantity)
|
||||
}
|
||||
|
||||
updated, err = as.AdjustQuantity(ctx, a.ID, -100)
|
||||
if err != nil {
|
||||
t.Fatalf("adjust -100: %v", err)
|
||||
}
|
||||
if updated.Quantity != 0 {
|
||||
t.Fatalf("expected quantity floored at 0, got %d", updated.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
func TestItemTypeBecomingUniqueClampsExistingQuantities(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
as, _, db := testStores(t)
|
||||
itStore := itemtypes.NewStore(db)
|
||||
|
||||
cable := &models.ItemType{Name: "Cable", Enabled: true, IsUnique: false}
|
||||
if err := itStore.Create(ctx, cable); err != nil {
|
||||
t.Fatalf("create item type: %v", err)
|
||||
}
|
||||
a := &models.Asset{Name: "USB-A Cable", ItemTypeID: cable.ID, StatusID: statusInUse, Quantity: 20}
|
||||
if err := as.Create(ctx, a, nil); err != nil {
|
||||
t.Fatalf("create asset: %v", err)
|
||||
}
|
||||
|
||||
cable.IsUnique = true
|
||||
if err := itStore.Update(ctx, cable); err != nil {
|
||||
t.Fatalf("update item type: %v", err)
|
||||
}
|
||||
|
||||
got, err := as.Get(ctx, a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.Quantity != 1 {
|
||||
t.Fatalf("expected quantity clamped to 1 after type became unique, got %d", got.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssignUserChecksInAndOut(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
as, _, _ := testStores(t)
|
||||
|
||||
a := &models.Asset{Name: "Laptop 1", ItemTypeID: itemTypeLaptop, StatusID: statusInUse}
|
||||
if err := as.Create(ctx, a, nil); err != nil {
|
||||
t.Fatalf("create: %v", err)
|
||||
}
|
||||
|
||||
userID := int64(1) // seeded admin
|
||||
if err := as.AssignUser(ctx, a.ID, &userID); err != nil {
|
||||
t.Fatalf("assign user: %v", err)
|
||||
}
|
||||
got, err := as.Get(ctx, a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.AssignedUserID == nil || *got.AssignedUserID != userID {
|
||||
t.Fatalf("expected asset checked out to user %d, got %+v", userID, got.AssignedUserID)
|
||||
}
|
||||
|
||||
if err := as.AssignUser(ctx, a.ID, nil); err != nil {
|
||||
t.Fatalf("check in: %v", err)
|
||||
}
|
||||
got, err = as.Get(ctx, a.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if got.AssignedUserID != nil {
|
||||
t.Fatalf("expected asset checked in (unassigned), got %+v", got.AssignedUserID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user