#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::StatRate;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::DB::ItemCollection;

use Common::DB::Item;
use base 'Common::DB::Item';

use constant kTable => 'stat_rate';
use constant kDB    => Common::DB::Item::kCommonDB();

use constant kUSMechStatRateType => 1;
use constant kCAMechStatRateType => 2;
use constant kUSRingStatRateType => 3;
use constant kCARingStatRateType => 4;

# this only returns one item
#
sub GetByDate {
    my ( $class, $date, $rateTypeID ) = @_;
    return undef unless ($date);

    my $dbo = Common::RSApp::GetCommonDB();
    my $sql =
        "SELECT * FROM stat_rate WHERE stat_rate_type_id = $rateTypeID AND date_effective <= "
      . $dbo->DBQuote($date)
      . " ORDER BY date_effective DESC LIMIT 1";

    my $collection = $class->SUPER::GetAll($sql);

    if ( defined $collection && $collection->hasNext() ) {
        return $collection->next();
    } else {
        return undef;
    }
}

sub GetAll {
    my ($class) = @_;

    my $order = "stat_rate_type_id, date_effective DESC";

    my $sql = "SELECT * FROM stat_rate ORDER BY $order";

    return $class->SUPER::GetAll($sql);
}

sub GetRatesCA {
    my ($class) = @_;

    my $order = "date_effective DESC";

    my $sql = "SELECT * FROM stat_rate WHERE stat_rate_type_id = " . kCAMechStatRateType . " ORDER BY $order";

    return $class->SUPER::GetAll($sql);
}

sub GetRatesUS {
    my ($class) = @_;

    my $order = "date_effective DESC";

    my $sql =
        "SELECT * FROM stat_rate WHERE (stat_rate_type_id = "
      . kUSMechStatRateType
      . " OR stat_rate_type_id = "
      . kUSRingStatRateType
      . ") ORDER BY $order";

    return $class->SUPER::GetAll($sql);
}

sub GetByType {
    my ( $class, $type ) = @_;

    my $order = "date_effective DESC";

    my $sql = "SELECT * FROM stat_rate WHERE stat_rate_type_id = " . $type . " order by $order";

    return $class->SUPER::GetAll($sql);
}

# This returns a reference to hash that maps stat_rate_id to a string of "startyear-endyear".
#
sub GetRatePeriodMap {
    my $class = @_;

    my $currentYear = 1900 + (localtime)[5];

    my $statRates = RPS::DB::Item::StatRate->GetAll();
    my $lastTypeID;
    my $nextYear;
    my %ratePeriodMap;

    while ( my $statRate = $statRates->next() ) {
        my ( $year, $month, $day ) = split( '-', $statRate->date_effective );
        if ( !$lastTypeID || $statRate->stat_rate_type_id != $lastTypeID ) {

            # Starting a new type, reset the next year.
            #
            $nextYear   = $currentYear;
            $lastTypeID = $statRate->stat_rate_type_id;
        }
        my $range = "$year-$nextYear";
        $ratePeriodMap{ $statRate->stat_rate_id } = $range;
        $nextYear = $year - 1;
    }

    return \%ratePeriodMap;
}
1;
