Voile.Schema.Library.Circulation (Voile v0.1.26)

Copy Markdown View Source

Summary

Functions

Assigns a requisition to staff member.

Calculate the number of days a transaction is overdue.

Calculate the fine amount for an overdue transaction.

Calculates membership expiry based on member type configuration.

Cancels a pending payment.

Creates a transaction (checkout) - respects node and member type policies.

Count active reservations (pending and available). If node_id is nil, returns count for all nodes. Reservations don't have unit_id, so we join to collection.

Count active transactions. If node_id is nil, returns count for all nodes.

Count history entries that occurred on a given date (UTC date comparison).

Count history entries for a specific event type on a given date.

Count overdue transactions. If node_id is nil, returns count for all nodes.

Creates a collection-level reservation - respects member type policies.

Creates a payment link for a fine using Xendit.

Creates a requisition. If requested_by_id is provided, sets request_date and status.

Creates a reservation for an item - simplified version for LiveView.

Creates a reservation - respects member type reservation policies.

Expires old reservations that haven't been picked up.

Marks a requisition as fulfilled.

Fulfills a reservation (converts to checkout).

Get the active transaction for a specific item. Returns nil if no active transaction found.

Returns a map with all circulation stats for a given node. If node_id is nil, returns stats for all nodes (super admin view).

Gets a fine by transaction id if present. Returns {:ok, fine} or {:error, reason}

Gets a fine with all related details including payments. Returns {:ok, fine} or {:error, reason}

Gets circulation history for an item.

Gets circulation history for a member.

Gets member's total outstanding fine amount.

Gets recommended items for a member based on their borrowing history and member type access.

Gets member type by slug.

Gets a payment by ID with preloaded associations.

Gets a payment by external_id.

Gets pending payments for a fine.

Safe non-bang version of get_transaction/1. Returns the transaction struct preloaded or nil if not found.

Handles Xendit webhook callback for payment updates.

Checks if any holidays are configured for the library.

Checks if weekly schedule is configured for the library.

Gets all active member types.

Returns the circulation history.

List circulation history paginated with filters and node filtering. Only returns history for items belonging to the specified node.

Gets expired reservations.

Lists payments for a fine.

List fines paginated with filters and node filtering. Only returns fines for items belonging to the specified node.

Gets active transactions for a member.

Lists all fines for a member (including pending, paid, and waived).

Lists paid fines for a member with pagination. Returns {[fines], total_pages}

Lists payments for a member.

Gets active reservations for a member.

Lists transaction history for a member (returned/completed transactions) with pagination. Returns {[transactions], total_pages}

Gets unpaid fines for a member.

Lists all members with active loans, grouped by member. Returns paginated results with member info and their active transaction count. Useful for librarians to see who has active loans and send manual reminders.

Gets overdue transactions.

Gets requisitions by status.

List reservations paginated with filters and node filtering. Only returns reservations for items belonging to the specified node.

List transactions paginated with filters and node filtering. Only returns transactions for items belonging to the specified node.

Gets transactions due soon (within specified days).

Manually marks a payment as paid (for in-person cash payments).

Marks a reservation as available for pickup.

Checks if a member's privileges should be suspended based on fines.

Auto-renew items for member types that support it and haven't reached max renewals.

Marks overdue transactions and creates fines based on member type policies.

Renews a transaction - respects member type renewal policies.

Determines whether to skip holidays in fine calculation based on library configuration. If no holidays are configured (library is open all day), skip holidays in calculation. If holidays are configured, use business days only.

Suggest items for autocomplete by item_code, collection title, or collection description. Returns a list of items (preloaded with collection) matching the query.

Calculate the total amount of unpaid fines for a member. Returns a Decimal representing the sum of all balances.

Sum outstanding fines. If node_id is nil, returns sum for all nodes.

Functions

approve_requisition(requisition_id, staff_notes \\ nil)

Approves a requisition.

assign_requisition(requisition_id, assigned_to_id)

Assigns a requisition to staff member.

calculate_days_for_fine(transaction, opts \\ [])

Calculate the number of days a transaction is overdue.

Options:

  • skip_holidays: boolean (default: false) - when true, counts ALL calendar days; when false, excludes holidays/weekends

Examples

iex> calculate_days_for_fine(transaction, skip_holidays: true)
10  # All calendar days

iex> calculate_days_for_fine(transaction, skip_holidays: false)
7   # Business days only (excluding weekends/holidays)

calculate_fine_amount(transaction, member_type, opts \\ [])

Calculate the fine amount for an overdue transaction.

Options:

  • skip_holidays: boolean (default: false) - when true, counts ALL calendar days; when false, excludes holidays/weekends

Returns the calculated fine amount as a Decimal, respecting the member type's fine_per_day and max_fine settings.

Examples

iex> calculate_fine_amount(transaction, member_type, skip_holidays: true)
#Decimal<50000>  # 10 days × 5000/day

iex> calculate_fine_amount(transaction, member_type, skip_holidays: false)
#Decimal<35000>  # 7 business days × 5000/day

calculate_membership_expiry(member_type, start_date \\ nil)

Calculates membership expiry based on member type configuration.

cancel_payment(payment_id, reason \\ nil)

Cancels a pending payment.

cancel_reservation(reservation_id, reason \\ nil)

Cancels a reservation.

change_circulation_history(circulation_history, attrs)

change_fine(fine, attrs)

change_requisition(requisition, attrs)

change_reservation(reservation, attrs)

change_transaction(transaction, attrs)

checkout_item(member_id, item_id, librarian_id, attrs \\ %{})

Creates a transaction (checkout) - respects node and member type policies.

Pass node or node_id in attrs to apply node-specific rules: checkout_item(member_id, item_id, librarian_id, %{node: node}) checkout_item(member_id, item_id, librarian_id, %{node_id: 123})

When node_id is provided but node is not, the node will be fetched automatically. Node rules are applied when node.override_loan_rules is true.

count_active_fines_by_user(user_id)

count_active_reservations(node_id)

Count active reservations (pending and available). If node_id is nil, returns count for all nodes. Reservations don't have unit_id, so we join to collection.

count_active_transactions(node_id)

Count active transactions. If node_id is nil, returns count for all nodes.

count_history_by_date(date)

Count history entries that occurred on a given date (UTC date comparison).

count_history_by_date_and_type(date, type)

Count history entries for a specific event type on a given date.

count_list_active_transactions(id)

count_member_unpaid_fines(member_id)

count_of_collection_based_on_status(status)

count_overdue_transactions(node_id)

Count overdue transactions. If node_id is nil, returns count for all nodes.

create_circulation_history(attrs \\ %{})

create_collection_reservation(member_id, collection_id, attrs \\ %{})

Creates a collection-level reservation - respects member type policies.

create_fine(attrs \\ %{})

create_requisition(attrs)

Creates a requisition. If requested_by_id is provided, sets request_date and status.

create_requisition(requested_by_id, attrs)

create_reservation(attrs \\ %{})

create_reservation(member_id, item_id)

Creates a reservation for an item - simplified version for LiveView.

create_reservation(member_id, item_id, librarian_id, attrs \\ %{})

Creates a reservation - respects member type reservation policies.

create_transaction(attrs \\ %{})

delete_circulation_history(circulation_history)

delete_fine(fine)

delete_requisition(requisition)

delete_reservation(reservation)

delete_transaction(transaction)

expire_old_reservations()

Expires old reservations that haven't been picked up.

fulfill_requisition(requisition_id)

Marks a requisition as fulfilled.

fulfill_reservation(reservation_id, librarian_id, attrs \\ %{})

Fulfills a reservation (converts to checkout).

get_active_transaction_by_item(item_id)

Get the active transaction for a specific item. Returns nil if no active transaction found.

get_admin_id_for_self_renewal()

get_circulation_history(id)

get_circulation_history!(id)

get_circulation_stats(node_id \\ nil)

Returns a map with all circulation stats for a given node. If node_id is nil, returns stats for all nodes (super admin view).

Examples

iex> get_circulation_stats(1)
%{active_transactions: 10, overdue_count: 2, active_reservations: 5, outstanding_fines: 50000}

iex> get_circulation_stats(nil)
%{active_transactions: 100, overdue_count: 20, active_reservations: 50, outstanding_fines: 500000}

get_fine!(id)

get_fine_by_transaction(transaction_id)

Gets a fine by transaction id if present. Returns {:ok, fine} or {:error, reason}

get_fine_with_details(fine_id)

Gets a fine with all related details including payments. Returns {:ok, fine} or {:error, reason}

get_item_history(item_id)

Gets circulation history for an item.

get_member_history(member_id)

Gets circulation history for a member.

get_member_outstanding_fine_amount(member_id)

Gets member's total outstanding fine amount.

get_member_recommendations(member_id, limit \\ 10)

Gets recommended items for a member based on their borrowing history and member type access.

get_member_type_by_slug(slug)

Gets member type by slug.

get_payment!(id)

Gets a payment by ID with preloaded associations.

get_payment_by_external_id(external_id)

Gets a payment by external_id.

get_pending_payment_for_fine(fine_id)

Gets pending payments for a fine.

get_requisition!(id)

get_reservation!(id)

get_total_fine_by_user(user_id)

get_transaction(id)

Safe non-bang version of get_transaction/1. Returns the transaction struct preloaded or nil if not found.

get_transaction!(id)

handle_payment_webhook(webhook_payload)

Handles Xendit webhook callback for payment updates.

Examples

iex> handle_payment_webhook(%{
  "external_id" => "payment_123",
  "status" => "PAID",
  "paid_amount" => 50000
})
{:ok, %Payment{status: "paid"}}

has_any_holidays_configured?(unit_id \\ nil)

Checks if any holidays are configured for the library.

has_weekly_schedule_configured?(unit_id \\ nil)

Checks if weekly schedule is configured for the library.

list_active_member_types()

Gets all active member types.

list_circulation_history(limit \\ 100)

Returns the circulation history.

list_circulation_history_paginated(page \\ 1, per_page \\ 10)

list_circulation_history_paginated_with_filters(page \\ 1, per_page \\ 10, filters \\ %{})

list_circulation_history_paginated_with_filters_by_member(member_id, page \\ 1, per_page \\ 50, opts \\ [])

Gets circulation history for a member with pagination and filters.

list_circulation_history_paginated_with_filters_by_node(page \\ 1, per_page \\ 10, filters \\ %{}, node_id)

List circulation history paginated with filters and node filtering. Only returns history for items belonging to the specified node.

list_expired_reservations()

Gets expired reservations.

list_fine_payments(fine_id)

Lists payments for a fine.

list_fines()

list_fines_paginated(page \\ 1, per_page \\ 10)

list_fines_paginated_with_filters(page \\ 1, per_page \\ 10, filters \\ %{})

list_fines_paginated_with_filters_by_node(page \\ 1, per_page \\ 10, filters \\ %{}, node_id)

List fines paginated with filters and node filtering. Only returns fines for items belonging to the specified node.

list_member_active_transactions(member_id)

Gets active transactions for a member.

list_member_active_transactions_paginated(member_id, page \\ 1, per_page \\ 10)

list_member_all_fines(member_id)

Lists all fines for a member (including pending, paid, and waived).

list_member_paid_fines_paginated(member_id, page \\ 1, per_page \\ 10)

Lists paid fines for a member with pagination. Returns {[fines], total_pages}

list_member_payments(member_id, opts \\ [])

Lists payments for a member.

list_member_requisitions_paginated(member_id, page \\ 1, per_page \\ 10)

list_member_reservations(member_id)

Gets active reservations for a member.

list_member_transaction_history_paginated(member_id, page \\ 1, per_page \\ 10)

Lists transaction history for a member (returned/completed transactions) with pagination. Returns {[transactions], total_pages}

list_member_unpaid_fines(member_id)

Gets unpaid fines for a member.

list_member_unpaid_fines_paginated(member_id, page \\ 1, per_page \\ 10)

list_members_with_active_loans_paginated(page \\ 1, per_page \\ 10, filters \\ %{})

Lists all members with active loans, grouped by member. Returns paginated results with member info and their active transaction count. Useful for librarians to see who has active loans and send manual reminders.

list_overdue_transactions()

Gets overdue transactions.

list_requisitions()

list_requisitions_by_status(status)

Gets requisitions by status.

list_requisitions_paginated(page \\ 1, per_page \\ 10)

list_requisitions_paginated_with_filters(page \\ 1, per_page \\ 10, filters \\ %{})

list_requisitions_paginated_with_filters_by_node(page \\ 1, per_page \\ 10, filters \\ %{}, node_id)

list_reservations()

list_reservations_paginated(page \\ 1, per_page \\ 10)

list_reservations_paginated_with_filters(page \\ 1, per_page \\ 10, filters \\ %{})

list_reservations_paginated_with_filters_by_node(page \\ 1, per_page \\ 10, filters \\ %{}, node_id)

List reservations paginated with filters and node filtering. Only returns reservations for items belonging to the specified node.

list_transaction_paginated_with_filter(page \\ 1, per_page \\ 10, filters \\ %{})

list_transaction_paginated_with_filter_by_node(page \\ 1, per_page \\ 10, filters \\ %{}, node_id)

List transactions paginated with filters and node filtering. Only returns transactions for items belonging to the specified node.

list_transactions()

list_transactions_due_soon(days \\ 3)

Gets transactions due soon (within specified days).

list_transactions_paginated(page \\ 1, per_page \\ 10)

mark_payment_as_paid(payment_id, processed_by_id)

Manually marks a payment as paid (for in-person cash payments).

mark_reservation_available(reservation_id, processed_by_id)

Marks a reservation as available for pickup.

member_privileges_suspended?(member_id)

Checks if a member's privileges should be suspended based on fines.

pay_fine(fine_id, payment_amount, payment_method, processed_by_id, receipt_number \\ nil)

Pays a fine (full or partial payment).

process_auto_renewals()

Auto-renew items for member types that support it and haven't reached max renewals.

process_overdue_items()

Marks overdue transactions and creates fines based on member type policies.

reject_requisition(requisition_id, staff_notes \\ nil)

Rejects a requisition.

renew_transaction(transaction_id, librarian_id, attrs \\ %{})

Renews a transaction - respects member type renewal policies.

return_item(transaction_id, librarian_id, attrs \\ %{})

Returns an item (return).

Options in attrs:

  • skip_holidays: boolean (default: false) - when true, counts ALL calendar days for fines; when false, excludes holidays/weekends from fine calculation
  • node: Node struct (optional) - for node-based fine calculation
  • node_id: integer (optional) - fetch node automatically for fine calculation

should_skip_holidays_in_fines?(unit_id \\ nil)

Determines whether to skip holidays in fine calculation based on library configuration. If no holidays are configured (library is open all day), skip holidays in calculation. If holidays are configured, use business days only.

suggest_items_by_code_or_collection(query, opts \\ [])

Suggest items for autocomplete by item_code, collection title, or collection description. Returns a list of items (preloaded with collection) matching the query.

sum_member_unpaid_fines(member_id)

Calculate the total amount of unpaid fines for a member. Returns a Decimal representing the sum of all balances.

sum_outstanding_fines(node_id)

Sum outstanding fines. If node_id is nil, returns sum for all nodes.

update_circulation_history(circulation_history, attrs)

update_fine(fine, attrs)

update_requisition(requisition, attrs)

update_reservation(reservation, attrs)

update_transaction(transaction, attrs)

waive_fine(fine_id, reason, waived_by_id)

Waives a fine.