Bug 17261: (followup) Rebase against 17189 and 11921
[koha.git] / serials / serials-edit.pl
blob2bff94b66e203ad1b5b25e1df03e044083b43350
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 qw ( -utf8 );
67 use Encode qw( decode is_utf8 );
68 use C4::Auth;
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 Koha::DateUtils;
77 use Koha::Serial::Items;
79 use List::MoreUtils qw/uniq/;
81 my $query = CGI->new();
82 my $dbh = C4::Context->dbh;
83 my @serialids = $query->multi_param('serialid');
84 my @serialseqs = $query->multi_param('serialseq');
85 my @planneddates = $query->multi_param('planneddate');
86 my @publisheddates = $query->multi_param('publisheddate');
87 my @publisheddatetexts = $query->multi_param('publisheddatetext');
88 my @status = $query->multi_param('status');
89 my @notes = $query->multi_param('notes');
90 my @subscriptionids = $query->multi_param('subscriptionid');
91 my $op = $query->param('op');
92 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
93 @subscriptionids = split( /,/, $subscriptionids[0] );
95 my @errors;
96 my @errseq;
98 # If user comes from subscription details
99 unless (@serialids) {
100 my $serstatus = $query->param('serstatus');
101 my @statuses = split ',', $serstatus;
102 if ($serstatus) {
103 foreach my $subscriptionid (@subscriptionids) {
104 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
105 push @serialids, map { $_->{serialid} } @tmpser;
110 unless ( @serialids ) {
111 my $string =
112 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
113 $string =~ s/,$//;
115 print $query->redirect($string);
118 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
120 template_name => 'serials/serials-edit.tt',
121 query => $query,
122 type => 'intranet',
123 authnotrequired => 0,
124 flagsrequired => { serials => 'receive_serials' },
125 debug => 1,
129 my @serialdatalist;
130 my %processedserialid;
132 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
134 foreach my $serialid (@serialids) {
136 #filtering serialid for duplication
137 #NEW serial should appear only once and are created afterwards
138 if ( $serialid
139 && $serialid =~ /^[0-9]+$/
140 && !$processedserialid{$serialid} )
142 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
144 for my $d ( qw( publisheddate planneddate )){
145 if ( $serinfo->{$d} =~m/^00/ ) {
146 $serinfo->{$d} = q{};
148 else {
149 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
152 $serinfo->{arriveddate} = $today;
154 $serinfo->{'editdisable'} = (
156 HasSubscriptionExpired( $serinfo->{subscriptionid} )
157 && $serinfo->{'status1'}
159 || $serinfo->{'cannotedit'}
161 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
162 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
163 push @serialdatalist, $serinfo;
164 $processedserialid{$serialid} = 1;
167 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
169 my @newserialloop;
170 my @subscriptionloop;
172 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
173 my %processedsubscriptionid;
174 foreach my $subscriptionid (@subscriptionids) {
176 #Do not process subscriptionid twice if it was already processed.
177 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
179 my $cell;
180 if ( $serialdatalist[0]->{'serialsadditems'} ) {
182 #Create New empty item
183 $cell =
184 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
185 '', GetSubscription($subscriptionid) );
186 $cell->{serialsadditems} = 1;
188 $cell->{'subscriptionid'} = $subscriptionid;
189 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
190 $cell->{'itemid'} = 'NNEW';
191 $cell->{'serialid'} = 'NEW';
192 $cell->{'issuesatonce'} = 1;
193 $cell->{arriveddate} = $today;
195 push @newserialloop, $cell;
196 push @subscriptionloop,
198 'subscriptionid' => $subscriptionid,
199 'abouttoexpire' => abouttoexpire($subscriptionid),
200 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
202 $processedsubscriptionid{$subscriptionid} = 1;
205 $template->param( newserialloop => \@newserialloop );
206 $template->param( subscriptions => \@subscriptionloop );
208 if ( $op and $op eq 'serialchangestatus' ) {
210 # Convert serialseqs to UTF-8 to prevent encoding problems
211 foreach my $seq (@serialseqs) {
212 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
215 my $newserial;
216 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
217 my ($plan_date, $pub_date);
219 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
220 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
222 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
223 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
226 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
227 if ( $serialseqs[$i] ) {
229 #IF newserial was provided a name Then we have to create a newSerial
230 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
231 $newserial = NewIssue(
232 $serialseqs[$i],
233 $subscriptionids[0],
234 $serialdatalist[0]->{'biblionumber'},
235 $status[$i],
236 $plan_date,
237 $pub_date,
238 $publisheddatetexts[$i],
239 $notes[$i]
243 elsif ( $serialids[$i] ) {
244 ModSerialStatus(
245 $serialids[$i],
246 $serialseqs[$i],
247 $plan_date,
248 $pub_date,
249 $publisheddatetexts[$i],
250 $status[$i],
251 $notes[$i]
254 my $makePreviousSerialAvailable = C4::Context->preference('makePreviousSerialAvailable');
255 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
256 # We already have created the new expected serial at this point, so we get the second previous serial
257 my $previous = GetPreviousSerialid($subscriptionids[$i]);
258 if ($previous) {
260 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
261 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
262 if ($itemnumber) {
264 # Getting the itemtype to set from the database
265 my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
267 # Changing the status to "available" and the itemtype according to the previousitemtype db field
268 ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
274 my @moditems = $query->multi_param('moditem');
275 if ( scalar(@moditems) ) {
276 my @tags = $query->multi_param('tag');
277 my @subfields = $query->multi_param('subfield');
278 my @field_values = $query->multi_param('field_value');
279 my @serials = $query->multi_param('serial');
280 my @bibnums = $query->multi_param('bibnum');
281 my @itemid = $query->multi_param('itemid');
282 my @ind_tag = $query->multi_param('ind_tag');
283 my @indicator = $query->multi_param('indicator');
285 #Rebuilding ALL the data for items into a hash
286 # parting them on $itemid.
287 my %itemhash;
288 my $countdistinct;
289 my $range = scalar(@itemid);
290 for ( my $i = 0 ; $i < $range ; $i++ ) {
291 unless ( $itemhash{ $itemid[$i] } ) {
292 if ( $serials[$countdistinct]
293 && $serials[$countdistinct] ne "NEW" )
295 $itemhash{ $itemid[$i] }->{'serial'} =
296 $serials[$countdistinct];
298 else {
299 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
301 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
302 $countdistinct++;
304 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
305 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
306 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
307 $field_values[$i];
308 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
309 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
311 foreach my $item ( keys %itemhash ) {
313 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
314 my $index = -1;
315 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
316 if (
317 $itemhash{$item}->{serial} eq $serialids[$i]
318 || ( $itemhash{$item}->{serial} == $newserial
319 && $serialids[$i] eq 'NEW' )
321 $index = $i
324 if ( $index >= 0 && $status[$index] == 2 ) {
325 my $xml = TransformHtmlToXml(
326 $itemhash{$item}->{'tags'},
327 $itemhash{$item}->{'subfields'},
328 $itemhash{$item}->{'field_values'},
329 $itemhash{$item}->{'indicator'},
330 $itemhash{$item}->{'ind_tag'}
333 # warn $xml;
334 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
335 if ( $item =~ /^N/ ) {
337 #New Item
339 # if autoBarcode is set to 'incremental', calculate barcode...
340 my ( $barcodetagfield, $barcodetagsubfield ) =
341 GetMarcFromKohaField(
342 'items.barcode',
343 GetFrameworkCode(
344 $serialdatalist[0]->{'biblionumber'}
347 if ( C4::Context->preference('autoBarcode') eq
348 'incremental' )
350 if (
352 $bib_record->field($barcodetagfield)
353 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
357 my $sth_barcode = $dbh->prepare(
358 'select max(abs(barcode)) from items');
359 $sth_barcode->execute;
360 my ($newbarcode) = $sth_barcode->fetchrow;
362 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
363 $bib_record->field($barcodetagfield)
364 ->update( $barcodetagsubfield => ++$newbarcode );
368 # check for item barcode # being unique
369 my $exists;
370 if (
371 $bib_record->subfield(
372 $barcodetagfield, $barcodetagsubfield
376 $exists = GetItemnumberFromBarcode(
377 $bib_record->subfield(
378 $barcodetagfield, $barcodetagsubfield
383 # push @errors,"barcode_not_unique" if($exists);
384 # if barcode exists, don't create, but report The problem.
385 if ($exists) {
386 push @errors, 'barcode_not_unique';
387 push @errseq, { serialseq => $serialseqs[$index] };
389 else {
390 my ( $biblionumber, $bibitemnum, $itemnumber ) =
391 AddItemFromMarc( $bib_record,
392 $itemhash{$item}->{bibnum} );
393 AddItem2Serial( $itemhash{$item}->{serial},
394 $itemnumber );
397 else {
399 #modify item
400 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
401 ModItemFromMarc( $bib_record,
402 $itemhash{$item}->{'bibnum'}, $item );
409 if ( @errors ) {
410 $template->param( Errors => 1 );
411 if ( @errseq ) {
412 $template->param( barcode_not_unique => 1, errseq => \@errseq );
415 else {
416 my $redirect = 'serials-collection.pl?';
417 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
418 print $query->redirect($redirect);
421 my $location = $serialdatalist[0]->{'location'};
422 my $default_bib_view = get_default_view();
424 $template->param(
425 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
426 callnumber => $serialdatalist[0]->{'callnumber'},
427 internalnotes => $serialdatalist[0]->{'internalnotes'},
428 bibliotitle => $biblio->{'title'},
429 biblionumber => $serialdatalist[0]->{'biblionumber'},
430 serialslist => \@serialdatalist,
431 default_bib_view => $default_bib_view,
432 location => $location,
433 (uc(C4::Context->preference("marcflavour"))) => 1
436 output_html_with_http_headers $query, $cookie, $template->output;
438 sub get_default_view {
439 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
440 my %views = C4::Search::enabled_staff_search_views();
441 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
442 return 'ISBDdetail';
444 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
445 return 'MARCdetail';
447 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
448 return 'labeledMARCdetail';
450 return 'detail';