package Opals::Eq_BarcodeMgmt;

require Exporter;
@ISA       = qw(Exporter);

@EXPORT_OK = qw(

    eq_bcm_validateBc
    eq_bcm_validateFollettBarcode
    eq_bcm_validateSpectrumBarcode
    eq_bcm_validateLeadingZeroBc
    eq_bcm_validateSagebrushBarcode
);

# Version number
$VERSION   = 0.01;      

use strict;
use Encode;

# return barcode depends on system preference setting  'barcodeType'
#  1: follett
#  2: spectrum
#  3: leading zero
#  4: Sagebrush
sub eq_bcm_validateBc{
   my($dbh,$barcode,$barcodeType) =@_;
   if($barcodeType eq '1'){#follett
        $barcode  = eq_bcm_validateFollettBarcode($dbh,$barcode );
    }
    elsif($barcodeType eq '2'){#spectrum
        $barcode  = eq_bcm_validateSpectrumBarcode($dbh,$barcode );
    }
    elsif($barcodeType eq '3'){#leading zero
        $barcode  = eq_bcm_validateLeadingZeroBc($dbh,$barcode );
    }
    elsif($barcodeType eq '4'){#Sagebrush
        $barcode  = eq_bcm_validateSagebrushBarcode($dbh,$barcode );
    }
    return $barcode;
   
}
#########################################################################
# Tue, Aug 14, 2012 @ 08:55:03 EDT
#  FORMAT: (0nn)nnnn(0nn)
sub eq_bcm_validateSagebrushBarcode{
    my ($dbh,$bc) =@_;
    $bc =~ s/ +//g;
    my $sth = $dbh->prepare("select barcode from eq_items where barcode=?");
    
    $sth->execute($bc) || return;
    my ($retBc) = $sth->fetchrow_array;
    if(!$retBc ||$retBc eq ''){
	$retBc =$bc;
        if($retBc =~ m/^0/gi){
            $retBc =~ s/^0+//g;
            $retBc =~ s/0\d\d$//g;
            $sth->execute($retBc) || return;
            ($retBc) = $sth->fetchrow_array;
        }
    }
    if(!$retBc ||$retBc eq ''){
        $retBc=$bc;
    }
    $sth->finish;
    return $retBc;
 
}
#########################################################################
# Wed, Sep 12, 2012 @ 08:36:19 EDT
# FORMAT: length:14
#         1st char:  TYPE 
#           (For Code 39 Mod 10 : 3 = library books, 4 = textbooks, and 2 = patrons OR
#            For Follett Classic: T = library books, X = textbooks, and P = patrons)
#         4   chars: INDICATOR LOCATION 
#         8   chars: CODE ITEM OR PATRON (these are numeric) 
#         1   char:  NUMBER CHECK DIGIT
#            (For Mod 10 this character is numeric (digits 0-9).
#             For Mod 43 this character is alphanumeric  (characters A-Z 
#                  plus the symbols “-“, “.”, blank space,“$”, ”/”, “+”, “%”)
#
sub eq_bcm_validateFollettBarcode{
     my ($dbh,$bc) =@_;
     $bc =~ s/ +//g;

    my $sth = $dbh->prepare("select barcode from eq_items where barcode=?");
    $sth->execute($bc) || return;
    my ($retBc) = $sth->fetchrow_array;

    if(!$retBc ||$retBc eq ''){
        my $fBc =  $bc;
        if($fBc =~ s/^3([\w]{4}){0,1}([\w]{8})[A-Z\-\.\s\$\/\+\%]{0,1}//g ){
            $fBc=$2;
            $fBc=~ s/^[0]+//g;
            $sth->execute($fBc) || return;
            ($retBc) = $sth->fetchrow_array;       
        }
        else{
            return eq_bcm_validateFollettBarcode_classic($dbh,$bc);
        }
    }
    if(!$retBc ||$retBc eq ''){
        $retBc=$bc;
    }
    $sth->finish;
    return $retBc;
   
}


#########################################################################
# Added on 2007-09-13
#
# Move from Circulation module to here on
#           Tue, Aug 14, 2012 @ 08:55:03 EDT
sub eq_bcm_validateFollettBarcode_classic{
    my ($dbh,$bc) =@_;
    $bc =~ s/ +//g;
   my $sth = $dbh->prepare("select barcode from eq_items where barcode=?");
    
    $sth->execute($bc) || return;
    my ($retBc) = $sth->fetchrow_array;
    if(!$retBc ||$retBc eq ''){
        my $fBc =folletize($bc);
        $sth->execute($fBc) || return;
        ($retBc) = $sth->fetchrow_array;

    }
    if(!$retBc ||$retBc eq ''){
        my $fBc =deFolletize($bc);
        $sth->execute($fBc) || return;
        ($retBc) = $sth->fetchrow_array;

    }
    if(!$retBc ||$retBc eq ''){
        $retBc=$bc;
    }
    $sth->finish;
    return $retBc;
}

#-----------------------------------------------------
sub folletize{
    my ($bc)=@_;

    $bc = deFolletize($bc);
    $bc = 'T' . substr("0000000$bc", -7);

return $bc;
}
#-----------------------------------------------------
# added on 2007-10-05

sub deFolletize{
    my ($bc)=@_;
    if($bc =~ m/^T/gi){
        $bc =~ s/^T//gi;
        $bc =~ s/^0+//g;
    }
    elsif($bc =~ m/^([\d]{7})0\d/g){
        $bc =$1;
        $bc =~ s/^0+//g;
    }
    return $bc;
}


#########################################################################
# Added on Fri, Nov 09, 2007 @ 09:11:17 EST
# Move from Circulation module to here on
#           Tue, Aug 14, 2012 @ 08:55:03 EDT
#Tue, Aug 14, 2012 @ 08:55:03 EDT
#
sub eq_bcm_validateSpectrumBarcode{
    my ($dbh,$bc) =@_;
    $bc =~ s/ +//g;
   
    my $sth = $dbh->prepare("select barcode from eq_items where barcode=?");
    
    $sth->execute($bc) || return;
    my ($retBc) = $sth->fetchrow_array;
    if(!$retBc ||$retBc eq ''){
        $retBc =deSpectrumize($bc);
        $sth->execute($retBc) || return;
        ($retBc) = $sth->fetchrow_array;

    }
    if(!$retBc ||$retBc eq ''){
        $retBc=$bc;
    }
    $sth->finish;
    return $retBc;
    
}
#-----------------------------------------------------

sub deSpectrumize{
    my ($bc)=@_;
    my $bcLen=length($bc);
    if($bcLen ==14 || $bcLen ==13){
        $bc = substr $bc,5,8;
        $bc =~ s/^0+//g;
    }
    elsif($bcLen ==8 || $bcLen ==7 ){
        $bc = substr $bc,0,$bcLen -1;
        $bc =~ s/^0+//g;
    }   
    elsif($bcLen <7 ){
        $bc = sprintf("%08d", $bc);
    }

    return $bc;
}

#########################################################################
# Added on  Tue, Sep 01, 2009 @ 11:10:31 EDT
#
# Move from Circulation module to here on
#           Tue, Aug 14, 2012 @ 08:55:03 EDT
sub eq_bcm_validateLeadingZeroBc{
    my ($dbh,$bc) =@_;
    $bc =~ s/ +//g;
   
    my $sth = $dbh->prepare("select barcode from eq_items where barcode=?");
    
    $sth->execute($bc) || return;
    my ($retBc) = $sth->fetchrow_array;
    if(!$retBc ||$retBc eq ''){
        $retBc =$bc;
        $retBc =~ s/^0+//g;
        $sth->execute($retBc) || return;
        ($retBc) = $sth->fetchrow_array;

    }
    if(!$retBc ||$retBc eq ''){
        $retBc=$bc;
    }
    $sth->finish;
    return $retBc;
    
}


1;

