Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use C4::Members (); |
21 |
use C4::Auth qw( get_template_and_user ); |
22 |
use C4::Output qw( output_html_with_http_headers ); |
23 |
|
24 |
use Koha::Biblios (); |
25 |
use Koha::Booking (); |
26 |
use Koha::Bookings (); |
27 |
use Koha::DateUtils qw(dt_from_string); |
28 |
use Koha::Patrons (); |
29 |
|
30 |
use CGI qw( -utf8 ); |
31 |
use Carp qw( croak ); |
32 |
use Digest::SHA qw( sha1_base64 ); |
33 |
use Time::HiRes qw( time ); |
34 |
|
35 |
my $query = CGI->new; |
36 |
|
37 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
38 |
{ |
39 |
template_name => 'opac-bookings.tt', |
40 |
query => $query, |
41 |
type => 'opac', |
42 |
} |
43 |
); |
44 |
|
45 |
my $patron = Koha::Patrons->find($borrowernumber); |
46 |
|
47 |
my $op = $query->param('op') // 'list'; |
48 |
if ( $op eq 'list' ) { |
49 |
my $bookings = Koha::Bookings->search( { patron_id => $patron->borrowernumber } )->filter_by_active; |
50 |
my $hash = sha1_base64( join q{}, time, rand ); |
51 |
|
52 |
my $biblio; |
53 |
my $biblio_id = $query->param('biblio_id'); |
54 |
if ($biblio_id) { |
55 |
$biblio = Koha::Biblios->find($biblio_id); |
56 |
} |
57 |
|
58 |
$template->param( |
59 |
op => 'list', BOOKINGS => $bookings, |
60 |
BOOKING => { booking_id => $hash }, |
61 |
biblio => $biblio, |
62 |
); |
63 |
} |
64 |
|
65 |
if ( $op eq 'cud-add' ) { |
66 |
my $booking = Koha::Booking->new( |
67 |
{ |
68 |
patron_id => $patron->borrowernumber, |
69 |
biblio_id => $query->param('biblio_id'), |
70 |
item_id => $query->param('item_id'), |
71 |
pickup_library_id => $query->param('pickup_library_id'), |
72 |
start_date => dt_from_string( $query->param('period') ), |
73 |
end_date => dt_from_string( $query->param('period') )->add( days => 7 )->truncate( to => 'day' ), |
74 |
} |
75 |
)->store; |
76 |
|
77 |
print $query->redirect('/cgi-bin/koha/opac-bookings.pl?op=list') or croak; |
78 |
} |
79 |
|
80 |
if ( $op eq 'cud-cancel' ) { |
81 |
my $booking_id = $query->param('booking_id'); |
82 |
my $booking = Koha::Bookings->find($booking_id); |
83 |
if ( !$booking ) { |
84 |
print $query->redirect('/cgi-bin/koha/errors/404.pl') or croak; |
85 |
exit; |
86 |
} |
87 |
|
88 |
if ( $booking->patron_id ne $patron->borrowernumber ) { |
89 |
print $query->redirect('/cgi-bin/koha/errors/403.pl') or croak; |
90 |
exit; |
91 |
} |
92 |
|
93 |
my $is_deleted = $booking->delete; |
94 |
if ( !$is_deleted ) { |
95 |
print $query->redirect('/cgi-bin/koha/errors/500.pl') or croak; |
96 |
exit; |
97 |
} |
98 |
|
99 |
print $query->redirect('/cgi-bin/koha/opac-user.pl?tab=opac-user-bookings') or croak; |
100 |
} |
101 |
|
102 |
if ( $op eq 'cud-change_pickup_location' ) { |
103 |
my $booking_id = $query->param('booking_id'); |
104 |
my $new_pickup_location = $query->param('new_pickup_location'); |
105 |
my $booking = Koha::Bookings->find($booking_id); |
106 |
|
107 |
if ( !$booking ) { |
108 |
print $query->redirect('/cgi-bin/koha/errors/404.pl') or croak; |
109 |
exit; |
110 |
} |
111 |
|
112 |
if ( $booking->patron_id ne $patron->borrowernumber ) { |
113 |
print $query->redirect('/cgi-bin/koha/errors/403.pl') or croak; |
114 |
exit; |
115 |
} |
116 |
|
117 |
my $is_updated = $booking->update( { pickup_library_id => $new_pickup_location } ); |
118 |
if ( !$is_updated ) { |
119 |
print $query->redirect('/cgi-bin/koha/errors/500.pl') or croak; |
120 |
exit; |
121 |
} |
122 |
|
123 |
print $query->redirect('/cgi-bin/koha/opac-user.pl?tab=opac-user-bookings') or croak; |
124 |
} |
125 |
|
126 |
$template->param( |
127 |
bookingsview => 1, |
128 |
); |
129 |
|
130 |
output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 }; |