<commit_before>130-bug17371-API-biblios-CRUD-expanded

Patch to enable full CRUD on /api/v1/biblios
Delete all items on DELETE as default, except if items have holds or issues
Also enables expanded item with status and reservability

diff --git a/Koha/REST/V1/Biblio.pm b/Koha/REST/V1/Biblio.pm
new file mode 100644
index 0000000..1f1e1d0
--- /dev/null
+++ b/Koha/REST/V1/Biblio.pm
@@ -0,0 +1,224 @@
+package Koha::REST::V1::Biblio;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Mojo::Base 'Mojolicious::Controller';
+
+use Koha::AuthUtils qw(hash_password);
+use C4::Biblio qw( GetBiblioData AddBiblio ModBiblio DelBiblio ModZebra );
+use C4::Items qw ( AddItemBatchFromMarc DelItemCheck );
+use Koha::Biblios;
+use MARC::Record;
+use MARC::Batch;
+use MARC::File::USMARC;
+use MARC::File::XML;
+
+use Data::Dumper;
+
+sub get {
+    my ($c, $args, $cb) = @_;
+
+    my $biblio = &GetBiblioData($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+    return $c->$cb($biblio, 200);
+}
+
+sub getitems {
+    my ($c, $args, $cb) = @_;
+
+    #my $biblio = &GetBiblioData($args->{biblionumber});
+    my $biblio = Koha::Biblios->find($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+    return $c->$cb({ biblio => $biblio->unblessed, items => $biblio->items->unblessed }, 200);
+}
+
+sub getexpanded {
+    my ($c, $args, $cb) = @_;
+
+    my $biblio = Koha::Biblios->find($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+
+
+    my $expanded = $biblio->items->unblessed;
+
+    $biblio = $biblio->unblessed;
+    my $holds = Koha::Holds->search({biblionumber => $args->{biblionumber}});
+    $biblio->{numholds} = $holds->count;
+
+    # do we have a pending hold with priority 0?
+    $biblio->{behindExpiditedUser} = 0;
+    for my $hold (@{$holds->unblessed}) {
+        $biblio->{behindExpiditedUser} = 1 if $hold->{priority} == 0;
+    }
+
+    for my $item (@{$expanded}) {
+
+        # we assume item is available and reservable by default
+        $item->{status} = "Ledig";
+        $item->{reservable} = 1;
+
+        if ($item->{holdingbranch} ne $item->{homebranch}) {
+            # This will match items in branchtransfers
+            $item->{status} = "Utlånt";
+        }
+
+        if ($item->{holdingbranch} eq 'hsko') {
+            $item->{reservable} = 0;        }
+
+        if ($item->{onloan}) {
+            $item->{status} = "Utlånt";
+        }
+
+        my $notloan = $item->{notforloan};
+        if ($notloan) {
+            # -2  Bestilt
+            #  8  Ikke til hjemlån
+            #  1  Til internt bruk
+            # -1  Til klargjøring
+            #  2  Til reparasjon
+            #  4  Til retting
+            if ($notloan == -2 || $notloan == -1 || $notloan == 2 || $notloan == 4 ) {
+                $item->{status} = "Utlånt";
+            } elsif ($notloan == 1) {
+                $item->{status} = "Utilgjengelig";
+                $item->{reservable} = 0;
+            } elsif ($notloan == 8) {
+                $item->{status} = "Ikke til hjemlån";
+                $item->{reservable} = 0;
+                if ($item->{onloan}) {
+                    $item->{status} = "Ikke til hjemlån (utlånt)"
+                }
+            }
+        }
+
+        # certain item types are not reservable
+        if ( grep { $_ eq $item->{itype}} qw(10DLAAN TOUKESLAAN UKESLAAN SETT) ) {
+            $item->{reservable} = 0;
+        }
+
+        if ($item->{itemlost}) {
+            $item->{status} = "Utilgjengelig";
+            $item->{reservable} = 0;
+        }
+
+
+        if ($biblio->{numholds} > 0 && Koha::Holds->search({itemnumber => $item->{itemnumber}})->count > 0) {
+            $item->{status} = "Utlånt";<commit_msg>biblio expanded: treat damaged items as anavailable<commit_after>130-bug17371-API-biblios-CRUD-expanded

Patch to enable full CRUD on /api/v1/biblios
Delete all items on DELETE as default, except if items have holds or issues
Also enables expanded item with status and reservability

diff --git a/Koha/REST/V1/Biblio.pm b/Koha/REST/V1/Biblio.pm
new file mode 100644
index 0000000..236af1a
--- /dev/null
+++ b/Koha/REST/V1/Biblio.pm
@@ -0,0 +1,224 @@
+package Koha::REST::V1::Biblio;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Mojo::Base 'Mojolicious::Controller';
+
+use Koha::AuthUtils qw(hash_password);
+use C4::Biblio qw( GetBiblioData AddBiblio ModBiblio DelBiblio ModZebra );
+use C4::Items qw ( AddItemBatchFromMarc DelItemCheck );
+use Koha::Biblios;
+use MARC::Record;
+use MARC::Batch;
+use MARC::File::USMARC;
+use MARC::File::XML;
+
+use Data::Dumper;
+
+sub get {
+    my ($c, $args, $cb) = @_;
+
+    my $biblio = &GetBiblioData($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+    return $c->$cb($biblio, 200);
+}
+
+sub getitems {
+    my ($c, $args, $cb) = @_;
+
+    #my $biblio = &GetBiblioData($args->{biblionumber});
+    my $biblio = Koha::Biblios->find($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+    return $c->$cb({ biblio => $biblio->unblessed, items => $biblio->items->unblessed }, 200);
+}
+
+sub getexpanded {
+    my ($c, $args, $cb) = @_;
+
+    my $biblio = Koha::Biblios->find($args->{biblionumber});
+    unless ($biblio) {
+        return $c->$cb({error => "Biblio not found"}, 404);
+    }
+
+
+    my $expanded = $biblio->items->unblessed;
+
+    $biblio = $biblio->unblessed;
+    my $holds = Koha::Holds->search({biblionumber => $args->{biblionumber}});
+    $biblio->{numholds} = $holds->count;
+
+    # do we have a pending hold with priority 0?
+    $biblio->{behindExpiditedUser} = 0;
+    for my $hold (@{$holds->unblessed}) {
+        $biblio->{behindExpiditedUser} = 1 if $hold->{priority} == 0;
+    }
+
+    for my $item (@{$expanded}) {
+
+        # we assume item is available and reservable by default
+        $item->{status} = "Ledig";
+        $item->{reservable} = 1;
+
+        if ($item->{holdingbranch} ne $item->{homebranch}) {
+            # This will match items in branchtransfers
+            $item->{status} = "Utlånt";
+        }
+
+        if ($item->{holdingbranch} eq 'hsko') {
+            $item->{reservable} = 0;        }
+
+        if ($item->{onloan}) {
+            $item->{status} = "Utlånt";
+        }
+
+        my $notloan = $item->{notforloan};
+        if ($notloan) {
+            # -2  Bestilt
+            #  8  Ikke til hjemlån
+            #  1  Til internt bruk
+            # -1  Til klargjøring
+            #  2  Til reparasjon
+            #  4  Til retting
+            if ($notloan == -2 || $notloan == -1 || $notloan == 2 || $notloan == 4 ) {
+                $item->{status} = "Utlånt";
+            } elsif ($notloan == 1) {
+                $item->{status} = "Utilgjengelig";
+                $item->{reservable} = 0;
+            } elsif ($notloan == 8) {
+                $item->{status} = "Ikke til hjemlån";
+                $item->{reservable} = 0;
+                if ($item->{onloan}) {
+                    $item->{status} = "Ikke til hjemlån (utlånt)"
+                }
+            }
+        }
+
+        # certain item types are not reservable
+        if ( grep { $_ eq $item->{itype}} qw(10DLAAN TOUKESLAAN UKESLAAN SETT) ) {
+            $item->{reservable} = 0;
+        }
+
+        if ($item->{itemlost} || $item->{damaged}) {
+            $item->{status} = "Utilgjengelig";
+            $item->{reservable} = 0;
+        }
+
+
+        if ($biblio->{numholds} > 0 && Koha::Holds->search({itemnumber => $item->{itemnumber}})->count > 0) {
+            $item->{status} = "Utlånt";
{'fraction_non_alphanumeric': 0.17500541477149664, 'fraction_numerical': 0.019709768247779945, 'mean_word_length': 2.554657428791378, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4441289', 'n_tokens_mistral': 3620, 'n_tokens_neox': 3310, 'n_words': 840}