Bug 5187: Show place of publication (MARC21, 260$c), XSLT
[koha.git] / serials / serials-edit.pl
blobdee624c73eb6e3c7a6e52ae91f4dd0741ae69647
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 =head1 NAME
22 serials-recieve.pl
24 =head1 Parameters
26 =over 4
28 =item op
29 op can be :
30 * modsubscriptionhistory :to modify the subscription history
31 * serialchangestatus :to modify the status of this subscription
33 =item subscriptionid
35 =item user
37 =item histstartdate
39 =item enddate
41 =item recievedlist
43 =item missinglist
45 =item opacnote
47 =item librariannote
49 =item serialid
51 =item serialseq
53 =item planneddate
55 =item notes
57 =item status
59 =back
61 =cut
63 use strict;
64 use warnings;
65 use CGI;
66 use C4::Auth;
67 use C4::Dates qw/format_date format_date_in_iso/;
68 use C4::Biblio;
69 use C4::Items;
70 use C4::Koha;
71 use C4::Output;
72 use C4::Context;
73 use C4::Serials;
75 my $query = CGI->new();
76 my $dbh = C4::Context->dbh;
77 my @serialids = $query->param('serialid');
78 my @serialseqs = $query->param('serialseq');
79 my @planneddates = $query->param('planneddate');
80 my @publisheddates = $query->param('publisheddate');
81 my @status = $query->param('status');
82 my @notes = $query->param('notes');
83 my @subscriptionids = $query->param('subscriptionid');
84 my $op = $query->param('op');
85 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
86 @subscriptionids = split( /,/, $subscriptionids[0] );
88 my @errors;
89 my @errseq;
91 # If user comes from subscription details
92 unless (@serialids) {
93 foreach my $subscriptionid (@subscriptionids) {
94 my $serstatus = $query->param('serstatus');
95 if ($serstatus) {
96 my @tmpser = GetSerials2( $subscriptionid, $serstatus );
97 foreach (@tmpser) {
98 push @serialids, $_->{'serialid'};
104 unless ( scalar(@serialids) ) {
105 my $string =
106 "serials-collection.pl?subscriptionid=" . join( ",", @subscriptionids );
107 $string =~ s/,$//;
109 print $query->redirect($string);
112 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
114 template_name => "serials/serials-edit.tmpl",
115 query => $query,
116 type => "intranet",
117 authnotrequired => 0,
118 flagsrequired => {serials => 'receive_serials'},
119 debug => 1,
123 my @serialdatalist;
124 my %processedserialid;
126 my $today = C4::Dates->new();
127 foreach my $tmpserialid (@serialids) {
129 #filtering serialid for duplication
130 #NEW serial should appear only once and are created afterwards
131 if ( defined($tmpserialid)
132 && $tmpserialid =~ /^[0-9]+$/
133 && !$processedserialid{$tmpserialid} )
135 my $data = GetSerialInformation($tmpserialid);
136 $data->{publisheddate} = format_date( $data->{publisheddate} );
137 $data->{planneddate} = format_date( $data->{planneddate} );
138 $data->{arriveddate}=$today->output('syspref');
139 $data->{'editdisable'} = (
141 HasSubscriptionExpired( $data->{subscriptionid} )
142 && $data->{'status1'}
144 || $data->{'cannotedit'}
146 push @serialdatalist, $data;
147 $processedserialid{$tmpserialid} = 1;
150 my $bibdata = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
152 my @newserialloop;
153 my @subscriptionloop;
155 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
156 my %processedsubscriptionid;
157 foreach my $subscriptionid (@subscriptionids) {
159 #Do not process subscriptionid twice if it was already processed.
160 if ( defined($subscriptionid)
161 && !$processedsubscriptionid{$subscriptionid} )
163 my $cell;
164 if ( $serialdatalist[0]->{'serialsadditems'} ) {
166 #Create New empty item
167 $cell =
168 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
169 '', GetSubscription($subscriptionid) );
170 $cell->{serialsadditems} = 1;
172 $cell->{'subscriptionid'} = $subscriptionid;
173 $cell->{'itemid'} = "NNEW";
174 $cell->{'serialid'} = "NEW";
175 $cell->{'issuesatonce'} = 1;
176 push @newserialloop, $cell;
177 push @subscriptionloop,
179 'subscriptionid' => $subscriptionid,
180 'abouttoexpire' => abouttoexpire($subscriptionid),
181 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
183 $processedsubscriptionid{$subscriptionid} = 1;
186 $template->param( newserialloop => \@newserialloop );
187 $template->param( subscriptions => \@subscriptionloop );
189 if ( $op and $op eq 'serialchangestatus' ) {
191 my $newserial;
192 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
194 if ( $serialids[$i] && $serialids[$i] eq "NEW" ) {
195 if ( $serialseqs[$i] ) {
197 #IF newserial was provided a name Then we have to create a newSerial
198 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
199 $newserial = NewIssue(
200 $serialseqs[$i],
201 $subscriptionids[$i],
202 $serialdatalist[0]->{'biblionumber'},
203 $status[$i],
204 format_date_in_iso( $planneddates[$i] ),
205 format_date_in_iso( $publisheddates[$i] ),
206 $notes[$i]
210 elsif ( $serialids[$i] ) {
211 ModSerialStatus(
212 $serialids[$i],
213 $serialseqs[$i],
214 format_date_in_iso( $planneddates[$i] ),
215 format_date_in_iso( $publisheddates[$i] ),
216 $status[$i],
217 $notes[$i]
221 my @moditems = $query->param('moditem');
222 if ( scalar(@moditems) ) {
223 my @tags = $query->param('tag');
224 my @subfields = $query->param('subfield');
225 my @field_values = $query->param('field_value');
226 my @serials = $query->param('serial');
227 my @bibnums = $query->param('bibnum');
228 my @itemid = $query->param('itemid');
229 my @ind_tag = $query->param('ind_tag');
230 my @indicator = $query->param('indicator');
232 #Rebuilding ALL the data for items into a hash
233 # parting them on $itemid.
234 my %itemhash;
235 my $countdistinct;
236 my $range = scalar(@itemid);
237 for ( my $i = 0 ; $i < $range ; $i++ ) {
238 unless ( $itemhash{ $itemid[$i] } ) {
239 if ( $serials[$countdistinct]
240 && $serials[$countdistinct] ne "NEW" )
242 $itemhash{ $itemid[$i] }->{'serial'} =
243 $serials[$countdistinct];
245 else {
246 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
248 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
249 $countdistinct++;
251 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
252 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
253 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
254 $field_values[$i];
255 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
256 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
258 foreach my $item ( keys %itemhash ) {
260 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
261 my $index = -1;
262 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
263 if (
264 $itemhash{$item}->{serial} eq $serialids[$i]
265 || ( $itemhash{$item}->{serial} == $newserial
266 && $serialids[$i] eq 'NEW' )
268 $index = $i
271 if ( $index >= 0 && $status[$index] == 2 ) {
272 my $xml = TransformHtmlToXml(
273 $itemhash{$item}->{'tags'},
274 $itemhash{$item}->{'subfields'},
275 $itemhash{$item}->{'field_values'},
276 $itemhash{$item}->{'indicator'},
277 $itemhash{$item}->{'ind_tag'}
280 # warn $xml;
281 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
282 if ( $item =~ /^N/ ) {
284 #New Item
286 # if autoBarcode is set to 'incremental', calculate barcode...
287 my ( $barcodetagfield, $barcodetagsubfield ) =
288 GetMarcFromKohaField(
289 'items.barcode',
290 GetFrameworkCode(
291 $serialdatalist[0]->{'biblionumber'}
294 if ( C4::Context->preference("autoBarcode") eq
295 'incremental' )
297 if ( !$bib_record->field($barcodetagfield)
298 ->subfield($barcodetagsubfield) )
300 my $sth_barcode = $dbh->prepare(
301 "select max(abs(barcode)) from items");
302 $sth_barcode->execute;
303 my ($newbarcode) = $sth_barcode->fetchrow;
305 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
306 $bib_record->field($barcodetagfield)
307 ->update( $barcodetagsubfield => ++$newbarcode );
311 # check for item barcode # being unique
312 my $exists;
313 if (
314 $bib_record->subfield(
315 $barcodetagfield, $barcodetagsubfield
319 $exists = GetItemnumberFromBarcode(
320 $bib_record->subfield(
321 $barcodetagfield, $barcodetagsubfield
326 # push @errors,"barcode_not_unique" if($exists);
327 # if barcode exists, don't create, but report The problem.
328 if ($exists) {
329 push @errors, 'barcode_not_unique';
330 push @errseq, { serialseq => $serialseqs[$index] };
332 else {
333 my ( $biblionumber, $bibitemnum, $itemnumber ) =
334 AddItemFromMarc( $bib_record,
335 $itemhash{$item}->{bibnum} );
336 AddItem2Serial( $itemhash{$item}->{serial},
337 $itemnumber );
340 else {
342 #modify item
343 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
344 ModItemFromMarc( $bib_record,
345 $itemhash{$item}->{'bibnum'}, $item );
352 if ( @errors ) {
353 $template->param( Errors => 1 );
354 if ( @errseq ) {
355 $template->param( barcode_not_unique => 1, errseq => \@errseq );
358 else {
359 my $redirect = "serials-collection.pl?";
360 $redirect .= join( '&', map { "subscriptionid=" . $_ } sort @subscriptionids );# ID The sort necessary
361 print $query->redirect($redirect);
364 my $default_bib_view = get_default_view();
366 $template->param(
367 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
368 bibliotitle => $bibdata->{'title'},
369 biblionumber => $serialdatalist[0]->{'biblionumber'},
370 serialslist => \@serialdatalist,
371 default_bib_view => $default_bib_view,
373 output_html_with_http_headers $query, $cookie, $template->output;
375 sub get_default_view {
376 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
377 my $views = { C4::Search::enabled_staff_search_views };
378 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
379 return 'ISBDdetail';
380 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
381 return 'MARCdetail';
382 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
383 return 'labeledMARCdetail';
384 } else {
385 return 'detail';