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 #grabs a callnumber for the specified barcode
139 my $query = "select dewey from items,biblioitems where items.biblionumber=biblioitems.biblionumber and items.barcode=?";
140 my $dbh = C4::Context->dbh;
141 my $sth = $dbh->prepare($query);
142 $sth->execute($barcode);
143 my ($callnum) = $sth->fetchrow_array();
144 warn "Call number is:".$barcode;
147 # Takes inventary codes from database and if they are between
148 # the interval specify by parameters, it generates the correspond barcodes
149 sub barcodesGenerator {
150 my ($from, $to, $rangeType, $individualCodes,$text_under_label) = @_;
151 # Returns a database handler
152 my $dbh = C4::Context->dbh;
153 # Create the query to database
154 # Assigns a temporary filename for the pdf file
155 my $tmpFileName = &assingFilename($from, $to);
156 # warn "range type: ".$rangeType;
157 if ($rangeType eq 'continuous') {
158 # Set the temp directory for pdf´s files
159 if (!defined($ENV{'TEMP'})) {
160 $ENV{'TEMP'} = '/tmp/';
162 $tmpFileName = $ENV{'TEMP'}.$tmpFileName;
163 # Creates a PDF object
164 my $pdf = PDF::API2->new(-file => $tmpFileName);
165 # Set the positions where barcodes are going to be placed
166 C4::Barcodes::PrinterConfig::setPositionsForX($labelConfig{'marginLeft'}, $labelConfig{'labelWidth'}, $labelConfig{'columns'}, $labelConfig{'pageType'});
167 C4::Barcodes::PrinterConfig::setPositionsForY($labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'}, $labelConfig{'rows'}, $labelConfig{'pageType'});
168 # Creates a font object
169 my $tr = $pdf->corefont('Helvetica-Bold');
171 my ($page, $gfx, $text);
172 for (my $code=$from; $code<=$to; $code++) {
173 # Generetase checksum
174 my $codeC = &checksum($code);
175 # Generate the corresponde barcode to $code
176 # warn "Code is :-->".$codeC."<--";
177 my $barcode = $pdf->barcode(-font => $tr, # The font object to use
178 -type => 'ean128', # Standard of codification
179 -code => $codeC, # Text to codify
180 -extn => '012345', # Barcode extension (if it is aplicable)
181 -umzn => 10, # Top limit of the finished bar
182 -lmzn => 10, # Bottom limit of the finished bar
183 -zone => 15, # Bars size
184 -quzn => 0, # Space destinated for legend
185 -ofwt => 0.01, # Bars width
186 -fnsz => 8, # Font size
190 (my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label) = C4::Barcodes::PrinterConfig::getLabelPosition(
198 # Assigns a barcodes to $gfx
199 $gfx->barcode($barcode, $x, $y , (72/$labelConfig{'systemDpi'}));
200 # Assigns the additional information to the barcode (Legend)
201 $text->translate($x - 48, $y - 22);
202 warn "code is ".$codeC;
203 if ($text_under_label) {
204 $text->text($text_under_label);
206 $text->text(getCallnum($code));
209 # Writes the objects added in $gfx to $page
210 $pdf->finishobjects($page,$gfx, $text);
211 # Save changes to the PDF
213 # Close the conection with the PDF file
216 print $cgi->redirect("/cgi-bin/koha/barcodes/pdfViewer.pl?tmpFileName=$tmpFileName");
219 if ($individualCodes ne "") {
220 $rangeCondition = "AND (I.barcode IN " . $individualCodes . ")";
222 $rangeCondition = "AND (I.barcode >= " . $from . " AND I.barcode <=" . $to . " )";
224 warn "above the query";
225 my $query = "SELECT CONCAT('$numbersystem',REPEAT('0',((12 - LENGTH('$numbersystem')) - LENGTH(I.barcode))), I.barcode) AS Codigo, I.dewey as dewey B.title, B.author FROM biblio B, items I WHERE (I.biblionumber = B.biblioNumber ) " .$rangeCondition. " AND (I.barcode <> 'FALTA') ORDER BY Codigo";
228 my $sth = $dbh->prepare($query);
231 if ($sth->rows) { # There are inventary codes
232 # Set the temp directory for pdf´s files
233 if (!defined($ENV{'TEMP'})) {
234 $ENV{'TEMP'} = '/tmp/';
236 # Assigns a temporary filename for the pdf file
237 my $tmpFileName = &assingFilename($from, $to);
238 $tmpFileName = $ENV{'TEMP'}.$tmpFileName;
239 # Creates a PDF object
240 my $pdf = PDF::API2->new(-file => $tmpFileName);
241 # Set the positions where barcodes are going to be placed
242 C4::Barcodes::PrinterConfig::setPositionsForX($labelConfig{'marginLeft'}, $labelConfig{'labelWidth'}, $labelConfig{'columns'}, $labelConfig{'pageType'});
243 C4::Barcodes::PrinterConfig::setPositionsForY($labelConfig{'marginBottom'}, $labelConfig{'labelHeigth'}, $labelConfig{'rows'}, $labelConfig{'pageType'});
244 # Creates a font object
245 my $tr = $pdf->corefont('Helvetica-Bold');
247 my ($page, $gfx, $text);
248 while (my ($code,$dewey,$title,$author) = $sth->fetchrow_array) {
249 # Generetase checksum
250 $code = &checksum($code);
251 # Generate the corresponde barcode to $code
252 my $barcode = $pdf->barcode(-font => $tr, # The font object to use
253 -type => 'ean13', # Standard of codification
254 -code => $code, # Text to codify
255 -extn => '012345', # Barcode extension (if it is aplicable)
256 -umzn => 10, # Top limit of the finished bar
257 -lmzn => 10, # Bottom limit of the finished bar
258 -zone => 15, # Bars size
259 -quzn => 0, # Space destinated for legend
260 -ofwt => 0.01, # Bars width
261 -fnsz => 8, # Font size
265 (my $x, my $y, $pdf, $page, $gfx, $text, $tr, $label) = C4::Barcodes::PrinterConfig::getLabelPosition(
273 # Assigns a barcodes to $gfx
274 $gfx->barcode($barcode, $x, $y , (72/$labelConfig{'systemDpi'}));
275 # Assigns the additional information to the barcode (Legend)
276 $text->translate($x - 48, $y - 22);
277 if ($text_under_label) {
278 $text->text($text_under_label);
280 warn "here a dewey:".$dewey;
281 $text->text(substr $dewey, 0, 30);
282 $text->translate($x - 48, $y - 29);
283 #$text->text(substr $author, 0, 30);
286 # Writes the objects added in $gfx to $page
287 $pdf->finishobjects($page,$gfx, $text);
288 # Save changes to the PDF
290 # Close the conection with the PDF file
293 print $cgi->redirect("/cgi-bin/koha/tools/pdfViewer.pl?tmpFileName=$tmpFileName");
295 # Rollback and shows the error legend
296 print $cgi->redirect("/cgi-bin/koha/tools/barcodes.pl?error=1");
302 barcodesGenerator($from, $to, $rangeType, $individualCodes,$text_under_label);