Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ElthonJohan/Sistema-MRP/llms.txt

Use this file to discover all available pages before exploring further.

The requirement service sits at the top of the MRP demand chain. When a work-site warehouse needs materials, a requirement is created here. The service immediately attempts to reserve matching stock in the principal warehouse, marking each line item reserved if stock is available or pending if not. Pending items are retried automatically whenever add_stock is called through reprocess_requirements. A requirement only advances to fulfilled or partial once a dispatch is created against it.

create_requirement

create_requirement(db, warehouse_id, items, budget_id=None) -> tuple[bool, str]
Creates a new requirement for a work-site warehouse. The function resolves the owner’s principal warehouse automatically, validates that budget_id is provided, then attempts to reserve stock for each line item in a single transaction. Each RequirementItem receives status reserved or pending depending on whether stock was available.
budget_id is mandatory. Omitting it or passing None returns (False, "Debes seleccionar un proyecto. El proyecto es obligatorio.") without creating any record.
db
Session
required
Active SQLAlchemy session. The caller opens and closes the session.
warehouse_id
int
required
ID of the work-site (obra) warehouse making the request.
items
object[]
required
List of material–quantity pairs to request.
[
  {"material_id": 1, "qty": 10},
  {"material_id": 2, "qty": 5}
]
budget_id
int
required
ID of the project this requirement belongs to. The project must exist. Passing None causes an early return with an error message.
[0]
bool
True when the requirement was created successfully.
[1]
str
Human-readable message. On success: "Requerimiento creado exitosamente". On failure: a descriptive error string (e.g. missing warehouse, missing project).
from services.requirement_service import create_requirement

ok, msg = create_requirement(
    db,
    warehouse_id=3,
    items=[
        {"material_id": 1, "qty": 50},
        {"material_id": 4, "qty": 12},
    ],
    budget_id=7,
)

if ok:
    print("Requirement created:", msg)
else:
    print("Error:", msg)

get_requirements

get_requirements(db, skip, limit, requirement_id, status, start_date, end_date, warehouse_ids) -> tuple[list, int]
Retrieves requirements with optional filtering and pagination. Results are ordered by created_at descending. All filter parameters are optional; omitting them returns all requirements.
db
Session
required
Active SQLAlchemy session.
skip
int
required
Number of records to skip. Use 0 for the first page.
limit
int
required
Maximum number of records to return per page.
requirement_id
int
Filter to a single requirement by ID.
status
str
Filter by status. Accepted values: "pending", "partial", "fulfilled", "cancelled".
start_date
datetime
Return only requirements created on or after this date.
end_date
datetime
Return only requirements created on or before this date.
warehouse_ids
int[]
List of obra warehouse IDs to filter by. When None, requirements from all warehouses are returned.
[0]
list
List of Requirement ORM objects for the requested page.
[1]
int
Total count of matching requirements before pagination, useful for building page controls.
from services.requirement_service import get_requirements
from datetime import datetime

requirements, total = get_requirements(
    db,
    skip=0,
    limit=20,
    requirement_id=None,
    status="pending",
    start_date=datetime(2025, 1, 1),
    end_date=datetime(2025, 12, 31),
    warehouse_ids=[3, 4],
)

print(f"Showing {len(requirements)} of {total} pending requirements")

cancel_requirement

cancel_requirement(db, requirement_id, reason=None) -> bool
Cancels a requirement and releases all reserved stock back to available inventory. For each line item with status reserved, release_reservation is called against the principal warehouse. The requirement status is set to cancelled. Returns False when the requirement does not exist.
db
Session
required
Active SQLAlchemy session. Commits internally via release_reservation.
requirement_id
int
required
ID of the requirement to cancel.
reason
str
Optional free-text reason stored in Requirement.notes.
return
bool
True when the requirement was found and cancelled; False otherwise.
from services.requirement_service import cancel_requirement

ok = cancel_requirement(db, requirement_id=12, reason="Material not needed this cycle")
if not ok:
    print("Requirement not found")

reprocess_requirements

reprocess_requirements(db) -> list
Scans all requirements with status pending or partial and attempts to reserve stock for any line items still in pending state. This function is called automatically by add_stock after new stock is committed; it should rarely need to be called directly. For each requirement, if all items transition to reserved or fulfilled during this pass, the requirement is added to the returned list so the caller can notify interested parties. The parent requirement status (pending/partial) does not change here—only a successful dispatch changes it to fulfilled.
db
Session
required
Active SQLAlchemy session. Commits at the end of processing.
return
list
List of Requirement ORM objects that had at least one pending item and are now fully reserved (all items reserved or fulfilled). Empty when no requirements reached that state.
from services.requirement_service import reprocess_requirements

newly_ready = reprocess_requirements(db)
for req in newly_ready:
    print(f"Requirement #{req.id} is now fully reserved and ready to dispatch")

Build docs developers (and LLMs) love