Bug 25341: Remove whitespace from barcode in add_items.pl
[koha.git] / serials / serials-edit.pl
blob627779140f5f3f3fd8849ee60c44b8f612aadb37
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
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>.
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 Modern::Perl;
65 use CGI qw ( -utf8 );
66 use Encode qw( decode is_utf8 );
67 use C4::Auth;
68 use C4::Biblio;
69 use C4::Items;
70 use C4::Koha;
71 use C4::Output;
72 use C4::Context;
73 use C4::Serials;
74 use C4::Search qw/enabled_staff_search_views/;
76 use Koha::DateUtils;
77 use Koha::Items;
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] );
96 my @errors;
97 my @errseq;
99 # If user comes from subscription details
100 unless (@serialids) {
101 my $serstatus = $query->param('serstatus');
102 my @statuses = split ',', $serstatus;
103 if ($serstatus) {
104 foreach my $subscriptionid (@subscriptionids) {
105 my @tmpser = GetSerials2( $subscriptionid, \@statuses );
106 push @serialids, map { $_->{serialid} } @tmpser;
111 unless ( @serialids ) {
112 my $string =
113 'serials-collection.pl?subscriptionid=' . join ',', uniq @subscriptionids;
114 $string =~ s/,$//;
116 print $query->redirect($string);
117 exit;
120 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
122 template_name => 'serials/serials-edit.tt',
123 query => $query,
124 type => 'intranet',
125 authnotrequired => 0,
126 flagsrequired => { serials => 'receive_serials' },
127 debug => 1,
131 my @serialdatalist;
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
140 if ( $serialid
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{};
150 else {
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} );
171 my @newserialloop;
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} )
181 my $cell;
182 if ( $serialdatalist[0]->{'serialsadditems'} ) {
184 #Create New empty item
185 $cell =
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);
217 my $newserial;
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(
234 $serialseqs[$i],
235 $subscriptionids[0],
236 $serialdatalist[0]->{'biblionumber'},
237 $status[$i],
238 $plan_date,
239 $pub_date,
240 $publisheddatetexts[$i],
241 $notes[$i],
242 $serialdatalist[0]->{'routingnotes'}
246 elsif ( $serialids[$i] ) {
247 ModSerialStatus(
248 $serialids[$i],
249 $serialseqs[$i],
250 $plan_date,
251 $pub_date,
252 $publisheddatetexts[$i],
253 $status[$i],
254 $notes[$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]);
261 if ($previous) {
263 my $serialitem = Koha::Serial::Items->search( {serialid => $previous} )->next;
264 my $itemnumber = $serialitem ? $serialitem->itemnumber : undef;
265 if ($itemnumber) {
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 $serialitem->set(
273 notforloan => 0,
274 itype => $subscriptioninfos->{'previousitemtype'}
276 )->store;
282 my @moditems = $query->multi_param('moditem');
283 if ( scalar(@moditems) ) {
284 my @tags = $query->multi_param('tag');
285 my @subfields = $query->multi_param('subfield');
286 my @field_values = $query->multi_param('field_value');
287 my @serials = $query->multi_param('serial');
288 my @bibnums = $query->multi_param('bibnum');
289 my @itemid = $query->multi_param('itemid');
290 my @ind_tag = $query->multi_param('ind_tag');
291 my @indicator = $query->multi_param('indicator');
292 my @num_copies = $query->multi_param('number_of_copies');
294 #Rebuilding ALL the data for items into a hash
295 # parting them on $itemid.
296 my %itemhash;
297 my $countdistinct = 0;
298 my $range = scalar(@itemid);
299 for ( my $i = 0 ; $i < $range ; $i++ ) {
300 unless ( $itemhash{ $itemid[$i] } ) {
301 if ( $serials[$countdistinct]
302 && $serials[$countdistinct] ne "NEW" )
304 $itemhash{ $itemid[$i] }->{'serial'} =
305 $serials[$countdistinct];
307 else {
308 $itemhash{ $itemid[$i] }->{'serial'} = $newserial;
310 $itemhash{ $itemid[$i] }->{'bibnum'} = $bibnums[$countdistinct];
311 $itemhash{ $itemid[$i] }->{'num_copies'} = $num_copies[$countdistinct];
312 $countdistinct++;
314 push @{ $itemhash{ $itemid[$i] }->{'tags'} }, $tags[$i];
315 push @{ $itemhash{ $itemid[$i] }->{'subfields'} }, $subfields[$i];
316 push @{ $itemhash{ $itemid[$i] }->{'field_values'} },
317 $field_values[$i];
318 push @{ $itemhash{ $itemid[$i] }->{'ind_tag'} }, $ind_tag[$i];
319 push @{ $itemhash{ $itemid[$i] }->{'indicator'} }, $indicator[$i];
321 foreach my $item ( keys %itemhash ) {
323 # Verify Itemization is "Valid", i.e. serial status is Arrived or Missing
324 my $index = -1;
325 for ( my $i = 0 ; $i < scalar(@serialids) ; $i++ ) {
326 if (
327 $itemhash{$item}->{serial} eq $serialids[$i]
328 || ( $itemhash{$item}->{serial} == $newserial
329 && $serialids[$i] eq 'NEW' )
331 $index = $i
334 if ( $index >= 0 && $status[$index] == 2 ) {
335 my $xml = TransformHtmlToXml(
336 $itemhash{$item}->{'tags'},
337 $itemhash{$item}->{'subfields'},
338 $itemhash{$item}->{'field_values'},
339 $itemhash{$item}->{'indicator'},
340 $itemhash{$item}->{'ind_tag'}
343 # warn $xml;
344 my $bib_record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
345 if ( $item =~ /^N/ ) {
347 $itemhash{$item}->{'num_copies'} //= 1;
349 for (my $copy = 0; $copy < $itemhash{$item}->{'num_copies'};){
351 # New Item
353 # if autoBarcode is set to 'incremental', calculate barcode...
354 my ( $barcodetagfield, $barcodetagsubfield ) = GetMarcFromKohaField( 'items.barcode' );
355 if ( C4::Context->preference('autoBarcode') eq
356 'incremental' )
358 if (
360 $bib_record->field($barcodetagfield)
361 && $bib_record->field($barcodetagfield)->subfield($barcodetagsubfield)
365 my $sth_barcode = $dbh->prepare(
366 'select max(abs(barcode)) from items');
367 $sth_barcode->execute;
368 my ($newbarcode) = $sth_barcode->fetchrow;
370 # OK, we have the new barcode, add the entry in MARC record # FIXME -> should be using barcode plugin here.
371 $bib_record->field($barcodetagfield)->update( $barcodetagsubfield => ++$newbarcode );
375 # check for item barcode # being unique
376 my $exists;
377 if ( $bib_record->subfield( $barcodetagfield, $barcodetagsubfield ) ) {
378 my $barcode = $bib_record->subfield( $barcodetagfield, $barcodetagsubfield );
380 if ($copy > 0){
381 use C4::Barcodes;
382 my $barcodeobj = C4::Barcodes->new;
383 my $newbarcode = $barcodeobj->next_value($barcode);
384 $barcode = $newbarcode;
385 $bib_record->field($barcodetagfield)->update($barcodetagsubfield => $barcode);
388 $exists = Koha::Items->find({barcode => $barcode});
391 # push @errors,"barcode_not_unique" if($exists);
392 # if barcode exists, don't create, but report The problem.
393 if ($exists) {
394 push @errors, 'barcode_not_unique';
395 push @errseq, { serialseq => $serialseqs[$index] };
397 else {
398 my ( $biblionumber, $bibitemnum, $itemnumber ) =
399 AddItemFromMarc( $bib_record,
400 $itemhash{$item}->{bibnum} );
401 AddItem2Serial( $itemhash{$item}->{serial},
402 $itemnumber );
404 $copy++;
407 } # if ( $item =~ /^N/ ) {
409 else {
411 #modify item
412 my ( $oldbiblionumber, $oldbibnum, $itemnumber ) =
413 ModItemFromMarc( $bib_record,
414 $itemhash{$item}->{'bibnum'}, $item );
421 if ( @errors ) {
422 $template->param( Errors => 1 );
423 if ( @errseq ) {
424 $template->param( barcode_not_unique => 1, errseq => \@errseq );
427 else {
428 my $redirect = 'serials-collection.pl?';
429 $redirect .= join( '&', map { 'subscriptionid=' . $_ } @subscriptionids );
430 print $query->redirect($redirect);
433 my $location = $serialdatalist[0]->{'location'};
434 my $default_bib_view = get_default_view();
436 $template->param(
437 subscriptionid => $serialdatalist[0]->{subscriptionid},
438 serialsadditems => $serialdatalist[0]->{'serialsadditems'},
439 callnumber => $serialdatalist[0]->{'callnumber'},
440 internalnotes => $serialdatalist[0]->{'internalnotes'},
441 bibliotitle => $biblio->title,
442 biblionumber => $serialdatalist[0]->{'biblionumber'},
443 serialslist => \@serialdatalist,
444 default_bib_view => $default_bib_view,
445 location => $location,
446 (uc(C4::Context->preference("marcflavour"))) => 1
449 output_html_with_http_headers $query, $cookie, $template->output;
451 sub get_default_view {
452 my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
453 my %views = C4::Search::enabled_staff_search_views();
454 if ( $defaultview eq 'isbd' && $views{can_view_ISBD} ) {
455 return 'ISBDdetail';
457 elsif ( $defaultview eq 'marc' && $views{can_view_MARC} ) {
458 return 'MARCdetail';
460 elsif ( $defaultview eq 'labeled_marc' && $views{can_view_labeledMARC} ) {
461 return 'labeledMARCdetail';
463 return 'detail';