2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
5 # This file is part of Koha.
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
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.
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.
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
27 use Algorithm::CheckDigits;
33 plugin_parameters : other parameters added when the plugin is called by the dopop function
37 sub plugin_parameters {
38 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
44 plugin_javascript : the javascript function called when the user enters the subfield.
45 contain 3 javascript functions :
46 * one called when the field is entered (OnFocus). Named FocusXXX
47 * one called when the field is leaved (onBlur). Named BlurXXX
48 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
52 * a variable containing the 3 scripts.
53 the 3 scripts are inserted after the <input> in the html code
57 sub plugin_javascript {
58 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
59 my $function_name= "barcode".(int(rand(100000))+1);
62 my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
63 my ($tag,$subfield) = GetMarcFromKohaField("items.barcode", '');
64 my ($loctag,$locsubfield) = GetMarcFromKohaField("items.homebranch", '');
69 my $autoBarcodeType = C4::Context->preference("autoBarcode");
70 warn "Barcode type = $autoBarcodeType" if $DEBUG;
71 if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
72 # don't return a value unless we have the appropriate syspref set
73 return ($function_name,
74 "<script type=\"text/javascript\">
75 // autoBarcodeType OFF (or not defined)
76 function Focus$function_name() { return 0;}
77 function Clic$function_name() { return 0;}
78 function Blur$function_name() { return 0;}
81 if ($autoBarcodeType eq 'annual') {
82 $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
83 my $sth=$dbh->prepare($query);
84 $sth->execute("$year%");
85 while (my ($count)= $sth->fetchrow_array) {
86 warn "Examining Record: $count" if $DEBUG;
87 $nextnum = $count if $count;
90 $nextnum = sprintf("%0*d", "4",$nextnum);
91 $nextnum = "$year-$nextnum";
93 elsif ($autoBarcodeType eq 'incremental') {
94 # not the best, two catalogers could add the same barcode easily this way :/
95 $query = "select max(abs(barcode)) from items";
96 my $sth = $dbh->prepare($query);
98 while (my ($count)= $sth->fetchrow_array) {
103 elsif ($autoBarcodeType eq 'hbyymmincr') { # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
104 $year = substr($year, -2);
105 $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
106 my $sth = $dbh->prepare($query);
107 $sth->execute("^[-a-zA-Z]{1,}$year");
108 while (my ($count)= $sth->fetchrow_array) {
109 $nextnum = $count if $count;
110 $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
111 warn "Existing incremental number = $nextnum" if $DEBUG;
114 $nextnum = sprintf("%0*d", "4",$nextnum);
115 $nextnum = $year . $mon . $nextnum;
116 warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
118 for (i=0 ; i<document.f.field_value.length ; i++) {
119 if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
123 if (\$('#' + id).val() == '' || force) {
124 \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
128 elsif ($autoBarcodeType eq 'EAN13') {
129 # not the best, two catalogers could add the same barcode easily this way :/
130 $query = "select max(abs(barcode)) from items";
131 my $sth = $dbh->prepare($query);
133 while (my ($last)= $sth->fetchrow_array) {
136 my $ean = CheckDigits('ean');
137 if ( $ean->is_valid($nextnum) ) {
138 my $next = $ean->basenumber( $nextnum ) + 1;
139 $nextnum = $ean->complete( $next );
140 $nextnum = '0' x ( 13 - length($nextnum) ) . $nextnum; # pad zeros
142 warn "ERROR: invalid EAN-13 $nextnum, using increment";
147 warn "ERROR: unknown autoBarcode: $autoBarcodeType";
150 # default js body (if not filled by hbyymmincr)
151 $scr or $scr = <<END_OF_JS;
152 if (\$('#' + id).val() == '' || force) {
153 \$('#' + id).val('$nextnum');
157 my $js = <<END_OF_JS;
158 <script type="text/javascript">
161 function Blur$function_name(index) {
162 //barcode validation might go here
165 function Focus$function_name(subfield_managed, id, force) {
170 function Clic$function_name(id) {
171 return Focus$function_name('not_relavent', id, 1);
176 return ($function_name, $js);