Bug 10809: use branches template plugin in serials search
[koha.git] / serials / serials-recieve.pl
blobc14e4d075b32a7a1ead0206c4b3d543a584a6ccc
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 =head1 NAME
23 serials-recieve.pl
25 =head1 Parameters
27 =over 4
29 =item op
30 op can be :
31 * modsubscriptionhistory :to modify the subscription history
32 * serialchangestatus :to modify the status of this subscription
34 =item subscriptionid
36 =item user
38 =item histstartdate
40 =item enddate
42 =item recievedlist
44 =item missinglist
46 =item opacnote
48 =item librariannote
50 =item serialid
52 =item serialseq
54 =item planneddate
56 =item notes
58 =item status
60 =back
62 =cut
65 use strict;
66 use warnings;
67 use CGI;
68 use C4::Auth;
69 use C4::Dates qw/format_date format_date_in_iso/;
70 use C4::Biblio;
71 use C4::Items;
72 use C4::Koha;
73 use C4::Output;
74 use C4::Context;
75 use C4::Serials;
76 use C4::Branch; # GetBranches
78 my $query = new CGI;
79 my $op = $query->param('op') || q{};
80 my $dbh = C4::Context->dbh;
81 my $subscriptionid = $query->param('subscriptionid');
82 # my $auser = $query->param('user');
83 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
84 my $enddate = format_date_in_iso($query->param('enddate'));
85 my $recievedlist = $query->param('recievedlist');
86 my $missinglist = $query->param('missinglist');
87 my $opacnote = $query->param('opacnote');
88 my $librariannote = $query->param('librariannote');
89 my @serialids = $query->param('serialid');
90 my @serialseqs = $query->param('serialseq');
91 my @planneddates = $query->param('planneddate');
92 my @publisheddates = $query->param('publisheddate');
93 my @status = $query->param('status');
94 my @notes = $query->param('notes');
95 my @barcodes = $query->param('barcode');
96 my @itemcallnumbers = $query->param('itemcallnumber');
97 my @locations = $query->param('location');
98 my @itemstatus = $query->param('itemstatus');
99 my @homebranches = $query->param('branch');
100 my $hassubscriptionexpired = HasSubscriptionExpired($subscriptionid);
101 my $abouttoexpire = abouttoexpire($subscriptionid);
103 my $subscription=GetSubscription($subscriptionid);
106 my $auser = $subscription->{'librarian'}; # bob
107 my $routing = check_routing($subscriptionid); # to see if routing list exists
108 my $manualdate ='';
109 my $manualissue ='';
110 my $manualstatus =0;
111 my $manualid ='';
112 if ($op eq 'found'){
113 $manualdate = $query->param('planneddate');
114 $manualissue = $query->param('missingissue');
115 $manualstatus = 1;
116 my $sth = $dbh->prepare("select serialid from serial where subscriptionid = ? AND serialseq = ? AND planneddate = ?");
117 $sth->execute($subscriptionid,$manualissue,format_date_in_iso($manualdate));
118 $manualid = $sth->fetchrow;
120 if ($op eq 'modsubscriptionhistory') {
121 ModSubscriptionHistory($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
124 # change status except, if subscription has expired, for the "waited" issue.
125 if ($op eq 'serialchangestatus') {
126 my $sth = $dbh->prepare("select status from serial where serialid=?");
127 for (my $i=0;$i<=$#serialids;$i++) {
128 $sth->execute($serialids[$i]);
130 my ($oldstatus) = $sth->fetchrow;
131 if ($serialids[$i]) {
132 ModSerialStatus($serialids[$i],$serialseqs[$i],format_date_in_iso($planneddates[$i]),format_date_in_iso($publisheddates[$i]),$status[$i],$notes[$i]) unless ($hassubscriptionexpired && $oldstatus == 1);
133 if (($status[$i]==2) && C4::Context->preference("serialsadditems")){
134 my %info;
135 $info{branch}=$homebranches[$i];
136 $info{barcode}=$barcodes[$i];
137 $info{itemcallnumber}=$itemcallnumbers[$i];
138 $info{location}=$locations[$i];
139 $info{status}=$itemstatus[$i];
140 $info{notes}=$serialseqs[$i]." (".$planneddates[$i].")";
141 my ($status2, @errors)= ItemizeSerials($serialids[$i],\%info);
142 my $sth2 = $dbh->prepare("UPDATE subscriptionhistory SET lastbranch = ? WHERE subscriptionid = ?");
143 $sth2->execute($homebranches[$i],$subscriptionid);
144 $sth2->finish;
145 # remove from missing list if item being checked in is on it
146 if ($status2 ==1){
147 removeMissingIssue($serialseqs[$i],$subscriptionid);
150 } else {
151 # add a special issue
152 if ($serialseqs[$i]) {
153 NewIssue($serialseqs[$i],$subscriptionid,$subscription->{biblionumber},$status[$i] ,format_date_in_iso($publisheddates[$i]),format_date_in_iso($planneddates[$i]));
155 if (($status[$i]==2) && C4::Context->preference("serialsadditems") && !hassubscriptionexpired($subscriptionid)){
156 my %info;
157 $info{branch}=$homebranches[$i];
158 $info{barcode}=$barcodes[$i];
159 $info{itemcallnumber}=$itemcallnumbers[$i];
160 $info{location}=$locations[$i];
161 $info{status}=$itemstatus[$i];
162 $info{notes}=$serialseqs[$i]." (".$planneddates[$i].")";
163 my ($status2, @errors)= ItemizeSerials($serialids[$i],\%info);
164 my $sth2 = $dbh->prepare("UPDATE subscriptionhistory SET lastbranch = ? WHERE subscriptionid = ?");
165 $sth2->execute($homebranches[$i],$subscriptionid);
166 $sth2->finish;
167 # remove from missing list if item being checked in is on it
168 if ($status2 ==1){
169 removeMissingIssue($serialseqs[$i],$subscriptionid);
176 my ($template, $loggedinuser, $cookie)
177 = get_template_and_user({template_name => "serials/serials-recieve.tmpl",
178 query => $query,
179 type => "intranet",
180 authnotrequired => 0,
181 flagsrequired => {serials => 1},
182 debug => 1,
185 my $subs = &GetSubscription($subscriptionid);
186 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
187 my $count = @serialslist;
188 for(my $i=0;$i<$count;$i++){
189 #warn "la : $i";
190 $serialslist[$i]->{'callnumber'} = $subscription->{'callnumber'};
191 my $temp = rand(10000000);
192 $serialslist[$i]->{'barcode'} = "TEMP" . sprintf("%.0f",$temp);
195 my $solhistory = GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
197 $subs = &GetSubscription($subscriptionid);
198 ($totalissues,@serialslist) = GetSerials($subscriptionid);
200 if (C4::Context->preference("serialsadditems")){
201 my $fwk=GetFrameworkCode($subscription->{biblionumber});
203 my $branches = GetBranches;
204 my @branchloop;
205 foreach my $thisbranch (keys %$branches) {
206 my $selected = 0;
207 if($thisbranch eq $solhistory->{'lastbranch'}){
208 $selected = 1;
210 my %row =(value => $thisbranch,
211 branchname => $branches->{$thisbranch}->{'branchname'},
212 selected => $selected,
214 push @branchloop, \%row;
216 my $itemstatushash = GetItemStatus($fwk);
217 my @itemstatusloop;
218 my $itemstatusloopcount=0;
219 foreach my $thisitemstatus (keys %$itemstatushash) {
220 my %row =(itemval => $thisitemstatus,
221 itemlib => $itemstatushash->{$thisitemstatus},
223 # warn "".$row{'itemval'}.", ". $row{"itemlib"};
224 $itemstatusloopcount++;
225 push @itemstatusloop, \%row;
227 my $itemlocationhash = GetItemLocation($fwk);
228 my @itemlocationloop;
229 foreach my $thisitemlocation (keys %$itemlocationhash) {
230 my %row =(value => $thisitemlocation,
231 itemlocationname => $itemlocationhash->{$thisitemlocation},
233 push @itemlocationloop, \%row;
236 my $choice = ($itemstatusloopcount == 1) ? 1 : 0;
237 foreach my $data (@serialslist){
238 $data->{"itemstatusloop"} = (scalar(@itemstatusloop )) ? \@itemstatusloop : [];
239 $data->{"itemlocationloop"} = (scalar(@itemlocationloop)) ? \@itemlocationloop : [];
240 $data->{"branchloop"} = \@branchloop ;
242 # warn "Choice: $choice";
243 $template->param(choice => $choice);
244 $template->param(serialadditems =>C4::Context->preference("serialsadditems"),
245 branchloop => \@branchloop,
247 $template->param( itemstatus=>1, itemstatusloop=>\@itemstatusloop ) if (scalar(@itemstatusloop ));
248 $template->param(itemlocation=>1,itemlocationloop=>\@itemlocationloop) if (scalar(@itemlocationloop));
249 } else {
250 $template->param(branchloop=>[],itemstatusloop=>[],itemlocationloop=>[]) ;
253 $solhistory = GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
255 $template->param(
256 user => $auser,
257 serialslist => \@serialslist,
258 count => $count,
259 biblionumber => $subscription->{biblionumber},
260 histstartdate => format_date($solhistory->{'histstartdate'}),
261 enddate => format_date($solhistory->{'enddate'}),
262 recievedlist => $solhistory->{'recievedlist'},
263 missinglist => $solhistory->{'missinglist'},
264 opacnote => $solhistory->{'opacnote'},
265 librariannote => $solhistory->{'librariannote'},
266 subscriptionid => $subscriptionid,
267 bibliotitle => $subs->{bibliotitle},
268 biblionumber => $subs->{biblionumber},
269 hassubscriptionexpired =>$hassubscriptionexpired,
270 abouttoexpire =>$abouttoexpire,
271 routing => $routing,
272 missingseq => $manualissue,
273 frommissing => $manualstatus,
274 missingdate => $manualdate,
275 missingid => $manualid,
276 (uc(C4::Context->preference("marcflavour"))) => 1
278 output_html_with_http_headers $query, $cookie, $template->output;