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/;
78 use Koha
::Serial
::Items
;
80 use List
::MoreUtils qw
/uniq/;
82 my $query = CGI
->new();
83 my $dbh = C4
::Context
->dbh;
84 my @serialids = $query->multi_param('serialid');
85 my @serialseqs = $query->multi_param('serialseq');
86 my @planneddates = $query->multi_param('planneddate');
87 my @publisheddates = $query->multi_param('publisheddate');
88 my @publisheddatetexts = $query->multi_param('publisheddatetext');
89 my @status = $query->multi_param('status');
90 my @notes = $query->multi_param('notes');
91 my @subscriptionids = $query->multi_param('subscriptionid');
92 my $op = $query->param('op');
93 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q
|,| ) > 0 ) {
94 @subscriptionids = split( /,/, $subscriptionids[0] );
99 # If user comes from subscription details
100 unless (@serialids) {
101 my $serstatus = $query->param('serstatus');
102 my @statuses = split ',', $serstatus;
104 foreach my $subscriptionid (@subscriptionids) {
105 my @tmpser = GetSerials2
( $subscriptionid, \
@statuses );
106 push @serialids, map { $_->{serialid
} } @tmpser;
111 unless ( @serialids ) {
113 'serials-collection.pl?subscriptionid=' . join ',', uniq
@subscriptionids;
116 print $query->redirect($string);
120 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
122 template_name
=> 'serials/serials-edit.tt',
125 authnotrequired
=> 0,
126 flagsrequired
=> { serials
=> 'receive_serials' },
132 my %processedserialid;
134 my $today = output_pref
( { dt
=> dt_from_string
, dateonly
=> 1 } );
136 foreach my $serialid (@serialids) {
138 #filtering serialid for duplication
139 #NEW serial should appear only once and are created afterwards
141 && $serialid =~ /^[0-9]+$/
142 && !$processedserialid{$serialid} )
144 my $serinfo = GetSerialInformation
($serialid); #TODO duplicates work done by GetSerials2 above
146 for my $d ( qw( publisheddate planneddate )){
147 if ( $serinfo->{$d} =~m/^00/ ) {
148 $serinfo->{$d} = q{};
151 $serinfo->{$d} = output_pref
( { dt
=> dt_from_string
( $serinfo->{$d} ), dateonly
=> 1 } );
154 $serinfo->{arriveddate
} = $today;
156 $serinfo->{'editdisable'} = (
158 HasSubscriptionExpired
( $serinfo->{subscriptionid
} )
159 && $serinfo->{'status1'}
161 || $serinfo->{'cannotedit'}
163 $serinfo->{editdisable
} = 0 if C4
::Auth
::haspermission
( C4
::Context
->userenv->{id
}, { serials
=> 'receive_serials' } );
164 $serinfo->{editdisable
} ||= ($serinfo->{status8
} and $serinfo->{closed
});
165 push @serialdatalist, $serinfo;
166 $processedserialid{$serialid} = 1;
169 my $biblio = Koha
::Biblios
->find( $serialdatalist[0]->{biblionumber
} );
172 my @subscriptionloop;
174 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
175 my %processedsubscriptionid;
176 foreach my $subscriptionid (@subscriptionids) {
178 #Do not process subscriptionid twice if it was already processed.
179 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
182 if ( $serialdatalist[0]->{'serialsadditems'} ) {
184 #Create New empty item
186 PrepareItemrecordDisplay
( $serialdatalist[0]->{'biblionumber'},
187 '', GetSubscription
($subscriptionid) );
188 $cell->{serialsadditems
} = 1;
190 $cell->{'subscriptionid'} = $subscriptionid;
191 $cell->{biblionumber
} = $serialdatalist[0]->{'biblionumber'};
192 $cell->{'itemid'} = 'NNEW';
193 $cell->{'serialid'} = 'NEW';
194 $cell->{'issuesatonce'} = 1;
195 $cell->{arriveddate
} = $today;
197 push @newserialloop, $cell;
198 push @subscriptionloop,
200 'subscriptionid' => $subscriptionid,
201 'abouttoexpire' => abouttoexpire
($subscriptionid),
202 'subscriptionexpired' => HasSubscriptionExpired
($subscriptionid),
204 $processedsubscriptionid{$subscriptionid} = 1;
207 $template->param( newserialloop
=> \
@newserialloop );
208 $template->param( subscriptions
=> \
@subscriptionloop );
210 if ( $op and $op eq 'serialchangestatus' ) {
212 # Convert serialseqs to UTF-8 to prevent encoding problems
213 foreach my $seq (@serialseqs) {
214 $seq = Encode
::decode
('UTF-8', $seq) unless Encode
::is_utf8
($seq);
218 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
219 my ($plan_date, $pub_date);
221 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
222 $plan_date = eval { output_pref
( { dt
=> dt_from_string
( $planneddates[$i] ), dateonly
=> 1, dateformat
=> 'iso' } ); };
224 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
225 $pub_date = eval { output_pref
( { dt
=> dt_from_string
( $publisheddates[$i] ), dateonly
=> 1, dateformat
=> 'iso' } ); };
228 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
229 if ( $serialseqs[$i] ) {
231 #IF newserial was provided a name Then we have to create a newSerial
232 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
233 $newserial = NewIssue
(
236 $serialdatalist[0]->{'biblionumber'},
240 $publisheddatetexts[$i],
242 $serialdatalist[0]->{'routingnotes'}
246 elsif ( $serialids[$i] ) {
252 $publisheddatetexts[$i],
257 my $makePreviousSerialAvailable = C4
::Context
->preference('makePreviousSerialAvailable');
258 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
259 # We already have created the new expected serial at this point, so we get the second previous serial
260 my $previous = GetPreviousSerialid
($subscriptionids[$i]);
263 my $serialitem = Koha
::Serial
::Items
->search( {serialid
=> $previous} )->next;
264 my $itemnumber = $serialitem ?
$serialitem->itemnumber : undef;
267 # Getting the itemtype to set from the database
268 my $subscriptioninfos = GetSubscription
($subscriptionids[$i]);
270 # Changing the status to "available" and the itemtype according to the previousitemtype db field
271 ModItem
({notforloan
=> 0, itype
=> $subscriptioninfos->{'previousitemtype'} }, undef, $itemnumber);
277 my @moditems = $query->multi_param('moditem');
278 if ( scalar(@moditems) ) {
279 my @tags = $query->multi_param('tag');
280 my @subfields = $query->multi_param('subfield');
281 my @field_values = $query->multi_param('field_value');
282 my @serials = $query->multi_param('serial');
283 my @bibnums = $query->multi_param('bibnum');
284 my @itemid = $query->multi_param('itemid');
285 my @ind_tag = $query->multi_param('ind_tag');
286 my @indicator = $query->multi_param('indicator');
288 #Rebuilding ALL the data for items into a hash
289 # parting them on $itemid.
292 my $range = scalar(@itemid);
293 for ( my $i = 0 ; $i < $range ; $i++ ) {
294 unless ( $itemhash{ $itemid[$i] } ) {
295 if ( $serials[$countdistinct]
296 && $serials[$countdistinct] ne "NEW" )
298 $itemhash{ $itemid[$i] }->{'serial'} =
299 $serials[$countdistinct];
302 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
304 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
307 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
308 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
309 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
311 push @
{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
312 push @
{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
314 foreach my $item ( keys %itemhash ) {
316 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
318 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
320 $itemhash{$item}->{serial
} eq $serialids[$i]
321 || ( $itemhash{$item}->{serial
} == $newserial
322 && $serialids[$i] eq 'NEW' )
327 if ( $index >= 0 && $status[$index] == 2 ) {
328 my $xml = TransformHtmlToXml
(
329 $itemhash{$item}->{'tags'},
330 $itemhash{$item}->{'subfields'},
331 $itemhash{$item}->{'field_values'},
332 $itemhash{$item}->{'indicator'},
333 $itemhash{$item}->{'ind_tag'}
337 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
338 if ( $item =~ /^N/ ) {
342 # if autoBarcode is set to 'incremental', calculate barcode...
343 my ( $barcodetagfield, $barcodetagsubfield ) =
344 GetMarcFromKohaField
(
347 $serialdatalist[0]->{'biblionumber'}
350 if ( C4
::Context
->preference('autoBarcode') eq
355 $bib_record->field($barcodetagfield)
356 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
360 my $sth_barcode = $dbh->prepare(
361 'select max(abs(barcode)) from items');
362 $sth_barcode->execute;
363 my ($newbarcode) = $sth_barcode->fetchrow;
365 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
366 $bib_record->field($barcodetagfield)
367 ->update( $barcodetagsubfield => ++$newbarcode );
371 # check for item barcode # being unique
374 $bib_record->subfield(
375 $barcodetagfield, $barcodetagsubfield
379 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
380 $exists = Koha
::Items
->find({barcode
=> $barcode});
383 # push @errors,"barcode_not_unique" if($exists);
384 # if barcode exists, don't create, but report The problem.
386 push @errors, 'barcode_not_unique';
387 push @errseq, { serialseq
=> $serialseqs[$index] };
390 my ( $biblionumber, $bibitemnum, $itemnumber ) =
391 AddItemFromMarc
( $bib_record,
392 $itemhash{$item}->{bibnum
} );
393 AddItem2Serial
( $itemhash{$item}->{serial
},
400 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
401 ModItemFromMarc
( $bib_record,
402 $itemhash{$item}->{'bibnum'}, $item );
410 $template->param( Errors
=> 1 );
412 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
416 my $redirect = 'serials-collection.pl?';
417 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
418 print $query->redirect($redirect);
421 my $location = $serialdatalist[0]->{'location'};
422 my $default_bib_view = get_default_view
();
425 subscriptionid
=> $serialdatalist[0]->{subscriptionid
},
426 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
427 callnumber
=> $serialdatalist[0]->{'callnumber'},
428 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
429 bibliotitle
=> $biblio->title,
430 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
431 serialslist
=> \
@serialdatalist,
432 default_bib_view
=> $default_bib_view,
433 location
=> $location,
434 (uc(C4
::Context
->preference("marcflavour"))) => 1
437 output_html_with_http_headers
$query, $cookie, $template->output;
439 sub get_default_view
{
440 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
441 my %views = C4
::Search
::enabled_staff_search_views
();
442 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
445 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
448 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
449 return 'labeledMARCdetail';