Bug 23712: Silence warns from Koha/Illrequest/Logger.pm
[koha.git] / serials / serials-edit.pl
blob749e126216a096c9c5d5a6cf8cb06a5e8e756090
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 Modern::Perl;
65 use CGI qw ( -utf8 );
66 use Encode qw( decode is_utf8 );
67 use C4::Auth;
68 use C4::Biblio;
69 use C4::Items;
70 use C4::Koha;
71 use C4::Output;
72 use C4::Context;
73 use C4::Serials;
74 use C4::Search qw/enabled_staff_search_views/;
76 use Koha::DateUtils;
77 use Koha::Items;
78 use Koha::Serial::Items;
80 use List::MoreUtils qw/uniq/;
82 my $query = CGI->new();
83 my $dbh = C4::Context->dbh;
84 my @serialids = $query->multi_param('serialid');
85 my @serialseqs = $query->multi_param('serialseq');
86 my @planneddates = $query->multi_param('planneddate');
87 my @publisheddates = $query->multi_param('publisheddate');
88 my @publisheddatetexts = $query->multi_param('publisheddatetext');
89 my @status = $query->multi_param('status');
90 my @notes = $query->multi_param('notes');
91 my @subscriptionids = $query->multi_param('subscriptionid');
92 my $op = $query->param('op');
93 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
94 @subscriptionids = split( /,/, $subscriptionids[0] );
96 my @errors;
97 my @errseq;
99 # If user comes from subscription details
100 unless (@serialids) {
101 my $serstatus = $query->param('serstatus');
102 my @statuses = split ',', $serstatus;
103 if ($serstatus) {
104 foreach my $subscriptionid (@subscriptionids) {
105 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
106 push @serialids, map { $_->{serialid} } @tmpser;
111 unless ( @serialids ) {
112 my $string =
113 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
114 $string =~ s/,$//;
116 print $query->redirect($string);
117 exit;
120 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
122 template_name => 'serials/serials-edit.tt',
123 query => $query,
124 type => 'intranet',
125 authnotrequired => 0,
126 flagsrequired => { serials => 'receive_serials' },
127 debug => 1,
131 my @serialdatalist;
132 my %processedserialid;
134 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
136 foreach my $serialid (@serialids) {
138 #filtering serialid for duplication
139 #NEW serial should appear only once and are created afterwards
140 if ( $serialid
141 && $serialid =~ /^[0-9]+$/
142 && !$processedserialid{$serialid} )
144 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
146 for my $d ( qw( publisheddate planneddate )){
147 if ( $serinfo->{$d} =~m/^00/ ) {
148 $serinfo->{$d} = q{};
150 else {
151 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
154 $serinfo->{arriveddate} = $today;
156 $serinfo->{'editdisable'} = (
158 HasSubscriptionExpired( $serinfo->{subscriptionid} )
159 && $serinfo->{'status1'}
161 || $serinfo->{'cannotedit'}
163 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
164 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
165 push @serialdatalist, $serinfo;
166 $processedserialid{$serialid} = 1;
169 my $biblio = Koha::Biblios->find( $serialdatalist[0]->{biblionumber} );
171 my @newserialloop;
172 my @subscriptionloop;
174 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
175 my %processedsubscriptionid;
176 foreach my $subscriptionid (@subscriptionids) {
178 #Do not process subscriptionid twice if it was already processed.
179 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
181 my $cell;
182 if ( $serialdatalist[0]->{'serialsadditems'} ) {
184 #Create New empty item
185 $cell =
186 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
187 '', GetSubscription($subscriptionid) );
188 $cell->{serialsadditems} = 1;
190 $cell->{'subscriptionid'} = $subscriptionid;
191 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
192 $cell->{'itemid'} = 'NNEW';
193 $cell->{'serialid'} = 'NEW';
194 $cell->{'issuesatonce'} = 1;
195 $cell->{arriveddate} = $today;
197 push @newserialloop, $cell;
198 push @subscriptionloop,
200 'subscriptionid' => $subscriptionid,
201 'abouttoexpire' => abouttoexpire($subscriptionid),
202 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
204 $processedsubscriptionid{$subscriptionid} = 1;
207 $template->param( newserialloop => \@newserialloop );
208 $template->param( subscriptions => \@subscriptionloop );
210 if ( $op and $op eq 'serialchangestatus' ) {
212 # Convert serialseqs to UTF-8 to prevent encoding problems
213 foreach my $seq (@serialseqs) {
214 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
217 my $newserial;
218 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
219 my ($plan_date, $pub_date);
221 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
222 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
224 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
225 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
228 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
229 if ( $serialseqs[$i] ) {
231 #IF newserial was provided a name Then we have to create a newSerial
232 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
233 $newserial = NewIssue(
234 $serialseqs[$i],
235 $subscriptionids[0],
236 $serialdatalist[0]->{'biblionumber'},
237 $status[$i],
238 $plan_date,
239 $pub_date,
240 $publisheddatetexts[$i],
241 $notes[$i],
242 $serialdatalist[0]->{'routingnotes'}
246 elsif ( $serialids[$i] ) {
247 ModSerialStatus(
248 $serialids[$i],
249 $serialseqs[$i],
250 $plan_date,
251 $pub_date,
252 $publisheddatetexts[$i],
253 $status[$i],
254 $notes[$i]
257 my $makePreviousSerialAvailable = C4::Context->preference('makePreviousSerialAvailable');
258 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
259 # We already have created the new expected serial at this point, so we get the second previous serial
260 my $previous = GetPreviousSerialid($subscriptionids[$i]);
261 if ($previous) {
263 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
264 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
265 if ($itemnumber) {
267 # Getting the itemtype to set from the database
268 my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
270 # Changing the status to "available" and the itemtype according to the previousitemtype db field
271 ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
277 my @moditems = $query->multi_param('moditem');
278 if ( scalar(@moditems) ) {
279 my @tags = $query->multi_param('tag');
280 my @subfields = $query->multi_param('subfield');
281 my @field_values = $query->multi_param('field_value');
282 my @serials = $query->multi_param('serial');
283 my @bibnums = $query->multi_param('bibnum');
284 my @itemid = $query->multi_param('itemid');
285 my @ind_tag = $query->multi_param('ind_tag');
286 my @indicator = $query->multi_param('indicator');
287 my @num_copies = $query->multi_param('number_of_copies');
289 #Rebuilding ALL the data for items into a hash
290 # parting them on $itemid.
291 my %itemhash;
292 my $countdistinct;
293 my $range = scalar(@itemid);
294 for ( my $i = 0 ; $i < $range ; $i++ ) {
295 unless ( $itemhash{ $itemid[$i] } ) {
296 if ( $serials[$countdistinct]
297 && $serials[$countdistinct] ne "NEW" )
299 $itemhash{ $itemid[$i] }->{'serial'} =
300 $serials[$countdistinct];
302 else {
303 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
305 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
306 $countdistinct++;
308 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
309 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
310 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
311 $field_values[$i];
312 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
313 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
314 push @{ $itemhash{ $itemid[$i] }->{'num_copies'} }, $num_copies[$i];
316 foreach my $item ( keys %itemhash ) {
318 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
319 my $index = -1;
320 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
321 if (
322 $itemhash{$item}->{serial} eq $serialids[$i]
323 || ( $itemhash{$item}->{serial} == $newserial
324 && $serialids[$i] eq 'NEW' )
326 $index = $i
329 if ( $index >= 0 && $status[$index] == 2 ) {
330 my $xml = TransformHtmlToXml(
331 $itemhash{$item}->{'tags'},
332 $itemhash{$item}->{'subfields'},
333 $itemhash{$item}->{'field_values'},
334 $itemhash{$item}->{'indicator'},
335 $itemhash{$item}->{'ind_tag'}
338 # warn $xml;
339 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
340 if ( $item =~ /^N/ ) {
342 $itemhash{$item}->{'num_copies'} //= 1;
344 for (my $copy = 0; $copy < $itemhash{$item}->{'num_copies'}[$index];){
346 # New Item
348 # if autoBarcode is set to 'incremental', calculate barcode...
349 my ( $barcodetagfield, $barcodetagsubfield ) = GetMarcFromKohaField( 'items.barcode' );
350 if ( C4::Context->preference('autoBarcode') eq
351 'incremental' )
353 if (
355 $bib_record->field($barcodetagfield)
356 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
360 my $sth_barcode = $dbh->prepare(
361 'select max(abs(barcode)) from items');
362 $sth_barcode->execute;
363 my ($newbarcode) = $sth_barcode->fetchrow;
365 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
366 $bib_record->field($barcodetagfield)->update( $barcodetagsubfield => ++$newbarcode );
370 # check for item barcode # being unique
371 my $exists;
372 if ( $bib_record->subfield( $barcodetagfield, $barcodetagsubfield ) ) {
373 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
375 if ($copy > 0){
376 use C4::Barcodes;
377 my $barcodeobj = C4::Barcodes->new;
378 my $newbarcode = $barcodeobj->next_value($barcode);
379 $barcode = $newbarcode;
380 $bib_record->field($barcodetagfield)->update($barcodetagsubfield => $barcode);
383 $exists = Koha::Items->find({barcode => $barcode});
386 # push @errors,"barcode_not_unique" if($exists);
387 # if barcode exists, don't create, but report The problem.
388 if ($exists) {
389 push @errors, 'barcode_not_unique';
390 push @errseq, { serialseq => $serialseqs[$index] };
392 else {
393 my ( $biblionumber, $bibitemnum, $itemnumber ) =
394 AddItemFromMarc( $bib_record,
395 $itemhash{$item}->{bibnum} );
396 AddItem2Serial( $itemhash{$item}->{serial},
397 $itemnumber );
399 $copy++;
402 } # num_copies for loop
404 else {
406 #modify item
407 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
408 ModItemFromMarc( $bib_record,
409 $itemhash{$item}->{'bibnum'}, $item );
416 if ( @errors ) {
417 $template->param( Errors => 1 );
418 if ( @errseq ) {
419 $template->param( barcode_not_unique => 1, errseq => \@errseq );
422 else {
423 my $redirect = 'serials-collection.pl?';
424 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
425 print $query->redirect($redirect);
428 my $location = $serialdatalist[0]->{'location'};
429 my $default_bib_view = get_default_view();
431 $template->param(
432 subscriptionid => $serialdatalist[0]->{subscriptionid},
433 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
434 callnumber => $serialdatalist[0]->{'callnumber'},
435 internalnotes => $serialdatalist[0]->{'internalnotes'},
436 bibliotitle => $biblio->title,
437 biblionumber => $serialdatalist[0]->{'biblionumber'},
438 serialslist => \@serialdatalist,
439 default_bib_view => $default_bib_view,
440 location => $location,
441 (uc(C4::Context->preference("marcflavour"))) => 1
444 output_html_with_http_headers $query, $cookie, $template->output;
446 sub get_default_view {
447 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
448 my %views = C4::Search::enabled_staff_search_views();
449 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
450 return 'ISBDdetail';
452 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
453 return 'MARCdetail';
455 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
456 return 'labeledMARCdetail';
458 return 'detail';