Bug 6958 : Fix for leading spaces being stripped on translation
[koha.git] / serials / serials-edit.pl
blob9ecd09fe91ed08e9edb885fc6e8797dc0e606bd0
1 #!/usr/bin/perl
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
11 # version.
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.
21 =head1 NAME
23 serials-edit.pl
25 =head1 Parameters
27 =over 4
29 =item op
30 op can be :
31 * modsubscriptionhistory :to modify the subscription history
32 * serialchangestatus :to modify the status of this subscription
34 =item subscriptionid
36 =item user
38 =item histstartdate
40 =item enddate
42 =item recievedlist
44 =item missinglist
46 =item opacnote
48 =item librariannote
50 =item serialid
52 =item serialseq
54 =item planneddate
56 =item notes
58 =item status
60 =back
62 =cut
64 use strict;
65 use warnings;
66 use CGI;
67 use C4::Auth;
68 use C4::Dates qw/format_date format_date_in_iso/;
69 use C4::Biblio;
70 use C4::Items;
71 use C4::Koha;
72 use C4::Output;
73 use C4::Context;
74 use C4::Serials;
75 use List::MoreUtils qw/uniq/;
77 my $query = CGI->new();
78 my $dbh = C4::Context->dbh;
79 my @serialids = $query->param('serialid');
80 my @serialseqs = $query->param('serialseq');
81 my @planneddates = $query->param('planneddate');
82 my @publisheddates = $query->param('publisheddate');
83 my @status = $query->param('status');
84 my @notes = $query->param('notes');
85 my @subscriptionids = $query->param('subscriptionid');
86 my $op = $query->param('op');
87 if ( scalar(@subscriptionids) == 1 && index( $subscriptionids[0], q|,| ) > 0 ) {
88 @subscriptionids = split( /,/, $subscriptionids[0] );
90 my @errors;
91 my @errseq;
93 # If user comes from subscription details
94 unless (@serialids) {
95 my $serstatus = $query->param('serstatus');
96 if ($serstatus) {
97 foreach my $subscriptionid (@subscriptionids) {
98 my @tmpser = GetSerials2( $subscriptionid, $serstatus );
99 push @serialids, map { $_->{serialid} } @tmpser;
104 unless ( @serialids ) {
105 my $string =
106 'serials-collection.pl?subscriptionid=' . join ',', uniq @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 => 1 },
119 debug => 1,
123 my @serialdatalist;
124 my %processedserialid;
126 my $today = C4::Dates->new();
127 foreach my $serialid (@serialids) {
129 #filtering serialid for duplication
130 #NEW serial should appear only once and are created afterwards
131 if ( $serialid
132 && $serialid =~ /^[0-9]+$/
133 && !$processedserialid{$serialid} )
135 my $serinfo = GetSerialInformation($serialid); #TODO duplicates work done by GetSerials2 above
136 for my $d ( qw( publisheddate planneddate )){
137 if ( $serinfo->{$d} =~m/^00/ ) {
138 $serinfo->{$d} = q{};
140 else {
141 $serinfo->{$d} = format_date( $serinfo->{$d} );
144 $serinfo->{arriveddate}=$today->output('syspref');
146 $serinfo->{'editdisable'} = (
148 HasSubscriptionExpired( $serinfo->{subscriptionid} )
149 && $serinfo->{'status1'}
151 || $serinfo->{'cannotedit'}
153 push @serialdatalist, $serinfo;
154 $processedserialid{$serialid} = 1;
157 my $biblio = GetBiblioData( $serialdatalist[0]->{'biblionumber'} );
159 my @newserialloop;
160 my @subscriptionloop;
162 # check, for each subscription edited, that we have an empty item line if applicable for the subscription
163 my %processedsubscriptionid;
164 foreach my $subscriptionid (@subscriptionids) {
166 #Do not process subscriptionid twice if it was already processed.
167 if ( $subscriptionid && !$processedsubscriptionid{$subscriptionid} )
169 my $cell;
170 if ( $serialdatalist[0]->{'serialsadditems'} ) {
172 #Create New empty item
173 $cell =
174 PrepareItemrecordDisplay( $serialdatalist[0]->{'biblionumber'},
175 '', GetSubscription($subscriptionid) );
176 $cell->{serialsadditems} = 1;
178 $cell->{'subscriptionid'} = $subscriptionid;
179 $cell->{biblionumber} = $serialdatalist[0]->{'biblionumber'};
180 $cell->{'itemid'} = 'NNEW';
181 $cell->{'serialid'} = 'NEW';
182 $cell->{'issuesatonce'} = 1;
183 $cell->{arriveddate}=$today->output('syspref');
185 push @newserialloop, $cell;
186 push @subscriptionloop,
188 'subscriptionid' => $subscriptionid,
189 'abouttoexpire' => abouttoexpire($subscriptionid),
190 'subscriptionexpired' => HasSubscriptionExpired($subscriptionid),
192 $processedsubscriptionid{$subscriptionid} = 1;
195 $template->param( newserialloop => \@newserialloop );
196 $template->param( subscriptions => \@subscriptionloop );
198 if ( $op and $op eq 'serialchangestatus' ) {
200 # Convert serialseqs to UTF-8 to prevent encoding problems
201 foreach my $seq (@serialseqs) {
202 utf8::decode($seq) unless utf8::is_utf8($seq);
205 my $newserial;
206 for ( my $i = 0 ; $i <= $#serialids ; $i++ ) {
207 my ($plan_date, $pub_date);
209 if (defined $planneddates[$i] && $planneddates[$i] ne 'XXX') {
210 $plan_date = format_date_in_iso( $planneddates[$i] );
212 if (defined $publisheddates[$i] && $publisheddates[$i] ne 'XXX') {
213 $pub_date = format_date_in_iso( $publisheddates[$i] );
216 if ( $serialids[$i] && $serialids[$i] eq 'NEW' ) {
217 if ( $serialseqs[$i] ) {
219 #IF newserial was provided a name Then we have to create a newSerial
220 ### FIXME if NewIssue is modified to use subscription biblionumber, then biblionumber would not be useful.
221 $newserial = NewIssue(
222 $serialseqs[$i],
223 $subscriptionids[0],
224 $serialdatalist[0]->{'biblionumber'},
225 $status[$i],
226 $plan_date,
227 $pub_date,
228 $notes[$i]
232 elsif ( $serialids[$i] ) {
233 ModSerialStatus(
234 $serialids[$i],
235 $serialseqs[$i],
236 $plan_date,
237 $pub_date,
238 $status[$i],
239 $notes[$i]
243 my @moditems = $query->param('moditem');
244 if ( scalar(@moditems) ) {
245 my @tags = $query->param('tag');
246 my @subfields = $query->param('subfield');
247 my @field_values = $query->param('field_value');
248 my @serials = $query->param('serial');
249 my @bibnums = $query->param('bibnum');
250 my @itemid = $query->param('itemid');
251 my @ind_tag = $query->param('ind_tag');
252 my @indicator = $query->param('indicator');
254 #Rebuilding ALL the data for items into a hash
255 # parting them on $itemid.
256 my %itemhash;
257 my $countdistinct;
258 my $range = scalar(@itemid);
259 for ( my $i = 0 ; $i < $range ; $i++ ) {
260 unless ( $itemhash{ $itemid[$i] } ) {
261 if ( $serials[$countdistinct]
262 && $serials[$countdistinct] ne "NEW" )
264 $itemhash{ $itemid[$i] }->{'serial'} =
265 $serials[$countdistinct];
267 else {
268 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
270 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
271 $countdistinct++;
273 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
274 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
275 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
276 $field_values[$i];
277 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
278 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
280 foreach my $item ( keys %itemhash ) {
282 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
283 my $index = -1;
284 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
285 if (
286 $itemhash{$item}->{serial} eq $serialids[$i]
287 || ( $itemhash{$item}->{serial} == $newserial
288 && $serialids[$i] eq 'NEW' )
290 $index = $i
293 if ( $index >= 0 && $status[$index] == 2 ) {
294 my $xml = TransformHtmlToXml(
295 $itemhash{$item}->{'tags'},
296 $itemhash{$item}->{'subfields'},
297 $itemhash{$item}->{'field_values'},
298 $itemhash{$item}->{'indicator'},
299 $itemhash{$item}->{'ind_tag'}
302 # warn $xml;
303 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
304 if ( $item =~ /^N/ ) {
306 #New Item
308 # if autoBarcode is set to 'incremental', calculate barcode...
309 my ( $barcodetagfield, $barcodetagsubfield ) =
310 GetMarcFromKohaField(
311 'items.barcode',
312 GetFrameworkCode(
313 $serialdatalist[0]->{'biblionumber'}
316 if ( C4::Context->preference('autoBarcode') eq
317 'incremental' )
319 if ( !$bib_record->field($barcodetagfield)
320 ->subfield($barcodetagsubfield) )
322 my $sth_barcode = $dbh->prepare(
323 'select max(abs(barcode)) from items');
324 $sth_barcode->execute;
325 my ($newbarcode) = $sth_barcode->fetchrow;
327 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
328 $bib_record->field($barcodetagfield)
329 ->update( $barcodetagsubfield => ++$newbarcode );
333 # check for item barcode # being unique
334 my $exists;
335 if (
336 $bib_record->subfield(
337 $barcodetagfield, $barcodetagsubfield
341 $exists = GetItemnumberFromBarcode(
342 $bib_record->subfield(
343 $barcodetagfield, $barcodetagsubfield
348 # push @errors,"barcode_not_unique" if($exists);
349 # if barcode exists, don't create, but report The problem.
350 if ($exists) {
351 push @errors, 'barcode_not_unique';
352 push @errseq, { serialseq => $serialseqs[$index] };
354 else {
355 my ( $biblionumber, $bibitemnum, $itemnumber ) =
356 AddItemFromMarc( $bib_record,
357 $itemhash{$item}->{bibnum} );
358 AddItem2Serial( $itemhash{$item}->{serial},
359 $itemnumber );
362 else {
364 #modify item
365 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
366 ModItemFromMarc( $bib_record,
367 $itemhash{$item}->{'bibnum'}, $item );
374 if ( @errors ) {
375 $template->param( Errors => 1 );
376 if ( @errseq ) {
377 $template->param( barcode_not_unique => 1, errseq => \@errseq );
380 else {
381 my $redirect = 'serials-collection.pl?';
382 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
383 print $query->redirect($redirect);
386 my $location = GetAuthorisedValues('LOC', $serialdatalist[0]->{'location'});
387 my $locationlib;
388 foreach (@$location) {
389 $locationlib = $_->{'lib'} if $_->{'selected'};
391 my $default_bib_view = get_default_view();
393 $template->param(
394 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
395 callnumber => $serialdatalist[0]->{'callnumber'},
396 bibliotitle => $biblio->{'title'},
397 biblionumber => $serialdatalist[0]->{'biblionumber'},
398 serialslist => \@serialdatalist,
399 default_bib_view => $default_bib_view,
400 location => $locationlib,
402 output_html_with_http_headers $query, $cookie, $template->output;
404 sub get_default_view {
405 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
406 my $views = { C4::Search::enabled_staff_search_views };
407 if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
408 return 'ISBDdetail';
409 } elsif ($defaultview eq 'marc' && $views->{can_view_MARC}) {
410 return 'MARCdetail';
411 } elsif ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
412 return 'labeledMARCdetail';
413 } else {
414 return 'detail';