#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package HarmoniaMundiArtistPayee;

use strict;
use warnings;

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

use Importer;
use base 'Importer';

my @gColumns = ( {
        fieldName => 'client_account_id',
        saveAs    => [ 'client_account_id', 'client_payee_id' ],
        lookup    => 1,
    },
    {
        fieldName => 'name',
        required  => 1,
    },
    {
        fieldName => 'street_address',
    },
    {
        fieldName => 'city',
    },
    {
        fieldName => 'state_province',
    },
    {
        fieldName => 'postal_code',
    },
    {
        fieldName  => 'country_code',
        preProcess => 'deriveCountry',
    },
    {
        fieldName => 'phone_number',
    },
    {
        fieldName => 'fax_number',
    },
    {
        fieldName  => 'comments',
        preProcess => 'addComment',
    },
);

sub _columns {
    return \@gColumns;
}

sub addComment {
    my ( $self, $contact ) = @_;

    return "Contact: $contact";
}

sub deriveCountry {
    my ( $self, $countryName ) = @_;

    return RPS::CountryList::CountryCodeFor($countryName);
}

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

    my $valid = $self->SUPER::validate($fieldConfig);

    return $valid;
}

sub lookup {
    my ( $self, $fieldConfig ) = @_;

    my $itemID;

    my $lookups = $self->_getLookups($fieldConfig);
    foreach my $lookupValue (@$lookups) {

        # Does this payee already exist?
        #
        my $coll = RPS::DB::Item::ArtistPayee->Match($lookupValue);
        if ( defined $coll && $coll->size == 1 ) {
            my $dbItem = $coll->next();
            $itemID = $dbItem->artist_payee_id;

            $self->found( $fieldConfig, $itemID );
        }
    }

    return $itemID;
}

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

    return $self->SUPER::save(
        dataRow => $fieldValues,
        class   => "RPS::DB::Item::ArtistPayee",
        id      => "artist_payee_id"
    );
}

1;

