+=head2 GetBarcodeData
+
+=over 4
+
+Parse labels_conf.formatstring value
+(one value of the csv, which has already been split)
+and return string from koha tables or MARC record.
+
+=back
+
+=cut
+
+sub GetBarcodeData {
+ my ( $f, $item, $record ) = @_;
+ my $kohatables = &_descKohaTables();
+ my $datastring = '';
+ my $match_kohatable = join(
+ '|',
+ (
+ @{ $kohatables->{biblio} },
+ @{ $kohatables->{biblioitems} },
+ @{ $kohatables->{items} }
+ )
+ );
+ while ($f) {
+ $f =~ s/^\s?//;
+ if ( $f =~ /^'(.*)'.*/ ) {
+ # single quotes indicate a static text string.
+ $datastring .= $1;
+ $f = $';
+ }
+ elsif ( $f =~ /^($match_kohatable).*/ ) {
+ $datastring .= $item->{$f};
+ $f = $';
+ }
+ elsif ( $f =~ /^([0-9a-z]{3})(\w)(\W?).*?/ ) {
+ my ($field,$subf,$ws) = ($1,$2,$3);
+ my $subf_data;
+ my ($itemtag, $itemsubfieldcode) = &GetMarcFromKohaField("items.itemnumber",'');
+ my @marcfield = $record->field($field);
+ if(@marcfield) {
+ if($field eq $itemtag) { # item-level data, we need to get the right item.
+ foreach my $itemfield (@marcfield) {
+ if ( $itemfield->subfield($itemsubfieldcode) eq $item->{'itemnumber'} ) {
+ $datastring .= $itemfield->subfield($subf ) . $ws;
+ last;
+ }
+ }
+ } else { # bib-level data, we'll take the first matching tag/subfield.
+ $datastring .= $marcfield[0]->subfield($subf) . $ws ;
+ }
+ }
+ $f = $';
+ }
+ else {
+ warn "failed to parse label formatstring: $f";
+ last; # Failed to match
+ }
+ }
+ return $datastring;
+}
+
+=head2 descKohaTables
+
+Return a hashref of an array of hashes,
+with name,type keys.
+
+=cut
+
+sub _descKohaTables {
+ my $dbh = C4::Context->dbh();
+ my $kohatables;
+ for my $table ( 'biblio','biblioitems','items' ) {
+ my $sth = $dbh->column_info(undef,undef,$table,'%');
+ while (my $info = $sth->fetchrow_hashref()){
+ push @{$kohatables->{$table}} , $info->{'COLUMN_NAME'} ;
+ }
+ $sth->finish;
+ }
+ return $kohatables;
+}
+