View | Details | Raw Unified | Return to bug 8524
Collapse All | Expand All

(-)a/C4/Barcodes/ValueBuilder.pm (+109 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
#
3
# Copyright 2008-2010 Foundations Bible College
4
# Parts copyright 2012 C & P Bibliography Services
5
#
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it under the
9
# terms of the GNU General Public License as published by the Free Software
10
# Foundation; either version 2 of the License, or (at your option) any later
11
# version.
12
#
13
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License along
18
# with Koha; if not, write to the Free Software Foundation, Inc.,
19
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
package C4::Barcodes::ValueBuilder::incremental;
22
use C4::Context;
23
my $DEBUG = 0;
24
25
sub get_barcode {
26
    my ($args) = @_;
27
    my $nextnum;
28
    # not the best, two catalogers could add the same barcode easily this way :/
29
    my $query = "select max(abs(barcode)) from items";
30
    my $sth = C4::Context->dbh->prepare($query);
31
    $sth->execute();
32
    while (my ($count)= $sth->fetchrow_array) {
33
        $nextnum = $count;
34
    }
35
    $nextnum++;
36
    return $nextnum;
37
}
38
39
1;
40
41
package C4::Barcodes::ValueBuilder::hbyymmincr;
42
use C4::Context;
43
my $DEBUG = 0;
44
45
sub get_barcode {
46
    my ($args) = @_;
47
    my $nextnum;
48
    my $year = substr($args->{year}, -2);
49
    my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
50
    my $sth = C4::Context->dbh->prepare($query);
51
    $sth->execute("^[-a-zA-Z]{1,}$year");
52
    while (my ($count)= $sth->fetchrow_array) {
53
        $nextnum = $count if $count;
54
        $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
55
            warn "Existing incremental number = $nextnum" if $DEBUG;
56
    }
57
    $nextnum++;
58
    $nextnum = sprintf("%0*d", "4",$nextnum);
59
    $nextnum = $year . $args->{mon} . $nextnum;
60
    warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
61
    my $scr = " 
62
        for (i=0 ; i<document.f.field_value.length ; i++) {
63
            if (document.f.tag[i].value == '$args->{loctag}' && document.f.subfield[i].value == '$args->{locsubfield}') {
64
                fnum = i;
65
            }
66
        }
67
    if (\$('#' + id).val() == '' || force) {
68
        \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
69
    }
70
    ";
71
    return $nextnum, $scr;
72
}
73
74
75
package C4::Barcodes::ValueBuilder::annual;
76
use C4::Context;
77
my $DEBUG = 0;
78
79
sub get_barcode {
80
    my ($args) = @_;
81
    my $nextnum;
82
    my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
83
    my $sth=C4::Context->dbh->prepare($query);
84
    $sth->execute("$args->{year}%");
85
    while (my ($count)= $sth->fetchrow_array) {
86
        warn "Examining Record: $count" if $DEBUG;
87
        $nextnum = $count if $count;
88
    }
89
    $nextnum++;
90
    $nextnum = sprintf("%0*d", "4",$nextnum);
91
    $nextnum = "$args->{year}-$nextnum";
92
    return $nextnum;
93
}
94
95
1;
96
97
98
=head1 Barcodes::ValueBuilder
99
100
This module is intended as a shim to ease the eventual transition from
101
having all barcode-related code in the value builder plugin .pl file
102
to using C4::Barcodes. Since the shift will require a rather significant
103
amount of refactoring, this module will return value builder-formatted
104
results, at first by merely running the code that was formerly in the
105
barcodes.pl value builder, but later by using C4::Barcodes.
106
107
=cut
108
109
1;
(-)a/cataloguing/value_builder/barcode.pl (-45 / +8 lines)
Lines 22-27 use warnings; Link Here
22
no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
22
no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
23
23
24
use C4::Context;
24
use C4::Context;
25
require C4::Barcodes::ValueBuilder;
25
require C4::Dates;
26
require C4::Dates;
26
27
27
my $DEBUG = 0;
28
my $DEBUG = 0;
Lines 55-68 the 3 scripts are inserted after the <input> in the html code Link Here
55
sub plugin_javascript {
56
sub plugin_javascript {
56
	my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
57
	my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
57
	my $function_name= "barcode".(int(rand(100000))+1);
58
	my $function_name= "barcode".(int(rand(100000))+1);
59
    my %args;
58
60
59
	# find today's date
61
	# find today's date
60
	my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
62
    ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
61
	my ($tag,$subfield)       =  GetMarcFromKohaField("items.barcode", '');
63
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
62
	my ($loctag,$locsubfield) =  GetMarcFromKohaField("items.homebranch", '');
64
    ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
63
65
64
	my $nextnum;
66
	my $nextnum;
65
	my $query;
66
    my $scr;
67
    my $scr;
67
	my $autoBarcodeType = C4::Context->preference("autoBarcode");
68
	my $autoBarcodeType = C4::Context->preference("autoBarcode");
68
    warn "Barcode type = $autoBarcodeType" if $DEBUG;
69
    warn "Barcode type = $autoBarcodeType" if $DEBUG;
Lines 77-127 sub plugin_javascript { Link Here
77
        </script>");
78
        </script>");
78
    }
79
    }
79
	if ($autoBarcodeType eq 'annual') {
80
	if ($autoBarcodeType eq 'annual') {
80
		$query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
81
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
81
		my $sth=$dbh->prepare($query);
82
		$sth->execute("$year%");
83
		while (my ($count)= $sth->fetchrow_array) {
84
            warn "Examining Record: $count" if $DEBUG;
85
    		$nextnum = $count if $count;
86
		}
87
		$nextnum++;
88
		$nextnum = sprintf("%0*d", "4",$nextnum);
89
		$nextnum = "$year-$nextnum";
90
	}
82
	}
91
	elsif ($autoBarcodeType eq 'incremental') {
83
	elsif ($autoBarcodeType eq 'incremental') {
92
		# not the best, two catalogers could add the same barcode easily this way :/
84
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
93
		$query = "select max(abs(barcode)) from items";
94
        my $sth = $dbh->prepare($query);
95
		$sth->execute();
96
		while (my ($count)= $sth->fetchrow_array) {
97
			$nextnum = $count;
98
		}
99
		$nextnum++;
100
    }
85
    }
101
    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
86
    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
102
        $year = substr($year, -2);
87
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
103
        $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
104
        my $sth = $dbh->prepare($query);
105
        $sth->execute("^[-a-zA-Z]{1,}$year");
106
        while (my ($count)= $sth->fetchrow_array) {
107
            $nextnum = $count if $count;
108
            $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
109
            warn "Existing incremental number = $nextnum" if $DEBUG;
110
        }
111
        $nextnum++;
112
        $nextnum = sprintf("%0*d", "4",$nextnum);
113
        $nextnum = $year . $mon . $nextnum;
114
        warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
115
        $scr = " 
116
        for (i=0 ; i<document.f.field_value.length ; i++) {
117
            if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
118
                fnum = i;
119
            }
120
        }
121
        if (\$('#' + id).val() == '' || force) {
122
            \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
123
        }
124
        ";
125
    }
88
    }
126
89
127
    # default js body (if not filled by hbyymmincr)
90
    # default js body (if not filled by hbyymmincr)
(-)a/cataloguing/value_builder/barcode_manual.pl (+132 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
# Copyright 2000-2002 Katipo Communications
3
# Parts copyright 2008-2010 Foundations Bible College
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
no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
23
24
use C4::Context;
25
require C4::Barcodes::ValueBuilder;
26
require C4::Dates;
27
28
my $DEBUG = 0;
29
30
=head1
31
32
plugin_parameters : other parameters added when the plugin is called by the dopop function
33
34
=cut
35
36
sub plugin_parameters {
37
#   my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
38
    return "";
39
}
40
41
=head1
42
43
plugin_javascript : the javascript function called when the user enters the subfield.
44
contain 3 javascript functions :
45
* one called when the field is entered (OnFocus). Named FocusXXX
46
* one called when the field is leaved (onBlur). Named BlurXXX
47
* one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
48
49
returns :
50
* XXX
51
* a variable containing the 3 scripts.
52
the 3 scripts are inserted after the <input> in the html code
53
54
=cut
55
56
sub plugin_javascript {
57
    my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
58
    my $function_name= "barcode".(int(rand(100000))+1);
59
    my %args;
60
61
    $args{dbh} = $dbh;
62
63
# find today's date
64
    ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
65
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
66
    ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
67
68
    my $nextnum;
69
    my $scr;
70
    my $autoBarcodeType = C4::Context->preference("autoBarcode");
71
    warn "Barcode type = $autoBarcodeType" if $DEBUG;
72
    if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
73
# don't return a value unless we have the appropriate syspref set
74
        return ($function_name, 
75
                "<script type=\"text/javascript\">
76
                // autoBarcodeType OFF (or not defined)
77
                function Focus$function_name() { return 0;}
78
                function  Clic$function_name() { return 0;}
79
                function  Blur$function_name() { return 0;}
80
                </script>");
81
    }
82
    if ($autoBarcodeType eq 'annual') {
83
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
84
    }
85
    elsif ($autoBarcodeType eq 'incremental') {
86
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
87
    }
88
    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
89
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
90
    }
91
92
# default js body (if not filled by hbyymmincr)
93
    $scr or $scr = <<END_OF_JS;
94
    if (\$('#' + id).val() == '' || force) {
95
        \$('#' + id).val('$nextnum');
96
    }
97
END_OF_JS
98
99
        my $js  = <<END_OF_JS;
100
    <script type="text/javascript">
101
        //<![CDATA[
102
103
        function Blur$function_name(index) {
104
            //barcode validation might go here
105
        }
106
107
    function Focus$function_name(subfield_managed, id, force) {
108
        return 0;
109
    }
110
111
    function Clic$function_name(id) {
112
        $scr
113
            return 0;
114
    }
115
    //]]>
116
    </script>
117
END_OF_JS
118
        return ($function_name, $js);
119
}
120
121
=head1
122
123
plugin: useless here
124
125
=cut
126
127
sub plugin {
128
# my ($input) = @_;
129
    return "";
130
}
131
132
1;
(-)a/t/Barcodes_ValueBuilder.t (-1 / +78 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
use strict;
3
use warnings;
4
use DBI;
5
use Test::More tests => 15;
6
use Test::MockModule;
7
8
BEGIN {
9
    use_ok('C4::Barcodes::ValueBuilder');
10
}
11
               
12
13
my $module = new Test::MockModule('C4::Context');
14
$module->mock('_new_dbh', sub {
15
                             my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
16
                             || die "Cannot create handle: $DBI::errstr\n";
17
                             return $dbh });
18
19
# Mock data
20
my $incrementaldata = [
21
    ['max(abs(barcode))'],
22
    ['33333074344563'],
23
];
24
25
26
my $dbh = C4::Context->dbh();
27
28
my %args = (
29
    year        => '2012',
30
    mon         => '07',
31
    day         => '30',
32
    tag         => '952',
33
    subfield    => 'p',
34
    loctag      => '952',
35
    locsubfield => 'a'
36
);
37
38
$dbh->{mock_add_resultset} = $incrementaldata;
39
my ($nextnum, $scr, $history);
40
41
($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
42
is($nextnum, 33333074344564, 'incremental barcode');
43
ok(length($scr) == 0, 'incremental javascript');
44
45
# This should run exactly one query so we can test
46
$history = $dbh->{mock_all_history};
47
is(scalar(@{$history}), 1, 'Correct number of statements executed for incremental barcode') ;
48
49
my $hbyymmincrdata = [
50
    ['number'],
51
    ['890'],
52
];
53
54
$dbh->{mock_add_resultset} = $hbyymmincrdata;
55
$dbh->{mock_clear_history} = 1;
56
($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
57
is($nextnum, '12070891', 'hbyymmincr barcode');
58
ok(length($scr) > 0, 'hbyymmincr javascript');
59
60
# This should run exactly one query so we can test
61
$history = $dbh->{mock_all_history};
62
is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;
63
64
my $annualdata = [
65
    ['max(cast( substring_index(barcode, \'-\',-1) as signed))'],
66
    ['34'],
67
];
68
69
$dbh->{mock_add_resultset} = $annualdata;
70
$dbh->{mock_clear_history} = 1;
71
($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
72
is($nextnum, '2012-0035', 'annual barcode');
73
ok(length($scr) == 0, 'annual javascript');
74
75
# This should run exactly one query so we can test
76
$history = $dbh->{mock_all_history};
77
is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;
78

Return to bug 8524