#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Sony::v24;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;
use Common::Country;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;
use base 'RPS::Import::Sony::v11';

sub _fieldMap {
    {
        # header
        #
        labelName        => 'A6',
        dateBegin        => 'E3',
        dateEnd          => 'F3',

        # detail
        #
        serviceID        => 'N',
        countryCode      => 'J',
        productType      => 'D',
        formatType       => 'K',
        artistName       => 'C',
        serviceProductID => 'A',
        upc              => 'I',
        albumName        => 'B',
        isrc             => 'H',
        trackName        => 'B',
        units            => 'P',
        currencyCode     => 'U',
        price            => 'U',

    }
}

sub _unverifiedFieldMap {
    {
        altDateBegin => 'F',
        altDateEnd   => 'G',
    }
}

sub _headerIdentifier {
    ( 'upc' => 'Barcode (UPC)' )
}

sub currencyCode { 'AUD' }

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID');

    # Some of the service names have an "_AU" suffix
    #
    $service =~ s/(.*)_AU$/$1/;

    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };

    return $serviceID if( $serviceID );

    return $self->handleError("Unknown service '$service'", RPS::DB::Item::SaleImportError::kErrorService );
}

sub price {
    my $price = abs( shift->SUPER::price() );
    return $price;
}

sub unitPrice { abs( shift->SUPER::unitPrice ) }

# _processSheet is used to process a tab from the spreadsheet.
# Here, we're extracting the sale date and label information
# from the cells called out in the template.
#
sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    # Try to find the date information by examining the header area
    # at the top of the sheet.  The date information can be in one of
    # two places, even though the main header (the one we use for file
    # identification) is the same.
    #
    my $foundAlternateDate;
    foreach my $line(@$sheet) {
        $self->{line} = $line;


        my $labelName = $self->_getByFieldName('labelName');
        $self->{_labelName} = $labelName;

        # Check for alternate date location first
        if( !$foundAlternateDate )
        {
            my $sd = $self->_getByFieldName('altDateBegin');
            my $ed = $self->_getByFieldName('altDateEnd');

            $foundAlternateDate = 1 if( $sd =~ /Period Start Date/i );
        }
        else
        {
            my $startDate = $self->_getByFieldName('altDateBegin');
            my $endDate = $self->_getByFieldName('altDateEnd');

            $startDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $startDate ) if( $startDate =~ /^\d{5}$/ );
            $endDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $endDate ) if( $endDate =~ /^\d{5}$/ );

            my($dateBegin, $dateEnd) = $self->_getDates( date_begin => $startDate, date_end => $endDate );
            $self->{_startDate} = $dateBegin;
            $self->{_endDate}   = $dateEnd;
            last;
        }
    }

    if( !$foundAlternateDate )
    {
        # No alternate date found, look in regular spot
        #
        foreach my $line(@$sheet) {
            $self->{line} = $line;


            my $labelName = $self->_getByFieldName('labelName');
            $self->{_labelName} = $labelName;

            my $startDate = $self->_getByFieldName('dateBegin');
            my $endDate   = $self->_getByFieldName('dateEnd');

            my($dateBegin, $dateEnd) = $self->_getDates( date_begin => $startDate, date_end => $endDate );

            $self->{_startDate} = $dateBegin;
            $self->{_endDate}   = $dateEnd;
        }

    }

    # Now process the detail lines
    #
    return $self->RPS::Import::Importer::_processSheet( $sheet, $sheetnum );
}


###
1;# Play nicely.
###
