FFZG stocknumber - use last year until 15th of january
[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 use DateTime;
26
27 =head1 DESCRIPTION
28
29 This plugin is specific to FFZG but could be used as a base for similar operations.
30 It is used for stocknumber computation.
31
32 If the user send an empty string, we return a simple incremented stocknumber for current year.
33 If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented.
34 In this case, a stocknumber has this form : "YEAR-0009678570".
35  - YEAR is numeric 4-digit year, like 2012
36  - dash
37  - digits, without leading zero
38
39 Required database changes:
40
41   create unique index item_stocknumer on items(stocknumber) ;
42
43   create table ffzg_inventarna_knjiga (
44         id int not null auto_increment primary key,
45         year int not null,
46         num int not null,
47         biblionumber int not null,
48         last_update timestamp default current_timestamp on update current_timestamp,
49         unique index ffzg_inv_br(year,num)
50   ) ;
51
52 =cut
53
54 sub plugin_parameters {
55 }
56
57 sub plugin_javascript {
58     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
59
60     my $res="
61     <script type='text/javascript'>
62         function Focus$field_number() {
63             return 1;
64         }
65
66         function Blur$field_number() {
67                 return 1;
68         }
69
70         function Clic$field_number() {
71                 var code = document.getElementById('$field_number');
72
73                 if ( ! confirm('Jeste li sigurni da želite dodijeliti novi inventarni broj?') )
74                         return;
75
76                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=ffzg-stocknumber.pl&code=' + code.value;
77                 var blurcallbackstocknumber = {
78                     success: function(o) {
79                         var field = document.getElementById('$field_number');
80                         field.value = o.responseText;
81                         return 1;
82                     }
83                 }
84                 var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallbackstocknumber, null);
85             return 1;
86         }
87     </script>
88     ";
89
90     return ($field_number,$res);
91 }
92
93 sub plugin {
94     my ($input) = @_;
95
96
97     my $code = $input->param('code');
98     my ( $year, $num ) = split(/-/,$code);
99
100     if ( ! $year ) {
101         my $now = DateTime->now;
102         $year = $now->year;
103         if ( $now->month == 1 && $now->day <= 15 ) {
104                 $year--;
105         }
106     }
107
108 warn "XXX plugin code = $code";
109
110     my ($template, $loggedinuser, $cookie) = get_template_and_user({
111         template_name   => "cataloguing/value_builder/ajax.tmpl",
112         query           => $input,
113         type            => "intranet",
114         authnotrequired => 0,
115         flagsrequired   => {editcatalogue => '*'},
116         debug           => 1,
117     });
118
119         if ( ! $num ) {
120
121                         my $dbh = C4::Context->dbh;
122
123                         $dbh->begin_work;
124
125                         my $sth = $dbh->prepare("select max(num) from ffzg_inventarna_knjiga where year = ?");
126                         $sth->execute($year);
127                         
128                         my $max = $sth->fetchrow; # return null without any data
129                         $max += 1;
130
131                         $sth = $dbh->prepare("insert into ffzg_inventarna_knjiga (year,num) values (?,?)");
132                         $sth->execute( $year, $max );
133
134                         $dbh->commit;
135
136                         $num = $max;
137
138         }
139
140         $template->param(
141                 return => $year . '-' . $num,
142         );
143
144     output_html_with_http_headers $input, $cookie, $template->output;
145 }
146
147 1;