def update_dates_loan(
    record,
    start_date=None,
    end_date=None,
    request_start_date=None,
    request_expire_date=None,
):
    """Updates the dates of a loan."""
    state = record["state"]
    is_active_or_completed = (
        state in CIRCULATION_STATES_LOAN_ACTIVE
        or state in CIRCULATION_STATES_LOAN_COMPLETED
    )

    data = copy(record)

    if is_active_or_completed:
        today = date.today().strftime("%Y-%m-%d")
        if request_start_date or request_expire_date:
            raise IlsException(
                description="Cannot modify request dates of "
                "an active or completed loan."
            )
        if start_date:
            if start_date > today:
                raise InvalidParameterError(
                    description="Start date cannot be in "
                    "the future for active loans."
                )
            data["start_date"] = start_date
        if end_date:
            data["end_date"] = end_date
        if data["end_date"] < data["start_date"]:
            raise InvalidParameterError(description="Negative date range.")
    else:  # Pending or cancelled
        if start_date or end_date:
            raise IlsException(
                description="Cannot modify dates of "
                "a pending or cancelled loan."
            )
        if request_start_date:
            data["request_start_date"] = request_start_date
        if request_expire_date:
            data["request_expire_date"] = request_expire_date
        if data["request_expire_date"] < data["request_start_date"]:
            raise InvalidParameterError(description="Negative date range.")

    record.update(data)
    record.commit()
    db.session.commit()
    current_circulation.loan_indexer().index(record)

    return record
Example #2
0
def find_next_open_date(location_pid, date):
    """Finds the next day where this location is open."""
    location = current_app_ils.location_record_cls.get_record_by_pid(
        location_pid)
    _infinite_loop_guard = date + timedelta(days=365)
    while date < _infinite_loop_guard:
        if _is_open_on(location, date):
            return date
        date += _ONE_DAY_INCREMENT

    # Termination is normally guaranteed if there is at least one weekday open
    raise IlsException(description="Cannot find any date for which the "
                       "location %s is open after the given date %s."
                       "Please check opening/closures dates." %
                       (location_pid, date.isoformat()))