new inventory number confirmation
[koha.git] / cataloguing / value_builder / ffzg-stocknumber.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25
26 =head1 DESCRIPTION
27
28 This plugin is specific to FFZG but could be used as a base for similar operations.
29 It is used for stocknumber computation.
30
31 If the user send an empty string, we return a simple incremented stocknumber for current year.
32 If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented.
33 In this case, a stocknumber has this form : "YEAR-0009678570".
34  - YEAR is numeric 4-digit year, like 2012
35  - dash
36  - digits, without leading zero
37
38 Required database changes:
39
40   create unique index item_stocknumer on items(stocknumber) ;
41
42   create table ffzg_inventarna_knjiga (
43         id int not null auto_increment primary key,
44         year int not null,
45         num int not null,
46         biblionumber int not null,
47         last_update timestamp default current_timestamp on update current_timestamp,
48         unique index ffzg_inv_br(year,num)
49   ) ;
50
51 =cut
52
53 sub plugin_parameters {
54 }
55
56 sub plugin_javascript {
57     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
58
59     my $res="
60     <script type='text/javascript'>
61         function Focus$field_number() {
62             return 1;
63         }
64
65         function Blur$field_number() {
66                 return 1;
67         }
68
69         function Clic$field_number() {
70                 var code = document.getElementById('$field_number');
71
72                 if ( ! confirm('Jeste li sigurni da želite dodijeliti novi inventarni broj?') )
73                         return;
74
75                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=ffzg-stocknumber.pl&code=' + code.value;
76                 var blurcallbackstocknumber = {
77                     success: function(o) {
78                         var field = document.getElementById('$field_number');
79                         field.value = o.responseText;
80                         return 1;
81                     }
82                 }
83                 var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallbackstocknumber, null);
84             return 1;
85         }
86     </script>
87     ";
88
89     return ($field_number,$res);
90 }
91
92 sub plugin {
93     my ($input) = @_;
94
95
96     my $code = $input->param('code');
97         my ( $year, $num ) = split(/-/,$code);
98
99     $year = (localtime)[5] + 1900 unless $year;
100
101 warn "XXX plugin code = $code";
102
103     my ($template, $loggedinuser, $cookie) = get_template_and_user({
104         template_name   => "cataloguing/value_builder/ajax.tmpl",
105         query           => $input,
106         type            => "intranet",
107         authnotrequired => 0,
108         flagsrequired   => {editcatalogue => '*'},
109         debug           => 1,
110     });
111
112         if ( ! $num ) {
113
114                         my $dbh = C4::Context->dbh;
115
116                         $dbh->begin_work;
117
118                         my $sth = $dbh->prepare("select max(num) from ffzg_inventarna_knjiga where year = ?");
119                         $sth->execute($year);
120                         
121                         my $max = $sth->fetchrow; # return null without any data
122                         $max += 1;
123
124                         $sth = $dbh->prepare("insert into ffzg_inventarna_knjiga (year,num) values (?,?)");
125                         $sth->execute( $year, $max );
126
127                         $dbh->commit;
128
129                         $num = $max;
130
131         }
132
133         $template->param(
134                 return => $year . '-' . $num,
135         );
136
137     output_html_with_http_headers $input, $cookie, $template->output;
138 }
139
140 1;