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 C4
::Search qw
/enabled_staff_search_views/;
76 use List
::MoreUtils qw
/uniq/;
78 my $query = CGI
->new();
79 my $dbh = C4
::Context
->dbh;
80 my @serialids = $query->param('serialid');
81 my @serialseqs = $query->param('serialseq');
82 my @planneddates = $query->param('planneddate');
83 my @publisheddates = $query->param('publisheddate');
84 my @status = $query->param('status');
85 my @notes = $query->param('notes');
86 my @subscriptionids = $query->param('subscriptionid');
87 my $op = $query->param('op');
88 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q
|,| ) > 0 ) {
89 @subscriptionids = split( /,/, $subscriptionids[0] );
94 # If user comes from subscription details
96 my $serstatus = $query->param('serstatus');
98 foreach my $subscriptionid (@subscriptionids) {
99 my @tmpser = GetSerials2
( $subscriptionid, $serstatus );
100 push @serialids, map { $_->{serialid
} } @tmpser;
105 unless ( @serialids ) {
107 'serials-collection.pl?subscriptionid=' . join ',', uniq
@subscriptionids;
110 print $query->redirect($string);
113 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
115 template_name
=> 'serials/serials-edit.tt',
118 authnotrequired
=> 0,
119 flagsrequired
=> { serials
=> 'receive_serials' },
125 my %processedserialid;
127 my $today = C4
::Dates
->new();
128 foreach my $serialid (@serialids) {
130 #filtering serialid for duplication
131 #NEW serial should appear only once and are created afterwards
133 && $serialid =~ /^[0-9]+$/
134 && !$processedserialid{$serialid} )
136 my $serinfo = GetSerialInformation
($serialid); #TODO duplicates work done by GetSerials2 above
138 for my $d ( qw( publisheddate planneddate )){
139 if ( $serinfo->{$d} =~m/^00/ ) {
140 $serinfo->{$d} = q{};
143 $serinfo->{$d} = format_date
( $serinfo->{$d} );
146 $serinfo->{arriveddate
}=$today->output('syspref');
148 $serinfo->{'editdisable'} = (
150 HasSubscriptionExpired
( $serinfo->{subscriptionid
} )
151 && $serinfo->{'status1'}
153 || $serinfo->{'cannotedit'}
155 $serinfo->{editdisable
} ||= ($serinfo->{status8
} and $serinfo->{closed
});
156 push @serialdatalist, $serinfo;
157 $processedserialid{$serialid} = 1;
160 my $biblio = GetBiblioData
( $serialdatalist[0]->{'biblionumber'} );
163 my @subscriptionloop;
165 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
166 my %processedsubscriptionid;
167 foreach my $subscriptionid (@subscriptionids) {
169 #Do not process subscriptionid twice if it was already processed.
170 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
173 if ( $serialdatalist[0]->{'serialsadditems'} ) {
175 #Create New empty item
177 PrepareItemrecordDisplay
( $serialdatalist[0]->{'biblionumber'},
178 '', GetSubscription
($subscriptionid) );
179 $cell->{serialsadditems
} = 1;
181 $cell->{'subscriptionid'} = $subscriptionid;
182 $cell->{biblionumber
} = $serialdatalist[0]->{'biblionumber'};
183 $cell->{'itemid'} = 'NNEW';
184 $cell->{'serialid'} = 'NEW';
185 $cell->{'issuesatonce'} = 1;
186 $cell->{arriveddate
}=$today->output('syspref');
188 push @newserialloop, $cell;
189 push @subscriptionloop,
191 'subscriptionid' => $subscriptionid,
192 'abouttoexpire' => abouttoexpire
($subscriptionid),
193 'subscriptionexpired' => HasSubscriptionExpired
($subscriptionid),
195 $processedsubscriptionid{$subscriptionid} = 1;
198 $template->param( newserialloop
=> \
@newserialloop );
199 $template->param( subscriptions
=> \
@subscriptionloop );
201 if ( $op and $op eq 'serialchangestatus' ) {
203 # Convert serialseqs to UTF-8 to prevent encoding problems
204 foreach my $seq (@serialseqs) {
205 utf8
::decode
($seq) unless utf8
::is_utf8
($seq);
209 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
210 my ($plan_date, $pub_date);
212 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
213 $plan_date = format_date_in_iso
( $planneddates[$i] );
215 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
216 $pub_date = format_date_in_iso
( $publisheddates[$i] );
219 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
220 if ( $serialseqs[$i] ) {
222 #IF newserial was provided a name Then we have to create a newSerial
223 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
224 $newserial = NewIssue
(
227 $serialdatalist[0]->{'biblionumber'},
235 elsif ( $serialids[$i] ) {
246 my @moditems = $query->param('moditem');
247 if ( scalar(@moditems) ) {
248 my @tags = $query->param('tag');
249 my @subfields = $query->param('subfield');
250 my @field_values = $query->param('field_value');
251 my @serials = $query->param('serial');
252 my @bibnums = $query->param('bibnum');
253 my @itemid = $query->param('itemid');
254 my @ind_tag = $query->param('ind_tag');
255 my @indicator = $query->param('indicator');
257 #Rebuilding ALL the data for items into a hash
258 # parting them on $itemid.
261 my $range = scalar(@itemid);
262 for ( my $i = 0 ; $i < $range ; $i++ ) {
263 unless ( $itemhash{ $itemid[$i] } ) {
264 if ( $serials[$countdistinct]
265 && $serials[$countdistinct] ne "NEW" )
267 $itemhash{ $itemid[$i] }->{'serial'} =
268 $serials[$countdistinct];
271 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
273 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
276 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
277 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
278 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
280 push @
{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
281 push @
{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
283 foreach my $item ( keys %itemhash ) {
285 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
287 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
289 $itemhash{$item}->{serial
} eq $serialids[$i]
290 || ( $itemhash{$item}->{serial
} == $newserial
291 && $serialids[$i] eq 'NEW' )
296 if ( $index >= 0 && $status[$index] == 2 ) {
297 my $xml = TransformHtmlToXml
(
298 $itemhash{$item}->{'tags'},
299 $itemhash{$item}->{'subfields'},
300 $itemhash{$item}->{'field_values'},
301 $itemhash{$item}->{'indicator'},
302 $itemhash{$item}->{'ind_tag'}
306 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
307 if ( $item =~ /^N/ ) {
311 # if autoBarcode is set to 'incremental', calculate barcode...
312 my ( $barcodetagfield, $barcodetagsubfield ) =
313 GetMarcFromKohaField
(
316 $serialdatalist[0]->{'biblionumber'}
319 if ( C4
::Context
->preference('autoBarcode') eq
324 $bib_record->field($barcodetagfield)
325 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
329 my $sth_barcode = $dbh->prepare(
330 'select max(abs(barcode)) from items');
331 $sth_barcode->execute;
332 my ($newbarcode) = $sth_barcode->fetchrow;
334 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
335 $bib_record->field($barcodetagfield)
336 ->update( $barcodetagsubfield => ++$newbarcode );
340 # check for item barcode # being unique
343 $bib_record->subfield(
344 $barcodetagfield, $barcodetagsubfield
348 $exists = GetItemnumberFromBarcode
(
349 $bib_record->subfield(
350 $barcodetagfield, $barcodetagsubfield
355 # push @errors,"barcode_not_unique" if($exists);
356 # if barcode exists, don't create, but report The problem.
358 push @errors, 'barcode_not_unique';
359 push @errseq, { serialseq
=> $serialseqs[$index] };
362 my ( $biblionumber, $bibitemnum, $itemnumber ) =
363 AddItemFromMarc
( $bib_record,
364 $itemhash{$item}->{bibnum
} );
365 AddItem2Serial
( $itemhash{$item}->{serial
},
372 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
373 ModItemFromMarc
( $bib_record,
374 $itemhash{$item}->{'bibnum'}, $item );
382 $template->param( Errors
=> 1 );
384 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
388 my $redirect = 'serials-collection.pl?';
389 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
390 print $query->redirect($redirect);
393 my $location = GetAuthorisedValues
('LOC', $serialdatalist[0]->{'location'});
395 foreach (@
$location) {
396 $locationlib = $_->{'lib'} if $_->{'selected'};
398 my $default_bib_view = get_default_view
();
401 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
402 callnumber
=> $serialdatalist[0]->{'callnumber'},
403 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
404 bibliotitle
=> $biblio->{'title'},
405 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
406 serialslist
=> \
@serialdatalist,
407 default_bib_view
=> $default_bib_view,
408 location
=> $locationlib,
409 (uc(C4
::Context
->preference("marcflavour"))) => 1
412 output_html_with_http_headers
$query, $cookie, $template->output;
414 sub get_default_view
{
415 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
416 my %views = C4
::Search
::enabled_staff_search_views
();
417 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
420 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
423 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
424 return 'labeledMARCdetail';