Bug 26673: Remove shebangs from Perl modules
[koha.git] / C4 / Barcodes / ValueBuilder.pm
1 # Copyright 2008-2010 Foundations Bible College
2 # Parts copyright 2012 C & P Bibliography Services
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 package C4::Barcodes::ValueBuilder::incremental;
20 use Modern::Perl;
21 use C4::Context;
22
23 sub get_barcode {
24     my ($args) = @_;
25     my $nextnum;
26     # not the best, two catalogers could add the same barcode easily this way :/
27     my $query = "select max(abs(barcode)) from items";
28     my $sth = C4::Context->dbh->prepare($query);
29     $sth->execute();
30     while (my ($count)= $sth->fetchrow_array) {
31         $nextnum = $count;
32     }
33     $nextnum++;
34     return $nextnum;
35 }
36
37 1;
38
39 package C4::Barcodes::ValueBuilder::hbyymmincr;
40 use C4::Context;
41
42 sub get_barcode {
43     my ($args) = @_;
44     my $nextnum = 0;
45     my $year = substr($args->{year}, -2);
46     my $month = $args->{mon};
47     my $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
48     my $sth = C4::Context->dbh->prepare($query);
49     $sth->execute("^[-a-zA-Z]{1,}$year$month");
50     while (my ($count)= $sth->fetchrow_array) {
51         $nextnum = $count if $count;
52         $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 9999 items per month
53     }
54     $nextnum++;
55     $nextnum = sprintf("%0*d", "4",$nextnum);
56     $nextnum = $year . $month . $nextnum;
57     my $scr = "
58         var form = document.getElementById('f');
59         if ( !form ) {
60             form = document.getElementById('serials_edit');
61         }
62         if ( !form ) {
63             form = document.getElementById('Aform');
64         }
65         for (i=0 ; i<form.field_value.length ; i++) {
66             if (form.tag[i].value == '$args->{loctag}' && form.subfield[i].value == '$args->{locsubfield}') {
67                 fnum = i;
68             }
69         }
70     if (\$('#' + id).val() == '') {
71         \$('#' + id).val(form.field_value[fnum].value + '$nextnum');
72     }
73     ";
74     return $nextnum, $scr;
75 }
76
77
78 package C4::Barcodes::ValueBuilder::annual;
79 use C4::Context;
80
81 sub get_barcode {
82     my ($args) = @_;
83     my $nextnum;
84     my $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
85     my $sth=C4::Context->dbh->prepare($query);
86     $sth->execute($args->{year} . '-%');
87     while (my ($count)= $sth->fetchrow_array) {
88         $nextnum = $count if $count;
89     }
90     $nextnum++;
91     $nextnum = sprintf("%0*d", "4",$nextnum);
92     $nextnum = "$args->{year}-$nextnum";
93     return $nextnum;
94 }
95
96 1;
97
98
99 =head1 Barcodes::ValueBuilder
100
101 This module is intended as a shim to ease the eventual transition from
102 having all barcode-related code in the value builder plugin .pl file
103 to using C4::Barcodes. Since the shift will require a rather significant
104 amount of refactoring, this module will return value builder-formatted
105 results, at first by merely running the code that was formerly in the
106 barcodes.pl value builder, but later by using C4::Barcodes.
107
108 =cut
109
110 1;