Bug 19817: Open the manual in a new window, not a popup
[koha.git] / serials / serials-edit.pl
blob928ff47ab3e3a0efbd773c4a3c5084753dc0589c
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);
119 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
121 template_name => 'serials/serials-edit.tt',
122 query => $query,
123 type => 'intranet',
124 authnotrequired => 0,
125 flagsrequired => { serials => 'receive_serials' },
126 debug => 1,
130 my @serialdatalist;
131 my %processedserialid;
133 my $today = output_pref( { dt => dt_from_string, dateonly => 1 } );
135 foreach my $serialid (@serialids) {
137 #filtering serialid for duplication
138 #NEW serial should appear only once and are created afterwards
139 if ( $serialid
140 && $serialid =~ /^[0-9]+$/
141 && !$processedserialid{$serialid} )
143 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
145 for my $d ( qw( publisheddate planneddate )){
146 if ( $serinfo->{$d} =~m/^00/ ) {
147 $serinfo->{$d} = q{};
149 else {
150 $serinfo->{$d} = output_pref( { dt => dt_from_string( $serinfo->{$d} ), dateonly => 1 } );
153 $serinfo->{arriveddate} = $today;
155 $serinfo->{'editdisable'} = (
157 HasSubscriptionExpired( $serinfo->{subscriptionid} )
158 && $serinfo->{'status1'}
160 || $serinfo->{'cannotedit'}
162 $serinfo->{editdisable} = 0 if C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'receive_serials' } );
163 $serinfo->{editdisable} ||= ($serinfo->{status8} and $serinfo->{closed});
164 push @serialdatalist, $serinfo;
165 $processedserialid{$serialid} = 1;
168 my $biblio = Koha::Biblios->find( $serialdatalist[0]->{biblionumber} );
170 my @newserialloop;
171 my @subscriptionloop;
173 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
174 my %processedsubscriptionid;
175 foreach my $subscriptionid (@subscriptionids) {
177 #Do not process subscriptionid twice if it was already processed.
178 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
180 my $cell;
181 if ( $serialdatalist[0]->{'serialsadditems'} ) {
183 #Create New empty item
184 $cell =
185 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
186 '', GetSubscription($subscriptionid) );
187 $cell->{serialsadditems} = 1;
189 $cell->{'subscriptionid'} = $subscriptionid;
190 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
191 $cell->{'itemid'} = 'NNEW';
192 $cell->{'serialid'} = 'NEW';
193 $cell->{'issuesatonce'} = 1;
194 $cell->{arriveddate} = $today;
196 push @newserialloop, $cell;
197 push @subscriptionloop,
199 'subscriptionid' => $subscriptionid,
200 'abouttoexpire' => abouttoexpire($subscriptionid),
201 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
203 $processedsubscriptionid{$subscriptionid} = 1;
206 $template->param( newserialloop => \@newserialloop );
207 $template->param( subscriptions => \@subscriptionloop );
209 if ( $op and $op eq 'serialchangestatus' ) {
211 # Convert serialseqs to UTF-8 to prevent encoding problems
212 foreach my $seq (@serialseqs) {
213 $seq = Encode::decode('UTF-8', $seq) unless Encode::is_utf8($seq);
216 my $newserial;
217 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
218 my ($plan_date, $pub_date);
220 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
221 $plan_date = eval { output_pref( { dt => dt_from_string( $planneddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
223 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
224 $pub_date = eval { output_pref( { dt => dt_from_string( $publisheddates[$i] ), dateonly => 1, dateformat => 'iso' } ); };
227 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
228 if ( $serialseqs[$i] ) {
230 #IF newserial was provided a name Then we have to create a newSerial
231 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
232 $newserial = NewIssue(
233 $serialseqs[$i],
234 $subscriptionids[0],
235 $serialdatalist[0]->{'biblionumber'},
236 $status[$i],
237 $plan_date,
238 $pub_date,
239 $publisheddatetexts[$i],
240 $notes[$i]
244 elsif ( $serialids[$i] ) {
245 ModSerialStatus(
246 $serialids[$i],
247 $serialseqs[$i],
248 $plan_date,
249 $pub_date,
250 $publisheddatetexts[$i],
251 $status[$i],
252 $notes[$i]
255 my $makePreviousSerialAvailable = C4::Context->preference('makePreviousSerialAvailable');
256 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
257 # We already have created the new expected serial at this point, so we get the second previous serial
258 my $previous = GetPreviousSerialid($subscriptionids[$i]);
259 if ($previous) {
261 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
262 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
263 if ($itemnumber) {
265 # Getting the itemtype to set from the database
266 my $subscriptioninfos = GetSubscription($subscriptionids[$i]);
268 # Changing the status to "available" and the itemtype according to the previousitemtype db field
269 ModItem({notforloan => 0, itype => $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
275 my @moditems = $query->multi_param('moditem');
276 if ( scalar(@moditems) ) {
277 my @tags = $query->multi_param('tag');
278 my @subfields = $query->multi_param('subfield');
279 my @field_values = $query->multi_param('field_value');
280 my @serials = $query->multi_param('serial');
281 my @bibnums = $query->multi_param('bibnum');
282 my @itemid = $query->multi_param('itemid');
283 my @ind_tag = $query->multi_param('ind_tag');
284 my @indicator = $query->multi_param('indicator');
286 #Rebuilding ALL the data for items into a hash
287 # parting them on $itemid.
288 my %itemhash;
289 my $countdistinct;
290 my $range = scalar(@itemid);
291 for ( my $i = 0 ; $i < $range ; $i++ ) {
292 unless ( $itemhash{ $itemid[$i] } ) {
293 if ( $serials[$countdistinct]
294 && $serials[$countdistinct] ne "NEW" )
296 $itemhash{ $itemid[$i] }->{'serial'} =
297 $serials[$countdistinct];
299 else {
300 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
302 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
303 $countdistinct++;
305 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
306 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
307 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
308 $field_values[$i];
309 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
310 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
312 foreach my $item ( keys %itemhash ) {
314 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
315 my $index = -1;
316 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
317 if (
318 $itemhash{$item}->{serial} eq $serialids[$i]
319 || ( $itemhash{$item}->{serial} == $newserial
320 && $serialids[$i] eq 'NEW' )
322 $index = $i
325 if ( $index >= 0 && $status[$index] == 2 ) {
326 my $xml = TransformHtmlToXml(
327 $itemhash{$item}->{'tags'},
328 $itemhash{$item}->{'subfields'},
329 $itemhash{$item}->{'field_values'},
330 $itemhash{$item}->{'indicator'},
331 $itemhash{$item}->{'ind_tag'}
334 # warn $xml;
335 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
336 if ( $item =~ /^N/ ) {
338 #New Item
340 # if autoBarcode is set to 'incremental', calculate barcode...
341 my ( $barcodetagfield, $barcodetagsubfield ) =
342 GetMarcFromKohaField(
343 'items.barcode',
344 GetFrameworkCode(
345 $serialdatalist[0]->{'biblionumber'}
348 if ( C4::Context->preference('autoBarcode') eq
349 'incremental' )
351 if (
353 $bib_record->field($barcodetagfield)
354 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
358 my $sth_barcode = $dbh->prepare(
359 'select max(abs(barcode)) from items');
360 $sth_barcode->execute;
361 my ($newbarcode) = $sth_barcode->fetchrow;
363 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
364 $bib_record->field($barcodetagfield)
365 ->update( $barcodetagsubfield => ++$newbarcode );
369 # check for item barcode # being unique
370 my $exists;
371 if (
372 $bib_record->subfield(
373 $barcodetagfield, $barcodetagsubfield
377 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
378 $exists = Koha::Items->find({barcode => $barcode});
381 # push @errors,"barcode_not_unique" if($exists);
382 # if barcode exists, don't create, but report The problem.
383 if ($exists) {
384 push @errors, 'barcode_not_unique';
385 push @errseq, { serialseq => $serialseqs[$index] };
387 else {
388 my ( $biblionumber, $bibitemnum, $itemnumber ) =
389 AddItemFromMarc( $bib_record,
390 $itemhash{$item}->{bibnum} );
391 AddItem2Serial( $itemhash{$item}->{serial},
392 $itemnumber );
395 else {
397 #modify item
398 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
399 ModItemFromMarc( $bib_record,
400 $itemhash{$item}->{'bibnum'}, $item );
407 if ( @errors ) {
408 $template->param( Errors => 1 );
409 if ( @errseq ) {
410 $template->param( barcode_not_unique => 1, errseq => \@errseq );
413 else {
414 my $redirect = 'serials-collection.pl?';
415 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
416 print $query->redirect($redirect);
419 my $location = $serialdatalist[0]->{'location'};
420 my $default_bib_view = get_default_view();
422 $template->param(
423 subscriptionid => $serialdatalist[0]->{subscriptionid},
424 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
425 callnumber => $serialdatalist[0]->{'callnumber'},
426 internalnotes => $serialdatalist[0]->{'internalnotes'},
427 bibliotitle => $biblio->title,
428 biblionumber => $serialdatalist[0]->{'biblionumber'},
429 serialslist => \@serialdatalist,
430 default_bib_view => $default_bib_view,
431 location => $location,
432 (uc(C4::Context->preference("marcflavour"))) => 1
435 output_html_with_http_headers $query, $cookie, $template->output;
437 sub get_default_view {
438 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
439 my %views = C4::Search::enabled_staff_search_views();
440 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
441 return 'ISBDdetail';
443 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
444 return 'MARCdetail';
446 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
447 return 'labeledMARCdetail';
449 return 'detail';