Bug 22098: Update stocknumberAV cataloguing plugin to use objects
authorJosef Moravec <josef.moravec@gmail.com>
Fri, 11 Jan 2019 09:25:00 +0000 (09:25 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Tue, 24 Mar 2020 10:55:43 +0000 (10:55 +0000)
This patch:
- changes SQL to Koha::AuthorisedValues
- remove type param from script tag
- fixes the plugin description

0) Do not apply the patch
1) Set the plugin
1.1) Update a biblio framework and link plugin stocknumberAV.pl to some item
subfield
1.2) Add authorised values category called "INVENTORY"
1.3) Add some authorised values: authorised value is prefix and
description is a current stock/inventory number
2) Add an item and try to use this plugin to ensure you set it
correctly
2.1) Use a defined prefix to see if the number is correct
2.2) Ensure the number is correctly incremented in authorised values
2.3) Use not defined prefix to see the error message
2.4) Insert a not prefix string (eg number) to see it is not changed
3) Apply the patch
4) Repeat 2) and see it is working the same
5) Look into patch and confirm the description does make sense and is
rigth according to what you see in UI
6) Sign off :D

Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

cataloguing/value_builder/stocknumberAV.pl

index ac9d41f..395623e 100755 (executable)
@@ -23,18 +23,18 @@ use Modern::Perl;
 use CGI qw ( -utf8 );
 
 use C4::Auth;
-use C4::Context;
 use C4::Output;
+use Koha::AuthorisedValues;
 
 =head1 DESCRIPTION
 
 This plugin is based on authorised values INVENTORY.
 It is used for stocknumber computation.
 
-If the user send an empty string, we return a simple incremented stocknumber.
+If no prefix is submitted, or prefix does not contain only nubers, it returns the inserted code (= keep a field unchanged)
 If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented.
 In this case, a stocknumber has this form : "PREFIX 0009678570".
- - PREFIX is an upercase word
+ - PREFIX contains of letters
  - a space separator
  - 10 digits, with leading 0s if needed
 
@@ -43,7 +43,7 @@ In this case, a stocknumber has this form : "PREFIX 0009678570".
 my $builder = sub {
     my ( $params ) = @_;
     my $res = qq{
-    <script type='text/javascript'>
+    <script>
         function Click$params->{id}() {
                 var code = document.getElementById('$params->{id}');
                 \$.ajax({
@@ -81,20 +81,19 @@ my $launcher = sub {
         }
     );
 
-    my $dbh = C4::Context->dbh;
-
     # If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented
     $code =~ s/ *$//g;
     if ( $code =~ m/^[a-zA-Z]+$/ ) {
-        my $sth = $dbh->prepare("SELECT lib FROM authorised_values WHERE category='INVENTORY' AND authorised_value=?");
-        $sth->execute( $code);
-
-        if ( my $valeur = $sth->fetchrow ) {
-            $template->param( return => $code . ' ' . sprintf( '%010s', ( $valeur + 1 ) ), );
-            my $sth2 = $dbh->prepare("UPDATE authorised_values SET lib=? WHERE category='INVENTORY' AND authorised_value=?");
-            $sth2->execute($valeur+1,$code);
+        my $av = Koha::AuthorisedValues->find({
+            'category' => 'INVENTORY',
+            'authorised_value' => $code
+        });
+        if ( $av ) {
+            $av->lib($av->lib + 1);
+            $av->store;
+            $template->param( return => $code . ' ' . sprintf( '%010s', ( $av->lib ) ), );
         } else {
-                $template->param( return => "There is no defined value for $code");
+            $template->param( return => "There is no defined value for $code");
         }
         # The user entered a custom value, we don't touch it, this could be handled in js
     } else {