Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# |
4 |
# Script that fills the discrete_calendar table with dates, using the other date-related tables |
5 |
# |
6 |
use Modern::Perl; |
7 |
use DateTime; |
8 |
use DateTime::Format::Strptime; |
9 |
use Data::Dumper; |
10 |
use Getopt::Long; |
11 |
use C4::Context; |
12 |
|
13 |
# Options |
14 |
my $daysInFuture = 365; |
15 |
|
16 |
my $dbh = C4::Context->dbh; |
17 |
$dbh->{AutoCommit} = 0; |
18 |
$dbh->{RaiseError} = 1; |
19 |
|
20 |
my $currentDate = DateTime->today; |
21 |
|
22 |
# two years ago |
23 |
my $startDate = DateTime->new( |
24 |
day => $currentDate->day(), |
25 |
month => $currentDate->month(), |
26 |
year => $currentDate->year()-2, |
27 |
time_zone => C4::Context->tz() |
28 |
)->truncate( to => 'day' ); |
29 |
|
30 |
# a year into the future |
31 |
my $endDate = DateTime->new( |
32 |
day => $currentDate->day(), |
33 |
month => $currentDate->month(), |
34 |
year => $currentDate->year(), |
35 |
time_zone => C4::Context->tz() |
36 |
)->truncate( to => 'day' ); |
37 |
$endDate->add(days => $daysInFuture); |
38 |
|
39 |
#Added a default (standard) branch. |
40 |
my $add_default_branch = 'INSERT IGNORE INTO branches (branchname, branchcode) VALUES(?,?)'; |
41 |
my $add_Branch_Sth = $dbh->prepare($add_default_branch); |
42 |
$add_Branch_Sth->execute('Default', ''); |
43 |
# finds branches; |
44 |
my $selectBranchesSt = 'SELECT branchname, branchcode FROM branches'; |
45 |
my $selectBranchesSth = $dbh->prepare($selectBranchesSt); |
46 |
$selectBranchesSth->execute(); |
47 |
my @branches = (); |
48 |
while (my @row = $selectBranchesSth->fetchrow_array ) { |
49 |
print "[$row[1]] $row[0]\n"; |
50 |
push @branches, $row[1]; |
51 |
} |
52 |
|
53 |
# finds what days are closed for each branch |
54 |
my %repeatableHolidaysPerBranch = (); |
55 |
my %specialHolidaysPerBranch = (); |
56 |
my $selectWeeklySt; |
57 |
my $selectWeeklySth; |
58 |
|
59 |
foreach my $branch (@branches){ |
60 |
if ( !TableExists( 'repeatable_holidays' ) ) { |
61 |
$selectWeeklySt = 'SELECT weekday, title, day, month FROM repeatable_holidays WHERE branchcode = ?'; |
62 |
$selectWeeklySth = $dbh->prepare($selectWeeklySt); |
63 |
$selectWeeklySth->execute($branch); |
64 |
|
65 |
my @weeklyHolidays = (); |
66 |
|
67 |
while ( my ($weekDay, $title, $day, $month) = $selectWeeklySth->fetchrow_array ) { |
68 |
push @weeklyHolidays,{weekday => $weekDay, title => $title, day => $day, month => $month}; |
69 |
|
70 |
} |
71 |
|
72 |
$repeatableHolidaysPerBranch{$branch} = \@weeklyHolidays; |
73 |
} |
74 |
|
75 |
if ( !TableExists( 'repeatable_holidays' ) ) { |
76 |
my $selectSpecialHolidayDateSt = 'SELECT day,month,year,title FROM special_holidays WHERE branchcode = ? AND isexception = 0'; |
77 |
my $specialHolidayDatesSth = $dbh->prepare($selectSpecialHolidayDateSt); |
78 |
$specialHolidayDatesSth -> execute($branch); |
79 |
# Tranforms dates from specialHolidays table in DateTime for our new table |
80 |
my @specialHolidayDates = (); |
81 |
while ( my ($day, $month, $year, $title) = $specialHolidayDatesSth->fetchrow_array ) { |
82 |
|
83 |
my $specialHolidayDate = DateTime->new( |
84 |
day => $day, |
85 |
month => $month, |
86 |
year => $year, |
87 |
time_zone => C4::Context->tz() |
88 |
)->truncate( to => 'day' ); |
89 |
push @specialHolidayDates,{date=>$specialHolidayDate, title=> $title}; |
90 |
} |
91 |
|
92 |
$specialHolidaysPerBranch{$branch} = \@specialHolidayDates; |
93 |
} |
94 |
} |
95 |
|
96 |
# Fills table with dates and sets 'is_opened' according to the branch's weekly restrictions (repeatable_holidays) |
97 |
my $insertDateSt; |
98 |
my $insertDateSth; |
99 |
|
100 |
# Loop that does everything in the world |
101 |
for (my $tempDate = $startDate->clone(); $tempDate <= $endDate;$tempDate->add(days => 1)){ |
102 |
foreach my $branch (@branches){ |
103 |
my $dayOfWeek = $tempDate->day_of_week; |
104 |
# Representation fix |
105 |
# DateTime object dow (1-7) where Monday is 1 |
106 |
# Arrays are 0-based where 0 = Sunday, not 7. |
107 |
my $open_hour = "09:00:00"; |
108 |
my $close_hour = "17:00:00"; |
109 |
|
110 |
# Finds closed days |
111 |
my $is_opened =1; |
112 |
my $specialDescription = ""; |
113 |
my $holiday_type =''; |
114 |
$dayOfWeek = $tempDate->day_of_week % 7; |
115 |
foreach my $holidayWeekDay ( @{$repeatableHolidaysPerBranch{$branch}} ) { |
116 |
if (defined($holidayWeekDay) && $dayOfWeek == $holidayWeekDay->{weekday}) { |
117 |
$is_opened = 0; |
118 |
$specialDescription = $holidayWeekDay->{title}; |
119 |
$holiday_type = 'W'; |
120 |
} elsif ($holidayWeekDay->{day} && $holidayWeekDay->{month}) { |
121 |
my $date = DateTime->new( |
122 |
day => $holidayWeekDay->{day}, |
123 |
month => $holidayWeekDay->{month}, |
124 |
year => $tempDate->year(), |
125 |
time_zone => C4::Context->tz() |
126 |
)->truncate( to => 'day' ); |
127 |
|
128 |
if ($tempDate == $date) { |
129 |
$is_opened = 0; |
130 |
$specialDescription = $holidayWeekDay->{title}; |
131 |
$holiday_type = 'R'; |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
foreach my $specialDate (@{$specialHolidaysPerBranch{$branch}}){ |
137 |
if ($tempDate->datetime() eq $specialDate->{date}->datetime()) { |
138 |
$is_opened = 0; |
139 |
$specialDescription = $specialDate->{title}; |
140 |
$holiday_type = 'E'; |
141 |
} |
142 |
} |
143 |
#final insert statement |
144 |
|
145 |
$insertDateSt = 'INSERT IGNORE INTO discrete_calendar (date,branchcode,is_opened,holiday_type,note,open_hour,close_hour) VALUES (?,?,?,?,?,?,?)'; |
146 |
$insertDateSth = $dbh->prepare($insertDateSt); |
147 |
$insertDateSth->execute($tempDate,$branch,$is_opened,$holiday_type,$specialDescription,$open_hour,$close_hour); |
148 |
} |
149 |
} |
150 |
# If everything went well we commit to the database |
151 |
$dbh->commit(); |