Bug 32030: Link eHolding with packages
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 25 May 2022 11:33:35 +0000 (13:33 +0200)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 8 Nov 2022 12:44:00 +0000 (09:44 -0300)
Signed-off-by: Jonathan Field <jonathan.field@ptfs-europe.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/ERM/EHolding.pm
Koha/ERM/EHolding/Package.pm [new file with mode: 0644]
Koha/ERM/EHolding/Packages.pm [new file with mode: 0644]
Koha/REST/V1/ERM/EHoldings.pm
api/v1/swagger/definitions/erm_eholding.yaml
api/v1/swagger/definitions/erm_eholding_package.yaml [new file with mode: 0644]
api/v1/swagger/paths/erm_eholdings.yaml
koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingPackages.vue [new file with mode: 0644]
koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingsFormAdd.vue
koha-tmpl/intranet-tmpl/prog/js/vue/fetch.js

index 1af43cd..058bda1 100644 (file)
@@ -21,8 +21,7 @@ use Koha::Database;
 
 use base qw(Koha::Object);
 
-use Koha::ERM::Packages;
-use Koha::Acquisition::Booksellers;
+use Koha::ERM::EHolding::Packages;
 
 =head1 NAME
 
@@ -32,6 +31,31 @@ Koha::ERM::EHolding - Koha ERM EHolding Object class
 
 =head2 Class Methods
 
+=head3 eholding_packages
+
+Returns the eholding_packages link for this eHolding
+
+=cut
+
+sub eholding_packages {
+    my ( $self, $eholding_packages ) = @_;
+
+    if ( $eholding_packages ) {
+        my $schema = $self->_result->result_source->schema;
+        $schema->txn_do(
+            sub {
+                $self->eholding_packages->delete;
+
+                for my $eholding_package (@$eholding_packages) {
+                    $self->_result->add_to_erm_eholdings_packages($eholding_package);
+                }
+            }
+        );
+    }
+    my $eholding_packages_rs = $self->_result->erm_eholdings_packages;
+    return Koha::ERM::EHolding::Packages->_new_from_dbic($eholding_packages_rs);
+}
+
 =head2 Internal methods
 
 =head3 _type
diff --git a/Koha/ERM/EHolding/Package.pm b/Koha/ERM/EHolding/Package.pm
new file mode 100644 (file)
index 0000000..0f9e04a
--- /dev/null
@@ -0,0 +1,58 @@
+package Koha::ERM::EHolding::Package;
+
+# 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, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use Koha::ERM::Package;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::ERM::EHolding::Package - Koha EHolding Package Object class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 package
+
+Return the package for this link
+
+=cut
+
+sub package {
+    my ( $self ) = @_;
+    my $package_rs = $self->_result->package;
+    return Koha::ERM::Package->_new_from_dbic($package_rs);
+}
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'ErmEholdingsPackage';
+}
+
+1;
diff --git a/Koha/ERM/EHolding/Packages.pm b/Koha/ERM/EHolding/Packages.pm
new file mode 100644 (file)
index 0000000..46d1ba6
--- /dev/null
@@ -0,0 +1,49 @@
+package Koha::ERM::EHolding::Packages;
+
+# 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, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+
+use Koha::Database;
+
+use Koha::ERM::EHolding::Package;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::ERM::EHolding::Packages- Koha EHolding EHolding Object set class
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 type
+
+=cut
+
+sub _type {
+    return 'ErmEholdingsPackage';
+}
+
+sub object_class {
+    return 'Koha::ERM::EHolding::Package';
+}
+
+1;
index 01acab0..8860940 100644 (file)
@@ -91,8 +91,12 @@ sub add {
 
                 my $body = $c->validation->param('body');
 
+                my $eholding_packages = delete $body->{eholding_packages} // [];
+
                 my $eholding = Koha::ERM::EHolding->new_from_api($body)->store;
 
+                $eholding->eholding_packages($eholding_packages);
+
                 $c->res->headers->location($c->req->url->to_string . '/' . $eholding->eholding_id);
                 return $c->render(
                     status  => 201,
@@ -163,8 +167,12 @@ sub update {
 
                 my $body = $c->validation->param('body');
 
+                my $eholding_packages = delete $body->{eholding_packages} // [];
+
                 $eholding->set_from_api($body)->store;
 
+                $eholding->eholding_packages($eholding_packages);
+
                 $c->res->headers->location($c->req->url->to_string . '/' . $eholding->eholding_id);
                 return $c->render(
                     status  => 200,
index ce7417c..36ce4b7 100644 (file)
@@ -128,6 +128,11 @@ properties:
     type:
       - string
       - "null"
+  eholding_packages:
+    type: array
+    description: packages containing this title
+    items:
+      $ref: erm_eholding_package.yaml
 
 additionalProperties: false
 required:
diff --git a/api/v1/swagger/definitions/erm_eholding_package.yaml b/api/v1/swagger/definitions/erm_eholding_package.yaml
new file mode 100644 (file)
index 0000000..46fdc8a
--- /dev/null
@@ -0,0 +1,27 @@
+---
+type: object
+properties:
+  eholding_id:
+    type: integer
+    description: Internal related eHolding identifier
+  package_id:
+    type: integer
+    description: Internal package identifier
+  started_on:
+    description: Start date
+    type:
+      - string
+      - "null"
+  ended_on:
+    description: End date
+    type:
+      - string
+      - "null"
+  proxy:
+    description: Proxy
+    type:
+      - string
+      - "null"
+additionalProperties: false
+required:
+  - package_id
index de67b8a..b7d769d 100644 (file)
       permissions:
         erm: 1
     x-koha-embed:
-      - agreements
+      - eholding_packages
+      - eholding_packages.package
   put:
     x-mojo-to: ERM::EHoldings#update
     operationId: updateErmEHoldings
       permissions:
         erm: 1
     x-koha-embed:
-      - agreements
+      - eholding_packages
+      - eholding_packages.package
   delete:
     x-mojo-to: ERM::EHoldings#delete
     operationId: deleteErmEHoldings
diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingPackages.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/EHoldingPackages.vue
new file mode 100644 (file)
index 0000000..62024c4
--- /dev/null
@@ -0,0 +1,117 @@
+<template>
+    <fieldset class="rows" id="eholding_packages">
+        <legend>{{ $t("Packages") }}</legend>
+        <fieldset
+            class="rows"
+            v-for="(eholding_package, counter) in eholding_packages"
+            v-bind:key="counter"
+        >
+            <legend>
+                {{ $t("Package.counter", { counter: counter + 1 }) }}
+                <a href="#" @click.prevent="deletePackage(counter)"
+                    ><i class="fa fa-trash"></i>
+                    {{ $t("Remove from this package") }}</a
+                >
+            </legend>
+            <ol>
+                <li>
+                    <label
+                        :for="`eholding_package_id_${counter}`"
+                        class="required"
+                        >{{ $t("Package:") }}
+                    </label>
+                    <select
+                        v-model="eholding_package.package_id"
+                        :id="`eholding_package_id_${counter}`"
+                        required
+                    >
+                        <option value=""></option>
+                        <option
+                            v-for="p in packages"
+                            :key="p.package_id"
+                            :value="p.package_id"
+                            :selected="
+                                p.package_id == eholding_package.package_id
+                                    ? true
+                                    : false
+                            "
+                        >
+                            {{ p.name }}
+                        </option>
+                    </select>
+                    <span class="required">{{ $t("Required") }}</span>
+                </li>
+                <li>
+                    <label :for="`started_on_${counter}`"
+                        >{{ $t("Start date:") }}
+                    </label>
+                    <flat-pickr
+                        :id="`started_on_${counter}`"
+                        v-model="eholding_package.started_on"
+                        :config="fp_config"
+                        :data-date_to="`ended_on_${counter}`"
+                    />
+                </li>
+                <li>
+                    <label :for="`ended_on_${counter}`">{{
+                        $t("End date:")
+                    }}</label>
+                    <flat-pickr
+                        :id="`ended_on_${counter}`"
+                        v-model="eholding_package.ended_on"
+                        :config="fp_config"
+                    />
+                </li>
+                <li>
+                    <label :for="`${counter}`">{{ $t("Proxy:") }}</label>
+                    <input
+                        :id="`proxy_${counter}`"
+                        v-model="eholding_package.proxy"
+                        :placeholder="$t('Proxy')"
+                    />
+                </li>
+            </ol>
+        </fieldset>
+        <a v-if="packages.length" class="btn btn-default" @click="addPackage"
+            ><font-awesome-icon icon="plus" />
+            {{ $t("Add to another package") }}</a
+        >
+        <span v-else>{{ $t("There are no packages created yet") }}</span>
+    </fieldset>
+</template>
+
+<script>
+import flatPickr from 'vue-flatpickr-component'
+import { fetchPackages } from "../../fetch"
+
+export default {
+    data() {
+        return {
+            packages: [],
+            fp_config: flatpickr_defaults,
+            dates_fixed: 0,
+        }
+    },
+    beforeCreate() {
+        fetchPackages().then((packages) => this.packages = packages)
+    },
+    methods: {
+        addPackage() {
+            this.eholding_packages.push({
+                package_id: null,
+                started_on: null,
+                ended_on: null,
+                proxy: '',
+            })
+        },
+        deletePackage(counter) {
+            this.eholding_packages.splice(counter, 1)
+        },
+    },
+    props: {
+        eholding_packages: Array,
+    },
+    components: { flatPickr },
+    name: 'EHoldingPackages',
+}
+</script>
index d7ebb48..bdb3d00 100644 (file)
                                 :placeholder="$t('Access type')"
                             />
                         </li>
+
+                        <EHoldingPackages
+                            :eholding_packages="eholding.eholding_packages"
+                        />
                     </ol>
                 </fieldset>
                 <fieldset class="action">
 </template>
 
 <script>
+import EHoldingPackages from "./EHoldingPackages.vue"
 import { setMessage, setError } from "../../messages"
 import { fetchEHolding } from '../../fetch'
 
@@ -397,6 +402,7 @@ export default {
                 parent_publication_title_id: '',
                 preceeding_publication_title_id: '',
                 access_type: '',
+                eholding_packages: [],
             },
             initialized: false,
         }
@@ -453,6 +459,7 @@ export default {
                 }).catch(e => { console.log(e) })
         },
     },
+    components: { EHoldingPackages },
     name: "EHoldingsFormAdd",
 }
 </script>
index 44e7379..d8b596d 100644 (file)
@@ -145,7 +145,11 @@ export const fetchEHolding = async function (eholding_id) {
     if (!eholding_id) return;
     const apiUrl = "/api/v1/erm/eholdings/" + eholding_id;
     let erm_eholding;
-    await fetch(apiUrl)
+    await fetch(apiUrl, {
+        headers: {
+            "x-koha-embed": "eholding_packages,eholding_packages.package",
+        },
+    })
         .then(checkError)
         .then(
             (result) => {