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

(-)a/C4/Barcodes.pm (-5 / +7 lines)
Lines 27-32 use C4::Barcodes::hbyymmincr; Link Here
27
use C4::Barcodes::annual;
27
use C4::Barcodes::annual;
28
use C4::Barcodes::incremental;
28
use C4::Barcodes::incremental;
29
use C4::Barcodes::EAN13;
29
use C4::Barcodes::EAN13;
30
use C4::Barcodes::preyyyymmincr;
30
31
31
use vars qw($max $prefformat);
32
use vars qw($max $prefformat);
32
33
Lines 154-164 sub default_self { Link Here
154
}
155
}
155
156
156
our $types = {
157
our $types = {
157
	annual      => sub {C4::Barcodes::annual->new_object(@_);     },
158
    annual        => sub {C4::Barcodes::annual->new_object(@_);       },
158
	incremental => sub {C4::Barcodes::incremental->new_object(@_);},
159
    incremental   => sub {C4::Barcodes::incremental->new_object(@_);  },
159
	hbyymmincr  => sub {C4::Barcodes::hbyymmincr->new_object(@_); },
160
    hbyymmincr    => sub {C4::Barcodes::hbyymmincr->new_object(@_);   },
160
	OFF         => sub {C4::Barcodes::OFF->new_object(@_);        },
161
    OFF           => sub {C4::Barcodes::OFF->new_object(@_);          },
161
    EAN13       => sub {C4::Barcodes::EAN13->new_object(@_);      },
162
    EAN13         => sub {C4::Barcodes::EAN13->new_object(@_);        },
163
    preyyyymmincr => sub {C4::Barcodes::preyyyymmincr->new_object(@_);},
162
};
164
};
163
165
164
sub new {
166
sub new {
(-)a/C4/Barcodes/ValueBuilder.pm (+54 lines)
Lines 89-94 sub get_barcode { Link Here
89
    return $nextnum;
89
    return $nextnum;
90
}
90
}
91
91
92
package C4::Barcodes::ValueBuilder::preyyyymmincr;
93
use C4::Context;
94
use YAML::XS;
95
my $DEBUG = 0;
96
97
sub get_barcode {
98
    my ($args) = @_;
99
    my $nextnum;
100
    my $barcode;
101
    my $branchcode = $args->{branchcode};
102
    my $query;
103
    my $sth;
104
105
    # Getting the barcodePrefixes
106
    my $branchPrefixes = C4::Context->preference("BarcodePrefix");
107
    my $yaml = YAML::XS::Load(
108
                    Encode::encode(
109
                        'UTF-8',
110
                        $branchPrefixes,
111
                        Encode::FB_CROAK
112
                    )
113
                );
114
115
    my $prefix = $yaml->{$branchcode} || $yaml->{'Default'};
116
117
    $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) from items where barcode REGEXP ?";
118
    $sth=C4::Context->dbh->prepare($query);
119
    $sth->execute("^$prefix$args->{year}$args->{mon}");
120
121
    while (my ($count)= $sth->fetchrow_array) {
122
        $nextnum = $count if $count;
123
        $nextnum = 0 if $nextnum && $nextnum == 9999;
124
    }
125
126
    $nextnum++;
127
    $nextnum = sprintf("%0*d", "5",$nextnum);
128
129
    $barcode = $prefix;
130
    $barcode .= $args->{year}.$args->{mon}.$nextnum;
131
132
    my $scr = qq~
133
        let elt = \$("#"+id);
134
        let branchcode = elt.parents('fieldset.rows:first')
135
                            .find('input[name="kohafield"][value="items.homebranch"]')
136
                            .siblings("select")
137
                            .val();
138
        if ( \$(elt).val() == '' ) {
139
            \$(elt).val('$barcode');
140
        }
141
    ~;
142
143
    return $barcode, $scr;
144
}
145
92
1;
146
1;
93
147
94
148
(-)a/C4/Barcodes/preyyyymmincr.pm (+105 lines)
Line 0 Link Here
1
package C4::Barcodes::preyyyymmincr;
2
3
# Copyright 2022 Koha Development team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use strict;
21
use warnings;
22
23
use Carp qw( carp );
24
25
use C4::Context;
26
27
use Koha::DateUtils qw( dt_from_string output_pref );
28
29
use vars qw(@ISA);
30
31
BEGIN {
32
    @ISA = qw(C4::Barcodes);
33
}
34
35
sub new_object {
36
    my $class = shift;
37
	my $type = ref($class) || $class;
38
	my $self = $type->default_self('preyyyymmincr');
39
40
    my $branchcode = C4::Context->userenv->{'branch'};
41
    my $branchPrefixes = C4::Context->preference("BarcodePrefix");
42
    my $yaml = YAML::XS::Load(
43
                    Encode::encode(
44
                        'UTF-8',
45
                        $branchPrefixes,
46
                        Encode::FB_CROAK
47
                    )
48
                );
49
    my $prefix = $yaml->{$branchcode} || $yaml->{'Default'};
50
51
    $self->{prefix} = $prefix;
52
    $self->{datetime} = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
53
54
	return bless $self, $type;
55
}
56
57
sub initial {
58
    my $self = shift;
59
60
    my $prefix = $self->{prefix};
61
    my ($year, $month, $day) = split('-', $self->{datetime});
62
63
    return $prefix.$year.$month.'00001';
64
}
65
66
sub db_max {
67
    my $self = shift;
68
69
    my $prefix = $self->{prefix};
70
    my ($year, $month, $day) = split('-', $self->{datetime});
71
72
    my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) from items where barcode REGEXP ?";
73
    my $sth=C4::Context->dbh->prepare($query);
74
    $sth->execute("^$prefix$year$month");
75
76
    my $nextnum;
77
    while (my ($count)= $sth->fetchrow_array) {
78
        $nextnum = $count if $count;
79
        $nextnum = 0 if $nextnum && $nextnum == 9999;
80
    }
81
82
    $nextnum = sprintf("%0*d", "5",$nextnum);
83
84
    return $nextnum;
85
}
86
87
sub parse {
88
    my $self = shift;
89
90
    my $prefix = $self->{prefix};
91
    my ($year, $month, $day) = split('-', $self->{datetime});
92
93
    my $head = $prefix.$year.$month;
94
    my $incr = (@_) ? shift : $self->value;
95
    my $barcode = $head.$incr;
96
    unless ($incr){
97
        carp "Barcode '$barcode' has no incrementing part!";
98
		return ($barcode,undef,undef);
99
    }
100
101
    return ($head, $incr, '');
102
}
103
104
1;
105
__END__
(-)a/cataloguing/value_builder/barcode.pl (+4 lines)
Lines 37-42 my $builder = sub { Link Here
37
	# find today's date
37
	# find today's date
38
    ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
38
    ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
39
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField( "items.barcode" );
39
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField( "items.barcode" );
40
    ($args{branchcode}) = C4::Context->userenv->{'branch'};
40
41
41
	my $nextnum;
42
	my $nextnum;
42
    my $scr;
43
    my $scr;
Lines 73-78 my $builder = sub { Link Here
73
            $nextnum++;
74
            $nextnum++;
74
        }
75
        }
75
    }
76
    }
77
    elsif ($autoBarcodeType eq 'preyyyymmincr') {      # Generates a barcode where pre = branch specific prefix set on systempreference BarcodePrefix, yyyymm = year/month catalogued, incr = incremental number
78
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::preyyyymmincr::get_barcode(\%args);
79
    }
76
    else {
80
    else {
77
        warn "ERROR: unknown autoBarcode: $autoBarcodeType";
81
        warn "ERROR: unknown autoBarcode: $autoBarcodeType";
78
    }
82
    }
(-)a/cataloguing/value_builder/barcode_manual.pl (+4 lines)
Lines 38-43 my $builder = sub { Link Here
38
# find today's date
38
# find today's date
39
    ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
39
    ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
40
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField( "items.barcode" );
40
    ($args{tag},$args{subfield})       =  GetMarcFromKohaField( "items.barcode" );
41
    ($args{branchcode}) = C4::Context->userenv->{'branch'};
41
42
42
    my $nextnum;
43
    my $nextnum;
43
    my $scr;
44
    my $scr;
Lines 55-60 my $builder = sub { Link Here
55
    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
56
    elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
56
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
57
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
57
    }
58
    }
59
    elsif ($autoBarcodeType eq 'preyyyymmincr') {      # Generates a barcode where pre = branch specific prefix set on systempreference BarcodePrefix, yyyymm = year/month catalogued, incr = incremental number
60
        ($nextnum, $scr) = C4::Barcodes::ValueBuilder::preyyyymmincr::get_barcode(\%args);
61
    }
58
62
59
# default js body (if not filled by hbyymmincr)
63
# default js body (if not filled by hbyymmincr)
60
    $scr or $scr = <<END_OF_JS;
64
    $scr or $scr = <<END_OF_JS;
(-)a/installer/data/mysql/atomicupdate/bug_30328.pl (+15 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number => "30328",
5
    description => "Add option to create barcode with branch specific prefix",
6
    up => sub {
7
        my ($args) = @_;
8
        my ($dbh, $out) = @$args{qw(dbh out)};
9
        # Do you stuffs here
10
        $dbh->do(q{ UPDATE IGNORE systempreferences SET options = 'incremental|annual|hbyymmincr|EAN13|preyyyymmincr|OFF' , explanation = 'Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch; preyyyymmincr of the form PRE2021030001 where PRE = branch specific prefix set on systempreference BarcodePrefix' WHERE variable = 'autoBarcode'});
11
        $dbh->do(q{ INSERT IGNORE INTO systempreferences ( variable, value, options, explanation, type ) VALUES ('BarcodePrefix','','','Defines the barcode prefixes when the autoBarcode value is set as preyyyymmincr','Textarea')});
12
        # Print useful stuff here
13
        say $out "Update is going well so far";
14
    },
15
};
(-)a/installer/data/mysql/mandatory/sysprefs.sql (-1 / +2 lines)
Lines 72-78 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
72
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
72
('AuthoritySeparator','--','10','Used to separate a list of authorities in a display. Usually --','free'),
73
('AuthorityXSLTResultsDisplay','','','Enable XSL stylesheet control over authority results page display on intranet','Free'),
73
('AuthorityXSLTResultsDisplay','','','Enable XSL stylesheet control over authority results page display on intranet','Free'),
74
('AuthSuccessLog','0',NULL,'If enabled, log successful authentications','YesNo'),
74
('AuthSuccessLog','0',NULL,'If enabled, log successful authentications','YesNo'),
75
('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch','Choice'),
75
('autoBarcode','OFF','incremental|annual|hbyymmincr|EAN13|preyyyymmincr|OFF','Used to autogenerate a barcode: incremental will be of the form 1, 2, 3; annual of the form 2007-0001, 2007-0002; hbyymmincr of the form HB08010001 where HB=Home Branch; preyyyymmincr of the form PRE2021030001 where PRE = branch specific prefix set on systempreference BarcodePrefix','Choice'),
76
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
76
('AutoCreateAuthorities','0',NULL,'Automatically create authorities that do not exist when cataloging records.','YesNo'),
77
('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
77
('AutoCreditNumber', '', '', 'Automatically generate a number for account credits', 'Choice'),
78
('AutoEmailNewUser','0',NULL,'Send an email to newly created patrons.','YesNo'),
78
('AutoEmailNewUser','0',NULL,'Send an email to newly created patrons.','YesNo'),
Lines 96-101 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
96
('BakerTaylorEnabled','0','','Enable or disable all Baker & Taylor features.','YesNo'),
96
('BakerTaylorEnabled','0','','Enable or disable all Baker & Taylor features.','YesNo'),
97
('BakerTaylorPassword','','','Baker & Taylor Password for Content Cafe (external content)','Free'),
97
('BakerTaylorPassword','','','Baker & Taylor Password for Content Cafe (external content)','Free'),
98
('BakerTaylorUsername','','','Baker & Taylor Username for Content Cafe (external content)','Free'),
98
('BakerTaylorUsername','','','Baker & Taylor Username for Content Cafe (external content)','Free'),
99
('BarcodePrefix','','','Defines the barcode prefixes when the autoBarcode value is set as preyyyymmincr','Textarea'),
99
('BarcodeSeparators','\\s\\r\\n','','Splitting characters for barcodes','Free'),
100
('BarcodeSeparators','\\s\\r\\n','','Splitting characters for barcodes','Free'),
100
('BasketConfirmations','1','always ask for confirmation.|do not ask for confirmation.','When closing or reopening a basket,','Choice'),
101
('BasketConfirmations','1','always ask for confirmation.|do not ask for confirmation.','When closing or reopening a basket,','Choice'),
101
('BatchCheckouts','0','','Enable or disable batch checkouts','YesNo'),
102
('BatchCheckouts','0','','Enable or disable batch checkouts','YesNo'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/cataloguing.pref (+10 lines)
Lines 123-129 Cataloging: Link Here
123
                  annual: generated in the form <year>-0001, <year>-0002.
123
                  annual: generated in the form <year>-0001, <year>-0002.
124
                  hbyymmincr: generated in the form <branchcode>yymm0001.
124
                  hbyymmincr: generated in the form <branchcode>yymm0001.
125
                  EAN13: incremental EAN-13 barcodes.
125
                  EAN13: incremental EAN-13 barcodes.
126
                  preyyyymmincr: generated in the form <prefix>yyyymm00001.
126
                  "OFF": not generated automatically.
127
                  "OFF": not generated automatically.
128
            - Define the behavior of the <code><&#8203;prefix>yyyymm00001</code> option using <a href="/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=BarcodePrefix">BarcodePrefix</a>.
127
        -
129
        -
128
            - When a new item is added,
130
            - When a new item is added,
129
            - pref: PrefillItem
131
            - pref: PrefillItem
Lines 165-170 Cataloging: Link Here
165
            - and record's last modifier name in MARC subfield
167
            - and record's last modifier name in MARC subfield
166
            - pref: MarcFieldForModifierName
168
            - pref: MarcFieldForModifierName
167
            - ". <br/><strong>NOTE:</strong> Use a dollar sign between field and subfield like 123$a."
169
            - ". <br/><strong>NOTE:</strong> Use a dollar sign between field and subfield like 123$a."
170
        -
171
            - "Define branch specific barcode prefixes in a YAML format."
172
            - "This defines the behavior of the <code><&#8203;prefix>yyyymm00001</code> option of <a href='/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=autoBarcode'>autoBarcode</a>."
173
            - "Barcodes generated look like: <code>DEF20220400001</code>"
174
            - pref: BarcodePrefix
175
              type: textarea
176
              syntax: text/x-yaml
177
              class: code
168
    Display:
178
    Display:
169
        -
179
        -
170
            - 'Separate main entry and subdivisions with '
180
            - 'Separate main entry and subdivisions with '
(-)a/t/Barcodes_preyyyymmincr.t (+10 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use strict;
4
use warnings;
5
6
use Test::More tests => 1;
7
8
BEGIN {
9
        use_ok('C4::Barcodes::preyyyymmincr');
10
}
(-)a/t/db_dependent/Barcodes_ValueBuilder.t (-2 / +29 lines)
Lines 16-27 Link Here
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
18
19
use Test::More tests => 9;
19
use Test::More tests => 12;
20
use Test::MockModule;
20
use Test::MockModule;
21
use t::lib::TestBuilder;
21
use t::lib::TestBuilder;
22
22
23
use Koha::Database;
23
use Koha::Database;
24
24
25
use t::lib::Mocks;
26
25
BEGIN {
27
BEGIN {
26
    use_ok('C4::Barcodes::ValueBuilder', qw( get_barcode ));
28
    use_ok('C4::Barcodes::ValueBuilder', qw( get_barcode ));
27
};
29
};
Lines 85-88 my $item_5 = $builder->build_sample_item( Link Here
85
is($nextnum, '979', 'incremental barcode');
87
is($nextnum, '979', 'incremental barcode');
86
is($scr, undef, 'incremental javascript');
88
is($scr, undef, 'incremental javascript');
87
89
90
$dbh->do(q|DELETE FROM items|);
91
my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
92
93
my $prefix_yaml = 'Default: DEF
94
'.$library_1->branchcode.': TEST';
95
t::lib::Mocks::mock_preference( 'BarcodePrefix', $prefix_yaml );
96
97
my $item_6 = $builder->build_sample_item(
98
    {
99
        barcode => 'TEST20120700001',
100
        homebranch   => $library_1->branchcode
101
    }
102
);
103
104
($args{branchcode}) = $library_1->branchcode;
105
($nextnum, $scr) = C4::Barcodes::ValueBuilder::preyyyymmincr::get_barcode(\%args);
106
is($nextnum, 'TEST20120700002', 'preyyyymmincr barcode test branch specific prefix');
107
ok(length($scr) > 0, 'preyyyymmincr javascript');
108
109
$dbh->do(q|DELETE FROM items|);
110
my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
111
112
($args{branchcode}) = $library_2->branchcode;
113
($nextnum, $scr) = C4::Barcodes::ValueBuilder::preyyyymmincr::get_barcode(\%args);
114
is($nextnum, 'DEF20120700001', 'preyyyymmincr barcode test default prefix');
115
88
$schema->storage->txn_rollback;
116
$schema->storage->txn_rollback;
89
- 

Return to bug 30328