Bug 18309: UNIMARC update from IFLA - authority (fr) (default framework)
[koha.git] / serials / serials-edit.pl
blob9873fc99dbe01fef406b243437b34f98e3978f09
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');
288 #Rebuilding ALL the data for items into a hash
289 # parting them on $itemid.
290 my %itemhash;
291 my $countdistinct;
292 my $range = scalar(@itemid);
293 for ( my $i = 0 ; $i < $range ; $i++ ) {
294 unless ( $itemhash{ $itemid[$i] } ) {
295 if ( $serials[$countdistinct]
296 && $serials[$countdistinct] ne "NEW" )
298 $itemhash{ $itemid[$i] }->{'serial'} =
299 $serials[$countdistinct];
301 else {
302 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
304 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
305 $countdistinct++;
307 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
308 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
309 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
310 $field_values[$i];
311 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
312 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
314 foreach my $item ( keys %itemhash ) {
316 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
317 my $index = -1;
318 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
319 if (
320 $itemhash{$item}->{serial} eq $serialids[$i]
321 || ( $itemhash{$item}->{serial} == $newserial
322 && $serialids[$i] eq 'NEW' )
324 $index = $i
327 if ( $index >= 0 && $status[$index] == 2 ) {
328 my $xml = TransformHtmlToXml(
329 $itemhash{$item}->{'tags'},
330 $itemhash{$item}->{'subfields'},
331 $itemhash{$item}->{'field_values'},
332 $itemhash{$item}->{'indicator'},
333 $itemhash{$item}->{'ind_tag'}
336 # warn $xml;
337 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
338 if ( $item =~ /^N/ ) {
340 #New Item
342 # if autoBarcode is set to 'incremental', calculate barcode...
343 my ( $barcodetagfield, $barcodetagsubfield ) = GetMarcFromKohaField( 'items.barcode' );
344 if ( C4::Context->preference('autoBarcode') eq
345 'incremental' )
347 if (
349 $bib_record->field($barcodetagfield)
350 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
354 my $sth_barcode = $dbh->prepare(
355 'select max(abs(barcode)) from items');
356 $sth_barcode->execute;
357 my ($newbarcode) = $sth_barcode->fetchrow;
359 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
360 $bib_record->field($barcodetagfield)
361 ->update( $barcodetagsubfield => ++$newbarcode );
365 # check for item barcode # being unique
366 my $exists;
367 if (
368 $bib_record->subfield(
369 $barcodetagfield, $barcodetagsubfield
373 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
374 $exists = Koha::Items->find({barcode => $barcode});
377 # push @errors,"barcode_not_unique" if($exists);
378 # if barcode exists, don't create, but report The problem.
379 if ($exists) {
380 push @errors, 'barcode_not_unique';
381 push @errseq, { serialseq => $serialseqs[$index] };
383 else {
384 my ( $biblionumber, $bibitemnum, $itemnumber ) =
385 AddItemFromMarc( $bib_record,
386 $itemhash{$item}->{bibnum} );
387 AddItem2Serial( $itemhash{$item}->{serial},
388 $itemnumber );
391 else {
393 #modify item
394 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
395 ModItemFromMarc( $bib_record,
396 $itemhash{$item}->{'bibnum'}, $item );
403 if ( @errors ) {
404 $template->param( Errors => 1 );
405 if ( @errseq ) {
406 $template->param( barcode_not_unique => 1, errseq => \@errseq );
409 else {
410 my $redirect = 'serials-collection.pl?';
411 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
412 print $query->redirect($redirect);
415 my $location = $serialdatalist[0]->{'location'};
416 my $default_bib_view = get_default_view();
418 $template->param(
419 subscriptionid => $serialdatalist[0]->{subscriptionid},
420 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
421 callnumber => $serialdatalist[0]->{'callnumber'},
422 internalnotes => $serialdatalist[0]->{'internalnotes'},
423 bibliotitle => $biblio->title,
424 biblionumber => $serialdatalist[0]->{'biblionumber'},
425 serialslist => \@serialdatalist,
426 default_bib_view => $default_bib_view,
427 location => $location,
428 (uc(C4::Context->preference("marcflavour"))) => 1
431 output_html_with_http_headers $query, $cookie, $template->output;
433 sub get_default_view {
434 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
435 my %views = C4::Search::enabled_staff_search_views();
436 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
437 return 'ISBDdetail';
439 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
440 return 'MARCdetail';
442 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
443 return 'labeledMARCdetail';
445 return 'detail';