Bug 12598: New misc/import_borrowers.pl command line tool
[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 heirarchy
20 # of reserves for the serial
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Koha;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Acquisition;
27 use C4::Reserves;
28 use C4::Circulation;
29 use C4::Context;
30 use C4::Members;
31 use C4::Biblio;
32 use C4::Items;
33 use C4::Serials;
34 use URI::Escape;
35
36 use Koha::Biblios;
37 use Koha::Libraries;
38 use Koha::Patrons;
39
40 my $query = new CGI;
41 my $subscriptionid = $query->param('subscriptionid');
42 my $issue = $query->param('issue');
43 my $routingid;
44 my $ok = $query->param('ok');
45 my $edit = $query->param('edit');
46 my $delete = $query->param('delete');
47 my $dbh = C4::Context->dbh;
48
49 if($delete){
50     delroutingmember($routingid,$subscriptionid);
51     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
52     $sth->execute($subscriptionid);
53     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
54 }
55
56 if($edit){
57     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
58 }
59
60 my @routinglist = getroutinglist($subscriptionid);
61 my $subs = GetSubscription($subscriptionid);
62 my ($tmp ,@serials) = GetSerials($subscriptionid);
63 my ($template, $loggedinuser, $cookie);
64
65 my $library;
66 if($ok){
67     # get biblio information....
68     my $biblionumber = $subs->{'bibnum'};
69     my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblionumber);
70     my @itemresults = GetItemsInfo( $biblionumber );
71     my $branch = @itemresults ? $itemresults[0]->{'holdingbranch'} : $subs->{branchcode};
72     $library = Koha::Libraries->find($branch);
73
74         if (C4::Context->preference('RoutingListAddReserves')){
75                 # get existing reserves .....
76
77         my $biblio = Koha::Biblios->find( $biblionumber );
78         my $holds = $biblio->current_holds;
79         my $count = $holds->count;
80         while ( my $hold = $holds->next ) {
81             $count-- if $hold->is_waiting;
82         }
83                 my $notes;
84                 my $title = $subs->{'bibliotitle'};
85         for my $routing ( @routinglist ) {
86             my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1');
87             $sth->execute($biblionumber,$routing->{borrowernumber});
88             my $reserve = $sth->fetchrow_hashref;
89
90             if($routing->{borrowernumber} == $reserve->{borrowernumber}){
91                 ModReserve({
92                     rank           => $routing->{ranking},
93                     biblionumber   => $biblionumber,
94                     borrowernumber => $routing->{borrowernumber},
95                     branchcode     => $branch
96                 });
97             } else {
98                 AddReserve($branch,$routing->{borrowernumber},$biblionumber,\@bibitems,$routing->{ranking}, undef, undef, $notes,$title);
99         }
100     }
101         }
102
103     ($template, $loggedinuser, $cookie)
104 = get_template_and_user({template_name => "serials/routing-preview-slip.tt",
105                                 query => $query,
106                                 type => "intranet",
107                                 authnotrequired => 0,
108                                 flagsrequired => {serials => '*'},
109                                 debug => 1,
110                                 });
111 } else {
112     ($template, $loggedinuser, $cookie)
113 = get_template_and_user({template_name => "serials/routing-preview.tt",
114                                 query => $query,
115                                 type => "intranet",
116                                 authnotrequired => 0,
117                                 flagsrequired => {serials => '*'},
118                                 debug => 1,
119                                 });
120 }
121
122 $template->param( libraryname => $library->branchname ) if $library;
123
124 my $memberloop = [];
125 for my $routing (@routinglist) {
126     my $member = Koha::Patrons->find( $routing->{borrowernumber} )->unblessed;
127     $member->{name}           = "$member->{firstname} $member->{surname}";
128     push @{$memberloop}, $member;
129 }
130
131 my $routingnotes = $serials[0]->{'routingnotes'};
132 $routingnotes =~ s/\n/\<br \/\>/g;
133
134 $template->param(
135     title => $subs->{'bibliotitle'},
136     issue => $issue,
137     issue_escaped => URI::Escape::uri_escape_utf8($issue),
138     subscriptionid => $subscriptionid,
139     memberloop => $memberloop,
140     routingnotes => $routingnotes,
141     generalroutingnote => C4::Context->preference('RoutingListNote'),
142     hasRouting => check_routing($subscriptionid),
143     (uc(C4::Context->preference("marcflavour"))) => 1
144     );
145
146 output_html_with_http_headers $query, $cookie, $template->output;