Bug 33036: REST API: Merge biblio records implements merging of records
[koha.git] / serials / routing-preview.pl
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 # Routing Preview.pl script used to view a routing list after creation
19 # lets one print out routing slip and create (in this instance) the hierarchy
20 # of reserves for the serial
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Koha;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Reserves qw( AddReserve ModReserve );
27 use C4::Context;
28 use C4::Serials qw( delroutingmember getroutinglist GetSubscription GetSerials check_routing );
29 use URI::Escape;
30
31 use Koha::Biblios;
32 use Koha::Libraries;
33 use Koha::Patrons;
34
35 my $query = CGI->new;
36 my $subscriptionid = $query->param('subscriptionid');
37 my $issue = $query->param('issue');
38 my $routingid;
39 my $op = $query->param('op') || q{};
40 my $dbh = C4::Context->dbh;
41
42 if($op eq 'cud-delete'){
43     delroutingmember($routingid,$subscriptionid);
44     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
45     $sth->execute($subscriptionid);
46     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
47 }
48
49 if($op eq 'cud-edit'){
50     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
51 }
52
53 my @routinglist = getroutinglist($subscriptionid);
54 my $subs = GetSubscription($subscriptionid);
55 my ($tmp ,@serials) = GetSerials($subscriptionid);
56 my ($template, $loggedinuser, $cookie);
57
58 my $library;
59 if($op eq 'cud-save_and_preview'){
60     # get biblio information....
61     my $biblionumber = $subs->{'bibnum'};
62
63     my $biblio = Koha::Biblios->find( $biblionumber );
64     my $items = $biblio->items->search_ordered;
65     my $branch =
66         $items->count
67       ? $items->next->holding_branch->branchcode
68       : $subs->{branchcode};
69     $library = Koha::Libraries->find($branch);
70
71         if (C4::Context->preference('RoutingListAddReserves')){
72                 # get existing reserves .....
73
74         my $holds = $biblio->current_holds;
75         my $count = $holds->count;
76         while ( my $hold = $holds->next ) {
77             $count-- if $hold->is_waiting;
78         }
79                 my $notes;
80                 my $title = $subs->{'bibliotitle'};
81         for my $routing ( @routinglist ) {
82             my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1');
83             $sth->execute($biblionumber,$routing->{borrowernumber});
84             my $reserve = $sth->fetchrow_hashref;
85
86             if($routing->{borrowernumber} == $reserve->{borrowernumber}){
87                 ModReserve({
88                     rank           => $routing->{ranking},
89                     biblionumber   => $biblionumber,
90                     borrowernumber => $routing->{borrowernumber},
91                     branchcode     => $branch
92                 });
93             } else {
94                 AddReserve(
95                     {
96                         branchcode     => $branch,
97                         borrowernumber => $routing->{borrowernumber},
98                         biblionumber   => $biblionumber,
99                         priority       => $routing->{ranking},
100                         notes          => $notes,
101                         title          => $title,
102                     }
103                 );
104         }
105     }
106         }
107     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid&print_routing_list_issue=" . $query->param('issue_escaped'));
108     exit;
109 } elsif ( $op eq 'print' ) {
110     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
111         {
112             template_name => "serials/routing-preview-slip.tt",
113             query         => $query,
114             type          => "intranet",
115             flagsrequired => { serials => '*' },
116         }
117     );
118 } else {
119     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
120         {
121             template_name => "serials/routing-preview.tt",
122             query         => $query,
123             type          => "intranet",
124             flagsrequired => { serials => '*' },
125         }
126     );
127 }
128
129 $template->param( libraryname => $library->branchname ) if $library;
130
131 my $memberloop = [];
132 for my $routing (@routinglist) {
133     my $member = Koha::Patrons->find( $routing->{borrowernumber} )->unblessed;
134     $member->{name}           = "$member->{firstname} $member->{surname}";
135     push @{$memberloop}, $member;
136 }
137
138 my $routingnotes = $serials[0]->{'routingnotes'};
139 $routingnotes =~ s/\n/\<br \/\>/g;
140
141 $template->param(
142     title => $subs->{'bibliotitle'},
143     issue => $issue,
144     issue_escaped => URI::Escape::uri_escape_utf8($issue),
145     subscriptionid => $subscriptionid,
146     memberloop => $memberloop,
147     routingnotes => $routingnotes,
148     hasRouting => check_routing($subscriptionid),
149     (uc(C4::Context->preference("marcflavour"))) => 1
150     );
151
152 output_html_with_http_headers $query, $cookie, $template->output;