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:
Owen
2026-08-17 16:28:14 -05:00
parent 42e7b2a21c
commit fb85c9edbb
31 changed files with 975 additions and 77 deletions
+74 -9
View File
@@ -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 {