View | Details | Raw Unified | Return to bug 31798
Collapse All | Expand All

(-)a/Koha/REST/V1/Biblios.pm (-4 / +52 lines)
Lines 305-313 sub add_item { Link Here
305
305
306
        my $item = Koha::Item->new_from_api($body);
306
        my $item = Koha::Item->new_from_api($body);
307
307
308
        if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
308
        if ( !defined $item->barcode ) {
309
            my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
309
310
            $item->barcode($barcode);
310
            # FIXME This should be moved to Koha::Item->store
311
            my $autoBarcode = C4::Context->preference('autoBarcode');
312
            my $barcode     = '';
313
## Please see file perltidy.ERR
314
            if ( !$autoBarcode || $autoBarcode eq 'OFF' ) {
315
316
                #We do nothing
317
            }
318
            elsif ( $autobarcode eq 'incremental' ) {
319
                ($barcode) =
320
                  C4::Barcodes::ValueBuilder::incremental::get_barcode;
321
            }
322
            elsif ( $autoBarcodeType eq 'annual' ) {
323
                ($barcode) =
324
                  C4::Barcodes::ValueBuilder::annual::get_barcode( \%args );
325
            }
326
            elsif ( $autoBarcodeType eq 'hbyymmincr' ) {
327
328
# Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
329
                my $homebranch = $item->homebranch // '';
330
                ($barcode) =
331
                  C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode( \%args );
332
                $barcode = $homebranch . $barcode;
333
            }
334
            elsif ( $autoBarcodeType eq 'EAN13' ) {
335
336
    # not the best, two catalogers could add the same barcode easily this way :/
337
                my $query = "select max(abs(barcode)) from items";
338
                my $dbh   = $params->{dbh};
339
                my $sth   = $dbh->prepare($query);
340
                $sth->execute();
341
                while ( my ($last) = $sth->fetchrow_array ) {
342
                    $nextnum = $last;
343
                }
344
                my $ean = CheckDigits('ean');
345
                if ( $ean->is_valid($nextnum) ) {
346
                    my $next = $ean->basenumber($nextnum) + 1;
347
                    $nextnum = $ean->complete($next);
348
                    $nextnum =
349
                      '0' x ( 13 - length($nextnum) ) . $nextnum;    # pad zeros
350
                }
351
                else {
352
                    warn "ERROR: invalid EAN-13 $nextnum, using increment";
353
                    $nextnum++;
354
                }
355
            }
356
            else {
357
                warn "ERROR: unknown autoBarcode: $autoBarcodeType";
358
            }
359
            $item->barcode($barcode) if $barcode;
311
        }
360
        }
312
361
313
        $item->store->discard_changes;
362
        $item->store->discard_changes;
314
- 

Return to bug 31798