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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 * modsubscriptionhistory :to modify the subscription history
32 * serialchangestatus :to modify the status of this subscription
68 use C4::Dates qw/format_date format_date_in_iso/;
75 use List::MoreUtils qw/uniq/;
77 my $query = CGI->new();
78 my $dbh = C4::Context->dbh;
79 my @serialids = $query->param('serialid');
80 my @serialseqs = $query->param('serialseq');
81 my @planneddates = $query->param('planneddate');
82 my @publisheddates = $query->param('publisheddate');
83 my @status = $query->param('status');
84 my @notes = $query->param('notes');
85 my @subscriptionids = $query->param('subscriptionid');
86 my $op = $query->param('op');
87 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
88 @subscriptionids = split( /,/, $subscriptionids[0] );
93 # If user comes from subscription details
95 my $serstatus = $query->param('serstatus');
97 foreach my $subscriptionid (@subscriptionids) {
98 my @tmpser = GetSerials2( $subscriptionid, $serstatus );
99 push @serialids, map { $_->{serialid} } @tmpser;
104 unless ( @serialids ) {
106 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
109 print $query->redirect($string);
112 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
114 template_name => 'serials/serials-edit.tmpl',
117 authnotrequired => 0,
118 flagsrequired => { serials => 1 },
124 my %processedserialid;
126 my $today = C4::Dates->new();
127 foreach my $serialid (@serialids) {
129 #filtering serialid for duplication
130 #NEW serial should appear only once and are created afterwards
132 && $serialid =~ /^[0-9]+$/
133 && !$processedserialid{$serialid} )
135 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
136 for my $d ( qw( publisheddate planneddate )){
137 if ( $serinfo->{$d} =~m/^00/ ) {
138 $serinfo->{$d} = q{};
141 $serinfo->{$d} = format_date( $serinfo->{$d} );
144 $serinfo->{arriveddate}=$today->output('syspref');
146 $serinfo->{'editdisable'} = (
148 HasSubscriptionExpired( $serinfo->{subscriptionid} )
149 && $serinfo->{'status1'}
151 || $serinfo->{'cannotedit'}
153 push @serialdatalist, $serinfo;
154 $processedserialid{$serialid} = 1;
157 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
160 my @subscriptionloop;
162 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
163 my %processedsubscriptionid;
164 foreach my $subscriptionid (@subscriptionids) {
166 #Do not process subscriptionid twice if it was already processed.
167 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
170 if ( $serialdatalist[0]->{'serialsadditems'} ) {
172 #Create New empty item
174 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
175 '', GetSubscription($subscriptionid) );
176 $cell->{serialsadditems} = 1;
178 $cell->{'subscriptionid'} = $subscriptionid;
179 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
180 $cell->{'itemid'} = 'NNEW';
181 $cell->{'serialid'} = 'NEW';
182 $cell->{'issuesatonce'} = 1;
183 $cell->{arriveddate}=$today->output('syspref');
185 push @newserialloop, $cell;
186 push @subscriptionloop,
188 'subscriptionid' => $subscriptionid,
189 'abouttoexpire' => abouttoexpire($subscriptionid),
190 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
192 $processedsubscriptionid{$subscriptionid} = 1;
195 $template->param( newserialloop => \@newserialloop );
196 $template->param( subscriptions => \@subscriptionloop );
198 if ( $op and $op eq 'serialchangestatus' ) {
200 # Convert serialseqs to UTF-8 to prevent encoding problems
201 foreach my $seq (@serialseqs) {
202 utf8::decode($seq) unless utf8::is_utf8($seq);
206 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
207 my ($plan_date, $pub_date);
209 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
210 $plan_date = format_date_in_iso( $planneddates[$i] );
212 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
213 $pub_date = format_date_in_iso( $publisheddates[$i] );
216 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
217 if ( $serialseqs[$i] ) {
219 #IF newserial was provided a name Then we have to create a newSerial
220 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
221 $newserial = NewIssue(
224 $serialdatalist[0]->{'biblionumber'},
232 elsif ( $serialids[$i] ) {
243 my @moditems = $query->param('moditem');
244 if ( scalar(@moditems) ) {
245 my @tags = $query->param('tag');
246 my @subfields = $query->param('subfield');
247 my @field_values = $query->param('field_value');
248 my @serials = $query->param('serial');
249 my @bibnums = $query->param('bibnum');
250 my @itemid = $query->param('itemid');
251 my @ind_tag = $query->param('ind_tag');
252 my @indicator = $query->param('indicator');
254 #Rebuilding ALL the data for items into a hash
255 # parting them on $itemid.
258 my $range = scalar(@itemid);
259 for ( my $i = 0 ; $i < $range ; $i++ ) {
260 unless ( $itemhash{ $itemid[$i] } ) {
261 if ( $serials[$countdistinct]
262 && $serials[$countdistinct] ne "NEW" )
264 $itemhash{ $itemid[$i] }->{'serial'} =
265 $serials[$countdistinct];
268 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
270 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
273 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
274 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
275 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
277 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
278 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
280 foreach my $item ( keys %itemhash ) {
282 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
284 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
286 $itemhash{$item}->{serial} eq $serialids[$i]
287 || ( $itemhash{$item}->{serial} == $newserial
288 && $serialids[$i] eq 'NEW' )
293 if ( $index >= 0 && $status[$index] == 2 ) {
294 my $xml = TransformHtmlToXml(
295 $itemhash{$item}->{'tags'},
296 $itemhash{$item}->{'subfields'},
297 $itemhash{$item}->{'field_values'},
298 $itemhash{$item}->{'indicator'},
299 $itemhash{$item}->{'ind_tag'}
303 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
304 if ( $item =~ /^N/ ) {
308 # if autoBarcode is set to 'incremental', calculate barcode...
309 my ( $barcodetagfield, $barcodetagsubfield ) =
310 GetMarcFromKohaField(
313 $serialdatalist[0]->{'biblionumber'}
316 if ( C4::Context->preference('autoBarcode') eq
319 if ( !$bib_record->field($barcodetagfield)
320 ->subfield($barcodetagsubfield) )
322 my $sth_barcode = $dbh->prepare(
323 'select max(abs(barcode)) from items');
324 $sth_barcode->execute;
325 my ($newbarcode) = $sth_barcode->fetchrow;
327 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
328 $bib_record->field($barcodetagfield)
329 ->update( $barcodetagsubfield => ++$newbarcode );
333 # check for item barcode # being unique
336 $bib_record->subfield(
337 $barcodetagfield, $barcodetagsubfield
341 $exists = GetItemnumberFromBarcode(
342 $bib_record->subfield(
343 $barcodetagfield, $barcodetagsubfield
348 # push @errors,"barcode_not_unique" if($exists);
349 # if barcode exists, don't create, but report The problem.
351 push @errors, 'barcode_not_unique';
352 push @errseq, { serialseq => $serialseqs[$index] };
355 my ( $biblionumber, $bibitemnum, $itemnumber ) =
356 AddItemFromMarc( $bib_record,
357 $itemhash{$item}->{bibnum} );
358 AddItem2Serial( $itemhash{$item}->{serial},
365 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
366 ModItemFromMarc( $bib_record,
367 $itemhash{$item}->{'bibnum'}, $item );
375 $template->param( Errors => 1 );
377 $template->param( barcode_not_unique => 1, errseq => \@errseq );
381 my $redirect = 'serials-collection.pl?';
382 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
383 print $query->redirect($redirect);
386 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
388 foreach (@$location) {
389 $locationlib = $_->{'lib'} if $_->{'selected'};
391 my $default_bib_view = get_default_view();
394 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
395 callnumber => $serialdatalist[0]->{'callnumber'},
396 bibliotitle => $biblio->{'title'},
397 biblionumber => $serialdatalist[0]->{'biblionumber'},
398 serialslist => \@serialdatalist,
399 default_bib_view => $default_bib_view,
400 location => $locationlib,
402 output_html_with_http_headers $query, $cookie, $template->output;
404 sub get_default_view {
405 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
406 my $views = { C4::Search::enabled_staff_search_views };
407 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
409 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
411 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
412 return 'labeledMARCdetail';