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

(-)a/Koha/REST/V1/Biblios.pm (-6 / +67 lines)
Lines 20-25 use Modern::Perl; Link Here
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Biblios;
22
use Koha::Biblios;
23
use Koha::DateUtils;
23
use Koha::Ratings;
24
use Koha::Ratings;
24
use Koha::RecordProcessor;
25
use Koha::RecordProcessor;
25
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio );
26
use C4::Biblio qw( DelBiblio AddBiblio ModBiblio );
Lines 291-297 sub add_item { Link Here
291
292
292
    try {
293
    try {
293
        my $biblio_id = $c->validation->param('biblio_id');
294
        my $biblio_id = $c->validation->param('biblio_id');
294
        my $biblio = Koha::Biblios->find( $biblio_id );
295
        my $biblio    = Koha::Biblios->find( $biblio_id );
295
296
296
        unless ($biblio) {
297
        unless ($biblio) {
297
            return $c->render(
298
            return $c->render(
Lines 309-323 sub add_item { Link Here
309
310
310
        my $item = Koha::Item->new_from_api($body);
311
        my $item = Koha::Item->new_from_api($body);
311
312
312
        if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
313
        if ( !defined $item->barcode ) {
313
            my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
314
314
            $item->barcode($barcode);
315
            # FIXME This should be moved to Koha::Item->store
316
            my $autoBarcode = C4::Context->preference('autoBarcode');
317
            my $barcode     = '';
318
319
            if ( !$autoBarcode || $autoBarcode eq 'OFF' ) {
320
                #We do nothing
321
            }
322
            elsif ( $autoBarcode eq 'incremental' ) {
323
                ($barcode) =
324
                  C4::Barcodes::ValueBuilder::incremental::get_barcode;
325
            }
326
            elsif ( $autoBarcode eq 'annual' ) {
327
                my $year = Koha::DateUtils::dt_from_string()->year();
328
                ($barcode) =
329
                  C4::Barcodes::ValueBuilder::annual::get_barcode(
330
                    { year => $year } );
331
            }
332
            elsif ( $autoBarcode eq 'hbyymmincr' ) {
333
334
                # Generates a barcode where
335
                #  hb = home branch Code,
336
                #  yymm = year/month catalogued,
337
                #  incr = incremental number,
338
                #  reset yearly -fbcit
339
                my $now        = Koha::DateUtils::dt_from_string();
340
                my $year       = $now->year();
341
                my $month      = $now->month();
342
                my $homebranch = $item->homebranch // '';
343
                ($barcode) =
344
                  C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(
345
                    { year => $year, mon => $month } );
346
                $barcode = $homebranch . $barcode;
347
            }
348
            elsif ( $autoBarcode eq 'EAN13' ) {
349
350
                # not the best, two catalogers could add the same
351
                # barcode easily this way :/
352
                my $query = "select max(abs(barcode)) from items";
353
                my $dbh   = C4::Context->dbh;
354
                my $sth   = $dbh->prepare($query);
355
                $sth->execute();
356
                my $nextnum;
357
                while ( my ($last) = $sth->fetchrow_array ) {
358
                    $nextnum = $last;
359
                }
360
                my $ean = CheckDigits('ean');
361
                if ( $ean->is_valid($nextnum) ) {
362
                    my $next = $ean->basenumber($nextnum) + 1;
363
                    $nextnum = $ean->complete($next);
364
                    $nextnum =
365
                      '0' x ( 13 - length($nextnum) ) . $nextnum;    # pad zeros
366
                }
367
                else {
368
                    warn "ERROR: invalid EAN-13 $nextnum, using increment";
369
                    $nextnum++;
370
                }
371
                $barcode = $nextnum;
372
            }
373
            else {
374
                warn "ERROR: unknown autoBarcode: $autoBarcode";
375
            }
376
            $item->barcode($barcode) if $barcode;
315
        }
377
        }
316
378
317
        $item->store->discard_changes;
379
        $item->store->discard_changes;
318
380
319
        $c->render(
381
        $c->render(
320
            status => 201,
382
            status  => 201,
321
            openapi => $item->to_api
383
            openapi => $item->to_api
322
        );
384
        );
323
    }
385
    }
324
- 

Return to bug 31798