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
32 #FIXME : module deprecated ?
36 use C4::Barcodes::PrinterConfig;
39 # This function returns the path to deal with the correct files, considering
40 # templates set and language.
43 my $templatesSet = C4::Context->preference('template');
44 my $lang = C4::Context->preference('opaclanguages');
45 if ( $type eq "intranet" ) {
46 return "$ENV{'DOCUMENT_ROOT'}/intranet-tmpl/$templatesSet/$lang";
49 return "$ENV{'DOCUMENT_ROOT'}/opac-tmpl/$templatesSet/$lang";
53 # Load a configuration file. Before use this function, check if that file exists.
54 sub loadConfFromFile {
55 my $fileName = shift @_;
57 open FILE, "<$fileName";
60 if (/\s*([\w_]*)\s*=\s*([\[\]\<\>\w_\s:@,\.-]*)\s*/) {
68 # Save settings to a configuration file. It delete previous configuration settings.
70 my $fileName = shift @_;
71 my %keyValues = %{ shift @_ };
73 open FILE, ">$fileName";
74 foreach $i ( keys(%keyValues) ) {
75 print FILE $i . " = " . $keyValues{$i} . "\n";
80 # Load the config file.
82 &getPath("intranet") . "/includes/labelConfig/itemsLabelConfig.conf";
83 my %labelConfig = &loadConfFromFile($filenameConf);
85 # Creates a CGI object and take its parameters
87 my $from = $cgi->param('from');
88 my $to = $cgi->param('to');
89 my $individualCodes = $cgi->param('individualCodes');
90 my $rangeType = $cgi->param('rangeType');
91 my $pageType = $cgi->param('pages');
92 my $label = $cgi->param('label');
93 my $numbersystem = $cgi->param('numbersystem');
94 my $text_under_label = $cgi->param('text_under_label');
96 # Generate the checksum from an inventary code
104 for ( $i = length($code) - 1 ; $i >= 0 ; $i-- ) {
106 $sum = $sum + ( 3 * substr( $code, $i, 1 ) );
109 $sum = $sum + substr( $code, $i, 1 );
111 $odd_parity = !$odd_parity;
113 my $check_digit = 10 - ( $sum % 10 );
114 if ( $check_digit == 10 ) {
117 return $code . $check_digit;
120 my $currentCode = shift @_;
121 $currentCode = &calculateDigit($currentCode);
125 # Assigns a temporary name to the PDF file
127 my ( $from, $to ) = @_;
128 my $ip = $cgi->remote_addr();
129 my $random = int( rand(1000000) );
130 my $timeObj = localtime();
131 my ( $day, $month, $year, $hour, $min, $sec ) = (
134 $timeObj->year + 1900,
135 $timeObj->hour, $timeObj->min, $timeObj->sec
153 #grabs a callnumber for the specified barcode
156 "select dewey from items,biblioitems where items.biblionumber=biblioitems.biblionumber and items.barcode=?";
157 my $dbh = C4::Context->dbh;
158 my $sth = $dbh->prepare($query);
159 $sth->execute($barcode);
160 my ($callnum) = $sth->fetchrow_array();
161 warn "Call number is:" . $barcode;
165 # Takes inventary codes from database and if they are between
166 # the interval specify by parameters, it generates the correspond barcodes
167 sub barcodesGenerator {
168 my ( $from, $to, $rangeType, $individualCodes, $text_under_label ) = @_;
170 # Returns a database handler
171 my $dbh = C4::Context->dbh;
173 # Create the query to database
174 # Assigns a temporary filename for the pdf file
175 my $tmpFileName = &assingFilename( $from, $to );
177 # warn "range type: ".$rangeType;
178 if ( $rangeType eq 'continuous' ) {
180 # Set the temp directory for pdf´s files
181 if ( !defined( $ENV{'TEMP'} ) ) {
182 $ENV{'TEMP'} = '/tmp/';
184 $tmpFileName = $ENV{'TEMP'} . $tmpFileName;
186 # Creates a PDF object
187 my $pdf = PDF::API2->new( -file => $tmpFileName );
189 # Set the positions where barcodes are going to be placed
190 C4::Barcodes::PrinterConfig::setPositionsForX(
191 $labelConfig{'marginLeft'}, $labelConfig{'labelWidth'},
192 $labelConfig{'columns'}, $labelConfig{'pageType'}
194 C4::Barcodes::PrinterConfig::setPositionsForY(
195 $labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'},
196 $labelConfig{'rows'}, $labelConfig{'pageType'}
199 # Creates a font object
200 my $tr = $pdf->corefont('Helvetica-Bold');
203 my ( $page, $gfx, $text );
204 for ( my $code = $from ; $code <= $to ; $code++ ) {
206 # Generetase checksum
207 my $codeC = &checksum($code);
209 # Generate the corresponde barcode to $code
210 # warn "Code is :-->".$codeC."<--";
211 my $barcode = $pdf->barcode(
212 -font => $tr, # The font object to use
213 -type => 'ean128', # Standard of codification
214 -code => $codeC, # Text to codify
215 -extn => '012345', # Barcode extension (if it is aplicable)
216 -umzn => 10, # Top limit of the finished bar
217 -lmzn => 10, # Bottom limit of the finished bar
218 -zone => 15, # Bars size
219 -quzn => 0, # Space destinated for legend
220 -ofwt => 0.01, # Bars width
221 -fnsz => 8, # Font size
225 ( my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label ) =
226 C4::Barcodes::PrinterConfig::getLabelPosition( $label, $pdf,
227 $page, $gfx, $text, $tr, $pageType );
229 # Assigns a barcodes to $gfx
230 $gfx->barcode( $barcode, $x, $y,
231 ( 72 / $labelConfig{'systemDpi'} ) );
233 # Assigns the additional information to the barcode (Legend)
234 $text->translate( $x - 48, $y - 22 );
236 #warn "code is ".$codeC;
237 if ($text_under_label) {
238 $text->text($text_under_label);
241 $text->text( getCallnum($code) );
245 # Writes the objects added in $gfx to $page
246 $pdf->finishobjects( $page, $gfx, $text );
248 # Save changes to the PDF
251 # Close the conection with the PDF file
255 print $cgi->redirect(
256 "/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
260 if ( $individualCodes ne "" ) {
261 $rangeCondition = "AND (I.barcode IN " . $individualCodes . ")";
265 "AND (I.barcode >= " . $from . " AND I.barcode <=" . $to . " )";
269 "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 ) "
271 . " AND (I.barcode <> 'FALTA') ORDER BY Codigo";
274 my $sth = $dbh->prepare($query);
278 if ( $sth->rows ) { # There are inventary codes
279 # Set the temp directory for pdf´s files
280 if ( !defined( $ENV{'TEMP'} ) ) {
281 $ENV{'TEMP'} = '/tmp/';
284 # Assigns a temporary filename for the pdf file
285 my $tmpFileName = &assingFilename( $from, $to );
286 $tmpFileName = $ENV{'TEMP'} . $tmpFileName;
288 # Creates a PDF object
289 my $pdf = PDF::API2->new( -file => $tmpFileName );
291 # Set the positions where barcodes are going to be placed
292 C4::Barcodes::PrinterConfig::setPositionsForX(
293 $labelConfig{'marginLeft'}, $labelConfig{'labelWidth'},
294 $labelConfig{'columns'}, $labelConfig{'pageType'}
296 C4::Barcodes::PrinterConfig::setPositionsForY(
297 $labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'},
298 $labelConfig{'rows'}, $labelConfig{'pageType'}
301 # Creates a font object
302 my $tr = $pdf->corefont('Helvetica-Bold');
305 my ( $page, $gfx, $text );
306 while ( my ( $code, $dewey, $title, $author ) =
307 $sth->fetchrow_array )
310 # Generetase checksum
311 $code = &checksum($code);
313 # Generate the corresponde barcode to $code
314 my $barcode = $pdf->barcode(
315 -font => $tr, # The font object to use
316 -type => 'ean13', # Standard of codification
317 -code => $code, # Text to codify
318 -extn => '012345', # Barcode extension (if it is aplicable)
319 -umzn => 10, # Top limit of the finished bar
320 -lmzn => 10, # Bottom limit of the finished bar
321 -zone => 15, # Bars size
322 -quzn => 0, # Space destinated for legend
323 -ofwt => 0.01, # Bars width
324 -fnsz => 8, # Font size
328 ( my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label ) =
329 C4::Barcodes::PrinterConfig::getLabelPosition( $label, $pdf,
330 $page, $gfx, $text, $tr, $pageType );
332 # Assigns a barcodes to $gfx
333 $gfx->barcode( $barcode, $x, $y,
334 ( 72 / $labelConfig{'systemDpi'} ) );
336 # Assigns the additional information to the barcode (Legend)
337 $text->translate( $x - 48, $y - 22 );
338 if ($text_under_label) {
339 $text->text($text_under_label);
342 $text->text( substr $title, 0, 30 );
343 $text->translate( $x - 48, $y - 29 );
345 #$text->text(substr $author, 0, 30);
346 $text->text( substr $author, 0, 30 );
350 # Writes the objects added in $gfx to $page
351 $pdf->finishobjects( $page, $gfx, $text );
353 # Save changes to the PDF
356 # Close the conection with the PDF file
360 print $cgi->redirect(
361 "/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
365 # Rollback and shows the error legend
366 print $cgi->redirect("/cgi-bin/koha/barcodes/barcodes.pl?error=1");
372 barcodesGenerator( $from, $to, $rangeType, $individualCodes,