<<

NAME

Koha::Library::Calendar - Object containing a library's calendar

SYNOPSIS

  use Koha::Library::Calendar

  my $c = Koha::Library::Calendar->new( branchcode => 'MAIN' );
  my $dt = dt_from_string();

  # are we open
  $open = $c->is_holiday($dt);
  # when will item be due if loan period = $dur (a DateTime::Duration object)
  $duedate = $c->addDuration($dt,$dur,'days');

DESCRIPTION

  Implements those features of C4::Calendar needed for Staffs Rolling Loans

METHODS

_init

    $calendar->_init();

Populates the object with repeatable closed days (weekly and day/month) for the branch. Called automatically by new.

_holidays

    my $holidays = $calendar->_holidays();

Returns a hashref of special (one-off) holidays for the branch, keyed by YYYYMMDD date strings. Results are cached.

new : Create a calendar object

my $calendar = Koha::Library::Calendar->new( branchcode => 'MAIN' );

The option branchcode is required

addDuration

    my $dt = $calendar->addDuration($date, $dur, $unit)

$date is a DateTime object representing the starting date of the interval.

$offset is a DateTime::Duration to add to it

$unit is a string value 'days' or 'hours' toflag granularity of duration

Currently unit is only used to invoke Staffs return Monday at 10 am rule this parameter will be removed when issuingrules properly cope with that

addHours

    my $dt = $calendar->addHours($date, $dur, $return_by_hour )

$date is a DateTime object representing the starting date of the interval.

$offset is a DateTime::Duration to add to it

$return_by_hour is an integer value representing the opening hour for the branch

get_push_amt

    my $amt = $calendar->get_push_amt($date)

$date is a DateTime object representing a closed return date

Using the days_mode syspref value and the nature of the closed return date, return how many days we should jump forward to find another return date

addDays

    my $dt = $calendar->addDays($date, $dur)

$date is a DateTime object representing the starting date of the interval.

$offset is a DateTime::Duration to add to it

$unit is a string value 'days' or 'hours' toflag granularity of duration

Currently unit is only used to invoke Staffs return Monday at 10 am rule this parameter will be removed when issuingrules properly cope with that

is_holiday

$yesno = $calendar->is_holiday($dt);

passed a DateTime object returns 1 if it is a closed day 0 if not according to the calendar

days_between

$duration = $calendar->days_between($start_dt, $end_dt);

Passed two dates returns a DateTime::Duration object measuring the length between them ignoring closed days. Always returns a positive number irrespective of the relative order of the parameters.

Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day

hours_between

$duration = $calendar->hours_between($start_dt, $end_dt);

Passed two dates returns a DateTime::Duration object measuring the length between them ignoring closed days. Always returns a positive number irrespective of the relative order of the parameters.

Note: This routine assumes neither the passed start_dt nor end_dt can be a closed day

has_business_days_between

$boolean = $calendar->has_business_days_between($start_dt, $end_dt);

Passed two DateTime objects, returns 1 if there are any business days (non-holidays) between the start and end dates (exclusive), 0 otherwise.

This method is useful for determining if business days were missed between two dates, such as when checking if server downtime caused business days to be skipped.

Examples: - Monday to Wednesday with Tuesday as business day: returns 1 - Monday to Tuesday (consecutive days): returns 0 - Friday to Sunday with Saturday as holiday: returns 0 - Same date: returns 0

next_open_days

$datetime = $calendar->next_open_days($duedate_dt, $to_add)

Passed a Datetime and number of days, returns another Datetime representing the next open day after adding the passed number of days. It is intended for use to calculate the due date when useDaysMode syspref is set to either 'Datedue', 'Calendar' or 'Dayweek'.

prev_open_days

$datetime = $calendar->prev_open_days($duedate_dt, $to_sub)

Passed a Datetime and a number of days, returns another Datetime representing the previous open day after subtracting the number of passed days. It is intended for use to calculate the due date when useDaysMode syspref is set to either 'Datedue', 'Calendar' or 'Dayweek'.

days_forward

$datetime = $calendar->days_forward($start_dt, $to_add)

Passed a DateTime and a number of days, returns a DateTime representing that many open days after the starting date. Closed days (weekly closures and holidays) are skipped without being counted toward $to_add, so the result is always $to_add open days on from $start_dt.

Unlike "next_open_days", this method does not consult useDaysMode: it always advances one calendar day at a time. It is intended for windows that should give the patron a fixed number of open days, such as a hold's pickup shelf expiration -- not for aligning a recurring due-date to a particular weekday.

set_daysmode

For testing only allows the calling script to change days mode

clear_weekly_closed_days

In test mode changes the testing set of closed days to a new set with no closed days. TODO passing an array of closed days to this would allow testing of more configurations

add_holiday

Passed a datetime object this will add it to the calendar's list of closed days. This is for testing so that we can alter the Calendar object's list of specified dates

CRUD methods

add_weekly_closure

    $calendar->add_weekly_closure({ weekday => 0, title => 'Sundays', description => '' });

add_repeating_closure

    $calendar->add_repeating_closure({ day => 25, month => 12, title => 'Christmas', description => '' });

add_single_closure

    $calendar->add_single_closure({ date => '2026-06-15', title => 'Staff day', description => '' });

add_exception

    $calendar->add_exception({ date => '2026-12-25', title => 'Special opening', description => '' });

delete_weekly_closure

    $calendar->delete_weekly_closure({ weekday => 0 });

delete_repeating_closure

    $calendar->delete_repeating_closure({ day => 25, month => 12 });

delete_single_closure

    $calendar->delete_single_closure({ date => '2026-06-15' });

delete_exception

    $calendar->delete_exception({ date => '2026-12-25' });

closed_dates_in_range

    my $dates = $calendar->closed_dates_in_range($from_dt, $to_dt);

Returns an arrayref of ISO date strings (YYYY-MM-DD) representing every closed day between $from_dt and $to_dt (inclusive), taking weekly, annual-repeating, single and exception (open override) closures into account. Exceptions win: a date listed as an exception is never returned even if a weekly or repeating rule would otherwise close it.

copy_to

    $calendar->copy_to($target_branchcode);

Copies closure rules to another branch inside a single transaction.

Weekly and repeating closures are always copied. Single closures and exceptions are only copied when their date is today or later (past dates are filtered out by design).

Rows already present on the target (matched by (library_id, weekday), (library_id, day, month) or (library_id, date)) are skipped so the operation is idempotent.

Flushes the target library's _holidays cache before returning.

Internal methods

_init

Loads weekly closed days and repeating annual closures from the database into the object's internal data structures.

_holidays

Returns a hashref of single closures and exceptions keyed by YYYYMMDD. Values are 1 for closures and 0 for exceptions (open overrides). Results are cached in memcache.

DIAGNOSTICS

Will croak if not passed a branchcode in new

BUGS AND LIMITATIONS

This only contains a limited subset of the functionality in C4::Calendar Only enough to support Staffs Rolling loans

AUTHOR

Colin Campbell colin.campbell@ptfs-europe.com

LICENSE AND COPYRIGHT

Copyright (c) 2011 PTFS-Europe Ltd All rights reserved

Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Koha; if not, see <https://www.gnu.org/licenses>.

<<