Bug 31908: Resolve second login with another userid
[koha.git] / C4 / Barcodes / ValueBuilder.pm
1 package C4::Barcodes::ValueBuilder;
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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 package C4::Barcodes::ValueBuilder::incremental;
22 use Modern::Perl;
23 use C4::Context;
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(cast(barcode as unsigned)) 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
44 sub get_barcode {
45     my ($args) = @_;
46     my $nextnum = 0;
47     my $year = substr($args->{year}, -2);
48     my $month = $args->{mon};
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$month");
52     while (my ($count)= $sth->fetchrow_array) {
53         $nextnum = $count if $count;
54         $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 9999 items per month
55     }
56     $nextnum++;
57     $nextnum = sprintf("%0*d", "4",$nextnum);
58     $nextnum = $year . $month . $nextnum;
59     my $scr = qq~
60         let elt = \$("#"+id);
61         let homebranch = elt.parents('fieldset.rows:first')
62                             .find('input[name="kohafield"][value="items.homebranch"]')
63                             .siblings("select")
64                             .val();
65
66         if ( \$(elt).val() == '' ) {
67             \$(elt).val(homebranch + '$nextnum');
68         }
69     ~;
70     return $nextnum, $scr;
71 }
72
73
74 package C4::Barcodes::ValueBuilder::annual;
75 use C4::Context;
76
77 sub get_barcode {
78     my ($args) = @_;
79     my $nextnum;
80     my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
81     my $sth=C4::Context->dbh->prepare($query);
82     $sth->execute($args->{year} . '-%');
83     while (my ($count)= $sth->fetchrow_array) {
84         $nextnum = $count if $count;
85     }
86     $nextnum++;
87     $nextnum = sprintf("%0*d", "4",$nextnum);
88     $nextnum = "$args->{year}-$nextnum";
89     return $nextnum;
90 }
91
92 1;
93
94
95 =head1 Barcodes::ValueBuilder
96
97 This module is intended as a shim to ease the eventual transition from
98 having all barcode-related code in the value builder plugin .pl file
99 to using C4::Barcodes. Since the shift will require a rather significant
100 amount of refactoring, this module will return value builder-formatted
101 results, at first by merely running the code that was formerly in the
102 barcodes.pl value builder, but later by using C4::Barcodes.
103
104 =cut
105
106 1;