Bug 10080: (followup) change two last instances of the old syspref name
[koha.git] / serials / serials-edit.pl
blob66d3a4eedd3f6b186efe41091c081505ee193751
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 =head1 NAME
23 serials-edit.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
64 use strict;
65 use warnings;
66 use CGI;
67 use C4::Auth;
68 use C4::Dates qw/format_date format_date_in_iso/;
69 use C4::Biblio;
70 use C4::Items;
71 use C4::Koha;
72 use C4::Output;
73 use C4::Context;
74 use C4::Serials;
75 use C4::Search qw/enabled_staff_search_views/;
76 use List::MoreUtils qw/uniq/;
78 my $query = CGI->new();
79 my $dbh = C4::Context->dbh;
80 my @serialids = $query->param('serialid');
81 my @serialseqs = $query->param('serialseq');
82 my @planneddates = $query->param('planneddate');
83 my @publisheddates = $query->param('publisheddate');
84 my @status = $query->param('status');
85 my @notes = $query->param('notes');
86 my @subscriptionids = $query->param('subscriptionid');
87 my $op = $query->param('op');
88 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
89 @subscriptionids = split( /,/, $subscriptionids[0] );
91 my @errors;
92 my @errseq;
94 # If user comes from subscription details
95 unless (@serialids) {
96 my $serstatus = $query->param('serstatus');
97 if ($serstatus) {
98 foreach my $subscriptionid (@subscriptionids) {
99 my @tmpser = GetSerials2( $subscriptionid, $serstatus );
100 push @serialids, map { $_->{serialid} } @tmpser;
105 unless ( @serialids ) {
106 my $string =
107 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
108 $string =~ s/,$//;
110 print $query->redirect($string);
113 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115 template_name => 'serials/serials-edit.tmpl',
116 query => $query,
117 type => 'intranet',
118 authnotrequired => 0,
119 flagsrequired => { serials => 'receive_serials' },
120 debug => 1,
124 my @serialdatalist;
125 my %processedserialid;
127 my $today = C4::Dates->new();
128 foreach my $serialid (@serialids) {
130 #filtering serialid for duplication
131 #NEW serial should appear only once and are created afterwards
132 if ( $serialid
133 && $serialid =~ /^[0-9]+$/
134 && !$processedserialid{$serialid} )
136 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
137 for my $d ( qw( publisheddate planneddate )){
138 if ( $serinfo->{$d} =~m/^00/ ) {
139 $serinfo->{$d} = q{};
141 else {
142 $serinfo->{$d} = format_date( $serinfo->{$d} );
145 $serinfo->{arriveddate}=$today->output('syspref');
147 $serinfo->{'editdisable'} = (
149 HasSubscriptionExpired( $serinfo->{subscriptionid} )
150 && $serinfo->{'status1'}
152 || $serinfo->{'cannotedit'}
154 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
155 push @serialdatalist, $serinfo;
156 $processedserialid{$serialid} = 1;
159 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
161 my @newserialloop;
162 my @subscriptionloop;
164 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
165 my %processedsubscriptionid;
166 foreach my $subscriptionid (@subscriptionids) {
168 #Do not process subscriptionid twice if it was already processed.
169 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
171 my $cell;
172 if ( $serialdatalist[0]->{'serialsadditems'} ) {
174 #Create New empty item
175 $cell =
176 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
177 '', GetSubscription($subscriptionid) );
178 $cell->{serialsadditems} = 1;
180 $cell->{'subscriptionid'} = $subscriptionid;
181 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
182 $cell->{'itemid'} = 'NNEW';
183 $cell->{'serialid'} = 'NEW';
184 $cell->{'issuesatonce'} = 1;
185 $cell->{arriveddate}=$today->output('syspref');
187 push @newserialloop, $cell;
188 push @subscriptionloop,
190 'subscriptionid' => $subscriptionid,
191 'abouttoexpire' => abouttoexpire($subscriptionid),
192 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
194 $processedsubscriptionid{$subscriptionid} = 1;
197 $template->param( newserialloop => \@newserialloop );
198 $template->param( subscriptions => \@subscriptionloop );
200 if ( $op and $op eq 'serialchangestatus' ) {
202 # Convert serialseqs to UTF-8 to prevent encoding problems
203 foreach my $seq (@serialseqs) {
204 utf8::decode($seq) unless utf8::is_utf8($seq);
207 my $newserial;
208 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
209 my ($plan_date, $pub_date);
211 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
212 $plan_date = format_date_in_iso( $planneddates[$i] );
214 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
215 $pub_date = format_date_in_iso( $publisheddates[$i] );
218 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
219 if ( $serialseqs[$i] ) {
221 #IF newserial was provided a name Then we have to create a newSerial
222 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
223 $newserial = NewIssue(
224 $serialseqs[$i],
225 $subscriptionids[0],
226 $serialdatalist[0]->{'biblionumber'},
227 $status[$i],
228 $plan_date,
229 $pub_date,
230 $notes[$i]
234 elsif ( $serialids[$i] ) {
235 ModSerialStatus(
236 $serialids[$i],
237 $serialseqs[$i],
238 $plan_date,
239 $pub_date,
240 $status[$i],
241 $notes[$i]
245 my @moditems = $query->param('moditem');
246 if ( scalar(@moditems) ) {
247 my @tags = $query->param('tag');
248 my @subfields = $query->param('subfield');
249 my @field_values = $query->param('field_value');
250 my @serials = $query->param('serial');
251 my @bibnums = $query->param('bibnum');
252 my @itemid = $query->param('itemid');
253 my @ind_tag = $query->param('ind_tag');
254 my @indicator = $query->param('indicator');
256 #Rebuilding ALL the data for items into a hash
257 # parting them on $itemid.
258 my %itemhash;
259 my $countdistinct;
260 my $range = scalar(@itemid);
261 for ( my $i = 0 ; $i < $range ; $i++ ) {
262 unless ( $itemhash{ $itemid[$i] } ) {
263 if ( $serials[$countdistinct]
264 && $serials[$countdistinct] ne "NEW" )
266 $itemhash{ $itemid[$i] }->{'serial'} =
267 $serials[$countdistinct];
269 else {
270 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
272 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
273 $countdistinct++;
275 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
276 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
277 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
278 $field_values[$i];
279 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
280 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
282 foreach my $item ( keys %itemhash ) {
284 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
285 my $index = -1;
286 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
287 if (
288 $itemhash{$item}->{serial} eq $serialids[$i]
289 || ( $itemhash{$item}->{serial} == $newserial
290 && $serialids[$i] eq 'NEW' )
292 $index = $i
295 if ( $index >= 0 && $status[$index] == 2 ) {
296 my $xml = TransformHtmlToXml(
297 $itemhash{$item}->{'tags'},
298 $itemhash{$item}->{'subfields'},
299 $itemhash{$item}->{'field_values'},
300 $itemhash{$item}->{'indicator'},
301 $itemhash{$item}->{'ind_tag'}
304 # warn $xml;
305 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
306 if ( $item =~ /^N/ ) {
308 #New Item
310 # if autoBarcode is set to 'incremental', calculate barcode...
311 my ( $barcodetagfield, $barcodetagsubfield ) =
312 GetMarcFromKohaField(
313 'items.barcode',
314 GetFrameworkCode(
315 $serialdatalist[0]->{'biblionumber'}
318 if ( C4::Context->preference('autoBarcode') eq
319 'incremental' )
321 if ( !$bib_record->field($barcodetagfield)
322 ->subfield($barcodetagsubfield) )
324 my $sth_barcode = $dbh->prepare(
325 'select max(abs(barcode)) from items');
326 $sth_barcode->execute;
327 my ($newbarcode) = $sth_barcode->fetchrow;
329 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
330 $bib_record->field($barcodetagfield)
331 ->update( $barcodetagsubfield => ++$newbarcode );
335 # check for item barcode # being unique
336 my $exists;
337 if (
338 $bib_record->subfield(
339 $barcodetagfield, $barcodetagsubfield
343 $exists = GetItemnumberFromBarcode(
344 $bib_record->subfield(
345 $barcodetagfield, $barcodetagsubfield
350 # push @errors,"barcode_not_unique" if($exists);
351 # if barcode exists, don't create, but report The problem.
352 if ($exists) {
353 push @errors, 'barcode_not_unique';
354 push @errseq, { serialseq => $serialseqs[$index] };
356 else {
357 my ( $biblionumber, $bibitemnum, $itemnumber ) =
358 AddItemFromMarc( $bib_record,
359 $itemhash{$item}->{bibnum} );
360 AddItem2Serial( $itemhash{$item}->{serial},
361 $itemnumber );
364 else {
366 #modify item
367 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
368 ModItemFromMarc( $bib_record,
369 $itemhash{$item}->{'bibnum'}, $item );
376 if ( @errors ) {
377 $template->param( Errors => 1 );
378 if ( @errseq ) {
379 $template->param( barcode_not_unique => 1, errseq => \@errseq );
382 else {
383 my $redirect = 'serials-collection.pl?';
384 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
385 print $query->redirect($redirect);
388 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
389 my $locationlib;
390 foreach (@$location) {
391 $locationlib = $_->{'lib'} if $_->{'selected'};
393 my $default_bib_view = get_default_view();
395 $template->param(
396 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
397 callnumber => $serialdatalist[0]->{'callnumber'},
398 internalnotes => $serialdatalist[0]->{'internalnotes'},
399 bibliotitle => $biblio->{'title'},
400 biblionumber => $serialdatalist[0]->{'biblionumber'},
401 serialslist => \@serialdatalist,
402 default_bib_view => $default_bib_view,
403 location => $locationlib,
404 (uc(C4::Context->preference("marcflavour"))) => 1
407 output_html_with_http_headers $query, $cookie, $template->output;
409 sub get_default_view {
410 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
411 my %views = C4::Search::enabled_staff_search_views();
412 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
413 return 'ISBDdetail';
415 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
416 return 'MARCdetail';
418 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
419 return 'labeledMARCdetail';
421 return 'detail';