Bug 12695: Remove CGI::scrolling_list from marc21_linking_section.pl
[koha.git] / serials / serials-edit.pl
blob2eeed1092390b7ff4114e4c48af08490467b657b
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.tt',
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
138 for my $d ( qw( publisheddate planneddate )){
139 if ( $serinfo->{$d} =~m/^00/ ) {
140 $serinfo->{$d} = q{};
142 else {
143 $serinfo->{$d} = format_date( $serinfo->{$d} );
146 $serinfo->{arriveddate}=$today->output('syspref');
148 $serinfo->{'editdisable'} = (
150 HasSubscriptionExpired( $serinfo->{subscriptionid} )
151 && $serinfo->{'status1'}
153 || $serinfo->{'cannotedit'}
155 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
156 push @serialdatalist, $serinfo;
157 $processedserialid{$serialid} = 1;
160 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
162 my @newserialloop;
163 my @subscriptionloop;
165 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
166 my %processedsubscriptionid;
167 foreach my $subscriptionid (@subscriptionids) {
169 #Do not process subscriptionid twice if it was already processed.
170 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
172 my $cell;
173 if ( $serialdatalist[0]->{'serialsadditems'} ) {
175 #Create New empty item
176 $cell =
177 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
178 '', GetSubscription($subscriptionid) );
179 $cell->{serialsadditems} = 1;
181 $cell->{'subscriptionid'} = $subscriptionid;
182 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
183 $cell->{'itemid'} = 'NNEW';
184 $cell->{'serialid'} = 'NEW';
185 $cell->{'issuesatonce'} = 1;
186 $cell->{arriveddate}=$today->output('syspref');
188 push @newserialloop, $cell;
189 push @subscriptionloop,
191 'subscriptionid' => $subscriptionid,
192 'abouttoexpire' => abouttoexpire($subscriptionid),
193 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
195 $processedsubscriptionid{$subscriptionid} = 1;
198 $template->param( newserialloop => \@newserialloop );
199 $template->param( subscriptions => \@subscriptionloop );
201 if ( $op and $op eq 'serialchangestatus' ) {
203 # Convert serialseqs to UTF-8 to prevent encoding problems
204 foreach my $seq (@serialseqs) {
205 utf8::decode($seq) unless utf8::is_utf8($seq);
208 my $newserial;
209 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
210 my ($plan_date, $pub_date);
212 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
213 $plan_date = format_date_in_iso( $planneddates[$i] );
215 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
216 $pub_date = format_date_in_iso( $publisheddates[$i] );
219 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
220 if ( $serialseqs[$i] ) {
222 #IF newserial was provided a name Then we have to create a newSerial
223 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
224 $newserial = NewIssue(
225 $serialseqs[$i],
226 $subscriptionids[0],
227 $serialdatalist[0]->{'biblionumber'},
228 $status[$i],
229 $plan_date,
230 $pub_date,
231 $notes[$i]
235 elsif ( $serialids[$i] ) {
236 ModSerialStatus(
237 $serialids[$i],
238 $serialseqs[$i],
239 $plan_date,
240 $pub_date,
241 $status[$i],
242 $notes[$i]
246 my @moditems = $query->param('moditem');
247 if ( scalar(@moditems) ) {
248 my @tags = $query->param('tag');
249 my @subfields = $query->param('subfield');
250 my @field_values = $query->param('field_value');
251 my @serials = $query->param('serial');
252 my @bibnums = $query->param('bibnum');
253 my @itemid = $query->param('itemid');
254 my @ind_tag = $query->param('ind_tag');
255 my @indicator = $query->param('indicator');
257 #Rebuilding ALL the data for items into a hash
258 # parting them on $itemid.
259 my %itemhash;
260 my $countdistinct;
261 my $range = scalar(@itemid);
262 for ( my $i = 0 ; $i < $range ; $i++ ) {
263 unless ( $itemhash{ $itemid[$i] } ) {
264 if ( $serials[$countdistinct]
265 && $serials[$countdistinct] ne "NEW" )
267 $itemhash{ $itemid[$i] }->{'serial'} =
268 $serials[$countdistinct];
270 else {
271 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
273 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
274 $countdistinct++;
276 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
277 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
278 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
279 $field_values[$i];
280 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
281 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
283 foreach my $item ( keys %itemhash ) {
285 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
286 my $index = -1;
287 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
288 if (
289 $itemhash{$item}->{serial} eq $serialids[$i]
290 || ( $itemhash{$item}->{serial} == $newserial
291 && $serialids[$i] eq 'NEW' )
293 $index = $i
296 if ( $index >= 0 && $status[$index] == 2 ) {
297 my $xml = TransformHtmlToXml(
298 $itemhash{$item}->{'tags'},
299 $itemhash{$item}->{'subfields'},
300 $itemhash{$item}->{'field_values'},
301 $itemhash{$item}->{'indicator'},
302 $itemhash{$item}->{'ind_tag'}
305 # warn $xml;
306 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
307 if ( $item =~ /^N/ ) {
309 #New Item
311 # if autoBarcode is set to 'incremental', calculate barcode...
312 my ( $barcodetagfield, $barcodetagsubfield ) =
313 GetMarcFromKohaField(
314 'items.barcode',
315 GetFrameworkCode(
316 $serialdatalist[0]->{'biblionumber'}
319 if ( C4::Context->preference('autoBarcode') eq
320 'incremental' )
322 if (
324 $bib_record->field($barcodetagfield)
325 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
329 my $sth_barcode = $dbh->prepare(
330 'select max(abs(barcode)) from items');
331 $sth_barcode->execute;
332 my ($newbarcode) = $sth_barcode->fetchrow;
334 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
335 $bib_record->field($barcodetagfield)
336 ->update( $barcodetagsubfield => ++$newbarcode );
340 # check for item barcode # being unique
341 my $exists;
342 if (
343 $bib_record->subfield(
344 $barcodetagfield, $barcodetagsubfield
348 $exists = GetItemnumberFromBarcode(
349 $bib_record->subfield(
350 $barcodetagfield, $barcodetagsubfield
355 # push @errors,"barcode_not_unique" if($exists);
356 # if barcode exists, don't create, but report The problem.
357 if ($exists) {
358 push @errors, 'barcode_not_unique';
359 push @errseq, { serialseq => $serialseqs[$index] };
361 else {
362 my ( $biblionumber, $bibitemnum, $itemnumber ) =
363 AddItemFromMarc( $bib_record,
364 $itemhash{$item}->{bibnum} );
365 AddItem2Serial( $itemhash{$item}->{serial},
366 $itemnumber );
369 else {
371 #modify item
372 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
373 ModItemFromMarc( $bib_record,
374 $itemhash{$item}->{'bibnum'}, $item );
381 if ( @errors ) {
382 $template->param( Errors => 1 );
383 if ( @errseq ) {
384 $template->param( barcode_not_unique => 1, errseq => \@errseq );
387 else {
388 my $redirect = 'serials-collection.pl?';
389 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
390 print $query->redirect($redirect);
393 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
394 my $locationlib;
395 foreach (@$location) {
396 $locationlib = $_->{'lib'} if $_->{'selected'};
398 my $default_bib_view = get_default_view();
400 $template->param(
401 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
402 callnumber => $serialdatalist[0]->{'callnumber'},
403 internalnotes => $serialdatalist[0]->{'internalnotes'},
404 bibliotitle => $biblio->{'title'},
405 biblionumber => $serialdatalist[0]->{'biblionumber'},
406 serialslist => \@serialdatalist,
407 default_bib_view => $default_bib_view,
408 location => $locationlib,
409 (uc(C4::Context->preference("marcflavour"))) => 1
412 output_html_with_http_headers $query, $cookie, $template->output;
414 sub get_default_view {
415 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
416 my %views = C4::Search::enabled_staff_search_views();
417 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
418 return 'ISBDdetail';
420 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
421 return 'MARCdetail';
423 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
424 return 'labeledMARCdetail';
426 return 'detail';