package BookPub::Import::Importer::Scribd::Version4;

use strict;
use Date::Calc qw(Add_Delta_Days);

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

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer';

sub _headerIdentifier { ( isbn => 'Main Product ID#' ) };

sub _useIsSaleRec  { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _fieldMap {
	my $self = shift;

	my $map = {
		'sales report' => {
		    rPriceType         => 'H',
		    currencyCode       => 'I',
		    conversionRate     => 'BP',
		    countryCode        => 'AL',
		    dateBegin          => 'O',
		    rProductType       => 'AD',
            isbn               => 'U',
            rTitle             => 'X',
            rAuthor            => 'Y',
            rImprint           => 'AC',
            rUnits             => 'AH',
            rSales             => 'AF',
            rReturns           => 'AG',
            rListPrice         => 'BQ',
            rListPriceCurrency => 'AO',
            rPurchasePrice     => 'AM',
            rDiscount          => 'AP',
            rRevenue           => 'BC',
        },
	};

    # ignore all sheets except 'order details'
	my $sheet = lc( $self->sheetname ) || return {};
	$sheet =~ /sales report/ ? return $map->{$sheet} : return {};
} 

sub _unverifiedFieldMap {{
    transactionID       	=> 'P', 
}}

#   This field map will only be used for the Tax Report tab
#
my %taxFieldMap = (
    state                   => 9,
    zip                     => 13,
    transactionID           => 28,
);


sub purchaseType    { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType     { BookPub::DB::Item::Sale::kProductTypeEBook }

my %taxData;

sub _preProcess {
   my $self = shift;
   my %args = @_;

   Log->info( "Starting _preProcess" );
    $self->SUPER::_preProcess(%args);

    my $sheetnum = 0;

    foreach my $sheet(@{ $args{sheets} }) {
      my $sheetname = $args{sheet_names}->[$sheetnum];
        $sheetnum++;
        # We need to go through the Tax Report tab and store some of that data in a hash 
        # so that we can access it while processing the Details tab.
        if ($sheetname eq 'Tax Report')
        {
            my $foundHeader = 0;
            foreach my $line (@$sheet) {
                
                if ($line->[0] eq 'Report ID')
                {
                    $foundHeader = 1;
                    next;
                }
                elsif ($foundHeader == 0)
                {
                    next;
                }
                
                my $transactionID = $line->[$taxFieldMap{transactionID}];
                
                if (!$transactionID && $foundHeader == 1) {
                    last;
                }
                
                # Grab the data and store it
                $taxData{$transactionID}{state} = $line->[$taxFieldMap{state}];
                $taxData{$transactionID}{zip}   = $line->[$taxFieldMap{zip}];                    
            }
        }
   }
   
}

sub rState {
    my $self = shift;
    my $transactionID = $self->_getByFieldName( 'transactionID' );
    my $state = $taxData{$transactionID}{state};

    return $state;
}

sub rPostalCode {
    my $self = shift;
    my $transactionID = $self->_getByFieldName( 'transactionID' );
    my $zip = $taxData{$transactionID}{zip};

    return $zip;
}

sub _getDateBegin {
    my $self = shift;
    my $dateBegin =  $self->_getByFieldName( 'dateBegin' );
    if ($dateBegin =~ /(\d{4})(\d{2})(\d{2})/) {
        $dateBegin = $1 . "-" . $2 . "-" . $3;
    }
    
    return $dateBegin;
}

sub productType {
	my $self = shift;
	my $type = lc($self->rProductType) || return;
	
	if ($type eq 'other' || $type eq 'e101' || $type eq 'e107')	{
	    $type = BookPub::DB::Item::Sale::kProductTypeEBook;
	}	elsif ($type eq 'a103' || $type eq 'a106') {
	    $type = BookPub::DB::Item::Sale::kProductTypeAudioBook;
	}
	
	return $type;
}

sub priceType {
    my $self = shift;
    
    my $clientID = Common::RSApp::GetClientID();
    
    if ($clientID == 318) { # Macmillan US
        my $countryCode = $self->countryCode();
        
        if ($countryCode eq 'US' || $countryCode eq 'CA') {
            return 'agency';
        } else {
            return 'rrp';
        }
    } else {		
        my $type = lc($self->rPriceType) || return;
        
        if ($type == 41 || $type == 43)	{
            $type = 'agency';
        }	elsif ($type == 6) {
            $type = 'rrp'; 
        }		
        return $type;
    }
}

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