3 # script to generate items barcodes
5 # by Veleda Matias - matias_veleda@hotmail.com - Physics Library UNLP Argentina and
6 # Castañeda Sebastian - seba3c@yahoo.com.ar - Physics Library UNLP Argentina and
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA 02111-1307 USA
33 use PDF::API2::PDF::Utils;
34 use C4::Barcodes::PrinterConfig;
38 # This function returns the path to deal with the correct files, considering
39 # templates set and language.
42 my $templatesSet = C4::Context->preference('template');
43 my $lang = C4::Context->preference('opaclanguages');
44 if ($type eq "intranet") {
45 return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
47 return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
51 # Load a configuration file. Before use this function, check if that file exists.
52 sub loadConfFromFile {
53 my $fileName = shift @_;
55 open FILE, "<$fileName";
58 if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
66 # Save settings to a configuration file. It delete previous configuration settings.
68 my $fileName = shift @_;
69 my %keyValues = %{shift @_};
71 open FILE, ">$fileName";
73 foreach $i (keys(%keyValues)) {
74 print FILE $i." = ".$keyValues{$i}."\n";
79 # Load the config file.
80 my $filenameConf = &getPath("intranet")."/includes/labelConfig/itemsLabelConfig.conf";
81 my %labelConfig = &loadConfFromFile($filenameConf);
83 # Creates a CGI object and take its parameters
85 my $from = $cgi->param('from');
86 my $to = $cgi->param('to');
87 my $individualCodes = $cgi->param('individualCodes');
88 my $rangeType = $cgi->param('rangeType');
89 my $pageType = $cgi->param('pages');
90 my $label = $cgi->param('label');
91 my $numbersystem = $cgi->param('numbersystem');
92 my $text_under_label = $cgi->param('text_under_label');
94 # Generate the checksum from an inventary code
102 for ($i = length($code) - 1; $i >= 0; $i--){
104 $sum = $sum + ( 3 * substr($code, $i, 1) );
106 $sum = $sum + substr($code, $i, 1); }
107 $odd_parity = !$odd_parity;
109 my $check_digit = 10 - ($sum%10);
110 if ($check_digit==10) {
113 return $code.$check_digit;
116 my $currentCode = shift @_;
117 $currentCode = &calculateDigit($currentCode);
121 # Assigns a temporary name to the PDF file
123 my ($from, $to) = @_;
124 my $ip = $cgi->remote_addr();
125 my $random = int(rand(1000000));
126 my $timeObj = localtime();
127 my ($day, $month, $year, $hour, $min, $sec) = ($timeObj->mday,
129 $timeObj->year + 1900,
133 my $tmpFileName = $random.'-'.$ip.'-(From '.$from.' to '.$to.')-['.$day.'.'.$month.'.'.$year.']-['.$hour.':'.$min.':'.$sec.'].pdf';
137 # Takes inventary codes from database and if they are between
138 # the interval specify by parameters, it generates the correspond barcodes
139 sub barcodesGenerator {
140 my ($from, $to, $rangeType, $individualCodes,$text_under_label) = @_;
141 # Returns a database handler
142 my $dbh = C4::Context->dbh;
143 # Create the query to database
144 # Assigns a temporary filename for the pdf file
145 my $tmpFileName = &assingFilename($from, $to);
146 if ($rangeType eq 'continuous2') {
147 # Set the temp directory for pdf´s files
148 if (!defined($ENV{'TEMP'})) {
149 $ENV{'TEMP'} = '/tmp/';
151 $tmpFileName = $ENV{'TEMP'}.$tmpFileName;
152 # Creates a PDF object
153 my $pdf = PDF::API2->new(-file => $tmpFileName);
154 # Set the positions where barcodes are going to be placed
155 C4::Barcodes::PrinterConfig::setPositionsForX($labelConfig{'marginLeft'}, $labelConfig{'labelWidth'}, $labelConfig{'columns'}, $labelConfig{'pageType'});
156 C4::Barcodes::PrinterConfig::setPositionsForY($labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'}, $labelConfig{'rows'}, $labelConfig{'pageType'});
157 # Creates a font object
158 my $tr = $pdf->corefont('Helvetica-Bold');
160 my ($page, $gfx, $text);
161 for (my $code=$from; $code<=$to; $code++) {
162 # Generetase checksum
163 my $codeC = &checksum($code);
164 # Generate the corresponde barcode to $code
165 my $barcode = $pdf->barcode(-font => $tr, # The font object to use
166 -type => 'ean13', # Standard of codification
167 -code => $codeC, # Text to codify
168 -extn => '012345', # Barcode extension (if it is aplicable)
169 -umzn => 10, # Top limit of the finished bar
170 -lmzn => 10, # Bottom limit of the finished bar
171 -zone => 15, # Bars size
172 -quzn => 0, # Space destinated for legend
173 -ofwt => 0.01, # Bars width
174 -fnsz => 8, # Font size
178 (my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label) = C4::Barcodes::PrinterConfig::getLabelPosition(
186 # Assigns a barcodes to $gfx
187 $gfx->barcode($barcode, $x, $y , (72/$labelConfig{'systemDpi'}));
188 # Assigns the additional information to the barcode (Legend)
189 $text->translate($x - 48, $y - 22);
190 if ($text_under_label) {
191 $text->text($text_under_label);
194 # Writes the objects added in $gfx to $page
195 $pdf->finishobjects($page,$gfx, $text);
196 # Save changes to the PDF
198 # Close the conection with the PDF file
201 print $cgi->redirect("/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
204 if ($individualCodes ne "") {
205 $rangeCondition = "AND (I.barcode IN " . $individualCodes . ")";
207 $rangeCondition = "AND (I.barcode >= " . $from . " AND I.barcode <=" . $to . " )";
210 my $query = "SELECT CONCAT('$numbersystem',REPEAT('0',((12 - LENGTH('$numbersystem')) - LENGTH(I.barcode))), I.barcode) AS Codigo, B.title, B.author FROM biblio B, items I WHERE (I.biblionumber = B.biblioNumber ) " .$rangeCondition. " AND (I.barcode <> 'FALTA') ORDER BY Codigo";
213 my $sth = $dbh->prepare($query);
216 if ($sth->rows) { # There are inventary codes
217 # Set the temp directory for pdf´s files
218 if (!defined($ENV{'TEMP'})) {
219 $ENV{'TEMP'} = '/tmp/';
221 # Assigns a temporary filename for the pdf file
222 my $tmpFileName = &assingFilename($from, $to);
223 $tmpFileName = $ENV{'TEMP'}.$tmpFileName;
224 # Creates a PDF object
225 my $pdf = PDF::API2->new(-file => $tmpFileName);
226 # Set the positions where barcodes are going to be placed
227 C4::Barcodes::PrinterConfig::setPositionsForX($labelConfig{'marginLeft'}, $labelConfig{'labelWidth'}, $labelConfig{'columns'}, $labelConfig{'pageType'});
228 C4::Barcodes::PrinterConfig::setPositionsForY($labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'}, $labelConfig{'rows'}, $labelConfig{'pageType'});
229 # Creates a font object
230 my $tr = $pdf->corefont('Helvetica-Bold');
232 my ($page, $gfx, $text);
233 while (my ($code,$title,$author) = $sth->fetchrow_array) {
234 # Generetase checksum
235 $code = &checksum($code);
236 # Generate the corresponde barcode to $code
237 my $barcode = $pdf->barcode(-font => $tr, # The font object to use
238 -type => 'ean13', # Standard of codification
239 -code => $code, # Text to codify
240 -extn => '012345', # Barcode extension (if it is aplicable)
241 -umzn => 10, # Top limit of the finished bar
242 -lmzn => 10, # Bottom limit of the finished bar
243 -zone => 15, # Bars size
244 -quzn => 0, # Space destinated for legend
245 -ofwt => 0.01, # Bars width
246 -fnsz => 8, # Font size
250 (my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label) = C4::Barcodes::PrinterConfig::getLabelPosition(
258 # Assigns a barcodes to $gfx
259 $gfx->barcode($barcode, $x, $y , (72/$labelConfig{'systemDpi'}));
260 # Assigns the additional information to the barcode (Legend)
261 $text->translate($x - 48, $y - 22);
262 if ($text_under_label) {
263 $text->text($text_under_label);
265 $text->text(substr $title, 0, 30);
266 $text->translate($x - 48, $y - 29);
267 $text->text(substr $author, 0, 30);
270 # Writes the objects added in $gfx to $page
271 $pdf->finishobjects($page,$gfx, $text);
272 # Save changes to the PDF
274 # Close the conection with the PDF file
277 print $cgi->redirect("/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
279 # Rollback and shows the error legend
280 print $cgi->redirect("/cgi-bin/koha/barcodes/barcodes.pl?error=1");
286 barcodesGenerator($from, $to, $rangeType, $individualCodes,$text_under_label);