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 push @serialdatalist, $serinfo;
155 $processedserialid{$serialid} = 1;
158 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
161 my @subscriptionloop;
163 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
164 my %processedsubscriptionid;
165 foreach my $subscriptionid (@subscriptionids) {
167 #Do not process subscriptionid twice if it was already processed.
168 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
171 if ( $serialdatalist[0]->{'serialsadditems'} ) {
173 #Create New empty item
175 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
176 '', GetSubscription($subscriptionid) );
177 $cell->{serialsadditems} = 1;
179 $cell->{'subscriptionid'} = $subscriptionid;
180 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
181 $cell->{'itemid'} = 'NNEW';
182 $cell->{'serialid'} = 'NEW';
183 $cell->{'issuesatonce'} = 1;
184 $cell->{arriveddate}=$today->output('syspref');
186 push @newserialloop, $cell;
187 push @subscriptionloop,
189 'subscriptionid' => $subscriptionid,
190 'abouttoexpire' => abouttoexpire($subscriptionid),
191 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
193 $processedsubscriptionid{$subscriptionid} = 1;
196 $template->param( newserialloop => \@newserialloop );
197 $template->param( subscriptions => \@subscriptionloop );
199 if ( $op and $op eq 'serialchangestatus' ) {
201 # Convert serialseqs to UTF-8 to prevent encoding problems
202 foreach my $seq (@serialseqs) {
203 utf8::decode($seq) unless utf8::is_utf8($seq);
207 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
208 my ($plan_date, $pub_date);
210 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
211 $plan_date = format_date_in_iso( $planneddates[$i] );
213 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
214 $pub_date = format_date_in_iso( $publisheddates[$i] );
217 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
218 if ( $serialseqs[$i] ) {
220 #IF newserial was provided a name Then we have to create a newSerial
221 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
222 $newserial = NewIssue(
225 $serialdatalist[0]->{'biblionumber'},
233 elsif ( $serialids[$i] ) {
244 my @moditems = $query->param('moditem');
245 if ( scalar(@moditems) ) {
246 my @tags = $query->param('tag');
247 my @subfields = $query->param('subfield');
248 my @field_values = $query->param('field_value');
249 my @serials = $query->param('serial');
250 my @bibnums = $query->param('bibnum');
251 my @itemid = $query->param('itemid');
252 my @ind_tag = $query->param('ind_tag');
253 my @indicator = $query->param('indicator');
255 #Rebuilding ALL the data for items into a hash
256 # parting them on $itemid.
259 my $range = scalar(@itemid);
260 for ( my $i = 0 ; $i < $range ; $i++ ) {
261 unless ( $itemhash{ $itemid[$i] } ) {
262 if ( $serials[$countdistinct]
263 && $serials[$countdistinct] ne "NEW" )
265 $itemhash{ $itemid[$i] }->{'serial'} =
266 $serials[$countdistinct];
269 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
271 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
274 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
275 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
276 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
278 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
279 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
281 foreach my $item ( keys %itemhash ) {
283 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
285 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
287 $itemhash{$item}->{serial} eq $serialids[$i]
288 || ( $itemhash{$item}->{serial} == $newserial
289 && $serialids[$i] eq 'NEW' )
294 if ( $index >= 0 && $status[$index] == 2 ) {
295 my $xml = TransformHtmlToXml(
296 $itemhash{$item}->{'tags'},
297 $itemhash{$item}->{'subfields'},
298 $itemhash{$item}->{'field_values'},
299 $itemhash{$item}->{'indicator'},
300 $itemhash{$item}->{'ind_tag'}
304 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
305 if ( $item =~ /^N/ ) {
309 # if autoBarcode is set to 'incremental', calculate barcode...
310 my ( $barcodetagfield, $barcodetagsubfield ) =
311 GetMarcFromKohaField(
314 $serialdatalist[0]->{'biblionumber'}
317 if ( C4::Context->preference('autoBarcode') eq
320 if ( !$bib_record->field($barcodetagfield)
321 ->subfield($barcodetagsubfield) )
323 my $sth_barcode = $dbh->prepare(
324 'select max(abs(barcode)) from items');
325 $sth_barcode->execute;
326 my ($newbarcode) = $sth_barcode->fetchrow;
328 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
329 $bib_record->field($barcodetagfield)
330 ->update( $barcodetagsubfield => ++$newbarcode );
334 # check for item barcode # being unique
337 $bib_record->subfield(
338 $barcodetagfield, $barcodetagsubfield
342 $exists = GetItemnumberFromBarcode(
343 $bib_record->subfield(
344 $barcodetagfield, $barcodetagsubfield
349 # push @errors,"barcode_not_unique" if($exists);
350 # if barcode exists, don't create, but report The problem.
352 push @errors, 'barcode_not_unique';
353 push @errseq, { serialseq => $serialseqs[$index] };
356 my ( $biblionumber, $bibitemnum, $itemnumber ) =
357 AddItemFromMarc( $bib_record,
358 $itemhash{$item}->{bibnum} );
359 AddItem2Serial( $itemhash{$item}->{serial},
366 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
367 ModItemFromMarc( $bib_record,
368 $itemhash{$item}->{'bibnum'}, $item );
376 $template->param( Errors => 1 );
378 $template->param( barcode_not_unique => 1, errseq => \@errseq );
382 my $redirect = 'serials-collection.pl?';
383 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
384 print $query->redirect($redirect);
387 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
389 foreach (@$location) {
390 $locationlib = $_->{'lib'} if $_->{'selected'};
392 my $default_bib_view = get_default_view();
395 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
396 callnumber => $serialdatalist[0]->{'callnumber'},
397 bibliotitle => $biblio->{'title'},
398 biblionumber => $serialdatalist[0]->{'biblionumber'},
399 serialslist => \@serialdatalist,
400 default_bib_view => $default_bib_view,
401 location => $locationlib,
403 output_html_with_http_headers $query, $cookie, $template->output;
405 sub get_default_view {
406 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
407 my %views = C4::Search::enabled_staff_search_views();
408 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
411 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
414 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
415 return 'labeledMARCdetail';