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

Return to bug 31798