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 flagsrequired
=> { serials
=> 'receive_serials' },
131 my %processedserialid;
133 my $today = output_pref
( { dt
=> dt_from_string
, dateonly
=> 1 } );
135 foreach my $serialid (@serialids) {
137 #filtering serialid for duplication
138 #NEW serial should appear only once and are created afterwards
140 && $serialid =~ /^[0-9]+$/
141 && !$processedserialid{$serialid} )
143 my $serinfo = GetSerialInformation
($serialid); #TODO duplicates work done by GetSerials2 above
145 for my $d ( qw( publisheddate planneddate )){
146 if ( $serinfo->{$d} =~m/^00/ ) {
147 $serinfo->{$d} = q{};
150 $serinfo->{$d} = output_pref
( { dt
=> dt_from_string
( $serinfo->{$d} ), dateonly
=> 1 } );
153 $serinfo->{arriveddate
} = $today;
155 $serinfo->{'editdisable'} = (
157 HasSubscriptionExpired
( $serinfo->{subscriptionid
} )
158 && $serinfo->{'status1'}
160 || $serinfo->{'cannotedit'}
162 $serinfo->{editdisable
} = 0 if C4
::Auth
::haspermission
( C4
::Context
->userenv->{id
}, { serials
=> 'receive_serials' } );
163 $serinfo->{editdisable
} ||= ($serinfo->{status8
} and $serinfo->{closed
});
164 push @serialdatalist, $serinfo;
165 $processedserialid{$serialid} = 1;
168 my $biblio = Koha
::Biblios
->find( $serialdatalist[0]->{biblionumber
} );
171 my @subscriptionloop;
173 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
174 my %processedsubscriptionid;
175 foreach my $subscriptionid (@subscriptionids) {
177 #Do not process subscriptionid twice if it was already processed.
178 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
181 if ( $serialdatalist[0]->{'serialsadditems'} ) {
183 #Create New empty item
185 PrepareItemrecordDisplay
( $serialdatalist[0]->{'biblionumber'},
186 '', GetSubscription
($subscriptionid) );
187 $cell->{serialsadditems
} = 1;
189 $cell->{'subscriptionid'} = $subscriptionid;
190 $cell->{biblionumber
} = $serialdatalist[0]->{'biblionumber'};
191 $cell->{'itemid'} = 'NNEW';
192 $cell->{'serialid'} = 'NEW';
193 $cell->{'issuesatonce'} = 1;
194 $cell->{arriveddate
} = $today;
196 push @newserialloop, $cell;
197 push @subscriptionloop,
199 'subscriptionid' => $subscriptionid,
200 'abouttoexpire' => abouttoexpire
($subscriptionid),
201 'subscriptionexpired' => HasSubscriptionExpired
($subscriptionid),
203 $processedsubscriptionid{$subscriptionid} = 1;
206 $template->param( newserialloop
=> \
@newserialloop );
207 $template->param( subscriptions
=> \
@subscriptionloop );
209 if ( $op and $op eq 'serialchangestatus' ) {
211 # Convert serialseqs to UTF-8 to prevent encoding problems
212 foreach my $seq (@serialseqs) {
213 $seq = Encode
::decode
('UTF-8', $seq) unless Encode
::is_utf8
($seq);
217 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
218 my ($plan_date, $pub_date);
220 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
221 $plan_date = eval { output_pref
( { dt
=> dt_from_string
( $planneddates[$i] ), dateonly
=> 1, dateformat
=> 'iso' } ); };
223 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
224 $pub_date = eval { output_pref
( { dt
=> dt_from_string
( $publisheddates[$i] ), dateonly
=> 1, dateformat
=> 'iso' } ); };
227 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
228 if ( $serialseqs[$i] ) {
230 #IF newserial was provided a name Then we have to create a newSerial
231 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
232 $newserial = NewIssue
(
235 $serialdatalist[0]->{'biblionumber'},
239 $publisheddatetexts[$i],
241 $serialdatalist[0]->{'routingnotes'}
245 elsif ( $serialids[$i] ) {
251 $publisheddatetexts[$i],
256 my $makePreviousSerialAvailable = C4
::Context
->preference('makePreviousSerialAvailable');
257 if ($makePreviousSerialAvailable && $serialids[$i] ne "NEW") {
258 # We already have created the new expected serial at this point, so we get the second previous serial
259 my $previous = GetPreviousSerialid
($subscriptionids[$i]);
262 my $serialitem = Koha
::Serial
::Items
->search( {serialid
=> $previous} )->next;
263 my $itemnumber = $serialitem ?
$serialitem->itemnumber : undef;
266 # Getting the itemtype to set from the database
267 my $subscriptioninfos = GetSubscription
($subscriptionids[$i]);
269 # Changing the status to "available" and the itemtype according to the previousitemtype db field
273 itype
=> $subscriptioninfos->{'previousitemtype'}
281 my @moditems = $query->multi_param('moditem');
282 if ( scalar(@moditems) ) {
283 my @tags = $query->multi_param('tag');
284 my @subfields = $query->multi_param('subfield');
285 my @field_values = $query->multi_param('field_value');
286 my @serials = $query->multi_param('serial');
287 my @bibnums = $query->multi_param('bibnum');
288 my @itemid = $query->multi_param('itemid');
289 my @ind_tag = $query->multi_param('ind_tag');
290 my @indicator = $query->multi_param('indicator');
291 my @num_copies = $query->multi_param('number_of_copies');
293 #Rebuilding ALL the data for items into a hash
294 # parting them on $itemid.
296 my $countdistinct = 0;
297 my $range = scalar(@itemid);
298 for ( my $i = 0 ; $i < $range ; $i++ ) {
299 unless ( $itemhash{ $itemid[$i] } ) {
300 if ( $serials[$countdistinct]
301 && $serials[$countdistinct] ne "NEW" )
303 $itemhash{ $itemid[$i] }->{'serial'} =
304 $serials[$countdistinct];
307 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
309 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
310 $itemhash{ $itemid[$i] }->{'num_copies'} = $num_copies[$countdistinct];
313 push @
{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
314 push @
{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
315 push @
{ $itemhash{ $itemid[$i] }->{'field_values'} },
317 push @
{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
318 push @
{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
320 foreach my $item ( keys %itemhash ) {
322 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
324 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
326 $itemhash{$item}->{serial
} eq $serialids[$i]
327 || ( $itemhash{$item}->{serial
} == $newserial
328 && $serialids[$i] eq 'NEW' )
333 if ( $index >= 0 && $status[$index] == 2 ) {
334 my $xml = TransformHtmlToXml
(
335 $itemhash{$item}->{'tags'},
336 $itemhash{$item}->{'subfields'},
337 $itemhash{$item}->{'field_values'},
338 $itemhash{$item}->{'indicator'},
339 $itemhash{$item}->{'ind_tag'}
343 my $bib_record = MARC
::Record
::new_from_xml
( $xml, 'UTF-8' );
344 if ( $item =~ /^N/ ) {
346 $itemhash{$item}->{'num_copies'} //= 1;
348 for (my $copy = 0; $copy < $itemhash{$item}->{'num_copies'};){
352 # if autoBarcode is set to 'incremental', calculate barcode...
353 my ( $barcodetagfield, $barcodetagsubfield ) = GetMarcFromKohaField
( 'items.barcode' );
354 if ( C4
::Context
->preference('autoBarcode') eq
359 $bib_record->field($barcodetagfield)
360 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
364 my $sth_barcode = $dbh->prepare(
365 'select max(abs(barcode)) from items');
366 $sth_barcode->execute;
367 my ($newbarcode) = $sth_barcode->fetchrow;
369 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
370 $bib_record->field($barcodetagfield)->update( $barcodetagsubfield => ++$newbarcode );
374 # check for item barcode # being unique
376 if ( $bib_record->subfield( $barcodetagfield, $barcodetagsubfield ) ) {
377 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
381 my $barcodeobj = C4
::Barcodes
->new;
382 my $newbarcode = $barcodeobj->next_value($barcode);
383 $barcode = $newbarcode;
384 $bib_record->field($barcodetagfield)->update($barcodetagsubfield => $barcode);
387 $exists = Koha
::Items
->find({barcode
=> $barcode});
390 # push @errors,"barcode_not_unique" if($exists);
391 # if barcode exists, don't create, but report The problem.
393 push @errors, 'barcode_not_unique';
394 push @errseq, { serialseq
=> $serialseqs[$index] };
397 my ( $biblionumber, $bibitemnum, $itemnumber ) =
398 AddItemFromMarc
( $bib_record,
399 $itemhash{$item}->{bibnum
} );
400 AddItem2Serial
( $itemhash{$item}->{serial
},
406 } # if ( $item =~ /^N/ ) {
411 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
412 ModItemFromMarc
( $bib_record,
413 $itemhash{$item}->{'bibnum'}, $item );
421 $template->param( Errors
=> 1 );
423 $template->param( barcode_not_unique
=> 1, errseq
=> \
@errseq );
427 my $redirect = 'serials-collection.pl?';
428 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
429 print $query->redirect($redirect);
432 my $location = $serialdatalist[0]->{'location'};
433 my $default_bib_view = get_default_view
();
436 subscriptionid
=> $serialdatalist[0]->{subscriptionid
},
437 serialsadditems
=> $serialdatalist[0]->{'serialsadditems'},
438 callnumber
=> $serialdatalist[0]->{'callnumber'},
439 internalnotes
=> $serialdatalist[0]->{'internalnotes'},
440 bibliotitle
=> $biblio->title,
441 biblionumber
=> $serialdatalist[0]->{'biblionumber'},
442 serialslist
=> \
@serialdatalist,
443 default_bib_view
=> $default_bib_view,
444 location
=> $location,
445 (uc(C4
::Context
->preference("marcflavour"))) => 1
448 output_html_with_http_headers
$query, $cookie, $template->output;
450 sub get_default_view
{
451 my $defaultview = C4
::Context
->preference('IntranetBiblioDefaultView');
452 my %views = C4
::Search
::enabled_staff_search_views
();
453 if ( $defaultview eq 'isbd' && $views{can_view_ISBD
} ) {
456 elsif ( $defaultview eq 'marc' && $views{can_view_MARC
} ) {
459 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC
} ) {
460 return 'labeledMARCdetail';