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>.
31 * modsubscriptionhistory :to modify the subscription history
32 * serialchangestatus :to modify the status of this subscription
67 use Encode
qw( decode is_utf8 );
75 use C4
::Search qw
/enabled_staff_search_views/;
78 use List
::MoreUtils qw
/uniq/;
80 my $query = CGI
->new();
81 my $dbh = C4
::Context
->dbh;
82 my @serialids = $query->multi_param('serialid');
83 my @serialseqs = $query->multi_param('serialseq');
84 my @planneddates = $query->multi_param('planneddate');
85 my @publisheddates = $query->multi_param('publisheddate');
86 my @publisheddatetexts = $query->multi_param('publisheddatetext');
87 my @status = $query->multi_param('status');
88 my @notes = $query->multi_param('notes');
89 my @subscriptionids = $query->multi_param('subscriptionid');
90 my $op = $query->param('op');
91 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q
|,| ) > 0 ) {
92 @subscriptionids = split( /,/, $subscriptionids[0] );
97 # If user comes from subscription details
99 my $serstatus = $query->param('serstatus');
100 my @statuses = split ',', $serstatus;
102 foreach my $subscriptionid (@subscriptionids) {
103 my @tmpser = GetSerials2
( $subscriptionid, \
@statuses );
104 push @serialids, map { $_->{serialid
} } @tmpser;
109 unless ( @serialids ) {
111 'serials-collection.pl?subscriptionid=' . join ',', uniq
@subscriptionids;
114 print $query->redirect($string);
117 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
119 template_name
=> 'serials/serials-edit.tt',
122 authnotrequired
=> 0,
123 flagsrequired
=> { serials
=> 'receive_serials' },
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
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{};
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'} );
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} )
179 if ( $serialdatalist[0]->{'serialsadditems'} ) {
181 #Create New empty item
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);
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
(
233 $serialdatalist[0]->{'biblionumber'},
237 $publisheddatetexts[$i],
242 elsif ( $serialids[$i] ) {
248 $publisheddatetexts[$i],
254 my @moditems = $query->multi_param('moditem');
255 if ( scalar(@moditems) ) {
256 my @tags = $query->multi_param('tag');
257 my @subfields = $query->multi_param('subfield');
258 my @field_values = $query->multi_param('field_value');
259 my @serials = $query->multi_param('serial');
260 my @bibnums = $query->multi_param('bibnum');
261 my @itemid = $query->multi_param('itemid');
262 my @ind_tag = $query->multi_param('ind_tag');
263 my @indicator = $query->multi_param('indicator');
265 #Rebuilding ALL the data for items into a hash
266 # parting them on $itemid.
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];
279 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
281 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
284 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
285 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
286 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
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
295 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
297 $itemhash{$item}->{serial
} eq $serialids[$i]
298 || ( $itemhash{$item}->{serial
} == $newserial
299 && $serialids[$i] eq 'NEW' )
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'}
314 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
315 if ( $item =~ /^N/ ) {
319 # if autoBarcode is set to 'incremental', calculate barcode...
320 my ( $barcodetagfield, $barcodetagsubfield ) =
321 GetMarcFromKohaField
(
324 $serialdatalist[0]->{'biblionumber'}
327 if ( C4
::Context
->preference('autoBarcode') eq
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
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.
366 push @errors, 'barcode_not_unique';
367 push @errseq, { serialseq
=> $serialseqs[$index] };
370 my ( $biblionumber, $bibitemnum, $itemnumber ) =
371 AddItemFromMarc
( $bib_record,
372 $itemhash{$item}->{bibnum
} );
373 AddItem2Serial
( $itemhash{$item}->{serial
},
380 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
381 ModItemFromMarc
( $bib_record,
382 $itemhash{$item}->{'bibnum'}, $item );
390 $template->param( Errors
=> 1 );
392 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
396 my $redirect = 'serials-collection.pl?';
397 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
398 print $query->redirect($redirect);
401 my $location = $serialdatalist[0]->{'location'};
402 my $default_bib_view = get_default_view
();
405 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
406 callnumber
=> $serialdatalist[0]->{'callnumber'},
407 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
408 bibliotitle
=> $biblio->{'title'},
409 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
410 serialslist
=> \
@serialdatalist,
411 default_bib_view
=> $default_bib_view,
412 location
=> $location,
413 (uc(C4
::Context
->preference("marcflavour"))) => 1
416 output_html_with_http_headers
$query, $cookie, $template->output;
418 sub get_default_view
{
419 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
420 my %views = C4
::Search
::enabled_staff_search_views
();
421 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
424 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
427 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
428 return 'labeledMARCdetail';