View | Details | Raw Unified | Return to bug 17015
Collapse All | Expand All

(-)a/Koha/DiscreteCalendar.pm (-15 / +270 lines)
Lines 29-34 use C4::Output; Link Here
29
use Koha::Database;
29
use Koha::Database;
30
use Koha::DateUtils;
30
use Koha::DateUtils;
31
31
32
=head1 NAME
33
34
Koha::DiscreteCalendar - Object containing a branches calendar, working with the SQL database
35
36
=head1 SYNOPSIS
37
38
  use Koha::DiscreteCalendar
39
40
  my $c = Koha::DiscreteCalendar->new( branchcode => 'MAIN' );
41
  my $dt = DateTime->now();
42
43
  # are we open
44
  $open = $c->is_holiday($dt);
45
  # when will item be due if loan period = $dur (a DateTime::Duration object)
46
  $duedate = $c->addDate($dt,$dur,'days');
47
48
49
=head1 DESCRIPTION
50
51
  Implements a new Calendar object, but uses the SQL database to keep track of days and holidays.
52
  This results in a performance gain since the optimization is done by the MySQL database/team.
53
54
=head1 METHODS
55
56
=head2 new : Create a (discrete) calendar object
57
58
my $calendar = Koha::DiscreteCalendar->new( branchcode => 'MAIN' );
59
60
The option branchcode is required
61
62
=cut
63
32
sub new {
64
sub new {
33
    my ( $classname, %options ) = @_;
65
    my ( $classname, %options ) = @_;
34
    my $self = {};
66
    my $self = {};
Lines 67-73 sub _init { Link Here
67
99
68
}
100
}
69
101
70
# Returns an array of all the date objects of current branch.
102
=head2 getDatesInfo
103
104
  my @dates = $calendar->getDatesInfo();
105
106
Returns an array of hashes representing the dates in this calendar. The hash
107
contains the fields C<$date>, C<$outputdate>, C<$holidaytype>, C<$openhour>,
108
C<$closehour> and C<$note>.
109
110
=cut
111
71
sub getDatesInfo {
112
sub getDatesInfo {
72
    my $self = shift;
113
    my $self = shift;
73
    my $branchcode = $self->{branchcode};
114
    my $branchcode = $self->{branchcode};
Lines 101-109 sub getDatesInfo { Link Here
101
    return @datesInfos;
142
    return @datesInfos;
102
}
143
}
103
144
104
# This methode will copy everything from a given branch to a new branch
145
=head2 add_new_branch
146
147
    Koha::DiscreteCalendar->add_new_branch($copyBranch, $newBranch)
148
149
This methode will copy everything from a given branch to a new branch
150
C<$copyBranch> is the branch to copy from
151
C<$newBranch> is the branch to be created, and copy into
152
153
=cut
154
105
sub add_new_branch {
155
sub add_new_branch {
106
    my ($self, $copyBranch, $newBranch) = @_;
156
    my ( $classname, $copyBranch, $newBranch) = @_;
157
107
    $copyBranch = 'DFLT' unless $copyBranch;
158
    $copyBranch = 'DFLT' unless $copyBranch;
108
    my $schema = Koha::Database->new->schema;
159
    my $schema = Koha::Database->new->schema;
109
160
Lines 124-130 sub add_new_branch { Link Here
124
175
125
}
176
}
126
177
127
# DiscreteCalendar data transfer object (DTO)
178
=head2 get_date_info
179
180
    my $date = $calendar->get_date_info;
181
182
Returns a reference-to-hash representing a DiscreteCalendar date data object.
183
The hash contains the fields C<$date>, C<$outputdate>, C<$holidaytype>,
184
C<$openhour>, C<$closehour> and C<$note>.
185
186
=cut
187
128
sub get_date_info {
188
sub get_date_info {
129
    my ($self, $date) = @_;
189
    my ($self, $date) = @_;
130
    my $branchcode = $self->{branchcode};
190
    my $branchcode = $self->{branchcode};
Lines 159-165 sub get_date_info { Link Here
159
    return $dateDTO;
219
    return $dateDTO;
160
}
220
}
161
221
162
# Returns the furthest date available in the databse of current branch.
222
=head2 getMaxDate
223
224
    my $maxDate = $calendar->getMaxDate();
225
226
Returns the furthest date available in the databse of current branch.
227
228
=cut
229
163
sub getMaxDate {
230
sub getMaxDate {
164
    my $self       = shift;
231
    my $self       = shift;
165
    my $branchcode     = $self->{branchcode};
232
    my $branchcode     = $self->{branchcode};
Lines 178-184 sub getMaxDate { Link Here
178
    return $rs->next()->get_column('max');
245
    return $rs->next()->get_column('max');
179
}
246
}
180
247
181
# Returns the onldest date available in the databse of current branch.
248
=head2 getMinDate
249
250
    my $minDate = $calendar->getMinDate();
251
252
Returns the oldest date available in the databse of current branch.
253
254
=cut
255
182
sub getMinDate {
256
sub getMinDate {
183
    my $self       = shift;
257
    my $self       = shift;
184
    my $branchcode     = $self->{branchcode};
258
    my $branchcode     = $self->{branchcode};
Lines 197-203 sub getMinDate { Link Here
197
    return $rs->next()->get_column('min');
271
    return $rs->next()->get_column('min');
198
}
272
}
199
273
200
# Returns an array of all the date objects that are unique holidays.
274
=head2 get_unique_holidays
275
276
  my @unique_holidays = $calendar->get_unique_holidays();
277
278
Returns an array of all the date objects that are unique holidays.
279
280
=cut
281
201
sub get_unique_holidays {
282
sub get_unique_holidays {
202
    my $self = shift;
283
    my $self = shift;
203
    my $branchcode = $self->{branchcode};
284
    my $branchcode = $self->{branchcode};
Lines 227-233 sub get_unique_holidays { Link Here
227
    return @unique_holidays;
308
    return @unique_holidays;
228
}
309
}
229
310
230
# Returns an array of all the date objects  that are float holidays.
311
=head2 get_float_holidays
312
313
  my @float_holidays = $calendar->get_float_holidays();
314
315
Returns an array of all the date objects that are float holidays.
316
317
=cut
318
231
sub get_float_holidays {
319
sub get_float_holidays {
232
    my $self = shift;
320
    my $self = shift;
233
    my $branchcode = $self->{branchcode};
321
    my $branchcode = $self->{branchcode};
Lines 257-264 sub get_float_holidays { Link Here
257
    return @float_holidays;
345
    return @float_holidays;
258
}
346
}
259
347
260
# Returns an array of all the date objects  that are need validation holidays.
348
=head2 get_need_validation_holidays
261
sub get_need_valdation_holidays {
349
350
  my @need_validation_holidays = $calendar->get_need_validation_holidays();
351
352
Returns an array of all the date objects that are float holidays in need of validation.
353
354
=cut
355
356
sub get_need_validation_holidays {
262
    my $self = shift;
357
    my $self = shift;
263
    my $branchcode = $self->{branchcode};
358
    my $branchcode = $self->{branchcode};
264
    my @need_validation_holidays;
359
    my @need_validation_holidays;
Lines 287-293 sub get_need_valdation_holidays { Link Here
287
    return @need_validation_holidays;
382
    return @need_validation_holidays;
288
}
383
}
289
384
290
# Returns an array of all the date objects  that are repeatable holidays.
385
=head2 get_repeatable_holidays
386
387
  my @repeatable_holidays = $calendar->get_repeatable_holidays();
388
389
Returns an array of all the date objects that are repeatable holidays.
390
391
=cut
392
291
sub get_repeatable_holidays {
393
sub get_repeatable_holidays {
292
    my $self = shift;
394
    my $self = shift;
293
    my $branchcode = $self->{branchcode};
395
    my $branchcode = $self->{branchcode};
Lines 317-323 sub get_repeatable_holidays { Link Here
317
    return @repeatable_holidays;
419
    return @repeatable_holidays;
318
}
420
}
319
421
320
# Returns an array of all the date objects  that are weekly holidays.
422
=head2 get_week_days_holidays
423
424
  my @week_days_holidays = $calendar->get_week_days_holidays;
425
426
Returns an array of all the date objects that are weekly holidays.
427
428
=cut
429
321
sub get_week_days_holidays {
430
sub get_week_days_holidays {
322
    my $self = shift;
431
    my $self = shift;
323
    my $branchcode = $self->{branchcode};
432
    my $branchcode = $self->{branchcode};
Lines 346-352 sub get_week_days_holidays { Link Here
346
    return @week_days;
455
    return @week_days;
347
}
456
}
348
457
349
=head1 edit_holiday
458
=head2 edit_holiday
350
459
351
Modifies a date or a range of dates
460
Modifies a date or a range of dates
352
461
Lines 484-489 sub edit_holiday { Link Here
484
    $schema->storage->txn_commit;
593
    $schema->storage->txn_commit;
485
}
594
}
486
595
596
=head2 remove_weekly_holidays
597
598
    $calendar->remove_weekly_holidays($weekday, $updateValues, $today);
599
600
Removes a weekly holiday and updates the days' parameters
601
C<$weekday> is the weekday to un-holiday
602
C<$updatevalues> is hashref containing the new parameters
603
C<$today> is today's date
604
605
=cut
606
487
sub remove_weekly_holidays {
607
sub remove_weekly_holidays {
488
    my ($self, $weekday, $updateValues, $today) = @_;
608
    my ($self, $weekday, $updateValues, $today) = @_;
489
    my $branchcode = $self->{branchcode};
609
    my $branchcode = $self->{branchcode};
Lines 505-510 sub remove_weekly_holidays { Link Here
505
    }
625
    }
506
}
626
}
507
627
628
=head2 remove_repeatable_holidays
629
630
    $calendar->remove_repeatable_holidays($startDate, $endDate, $today);
631
632
Removes a repeatable holiday and updates the days' parameters
633
C<$startDatey> is the start date of the repeatable holiday
634
C<$endDate> is the end date of the repeatble holiday
635
C<$updatevalues> is hashref containing the new parameters
636
C<$today> is today's date
637
638
=cut
639
508
sub remove_repeatable_holidays {
640
sub remove_repeatable_holidays {
509
    my ($self, $startDate, $endDate, $updateValues, $today) = @_;
641
    my ($self, $startDate, $endDate, $updateValues, $today) = @_;
510
    my $branchcode = $self->{branchcode};
642
    my $branchcode = $self->{branchcode};
Lines 533-538 sub remove_repeatable_holidays { Link Here
533
    }
665
    }
534
}
666
}
535
667
668
=head2 copyToBranch
669
670
  $calendar->copyToBranch($branch2);
671
672
Copies the days and holidays from this branch to $branch2, ignoring dates in C<$self>
673
but not in C<$branch2>
674
675
C<$branch2> the branch to copy into
676
677
=cut
678
536
sub copyToBranch {
679
sub copyToBranch {
537
    my ($self,$newBranch) =@_;
680
    my ($self,$newBranch) =@_;
538
    my $branchcode = $self->{branchcode};
681
    my $branchcode = $self->{branchcode};
Lines 570-575 sub copyToBranch { Link Here
570
    }
713
    }
571
}
714
}
572
715
716
=head2 isOpened
717
718
    $calendar->isOpened($date)
719
720
Returns whether the library is open on C<$date>
721
722
=cut
723
573
sub isOpened {
724
sub isOpened {
574
    my($self, $date) = @_;
725
    my($self, $date) = @_;
575
    my $branchcode = $self->{branchcode};
726
    my $branchcode = $self->{branchcode};
Lines 591-596 sub isOpened { Link Here
591
    return $isOpened;
742
    return $isOpened;
592
}
743
}
593
744
745
=head2 is_holiday
746
747
    $calendar->is_holiday($date)
748
749
Returns whether C<$date> is a holiday or not
750
751
=cut
752
594
sub is_holiday {
753
sub is_holiday {
595
    my($self, $date) = @_;
754
    my($self, $date) = @_;
596
    my $branchcode = $self->{branchcode};
755
    my $branchcode = $self->{branchcode};
Lines 616-621 sub is_holiday { Link Here
616
    return $isHoliday;
775
    return $isHoliday;
617
}
776
}
618
777
778
=head2 copyHoliday
779
780
  $calendar->copyHoliday($from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber);
781
782
Copies a holiday's parameters from a range to a new range
783
C<$from_startDate> the source holiday's start date
784
C<$from_endDate> the source holiday's end date
785
C<$to_startDate> the destination holiday's start date
786
C<$to_endDate> the destination holiday's end date
787
C<$daysnumber> the number of days in the range.
788
789
Both ranges should have the same number of days in them.
790
791
=cut
792
619
sub copyHoliday {
793
sub copyHoliday {
620
    my ($self, $from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber) = @_;
794
    my ($self, $from_startDate, $from_endDate, $to_startDate, $to_endDate, $daysnumber) = @_;
621
    my $branchcode = $self->{branchcode};
795
    my $branchcode = $self->{branchcode};
Lines 664-670 sub copyHoliday { Link Here
664
        my $endDate = dt_from_string($from_endDate);
838
        my $endDate = dt_from_string($from_endDate);
665
        $to_startDate = $dtf->format_datetime($to_startDate);
839
        $to_startDate = $dtf->format_datetime($to_startDate);
666
        $to_endDate = $dtf->format_datetime($to_endDate);
840
        $to_endDate = $dtf->format_datetime($to_endDate);
667
        if($daysnumber ==7){
841
        if($daysnumber == 7){
668
            for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)){
842
            for (my $tempDate = $from_startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)){
669
                my $formatedDate = $dtf->format_datetime($tempDate);
843
                my $formatedDate = $dtf->format_datetime($tempDate);
670
                my $fromDate = $schema->resultset('DiscreteCalendar')->search(
844
                my $fromDate = $schema->resultset('DiscreteCalendar')->search(
Lines 740-745 sub copyHoliday { Link Here
740
    }
914
    }
741
}
915
}
742
916
917
=head2 days_between
918
919
   $cal->days_between( $start_date, $end_date )
920
921
Calculates the number of days the library is opened between C<$start_date> and C<$end_date>
922
923
=cut
924
743
sub days_between {
925
sub days_between {
744
    my ($self, $start_date, $end_date, ) = @_;
926
    my ($self, $start_date, $end_date, ) = @_;
745
    my $branchcode = $self->{branchcode};
927
    my $branchcode = $self->{branchcode};
Lines 769-774 sub days_between { Link Here
769
    return DateTime::Duration->new( days => $days_between->count());
951
    return DateTime::Duration->new( days => $days_between->count());
770
}
952
}
771
953
954
=head2 next_open_day
955
956
   $open_date = $self->next_open_day($base_date);
957
958
Returns a string representing the next day the library is open, starting from C<$base_date>
959
960
=cut
961
772
sub next_open_day {
962
sub next_open_day {
773
    my ( $self, $date ) = @_;
963
    my ( $self, $date ) = @_;
774
    my $branchcode = $self->{branchcode};
964
    my $branchcode = $self->{branchcode};
Lines 790-795 sub next_open_day { Link Here
790
    return dt_from_string( $rs->next()->date(), 'iso');
980
    return dt_from_string( $rs->next()->date(), 'iso');
791
}
981
}
792
982
983
=head2 prev_open_day
984
985
   $open_date = $self->prev_open_day($base_date);
986
987
Returns a string representing the closest previous day the library was open, starting from C<$base_date>
988
989
=cut
990
793
sub prev_open_day {
991
sub prev_open_day {
794
    my ( $self, $date ) = @_;
992
    my ( $self, $date ) = @_;
795
    my $branchcode = $self->{branchcode};
993
    my $branchcode = $self->{branchcode};
Lines 811-816 sub prev_open_day { Link Here
811
    return dt_from_string( $rs->next()->date(), 'iso');
1009
    return dt_from_string( $rs->next()->date(), 'iso');
812
}
1010
}
813
1011
1012
=head2 days_forward
1013
1014
    $fwrd_date = $calendar->days_forward($start, $count)
1015
1016
Returns the date C<$count> days in the future from C<$start>, ignoring days where the library is closed.
1017
1018
=cut
1019
814
sub days_forward {
1020
sub days_forward {
815
    my $self     = shift;
1021
    my $self     = shift;
816
    my $start_dt = shift;
1022
    my $start_dt = shift;
Lines 827-832 sub days_forward { Link Here
827
    return $base_dt;
1033
    return $base_dt;
828
}
1034
}
829
1035
1036
=head2 hours_between
1037
1038
    $hours = $calendar->hours_between($start_dt, $end_dt)
1039
1040
Returns the number of hours between C<$start_dt> and C<$end_dt>. This is the imprecise
1041
version, which simply calculates the number of day times 24. To take opening hours into account
1042
see C<open_hours_between>/
1043
1044
=cut
1045
830
sub hours_between {
1046
sub hours_between {
831
    my ($self, $start_dt, $end_dt) = @_;
1047
    my ($self, $start_dt, $end_dt) = @_;
832
    my $branchcode = $self->{branchcode};
1048
    my $branchcode = $self->{branchcode};
Lines 862-867 sub hours_between { Link Here
862
    return $duration;
1078
    return $duration;
863
}
1079
}
864
1080
1081
=head2 open_hours_between
1082
1083
  $hours = $calendar->open_hours_between($start_date, $end_date)
1084
1085
Returns the number of hours between C<$start_date> and C<$end_date>, taking into
1086
account the opening hours of the library.
1087
1088
=cut
1089
865
sub open_hours_between {
1090
sub open_hours_between {
866
    my ($self, $start_date, $end_date) = @_;
1091
    my ($self, $start_date, $end_date) = @_;
867
    my $branchcode = $self->{branchcode};
1092
    my $branchcode = $self->{branchcode};
Lines 919-924 sub open_hours_between { Link Here
919
1144
920
    return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours'));
1145
    return ($working_hours_between->next()->get_column('hours_between') - $not_used_hours->next()->get_column('not_used_hours'));
921
}
1146
}
1147
1148
=head2 addDate
1149
1150
  my $dt = $calendar->addDate($date, $dur, $unit)
1151
1152
C<$date> is a DateTime object representing the starting date of the interval.
1153
C<$offset> is a duration to add to it (DateTime::Duration objects are supported as legacy)
1154
C<$unit> is a string value 'days' or 'hours' toflag granularity of duration
1155
1156
=cut
1157
922
sub addDate {
1158
sub addDate {
923
    my ( $self, $startdate, $add_duration, $unit ) = @_;
1159
    my ( $self, $startdate, $add_duration, $unit ) = @_;
924
1160
Lines 943-948 sub addDate { Link Here
943
    return $dt;
1179
    return $dt;
944
}
1180
}
945
1181
1182
=head2 addHours
1183
1184
  $end = $calendar->addHours($start, $hours_duration, $return_by_hour)
1185
1186
Add C<$hours_duration> to C<$start> date.
1187
C<$return_by_hour> is an integer value representing the opening hour for the branch
1188
1189
=cut
1190
946
sub addHours {
1191
sub addHours {
947
    my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
1192
    my ( $self, $startdate, $hours_duration, $return_by_hour ) = @_;
948
    my $base_date = $startdate->clone();
1193
    my $base_date = $startdate->clone();
Lines 968-973 sub addHours { Link Here
968
    return $base_date;
1213
    return $base_date;
969
}
1214
}
970
1215
1216
=head2 addDays
1217
1218
  $date = $calendar->addDays($start, $duration)
1219
1220
Add C<$days_duration> to C<$start> date. If the calendar's days_mode is set
1221
to 'Calendar', it ignores closed days. Else if the calendar is set to 'Datedue'
1222
it calculates the date normally, and then pushes to result to the next open day.
1223
1224
=cut
1225
971
sub addDays {
1226
sub addDays {
972
    my ( $self, $startdate, $days_duration ) = @_;
1227
    my ( $self, $startdate, $days_duration ) = @_;
973
    my $base_date = $startdate->clone();
1228
    my $base_date = $startdate->clone();
Lines 1013-1016 sub addDays { Link Here
1013
    return $base_date;
1268
    return $base_date;
1014
}
1269
}
1015
1270
1016
1;
1271
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/discrete_calendar.tt (-1 / +1 lines)
Lines 155-161 Link Here
155
        }
155
        }
156
156
157
        /**
157
        /**
158
        * This function seperate a given date object a returns an array containing all needed information about the date.
158
        * This function separate a given date object a returns an array containing all needed information about the date.
159
        */
159
        */
160
        function getSeparetedDate(date){
160
        function getSeparetedDate(date){
161
            var mydate = new Array();
161
            var mydate = new Array();
(-)a/t/db_dependent/DiscreteCalendar.t (-3 / +3 lines)
Lines 46-56 $schema->resultset('DiscreteCalendar')->search({ Link Here
46
});
46
});
47
47
48
#Added entries for the branches.
48
#Added entries for the branches.
49
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch1);
49
Koha::DiscreteCalendar->add_new_branch('DFLT', $branch1);
50
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch2);
50
Koha::DiscreteCalendar->add_new_branch('DFLT', $branch2);
51
51
52
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1");
52
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1");
53
isnt($branch1,'', "Second branch  to do tests. BranchCode => $branch2");
53
isnt($branch2,'', "Second branch to do tests. BranchCode => $branch2");
54
54
55
#2 Calendars to make sure branches are treated separately.
55
#2 Calendars to make sure branches are treated separately.
56
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch1);
56
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch1);
(-)a/tools/discrete_calendar.pl (-3 / +2 lines)
Lines 41-47 my ($template, $loggedinuser, $cookie) Link Here
41
41
42
my $branch = $input->param('branch') || C4::Context->userenv->{'branch'};
42
my $branch = $input->param('branch') || C4::Context->userenv->{'branch'};
43
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch);
43
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch);
44
#alert the user that he is using the default calendar because he does not have a library set
44
#alert the user that they are using the default calendar because they do not have a library set
45
my $no_branch_selected = $calendar->{no_branch_selected};
45
my $no_branch_selected = $calendar->{no_branch_selected};
46
46
47
my $weekday = $input->param('day_of_week');
47
my $weekday = $input->param('day_of_week');
Lines 120-126 my @unique_holidays =$calendar->get_unique_holidays(); Link Here
120
#discrete_calendar floating holidays
120
#discrete_calendar floating holidays
121
my @float_holidays =$calendar->get_float_holidays();
121
my @float_holidays =$calendar->get_float_holidays();
122
#discrete_caledar need validation holidays
122
#discrete_caledar need validation holidays
123
my @need_validation_holidays =$calendar->get_need_valdation_holidays();
123
my @need_validation_holidays =$calendar->get_need_validation_holidays();
124
124
125
#Calendar maximum date
125
#Calendar maximum date
126
my $minDate = $calendar->getMinDate($branch);
126
my $minDate = $calendar->getMinDate($branch);
127
- 

Return to bug 17015