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.tmpl',
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
137 for my $d ( qw( publisheddate planneddate )){
138 if ( $serinfo->{$d} =~m/^00/ ) {
139 $serinfo->{$d} = q{};
142 $serinfo->{$d} = format_date
( $serinfo->{$d} );
145 $serinfo->{arriveddate
}=$today->output('syspref');
147 $serinfo->{'editdisable'} = (
149 HasSubscriptionExpired
( $serinfo->{subscriptionid
} )
150 && $serinfo->{'status1'}
152 || $serinfo->{'cannotedit'}
154 $serinfo->{editdisable
} ||= ($serinfo->{status8
} and $serinfo->{closed
});
155 push @serialdatalist, $serinfo;
156 $processedserialid{$serialid} = 1;
159 my $biblio = GetBiblioData
( $serialdatalist[0]->{'biblionumber'} );
162 my @subscriptionloop;
164 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
165 my %processedsubscriptionid;
166 foreach my $subscriptionid (@subscriptionids) {
168 #Do not process subscriptionid twice if it was already processed.
169 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
172 if ( $serialdatalist[0]->{'serialsadditems'} ) {
174 #Create New empty item
176 PrepareItemrecordDisplay
( $serialdatalist[0]->{'biblionumber'},
177 '', GetSubscription
($subscriptionid) );
178 $cell->{serialsadditems
} = 1;
180 $cell->{'subscriptionid'} = $subscriptionid;
181 $cell->{biblionumber
} = $serialdatalist[0]->{'biblionumber'};
182 $cell->{'itemid'} = 'NNEW';
183 $cell->{'serialid'} = 'NEW';
184 $cell->{'issuesatonce'} = 1;
185 $cell->{arriveddate
}=$today->output('syspref');
187 push @newserialloop, $cell;
188 push @subscriptionloop,
190 'subscriptionid' => $subscriptionid,
191 'abouttoexpire' => abouttoexpire
($subscriptionid),
192 'subscriptionexpired' => HasSubscriptionExpired
($subscriptionid),
194 $processedsubscriptionid{$subscriptionid} = 1;
197 $template->param( newserialloop
=> \
@newserialloop );
198 $template->param( subscriptions
=> \
@subscriptionloop );
200 if ( $op and $op eq 'serialchangestatus' ) {
202 # Convert serialseqs to UTF-8 to prevent encoding problems
203 foreach my $seq (@serialseqs) {
204 utf8
::decode
($seq) unless utf8
::is_utf8
($seq);
208 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
209 my ($plan_date, $pub_date);
211 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
212 $plan_date = format_date_in_iso
( $planneddates[$i] );
214 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
215 $pub_date = format_date_in_iso
( $publisheddates[$i] );
218 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
219 if ( $serialseqs[$i] ) {
221 #IF newserial was provided a name Then we have to create a newSerial
222 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
223 $newserial = NewIssue
(
226 $serialdatalist[0]->{'biblionumber'},
234 elsif ( $serialids[$i] ) {
245 my @moditems = $query->param('moditem');
246 if ( scalar(@moditems) ) {
247 my @tags = $query->param('tag');
248 my @subfields = $query->param('subfield');
249 my @field_values = $query->param('field_value');
250 my @serials = $query->param('serial');
251 my @bibnums = $query->param('bibnum');
252 my @itemid = $query->param('itemid');
253 my @ind_tag = $query->param('ind_tag');
254 my @indicator = $query->param('indicator');
256 #Rebuilding ALL the data for items into a hash
257 # parting them on $itemid.
260 my $range = scalar(@itemid);
261 for ( my $i = 0 ; $i < $range ; $i++ ) {
262 unless ( $itemhash{ $itemid[$i] } ) {
263 if ( $serials[$countdistinct]
264 && $serials[$countdistinct] ne "NEW" )
266 $itemhash{ $itemid[$i] }->{'serial'} =
267 $serials[$countdistinct];
270 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
272 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
275 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
276 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
277 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
279 push @
{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
280 push @
{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
282 foreach my $item ( keys %itemhash ) {
284 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
286 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
288 $itemhash{$item}->{serial
} eq $serialids[$i]
289 || ( $itemhash{$item}->{serial
} == $newserial
290 && $serialids[$i] eq 'NEW' )
295 if ( $index >= 0 && $status[$index] == 2 ) {
296 my $xml = TransformHtmlToXml
(
297 $itemhash{$item}->{'tags'},
298 $itemhash{$item}->{'subfields'},
299 $itemhash{$item}->{'field_values'},
300 $itemhash{$item}->{'indicator'},
301 $itemhash{$item}->{'ind_tag'}
305 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
306 if ( $item =~ /^N/ ) {
310 # if autoBarcode is set to 'incremental', calculate barcode...
311 my ( $barcodetagfield, $barcodetagsubfield ) =
312 GetMarcFromKohaField
(
315 $serialdatalist[0]->{'biblionumber'}
318 if ( C4
::Context
->preference('autoBarcode') eq
323 $bib_record->field($barcodetagfield)
324 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
328 my $sth_barcode = $dbh->prepare(
329 'select max(abs(barcode)) from items');
330 $sth_barcode->execute;
331 my ($newbarcode) = $sth_barcode->fetchrow;
333 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
334 $bib_record->field($barcodetagfield)
335 ->update( $barcodetagsubfield => ++$newbarcode );
339 # check for item barcode # being unique
342 $bib_record->subfield(
343 $barcodetagfield, $barcodetagsubfield
347 $exists = GetItemnumberFromBarcode
(
348 $bib_record->subfield(
349 $barcodetagfield, $barcodetagsubfield
354 # push @errors,"barcode_not_unique" if($exists);
355 # if barcode exists, don't create, but report The problem.
357 push @errors, 'barcode_not_unique';
358 push @errseq, { serialseq
=> $serialseqs[$index] };
361 my ( $biblionumber, $bibitemnum, $itemnumber ) =
362 AddItemFromMarc
( $bib_record,
363 $itemhash{$item}->{bibnum
} );
364 AddItem2Serial
( $itemhash{$item}->{serial
},
371 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
372 ModItemFromMarc
( $bib_record,
373 $itemhash{$item}->{'bibnum'}, $item );
381 $template->param( Errors
=> 1 );
383 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
387 my $redirect = 'serials-collection.pl?';
388 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
389 print $query->redirect($redirect);
392 my $location = GetAuthorisedValues
('LOC', $serialdatalist[0]->{'location'});
394 foreach (@
$location) {
395 $locationlib = $_->{'lib'} if $_->{'selected'};
397 my $default_bib_view = get_default_view
();
400 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
401 callnumber
=> $serialdatalist[0]->{'callnumber'},
402 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
403 bibliotitle
=> $biblio->{'title'},
404 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
405 serialslist
=> \
@serialdatalist,
406 default_bib_view
=> $default_bib_view,
407 location
=> $locationlib,
408 (uc(C4
::Context
->preference("marcflavour"))) => 1
411 output_html_with_http_headers
$query, $cookie, $template->output;
413 sub get_default_view
{
414 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
415 my %views = C4
::Search
::enabled_staff_search_views
();
416 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
419 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
422 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
423 return 'labeledMARCdetail';