#!/usr/bin/perl -w
#
# This script is used to remove all duplicate barcodes.
#
use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
    floor
);


my %options = ();
getopts("c:",\%options);
my $configFile = $options{c};
if (!$configFile || ! -f $configFile) {
    print "Usage: $0 -c CONFIG_FILE\n";
    exit 1;
}
#
#sudo  /www/opals/script/addDecimal2price -c /etc/opals/conf/wswhe_fane
#
my $config = loadConfig($configFile);
my $dbh = makeConnection($config);
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# fix -- update barcodes
my $defaultPrice="15.00";
my $sth = $dbh->prepare(<<_SQL_);

select   * 
from opl_loan
where dateReturn regexp '-00'
_SQL_

    $sth->execute;
    while (my $loan = $sth->fetchrow_hashref) {
        fixDateReturn($dbh,$loan);
        printf "dateReturn:%s -- idloan:%s;BC:%s\n",$loan->{'dateReturn'},  $loan->{'idloan'} , $loan->{'barcode'}; 
        
    }
    $sth->finish;

############################################################

############################################################
sub fixDateReturn{
    my ($dbh,$loan)=@_;
    my $dateReturn=getDateFromODL($dbh,$loan);
    my $sth=$dbh->prepare("update opl_loan set dateReturn=if(?>now(),now(),?) where idloan=?");
    if(!defined $dateReturn){
        $dateReturn =getDateLoanBeforDateDue($dbh,$loan);
    }
    if(!defined $dateReturn){
        $dateReturn =$loan->{'dateDue'};
    }
    $sth->execute($dateReturn,$dateReturn,$loan->{'idloan'});
    $sth->finish;

}

sub getDateFromODL{
   my ($dbh,$loan)=@_;
   my $dateReturn=undef;
   my $sth=$dbh->prepare("select ondate from opl_odl where idloan=? limit 1");
   $sth->execute($loan->{'idloan'});
   if(my $rec =$sth->fetchrow_hashref){
      $dateReturn=$rec->{'ondate'}; 
   }
   $sth->finish;
   return  $dateReturn;
}

sub getDateLoanBeforDateDue{
    my ($dbh,$loan)=@_;
    my $dateReturn=undef;
    my $sth=$dbh->prepare("select dateLoan from opl_loan where barcode= ? && idloan >? && dateLoan <? order by idloan ASC limit 1");
    $sth->execute($loan->{'barcode'},$loan->{'idloan'},$loan->{'dateDue'});
    if(my $rec =$sth->fetchrow_hashref){
      $dateReturn=$rec->{'dateLoan'}; 
    }
    $sth->finish;
    return  $dateReturn;

}



############################################################
sub makeConnection {
    my ($config) = @_;
    if (!$config) {
        return;
    }
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);

    $db_driver   = $config->{'db_driver'} || 'mysql';
    $db_name     = $config->{'db_name'};
    $db_host     = $config->{'db_host'};
    $db_port     = $config->{'db_port'}   || '3306';
    $db_user     = $config->{'db_user'};
    $db_password = $config->{'db_password'};

    my $dsn = "dbi:$db_driver:$db_name:$db_host:$db_port";

    return DBI->connect($dsn, $db_user, $db_password);
}


############################################################
sub loadConfig {
    my ($configFile) = @_;
#    print "Enter the config filename of Opals: ";
#    $configFile = <STDIN>;
    my $config = {};

    open CONF, $configFile || die "Cannot open file $configFile";
    while (<CONF>) {
        chomp;
        s/#.*//;                # remove comments
        next if /^\s*$/;        # ignore blank lines

        if (/^\s*(\w+)\s*=\s*(.*?)\s*$/) {
            $config->{$1} = $2;
        }
    }
    close CONF;
    
    return $config;
}
############################################################


exit 0;

