From 700d929478bd33e85c3fc66cf97f821ca21df32a Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 11 Nov 2019 12:14:50 +0100
Subject: [PATCH] Bug 23888: Do not allow invalid vendor id on creating a
 subscription
Content-Type: text/plain; charset=utf-8

It will avoid crash and invalid data when creating/updating a
subscription.

This could have been done with a AJAX query but seems more convenient
this way.

Test plan:
- Create or update a subscription
- In the "Vendor" input try an empty string, a valid vendor's id, and
invalid one.
=> With an empty string you get the existing alert message
=> With a valid id you do not get any messages
=> With an invalid id you are not allowed to go to page 2

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 .../intranet-tmpl/prog/en/modules/serials/subscription-add.tt    | 3 +++
 koha-tmpl/intranet-tmpl/prog/js/subscription-add.js              | 9 ++++++++-
 serials/subscription-add.pl                                      | 3 +++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
index c2b3af0a59..fd603e167a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt
@@ -1,4 +1,5 @@
 [% USE raw %]
+[% USE To %]
 [% USE Asset %]
 [% USE KohaDates %]
 [% USE Branches %]
@@ -554,6 +555,8 @@ fieldset.rows table { clear: none; margin: 0; }
         var MSG_MANA_SHOW_DETAILS = _("Show Mana results");
         var MSG_MANA_NO_SUBSCRIPTION_FOUND = _("No subscription found on Mana Knowledge Base");
         var MSG_MANA_SHARE_PATTERN = _("Please feel free to share your pattern with all others librarians once you are done");
+
+        var BOOKSELLER_IDS = [% To.json( bookseller_ids ) || '[]' | $raw %];
     </script>
     [% Asset.js("js/subscription-add.js") | $raw %]
     [% Asset.js("js/showpredictionpattern.js") | $raw %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
index 06fae0fdde..79de60df37 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
@@ -46,11 +46,18 @@ function Clear(id) {
 }
 
 function Check_page1() {
-    if ( $("#aqbooksellerid").val().length == 0) {
+    var bookseller_id = $("#aqbooksellerid").val();
+    if ( bookseller_id.length == 0) {
         input_box = confirm( MSG_LINK_TO_VENDOR );
         if (input_box==false) {
             return false;
         }
+    } else {
+        var bookseller_ids = BOOKSELLER_IDS;
+        if ( $.inArray(Number(bookseller_id), bookseller_ids) == -1 ) {
+            alert ("The vendor does not exist");
+            return false;
+        }
     }
 
     var biblionumber = $("#biblionumber").val()
diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl
index ebc19a05e9..a7b483be18 100755
--- a/serials/subscription-add.pl
+++ b/serials/subscription-add.pl
@@ -244,6 +244,9 @@ if ($op eq 'addsubscription') {
 
     $template->param( locales => $languages );
 
+    my @bookseller_ids = Koha::Acquisition::Booksellers->search->get_column('id');
+    $template->param( bookseller_ids => \@bookseller_ids );
+
     output_html_with_http_headers $query, $cookie, $template->output;
 }
 
-- 
2.11.0