Bug 14349: Checkouts and Fines tab missing category description on the left
[koha.git] / serials / serials-edit.pl
blob309527ea4addb00416ea599227b9d14b1c21304c
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;
78 use List::MoreUtils qw/uniq/;
80 my $query = CGI->new();
81 my $dbh = C4::Context->dbh;
82 my @serialids = $query->param('serialid');
83 my @serialseqs = $query->param('serialseq');
84 my @planneddates = $query->param('planneddate');
85 my @publisheddates = $query->param('publisheddate');
86 my @publisheddatetexts = $query->param('publisheddatetext');
87 my @status = $query->param('status');
88 my @notes = $query->param('notes');
89 my @subscriptionids = $query->param('subscriptionid');
90 my $op = $query->param('op');
91 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
92 @subscriptionids = split( /,/, $subscriptionids[0] );
94 my @errors;
95 my @errseq;
97 # If user comes from subscription details
98 unless (@serialids) {
99 my $serstatus = $query->param('serstatus');
100 my @statuses = split ',', $serstatus;
101 if ($serstatus) {
102 foreach my $subscriptionid (@subscriptionids) {
103 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
104 push @serialids, map { $_->{serialid} } @tmpser;
109 unless ( @serialids ) {
110 my $string =
111 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
112 $string =~ s/,$//;
114 print $query->redirect($string);
117 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
119 template_name => 'serials/serials-edit.tt',
120 query => $query,
121 type => 'intranet',
122 authnotrequired => 0,
123 flagsrequired => { serials => 'receive_serials' },
124 debug => 1,
128 my @serialdatalist;
129 my %processedserialid;
131 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
133 foreach my $serialid (@serialids) {
135 #filtering serialid for duplication
136 #NEW serial should appear only once and are created afterwards
137 if ( $serialid
138 && $serialid =~ /^[0-9]+$/
139 && !$processedserialid{$serialid} )
141 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
143 for my $d ( qw( publisheddate planneddate )){
144 if ( $serinfo->{$d} =~m/^00/ ) {
145 $serinfo->{$d} = q{};
147 else {
148 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
151 $serinfo->{arriveddate} = $today;
153 $serinfo->{'editdisable'} = (
155 HasSubscriptionExpired( $serinfo->{subscriptionid} )
156 && $serinfo->{'status1'}
158 || $serinfo->{'cannotedit'}
160 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
161 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
162 push @serialdatalist, $serinfo;
163 $processedserialid{$serialid} = 1;
166 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
168 my @newserialloop;
169 my @subscriptionloop;
171 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
172 my %processedsubscriptionid;
173 foreach my $subscriptionid (@subscriptionids) {
175 #Do not process subscriptionid twice if it was already processed.
176 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
178 my $cell;
179 if ( $serialdatalist[0]->{'serialsadditems'} ) {
181 #Create New empty item
182 $cell =
183 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
184 '', GetSubscription($subscriptionid) );
185 $cell->{serialsadditems} = 1;
187 $cell->{'subscriptionid'} = $subscriptionid;
188 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
189 $cell->{'itemid'} = 'NNEW';
190 $cell->{'serialid'} = 'NEW';
191 $cell->{'issuesatonce'} = 1;
192 $cell->{arriveddate} = $today;
194 push @newserialloop, $cell;
195 push @subscriptionloop,
197 'subscriptionid' => $subscriptionid,
198 'abouttoexpire' => abouttoexpire($subscriptionid),
199 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
201 $processedsubscriptionid{$subscriptionid} = 1;
204 $template->param( newserialloop => \@newserialloop );
205 $template->param( subscriptions => \@subscriptionloop );
207 if ( $op and $op eq 'serialchangestatus' ) {
209 # Convert serialseqs to UTF-8 to prevent encoding problems
210 foreach my $seq (@serialseqs) {
211 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
214 my $newserial;
215 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
216 my ($plan_date, $pub_date);
218 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
219 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
221 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
222 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
225 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
226 if ( $serialseqs[$i] ) {
228 #IF newserial was provided a name Then we have to create a newSerial
229 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
230 $newserial = NewIssue(
231 $serialseqs[$i],
232 $subscriptionids[0],
233 $serialdatalist[0]->{'biblionumber'},
234 $status[$i],
235 $plan_date,
236 $pub_date,
237 $publisheddatetexts[$i],
238 $notes[$i]
242 elsif ( $serialids[$i] ) {
243 ModSerialStatus(
244 $serialids[$i],
245 $serialseqs[$i],
246 $plan_date,
247 $pub_date,
248 $publisheddatetexts[$i],
249 $status[$i],
250 $notes[$i]
254 my @moditems = $query->param('moditem');
255 if ( scalar(@moditems) ) {
256 my @tags = $query->param('tag');
257 my @subfields = $query->param('subfield');
258 my @field_values = $query->param('field_value');
259 my @serials = $query->param('serial');
260 my @bibnums = $query->param('bibnum');
261 my @itemid = $query->param('itemid');
262 my @ind_tag = $query->param('ind_tag');
263 my @indicator = $query->param('indicator');
265 #Rebuilding ALL the data for items into a hash
266 # parting them on $itemid.
267 my %itemhash;
268 my $countdistinct;
269 my $range = scalar(@itemid);
270 for ( my $i = 0 ; $i < $range ; $i++ ) {
271 unless ( $itemhash{ $itemid[$i] } ) {
272 if ( $serials[$countdistinct]
273 && $serials[$countdistinct] ne "NEW" )
275 $itemhash{ $itemid[$i] }->{'serial'} =
276 $serials[$countdistinct];
278 else {
279 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
281 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
282 $countdistinct++;
284 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
285 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
286 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
287 $field_values[$i];
288 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
289 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
291 foreach my $item ( keys %itemhash ) {
293 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
294 my $index = -1;
295 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
296 if (
297 $itemhash{$item}->{serial} eq $serialids[$i]
298 || ( $itemhash{$item}->{serial} == $newserial
299 && $serialids[$i] eq 'NEW' )
301 $index = $i
304 if ( $index >= 0 && $status[$index] == 2 ) {
305 my $xml = TransformHtmlToXml(
306 $itemhash{$item}->{'tags'},
307 $itemhash{$item}->{'subfields'},
308 $itemhash{$item}->{'field_values'},
309 $itemhash{$item}->{'indicator'},
310 $itemhash{$item}->{'ind_tag'}
313 # warn $xml;
314 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
315 if ( $item =~ /^N/ ) {
317 #New Item
319 # if autoBarcode is set to 'incremental', calculate barcode...
320 my ( $barcodetagfield, $barcodetagsubfield ) =
321 GetMarcFromKohaField(
322 'items.barcode',
323 GetFrameworkCode(
324 $serialdatalist[0]->{'biblionumber'}
327 if ( C4::Context->preference('autoBarcode') eq
328 'incremental' )
330 if (
332 $bib_record->field($barcodetagfield)
333 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
337 my $sth_barcode = $dbh->prepare(
338 'select max(abs(barcode)) from items');
339 $sth_barcode->execute;
340 my ($newbarcode) = $sth_barcode->fetchrow;
342 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
343 $bib_record->field($barcodetagfield)
344 ->update( $barcodetagsubfield => ++$newbarcode );
348 # check for item barcode # being unique
349 my $exists;
350 if (
351 $bib_record->subfield(
352 $barcodetagfield, $barcodetagsubfield
356 $exists = GetItemnumberFromBarcode(
357 $bib_record->subfield(
358 $barcodetagfield, $barcodetagsubfield
363 # push @errors,"barcode_not_unique" if($exists);
364 # if barcode exists, don't create, but report The problem.
365 if ($exists) {
366 push @errors, 'barcode_not_unique';
367 push @errseq, { serialseq => $serialseqs[$index] };
369 else {
370 my ( $biblionumber, $bibitemnum, $itemnumber ) =
371 AddItemFromMarc( $bib_record,
372 $itemhash{$item}->{bibnum} );
373 AddItem2Serial( $itemhash{$item}->{serial},
374 $itemnumber );
377 else {
379 #modify item
380 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
381 ModItemFromMarc( $bib_record,
382 $itemhash{$item}->{'bibnum'}, $item );
389 if ( @errors ) {
390 $template->param( Errors => 1 );
391 if ( @errseq ) {
392 $template->param( barcode_not_unique => 1, errseq => \@errseq );
395 else {
396 my $redirect = 'serials-collection.pl?';
397 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
398 print $query->redirect($redirect);
401 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
402 my $locationlib;
403 foreach (@$location) {
404 $locationlib = $_->{'lib'} if $_->{'selected'};
406 my $default_bib_view = get_default_view();
408 $template->param(
409 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
410 callnumber => $serialdatalist[0]->{'callnumber'},
411 internalnotes => $serialdatalist[0]->{'internalnotes'},
412 bibliotitle => $biblio->{'title'},
413 biblionumber => $serialdatalist[0]->{'biblionumber'},
414 serialslist => \@serialdatalist,
415 default_bib_view => $default_bib_view,
416 location => $locationlib,
417 (uc(C4::Context->preference("marcflavour"))) => 1
420 output_html_with_http_headers $query, $cookie, $template->output;
422 sub get_default_view {
423 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
424 my %views = C4::Search::enabled_staff_search_views();
425 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
426 return 'ISBDdetail';
428 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
429 return 'MARCdetail';
431 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
432 return 'labeledMARCdetail';
434 return 'detail';