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
66 use Encode
qw( decode is_utf8 );
74 use C4
::Search qw
/enabled_staff_search_views/;
76 use Koha
::Serial
::Items
;
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 = Koha
::Biblios
->find( $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],
253 my $makePreviousSerialAvailable = C4
::Context
->preference('makePreviousSerialAvailable');
254 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
255 # We already have created the new expected serial at this point, so we get the second previous serial
256 my $previous = GetPreviousSerialid
($subscriptionids[$i]);
259 my $serialitem = Koha
::Serial
::Items
->search( {serialid
=> $previous} )->next;
260 my $itemnumber = $serialitem ?
$serialitem->itemnumber : undef;
263 # Getting the itemtype to set from the database
264 my $subscriptioninfos = GetSubscription
($subscriptionids[$i]);
266 # Changing the status to "available" and the itemtype according to the previousitemtype db field
267 ModItem
({notforloan
=> 0, itype
=> $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
273 my @moditems = $query->multi_param('moditem');
274 if ( scalar(@moditems) ) {
275 my @tags = $query->multi_param('tag');
276 my @subfields = $query->multi_param('subfield');
277 my @field_values = $query->multi_param('field_value');
278 my @serials = $query->multi_param('serial');
279 my @bibnums = $query->multi_param('bibnum');
280 my @itemid = $query->multi_param('itemid');
281 my @ind_tag = $query->multi_param('ind_tag');
282 my @indicator = $query->multi_param('indicator');
284 #Rebuilding ALL the data for items into a hash
285 # parting them on $itemid.
288 my $range = scalar(@itemid);
289 for ( my $i = 0 ; $i < $range ; $i++ ) {
290 unless ( $itemhash{ $itemid[$i] } ) {
291 if ( $serials[$countdistinct]
292 && $serials[$countdistinct] ne "NEW" )
294 $itemhash{ $itemid[$i] }->{'serial'} =
295 $serials[$countdistinct];
298 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
300 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
303 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
304 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
305 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
307 push @
{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
308 push @
{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
310 foreach my $item ( keys %itemhash ) {
312 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
314 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
316 $itemhash{$item}->{serial
} eq $serialids[$i]
317 || ( $itemhash{$item}->{serial
} == $newserial
318 && $serialids[$i] eq 'NEW' )
323 if ( $index >= 0 && $status[$index] == 2 ) {
324 my $xml = TransformHtmlToXml
(
325 $itemhash{$item}->{'tags'},
326 $itemhash{$item}->{'subfields'},
327 $itemhash{$item}->{'field_values'},
328 $itemhash{$item}->{'indicator'},
329 $itemhash{$item}->{'ind_tag'}
333 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
334 if ( $item =~ /^N/ ) {
338 # if autoBarcode is set to 'incremental', calculate barcode...
339 my ( $barcodetagfield, $barcodetagsubfield ) =
340 GetMarcFromKohaField
(
343 $serialdatalist[0]->{'biblionumber'}
346 if ( C4
::Context
->preference('autoBarcode') eq
351 $bib_record->field($barcodetagfield)
352 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
356 my $sth_barcode = $dbh->prepare(
357 'select max(abs(barcode)) from items');
358 $sth_barcode->execute;
359 my ($newbarcode) = $sth_barcode->fetchrow;
361 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
362 $bib_record->field($barcodetagfield)
363 ->update( $barcodetagsubfield => ++$newbarcode );
367 # check for item barcode # being unique
370 $bib_record->subfield(
371 $barcodetagfield, $barcodetagsubfield
375 $exists = GetItemnumberFromBarcode
(
376 $bib_record->subfield(
377 $barcodetagfield, $barcodetagsubfield
382 # push @errors,"barcode_not_unique" if($exists);
383 # if barcode exists, don't create, but report The problem.
385 push @errors, 'barcode_not_unique';
386 push @errseq, { serialseq
=> $serialseqs[$index] };
389 my ( $biblionumber, $bibitemnum, $itemnumber ) =
390 AddItemFromMarc
( $bib_record,
391 $itemhash{$item}->{bibnum
} );
392 AddItem2Serial
( $itemhash{$item}->{serial
},
399 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
400 ModItemFromMarc
( $bib_record,
401 $itemhash{$item}->{'bibnum'}, $item );
409 $template->param( Errors
=> 1 );
411 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
415 my $redirect = 'serials-collection.pl?';
416 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
417 print $query->redirect($redirect);
420 my $location = $serialdatalist[0]->{'location'};
421 my $default_bib_view = get_default_view
();
424 subscriptionid
=> $serialdatalist[0]->{subscriptionid
},
425 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
426 callnumber
=> $serialdatalist[0]->{'callnumber'},
427 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
428 bibliotitle
=> $biblio->title,
429 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
430 serialslist
=> \
@serialdatalist,
431 default_bib_view
=> $default_bib_view,
432 location
=> $location,
433 (uc(C4
::Context
->preference("marcflavour"))) => 1
436 output_html_with_http_headers
$query, $cookie, $template->output;
438 sub get_default_view
{
439 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
440 my %views = C4
::Search
::enabled_staff_search_views
();
441 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
444 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
447 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
448 return 'labeledMARCdetail';