#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Admin::Mcps::ExportUplift;
use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::RSApp;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::McpsExportUplift;

use Common::FormObject;
use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    my %properties;
    if ( $args{_dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{_dbItem} );
    } elsif ( $args{mcpsExportUpliftID} ) {
        $self->_propertiesFromDB( \%properties, $args{mcpsExportUpliftID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );
    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $exportUpliftID ) = @_;

    my $dbItem = RPS::DB::Item::McpsExportUplift->Lookup( mcps_export_uplift_id => $exportUpliftID );
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;

    $self->{_inDB} = 1;

    $hrProperties->{mcps_export_uplift_id} = $dbItem->mcps_export_uplift_id;
    $hrProperties->{country_code}          = $dbItem->country_code;
    $hrProperties->{product_type_id}       = $dbItem->product_type_id;
    $hrProperties->{rate}                  = $dbItem->rate;
}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    $self->{McpsExportUpliftID} = Common::FormObject::Scalar::Integer->new( value => $props->{mcps_export_uplift_id} );
    $self->{CountryCode} = Common::FormObject::Scalar->new( value => $props->{country_code} );
    $self->{ProductTypeID} = Common::FormObject::Scalar::Integer->new( value => $props->{product_type_id} );
    $self->{Rate} = Common::FormObject::Scalar::Decimal->new( value => $props->{rate}, precision => '3.2' );

    $self->{Delete} = Common::FormObject::Scalar::Boolean->new();

    return $self;
}

sub _initWithDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub validate {
    my ($self) = @_;

    if ( $self->Delete() ) {
        if ( $self->hasDependency() ) {
            $self->{Delete}->setError('has_dependency');
            return 0;
        }

        # else, no need to validate any properties
        return 1;
    }

    foreach my $field (qw(CountryCode ProductTypeID Rate)) {
        if ( $self->$field() ) {
            $self->{_hasData} = 1;
        }
    }
    if ( $self->{_hasData} ) {
        my $valid = $self->SUPER::validate();

        if ( !defined $self->CountryCode() ) {
            $self->{CountryCode}->setError(Common::FormObject::kErrFieldBlank);
            $valid = 0;
        }
        if ( !defined $self->ProductTypeID() ) {
            $self->{ProductTypeID}->setError(Common::FormObject::kErrFieldBlank);
            $valid = 0;
        }
        if ( !defined $self->Rate() ) {
            $self->{Rate}->setError(Common::FormObject::kErrFieldBlank);
            $valid = 0;
        }

        # I'm not exactly sure what the real limit is for the rate field,
        # but anything over a thousand would just be ridiculous.
        if ( $self->Rate() > 999.99 ) {
            $self->{Rate}->setError(Common::FormObject::kErrFieldInvalid);
            $valid = 0;
        }

        return $valid;
    } else {
        return 1;
    }
}

sub save {
    my ($self) = @_;

    if ( $self->Delete() ) {
        $self->delete();
        return;
    }

    return unless $self->{_hasData};

    my $dbObj;

    if ( $self->McpsExportUpliftID() ) {
        $dbObj = RPS::DB::Item::McpsExportUplift->Lookup( mcps_export_uplift_id => $self->McpsExportUpliftID() );
    } else {
        $dbObj = RPS::DB::Item::McpsExportUplift->Create();
    }

    # set fields here
    #
    $dbObj->country_code( $self->CountryCode() );
    $dbObj->product_type_id( $self->ProductTypeID() );
    $dbObj->rate( $self->Rate() );

    # save db item
    #
    $dbObj->save();

    # reload object
    #
    $self->_init( _dbItem => $dbObj );
}

sub setDependency {
    my $self = shift;
    $self->{HasDependency} = 1;
}

sub hasDependency {
    my $self = shift;
    return $self->{HasDependency} ? 1 : 0;
}

sub delete {
    my $self = shift;

    if ( $self->{_inDB} ) {
        my $myDBObj = RPS::DB::Item::McpsExportUplift->Lookup( mcps_export_uplift_id => $self->McpsExportUpliftID() );
        $myDBObj->delete();
    }
    return 1;
}

1;

